Mercurial > emacs
annotate lisp/emacs-lisp/re-builder.el @ 94262:0a5264d5b86f
*** empty log message ***
author | Juri Linkov <juri@jurta.org> |
---|---|
date | Tue, 22 Apr 2008 20:32:23 +0000 |
parents | 1e3a407766b9 |
children | 90a2847062be |
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 |
64751
5b1a238fcbb4
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64624
diff
changeset
|
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, |
79704 | 4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
28077 | 5 |
6 ;; Author: Detlev Zundel <dzu@gnu.org> | |
7 ;; Keywords: matching, lisp, tools | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
78217
935157c0b596
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
76540
diff
changeset
|
13 ;; the Free Software Foundation; either version 3, or (at your option) |
28077 | 14 ;; any later version. |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64085 | 23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 ;; Boston, MA 02110-1301, USA. | |
28077 | 25 |
26 ;;; Commentary: | |
27 | |
28 ;; When I have to come up with regular expressions that are more | |
29 ;; complex than simple string matchers, especially if they contain sub | |
30 ;; expressions, I find myself spending quite some time in the | |
31 ;; `development cycle'. `re-builder' aims to shorten this time span | |
32 ;; so I can get on with the more interesting bits. | |
33 | |
34 ;; With it you can have immediate visual feedback about how well the | |
35 ;; regexp behaves to your expectations on the intended data. | |
36 | |
37 ;; When called up `re-builder' attaches itself to the current buffer | |
38 ;; which becomes its target buffer, where all the matching is done. | |
39 ;; The active window is split so you have a view on the data while | |
40 ;; authoring the RE. If the edited expression is valid the matches in | |
41 ;; the target buffer are marked automatically with colored overlays | |
42 ;; (for non-color displays see below) giving you feedback over the | |
43 ;; extents of the matched (sub) expressions. The (non-)validity is | |
44 ;; shown only in the modeline without throwing the errors at you. If | |
45 ;; you want to know the reason why RE Builder considers it as invalid | |
46 ;; call `reb-force-update' ("\C-c\C-u") which should reveal the error. | |
47 | |
41313
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
48 ;; 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
|
49 ;; ("\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
|
50 ;; 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
|
51 ;; target window. |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
52 |
28077 | 53 ;; The `re-builder' keeps the focus while updating the matches in the |
54 ;; target buffer so corrections are easy to incorporate. If you are | |
55 ;; satisfied with the result you can paste the RE to the kill-ring | |
56 ;; with `reb-copy' ("\C-c\C-w"), quit the `re-builder' ("\C-c\C-q") | |
57 ;; and use it wherever you need it. | |
58 | |
59 ;; As the automatic updates can take some time on large buffers, they | |
60 ;; can be limited by `reb-auto-match-limit' so that they should not | |
61 ;; have a negative impact on the editing. Setting it to nil makes | |
62 ;; even the auto updates go all the way. Forcing an update overrides | |
63 ;; this limit allowing an easy way to see all matches. | |
64 | |
59396
1c58b91ca0df
Update copyright. Update commentary to mention rx syntax support.
John Paul Wallington <jpw@pobox.com>
parents:
56698
diff
changeset
|
65 ;; 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
|
66 ;; namely `read', `string', `rx', `sregex' and `lisp-re' syntax. Read |
28077 | 67 ;; syntax and string syntax are both delimited by `"'s and behave |
68 ;; according to their name. With the `string' syntax there's no need | |
69 ;; 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
|
70 ;; somewhat. The other three allow editing of symbolic regular |
28077 | 71 ;; expressions supported by the packages of the same name. (`lisp-re' |
72 ;; is a package by me and its support may go away as it is nearly the | |
73 ;; same as the `sregex' package in Emacs) | |
74 | |
75 ;; Editing symbolic expressions is done through a major mode derived | |
76 ;; from `emacs-lisp-mode' so you'll get all the good stuff like | |
77 ;; automatic indentation and font-locking etc. | |
78 | |
79 ;; When editing a symbolic regular expression, only the first | |
80 ;; expression in the RE Builder buffer is considered, which helps | |
81 ;; limiting the extent of the expression like the `"'s do for the text | |
82 ;; modes. For the `sregex' syntax the function `sregex' is applied to | |
83 ;; the evaluated expression read. So you can use quoted arguments | |
84 ;; with something like '("findme") or you can construct arguments to | |
85 ;; your hearts delight with a valid ELisp expression. (The compiled | |
86 ;; string form will be copied by `reb-copy') If you want to take | |
87 ;; a glance at the corresponding string you can temporarily change the | |
88 ;; input syntax. | |
89 | |
90 ;; Changing the input syntax is transparent (for the obvious exception | |
91 ;; non-symbolic -> symbolic) so you can change your mind as often as | |
92 ;; you like. | |
93 | |
94 ;; There is also a shortcut function for toggling the | |
95 ;; `case-fold-search' variable in the target buffer with an immediate | |
96 ;; update. | |
97 | |
98 | |
99 ;; Q: But what if my display cannot show colored overlays? | |
100 ;; A: Then the cursor will flash around the matched text making it stand | |
101 ;; out. | |
102 | |
103 ;; Q: But how can I then make out the sub-expressions? | |
104 ;; A: Thats where the `sub-expression mode' comes in. In it only the | |
105 ;; digit keys are assigned to perform an update that will flash the | |
106 ;; corresponding subexp only. | |
107 | |
108 | |
109 ;;; Code: | |
110 | |
111 ;; On XEmacs, load the overlay compatibility library | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
112 (unless (fboundp 'make-overlay) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
113 (require 'overlay)) |
28077 | 114 |
70404
ce092da13632
(reb-update-overlays): Cycle through provided faces once they all have been
Eli Zaretskii <eliz@gnu.org>
parents:
68648
diff
changeset
|
115 ;; User customizable variables |
28077 | 116 (defgroup re-builder nil |
117 "Options for the RE Builder." | |
118 :group 'lisp | |
119 :prefix "reb-") | |
120 | |
121 (defcustom reb-blink-delay 0.5 | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
122 "Seconds to blink cursor for next/previous match in RE Builder." |
28077 | 123 :group 're-builder |
124 :type 'number) | |
125 | |
126 (defcustom reb-mode-hook nil | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
127 "Hooks to run on entering RE Builder mode." |
28077 | 128 :group 're-builder |
129 :type 'hook) | |
130 | |
131 (defcustom reb-re-syntax 'read | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
132 "Syntax for the REs in the RE Builder. |
76350
afaae6720920
(reb-re-syntax): Fix custom type.
Richard M. Stallman <rms@gnu.org>
parents:
75346
diff
changeset
|
133 Can either be `read', `string', `sregex', `lisp-re', `rx'." |
28077 | 134 :group 're-builder |
135 :type '(choice (const :tag "Read syntax" read) | |
136 (const :tag "String syntax" string) | |
137 (const :tag "`sregex' syntax" sregex) | |
138 (const :tag "`lisp-re' syntax" lisp-re) | |
76350
afaae6720920
(reb-re-syntax): Fix custom type.
Richard M. Stallman <rms@gnu.org>
parents:
75346
diff
changeset
|
139 (const :tag "`rx' syntax" rx))) |
28077 | 140 |
141 (defcustom reb-auto-match-limit 200 | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
142 "Positive integer limiting the matches for RE Builder auto updates. |
28077 | 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 | |
61394
31aa9a390538
* mh-customize.el (mh-speedbar-selected-folder-face): Special case
Dan Nicolaescu <dann@ics.uci.edu>
parents:
60282
diff
changeset
|
180 '((((min-colors 88) (class color) (background light)) |
31aa9a390538
* mh-customize.el (mh-speedbar-selected-folder-face): Special case
Dan Nicolaescu <dann@ics.uci.edu>
parents:
60282
diff
changeset
|
181 :background "yellow1") |
31aa9a390538
* mh-customize.el (mh-speedbar-selected-folder-face): Special case
Dan Nicolaescu <dann@ics.uci.edu>
parents:
60282
diff
changeset
|
182 (((class color) (background light)) |
41371 | 183 :background "yellow") |
184 (((class color) (background dark)) | |
185 :background "sienna4") | |
186 (t | |
187 :inverse-video t)) | |
28077 | 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 | |
56698
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
234 (defvar reb-mode-map |
93674
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
235 (let ((map (make-sparse-keymap)) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
236 (menu-map (make-sparse-keymap))) |
56698
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
237 (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
|
238 (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
|
239 (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
|
240 (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
|
241 (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
|
242 (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
|
243 (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
|
244 (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
|
245 (define-key map "\C-c\C-u" 'reb-force-update) |
93674
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
246 (define-key map [menu-bar reb-mode] (cons "Re-Builder" menu-map)) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
247 (define-key menu-map [rq] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
248 '(menu-item "Quit" reb-quit |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
249 :help "Quit the RE Builder mode")) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
250 (define-key menu-map [rt] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
251 '(menu-item "Case sensitive" reb-toggle-case |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
252 :button (:toggle . case-fold-search) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
253 :help "Toggle case sensitivity of searches for RE Builder target buffer.")) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
254 (define-key menu-map [rb] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
255 '(menu-item "Change target buffer..." reb-change-target-buffer |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
256 :help "Change the target buffer and display it in the target window")) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
257 (define-key menu-map [rs] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
258 '(menu-item "Change syntax..." reb-change-syntax |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
259 :help "Change the syntax used by the RE Builder")) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
260 (define-key menu-map [re] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
261 '(menu-item "Enter subexpression mode" reb-enter-subexp-mode |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
262 :help "Enter the subexpression mode in the RE Builder")) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
263 (define-key menu-map [ru] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
264 '(menu-item "Force update" reb-force-update |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
265 :help "Force an update in the RE Builder target window without a match limit")) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
266 (define-key menu-map [rn] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
267 '(menu-item "Go to next match" reb-next-match |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
268 :help "Go to next match in the RE Builder target window")) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
269 (define-key menu-map [rp] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
270 '(menu-item "Go to previous match" reb-prev-match |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
271 :help "Go to previous match in the RE Builder target window")) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
272 (define-key menu-map [rc] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
273 '(menu-item "Copy current RE" reb-copy |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
274 :help "Copy current RE into the kill ring for later insertion")) |
56698
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
275 map) |
28077 | 276 "Keymap used by the RE Builder.") |
277 | |
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
|
278 (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
|
279 "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
|
280 \\{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
|
281 (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
|
282 (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
|
283 (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
|
284 mode-name "RE Builder") |
73928
5aa08f9f45e5
(reb-mode): Set `blink-matching-paren' to nil in the *RE-Builder*
Juanma Barranquero <lekktu@gmail.com>
parents:
70404
diff
changeset
|
285 (set (make-local-variable 'blink-matching-paren) nil) |
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
|
286 (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
|
287 (reb-mode-common) |
62757
0c94929d0a7d
(reb-mode): Use run-mode-hooks.
Lute Kamstra <lute@gnu.org>
parents:
62402
diff
changeset
|
288 (run-mode-hooks 'reb-mode-hook)) |
28077 | 289 |
290 (define-derived-mode reb-lisp-mode | |
291 emacs-lisp-mode "RE Builder Lisp" | |
41225
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
292 "Major mode for interactively building symbolic Regular Expressions." |
28077 | 293 (cond ((eq reb-re-syntax 'lisp-re) ; Pull in packages |
294 (require 'lisp-re)) ; as needed | |
295 ((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
|
296 (require 'sregex)) ; right now.. |
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
297 ((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
|
298 (require 'rx))) ; require rx anyway |
28077 | 299 (reb-mode-common)) |
300 | |
301 ;; Use the same "\C-c" keymap as `reb-mode' and use font-locking from | |
302 ;; `emacs-lisp-mode' | |
303 (define-key reb-lisp-mode-map "\C-c" | |
304 (lookup-key reb-mode-map "\C-c")) | |
305 | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48379
diff
changeset
|
306 (defvar reb-subexp-mode-map |
41225
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
307 (let ((m (make-keymap))) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
308 (suppress-keymap m) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
309 ;; 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
|
310 (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
|
311 (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
|
312 (dotimes (digit 10) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
313 (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
|
314 m) |
28077 | 315 "Keymap used by the RE Builder for the subexpression mode.") |
316 | |
317 (defun reb-mode-common () | |
318 "Setup functions common to functions `reb-mode' and `reb-mode-lisp'." | |
319 | |
320 (setq reb-mode-string "" | |
321 reb-valid-string "" | |
322 mode-line-buffer-identification | |
323 '(25 . ("%b" reb-mode-string reb-valid-string))) | |
324 (reb-update-modestring) | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
325 (add-hook 'after-change-functions 'reb-auto-update nil t) |
28077 | 326 ;; At least make the overlays go away if the buffer is killed |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
327 (add-hook 'kill-buffer-hook 'reb-kill-buffer nil t) |
28077 | 328 (reb-auto-update nil nil nil)) |
329 | |
330 (defun reb-color-display-p () | |
331 "Return t if display is capable of displaying colors." | |
332 (eq 'color | |
333 ;; emacs/xemacs compatibility | |
334 (if (fboundp 'frame-parameter) | |
335 (frame-parameter (selected-frame) 'display-type) | |
64382
c16c027014c6
(reb-cook-regexp): Avoid warning calling lre-compile-string.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
336 (if (fboundp 'frame-property) |
c16c027014c6
(reb-cook-regexp): Avoid warning calling lre-compile-string.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
337 (frame-property (selected-frame) 'display-type))))) |
28077 | 338 |
339 (defsubst reb-lisp-syntax-p () | |
340 "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
|
341 (memq reb-re-syntax '(lisp-re sregex rx))) |
28077 | 342 |
343 (defmacro reb-target-binding (symbol) | |
344 "Return binding for SYMBOL in the RE Builder target buffer." | |
345 `(with-current-buffer reb-target-buffer ,symbol)) | |
346 | |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
347 (defun reb-initialize-buffer () |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
348 "Initialize the current buffer as a RE Builder buffer." |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
349 (erase-buffer) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
350 (reb-insert-regexp) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
351 (goto-char (+ 2 (point-min))) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
352 (cond ((reb-lisp-syntax-p) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
353 (reb-lisp-mode)) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
354 (t (reb-mode)))) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
355 |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
356 (defun reb-mode-buffer-p () |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
357 "Return non-nil if the current buffer is a RE Builder buffer." |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
358 (memq major-mode '(reb-mode reb-lisp-mode))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
359 |
60282
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
360 ;;; This is to help people find this in Apropos. |
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
361 ;;;###autoload |
64557
c68bf82df188
(regexp-builder): Use `defalias' instead of faking it.
Juanma Barranquero <lekktu@gmail.com>
parents:
64382
diff
changeset
|
362 (defalias 'regexp-builder 're-builder) |
28077 | 363 |
364 ;;;###autoload | |
365 (defun re-builder () | |
60282
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
366 "Construct a regexp interactively." |
28077 | 367 (interactive) |
368 | |
41314
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
369 (if (and (string= (buffer-name) reb-buffer) |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
370 (reb-mode-buffer-p)) |
41314
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
371 (message "Already in the RE Builder") |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
372 (when reb-target-buffer |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
373 (reb-delete-overlays)) |
41314
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
374 (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
|
375 reb-target-window (selected-window) |
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
376 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
|
377 (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
|
378 (switch-to-buffer (get-buffer-create reb-buffer)) |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
379 (reb-initialize-buffer))) |
28077 | 380 |
41313
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
381 (defun reb-change-target-buffer (buf) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
382 "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
|
383 (interactive "bSet target buffer to: ") |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
384 |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
385 (let ((buffer (get-buffer buf))) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
386 (if (not buffer) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
387 (error "No such buffer") |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
388 (reb-delete-overlays) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
389 (setq reb-target-buffer buffer) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
390 (reb-do-update |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
391 (if reb-subexp-mode reb-subexp-displayed nil)) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
392 (reb-update-modestring)))) |
28077 | 393 |
394 (defun reb-force-update () | |
56698
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
395 "Force an update in the RE Builder target window without a match limit." |
28077 | 396 (interactive) |
397 | |
398 (let ((reb-auto-match-limit nil)) | |
399 (reb-update-overlays | |
400 (if reb-subexp-mode reb-subexp-displayed nil)))) | |
401 | |
402 (defun reb-quit () | |
403 "Quit the RE Builder mode." | |
404 (interactive) | |
405 | |
406 (setq reb-subexp-mode nil | |
407 reb-subexp-displayed nil) | |
408 (reb-delete-overlays) | |
409 (bury-buffer) | |
410 (set-window-configuration reb-window-config)) | |
411 | |
412 (defun reb-next-match () | |
413 "Go to next match in the RE Builder target window." | |
414 (interactive) | |
415 | |
416 (reb-assert-buffer-in-window) | |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
417 (with-selected-window reb-target-window |
28077 | 418 (if (not (re-search-forward reb-regexp (point-max) t)) |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
419 (message "No more matches") |
28077 | 420 (reb-show-subexp |
421 (or (and reb-subexp-mode reb-subexp-displayed) 0) | |
422 t)))) | |
423 | |
424 (defun reb-prev-match () | |
425 "Go to previous match in the RE Builder target window." | |
426 (interactive) | |
427 | |
428 (reb-assert-buffer-in-window) | |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
429 (with-selected-window reb-target-window |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
430 (let ((p (point))) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
431 (goto-char (1- p)) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
432 (if (re-search-backward reb-regexp (point-min) t) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
433 (reb-show-subexp |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
434 (or (and reb-subexp-mode reb-subexp-displayed) 0) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
435 t) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
436 (goto-char p) |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
437 (message "No more matches"))))) |
28077 | 438 |
439 (defun reb-toggle-case () | |
440 "Toggle case sensitivity of searches for RE Builder target buffer." | |
441 (interactive) | |
442 | |
443 (with-current-buffer reb-target-buffer | |
444 (setq case-fold-search (not case-fold-search))) | |
445 (reb-update-modestring) | |
446 (reb-auto-update nil nil nil t)) | |
447 | |
448 (defun reb-copy () | |
449 "Copy current RE into the kill ring for later insertion." | |
450 (interactive) | |
451 | |
452 (reb-update-regexp) | |
453 (let ((re (with-output-to-string | |
454 (print (reb-target-binding reb-regexp))))) | |
455 (kill-new (substring re 1 (1- (length re)))) | |
456 (message "Regexp copied to kill-ring"))) | |
457 | |
458 ;; The subexpression mode is not electric because the number of | |
459 ;; matches should be seen rather than a prompt. | |
460 (defun reb-enter-subexp-mode () | |
461 "Enter the subexpression mode in the RE Builder." | |
462 (interactive) | |
463 (setq reb-subexp-mode t) | |
464 (reb-update-modestring) | |
465 (use-local-map reb-subexp-mode-map) | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
466 (message "`0'-`9' to display subexpressions `q' to quit subexp mode")) |
28077 | 467 |
468 (defun reb-show-subexp (subexp &optional pause) | |
469 "Visually show limit of subexpression SUBEXP of recent search. | |
470 On color displays this just puts point to the end of the expression as | |
471 the match should already be marked by an overlay. | |
472 On other displays jump to the beginning and the end of it. | |
473 If the optional PAUSE is non-nil then pause at the end in any case." | |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
474 (with-selected-window reb-target-window |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
475 (unless (reb-color-display-p) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
476 (goto-char (match-beginning subexp)) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
477 (sit-for reb-blink-delay)) |
28077 | 478 (goto-char (match-end subexp)) |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
479 (when (or (not (reb-color-display-p)) pause) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
480 (sit-for reb-blink-delay)))) |
28077 | 481 |
482 (defun reb-quit-subexp-mode () | |
483 "Quit the subexpression mode in the RE Builder." | |
484 (interactive) | |
485 (setq reb-subexp-mode nil | |
486 reb-subexp-displayed nil) | |
487 (reb-update-modestring) | |
488 (use-local-map reb-mode-map) | |
489 (reb-do-update)) | |
490 | |
491 (defun reb-change-syntax (&optional syntax) | |
492 "Change the syntax used by the RE Builder. | |
493 Optional argument SYNTAX must be specified if called non-interactively." | |
494 (interactive | |
495 (list (intern | |
496 (completing-read "Select syntax: " | |
497 (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
|
498 '(read string lisp-re sregex rx)) |
28077 | 499 nil t (symbol-name reb-re-syntax))))) |
500 | |
56611
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
501 (if (memq syntax '(read string lisp-re sregex rx)) |
28077 | 502 (let ((buffer (get-buffer reb-buffer))) |
503 (setq reb-re-syntax syntax) | |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
504 (when buffer |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
505 (with-current-buffer buffer |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
506 (reb-initialize-buffer)))) |
28077 | 507 (error "Invalid syntax: %s" syntax))) |
508 | |
509 | |
510 ;; Non-interactive functions below | |
511 (defun reb-do-update (&optional subexp) | |
512 "Update matches in the RE Builder target window. | |
513 If SUBEXP is non-nil mark only the corresponding sub-expressions." | |
514 | |
515 (reb-assert-buffer-in-window) | |
516 (reb-update-regexp) | |
517 (reb-update-overlays subexp)) | |
518 | |
519 (defun reb-auto-update (beg end lenold &optional force) | |
520 "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
|
521 BEG, END and LENOLD are passed in from the hook. |
28077 | 522 An actual update is only done if the regexp has changed or if the |
523 optional fourth argument FORCE is non-nil." | |
524 (let ((prev-valid reb-valid-string) | |
525 (new-valid | |
526 (condition-case nil | |
527 (progn | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
528 (when (or (reb-update-regexp) force) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
529 (reb-assert-buffer-in-window) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
530 (reb-do-update)) |
28077 | 531 "") |
532 (error " *invalid*")))) | |
533 (setq reb-valid-string new-valid) | |
534 (force-mode-line-update) | |
535 | |
536 ;; Through the caching of the re a change invalidating the syntax | |
537 ;; for symbolic expressions will not delete the overlays so we | |
538 ;; catch it here | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
539 (when (and (reb-lisp-syntax-p) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
540 (not (string= prev-valid new-valid)) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
541 (string= prev-valid "")) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
542 (reb-delete-overlays)))) |
28077 | 543 |
544 (defun reb-delete-overlays () | |
545 "Delete all RE Builder overlays in the `reb-target-buffer' buffer." | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
546 (when (buffer-live-p reb-target-buffer) |
28077 | 547 (with-current-buffer reb-target-buffer |
84902
ba9c9ff8aab7
(reb-delete-overlays): Use `mapc' rather than `mapcar'.
Juanma Barranquero <lekktu@gmail.com>
parents:
78217
diff
changeset
|
548 (mapc 'delete-overlay reb-overlays) |
28077 | 549 (setq reb-overlays nil)))) |
550 | |
551 (defun reb-assert-buffer-in-window () | |
552 "Assert that `reb-target-buffer' is displayed in `reb-target-window'." | |
553 | |
554 (if (not (eq reb-target-buffer (window-buffer reb-target-window))) | |
555 (set-window-buffer reb-target-window reb-target-buffer))) | |
556 | |
557 (defun reb-update-modestring () | |
558 "Update the variable `reb-mode-string' displayed in the mode line." | |
559 (setq reb-mode-string | |
560 (concat | |
561 (if reb-subexp-mode | |
35756
8a4490cbee15
(reb-update-modestring): Don't use concat for integers.
Eli Zaretskii <eliz@gnu.org>
parents:
28107
diff
changeset
|
562 (format " (subexp %s)" (or reb-subexp-displayed "-")) |
28077 | 563 "") |
564 (if (not (reb-target-binding case-fold-search)) | |
565 " Case" | |
566 ""))) | |
567 (force-mode-line-update)) | |
568 | |
569 (defun reb-display-subexp (&optional subexp) | |
570 "Highlight only subexpression SUBEXP in the RE Builder." | |
571 (interactive) | |
572 | |
573 (setq reb-subexp-displayed | |
62402
a7e02ef1e3d6
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
61394
diff
changeset
|
574 (or subexp (string-to-number (format "%c" last-command-char)))) |
28077 | 575 (reb-update-modestring) |
576 (reb-do-update reb-subexp-displayed)) | |
577 | |
578 (defun reb-kill-buffer () | |
579 "When the RE Builder buffer is killed make sure no overlays stay around." | |
580 | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
581 (when (reb-mode-buffer-p) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
582 (reb-delete-overlays))) |
28077 | 583 |
584 | |
585 ;; The next functions are the interface between the regexp and | |
586 ;; its textual representation in the RE Builder buffer. | |
587 ;; They are the only functions concerned with the actual syntax | |
588 ;; being used. | |
589 (defun reb-read-regexp () | |
590 "Read current RE." | |
591 (save-excursion | |
592 (cond ((eq reb-re-syntax 'read) | |
593 (goto-char (point-min)) | |
594 (read (current-buffer))) | |
595 ((eq reb-re-syntax 'string) | |
596 (goto-char (point-min)) | |
597 (re-search-forward "\"") | |
598 (let ((beg (point))) | |
599 (goto-char (point-max)) | |
600 (re-search-backward "\"") | |
601 (buffer-substring-no-properties beg (point)))) | |
602 ((reb-lisp-syntax-p) | |
603 (buffer-string))))) | |
604 | |
605 (defun reb-empty-regexp () | |
606 "Return empty RE for current syntax." | |
607 (cond ((reb-lisp-syntax-p) "'()") | |
608 (t ""))) | |
609 | |
610 (defun reb-insert-regexp () | |
611 "Insert current RE." | |
612 | |
613 (let ((re (or (reb-target-binding reb-regexp) | |
614 (reb-empty-regexp)))) | |
615 (cond ((eq reb-re-syntax 'read) | |
616 (print re (current-buffer))) | |
617 ((eq reb-re-syntax 'string) | |
618 (insert "\n\"" re "\"")) | |
619 ;; For the Lisp syntax we need the "source" of the regexp | |
620 ((reb-lisp-syntax-p) | |
621 (insert (or (reb-target-binding reb-regexp-src) | |
622 (reb-empty-regexp))))))) | |
623 | |
624 (defun reb-cook-regexp (re) | |
625 "Return RE after processing it according to `reb-re-syntax'." | |
626 (cond ((eq reb-re-syntax 'lisp-re) | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
627 (when (fboundp 'lre-compile-string) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
628 (lre-compile-string (eval (car (read-from-string re)))))) |
28077 | 629 ((eq reb-re-syntax 'sregex) |
630 (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
|
631 ((eq reb-re-syntax 'rx) |
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
632 (rx-to-string (eval (car (read-from-string re))))) |
28077 | 633 (t re))) |
634 | |
635 (defun reb-update-regexp () | |
636 "Update the regexp for the target buffer. | |
637 Return t if the (cooked) expression changed." | |
638 (let* ((re-src (reb-read-regexp)) | |
639 (re (reb-cook-regexp re-src))) | |
640 (with-current-buffer reb-target-buffer | |
641 (let ((oldre reb-regexp)) | |
642 (prog1 | |
643 (not (string= oldre re)) | |
644 (setq reb-regexp re) | |
645 ;; Only update the source re for the lisp formats | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
646 (when (reb-lisp-syntax-p) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
647 (setq reb-regexp-src re-src))))))) |
28077 | 648 |
649 | |
650 ;; And now the real core of the whole thing | |
651 (defun reb-count-subexps (re) | |
652 "Return number of sub-expressions in the regexp RE." | |
653 | |
654 (let ((i 0) (beg 0)) | |
655 (while (string-match "\\\\(" re beg) | |
656 (setq i (1+ i) | |
657 beg (match-end 0))) | |
658 i)) | |
659 | |
660 (defun reb-update-overlays (&optional subexp) | |
661 "Switch to `reb-target-buffer' and mark all matches of `reb-regexp'. | |
662 If SUBEXP is non-nil mark only the corresponding sub-expressions." | |
663 (let* ((re (reb-target-binding reb-regexp)) | |
664 (subexps (reb-count-subexps re)) | |
665 (matches 0) | |
666 (submatches 0) | |
667 firstmatch) | |
668 (save-excursion | |
669 (set-buffer reb-target-buffer) | |
670 (reb-delete-overlays) | |
671 (goto-char (point-min)) | |
76540
2f2e0b6d33c0
(reb-update-overlays): Do not mark zero-width regexps as invalid
Juanma Barranquero <lekktu@gmail.com>
parents:
76350
diff
changeset
|
672 (while (and (not (eobp)) |
2f2e0b6d33c0
(reb-update-overlays): Do not mark zero-width regexps as invalid
Juanma Barranquero <lekktu@gmail.com>
parents:
76350
diff
changeset
|
673 (re-search-forward re (point-max) t) |
28077 | 674 (or (not reb-auto-match-limit) |
675 (< matches reb-auto-match-limit))) | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
676 (when (and (= 0 (length (match-string 0))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
677 (not (eobp))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
678 (forward-char 1)) |
70404
ce092da13632
(reb-update-overlays): Cycle through provided faces once they all have been
Eli Zaretskii <eliz@gnu.org>
parents:
68648
diff
changeset
|
679 (let ((i 0) |
ce092da13632
(reb-update-overlays): Cycle through provided faces once they all have been
Eli Zaretskii <eliz@gnu.org>
parents:
68648
diff
changeset
|
680 suffix max-suffix) |
28077 | 681 (setq matches (1+ matches)) |
682 (while (<= i subexps) | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
683 (when (and (or (not subexp) (= subexp i)) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
684 (match-beginning i)) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
685 (let ((overlay (make-overlay (match-beginning i) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
686 (match-end i))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
687 ;; When we have exceeded the number of provided faces, |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
688 ;; cycle thru them where `max-suffix' denotes the maximum |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
689 ;; suffix for `reb-match-*' that has been defined and |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
690 ;; `suffix' the suffix calculated for the current match. |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
691 (face |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
692 (cond |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
693 (max-suffix |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
694 (if (= suffix max-suffix) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
695 (setq suffix 1) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
696 (setq suffix (1+ suffix))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
697 (intern-soft (format "reb-match-%d" suffix))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
698 ((intern-soft (format "reb-match-%d" i))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
699 ((setq max-suffix (1- i)) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
700 (setq suffix 1) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
701 ;; `reb-match-1' must exist. |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
702 'reb-match-1)))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
703 (unless firstmatch (setq firstmatch (match-data))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
704 (setq reb-overlays (cons overlay reb-overlays) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
705 submatches (1+ submatches)) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
706 (overlay-put overlay 'face face) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
707 (overlay-put overlay 'priority i))) |
28077 | 708 (setq i (1+ i)))))) |
709 (let ((count (if subexp submatches matches))) | |
56611
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
710 (message "%s %smatch%s%s" |
28077 | 711 (if (= 0 count) "No" (int-to-string count)) |
712 (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
|
713 (if (= 1 count) "" "es") |
28077 | 714 (if (and reb-auto-match-limit |
715 (= reb-auto-match-limit count)) | |
716 " (limit reached)" ""))) | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
717 (when firstmatch |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
718 (store-match-data firstmatch) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
719 (reb-show-subexp (or subexp 0))))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
720 |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
721 ;; The End |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
722 (defun re-builder-unload-function () |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
723 "Unload the RE Builder library." |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
724 (when (buffer-live-p (get-buffer reb-buffer)) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
725 (with-current-buffer reb-buffer |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
726 (remove-hook 'after-change-functions 'reb-auto-update t) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
727 (remove-hook 'kill-buffer-hook 'reb-kill-buffer t) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
728 (when (reb-mode-buffer-p) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
729 (reb-delete-overlays) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
730 (funcall default-major-mode)))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
731 ;; continue standard unloading |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
732 nil) |
28077 | 733 |
48379
6e55b7742a81
Add provide call.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
41371
diff
changeset
|
734 (provide 're-builder) |
6e55b7742a81
Add provide call.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
41371
diff
changeset
|
735 |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
93674
diff
changeset
|
736 ;; arch-tag: 5c5515ac-4085-4524-a421-033f44f032e7 |
28077 | 737 ;;; re-builder.el ends here |