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