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