]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/re-builder.el
(reb-mode): Don't use define-derived-mode. Call kill-all-local-variables.
[gnu-emacs] / lisp / emacs-lisp / re-builder.el
1 ;;; re-builder.el --- building Regexps with visual feedback
2
3 ;; Copyright (C) 1999, 2000, 2001 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 2, or (at your option)
13 ;; 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; 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.
24
25 ;;; Commentary:
26
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.
32
33 ;; With it you can have immediate visual feedback about how well the
34 ;; regexp behaves to your expectations on the intended data.
35
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.
46
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.
51
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.
57
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.
63
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)
73
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.
77
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.
88
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.
92
93 ;; There is also a shortcut function for toggling the
94 ;; `case-fold-search' variable in the target buffer with an immediate
95 ;; update.
96
97
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.
101
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.
106
107
108 ;;; Code:
109
110 ;; On XEmacs, load the overlay compatibility library
111 (if (not (fboundp 'make-overlay))
112 (require 'overlay))
113
114 ;; User costomizable variables
115 (defgroup re-builder nil
116 "Options for the RE Builder."
117 :group 'lisp
118 :prefix "reb-")
119
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)
124
125 (defcustom reb-mode-hook nil
126 "*Hooks to run on entering RE Builder mode."
127 :group 're-builder
128 :type 'hook)
129
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 (value: string)))
139
140 (defcustom reb-auto-match-limit 200
141 "*Positive integer limiting the matches for RE Builder auto updates.
142 Set it to nil if you don't want limits here."
143 :group 're-builder
144 :type '(restricted-sexp :match-alternatives
145 (integerp 'nil)))
146
147
148 (defface reb-match-0
149 '((((class color))
150 (:background "lightblue"))
151 (t (:inverse-video t)))
152 "Used for displaying the whole match."
153 :group 're-builder)
154
155 (defface reb-match-1
156 '((((class color))
157 (:background "aquamarine"))
158 (t (:inverse-video t)))
159 "Used for displaying the first matching subexpression."
160 :group 're-builder)
161
162 (defface reb-match-2
163 '((((class color))
164 (:background "springgreen"))
165 (t (:inverse-video t)))
166 "Used for displaying the second matching subexpression."
167 :group 're-builder)
168
169 (defface reb-match-3
170 '((((class color))
171 (:background "yellow"))
172 (t (:inverse-video t)))
173 "Used for displaying the third matching subexpression."
174 :group 're-builder)
175
176 ;; Internal variables below
177 (defvar reb-mode nil
178 "Enables the RE Builder minor mode.")
179
180 (defvar reb-target-buffer nil
181 "Buffer to which the RE is applied to.")
182
183 (defvar reb-target-window nil
184 "Window to which the RE is applied to.")
185
186 (defvar reb-regexp nil
187 "Last regexp used by RE Builder.")
188
189 (defvar reb-regexp-src nil
190 "Last regexp used by RE Builder before processing it.
191 Except for Lisp syntax this is the same as `reb-regexp'.")
192
193 (defvar reb-overlays nil
194 "List of overlays of the RE Builder.")
195
196 (defvar reb-window-config nil
197 "Old window configuration.")
198
199 (defvar reb-subexp-mode nil
200 "Indicates whether sub-exp mode is active.")
201
202 (defvar reb-subexp-displayed nil
203 "Indicates which sub-exp is active.")
204
205 (defvar reb-mode-string ""
206 "String in mode line for additional info.")
207
208 (defvar reb-valid-string ""
209 "String in mode line showing validity of RE.")
210
211 (make-variable-buffer-local 'reb-overlays)
212 (make-variable-buffer-local 'reb-regexp)
213 (make-variable-buffer-local 'reb-regexp-src)
214
215 (defconst reb-buffer "*RE-Builder*"
216 "Buffer to use for the RE Builder.")
217
218 ;; Define the local "\C-c" keymap
219 (defvar reb-mode-map nil
220 "Keymap used by the RE Builder.")
221
222 (if (not reb-mode-map)
223 (progn
224 (setq reb-mode-map (make-sparse-keymap))
225 (define-key reb-mode-map "\C-c\C-c" 'reb-toggle-case)
226 (define-key reb-mode-map "\C-c\C-q" 'reb-quit)
227 (define-key reb-mode-map "\C-c\C-w" 'reb-copy)
228 (define-key reb-mode-map "\C-c\C-s" 'reb-next-match)
229 (define-key reb-mode-map "\C-c\C-r" 'reb-prev-match)
230 (define-key reb-mode-map "\C-c\C-i" 'reb-change-syntax)
231 (define-key reb-mode-map "\C-c\C-e" 'reb-enter-subexp-mode)
232 (define-key reb-mode-map "\C-c\C-b" 'reb-change-target-buffer)
233 (define-key reb-mode-map "\C-c\C-u" 'reb-force-update)))
234
235 (defun reb-mode ()
236 "Major mode for interactively building Regular Expressions.
237 \\{reb-mode-map}"
238 (interactive)
239 (kill-all-local-variables)
240 (setq major-mode 'reb-mode
241 mode-name "RE Builder")
242 (use-local-map reb-mode-map)
243 (reb-mode-common)
244 (run-hooks 'reb-mode-hook))
245
246 (define-derived-mode reb-lisp-mode
247 emacs-lisp-mode "RE Builder Lisp"
248 "Major mode for interactively building symbolic Regular Expressions."
249 (cond ((eq reb-re-syntax 'lisp-re) ; Pull in packages
250 (require 'lisp-re)) ; as needed
251 ((eq reb-re-syntax 'sregex) ; sregex is not autoloaded
252 (require 'sregex))) ; right now..
253 (reb-mode-common))
254
255 ;; Use the same "\C-c" keymap as `reb-mode' and use font-locking from
256 ;; `emacs-lisp-mode'
257 (define-key reb-lisp-mode-map "\C-c"
258 (lookup-key reb-mode-map "\C-c"))
259
260 (defvar reb-subexp-mode-map
261 (let ((m (make-keymap)))
262 (suppress-keymap m)
263 ;; Again share the "\C-c" keymap for the commands
264 (define-key m "\C-c" (lookup-key reb-mode-map "\C-c"))
265 (define-key m "q" 'reb-quit-subexp-mode)
266 (dotimes (digit 10)
267 (define-key m (int-to-string digit) 'reb-display-subexp))
268 m)
269 "Keymap used by the RE Builder for the subexpression mode.")
270
271 (defun reb-mode-common ()
272 "Setup functions common to functions `reb-mode' and `reb-mode-lisp'."
273
274 (setq reb-mode-string ""
275 reb-valid-string ""
276 mode-line-buffer-identification
277 '(25 . ("%b" reb-mode-string reb-valid-string)))
278 (reb-update-modestring)
279 (make-local-variable 'after-change-functions)
280 (add-hook 'after-change-functions
281 'reb-auto-update)
282 ;; At least make the overlays go away if the buffer is killed
283 (make-local-variable 'reb-kill-buffer)
284 (add-hook 'kill-buffer-hook 'reb-kill-buffer)
285 (reb-auto-update nil nil nil))
286
287
288 ;; Handy macro for doing things in other windows
289 (defmacro reb-with-current-window (window &rest body)
290 "With WINDOW selected evaluate BODY forms and reselect previous window."
291
292 (let ((oldwindow (make-symbol "*oldwindow*")))
293 `(let ((,oldwindow (selected-window)))
294 (select-window ,window)
295 (unwind-protect
296 (progn
297 ,@body)
298 (select-window ,oldwindow)))))
299 (put 'reb-with-current-window 'lisp-indent-function 0)
300
301 (defun reb-color-display-p ()
302 "Return t if display is capable of displaying colors."
303 (eq 'color
304 ;; emacs/xemacs compatibility
305 (if (fboundp 'frame-parameter)
306 (frame-parameter (selected-frame) 'display-type)
307 (frame-property (selected-frame) 'display-type))))
308
309 (defsubst reb-lisp-syntax-p ()
310 "Return non-nil if RE Builder uses a Lisp syntax."
311 (memq reb-re-syntax '(lisp-re sregex)))
312
313 (defmacro reb-target-binding (symbol)
314 "Return binding for SYMBOL in the RE Builder target buffer."
315 `(with-current-buffer reb-target-buffer ,symbol))
316
317
318 ;;;###autoload
319 (defun re-builder ()
320 "Call up the RE Builder for the current window."
321 (interactive)
322
323 (if (and (string= (buffer-name) reb-buffer)
324 (memq major-mode '(reb-mode reb-lisp-mode)))
325 (message "Already in the RE Builder")
326 (if reb-target-buffer
327 (reb-delete-overlays))
328 (setq reb-target-buffer (current-buffer)
329 reb-target-window (selected-window)
330 reb-window-config (current-window-configuration))
331 (select-window (split-window (selected-window) (- (window-height) 4)))
332 (switch-to-buffer (get-buffer-create reb-buffer))
333 (erase-buffer)
334 (reb-insert-regexp)
335 (goto-char (+ 2 (point-min)))
336 (cond
337 ((reb-lisp-syntax-p)
338 (reb-lisp-mode))
339 (t (reb-mode)))))
340
341 (defun reb-change-target-buffer (buf)
342 "Change the target buffer and display it in the target window."
343 (interactive "bSet target buffer to: ")
344
345 (let ((buffer (get-buffer buf)))
346 (if (not buffer)
347 (error "No such buffer")
348 (reb-delete-overlays)
349 (setq reb-target-buffer buffer)
350 (reb-do-update
351 (if reb-subexp-mode reb-subexp-displayed nil))
352 (reb-update-modestring))))
353
354 (defun reb-force-update ()
355 "Forces an update in the RE Builder target window without a match limit."
356 (interactive)
357
358 (let ((reb-auto-match-limit nil))
359 (reb-update-overlays
360 (if reb-subexp-mode reb-subexp-displayed nil))))
361
362 (defun reb-quit ()
363 "Quit the RE Builder mode."
364 (interactive)
365
366 (setq reb-subexp-mode nil
367 reb-subexp-displayed nil)
368 (reb-delete-overlays)
369 (bury-buffer)
370 (set-window-configuration reb-window-config))
371
372 (defun reb-next-match ()
373 "Go to next match in the RE Builder target window."
374 (interactive)
375
376 (reb-assert-buffer-in-window)
377 (reb-with-current-window
378 reb-target-window
379 (if (not (re-search-forward reb-regexp (point-max) t))
380 (message "No more matches.")
381 (reb-show-subexp
382 (or (and reb-subexp-mode reb-subexp-displayed) 0)
383 t))))
384
385 (defun reb-prev-match ()
386 "Go to previous match in the RE Builder target window."
387 (interactive)
388
389 (reb-assert-buffer-in-window)
390 (reb-with-current-window reb-target-window
391 (goto-char (1- (point)))
392 (if (not (re-search-backward reb-regexp (point-min) t))
393 (message "No more matches.")
394 (reb-show-subexp
395 (or (and reb-subexp-mode reb-subexp-displayed) 0)
396 t))))
397
398 (defun reb-toggle-case ()
399 "Toggle case sensitivity of searches for RE Builder target buffer."
400 (interactive)
401
402 (with-current-buffer reb-target-buffer
403 (setq case-fold-search (not case-fold-search)))
404 (reb-update-modestring)
405 (reb-auto-update nil nil nil t))
406
407 (defun reb-copy ()
408 "Copy current RE into the kill ring for later insertion."
409 (interactive)
410
411 (reb-update-regexp)
412 (let ((re (with-output-to-string
413 (print (reb-target-binding reb-regexp)))))
414 (kill-new (substring re 1 (1- (length re))))
415 (message "Regexp copied to kill-ring")))
416
417 ;; The subexpression mode is not electric because the number of
418 ;; matches should be seen rather than a prompt.
419 (defun reb-enter-subexp-mode ()
420 "Enter the subexpression mode in the RE Builder."
421 (interactive)
422 (setq reb-subexp-mode t)
423 (reb-update-modestring)
424 (use-local-map reb-subexp-mode-map)
425 (message "`0'-`9' to display subexpressions `q' to quit subexp mode."))
426
427 (defun reb-show-subexp (subexp &optional pause)
428 "Visually show limit of subexpression SUBEXP of recent search.
429 On color displays this just puts point to the end of the expression as
430 the match should already be marked by an overlay.
431 On other displays jump to the beginning and the end of it.
432 If the optional PAUSE is non-nil then pause at the end in any case."
433 (reb-with-current-window reb-target-window
434 (if (not (reb-color-display-p))
435 (progn (goto-char (match-beginning subexp))
436 (sit-for reb-blink-delay)))
437 (goto-char (match-end subexp))
438 (if (or (not (reb-color-display-p)) pause)
439 (sit-for reb-blink-delay))))
440
441 (defun reb-quit-subexp-mode ()
442 "Quit the subexpression mode in the RE Builder."
443 (interactive)
444 (setq reb-subexp-mode nil
445 reb-subexp-displayed nil)
446 (reb-update-modestring)
447 (use-local-map reb-mode-map)
448 (reb-do-update))
449
450 (defun reb-change-syntax (&optional syntax)
451 "Change the syntax used by the RE Builder.
452 Optional argument SYNTAX must be specified if called non-interactively."
453 (interactive
454 (list (intern
455 (completing-read "Select syntax: "
456 (mapcar (lambda (el) (cons (symbol-name el) 1))
457 '(read string lisp-re sregex))
458 nil t (symbol-name reb-re-syntax)))))
459
460 (if (memq syntax '(read string lisp-re sregex))
461 (let ((buffer (get-buffer reb-buffer)))
462 (setq reb-re-syntax syntax)
463 (if buffer
464 (with-current-buffer buffer
465 (erase-buffer)
466 (reb-insert-regexp)
467 (goto-char (+ 2 (point-min)))
468 (cond ((reb-lisp-syntax-p)
469 (reb-lisp-mode))
470 (t (reb-mode))))))
471 (error "Invalid syntax: %s" syntax)))
472
473
474 ;; Non-interactive functions below
475 (defun reb-do-update (&optional subexp)
476 "Update matches in the RE Builder target window.
477 If SUBEXP is non-nil mark only the corresponding sub-expressions."
478
479 (reb-assert-buffer-in-window)
480 (reb-update-regexp)
481 (reb-update-overlays subexp))
482
483 (defun reb-auto-update (beg end lenold &optional force)
484 "Called from `after-update-functions' to update the display.
485 BEG END and LENOLD are passed in from the hook.
486 An actual update is only done if the regexp has changed or if the
487 optional fourth argument FORCE is non-nil."
488 (let ((prev-valid reb-valid-string)
489 (new-valid
490 (condition-case nil
491 (progn
492 (if (or (reb-update-regexp) force)
493 (progn
494 (reb-assert-buffer-in-window)
495 (reb-do-update)))
496 "")
497 (error " *invalid*"))))
498 (setq reb-valid-string new-valid)
499 (force-mode-line-update)
500
501 ;; Through the caching of the re a change invalidating the syntax
502 ;; for symbolic expressions will not delete the overlays so we
503 ;; catch it here
504 (if (and (reb-lisp-syntax-p)
505 (not (string= prev-valid new-valid))
506 (string= prev-valid ""))
507 (reb-delete-overlays))))
508
509 (defun reb-delete-overlays ()
510 "Delete all RE Builder overlays in the `reb-target-buffer' buffer."
511 (if (buffer-live-p reb-target-buffer)
512 (with-current-buffer reb-target-buffer
513 (mapcar 'delete-overlay reb-overlays)
514 (setq reb-overlays nil))))
515
516 (defun reb-assert-buffer-in-window ()
517 "Assert that `reb-target-buffer' is displayed in `reb-target-window'."
518
519 (if (not (eq reb-target-buffer (window-buffer reb-target-window)))
520 (set-window-buffer reb-target-window reb-target-buffer)))
521
522 (defun reb-update-modestring ()
523 "Update the variable `reb-mode-string' displayed in the mode line."
524 (setq reb-mode-string
525 (concat
526 (if reb-subexp-mode
527 (format " (subexp %s)" (or reb-subexp-displayed "-"))
528 "")
529 (if (not (reb-target-binding case-fold-search))
530 " Case"
531 "")))
532 (force-mode-line-update))
533
534 (defun reb-display-subexp (&optional subexp)
535 "Highlight only subexpression SUBEXP in the RE Builder."
536 (interactive)
537
538 (setq reb-subexp-displayed
539 (or subexp (string-to-int (format "%c" last-command-char))))
540 (reb-update-modestring)
541 (reb-do-update reb-subexp-displayed))
542
543 (defun reb-kill-buffer ()
544 "When the RE Builder buffer is killed make sure no overlays stay around."
545
546 (if (member major-mode '(reb-mode reb-lisp-mode))
547 (reb-delete-overlays)))
548
549
550 ;; The next functions are the interface between the regexp and
551 ;; its textual representation in the RE Builder buffer.
552 ;; They are the only functions concerned with the actual syntax
553 ;; being used.
554 (defun reb-read-regexp ()
555 "Read current RE."
556 (save-excursion
557 (cond ((eq reb-re-syntax 'read)
558 (goto-char (point-min))
559 (read (current-buffer)))
560 ((eq reb-re-syntax 'string)
561 (goto-char (point-min))
562 (re-search-forward "\"")
563 (let ((beg (point)))
564 (goto-char (point-max))
565 (re-search-backward "\"")
566 (buffer-substring-no-properties beg (point))))
567 ((reb-lisp-syntax-p)
568 (buffer-string)))))
569
570 (defun reb-empty-regexp ()
571 "Return empty RE for current syntax."
572 (cond ((reb-lisp-syntax-p) "'()")
573 (t "")))
574
575 (defun reb-insert-regexp ()
576 "Insert current RE."
577
578 (let ((re (or (reb-target-binding reb-regexp)
579 (reb-empty-regexp))))
580 (cond ((eq reb-re-syntax 'read)
581 (print re (current-buffer)))
582 ((eq reb-re-syntax 'string)
583 (insert "\n\"" re "\""))
584 ;; For the Lisp syntax we need the "source" of the regexp
585 ((reb-lisp-syntax-p)
586 (insert (or (reb-target-binding reb-regexp-src)
587 (reb-empty-regexp)))))))
588
589 (defun reb-cook-regexp (re)
590 "Return RE after processing it according to `reb-re-syntax'."
591 (cond ((eq reb-re-syntax 'lisp-re)
592 (lre-compile-string (eval (car (read-from-string re)))))
593 ((eq reb-re-syntax 'sregex)
594 (apply 'sregex (eval (car (read-from-string re)))))
595 (t re)))
596
597 (defun reb-update-regexp ()
598 "Update the regexp for the target buffer.
599 Return t if the (cooked) expression changed."
600 (let* ((re-src (reb-read-regexp))
601 (re (reb-cook-regexp re-src)))
602 (with-current-buffer reb-target-buffer
603 (let ((oldre reb-regexp))
604 (prog1
605 (not (string= oldre re))
606 (setq reb-regexp re)
607 ;; Only update the source re for the lisp formats
608 (if (reb-lisp-syntax-p)
609 (setq reb-regexp-src re-src)))))))
610
611
612 ;; And now the real core of the whole thing
613 (defun reb-count-subexps (re)
614 "Return number of sub-expressions in the regexp RE."
615
616 (let ((i 0) (beg 0))
617 (while (string-match "\\\\(" re beg)
618 (setq i (1+ i)
619 beg (match-end 0)))
620 i))
621
622
623 (defun reb-update-overlays (&optional subexp)
624 "Switch to `reb-target-buffer' and mark all matches of `reb-regexp'.
625 If SUBEXP is non-nil mark only the corresponding sub-expressions."
626
627 (let* ((re (reb-target-binding reb-regexp))
628 (subexps (reb-count-subexps re))
629 (matches 0)
630 (submatches 0)
631 firstmatch)
632 (save-excursion
633 (set-buffer reb-target-buffer)
634 (reb-delete-overlays)
635 (goto-char (point-min))
636 (while (and (re-search-forward re (point-max) t)
637 (or (not reb-auto-match-limit)
638 (< matches reb-auto-match-limit)))
639 (if (= 0 (length (match-string 0)))
640 (error "Empty regular expression!"))
641 (let ((i 0))
642 (setq matches (1+ matches))
643 (while (<= i subexps)
644 (if (and (or (not subexp) (= subexp i))
645 (match-beginning i))
646 (let ((overlay (make-overlay (match-beginning i)
647 (match-end i)))
648 (face-name (format "reb-match-%d" i)))
649 (if (not firstmatch)
650 (setq firstmatch (match-data)))
651 (setq reb-overlays (cons overlay reb-overlays)
652 submatches (1+ submatches))
653 (overlay-put
654 overlay 'face
655 (or (intern-soft face-name)
656 (error "Too many subexpressions - face `%s' not defined"
657 face-name )))
658 (overlay-put overlay 'priority i)))
659 (setq i (1+ i))))))
660 (let ((count (if subexp submatches matches)))
661 (message"%s %smatch(es)%s"
662 (if (= 0 count) "No" (int-to-string count))
663 (if subexp "subexpression " "")
664 (if (and reb-auto-match-limit
665 (= reb-auto-match-limit count))
666 " (limit reached)" "")))
667 (if firstmatch
668 (progn (store-match-data firstmatch)
669 (reb-show-subexp (or subexp 0))))))
670
671 ;;; re-builder.el ends here