Mercurial > emacs
annotate lisp/emacs-lisp/re-builder.el @ 61180:cc04bb9ef8b6
(ido-file-internal): Fall back to non-ido command if
initial directory is on slow ftp (or tramp) host.
author | Kim F. Storm <storm@cua.dk> |
---|---|
date | Thu, 31 Mar 2005 22:22:45 +0000 |
parents | b6fcc94c2d6a |
children | 31aa9a390538 e4694597cbf4 |
rev | line source |
---|---|
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
36012
diff
changeset
|
1 ;;; re-builder.el --- building Regexps with visual feedback |
28077 | 2 |
59396
1c58b91ca0df
Update copyright. Update commentary to mention rx syntax support.
John Paul Wallington <jpw@pobox.com>
parents:
56698
diff
changeset
|
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc. |
28077 | 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 | |
41313
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
47 ;; The target buffer can be changed with `reb-change-target-buffer' |
56698
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
48 ;; ("\C-c\C-b"). Changing the target buffer automatically removes |
41313
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
49 ;; the overlays from the old buffer and displays the new one in the |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
50 ;; target window. |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
51 |
28077 | 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 | |
59396
1c58b91ca0df
Update copyright. Update commentary to mention rx syntax support.
John Paul Wallington <jpw@pobox.com>
parents:
56698
diff
changeset
|
64 ;; Currently `re-builder' understands five different forms of input, |
1c58b91ca0df
Update copyright. Update commentary to mention rx syntax support.
John Paul Wallington <jpw@pobox.com>
parents:
56698
diff
changeset
|
65 ;; namely `read', `string', `rx', `sregex' and `lisp-re' syntax. Read |
28077 | 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 | |
59396
1c58b91ca0df
Update copyright. Update commentary to mention rx syntax support.
John Paul Wallington <jpw@pobox.com>
parents:
56698
diff
changeset
|
69 ;; somewhat. The other three allow editing of symbolic regular |
28077 | 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. | |
28097
5d7390b72a44
(reb-re-syntax): Fix typo in `:type'. Fix comment.
Gerd Moellmann <gerd@gnu.org>
parents:
28077
diff
changeset
|
132 Can either be `read', `string', `sregex' or `lisp-re'." |
28077 | 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) | |
56611
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
138 (const :tag "`rx' syntax" rx) |
28097
5d7390b72a44
(reb-re-syntax): Fix typo in `:type'. Fix comment.
Gerd Moellmann <gerd@gnu.org>
parents:
28077
diff
changeset
|
139 (value: string))) |
28077 | 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 | |
41371 | 150 '((((class color) (background light)) |
151 :background "lightblue") | |
152 (((class color) (background dark)) | |
153 :background "steelblue4") | |
154 (t | |
155 :inverse-video t)) | |
28077 | 156 "Used for displaying the whole match." |
157 :group 're-builder) | |
158 | |
159 (defface reb-match-1 | |
41371 | 160 '((((class color) (background light)) |
161 :background "aquamarine") | |
162 (((class color) (background dark)) | |
163 :background "blue3") | |
164 (t | |
165 :inverse-video t)) | |
28077 | 166 "Used for displaying the first matching subexpression." |
167 :group 're-builder) | |
168 | |
169 (defface reb-match-2 | |
41371 | 170 '((((class color) (background light)) |
171 :background "springgreen") | |
172 (((class color) (background dark)) | |
173 :background "chartreuse4") | |
174 (t | |
175 :inverse-video t)) | |
28077 | 176 "Used for displaying the second matching subexpression." |
177 :group 're-builder) | |
178 | |
179 (defface reb-match-3 | |
41371 | 180 '((((class color) (background light)) |
181 :background "yellow") | |
182 (((class color) (background dark)) | |
183 :background "sienna4") | |
184 (t | |
185 :inverse-video t)) | |
28077 | 186 "Used for displaying the third matching subexpression." |
187 :group 're-builder) | |
188 | |
189 ;; Internal variables below | |
190 (defvar reb-mode nil | |
191 "Enables the RE Builder minor mode.") | |
192 | |
193 (defvar reb-target-buffer nil | |
194 "Buffer to which the RE is applied to.") | |
195 | |
196 (defvar reb-target-window nil | |
197 "Window to which the RE is applied to.") | |
198 | |
199 (defvar reb-regexp nil | |
200 "Last regexp used by RE Builder.") | |
201 | |
202 (defvar reb-regexp-src nil | |
203 "Last regexp used by RE Builder before processing it. | |
204 Except for Lisp syntax this is the same as `reb-regexp'.") | |
205 | |
206 (defvar reb-overlays nil | |
207 "List of overlays of the RE Builder.") | |
208 | |
209 (defvar reb-window-config nil | |
210 "Old window configuration.") | |
211 | |
212 (defvar reb-subexp-mode nil | |
213 "Indicates whether sub-exp mode is active.") | |
214 | |
215 (defvar reb-subexp-displayed nil | |
216 "Indicates which sub-exp is active.") | |
217 | |
218 (defvar reb-mode-string "" | |
219 "String in mode line for additional info.") | |
220 | |
221 (defvar reb-valid-string "" | |
222 "String in mode line showing validity of RE.") | |
223 | |
224 (make-variable-buffer-local 'reb-overlays) | |
225 (make-variable-buffer-local 'reb-regexp) | |
226 (make-variable-buffer-local 'reb-regexp-src) | |
227 | |
228 (defconst reb-buffer "*RE-Builder*" | |
229 "Buffer to use for the RE Builder.") | |
230 | |
231 ;; Define the local "\C-c" keymap | |
56698
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
232 (defvar reb-mode-map |
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
233 (let ((map (make-sparse-keymap))) |
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
234 (define-key map "\C-c\C-c" 'reb-toggle-case) |
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
235 (define-key map "\C-c\C-q" 'reb-quit) |
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
236 (define-key map "\C-c\C-w" 'reb-copy) |
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
237 (define-key map "\C-c\C-s" 'reb-next-match) |
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
238 (define-key map "\C-c\C-r" 'reb-prev-match) |
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
239 (define-key map "\C-c\C-i" 'reb-change-syntax) |
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
240 (define-key map "\C-c\C-e" 'reb-enter-subexp-mode) |
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
241 (define-key map "\C-c\C-b" 'reb-change-target-buffer) |
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
242 (define-key map "\C-c\C-u" 'reb-force-update) |
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
243 map) |
28077 | 244 "Keymap used by the RE Builder.") |
245 | |
41333
fcb2aaafc8b2
(reb-mode): Don't use define-derived-mode. Call kill-all-local-variables.
Richard M. Stallman <rms@gnu.org>
parents:
41314
diff
changeset
|
246 (defun reb-mode () |
fcb2aaafc8b2
(reb-mode): Don't use define-derived-mode. Call kill-all-local-variables.
Richard M. Stallman <rms@gnu.org>
parents:
41314
diff
changeset
|
247 "Major mode for interactively building Regular Expressions. |
fcb2aaafc8b2
(reb-mode): Don't use define-derived-mode. Call kill-all-local-variables.
Richard M. Stallman <rms@gnu.org>
parents:
41314
diff
changeset
|
248 \\{reb-mode-map}" |
fcb2aaafc8b2
(reb-mode): Don't use define-derived-mode. Call kill-all-local-variables.
Richard M. Stallman <rms@gnu.org>
parents:
41314
diff
changeset
|
249 (interactive) |
fcb2aaafc8b2
(reb-mode): Don't use define-derived-mode. Call kill-all-local-variables.
Richard M. Stallman <rms@gnu.org>
parents:
41314
diff
changeset
|
250 (kill-all-local-variables) |
fcb2aaafc8b2
(reb-mode): Don't use define-derived-mode. Call kill-all-local-variables.
Richard M. Stallman <rms@gnu.org>
parents:
41314
diff
changeset
|
251 (setq major-mode 'reb-mode |
fcb2aaafc8b2
(reb-mode): Don't use define-derived-mode. Call kill-all-local-variables.
Richard M. Stallman <rms@gnu.org>
parents:
41314
diff
changeset
|
252 mode-name "RE Builder") |
fcb2aaafc8b2
(reb-mode): Don't use define-derived-mode. Call kill-all-local-variables.
Richard M. Stallman <rms@gnu.org>
parents:
41314
diff
changeset
|
253 (use-local-map reb-mode-map) |
fcb2aaafc8b2
(reb-mode): Don't use define-derived-mode. Call kill-all-local-variables.
Richard M. Stallman <rms@gnu.org>
parents:
41314
diff
changeset
|
254 (reb-mode-common) |
fcb2aaafc8b2
(reb-mode): Don't use define-derived-mode. Call kill-all-local-variables.
Richard M. Stallman <rms@gnu.org>
parents:
41314
diff
changeset
|
255 (run-hooks 'reb-mode-hook)) |
28077 | 256 |
257 (define-derived-mode reb-lisp-mode | |
258 emacs-lisp-mode "RE Builder Lisp" | |
41225
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
259 "Major mode for interactively building symbolic Regular Expressions." |
28077 | 260 (cond ((eq reb-re-syntax 'lisp-re) ; Pull in packages |
261 (require 'lisp-re)) ; as needed | |
262 ((eq reb-re-syntax 'sregex) ; sregex is not autoloaded | |
56611
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
263 (require 'sregex)) ; right now.. |
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
264 ((eq reb-re-syntax 'rx) ; rx-to-string is autoloaded |
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
265 (require 'rx))) ; require rx anyway |
28077 | 266 (reb-mode-common)) |
267 | |
268 ;; Use the same "\C-c" keymap as `reb-mode' and use font-locking from | |
269 ;; `emacs-lisp-mode' | |
270 (define-key reb-lisp-mode-map "\C-c" | |
271 (lookup-key reb-mode-map "\C-c")) | |
272 | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48379
diff
changeset
|
273 (defvar reb-subexp-mode-map |
41225
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
274 (let ((m (make-keymap))) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
275 (suppress-keymap m) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
276 ;; Again share the "\C-c" keymap for the commands |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
277 (define-key m "\C-c" (lookup-key reb-mode-map "\C-c")) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
278 (define-key m "q" 'reb-quit-subexp-mode) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
279 (dotimes (digit 10) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
280 (define-key m (int-to-string digit) 'reb-display-subexp)) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
281 m) |
28077 | 282 "Keymap used by the RE Builder for the subexpression mode.") |
283 | |
284 (defun reb-mode-common () | |
285 "Setup functions common to functions `reb-mode' and `reb-mode-lisp'." | |
286 | |
287 (setq reb-mode-string "" | |
288 reb-valid-string "" | |
289 mode-line-buffer-identification | |
290 '(25 . ("%b" reb-mode-string reb-valid-string))) | |
291 (reb-update-modestring) | |
292 (make-local-variable 'after-change-functions) | |
293 (add-hook 'after-change-functions | |
294 'reb-auto-update) | |
295 ;; At least make the overlays go away if the buffer is killed | |
296 (make-local-variable 'reb-kill-buffer) | |
297 (add-hook 'kill-buffer-hook 'reb-kill-buffer) | |
298 (reb-auto-update nil nil nil)) | |
299 | |
300 | |
301 ;; Handy macro for doing things in other windows | |
302 (defmacro reb-with-current-window (window &rest body) | |
303 "With WINDOW selected evaluate BODY forms and reselect previous window." | |
304 | |
305 (let ((oldwindow (make-symbol "*oldwindow*"))) | |
306 `(let ((,oldwindow (selected-window))) | |
307 (select-window ,window) | |
308 (unwind-protect | |
309 (progn | |
310 ,@body) | |
311 (select-window ,oldwindow))))) | |
312 (put 'reb-with-current-window 'lisp-indent-function 0) | |
313 | |
314 (defun reb-color-display-p () | |
315 "Return t if display is capable of displaying colors." | |
316 (eq 'color | |
317 ;; emacs/xemacs compatibility | |
318 (if (fboundp 'frame-parameter) | |
319 (frame-parameter (selected-frame) 'display-type) | |
320 (frame-property (selected-frame) 'display-type)))) | |
321 | |
322 (defsubst reb-lisp-syntax-p () | |
323 "Return non-nil if RE Builder uses a Lisp syntax." | |
56611
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
324 (memq reb-re-syntax '(lisp-re sregex rx))) |
28077 | 325 |
326 (defmacro reb-target-binding (symbol) | |
327 "Return binding for SYMBOL in the RE Builder target buffer." | |
328 `(with-current-buffer reb-target-buffer ,symbol)) | |
329 | |
60282
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
330 ;;; This is to help people find this in Apropos. |
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
331 ;;;###autoload |
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
332 (defun regexp-builder () |
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
333 "Alias for `re-builder': Construct a regexp interactively." |
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
334 (interactive) |
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
335 (re-builder)) |
28077 | 336 |
337 ;;;###autoload | |
338 (defun re-builder () | |
60282
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
339 "Construct a regexp interactively." |
28077 | 340 (interactive) |
341 | |
41314
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
342 (if (and (string= (buffer-name) reb-buffer) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
343 (memq major-mode '(reb-mode reb-lisp-mode))) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
344 (message "Already in the RE Builder") |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
345 (if reb-target-buffer |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
346 (reb-delete-overlays)) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
347 (setq reb-target-buffer (current-buffer) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
348 reb-target-window (selected-window) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
349 reb-window-config (current-window-configuration)) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
350 (select-window (split-window (selected-window) (- (window-height) 4))) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
351 (switch-to-buffer (get-buffer-create reb-buffer)) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
352 (erase-buffer) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
353 (reb-insert-regexp) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
354 (goto-char (+ 2 (point-min))) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
355 (cond |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
356 ((reb-lisp-syntax-p) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
357 (reb-lisp-mode)) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
358 (t (reb-mode))))) |
28077 | 359 |
41313
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
360 (defun reb-change-target-buffer (buf) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
361 "Change the target buffer and display it in the target window." |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
362 (interactive "bSet target buffer to: ") |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
363 |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
364 (let ((buffer (get-buffer buf))) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
365 (if (not buffer) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
366 (error "No such buffer") |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
367 (reb-delete-overlays) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
368 (setq reb-target-buffer buffer) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
369 (reb-do-update |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
370 (if reb-subexp-mode reb-subexp-displayed nil)) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
371 (reb-update-modestring)))) |
28077 | 372 |
373 (defun reb-force-update () | |
56698
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
374 "Force an update in the RE Builder target window without a match limit." |
28077 | 375 (interactive) |
376 | |
377 (let ((reb-auto-match-limit nil)) | |
378 (reb-update-overlays | |
379 (if reb-subexp-mode reb-subexp-displayed nil)))) | |
380 | |
381 (defun reb-quit () | |
382 "Quit the RE Builder mode." | |
383 (interactive) | |
384 | |
385 (setq reb-subexp-mode nil | |
386 reb-subexp-displayed nil) | |
387 (reb-delete-overlays) | |
388 (bury-buffer) | |
389 (set-window-configuration reb-window-config)) | |
390 | |
391 (defun reb-next-match () | |
392 "Go to next match in the RE Builder target window." | |
393 (interactive) | |
394 | |
395 (reb-assert-buffer-in-window) | |
396 (reb-with-current-window | |
397 reb-target-window | |
398 (if (not (re-search-forward reb-regexp (point-max) t)) | |
399 (message "No more matches.") | |
400 (reb-show-subexp | |
401 (or (and reb-subexp-mode reb-subexp-displayed) 0) | |
402 t)))) | |
403 | |
404 (defun reb-prev-match () | |
405 "Go to previous match in the RE Builder target window." | |
406 (interactive) | |
407 | |
408 (reb-assert-buffer-in-window) | |
409 (reb-with-current-window reb-target-window | |
410 (goto-char (1- (point))) | |
411 (if (not (re-search-backward reb-regexp (point-min) t)) | |
412 (message "No more matches.") | |
413 (reb-show-subexp | |
414 (or (and reb-subexp-mode reb-subexp-displayed) 0) | |
415 t)))) | |
416 | |
417 (defun reb-toggle-case () | |
418 "Toggle case sensitivity of searches for RE Builder target buffer." | |
419 (interactive) | |
420 | |
421 (with-current-buffer reb-target-buffer | |
422 (setq case-fold-search (not case-fold-search))) | |
423 (reb-update-modestring) | |
424 (reb-auto-update nil nil nil t)) | |
425 | |
426 (defun reb-copy () | |
427 "Copy current RE into the kill ring for later insertion." | |
428 (interactive) | |
429 | |
430 (reb-update-regexp) | |
431 (let ((re (with-output-to-string | |
432 (print (reb-target-binding reb-regexp))))) | |
433 (kill-new (substring re 1 (1- (length re)))) | |
434 (message "Regexp copied to kill-ring"))) | |
435 | |
436 ;; The subexpression mode is not electric because the number of | |
437 ;; matches should be seen rather than a prompt. | |
438 (defun reb-enter-subexp-mode () | |
439 "Enter the subexpression mode in the RE Builder." | |
440 (interactive) | |
441 (setq reb-subexp-mode t) | |
442 (reb-update-modestring) | |
443 (use-local-map reb-subexp-mode-map) | |
444 (message "`0'-`9' to display subexpressions `q' to quit subexp mode.")) | |
445 | |
446 (defun reb-show-subexp (subexp &optional pause) | |
447 "Visually show limit of subexpression SUBEXP of recent search. | |
448 On color displays this just puts point to the end of the expression as | |
449 the match should already be marked by an overlay. | |
450 On other displays jump to the beginning and the end of it. | |
451 If the optional PAUSE is non-nil then pause at the end in any case." | |
452 (reb-with-current-window reb-target-window | |
453 (if (not (reb-color-display-p)) | |
454 (progn (goto-char (match-beginning subexp)) | |
455 (sit-for reb-blink-delay))) | |
456 (goto-char (match-end subexp)) | |
457 (if (or (not (reb-color-display-p)) pause) | |
458 (sit-for reb-blink-delay)))) | |
459 | |
460 (defun reb-quit-subexp-mode () | |
461 "Quit the subexpression mode in the RE Builder." | |
462 (interactive) | |
463 (setq reb-subexp-mode nil | |
464 reb-subexp-displayed nil) | |
465 (reb-update-modestring) | |
466 (use-local-map reb-mode-map) | |
467 (reb-do-update)) | |
468 | |
469 (defun reb-change-syntax (&optional syntax) | |
470 "Change the syntax used by the RE Builder. | |
471 Optional argument SYNTAX must be specified if called non-interactively." | |
472 (interactive | |
473 (list (intern | |
474 (completing-read "Select syntax: " | |
475 (mapcar (lambda (el) (cons (symbol-name el) 1)) | |
56611
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
476 '(read string lisp-re sregex rx)) |
28077 | 477 nil t (symbol-name reb-re-syntax))))) |
478 | |
56611
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
479 (if (memq syntax '(read string lisp-re sregex rx)) |
28077 | 480 (let ((buffer (get-buffer reb-buffer))) |
481 (setq reb-re-syntax syntax) | |
482 (if buffer | |
483 (with-current-buffer buffer | |
484 (erase-buffer) | |
485 (reb-insert-regexp) | |
486 (goto-char (+ 2 (point-min))) | |
487 (cond ((reb-lisp-syntax-p) | |
488 (reb-lisp-mode)) | |
489 (t (reb-mode)))))) | |
490 (error "Invalid syntax: %s" syntax))) | |
491 | |
492 | |
493 ;; Non-interactive functions below | |
494 (defun reb-do-update (&optional subexp) | |
495 "Update matches in the RE Builder target window. | |
496 If SUBEXP is non-nil mark only the corresponding sub-expressions." | |
497 | |
498 (reb-assert-buffer-in-window) | |
499 (reb-update-regexp) | |
500 (reb-update-overlays subexp)) | |
501 | |
502 (defun reb-auto-update (beg end lenold &optional force) | |
503 "Called from `after-update-functions' to update the display. | |
55404
14a47b65c288
(reb-auto-update): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
504 BEG, END and LENOLD are passed in from the hook. |
28077 | 505 An actual update is only done if the regexp has changed or if the |
506 optional fourth argument FORCE is non-nil." | |
507 (let ((prev-valid reb-valid-string) | |
508 (new-valid | |
509 (condition-case nil | |
510 (progn | |
511 (if (or (reb-update-regexp) force) | |
512 (progn | |
513 (reb-assert-buffer-in-window) | |
514 (reb-do-update))) | |
515 "") | |
516 (error " *invalid*")))) | |
517 (setq reb-valid-string new-valid) | |
518 (force-mode-line-update) | |
519 | |
520 ;; Through the caching of the re a change invalidating the syntax | |
521 ;; for symbolic expressions will not delete the overlays so we | |
522 ;; catch it here | |
523 (if (and (reb-lisp-syntax-p) | |
524 (not (string= prev-valid new-valid)) | |
525 (string= prev-valid "")) | |
526 (reb-delete-overlays)))) | |
527 | |
528 (defun reb-delete-overlays () | |
529 "Delete all RE Builder overlays in the `reb-target-buffer' buffer." | |
530 (if (buffer-live-p reb-target-buffer) | |
531 (with-current-buffer reb-target-buffer | |
532 (mapcar 'delete-overlay reb-overlays) | |
533 (setq reb-overlays nil)))) | |
534 | |
535 (defun reb-assert-buffer-in-window () | |
536 "Assert that `reb-target-buffer' is displayed in `reb-target-window'." | |
537 | |
538 (if (not (eq reb-target-buffer (window-buffer reb-target-window))) | |
539 (set-window-buffer reb-target-window reb-target-buffer))) | |
540 | |
541 (defun reb-update-modestring () | |
542 "Update the variable `reb-mode-string' displayed in the mode line." | |
543 (setq reb-mode-string | |
544 (concat | |
545 (if reb-subexp-mode | |
35756
8a4490cbee15
(reb-update-modestring): Don't use concat for integers.
Eli Zaretskii <eliz@gnu.org>
parents:
28107
diff
changeset
|
546 (format " (subexp %s)" (or reb-subexp-displayed "-")) |
28077 | 547 "") |
548 (if (not (reb-target-binding case-fold-search)) | |
549 " Case" | |
550 ""))) | |
551 (force-mode-line-update)) | |
552 | |
553 (defun reb-display-subexp (&optional subexp) | |
554 "Highlight only subexpression SUBEXP in the RE Builder." | |
555 (interactive) | |
556 | |
557 (setq reb-subexp-displayed | |
558 (or subexp (string-to-int (format "%c" last-command-char)))) | |
559 (reb-update-modestring) | |
560 (reb-do-update reb-subexp-displayed)) | |
561 | |
562 (defun reb-kill-buffer () | |
563 "When the RE Builder buffer is killed make sure no overlays stay around." | |
564 | |
565 (if (member major-mode '(reb-mode reb-lisp-mode)) | |
566 (reb-delete-overlays))) | |
567 | |
568 | |
569 ;; The next functions are the interface between the regexp and | |
570 ;; its textual representation in the RE Builder buffer. | |
571 ;; They are the only functions concerned with the actual syntax | |
572 ;; being used. | |
573 (defun reb-read-regexp () | |
574 "Read current RE." | |
575 (save-excursion | |
576 (cond ((eq reb-re-syntax 'read) | |
577 (goto-char (point-min)) | |
578 (read (current-buffer))) | |
579 ((eq reb-re-syntax 'string) | |
580 (goto-char (point-min)) | |
581 (re-search-forward "\"") | |
582 (let ((beg (point))) | |
583 (goto-char (point-max)) | |
584 (re-search-backward "\"") | |
585 (buffer-substring-no-properties beg (point)))) | |
586 ((reb-lisp-syntax-p) | |
587 (buffer-string))))) | |
588 | |
589 (defun reb-empty-regexp () | |
590 "Return empty RE for current syntax." | |
591 (cond ((reb-lisp-syntax-p) "'()") | |
592 (t ""))) | |
593 | |
594 (defun reb-insert-regexp () | |
595 "Insert current RE." | |
596 | |
597 (let ((re (or (reb-target-binding reb-regexp) | |
598 (reb-empty-regexp)))) | |
599 (cond ((eq reb-re-syntax 'read) | |
600 (print re (current-buffer))) | |
601 ((eq reb-re-syntax 'string) | |
602 (insert "\n\"" re "\"")) | |
603 ;; For the Lisp syntax we need the "source" of the regexp | |
604 ((reb-lisp-syntax-p) | |
605 (insert (or (reb-target-binding reb-regexp-src) | |
606 (reb-empty-regexp))))))) | |
607 | |
608 (defun reb-cook-regexp (re) | |
609 "Return RE after processing it according to `reb-re-syntax'." | |
610 (cond ((eq reb-re-syntax 'lisp-re) | |
611 (lre-compile-string (eval (car (read-from-string re))))) | |
612 ((eq reb-re-syntax 'sregex) | |
613 (apply 'sregex (eval (car (read-from-string re))))) | |
56611
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
614 ((eq reb-re-syntax 'rx) |
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
615 (rx-to-string (eval (car (read-from-string re))))) |
28077 | 616 (t re))) |
617 | |
618 (defun reb-update-regexp () | |
619 "Update the regexp for the target buffer. | |
620 Return t if the (cooked) expression changed." | |
621 (let* ((re-src (reb-read-regexp)) | |
622 (re (reb-cook-regexp re-src))) | |
623 (with-current-buffer reb-target-buffer | |
624 (let ((oldre reb-regexp)) | |
625 (prog1 | |
626 (not (string= oldre re)) | |
627 (setq reb-regexp re) | |
628 ;; Only update the source re for the lisp formats | |
629 (if (reb-lisp-syntax-p) | |
630 (setq reb-regexp-src re-src))))))) | |
631 | |
632 | |
633 ;; And now the real core of the whole thing | |
634 (defun reb-count-subexps (re) | |
635 "Return number of sub-expressions in the regexp RE." | |
636 | |
637 (let ((i 0) (beg 0)) | |
638 (while (string-match "\\\\(" re beg) | |
639 (setq i (1+ i) | |
640 beg (match-end 0))) | |
641 i)) | |
642 | |
643 | |
644 (defun reb-update-overlays (&optional subexp) | |
645 "Switch to `reb-target-buffer' and mark all matches of `reb-regexp'. | |
646 If SUBEXP is non-nil mark only the corresponding sub-expressions." | |
647 | |
648 (let* ((re (reb-target-binding reb-regexp)) | |
649 (subexps (reb-count-subexps re)) | |
650 (matches 0) | |
651 (submatches 0) | |
652 firstmatch) | |
653 (save-excursion | |
654 (set-buffer reb-target-buffer) | |
655 (reb-delete-overlays) | |
656 (goto-char (point-min)) | |
657 (while (and (re-search-forward re (point-max) t) | |
658 (or (not reb-auto-match-limit) | |
659 (< matches reb-auto-match-limit))) | |
660 (if (= 0 (length (match-string 0))) | |
661 (error "Empty regular expression!")) | |
662 (let ((i 0)) | |
663 (setq matches (1+ matches)) | |
664 (while (<= i subexps) | |
665 (if (and (or (not subexp) (= subexp i)) | |
666 (match-beginning i)) | |
667 (let ((overlay (make-overlay (match-beginning i) | |
668 (match-end i))) | |
669 (face-name (format "reb-match-%d" i))) | |
670 (if (not firstmatch) | |
671 (setq firstmatch (match-data))) | |
672 (setq reb-overlays (cons overlay reb-overlays) | |
673 submatches (1+ submatches)) | |
674 (overlay-put | |
675 overlay 'face | |
676 (or (intern-soft face-name) | |
677 (error "Too many subexpressions - face `%s' not defined" | |
678 face-name ))) | |
679 (overlay-put overlay 'priority i))) | |
680 (setq i (1+ i)))))) | |
681 (let ((count (if subexp submatches matches))) | |
56611
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
682 (message "%s %smatch%s%s" |
28077 | 683 (if (= 0 count) "No" (int-to-string count)) |
684 (if subexp "subexpression " "") | |
56382
a4c0ce40be3d
(reb-update-overlays): Distinguish between one and several matches in message.
John Paul Wallington <jpw@pobox.com>
parents:
55404
diff
changeset
|
685 (if (= 1 count) "" "es") |
28077 | 686 (if (and reb-auto-match-limit |
687 (= reb-auto-match-limit count)) | |
688 " (limit reached)" ""))) | |
689 (if firstmatch | |
690 (progn (store-match-data firstmatch) | |
691 (reb-show-subexp (or subexp 0)))))) | |
692 | |
48379
6e55b7742a81
Add provide call.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
41371
diff
changeset
|
693 (provide 're-builder) |
6e55b7742a81
Add provide call.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
41371
diff
changeset
|
694 |
52401 | 695 ;;; arch-tag: 5c5515ac-4085-4524-a421-033f44f032e7 |
28077 | 696 ;;; re-builder.el ends here |