]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/re-builder.el
(reb-with-current-window): Delete.
[gnu-emacs] / lisp / emacs-lisp / re-builder.el
1 ;;; re-builder.el --- building Regexps with visual feedback
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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 five different forms of input,
65 ;; namely `read', `string', `rx', `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 three 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 (const :tag "`rx' syntax" rx)
139 (value: string)))
140
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)))
147
148
149 (defface reb-match-0
150 '((((class color) (background light))
151 :background "lightblue")
152 (((class color) (background dark))
153 :background "steelblue4")
154 (t
155 :inverse-video t))
156 "Used for displaying the whole match."
157 :group 're-builder)
158
159 (defface reb-match-1
160 '((((class color) (background light))
161 :background "aquamarine")
162 (((class color) (background dark))
163 :background "blue3")
164 (t
165 :inverse-video t))
166 "Used for displaying the first matching subexpression."
167 :group 're-builder)
168
169 (defface reb-match-2
170 '((((class color) (background light))
171 :background "springgreen")
172 (((class color) (background dark))
173 :background "chartreuse4")
174 (t
175 :inverse-video t))
176 "Used for displaying the second matching subexpression."
177 :group 're-builder)
178
179 (defface reb-match-3
180 '((((min-colors 88) (class color) (background light))
181 :background "yellow1")
182 (((class color) (background light))
183 :background "yellow")
184 (((class color) (background dark))
185 :background "sienna4")
186 (t
187 :inverse-video t))
188 "Used for displaying the third matching subexpression."
189 :group 're-builder)
190
191 ;; Internal variables below
192 (defvar reb-mode nil
193 "Enables the RE Builder minor mode.")
194
195 (defvar reb-target-buffer nil
196 "Buffer to which the RE is applied to.")
197
198 (defvar reb-target-window nil
199 "Window to which the RE is applied to.")
200
201 (defvar reb-regexp nil
202 "Last regexp used by RE Builder.")
203
204 (defvar reb-regexp-src nil
205 "Last regexp used by RE Builder before processing it.
206 Except for Lisp syntax this is the same as `reb-regexp'.")
207
208 (defvar reb-overlays nil
209 "List of overlays of the RE Builder.")
210
211 (defvar reb-window-config nil
212 "Old window configuration.")
213
214 (defvar reb-subexp-mode nil
215 "Indicates whether sub-exp mode is active.")
216
217 (defvar reb-subexp-displayed nil
218 "Indicates which sub-exp is active.")
219
220 (defvar reb-mode-string ""
221 "String in mode line for additional info.")
222
223 (defvar reb-valid-string ""
224 "String in mode line showing validity of RE.")
225
226 (make-variable-buffer-local 'reb-overlays)
227 (make-variable-buffer-local 'reb-regexp)
228 (make-variable-buffer-local 'reb-regexp-src)
229
230 (defconst reb-buffer "*RE-Builder*"
231 "Buffer to use for the RE Builder.")
232
233 ;; Define the local "\C-c" keymap
234 (defvar reb-mode-map
235 (let ((map (make-sparse-keymap)))
236 (define-key map "\C-c\C-c" 'reb-toggle-case)
237 (define-key map "\C-c\C-q" 'reb-quit)
238 (define-key map "\C-c\C-w" 'reb-copy)
239 (define-key map "\C-c\C-s" 'reb-next-match)
240 (define-key map "\C-c\C-r" 'reb-prev-match)
241 (define-key map "\C-c\C-i" 'reb-change-syntax)
242 (define-key map "\C-c\C-e" 'reb-enter-subexp-mode)
243 (define-key map "\C-c\C-b" 'reb-change-target-buffer)
244 (define-key map "\C-c\C-u" 'reb-force-update)
245 map)
246 "Keymap used by the RE Builder.")
247
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-mode-hooks 'reb-mode-hook))
258
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))
269
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"))
274
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))
283 m)
284 "Keymap used by the RE Builder for the subexpression mode.")
285
286 (defun reb-mode-common ()
287 "Setup functions common to functions `reb-mode' and `reb-mode-lisp'."
288
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))
301
302 (defun reb-color-display-p ()
303 "Return t if display is capable of displaying colors."
304 (eq 'color
305 ;; emacs/xemacs compatibility
306 (if (fboundp 'frame-parameter)
307 (frame-parameter (selected-frame) 'display-type)
308 (if (fboundp 'frame-property)
309 (frame-property (selected-frame) 'display-type)))))
310
311 (defsubst reb-lisp-syntax-p ()
312 "Return non-nil if RE Builder uses a Lisp syntax."
313 (memq reb-re-syntax '(lisp-re sregex rx)))
314
315 (defmacro reb-target-binding (symbol)
316 "Return binding for SYMBOL in the RE Builder target buffer."
317 `(with-current-buffer reb-target-buffer ,symbol))
318
319 (defun reb-initialize-buffer ()
320 "Initialize the current buffer as a RE Builder buffer."
321 (erase-buffer)
322 (reb-insert-regexp)
323 (goto-char (+ 2 (point-min)))
324 (cond ((reb-lisp-syntax-p)
325 (reb-lisp-mode))
326 (t (reb-mode))))
327
328 ;;; This is to help people find this in Apropos.
329 ;;;###autoload
330 (defalias 'regexp-builder 're-builder)
331
332 ;;;###autoload
333 (defun re-builder ()
334 "Construct a regexp interactively."
335 (interactive)
336
337 (if (and (string= (buffer-name) reb-buffer)
338 (memq major-mode '(reb-mode reb-lisp-mode)))
339 (message "Already in the RE Builder")
340 (if reb-target-buffer
341 (reb-delete-overlays))
342 (setq reb-target-buffer (current-buffer)
343 reb-target-window (selected-window)
344 reb-window-config (current-window-configuration))
345 (select-window (split-window (selected-window) (- (window-height) 4)))
346 (switch-to-buffer (get-buffer-create reb-buffer))
347 (reb-initialize-buffer)))
348
349 (defun reb-change-target-buffer (buf)
350 "Change the target buffer and display it in the target window."
351 (interactive "bSet target buffer to: ")
352
353 (let ((buffer (get-buffer buf)))
354 (if (not buffer)
355 (error "No such buffer")
356 (reb-delete-overlays)
357 (setq reb-target-buffer buffer)
358 (reb-do-update
359 (if reb-subexp-mode reb-subexp-displayed nil))
360 (reb-update-modestring))))
361
362 (defun reb-force-update ()
363 "Force an update in the RE Builder target window without a match limit."
364 (interactive)
365
366 (let ((reb-auto-match-limit nil))
367 (reb-update-overlays
368 (if reb-subexp-mode reb-subexp-displayed nil))))
369
370 (defun reb-quit ()
371 "Quit the RE Builder mode."
372 (interactive)
373
374 (setq reb-subexp-mode nil
375 reb-subexp-displayed nil)
376 (reb-delete-overlays)
377 (bury-buffer)
378 (set-window-configuration reb-window-config))
379
380 (defun reb-next-match ()
381 "Go to next match in the RE Builder target window."
382 (interactive)
383
384 (reb-assert-buffer-in-window)
385 (with-selected-window reb-target-window
386 (if (not (re-search-forward reb-regexp (point-max) t))
387 (message "No more matches.")
388 (reb-show-subexp
389 (or (and reb-subexp-mode reb-subexp-displayed) 0)
390 t))))
391
392 (defun reb-prev-match ()
393 "Go to previous match in the RE Builder target window."
394 (interactive)
395
396 (reb-assert-buffer-in-window)
397 (with-selected-window reb-target-window
398 (let ((p (point)))
399 (goto-char (1- p))
400 (if (re-search-backward reb-regexp (point-min) t)
401 (reb-show-subexp
402 (or (and reb-subexp-mode reb-subexp-displayed) 0)
403 t)
404 (goto-char p)
405 (message "No more matches.")))))
406
407 (defun reb-toggle-case ()
408 "Toggle case sensitivity of searches for RE Builder target buffer."
409 (interactive)
410
411 (with-current-buffer reb-target-buffer
412 (setq case-fold-search (not case-fold-search)))
413 (reb-update-modestring)
414 (reb-auto-update nil nil nil t))
415
416 (defun reb-copy ()
417 "Copy current RE into the kill ring for later insertion."
418 (interactive)
419
420 (reb-update-regexp)
421 (let ((re (with-output-to-string
422 (print (reb-target-binding reb-regexp)))))
423 (kill-new (substring re 1 (1- (length re))))
424 (message "Regexp copied to kill-ring")))
425
426 ;; The subexpression mode is not electric because the number of
427 ;; matches should be seen rather than a prompt.
428 (defun reb-enter-subexp-mode ()
429 "Enter the subexpression mode in the RE Builder."
430 (interactive)
431 (setq reb-subexp-mode t)
432 (reb-update-modestring)
433 (use-local-map reb-subexp-mode-map)
434 (message "`0'-`9' to display subexpressions `q' to quit subexp mode."))
435
436 (defun reb-show-subexp (subexp &optional pause)
437 "Visually show limit of subexpression SUBEXP of recent search.
438 On color displays this just puts point to the end of the expression as
439 the match should already be marked by an overlay.
440 On other displays jump to the beginning and the end of it.
441 If the optional PAUSE is non-nil then pause at the end in any case."
442 (with-selected-window reb-target-window
443 (if (not (reb-color-display-p))
444 (progn (goto-char (match-beginning subexp))
445 (sit-for reb-blink-delay)))
446 (goto-char (match-end subexp))
447 (if (or (not (reb-color-display-p)) pause)
448 (sit-for reb-blink-delay))))
449
450 (defun reb-quit-subexp-mode ()
451 "Quit the subexpression mode in the RE Builder."
452 (interactive)
453 (setq reb-subexp-mode nil
454 reb-subexp-displayed nil)
455 (reb-update-modestring)
456 (use-local-map reb-mode-map)
457 (reb-do-update))
458
459 (defun reb-change-syntax (&optional syntax)
460 "Change the syntax used by the RE Builder.
461 Optional argument SYNTAX must be specified if called non-interactively."
462 (interactive
463 (list (intern
464 (completing-read "Select syntax: "
465 (mapcar (lambda (el) (cons (symbol-name el) 1))
466 '(read string lisp-re sregex rx))
467 nil t (symbol-name reb-re-syntax)))))
468
469 (if (memq syntax '(read string lisp-re sregex rx))
470 (let ((buffer (get-buffer reb-buffer)))
471 (setq reb-re-syntax syntax)
472 (when buffer
473 (with-current-buffer buffer
474 (reb-initialize-buffer))))
475 (error "Invalid syntax: %s" syntax)))
476
477
478 ;; Non-interactive functions below
479 (defun reb-do-update (&optional subexp)
480 "Update matches in the RE Builder target window.
481 If SUBEXP is non-nil mark only the corresponding sub-expressions."
482
483 (reb-assert-buffer-in-window)
484 (reb-update-regexp)
485 (reb-update-overlays subexp))
486
487 (defun reb-auto-update (beg end lenold &optional force)
488 "Called from `after-update-functions' to update the display.
489 BEG, END and LENOLD are passed in from the hook.
490 An actual update is only done if the regexp has changed or if the
491 optional fourth argument FORCE is non-nil."
492 (let ((prev-valid reb-valid-string)
493 (new-valid
494 (condition-case nil
495 (progn
496 (if (or (reb-update-regexp) force)
497 (progn
498 (reb-assert-buffer-in-window)
499 (reb-do-update)))
500 "")
501 (error " *invalid*"))))
502 (setq reb-valid-string new-valid)
503 (force-mode-line-update)
504
505 ;; Through the caching of the re a change invalidating the syntax
506 ;; for symbolic expressions will not delete the overlays so we
507 ;; catch it here
508 (if (and (reb-lisp-syntax-p)
509 (not (string= prev-valid new-valid))
510 (string= prev-valid ""))
511 (reb-delete-overlays))))
512
513 (defun reb-delete-overlays ()
514 "Delete all RE Builder overlays in the `reb-target-buffer' buffer."
515 (if (buffer-live-p reb-target-buffer)
516 (with-current-buffer reb-target-buffer
517 (mapcar 'delete-overlay reb-overlays)
518 (setq reb-overlays nil))))
519
520 (defun reb-assert-buffer-in-window ()
521 "Assert that `reb-target-buffer' is displayed in `reb-target-window'."
522
523 (if (not (eq reb-target-buffer (window-buffer reb-target-window)))
524 (set-window-buffer reb-target-window reb-target-buffer)))
525
526 (defun reb-update-modestring ()
527 "Update the variable `reb-mode-string' displayed in the mode line."
528 (setq reb-mode-string
529 (concat
530 (if reb-subexp-mode
531 (format " (subexp %s)" (or reb-subexp-displayed "-"))
532 "")
533 (if (not (reb-target-binding case-fold-search))
534 " Case"
535 "")))
536 (force-mode-line-update))
537
538 (defun reb-display-subexp (&optional subexp)
539 "Highlight only subexpression SUBEXP in the RE Builder."
540 (interactive)
541
542 (setq reb-subexp-displayed
543 (or subexp (string-to-number (format "%c" last-command-char))))
544 (reb-update-modestring)
545 (reb-do-update reb-subexp-displayed))
546
547 (defun reb-kill-buffer ()
548 "When the RE Builder buffer is killed make sure no overlays stay around."
549
550 (if (member major-mode '(reb-mode reb-lisp-mode))
551 (reb-delete-overlays)))
552
553
554 ;; The next functions are the interface between the regexp and
555 ;; its textual representation in the RE Builder buffer.
556 ;; They are the only functions concerned with the actual syntax
557 ;; being used.
558 (defun reb-read-regexp ()
559 "Read current RE."
560 (save-excursion
561 (cond ((eq reb-re-syntax 'read)
562 (goto-char (point-min))
563 (read (current-buffer)))
564 ((eq reb-re-syntax 'string)
565 (goto-char (point-min))
566 (re-search-forward "\"")
567 (let ((beg (point)))
568 (goto-char (point-max))
569 (re-search-backward "\"")
570 (buffer-substring-no-properties beg (point))))
571 ((reb-lisp-syntax-p)
572 (buffer-string)))))
573
574 (defun reb-empty-regexp ()
575 "Return empty RE for current syntax."
576 (cond ((reb-lisp-syntax-p) "'()")
577 (t "")))
578
579 (defun reb-insert-regexp ()
580 "Insert current RE."
581
582 (let ((re (or (reb-target-binding reb-regexp)
583 (reb-empty-regexp))))
584 (cond ((eq reb-re-syntax 'read)
585 (print re (current-buffer)))
586 ((eq reb-re-syntax 'string)
587 (insert "\n\"" re "\""))
588 ;; For the Lisp syntax we need the "source" of the regexp
589 ((reb-lisp-syntax-p)
590 (insert (or (reb-target-binding reb-regexp-src)
591 (reb-empty-regexp)))))))
592
593 (defun reb-cook-regexp (re)
594 "Return RE after processing it according to `reb-re-syntax'."
595 (cond ((eq reb-re-syntax 'lisp-re)
596 (if (fboundp 'lre-compile-string)
597 (lre-compile-string (eval (car (read-from-string re))))))
598 ((eq reb-re-syntax 'sregex)
599 (apply 'sregex (eval (car (read-from-string re)))))
600 ((eq reb-re-syntax 'rx)
601 (rx-to-string (eval (car (read-from-string re)))))
602 (t re)))
603
604 (defun reb-update-regexp ()
605 "Update the regexp for the target buffer.
606 Return t if the (cooked) expression changed."
607 (let* ((re-src (reb-read-regexp))
608 (re (reb-cook-regexp re-src)))
609 (with-current-buffer reb-target-buffer
610 (let ((oldre reb-regexp))
611 (prog1
612 (not (string= oldre re))
613 (setq reb-regexp re)
614 ;; Only update the source re for the lisp formats
615 (if (reb-lisp-syntax-p)
616 (setq reb-regexp-src re-src)))))))
617
618
619 ;; And now the real core of the whole thing
620 (defun reb-count-subexps (re)
621 "Return number of sub-expressions in the regexp RE."
622
623 (let ((i 0) (beg 0))
624 (while (string-match "\\\\(" re beg)
625 (setq i (1+ i)
626 beg (match-end 0)))
627 i))
628
629
630 (defun reb-update-overlays (&optional subexp)
631 "Switch to `reb-target-buffer' and mark all matches of `reb-regexp'.
632 If SUBEXP is non-nil mark only the corresponding sub-expressions."
633
634 (let* ((re (reb-target-binding reb-regexp))
635 (subexps (reb-count-subexps re))
636 (matches 0)
637 (submatches 0)
638 firstmatch)
639 (save-excursion
640 (set-buffer reb-target-buffer)
641 (reb-delete-overlays)
642 (goto-char (point-min))
643 (while (and (re-search-forward re (point-max) t)
644 (or (not reb-auto-match-limit)
645 (< matches reb-auto-match-limit)))
646 (if (= 0 (length (match-string 0)))
647 (error "Empty regular expression!"))
648 (let ((i 0))
649 (setq matches (1+ matches))
650 (while (<= i subexps)
651 (if (and (or (not subexp) (= subexp i))
652 (match-beginning i))
653 (let ((overlay (make-overlay (match-beginning i)
654 (match-end i)))
655 (face-name (format "reb-match-%d" i)))
656 (if (not firstmatch)
657 (setq firstmatch (match-data)))
658 (setq reb-overlays (cons overlay reb-overlays)
659 submatches (1+ submatches))
660 (overlay-put
661 overlay 'face
662 (or (intern-soft face-name)
663 (error "Too many subexpressions - face `%s' not defined"
664 face-name )))
665 (overlay-put overlay 'priority i)))
666 (setq i (1+ i))))))
667 (let ((count (if subexp submatches matches)))
668 (message "%s %smatch%s%s"
669 (if (= 0 count) "No" (int-to-string count))
670 (if subexp "subexpression " "")
671 (if (= 1 count) "" "es")
672 (if (and reb-auto-match-limit
673 (= reb-auto-match-limit count))
674 " (limit reached)" "")))
675 (if firstmatch
676 (progn (store-match-data firstmatch)
677 (reb-show-subexp (or subexp 0))))))
678
679 (provide 're-builder)
680
681 ;;; arch-tag: 5c5515ac-4085-4524-a421-033f44f032e7
682 ;;; re-builder.el ends here