]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/re-builder.el
Replace `string-to-int' by `string-to-number'.
[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., 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 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-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
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."
306
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)
315
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))))
323
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)))
327
328 (defmacro reb-target-binding (symbol)
329 "Return binding for SYMBOL in the RE Builder target buffer."
330 `(with-current-buffer reb-target-buffer ,symbol))
331
332 ;;; This is to help people find this in Apropos.
333 ;;;###autoload
334 (defun regexp-builder ()
335 "Alias for `re-builder': Construct a regexp interactively."
336 (interactive)
337 (re-builder))
338
339 ;;;###autoload
340 (defun re-builder ()
341 "Construct a regexp interactively."
342 (interactive)
343
344 (if (and (string= (buffer-name) reb-buffer)
345 (memq major-mode '(reb-mode reb-lisp-mode)))
346 (message "Already in the RE Builder")
347 (if reb-target-buffer
348 (reb-delete-overlays))
349 (setq reb-target-buffer (current-buffer)
350 reb-target-window (selected-window)
351 reb-window-config (current-window-configuration))
352 (select-window (split-window (selected-window) (- (window-height) 4)))
353 (switch-to-buffer (get-buffer-create reb-buffer))
354 (erase-buffer)
355 (reb-insert-regexp)
356 (goto-char (+ 2 (point-min)))
357 (cond
358 ((reb-lisp-syntax-p)
359 (reb-lisp-mode))
360 (t (reb-mode)))))
361
362 (defun reb-change-target-buffer (buf)
363 "Change the target buffer and display it in the target window."
364 (interactive "bSet target buffer to: ")
365
366 (let ((buffer (get-buffer buf)))
367 (if (not buffer)
368 (error "No such buffer")
369 (reb-delete-overlays)
370 (setq reb-target-buffer buffer)
371 (reb-do-update
372 (if reb-subexp-mode reb-subexp-displayed nil))
373 (reb-update-modestring))))
374
375 (defun reb-force-update ()
376 "Force an update in the RE Builder target window without a match limit."
377 (interactive)
378
379 (let ((reb-auto-match-limit nil))
380 (reb-update-overlays
381 (if reb-subexp-mode reb-subexp-displayed nil))))
382
383 (defun reb-quit ()
384 "Quit the RE Builder mode."
385 (interactive)
386
387 (setq reb-subexp-mode nil
388 reb-subexp-displayed nil)
389 (reb-delete-overlays)
390 (bury-buffer)
391 (set-window-configuration reb-window-config))
392
393 (defun reb-next-match ()
394 "Go to next match in the RE Builder target window."
395 (interactive)
396
397 (reb-assert-buffer-in-window)
398 (reb-with-current-window
399 reb-target-window
400 (if (not (re-search-forward reb-regexp (point-max) t))
401 (message "No more matches.")
402 (reb-show-subexp
403 (or (and reb-subexp-mode reb-subexp-displayed) 0)
404 t))))
405
406 (defun reb-prev-match ()
407 "Go to previous match in the RE Builder target window."
408 (interactive)
409
410 (reb-assert-buffer-in-window)
411 (reb-with-current-window reb-target-window
412 (goto-char (1- (point)))
413 (if (not (re-search-backward reb-regexp (point-min) t))
414 (message "No more matches.")
415 (reb-show-subexp
416 (or (and reb-subexp-mode reb-subexp-displayed) 0)
417 t))))
418
419 (defun reb-toggle-case ()
420 "Toggle case sensitivity of searches for RE Builder target buffer."
421 (interactive)
422
423 (with-current-buffer reb-target-buffer
424 (setq case-fold-search (not case-fold-search)))
425 (reb-update-modestring)
426 (reb-auto-update nil nil nil t))
427
428 (defun reb-copy ()
429 "Copy current RE into the kill ring for later insertion."
430 (interactive)
431
432 (reb-update-regexp)
433 (let ((re (with-output-to-string
434 (print (reb-target-binding reb-regexp)))))
435 (kill-new (substring re 1 (1- (length re))))
436 (message "Regexp copied to kill-ring")))
437
438 ;; The subexpression mode is not electric because the number of
439 ;; matches should be seen rather than a prompt.
440 (defun reb-enter-subexp-mode ()
441 "Enter the subexpression mode in the RE Builder."
442 (interactive)
443 (setq reb-subexp-mode t)
444 (reb-update-modestring)
445 (use-local-map reb-subexp-mode-map)
446 (message "`0'-`9' to display subexpressions `q' to quit subexp mode."))
447
448 (defun reb-show-subexp (subexp &optional pause)
449 "Visually show limit of subexpression SUBEXP of recent search.
450 On color displays this just puts point to the end of the expression as
451 the match should already be marked by an overlay.
452 On other displays jump to the beginning and the end of it.
453 If the optional PAUSE is non-nil then pause at the end in any case."
454 (reb-with-current-window reb-target-window
455 (if (not (reb-color-display-p))
456 (progn (goto-char (match-beginning subexp))
457 (sit-for reb-blink-delay)))
458 (goto-char (match-end subexp))
459 (if (or (not (reb-color-display-p)) pause)
460 (sit-for reb-blink-delay))))
461
462 (defun reb-quit-subexp-mode ()
463 "Quit the subexpression mode in the RE Builder."
464 (interactive)
465 (setq reb-subexp-mode nil
466 reb-subexp-displayed nil)
467 (reb-update-modestring)
468 (use-local-map reb-mode-map)
469 (reb-do-update))
470
471 (defun reb-change-syntax (&optional syntax)
472 "Change the syntax used by the RE Builder.
473 Optional argument SYNTAX must be specified if called non-interactively."
474 (interactive
475 (list (intern
476 (completing-read "Select syntax: "
477 (mapcar (lambda (el) (cons (symbol-name el) 1))
478 '(read string lisp-re sregex rx))
479 nil t (symbol-name reb-re-syntax)))))
480
481 (if (memq syntax '(read string lisp-re sregex rx))
482 (let ((buffer (get-buffer reb-buffer)))
483 (setq reb-re-syntax syntax)
484 (if buffer
485 (with-current-buffer buffer
486 (erase-buffer)
487 (reb-insert-regexp)
488 (goto-char (+ 2 (point-min)))
489 (cond ((reb-lisp-syntax-p)
490 (reb-lisp-mode))
491 (t (reb-mode))))))
492 (error "Invalid syntax: %s" syntax)))
493
494
495 ;; Non-interactive functions below
496 (defun reb-do-update (&optional subexp)
497 "Update matches in the RE Builder target window.
498 If SUBEXP is non-nil mark only the corresponding sub-expressions."
499
500 (reb-assert-buffer-in-window)
501 (reb-update-regexp)
502 (reb-update-overlays subexp))
503
504 (defun reb-auto-update (beg end lenold &optional force)
505 "Called from `after-update-functions' to update the display.
506 BEG, END and LENOLD are passed in from the hook.
507 An actual update is only done if the regexp has changed or if the
508 optional fourth argument FORCE is non-nil."
509 (let ((prev-valid reb-valid-string)
510 (new-valid
511 (condition-case nil
512 (progn
513 (if (or (reb-update-regexp) force)
514 (progn
515 (reb-assert-buffer-in-window)
516 (reb-do-update)))
517 "")
518 (error " *invalid*"))))
519 (setq reb-valid-string new-valid)
520 (force-mode-line-update)
521
522 ;; Through the caching of the re a change invalidating the syntax
523 ;; for symbolic expressions will not delete the overlays so we
524 ;; catch it here
525 (if (and (reb-lisp-syntax-p)
526 (not (string= prev-valid new-valid))
527 (string= prev-valid ""))
528 (reb-delete-overlays))))
529
530 (defun reb-delete-overlays ()
531 "Delete all RE Builder overlays in the `reb-target-buffer' buffer."
532 (if (buffer-live-p reb-target-buffer)
533 (with-current-buffer reb-target-buffer
534 (mapcar 'delete-overlay reb-overlays)
535 (setq reb-overlays nil))))
536
537 (defun reb-assert-buffer-in-window ()
538 "Assert that `reb-target-buffer' is displayed in `reb-target-window'."
539
540 (if (not (eq reb-target-buffer (window-buffer reb-target-window)))
541 (set-window-buffer reb-target-window reb-target-buffer)))
542
543 (defun reb-update-modestring ()
544 "Update the variable `reb-mode-string' displayed in the mode line."
545 (setq reb-mode-string
546 (concat
547 (if reb-subexp-mode
548 (format " (subexp %s)" (or reb-subexp-displayed "-"))
549 "")
550 (if (not (reb-target-binding case-fold-search))
551 " Case"
552 "")))
553 (force-mode-line-update))
554
555 (defun reb-display-subexp (&optional subexp)
556 "Highlight only subexpression SUBEXP in the RE Builder."
557 (interactive)
558
559 (setq reb-subexp-displayed
560 (or subexp (string-to-number (format "%c" last-command-char))))
561 (reb-update-modestring)
562 (reb-do-update reb-subexp-displayed))
563
564 (defun reb-kill-buffer ()
565 "When the RE Builder buffer is killed make sure no overlays stay around."
566
567 (if (member major-mode '(reb-mode reb-lisp-mode))
568 (reb-delete-overlays)))
569
570
571 ;; The next functions are the interface between the regexp and
572 ;; its textual representation in the RE Builder buffer.
573 ;; They are the only functions concerned with the actual syntax
574 ;; being used.
575 (defun reb-read-regexp ()
576 "Read current RE."
577 (save-excursion
578 (cond ((eq reb-re-syntax 'read)
579 (goto-char (point-min))
580 (read (current-buffer)))
581 ((eq reb-re-syntax 'string)
582 (goto-char (point-min))
583 (re-search-forward "\"")
584 (let ((beg (point)))
585 (goto-char (point-max))
586 (re-search-backward "\"")
587 (buffer-substring-no-properties beg (point))))
588 ((reb-lisp-syntax-p)
589 (buffer-string)))))
590
591 (defun reb-empty-regexp ()
592 "Return empty RE for current syntax."
593 (cond ((reb-lisp-syntax-p) "'()")
594 (t "")))
595
596 (defun reb-insert-regexp ()
597 "Insert current RE."
598
599 (let ((re (or (reb-target-binding reb-regexp)
600 (reb-empty-regexp))))
601 (cond ((eq reb-re-syntax 'read)
602 (print re (current-buffer)))
603 ((eq reb-re-syntax 'string)
604 (insert "\n\"" re "\""))
605 ;; For the Lisp syntax we need the "source" of the regexp
606 ((reb-lisp-syntax-p)
607 (insert (or (reb-target-binding reb-regexp-src)
608 (reb-empty-regexp)))))))
609
610 (defun reb-cook-regexp (re)
611 "Return RE after processing it according to `reb-re-syntax'."
612 (cond ((eq reb-re-syntax 'lisp-re)
613 (lre-compile-string (eval (car (read-from-string re)))))
614 ((eq reb-re-syntax 'sregex)
615 (apply 'sregex (eval (car (read-from-string re)))))
616 ((eq reb-re-syntax 'rx)
617 (rx-to-string (eval (car (read-from-string re)))))
618 (t re)))
619
620 (defun reb-update-regexp ()
621 "Update the regexp for the target buffer.
622 Return t if the (cooked) expression changed."
623 (let* ((re-src (reb-read-regexp))
624 (re (reb-cook-regexp re-src)))
625 (with-current-buffer reb-target-buffer
626 (let ((oldre reb-regexp))
627 (prog1
628 (not (string= oldre re))
629 (setq reb-regexp re)
630 ;; Only update the source re for the lisp formats
631 (if (reb-lisp-syntax-p)
632 (setq reb-regexp-src re-src)))))))
633
634
635 ;; And now the real core of the whole thing
636 (defun reb-count-subexps (re)
637 "Return number of sub-expressions in the regexp RE."
638
639 (let ((i 0) (beg 0))
640 (while (string-match "\\\\(" re beg)
641 (setq i (1+ i)
642 beg (match-end 0)))
643 i))
644
645
646 (defun reb-update-overlays (&optional subexp)
647 "Switch to `reb-target-buffer' and mark all matches of `reb-regexp'.
648 If SUBEXP is non-nil mark only the corresponding sub-expressions."
649
650 (let* ((re (reb-target-binding reb-regexp))
651 (subexps (reb-count-subexps re))
652 (matches 0)
653 (submatches 0)
654 firstmatch)
655 (save-excursion
656 (set-buffer reb-target-buffer)
657 (reb-delete-overlays)
658 (goto-char (point-min))
659 (while (and (re-search-forward re (point-max) t)
660 (or (not reb-auto-match-limit)
661 (< matches reb-auto-match-limit)))
662 (if (= 0 (length (match-string 0)))
663 (error "Empty regular expression!"))
664 (let ((i 0))
665 (setq matches (1+ matches))
666 (while (<= i subexps)
667 (if (and (or (not subexp) (= subexp i))
668 (match-beginning i))
669 (let ((overlay (make-overlay (match-beginning i)
670 (match-end i)))
671 (face-name (format "reb-match-%d" i)))
672 (if (not firstmatch)
673 (setq firstmatch (match-data)))
674 (setq reb-overlays (cons overlay reb-overlays)
675 submatches (1+ submatches))
676 (overlay-put
677 overlay 'face
678 (or (intern-soft face-name)
679 (error "Too many subexpressions - face `%s' not defined"
680 face-name )))
681 (overlay-put overlay 'priority i)))
682 (setq i (1+ i))))))
683 (let ((count (if subexp submatches matches)))
684 (message "%s %smatch%s%s"
685 (if (= 0 count) "No" (int-to-string count))
686 (if subexp "subexpression " "")
687 (if (= 1 count) "" "es")
688 (if (and reb-auto-match-limit
689 (= reb-auto-match-limit count))
690 " (limit reached)" "")))
691 (if firstmatch
692 (progn (store-match-data firstmatch)
693 (reb-show-subexp (or subexp 0))))))
694
695 (provide 're-builder)
696
697 ;;; arch-tag: 5c5515ac-4085-4524-a421-033f44f032e7
698 ;;; re-builder.el ends here