Improve Tramp traces.
[emacs.git] / lisp / emacs-lisp / re-builder.el
Commit [+]AuthorDateLineData
2fbc1934 Juanma Barranquero2011-04-04 11:35:16 +02001;;; re-builder.el --- building Regexps with visual feedback -*- lexical-binding: t -*-
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +00002
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08003;; Copyright (C) 1999-2015 Free Software Foundation, Inc.
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +00004
5;; Author: Detlev Zundel <dzu@gnu.org>
6;; Keywords: matching, lisp, tools
7
8;; This file is part of GNU Emacs.
9
d6cba7ae Glenn Morris2008-05-06 03:21:21 +000010;; GNU Emacs is free software: you can redistribute it and/or modify
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +000011;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
Glenn Morris2008-05-06 03:21:21 +000012;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +000014
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
d6cba7ae Glenn Morris2008-05-06 03:21:21 +000021;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +000022
23;;; Commentary:
24
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +000025;; When I have to come up with regular expressions that are more
26;; complex than simple string matchers, especially if they contain sub
27;; expressions, I find myself spending quite some time in the
28;; `development cycle'. `re-builder' aims to shorten this time span
29;; so I can get on with the more interesting bits.
30
31;; With it you can have immediate visual feedback about how well the
32;; regexp behaves to your expectations on the intended data.
33
34;; When called up `re-builder' attaches itself to the current buffer
35;; which becomes its target buffer, where all the matching is done.
36;; The active window is split so you have a view on the data while
37;; authoring the RE. If the edited expression is valid the matches in
38;; the target buffer are marked automatically with colored overlays
39;; (for non-color displays see below) giving you feedback over the
40;; extents of the matched (sub) expressions. The (non-)validity is
37269466 Chong Yidong2012-06-02 18:56:09 +080041;; shown only in the mode line without throwing the errors at you. If
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +000042;; you want to know the reason why RE Builder considers it as invalid
43;; call `reb-force-update' ("\C-c\C-u") which should reveal the error.
44
84c7e2dc Eli Zaretskii2001-11-20 18:20:56 +000045;; The target buffer can be changed with `reb-change-target-buffer'
ee1fcbdd John Paul Wallington2004-08-17 09:58:06 +000046;; ("\C-c\C-b"). Changing the target buffer automatically removes
84c7e2dc
EZ
Eli Zaretskii2001-11-20 18:20:56 +000047;; the overlays from the old buffer and displays the new one in the
48;; target window.
49
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +000050;; The `re-builder' keeps the focus while updating the matches in the
51;; target buffer so corrections are easy to incorporate. If you are
52;; satisfied with the result you can paste the RE to the kill-ring
53;; with `reb-copy' ("\C-c\C-w"), quit the `re-builder' ("\C-c\C-q")
54;; and use it wherever you need it.
55
56;; As the automatic updates can take some time on large buffers, they
57;; can be limited by `reb-auto-match-limit' so that they should not
58;; have a negative impact on the editing. Setting it to nil makes
59;; even the auto updates go all the way. Forcing an update overrides
60;; this limit allowing an easy way to see all matches.
61
723ee192
SM
Stefan Monnier2010-12-26 18:17:09 -050062;; Currently `re-builder' understands three different forms of input,
63;; namely `read', `string', and `rx' syntax. Read
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +000064;; syntax and string syntax are both delimited by `"'s and behave
65;; according to their name. With the `string' syntax there's no need
66;; to escape the backslashes and double quotes simplifying the editing
d7975e20 John Paul Wallington2005-01-06 21:58:17 +000067;; somewhat. The other three allow editing of symbolic regular
128440c9 Aaron S. Hawley2010-07-28 01:38:46 +020068;; expressions supported by the packages of the same name.
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +000069
70;; Editing symbolic expressions is done through a major mode derived
71;; from `emacs-lisp-mode' so you'll get all the good stuff like
72;; automatic indentation and font-locking etc.
73
74;; When editing a symbolic regular expression, only the first
75;; expression in the RE Builder buffer is considered, which helps
76;; limiting the extent of the expression like the `"'s do for the text
723ee192 Stefan Monnier2010-12-26 18:17:09 -050077;; modes. For the `rx' syntax the function `rx-to-string' is applied to
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +000078;; the evaluated expression read. So you can use quoted arguments
79;; with something like '("findme") or you can construct arguments to
80;; your hearts delight with a valid ELisp expression. (The compiled
81;; string form will be copied by `reb-copy') If you want to take
82;; a glance at the corresponding string you can temporarily change the
83;; input syntax.
84
85;; Changing the input syntax is transparent (for the obvious exception
86;; non-symbolic -> symbolic) so you can change your mind as often as
87;; you like.
88
89;; There is also a shortcut function for toggling the
90;; `case-fold-search' variable in the target buffer with an immediate
91;; update.
92
93
94;; Q: But what if my display cannot show colored overlays?
95;; A: Then the cursor will flash around the matched text making it stand
96;; out.
97
98;; Q: But how can I then make out the sub-expressions?
99;; A: Thats where the `sub-expression mode' comes in. In it only the
100;; digit keys are assigned to perform an update that will flash the
101;; corresponding subexp only.
102
103
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000104;;; Code:
105
106;; On XEmacs, load the overlay compatibility library
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000107(unless (fboundp 'make-overlay)
108 (require 'overlay))
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +0000109
fc7d3ac5 Eli Zaretskii2006-05-05 13:02:14 +0000110;; User customizable variables
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000111(defgroup re-builder nil
112 "Options for the RE Builder."
113 :group 'lisp
114 :prefix "reb-")
115
116(defcustom reb-blink-delay 0.5
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000117 "Seconds to blink cursor for next/previous match in RE Builder."
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000118 :group 're-builder
119 :type 'number)
120
121(defcustom reb-mode-hook nil
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000122 "Hooks to run on entering RE Builder mode."
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000123 :group 're-builder
124 :type 'hook)
125
126(defcustom reb-re-syntax 'read
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000127 "Syntax for the REs in the RE Builder.
723ee192 Stefan Monnier2010-12-26 18:17:09 -0500128Can either be `read', `string', or `rx'."
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000129 :group 're-builder
130 :type '(choice (const :tag "Read syntax" read)
131 (const :tag "String syntax" string)
7b1730dd Richard M. Stallman2007-03-05 20:27:34 +0000132 (const :tag "`rx' syntax" rx)))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000133
134(defcustom reb-auto-match-limit 200
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000135 "Positive integer limiting the matches for RE Builder auto updates.
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000136Set it to nil if you don't want limits here."
137 :group 're-builder
138 :type '(restricted-sexp :match-alternatives
139 (integerp 'nil)))
140
141
142(defface reb-match-0
bf1ec482
MB
Miles Bader2001-11-22 14:55:55 +0000143 '((((class color) (background light))
144 :background "lightblue")
145 (((class color) (background dark))
146 :background "steelblue4")
147 (t
148 :inverse-video t))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000149 "Used for displaying the whole match."
150 :group 're-builder)
151
152(defface reb-match-1
bf1ec482
MB
Miles Bader2001-11-22 14:55:55 +0000153 '((((class color) (background light))
154 :background "aquamarine")
155 (((class color) (background dark))
156 :background "blue3")
157 (t
158 :inverse-video t))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000159 "Used for displaying the first matching subexpression."
160 :group 're-builder)
161
162(defface reb-match-2
bf1ec482
MB
Miles Bader2001-11-22 14:55:55 +0000163 '((((class color) (background light))
164 :background "springgreen")
165 (((class color) (background dark))
166 :background "chartreuse4")
167 (t
168 :inverse-video t))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000169 "Used for displaying the second matching subexpression."
170 :group 're-builder)
171
172(defface reb-match-3
ea81d57e
DN
Dan Nicolaescu2005-04-08 14:26:13 +0000173 '((((min-colors 88) (class color) (background light))
174 :background "yellow1")
175 (((class color) (background light))
bf1ec482
MB
Miles Bader2001-11-22 14:55:55 +0000176 :background "yellow")
177 (((class color) (background dark))
178 :background "sienna4")
179 (t
180 :inverse-video t))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000181 "Used for displaying the third matching subexpression."
182 :group 're-builder)
183
184;; Internal variables below
185(defvar reb-mode nil
186 "Enables the RE Builder minor mode.")
187
188(defvar reb-target-buffer nil
189 "Buffer to which the RE is applied to.")
190
191(defvar reb-target-window nil
192 "Window to which the RE is applied to.")
193
194(defvar reb-regexp nil
195 "Last regexp used by RE Builder.")
196
197(defvar reb-regexp-src nil
198 "Last regexp used by RE Builder before processing it.
199Except for Lisp syntax this is the same as `reb-regexp'.")
200
201(defvar reb-overlays nil
202 "List of overlays of the RE Builder.")
203
204(defvar reb-window-config nil
205 "Old window configuration.")
206
207(defvar reb-subexp-mode nil
208 "Indicates whether sub-exp mode is active.")
209
210(defvar reb-subexp-displayed nil
211 "Indicates which sub-exp is active.")
212
213(defvar reb-mode-string ""
214 "String in mode line for additional info.")
215
216(defvar reb-valid-string ""
217 "String in mode line showing validity of RE.")
218
219(make-variable-buffer-local 'reb-overlays)
220(make-variable-buffer-local 'reb-regexp)
221(make-variable-buffer-local 'reb-regexp-src)
222
223(defconst reb-buffer "*RE-Builder*"
224 "Buffer to use for the RE Builder.")
225
226;; Define the local "\C-c" keymap
ee1fcbdd John Paul Wallington2004-08-17 09:58:06 +0000227(defvar reb-mode-map
7cd25617
DN
Dan Nicolaescu2008-04-04 22:45:01 +0000228 (let ((map (make-sparse-keymap))
229 (menu-map (make-sparse-keymap)))
ee1fcbdd
JPW
John Paul Wallington2004-08-17 09:58:06 +0000230 (define-key map "\C-c\C-c" 'reb-toggle-case)
231 (define-key map "\C-c\C-q" 'reb-quit)
232 (define-key map "\C-c\C-w" 'reb-copy)
233 (define-key map "\C-c\C-s" 'reb-next-match)
234 (define-key map "\C-c\C-r" 'reb-prev-match)
235 (define-key map "\C-c\C-i" 'reb-change-syntax)
236 (define-key map "\C-c\C-e" 'reb-enter-subexp-mode)
237 (define-key map "\C-c\C-b" 'reb-change-target-buffer)
238 (define-key map "\C-c\C-u" 'reb-force-update)
7cd25617
DN
Dan Nicolaescu2008-04-04 22:45:01 +0000239 (define-key map [menu-bar reb-mode] (cons "Re-Builder" menu-map))
240 (define-key menu-map [rq]
241 '(menu-item "Quit" reb-quit
242 :help "Quit the RE Builder mode"))
243 (define-key menu-map [rt]
244 '(menu-item "Case sensitive" reb-toggle-case
d6476407
KM
Keitaro Miyazaki2011-01-22 15:54:27 -0500245 :button (:toggle . (with-current-buffer
246 reb-target-buffer
247 (null case-fold-search)))
febf3e6f Juanma Barranquero2008-11-29 20:25:54 +0000248 :help "Toggle case sensitivity of searches for RE Builder target buffer"))
7cd25617
DN
Dan Nicolaescu2008-04-04 22:45:01 +0000249 (define-key menu-map [rb]
250 '(menu-item "Change target buffer..." reb-change-target-buffer
251 :help "Change the target buffer and display it in the target window"))
252 (define-key menu-map [rs]
253 '(menu-item "Change syntax..." reb-change-syntax
254 :help "Change the syntax used by the RE Builder"))
255 (define-key menu-map [re]
256 '(menu-item "Enter subexpression mode" reb-enter-subexp-mode
257 :help "Enter the subexpression mode in the RE Builder"))
258 (define-key menu-map [ru]
259 '(menu-item "Force update" reb-force-update
260 :help "Force an update in the RE Builder target window without a match limit"))
261 (define-key menu-map [rn]
262 '(menu-item "Go to next match" reb-next-match
263 :help "Go to next match in the RE Builder target window"))
264 (define-key menu-map [rp]
265 '(menu-item "Go to previous match" reb-prev-match
266 :help "Go to previous match in the RE Builder target window"))
267 (define-key menu-map [rc]
268 '(menu-item "Copy current RE" reb-copy
269 :help "Copy current RE into the kill ring for later insertion"))
ee1fcbdd John Paul Wallington2004-08-17 09:58:06 +0000270 map)
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000271 "Keymap used by the RE Builder.")
272
febf3e6f
JB
Juanma Barranquero2008-11-29 20:25:54 +0000273(define-derived-mode reb-mode nil "RE Builder"
274 "Major mode for interactively building Regular Expressions."
9fc91759 Juanma Barranquero2006-11-12 05:28:37 +0000275 (set (make-local-variable 'blink-matching-paren) nil)
febf3e6f Juanma Barranquero2008-11-29 20:25:54 +0000276 (reb-mode-common))
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +0000277
b016851c
SM
Stefan Monnier2011-02-10 11:56:00 -0500278(defvar reb-lisp-mode-map
279 (let ((map (make-sparse-keymap)))
280 ;; Use the same "\C-c" keymap as `reb-mode' and use font-locking from
281 ;; `emacs-lisp-mode'
282 (define-key map "\C-c" (lookup-key reb-mode-map "\C-c"))
283 map))
284
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000285(define-derived-mode reb-lisp-mode
286 emacs-lisp-mode "RE Builder Lisp"
d6b3b5f4 Stefan Monnier2001-11-18 01:44:08 +0000287 "Major mode for interactively building symbolic Regular Expressions."
128440c9 Aaron S. Hawley2010-07-28 01:38:46 +0200288 ;; Pull in packages as needed
723ee192
SM
Stefan Monnier2010-12-26 18:17:09 -0500289 (cond ((memq reb-re-syntax '(sregex rx)) ; rx-to-string is autoloaded
290 (require 'rx))) ; require rx anyway
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000291 (reb-mode-common))
292
a1506d29 Juanma Barranquero2003-02-04 13:24:35 +0000293(defvar reb-subexp-mode-map
d6b3b5f4
SM
Stefan Monnier2001-11-18 01:44:08 +0000294 (let ((m (make-keymap)))
295 (suppress-keymap m)
296 ;; Again share the "\C-c" keymap for the commands
297 (define-key m "\C-c" (lookup-key reb-mode-map "\C-c"))
298 (define-key m "q" 'reb-quit-subexp-mode)
299 (dotimes (digit 10)
300 (define-key m (int-to-string digit) 'reb-display-subexp))
301 m)
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000302 "Keymap used by the RE Builder for the subexpression mode.")
303
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000304(defun reb-mode-common ()
305 "Setup functions common to functions `reb-mode' and `reb-mode-lisp'."
306
307 (setq reb-mode-string ""
308 reb-valid-string ""
309 mode-line-buffer-identification
310 '(25 . ("%b" reb-mode-string reb-valid-string)))
311 (reb-update-modestring)
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000312 (add-hook 'after-change-functions 'reb-auto-update nil t)
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +0000313 ;; At least make the overlays go away if the buffer is killed
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000314 (add-hook 'kill-buffer-hook 'reb-kill-buffer nil t)
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000315 (reb-auto-update nil nil nil))
316
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000317(defun reb-color-display-p ()
318 "Return t if display is capable of displaying colors."
319 (eq 'color
320 ;; emacs/xemacs compatibility
321 (if (fboundp 'frame-parameter)
3b734d1c Dmitry Antipov2013-07-31 10:52:26 +0400322 (frame-parameter nil 'display-type)
5834812a
RS
Richard M. Stallman2005-07-16 17:35:09 +0000323 (if (fboundp 'frame-property)
324 (frame-property (selected-frame) 'display-type)))))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000325
326(defsubst reb-lisp-syntax-p ()
327 "Return non-nil if RE Builder uses a Lisp syntax."
128440c9 Aaron S. Hawley2010-07-28 01:38:46 +0200328 (memq reb-re-syntax '(sregex rx)))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000329
330(defmacro reb-target-binding (symbol)
331 "Return binding for SYMBOL in the RE Builder target buffer."
332 `(with-current-buffer reb-target-buffer ,symbol))
333
78690f09
JB
Juanma Barranquero2005-07-24 02:29:14 +0000334(defun reb-initialize-buffer ()
335 "Initialize the current buffer as a RE Builder buffer."
336 (erase-buffer)
337 (reb-insert-regexp)
338 (goto-char (+ 2 (point-min)))
339 (cond ((reb-lisp-syntax-p)
340 (reb-lisp-mode))
2644eb3f
JB
Juanma Barranquero2008-11-28 23:54:23 +0000341 (t (reb-mode)))
342 (reb-do-update))
78690f09 Juanma Barranquero2005-07-24 02:29:14 +0000343
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000344(defun reb-mode-buffer-p ()
345 "Return non-nil if the current buffer is a RE Builder buffer."
346 (memq major-mode '(reb-mode reb-lisp-mode)))
347
e342a48a
RS
Richard M. Stallman2005-02-27 10:35:51 +0000348;;; This is to help people find this in Apropos.
349;;;###autoload
bbb319fb Juanma Barranquero2005-07-21 09:10:49 +0000350(defalias 'regexp-builder 're-builder)
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000351
352;;;###autoload
353(defun re-builder ()
23db196e
CY
Chong Yidong2011-05-28 14:48:41 -0400354 "Construct a regexp interactively.
355This command makes the current buffer the \"target\" buffer of
356the regexp builder. It displays a buffer named \"*RE-Builder*\"
357in another window, initially containing an empty regexp.
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +0000358
23db196e
CY
Chong Yidong2011-05-28 14:48:41 -0400359As you edit the regexp in the \"*RE-Builder*\" buffer, the
360matching parts of the target buffer will be highlighted."
361 (interactive)
38132149 Eli Zaretskii2001-11-20 18:26:55 +0000362 (if (and (string= (buffer-name) reb-buffer)
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000363 (reb-mode-buffer-p))
38132149 Eli Zaretskii2001-11-20 18:26:55 +0000364 (message "Already in the RE Builder")
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000365 (when reb-target-buffer
366 (reb-delete-overlays))
38132149 Eli Zaretskii2001-11-20 18:26:55 +0000367 (setq reb-target-buffer (current-buffer)
2644eb3f
JB
Juanma Barranquero2008-11-28 23:54:23 +0000368 reb-target-window (selected-window))
369 (select-window (or (get-buffer-window reb-buffer)
370 (progn
371 (setq reb-window-config (current-window-configuration))
372 (split-window (selected-window) (- (window-height) 4)))))
38132149 Eli Zaretskii2001-11-20 18:26:55 +0000373 (switch-to-buffer (get-buffer-create reb-buffer))
78690f09 Juanma Barranquero2005-07-24 02:29:14 +0000374 (reb-initialize-buffer)))
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +0000375
84c7e2dc
EZ
Eli Zaretskii2001-11-20 18:20:56 +0000376(defun reb-change-target-buffer (buf)
377 "Change the target buffer and display it in the target window."
378 (interactive "bSet target buffer to: ")
379
380 (let ((buffer (get-buffer buf)))
381 (if (not buffer)
382 (error "No such buffer")
383 (reb-delete-overlays)
384 (setq reb-target-buffer buffer)
385 (reb-do-update
386 (if reb-subexp-mode reb-subexp-displayed nil))
387 (reb-update-modestring))))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000388
389(defun reb-force-update ()
ee1fcbdd John Paul Wallington2004-08-17 09:58:06 +0000390 "Force an update in the RE Builder target window without a match limit."
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000391 (interactive)
392
393 (let ((reb-auto-match-limit nil))
394 (reb-update-overlays
395 (if reb-subexp-mode reb-subexp-displayed nil))))
396
397(defun reb-quit ()
398 "Quit the RE Builder mode."
399 (interactive)
400
401 (setq reb-subexp-mode nil
402 reb-subexp-displayed nil)
403 (reb-delete-overlays)
404 (bury-buffer)
405 (set-window-configuration reb-window-config))
406
407(defun reb-next-match ()
408 "Go to next match in the RE Builder target window."
409 (interactive)
410
411 (reb-assert-buffer-in-window)
78690f09 Juanma Barranquero2005-07-24 02:29:14 +0000412 (with-selected-window reb-target-window
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +0000413 (if (not (re-search-forward reb-regexp (point-max) t))
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000414 (message "No more matches")
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000415 (reb-show-subexp
416 (or (and reb-subexp-mode reb-subexp-displayed) 0)
417 t))))
418
419(defun reb-prev-match ()
420 "Go to previous match in the RE Builder target window."
421 (interactive)
422
423 (reb-assert-buffer-in-window)
78690f09
JB
Juanma Barranquero2005-07-24 02:29:14 +0000424 (with-selected-window reb-target-window
425 (let ((p (point)))
426 (goto-char (1- p))
427 (if (re-search-backward reb-regexp (point-min) t)
428 (reb-show-subexp
429 (or (and reb-subexp-mode reb-subexp-displayed) 0)
430 t)
431 (goto-char p)
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000432 (message "No more matches")))))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000433
434(defun reb-toggle-case ()
435 "Toggle case sensitivity of searches for RE Builder target buffer."
436 (interactive)
437
438 (with-current-buffer reb-target-buffer
439 (setq case-fold-search (not case-fold-search)))
440 (reb-update-modestring)
441 (reb-auto-update nil nil nil t))
442
443(defun reb-copy ()
444 "Copy current RE into the kill ring for later insertion."
445 (interactive)
446
447 (reb-update-regexp)
448 (let ((re (with-output-to-string
449 (print (reb-target-binding reb-regexp)))))
450 (kill-new (substring re 1 (1- (length re))))
451 (message "Regexp copied to kill-ring")))
452
453;; The subexpression mode is not electric because the number of
454;; matches should be seen rather than a prompt.
455(defun reb-enter-subexp-mode ()
456 "Enter the subexpression mode in the RE Builder."
457 (interactive)
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000458 (setq reb-subexp-mode t)
459 (reb-update-modestring)
460 (use-local-map reb-subexp-mode-map)
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000461 (message "`0'-`9' to display subexpressions `q' to quit subexp mode"))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000462
463(defun reb-show-subexp (subexp &optional pause)
464 "Visually show limit of subexpression SUBEXP of recent search.
465On color displays this just puts point to the end of the expression as
466the match should already be marked by an overlay.
467On other displays jump to the beginning and the end of it.
468If the optional PAUSE is non-nil then pause at the end in any case."
78690f09 Juanma Barranquero2005-07-24 02:29:14 +0000469 (with-selected-window reb-target-window
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000470 (unless (reb-color-display-p)
471 (goto-char (match-beginning subexp))
472 (sit-for reb-blink-delay))
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +0000473 (goto-char (match-end subexp))
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000474 (when (or (not (reb-color-display-p)) pause)
475 (sit-for reb-blink-delay))))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000476
477(defun reb-quit-subexp-mode ()
478 "Quit the subexpression mode in the RE Builder."
479 (interactive)
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000480 (setq reb-subexp-mode nil
481 reb-subexp-displayed nil)
482 (reb-update-modestring)
483 (use-local-map reb-mode-map)
484 (reb-do-update))
485
486(defun reb-change-syntax (&optional syntax)
487 "Change the syntax used by the RE Builder.
488Optional argument SYNTAX must be specified if called non-interactively."
489 (interactive
490 (list (intern
491 (completing-read "Select syntax: "
492 (mapcar (lambda (el) (cons (symbol-name el) 1))
128440c9 Aaron S. Hawley2010-07-28 01:38:46 +0200493 '(read string sregex rx))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000494 nil t (symbol-name reb-re-syntax)))))
495
128440c9 Aaron S. Hawley2010-07-28 01:38:46 +0200496 (if (memq syntax '(read string sregex rx))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000497 (let ((buffer (get-buffer reb-buffer)))
498 (setq reb-re-syntax syntax)
78690f09
JB
Juanma Barranquero2005-07-24 02:29:14 +0000499 (when buffer
500 (with-current-buffer buffer
501 (reb-initialize-buffer))))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000502 (error "Invalid syntax: %s" syntax)))
503
504
505;; Non-interactive functions below
506(defun reb-do-update (&optional subexp)
507 "Update matches in the RE Builder target window.
508If SUBEXP is non-nil mark only the corresponding sub-expressions."
509
510 (reb-assert-buffer-in-window)
511 (reb-update-regexp)
512 (reb-update-overlays subexp))
513
2fbc1934 Juanma Barranquero2011-04-04 11:35:16 +0200514(defun reb-auto-update (_beg _end _lenold &optional force)
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +0000515 "Called from `after-update-functions' to update the display.
403532ee Juanma Barranquero2004-05-07 00:53:56 +0000516BEG, END and LENOLD are passed in from the hook.
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000517An actual update is only done if the regexp has changed or if the
518optional fourth argument FORCE is non-nil."
519 (let ((prev-valid reb-valid-string)
520 (new-valid
521 (condition-case nil
522 (progn
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000523 (when (or (reb-update-regexp) force)
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000524 (reb-do-update))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000525 "")
526 (error " *invalid*"))))
527 (setq reb-valid-string new-valid)
528 (force-mode-line-update)
529
530 ;; Through the caching of the re a change invalidating the syntax
531 ;; for symbolic expressions will not delete the overlays so we
532 ;; catch it here
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000533 (when (and (reb-lisp-syntax-p)
534 (not (string= prev-valid new-valid))
535 (string= prev-valid ""))
536 (reb-delete-overlays))))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000537
538(defun reb-delete-overlays ()
539 "Delete all RE Builder overlays in the `reb-target-buffer' buffer."
4361b6c2 Juanma Barranquero2008-03-25 16:49:22 +0000540 (when (buffer-live-p reb-target-buffer)
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +0000541 (with-current-buffer reb-target-buffer
90ce9554 Juanma Barranquero2007-09-26 00:15:01 +0000542 (mapc 'delete-overlay reb-overlays)
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000543 (setq reb-overlays nil))))
544
545(defun reb-assert-buffer-in-window ()
546 "Assert that `reb-target-buffer' is displayed in `reb-target-window'."
547
548 (if (not (eq reb-target-buffer (window-buffer reb-target-window)))
549 (set-window-buffer reb-target-window reb-target-buffer)))
550
551(defun reb-update-modestring ()
552 "Update the variable `reb-mode-string' displayed in the mode line."
553 (setq reb-mode-string
554 (concat
555 (if reb-subexp-mode
5017058b Eli Zaretskii2001-01-31 08:29:28 +0000556 (format " (subexp %s)" (or reb-subexp-displayed "-"))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000557 "")
558 (if (not (reb-target-binding case-fold-search))
559 " Case"
560 "")))
561 (force-mode-line-update))
562
563(defun reb-display-subexp (&optional subexp)
564 "Highlight only subexpression SUBEXP in the RE Builder."
565 (interactive)
566
567 (setq reb-subexp-displayed
61a846fb Glenn Morris2009-01-09 04:29:16 +0000568 (or subexp (string-to-number (format "%c" last-command-event))))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000569 (reb-update-modestring)
570 (reb-do-update reb-subexp-displayed))
571
572(defun reb-kill-buffer ()
573 "When the RE Builder buffer is killed make sure no overlays stay around."
574
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000575 (when (reb-mode-buffer-p)
576 (reb-delete-overlays)))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000577
578
579;; The next functions are the interface between the regexp and
580;; its textual representation in the RE Builder buffer.
581;; They are the only functions concerned with the actual syntax
582;; being used.
583(defun reb-read-regexp ()
584 "Read current RE."
585 (save-excursion
586 (cond ((eq reb-re-syntax 'read)
587 (goto-char (point-min))
588 (read (current-buffer)))
589 ((eq reb-re-syntax 'string)
590 (goto-char (point-min))
591 (re-search-forward "\"")
592 (let ((beg (point)))
593 (goto-char (point-max))
594 (re-search-backward "\"")
595 (buffer-substring-no-properties beg (point))))
596 ((reb-lisp-syntax-p)
597 (buffer-string)))))
598
599(defun reb-empty-regexp ()
600 "Return empty RE for current syntax."
601 (cond ((reb-lisp-syntax-p) "'()")
602 (t "")))
603
604(defun reb-insert-regexp ()
605 "Insert current RE."
606
607 (let ((re (or (reb-target-binding reb-regexp)
608 (reb-empty-regexp))))
609 (cond ((eq reb-re-syntax 'read)
610 (print re (current-buffer)))
611 ((eq reb-re-syntax 'string)
612 (insert "\n\"" re "\""))
613 ;; For the Lisp syntax we need the "source" of the regexp
614 ((reb-lisp-syntax-p)
615 (insert (or (reb-target-binding reb-regexp-src)
616 (reb-empty-regexp)))))))
617
618(defun reb-cook-regexp (re)
619 "Return RE after processing it according to `reb-re-syntax'."
723ee192 Stefan Monnier2010-12-26 18:17:09 -0500620 (cond ((memq reb-re-syntax '(sregex rx))
8bd6323a John Paul Wallington2004-08-07 12:03:54 +0000621 (rx-to-string (eval (car (read-from-string re)))))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000622 (t re)))
623
624(defun reb-update-regexp ()
625 "Update the regexp for the target buffer.
626Return t if the (cooked) expression changed."
627 (let* ((re-src (reb-read-regexp))
628 (re (reb-cook-regexp re-src)))
629 (with-current-buffer reb-target-buffer
630 (let ((oldre reb-regexp))
631 (prog1
632 (not (string= oldre re))
633 (setq reb-regexp re)
634 ;; Only update the source re for the lisp formats
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000635 (when (reb-lisp-syntax-p)
636 (setq reb-regexp-src re-src)))))))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000637
638
639;; And now the real core of the whole thing
640(defun reb-count-subexps (re)
641 "Return number of sub-expressions in the regexp RE."
642
643 (let ((i 0) (beg 0))
644 (while (string-match "\\\\(" re beg)
645 (setq i (1+ i)
646 beg (match-end 0)))
647 i))
648
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000649(defun reb-update-overlays (&optional subexp)
650 "Switch to `reb-target-buffer' and mark all matches of `reb-regexp'.
651If SUBEXP is non-nil mark only the corresponding sub-expressions."
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000652 (let* ((re (reb-target-binding reb-regexp))
653 (subexps (reb-count-subexps re))
654 (matches 0)
655 (submatches 0)
656 firstmatch)
9a529312 Stefan Monnier2009-10-31 02:38:34 +0000657 (with-current-buffer reb-target-buffer
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000658 (reb-delete-overlays)
659 (goto-char (point-min))
640eb069
JB
Juanma Barranquero2007-03-18 14:34:18 +0000660 (while (and (not (eobp))
661 (re-search-forward re (point-max) t)
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000662 (or (not reb-auto-match-limit)
663 (< matches reb-auto-match-limit)))
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000664 (when (and (= 0 (length (match-string 0)))
665 (not (eobp)))
666 (forward-char 1))
fc7d3ac5
EZ
Eli Zaretskii2006-05-05 13:02:14 +0000667 (let ((i 0)
668 suffix max-suffix)
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000669 (setq matches (1+ matches))
670 (while (<= i subexps)
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000671 (when (and (or (not subexp) (= subexp i))
672 (match-beginning i))
673 (let ((overlay (make-overlay (match-beginning i)
674 (match-end i)))
675 ;; When we have exceeded the number of provided faces,
676 ;; cycle thru them where `max-suffix' denotes the maximum
677 ;; suffix for `reb-match-*' that has been defined and
678 ;; `suffix' the suffix calculated for the current match.
679 (face
680 (cond
681 (max-suffix
682 (if (= suffix max-suffix)
683 (setq suffix 1)
684 (setq suffix (1+ suffix)))
685 (intern-soft (format "reb-match-%d" suffix)))
686 ((intern-soft (format "reb-match-%d" i)))
687 ((setq max-suffix (1- i))
688 (setq suffix 1)
689 ;; `reb-match-1' must exist.
690 'reb-match-1))))
691 (unless firstmatch (setq firstmatch (match-data)))
692 (setq reb-overlays (cons overlay reb-overlays)
693 submatches (1+ submatches))
694 (overlay-put overlay 'face face)
695 (overlay-put overlay 'priority i)))
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000696 (setq i (1+ i))))))
697 (let ((count (if subexp submatches matches)))
8bd6323a John Paul Wallington2004-08-07 12:03:54 +0000698 (message "%s %smatch%s%s"
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000699 (if (= 0 count) "No" (int-to-string count))
700 (if subexp "subexpression " "")
7c20a7a9 John Paul Wallington2004-07-09 17:16:59 +0000701 (if (= 1 count) "" "es")
d1221ea9
GM
Gerd Moellmann2000-03-09 20:28:18 +0000702 (if (and reb-auto-match-limit
703 (= reb-auto-match-limit count))
704 " (limit reached)" "")))
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000705 (when firstmatch
706 (store-match-data firstmatch)
707 (reb-show-subexp (or subexp 0)))))
708
709;; The End
710(defun re-builder-unload-function ()
711 "Unload the RE Builder library."
712 (when (buffer-live-p (get-buffer reb-buffer))
713 (with-current-buffer reb-buffer
714 (remove-hook 'after-change-functions 'reb-auto-update t)
715 (remove-hook 'kill-buffer-hook 'reb-kill-buffer t)
716 (when (reb-mode-buffer-p)
74f53697 Juanma Barranquero2011-06-25 20:08:41 +0200717 (reb-delete-overlays))))
4361b6c2
JB
Juanma Barranquero2008-03-25 16:49:22 +0000718 ;; continue standard unloading
719 nil)
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +0000720
060b279a
MR
Markus Rost2002-11-17 22:43:07 +0000721(provide 're-builder)
722
d1221ea9 Gerd Moellmann2000-03-09 20:28:18 +0000723;;; re-builder.el ends here