]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/re-builder.el
(unload-feature): Handle (t . SYMBOL) entries in load history.
[gnu-emacs] / lisp / emacs-lisp / re-builder.el
1 ;;; re-builder.el --- building Regexps with visual feedback
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: Detlev Zundel <dzu@gnu.org>
7 ;; Keywords: matching, lisp, tools
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; When I have to come up with regular expressions that are more
29 ;; complex than simple string matchers, especially if they contain sub
30 ;; expressions, I find myself spending quite some time in the
31 ;; `development cycle'. `re-builder' aims to shorten this time span
32 ;; so I can get on with the more interesting bits.
33
34 ;; With it you can have immediate visual feedback about how well the
35 ;; regexp behaves to your expectations on the intended data.
36
37 ;; When called up `re-builder' attaches itself to the current buffer
38 ;; which becomes its target buffer, where all the matching is done.
39 ;; The active window is split so you have a view on the data while
40 ;; authoring the RE. If the edited expression is valid the matches in
41 ;; the target buffer are marked automatically with colored overlays
42 ;; (for non-color displays see below) giving you feedback over the
43 ;; extents of the matched (sub) expressions. The (non-)validity is
44 ;; shown only in the modeline without throwing the errors at you. If
45 ;; you want to know the reason why RE Builder considers it as invalid
46 ;; call `reb-force-update' ("\C-c\C-u") which should reveal the error.
47
48 ;; The target buffer can be changed with `reb-change-target-buffer'
49 ;; ("\C-c\C-b"). Changing the target buffer automatically removes
50 ;; the overlays from the old buffer and displays the new one in the
51 ;; target window.
52
53 ;; The `re-builder' keeps the focus while updating the matches in the
54 ;; target buffer so corrections are easy to incorporate. If you are
55 ;; satisfied with the result you can paste the RE to the kill-ring
56 ;; with `reb-copy' ("\C-c\C-w"), quit the `re-builder' ("\C-c\C-q")
57 ;; and use it wherever you need it.
58
59 ;; As the automatic updates can take some time on large buffers, they
60 ;; can be limited by `reb-auto-match-limit' so that they should not
61 ;; have a negative impact on the editing. Setting it to nil makes
62 ;; even the auto updates go all the way. Forcing an update overrides
63 ;; this limit allowing an easy way to see all matches.
64
65 ;; Currently `re-builder' understands five different forms of input,
66 ;; namely `read', `string', `rx', `sregex' and `lisp-re' syntax. Read
67 ;; syntax and string syntax are both delimited by `"'s and behave
68 ;; according to their name. With the `string' syntax there's no need
69 ;; to escape the backslashes and double quotes simplifying the editing
70 ;; somewhat. The other three allow editing of symbolic regular
71 ;; expressions supported by the packages of the same name. (`lisp-re'
72 ;; is a package by me and its support may go away as it is nearly the
73 ;; same as the `sregex' package in Emacs)
74
75 ;; Editing symbolic expressions is done through a major mode derived
76 ;; from `emacs-lisp-mode' so you'll get all the good stuff like
77 ;; automatic indentation and font-locking etc.
78
79 ;; When editing a symbolic regular expression, only the first
80 ;; expression in the RE Builder buffer is considered, which helps
81 ;; limiting the extent of the expression like the `"'s do for the text
82 ;; modes. For the `sregex' syntax the function `sregex' is applied to
83 ;; the evaluated expression read. So you can use quoted arguments
84 ;; with something like '("findme") or you can construct arguments to
85 ;; your hearts delight with a valid ELisp expression. (The compiled
86 ;; string form will be copied by `reb-copy') If you want to take
87 ;; a glance at the corresponding string you can temporarily change the
88 ;; input syntax.
89
90 ;; Changing the input syntax is transparent (for the obvious exception
91 ;; non-symbolic -> symbolic) so you can change your mind as often as
92 ;; you like.
93
94 ;; There is also a shortcut function for toggling the
95 ;; `case-fold-search' variable in the target buffer with an immediate
96 ;; update.
97
98
99 ;; Q: But what if my display cannot show colored overlays?
100 ;; A: Then the cursor will flash around the matched text making it stand
101 ;; out.
102
103 ;; Q: But how can I then make out the sub-expressions?
104 ;; A: Thats where the `sub-expression mode' comes in. In it only the
105 ;; digit keys are assigned to perform an update that will flash the
106 ;; corresponding subexp only.
107
108
109 ;;; Code:
110
111 ;; On XEmacs, load the overlay compatibility library
112 (if (not (fboundp 'make-overlay))
113 (require 'overlay))
114
115 ;; User customizable variables
116 (defgroup re-builder nil
117 "Options for the RE Builder."
118 :group 'lisp
119 :prefix "reb-")
120
121 (defcustom reb-blink-delay 0.5
122 "*Seconds to blink cursor for next/previous match in RE Builder."
123 :group 're-builder
124 :type 'number)
125
126 (defcustom reb-mode-hook nil
127 "*Hooks to run on entering RE Builder mode."
128 :group 're-builder
129 :type 'hook)
130
131 (defcustom reb-re-syntax 'read
132 "*Syntax for the REs in the RE Builder.
133 Can either be `read', `string', `sregex' or `lisp-re'."
134 :group 're-builder
135 :type '(choice (const :tag "Read syntax" read)
136 (const :tag "String syntax" string)
137 (const :tag "`sregex' syntax" sregex)
138 (const :tag "`lisp-re' syntax" lisp-re)
139 (const :tag "`rx' syntax" rx)
140 (value: string)))
141
142 (defcustom reb-auto-match-limit 200
143 "*Positive integer limiting the matches for RE Builder auto updates.
144 Set it to nil if you don't want limits here."
145 :group 're-builder
146 :type '(restricted-sexp :match-alternatives
147 (integerp 'nil)))
148
149
150 (defface reb-match-0
151 '((((class color) (background light))
152 :background "lightblue")
153 (((class color) (background dark))
154 :background "steelblue4")
155 (t
156 :inverse-video t))
157 "Used for displaying the whole match."
158 :group 're-builder)
159
160 (defface reb-match-1
161 '((((class color) (background light))
162 :background "aquamarine")
163 (((class color) (background dark))
164 :background "blue3")
165 (t
166 :inverse-video t))
167 "Used for displaying the first matching subexpression."
168 :group 're-builder)
169
170 (defface reb-match-2
171 '((((class color) (background light))
172 :background "springgreen")
173 (((class color) (background dark))
174 :background "chartreuse4")
175 (t
176 :inverse-video t))
177 "Used for displaying the second matching subexpression."
178 :group 're-builder)
179
180 (defface reb-match-3
181 '((((min-colors 88) (class color) (background light))
182 :background "yellow1")
183 (((class color) (background light))
184 :background "yellow")
185 (((class color) (background dark))
186 :background "sienna4")
187 (t
188 :inverse-video t))
189 "Used for displaying the third matching subexpression."
190 :group 're-builder)
191
192 ;; Internal variables below
193 (defvar reb-mode nil
194 "Enables the RE Builder minor mode.")
195
196 (defvar reb-target-buffer nil
197 "Buffer to which the RE is applied to.")
198
199 (defvar reb-target-window nil
200 "Window to which the RE is applied to.")
201
202 (defvar reb-regexp nil
203 "Last regexp used by RE Builder.")
204
205 (defvar reb-regexp-src nil
206 "Last regexp used by RE Builder before processing it.
207 Except for Lisp syntax this is the same as `reb-regexp'.")
208
209 (defvar reb-overlays nil
210 "List of overlays of the RE Builder.")
211
212 (defvar reb-window-config nil
213 "Old window configuration.")
214
215 (defvar reb-subexp-mode nil
216 "Indicates whether sub-exp mode is active.")
217
218 (defvar reb-subexp-displayed nil
219 "Indicates which sub-exp is active.")
220
221 (defvar reb-mode-string ""
222 "String in mode line for additional info.")
223
224 (defvar reb-valid-string ""
225 "String in mode line showing validity of RE.")
226
227 (make-variable-buffer-local 'reb-overlays)
228 (make-variable-buffer-local 'reb-regexp)
229 (make-variable-buffer-local 'reb-regexp-src)
230
231 (defconst reb-buffer "*RE-Builder*"
232 "Buffer to use for the RE Builder.")
233
234 ;; Define the local "\C-c" keymap
235 (defvar reb-mode-map
236 (let ((map (make-sparse-keymap)))
237 (define-key map "\C-c\C-c" 'reb-toggle-case)
238 (define-key map "\C-c\C-q" 'reb-quit)
239 (define-key map "\C-c\C-w" 'reb-copy)
240 (define-key map "\C-c\C-s" 'reb-next-match)
241 (define-key map "\C-c\C-r" 'reb-prev-match)
242 (define-key map "\C-c\C-i" 'reb-change-syntax)
243 (define-key map "\C-c\C-e" 'reb-enter-subexp-mode)
244 (define-key map "\C-c\C-b" 'reb-change-target-buffer)
245 (define-key map "\C-c\C-u" 'reb-force-update)
246 map)
247 "Keymap used by the RE Builder.")
248
249 (defun reb-mode ()
250 "Major mode for interactively building Regular Expressions.
251 \\{reb-mode-map}"
252 (interactive)
253 (kill-all-local-variables)
254 (setq major-mode 'reb-mode
255 mode-name "RE Builder")
256 (set (make-local-variable 'blink-matching-paren) nil)
257 (use-local-map reb-mode-map)
258 (reb-mode-common)
259 (run-mode-hooks 'reb-mode-hook))
260
261 (define-derived-mode reb-lisp-mode
262 emacs-lisp-mode "RE Builder Lisp"
263 "Major mode for interactively building symbolic Regular Expressions."
264 (cond ((eq reb-re-syntax 'lisp-re) ; Pull in packages
265 (require 'lisp-re)) ; as needed
266 ((eq reb-re-syntax 'sregex) ; sregex is not autoloaded
267 (require 'sregex)) ; right now..
268 ((eq reb-re-syntax 'rx) ; rx-to-string is autoloaded
269 (require 'rx))) ; require rx anyway
270 (reb-mode-common))
271
272 ;; Use the same "\C-c" keymap as `reb-mode' and use font-locking from
273 ;; `emacs-lisp-mode'
274 (define-key reb-lisp-mode-map "\C-c"
275 (lookup-key reb-mode-map "\C-c"))
276
277 (defvar reb-subexp-mode-map
278 (let ((m (make-keymap)))
279 (suppress-keymap m)
280 ;; Again share the "\C-c" keymap for the commands
281 (define-key m "\C-c" (lookup-key reb-mode-map "\C-c"))
282 (define-key m "q" 'reb-quit-subexp-mode)
283 (dotimes (digit 10)
284 (define-key m (int-to-string digit) 'reb-display-subexp))
285 m)
286 "Keymap used by the RE Builder for the subexpression mode.")
287
288 (defun reb-mode-common ()
289 "Setup functions common to functions `reb-mode' and `reb-mode-lisp'."
290
291 (setq reb-mode-string ""
292 reb-valid-string ""
293 mode-line-buffer-identification
294 '(25 . ("%b" reb-mode-string reb-valid-string)))
295 (reb-update-modestring)
296 (make-local-variable 'after-change-functions)
297 (add-hook 'after-change-functions
298 'reb-auto-update)
299 ;; At least make the overlays go away if the buffer is killed
300 (make-local-variable 'reb-kill-buffer)
301 (add-hook 'kill-buffer-hook 'reb-kill-buffer)
302 (reb-auto-update nil nil nil))
303
304 (defun reb-color-display-p ()
305 "Return t if display is capable of displaying colors."
306 (eq 'color
307 ;; emacs/xemacs compatibility
308 (if (fboundp 'frame-parameter)
309 (frame-parameter (selected-frame) 'display-type)
310 (if (fboundp 'frame-property)
311 (frame-property (selected-frame) 'display-type)))))
312
313 (defsubst reb-lisp-syntax-p ()
314 "Return non-nil if RE Builder uses a Lisp syntax."
315 (memq reb-re-syntax '(lisp-re sregex rx)))
316
317 (defmacro reb-target-binding (symbol)
318 "Return binding for SYMBOL in the RE Builder target buffer."
319 `(with-current-buffer reb-target-buffer ,symbol))
320
321 (defun reb-initialize-buffer ()
322 "Initialize the current buffer as a RE Builder buffer."
323 (erase-buffer)
324 (reb-insert-regexp)
325 (goto-char (+ 2 (point-min)))
326 (cond ((reb-lisp-syntax-p)
327 (reb-lisp-mode))
328 (t (reb-mode))))
329
330 ;;; This is to help people find this in Apropos.
331 ;;;###autoload
332 (defalias 'regexp-builder 're-builder)
333
334 ;;;###autoload
335 (defun re-builder ()
336 "Construct a regexp interactively."
337 (interactive)
338
339 (if (and (string= (buffer-name) reb-buffer)
340 (memq major-mode '(reb-mode reb-lisp-mode)))
341 (message "Already in the RE Builder")
342 (if reb-target-buffer
343 (reb-delete-overlays))
344 (setq reb-target-buffer (current-buffer)
345 reb-target-window (selected-window)
346 reb-window-config (current-window-configuration))
347 (select-window (split-window (selected-window) (- (window-height) 4)))
348 (switch-to-buffer (get-buffer-create reb-buffer))
349 (reb-initialize-buffer)))
350
351 (defun reb-change-target-buffer (buf)
352 "Change the target buffer and display it in the target window."
353 (interactive "bSet target buffer to: ")
354
355 (let ((buffer (get-buffer buf)))
356 (if (not buffer)
357 (error "No such buffer")
358 (reb-delete-overlays)
359 (setq reb-target-buffer buffer)
360 (reb-do-update
361 (if reb-subexp-mode reb-subexp-displayed nil))
362 (reb-update-modestring))))
363
364 (defun reb-force-update ()
365 "Force an update in the RE Builder target window without a match limit."
366 (interactive)
367
368 (let ((reb-auto-match-limit nil))
369 (reb-update-overlays
370 (if reb-subexp-mode reb-subexp-displayed nil))))
371
372 (defun reb-quit ()
373 "Quit the RE Builder mode."
374 (interactive)
375
376 (setq reb-subexp-mode nil
377 reb-subexp-displayed nil)
378 (reb-delete-overlays)
379 (bury-buffer)
380 (set-window-configuration reb-window-config))
381
382 (defun reb-next-match ()
383 "Go to next match in the RE Builder target window."
384 (interactive)
385
386 (reb-assert-buffer-in-window)
387 (with-selected-window reb-target-window
388 (if (not (re-search-forward reb-regexp (point-max) t))
389 (message "No more matches.")
390 (reb-show-subexp
391 (or (and reb-subexp-mode reb-subexp-displayed) 0)
392 t))))
393
394 (defun reb-prev-match ()
395 "Go to previous match in the RE Builder target window."
396 (interactive)
397
398 (reb-assert-buffer-in-window)
399 (with-selected-window reb-target-window
400 (let ((p (point)))
401 (goto-char (1- p))
402 (if (re-search-backward reb-regexp (point-min) t)
403 (reb-show-subexp
404 (or (and reb-subexp-mode reb-subexp-displayed) 0)
405 t)
406 (goto-char p)
407 (message "No more matches.")))))
408
409 (defun reb-toggle-case ()
410 "Toggle case sensitivity of searches for RE Builder target buffer."
411 (interactive)
412
413 (with-current-buffer reb-target-buffer
414 (setq case-fold-search (not case-fold-search)))
415 (reb-update-modestring)
416 (reb-auto-update nil nil nil t))
417
418 (defun reb-copy ()
419 "Copy current RE into the kill ring for later insertion."
420 (interactive)
421
422 (reb-update-regexp)
423 (let ((re (with-output-to-string
424 (print (reb-target-binding reb-regexp)))))
425 (kill-new (substring re 1 (1- (length re))))
426 (message "Regexp copied to kill-ring")))
427
428 ;; The subexpression mode is not electric because the number of
429 ;; matches should be seen rather than a prompt.
430 (defun reb-enter-subexp-mode ()
431 "Enter the subexpression mode in the RE Builder."
432 (interactive)
433 (setq reb-subexp-mode t)
434 (reb-update-modestring)
435 (use-local-map reb-subexp-mode-map)
436 (message "`0'-`9' to display subexpressions `q' to quit subexp mode."))
437
438 (defun reb-show-subexp (subexp &optional pause)
439 "Visually show limit of subexpression SUBEXP of recent search.
440 On color displays this just puts point to the end of the expression as
441 the match should already be marked by an overlay.
442 On other displays jump to the beginning and the end of it.
443 If the optional PAUSE is non-nil then pause at the end in any case."
444 (with-selected-window reb-target-window
445 (if (not (reb-color-display-p))
446 (progn (goto-char (match-beginning subexp))
447 (sit-for reb-blink-delay)))
448 (goto-char (match-end subexp))
449 (if (or (not (reb-color-display-p)) pause)
450 (sit-for reb-blink-delay))))
451
452 (defun reb-quit-subexp-mode ()
453 "Quit the subexpression mode in the RE Builder."
454 (interactive)
455 (setq reb-subexp-mode nil
456 reb-subexp-displayed nil)
457 (reb-update-modestring)
458 (use-local-map reb-mode-map)
459 (reb-do-update))
460
461 (defun reb-change-syntax (&optional syntax)
462 "Change the syntax used by the RE Builder.
463 Optional argument SYNTAX must be specified if called non-interactively."
464 (interactive
465 (list (intern
466 (completing-read "Select syntax: "
467 (mapcar (lambda (el) (cons (symbol-name el) 1))
468 '(read string lisp-re sregex rx))
469 nil t (symbol-name reb-re-syntax)))))
470
471 (if (memq syntax '(read string lisp-re sregex rx))
472 (let ((buffer (get-buffer reb-buffer)))
473 (setq reb-re-syntax syntax)
474 (when buffer
475 (with-current-buffer buffer
476 (reb-initialize-buffer))))
477 (error "Invalid syntax: %s" syntax)))
478
479
480 ;; Non-interactive functions below
481 (defun reb-do-update (&optional subexp)
482 "Update matches in the RE Builder target window.
483 If SUBEXP is non-nil mark only the corresponding sub-expressions."
484
485 (reb-assert-buffer-in-window)
486 (reb-update-regexp)
487 (reb-update-overlays subexp))
488
489 (defun reb-auto-update (beg end lenold &optional force)
490 "Called from `after-update-functions' to update the display.
491 BEG, END and LENOLD are passed in from the hook.
492 An actual update is only done if the regexp has changed or if the
493 optional fourth argument FORCE is non-nil."
494 (let ((prev-valid reb-valid-string)
495 (new-valid
496 (condition-case nil
497 (progn
498 (if (or (reb-update-regexp) force)
499 (progn
500 (reb-assert-buffer-in-window)
501 (reb-do-update)))
502 "")
503 (error " *invalid*"))))
504 (setq reb-valid-string new-valid)
505 (force-mode-line-update)
506
507 ;; Through the caching of the re a change invalidating the syntax
508 ;; for symbolic expressions will not delete the overlays so we
509 ;; catch it here
510 (if (and (reb-lisp-syntax-p)
511 (not (string= prev-valid new-valid))
512 (string= prev-valid ""))
513 (reb-delete-overlays))))
514
515 (defun reb-delete-overlays ()
516 "Delete all RE Builder overlays in the `reb-target-buffer' buffer."
517 (if (buffer-live-p reb-target-buffer)
518 (with-current-buffer reb-target-buffer
519 (mapcar 'delete-overlay reb-overlays)
520 (setq reb-overlays nil))))
521
522 (defun reb-assert-buffer-in-window ()
523 "Assert that `reb-target-buffer' is displayed in `reb-target-window'."
524
525 (if (not (eq reb-target-buffer (window-buffer reb-target-window)))
526 (set-window-buffer reb-target-window reb-target-buffer)))
527
528 (defun reb-update-modestring ()
529 "Update the variable `reb-mode-string' displayed in the mode line."
530 (setq reb-mode-string
531 (concat
532 (if reb-subexp-mode
533 (format " (subexp %s)" (or reb-subexp-displayed "-"))
534 "")
535 (if (not (reb-target-binding case-fold-search))
536 " Case"
537 "")))
538 (force-mode-line-update))
539
540 (defun reb-display-subexp (&optional subexp)
541 "Highlight only subexpression SUBEXP in the RE Builder."
542 (interactive)
543
544 (setq reb-subexp-displayed
545 (or subexp (string-to-number (format "%c" last-command-char))))
546 (reb-update-modestring)
547 (reb-do-update reb-subexp-displayed))
548
549 (defun reb-kill-buffer ()
550 "When the RE Builder buffer is killed make sure no overlays stay around."
551
552 (if (member major-mode '(reb-mode reb-lisp-mode))
553 (reb-delete-overlays)))
554
555
556 ;; The next functions are the interface between the regexp and
557 ;; its textual representation in the RE Builder buffer.
558 ;; They are the only functions concerned with the actual syntax
559 ;; being used.
560 (defun reb-read-regexp ()
561 "Read current RE."
562 (save-excursion
563 (cond ((eq reb-re-syntax 'read)
564 (goto-char (point-min))
565 (read (current-buffer)))
566 ((eq reb-re-syntax 'string)
567 (goto-char (point-min))
568 (re-search-forward "\"")
569 (let ((beg (point)))
570 (goto-char (point-max))
571 (re-search-backward "\"")
572 (buffer-substring-no-properties beg (point))))
573 ((reb-lisp-syntax-p)
574 (buffer-string)))))
575
576 (defun reb-empty-regexp ()
577 "Return empty RE for current syntax."
578 (cond ((reb-lisp-syntax-p) "'()")
579 (t "")))
580
581 (defun reb-insert-regexp ()
582 "Insert current RE."
583
584 (let ((re (or (reb-target-binding reb-regexp)
585 (reb-empty-regexp))))
586 (cond ((eq reb-re-syntax 'read)
587 (print re (current-buffer)))
588 ((eq reb-re-syntax 'string)
589 (insert "\n\"" re "\""))
590 ;; For the Lisp syntax we need the "source" of the regexp
591 ((reb-lisp-syntax-p)
592 (insert (or (reb-target-binding reb-regexp-src)
593 (reb-empty-regexp)))))))
594
595 (defun reb-cook-regexp (re)
596 "Return RE after processing it according to `reb-re-syntax'."
597 (cond ((eq reb-re-syntax 'lisp-re)
598 (if (fboundp 'lre-compile-string)
599 (lre-compile-string (eval (car (read-from-string re))))))
600 ((eq reb-re-syntax 'sregex)
601 (apply 'sregex (eval (car (read-from-string re)))))
602 ((eq reb-re-syntax 'rx)
603 (rx-to-string (eval (car (read-from-string re)))))
604 (t re)))
605
606 (defun reb-update-regexp ()
607 "Update the regexp for the target buffer.
608 Return t if the (cooked) expression changed."
609 (let* ((re-src (reb-read-regexp))
610 (re (reb-cook-regexp re-src)))
611 (with-current-buffer reb-target-buffer
612 (let ((oldre reb-regexp))
613 (prog1
614 (not (string= oldre re))
615 (setq reb-regexp re)
616 ;; Only update the source re for the lisp formats
617 (if (reb-lisp-syntax-p)
618 (setq reb-regexp-src re-src)))))))
619
620
621 ;; And now the real core of the whole thing
622 (defun reb-count-subexps (re)
623 "Return number of sub-expressions in the regexp RE."
624
625 (let ((i 0) (beg 0))
626 (while (string-match "\\\\(" re beg)
627 (setq i (1+ i)
628 beg (match-end 0)))
629 i))
630
631 (defun reb-update-overlays (&optional subexp)
632 "Switch to `reb-target-buffer' and mark all matches of `reb-regexp'.
633 If SUBEXP is non-nil mark only the corresponding sub-expressions."
634 (let* ((re (reb-target-binding reb-regexp))
635 (subexps (reb-count-subexps re))
636 (matches 0)
637 (submatches 0)
638 firstmatch)
639 (save-excursion
640 (set-buffer reb-target-buffer)
641 (reb-delete-overlays)
642 (goto-char (point-min))
643 (while (and (re-search-forward re (point-max) t)
644 (or (not reb-auto-match-limit)
645 (< matches reb-auto-match-limit)))
646 (if (= 0 (length (match-string 0)))
647 (error "Empty regular expression!"))
648 (let ((i 0)
649 suffix max-suffix)
650 (setq matches (1+ matches))
651 (while (<= i subexps)
652 (if (and (or (not subexp) (= subexp i))
653 (match-beginning i))
654 (let ((overlay (make-overlay (match-beginning i)
655 (match-end i)))
656 ;; When we have exceeded the number of provided faces,
657 ;; cycle thru them where `max-suffix' denotes the maximum
658 ;; suffix for `reb-match-*' that has been defined and
659 ;; `suffix' the suffix calculated for the current match.
660 (face
661 (cond
662 (max-suffix
663 (if (= suffix max-suffix)
664 (setq suffix 1)
665 (setq suffix (1+ suffix)))
666 (intern-soft (format "reb-match-%d" suffix)))
667 ((intern-soft (format "reb-match-%d" i)))
668 ((setq max-suffix (1- i))
669 (setq suffix 1)
670 ;; `reb-match-1' must exist.
671 'reb-match-1))))
672 (unless firstmatch (setq firstmatch (match-data)))
673 (setq reb-overlays (cons overlay reb-overlays)
674 submatches (1+ submatches))
675 (overlay-put overlay 'face face)
676 (overlay-put overlay 'priority i)))
677 (setq i (1+ i))))))
678 (let ((count (if subexp submatches matches)))
679 (message "%s %smatch%s%s"
680 (if (= 0 count) "No" (int-to-string count))
681 (if subexp "subexpression " "")
682 (if (= 1 count) "" "es")
683 (if (and reb-auto-match-limit
684 (= reb-auto-match-limit count))
685 " (limit reached)" "")))
686 (if firstmatch
687 (progn (store-match-data firstmatch)
688 (reb-show-subexp (or subexp 0))))))
689
690 (provide 're-builder)
691
692 ;;; arch-tag: 5c5515ac-4085-4524-a421-033f44f032e7
693 ;;; re-builder.el ends here