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