Mercurial > emacs
annotate lisp/emacs-lisp/re-builder.el @ 112415:555faf6e40d0
mm-decode.el (mm-preferred-alternative-precedence): Don't bug out on nested related parts.
nnfolder.el (nnfolder-request-expire-articles): Return the list of unexpired articles. This fixes the regression that led expiry marks to disappear from nnfolder groups.
author | Katsumi Yamaoka <yamaoka@jpl.org> |
---|---|
date | Sat, 22 Jan 2011 12:34:50 +0000 |
parents | ef719132ddfa |
children | 9f7614f1a892 |
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, |
112218
376148b31b5e
Add 2011 to FSF/AIST copyright years.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 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 | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
11 ;; GNU Emacs is free software: you can redistribute it and/or modify |
28077 | 12 ;; it under the terms of the GNU General Public License as published by |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
13 ;; the Free Software Foundation, either version 3 of the License, or |
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
14 ;; (at your option) any later version. |
28077 | 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 | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
28077 | 23 |
24 ;;; Commentary: | |
25 | |
26 ;; When I have to come up with regular expressions that are more | |
27 ;; complex than simple string matchers, especially if they contain sub | |
28 ;; expressions, I find myself spending quite some time in the | |
29 ;; `development cycle'. `re-builder' aims to shorten this time span | |
30 ;; so I can get on with the more interesting bits. | |
31 | |
32 ;; With it you can have immediate visual feedback about how well the | |
33 ;; regexp behaves to your expectations on the intended data. | |
34 | |
35 ;; When called up `re-builder' attaches itself to the current buffer | |
36 ;; which becomes its target buffer, where all the matching is done. | |
37 ;; The active window is split so you have a view on the data while | |
38 ;; authoring the RE. If the edited expression is valid the matches in | |
39 ;; the target buffer are marked automatically with colored overlays | |
40 ;; (for non-color displays see below) giving you feedback over the | |
41 ;; extents of the matched (sub) expressions. The (non-)validity is | |
42 ;; shown only in the modeline without throwing the errors at you. If | |
43 ;; you want to know the reason why RE Builder considers it as invalid | |
44 ;; call `reb-force-update' ("\C-c\C-u") which should reveal the error. | |
45 | |
41313
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
46 ;; 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
|
47 ;; ("\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
|
48 ;; 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
|
49 ;; target window. |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
50 |
28077 | 51 ;; The `re-builder' keeps the focus while updating the matches in the |
52 ;; target buffer so corrections are easy to incorporate. If you are | |
53 ;; satisfied with the result you can paste the RE to the kill-ring | |
54 ;; with `reb-copy' ("\C-c\C-w"), quit the `re-builder' ("\C-c\C-q") | |
55 ;; and use it wherever you need it. | |
56 | |
57 ;; As the automatic updates can take some time on large buffers, they | |
58 ;; can be limited by `reb-auto-match-limit' so that they should not | |
59 ;; have a negative impact on the editing. Setting it to nil makes | |
60 ;; even the auto updates go all the way. Forcing an update overrides | |
61 ;; this limit allowing an easy way to see all matches. | |
62 | |
112017
db006527425b
* lisp/emacs-lisp/rx.el: Make it a superset of sregex.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
109562
diff
changeset
|
63 ;; Currently `re-builder' understands three different forms of input, |
db006527425b
* lisp/emacs-lisp/rx.el: Make it a superset of sregex.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
109562
diff
changeset
|
64 ;; namely `read', `string', and `rx' syntax. Read |
28077 | 65 ;; syntax and string syntax are both delimited by `"'s and behave |
66 ;; according to their name. With the `string' syntax there's no need | |
67 ;; 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
|
68 ;; somewhat. The other three allow editing of symbolic regular |
109562
9a8064e866bb
* emacs-lisp/re-builder.el: Remove references to package `lisp-re' (bug#4369).
Juanma Barranquero <lekktu@gmail.com>
parents:
106815
diff
changeset
|
69 ;; expressions supported by the packages of the same name. |
28077 | 70 |
71 ;; Editing symbolic expressions is done through a major mode derived | |
72 ;; from `emacs-lisp-mode' so you'll get all the good stuff like | |
73 ;; automatic indentation and font-locking etc. | |
74 | |
75 ;; When editing a symbolic regular expression, only the first | |
76 ;; expression in the RE Builder buffer is considered, which helps | |
77 ;; limiting the extent of the expression like the `"'s do for the text | |
112017
db006527425b
* lisp/emacs-lisp/rx.el: Make it a superset of sregex.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
109562
diff
changeset
|
78 ;; modes. For the `rx' syntax the function `rx-to-string' is applied to |
28077 | 79 ;; the evaluated expression read. So you can use quoted arguments |
80 ;; with something like '("findme") or you can construct arguments to | |
81 ;; your hearts delight with a valid ELisp expression. (The compiled | |
82 ;; string form will be copied by `reb-copy') If you want to take | |
83 ;; a glance at the corresponding string you can temporarily change the | |
84 ;; input syntax. | |
85 | |
86 ;; Changing the input syntax is transparent (for the obvious exception | |
87 ;; non-symbolic -> symbolic) so you can change your mind as often as | |
88 ;; you like. | |
89 | |
90 ;; There is also a shortcut function for toggling the | |
91 ;; `case-fold-search' variable in the target buffer with an immediate | |
92 ;; update. | |
93 | |
94 | |
95 ;; Q: But what if my display cannot show colored overlays? | |
96 ;; A: Then the cursor will flash around the matched text making it stand | |
97 ;; out. | |
98 | |
99 ;; Q: But how can I then make out the sub-expressions? | |
100 ;; A: Thats where the `sub-expression mode' comes in. In it only the | |
101 ;; digit keys are assigned to perform an update that will flash the | |
102 ;; corresponding subexp only. | |
103 | |
104 | |
105 ;;; Code: | |
106 | |
107 ;; 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
|
108 (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
|
109 (require 'overlay)) |
28077 | 110 |
70404
ce092da13632
(reb-update-overlays): Cycle through provided faces once they all have been
Eli Zaretskii <eliz@gnu.org>
parents:
68648
diff
changeset
|
111 ;; User customizable variables |
28077 | 112 (defgroup re-builder nil |
113 "Options for the RE Builder." | |
114 :group 'lisp | |
115 :prefix "reb-") | |
116 | |
117 (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
|
118 "Seconds to blink cursor for next/previous match in RE Builder." |
28077 | 119 :group 're-builder |
120 :type 'number) | |
121 | |
122 (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
|
123 "Hooks to run on entering RE Builder mode." |
28077 | 124 :group 're-builder |
125 :type 'hook) | |
126 | |
127 (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
|
128 "Syntax for the REs in the RE Builder. |
112017
db006527425b
* lisp/emacs-lisp/rx.el: Make it a superset of sregex.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
109562
diff
changeset
|
129 Can either be `read', `string', or `rx'." |
28077 | 130 :group 're-builder |
131 :type '(choice (const :tag "Read syntax" read) | |
132 (const :tag "String syntax" string) | |
76350
afaae6720920
(reb-re-syntax): Fix custom type.
Richard M. Stallman <rms@gnu.org>
parents:
75346
diff
changeset
|
133 (const :tag "`rx' syntax" rx))) |
28077 | 134 |
135 (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
|
136 "Positive integer limiting the matches for RE Builder auto updates. |
28077 | 137 Set it to nil if you don't want limits here." |
138 :group 're-builder | |
139 :type '(restricted-sexp :match-alternatives | |
140 (integerp 'nil))) | |
141 | |
142 | |
143 (defface reb-match-0 | |
41371 | 144 '((((class color) (background light)) |
145 :background "lightblue") | |
146 (((class color) (background dark)) | |
147 :background "steelblue4") | |
148 (t | |
149 :inverse-video t)) | |
28077 | 150 "Used for displaying the whole match." |
151 :group 're-builder) | |
152 | |
153 (defface reb-match-1 | |
41371 | 154 '((((class color) (background light)) |
155 :background "aquamarine") | |
156 (((class color) (background dark)) | |
157 :background "blue3") | |
158 (t | |
159 :inverse-video t)) | |
28077 | 160 "Used for displaying the first matching subexpression." |
161 :group 're-builder) | |
162 | |
163 (defface reb-match-2 | |
41371 | 164 '((((class color) (background light)) |
165 :background "springgreen") | |
166 (((class color) (background dark)) | |
167 :background "chartreuse4") | |
168 (t | |
169 :inverse-video t)) | |
28077 | 170 "Used for displaying the second matching subexpression." |
171 :group 're-builder) | |
172 | |
173 (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
|
174 '((((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
|
175 :background "yellow1") |
31aa9a390538
* mh-customize.el (mh-speedbar-selected-folder-face): Special case
Dan Nicolaescu <dann@ics.uci.edu>
parents:
60282
diff
changeset
|
176 (((class color) (background light)) |
41371 | 177 :background "yellow") |
178 (((class color) (background dark)) | |
179 :background "sienna4") | |
180 (t | |
181 :inverse-video t)) | |
28077 | 182 "Used for displaying the third matching subexpression." |
183 :group 're-builder) | |
184 | |
185 ;; Internal variables below | |
186 (defvar reb-mode nil | |
187 "Enables the RE Builder minor mode.") | |
188 | |
189 (defvar reb-target-buffer nil | |
190 "Buffer to which the RE is applied to.") | |
191 | |
192 (defvar reb-target-window nil | |
193 "Window to which the RE is applied to.") | |
194 | |
195 (defvar reb-regexp nil | |
196 "Last regexp used by RE Builder.") | |
197 | |
198 (defvar reb-regexp-src nil | |
199 "Last regexp used by RE Builder before processing it. | |
200 Except for Lisp syntax this is the same as `reb-regexp'.") | |
201 | |
202 (defvar reb-overlays nil | |
203 "List of overlays of the RE Builder.") | |
204 | |
205 (defvar reb-window-config nil | |
206 "Old window configuration.") | |
207 | |
208 (defvar reb-subexp-mode nil | |
209 "Indicates whether sub-exp mode is active.") | |
210 | |
211 (defvar reb-subexp-displayed nil | |
212 "Indicates which sub-exp is active.") | |
213 | |
214 (defvar reb-mode-string "" | |
215 "String in mode line for additional info.") | |
216 | |
217 (defvar reb-valid-string "" | |
218 "String in mode line showing validity of RE.") | |
219 | |
220 (make-variable-buffer-local 'reb-overlays) | |
221 (make-variable-buffer-local 'reb-regexp) | |
222 (make-variable-buffer-local 'reb-regexp-src) | |
223 | |
224 (defconst reb-buffer "*RE-Builder*" | |
225 "Buffer to use for the RE Builder.") | |
226 | |
227 ;; Define the local "\C-c" keymap | |
56698
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
228 (defvar reb-mode-map |
93674
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
229 (let ((map (make-sparse-keymap)) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
230 (menu-map (make-sparse-keymap))) |
56698
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
231 (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
|
232 (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
|
233 (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
|
234 (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
|
235 (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
|
236 (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
|
237 (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
|
238 (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
|
239 (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
|
240 (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
|
241 (define-key menu-map [rq] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
242 '(menu-item "Quit" reb-quit |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
243 :help "Quit the RE Builder mode")) |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
244 (define-key menu-map [rt] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
245 '(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
|
246 :button (:toggle . case-fold-search) |
100048
d872c709fff2
* emacs-lisp/re-builder.el (reb-mode-map): Fix typo in menu tooltip.
Juanma Barranquero <lekktu@gmail.com>
parents:
100018
diff
changeset
|
247 :help "Toggle case sensitivity of searches for RE Builder target buffer")) |
93674
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
248 (define-key menu-map [rb] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
249 '(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
|
250 :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
|
251 (define-key menu-map [rs] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
252 '(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
|
253 :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
|
254 (define-key menu-map [re] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
255 '(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
|
256 :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
|
257 (define-key menu-map [ru] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
258 '(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
|
259 :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
|
260 (define-key menu-map [rn] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
261 '(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
|
262 :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
|
263 (define-key menu-map [rp] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
264 '(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
|
265 :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
|
266 (define-key menu-map [rc] |
44fab469d68d
* outline.el (outline-mode-menu-bar-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93198
diff
changeset
|
267 '(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
|
268 :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
|
269 map) |
28077 | 270 "Keymap used by the RE Builder.") |
271 | |
100048
d872c709fff2
* emacs-lisp/re-builder.el (reb-mode-map): Fix typo in menu tooltip.
Juanma Barranquero <lekktu@gmail.com>
parents:
100018
diff
changeset
|
272 (define-derived-mode reb-mode nil "RE Builder" |
d872c709fff2
* emacs-lisp/re-builder.el (reb-mode-map): Fix typo in menu tooltip.
Juanma Barranquero <lekktu@gmail.com>
parents:
100018
diff
changeset
|
273 "Major mode for interactively building Regular Expressions." |
73928
5aa08f9f45e5
(reb-mode): Set `blink-matching-paren' to nil in the *RE-Builder*
Juanma Barranquero <lekktu@gmail.com>
parents:
70404
diff
changeset
|
274 (set (make-local-variable 'blink-matching-paren) nil) |
100048
d872c709fff2
* emacs-lisp/re-builder.el (reb-mode-map): Fix typo in menu tooltip.
Juanma Barranquero <lekktu@gmail.com>
parents:
100018
diff
changeset
|
275 (reb-mode-common)) |
28077 | 276 |
277 (define-derived-mode reb-lisp-mode | |
278 emacs-lisp-mode "RE Builder Lisp" | |
41225
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
279 "Major mode for interactively building symbolic Regular Expressions." |
109562
9a8064e866bb
* emacs-lisp/re-builder.el: Remove references to package `lisp-re' (bug#4369).
Juanma Barranquero <lekktu@gmail.com>
parents:
106815
diff
changeset
|
280 ;; Pull in packages as needed |
112017
db006527425b
* lisp/emacs-lisp/rx.el: Make it a superset of sregex.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
109562
diff
changeset
|
281 (cond ((memq reb-re-syntax '(sregex rx)) ; rx-to-string is autoloaded |
db006527425b
* lisp/emacs-lisp/rx.el: Make it a superset of sregex.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
109562
diff
changeset
|
282 (require 'rx))) ; require rx anyway |
28077 | 283 (reb-mode-common)) |
284 | |
285 ;; Use the same "\C-c" keymap as `reb-mode' and use font-locking from | |
286 ;; `emacs-lisp-mode' | |
287 (define-key reb-lisp-mode-map "\C-c" | |
288 (lookup-key reb-mode-map "\C-c")) | |
289 | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48379
diff
changeset
|
290 (defvar reb-subexp-mode-map |
41225
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
291 (let ((m (make-keymap))) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
292 (suppress-keymap m) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
293 ;; 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
|
294 (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
|
295 (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
|
296 (dotimes (digit 10) |
74219f3013c6
(reb-mode): Use define-derived-mode.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
38436
diff
changeset
|
297 (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
|
298 m) |
28077 | 299 "Keymap used by the RE Builder for the subexpression mode.") |
300 | |
301 (defun reb-mode-common () | |
302 "Setup functions common to functions `reb-mode' and `reb-mode-lisp'." | |
303 | |
304 (setq reb-mode-string "" | |
305 reb-valid-string "" | |
306 mode-line-buffer-identification | |
307 '(25 . ("%b" reb-mode-string reb-valid-string))) | |
308 (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
|
309 (add-hook 'after-change-functions 'reb-auto-update nil t) |
28077 | 310 ;; 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
|
311 (add-hook 'kill-buffer-hook 'reb-kill-buffer nil t) |
28077 | 312 (reb-auto-update nil nil nil)) |
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) | |
64382
c16c027014c6
(reb-cook-regexp): Avoid warning calling lre-compile-string.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
320 (if (fboundp 'frame-property) |
c16c027014c6
(reb-cook-regexp): Avoid warning calling lre-compile-string.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
321 (frame-property (selected-frame) 'display-type))))) |
28077 | 322 |
323 (defsubst reb-lisp-syntax-p () | |
324 "Return non-nil if RE Builder uses a Lisp syntax." | |
109562
9a8064e866bb
* emacs-lisp/re-builder.el: Remove references to package `lisp-re' (bug#4369).
Juanma Barranquero <lekktu@gmail.com>
parents:
106815
diff
changeset
|
325 (memq reb-re-syntax '(sregex rx))) |
28077 | 326 |
327 (defmacro reb-target-binding (symbol) | |
328 "Return binding for SYMBOL in the RE Builder target buffer." | |
329 `(with-current-buffer reb-target-buffer ,symbol)) | |
330 | |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
331 (defun reb-initialize-buffer () |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
332 "Initialize the current buffer as a RE Builder buffer." |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
333 (erase-buffer) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
334 (reb-insert-regexp) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
335 (goto-char (+ 2 (point-min))) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
336 (cond ((reb-lisp-syntax-p) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
337 (reb-lisp-mode)) |
100018
811c2c99eed5
* emacs-lisp/re-builder.el (reb-auto-update): Remove redundant code.
Juanma Barranquero <lekktu@gmail.com>
parents:
94655
diff
changeset
|
338 (t (reb-mode))) |
811c2c99eed5
* emacs-lisp/re-builder.el (reb-auto-update): Remove redundant code.
Juanma Barranquero <lekktu@gmail.com>
parents:
94655
diff
changeset
|
339 (reb-do-update)) |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
340 |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
341 (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
|
342 "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
|
343 (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
|
344 |
60282
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
345 ;;; This is to help people find this in Apropos. |
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
346 ;;;###autoload |
64557
c68bf82df188
(regexp-builder): Use `defalias' instead of faking it.
Juanma Barranquero <lekktu@gmail.com>
parents:
64382
diff
changeset
|
347 (defalias 'regexp-builder 're-builder) |
28077 | 348 |
349 ;;;###autoload | |
350 (defun re-builder () | |
60282
b6fcc94c2d6a
(regexp-builder): New function.
Richard M. Stallman <rms@gnu.org>
parents:
59396
diff
changeset
|
351 "Construct a regexp interactively." |
28077 | 352 (interactive) |
353 | |
41314
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
354 (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
|
355 (reb-mode-buffer-p)) |
41314
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
356 (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
|
357 (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
|
358 (reb-delete-overlays)) |
41314
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
359 (setq reb-target-buffer (current-buffer) |
100018
811c2c99eed5
* emacs-lisp/re-builder.el (reb-auto-update): Remove redundant code.
Juanma Barranquero <lekktu@gmail.com>
parents:
94655
diff
changeset
|
360 reb-target-window (selected-window)) |
811c2c99eed5
* emacs-lisp/re-builder.el (reb-auto-update): Remove redundant code.
Juanma Barranquero <lekktu@gmail.com>
parents:
94655
diff
changeset
|
361 (select-window (or (get-buffer-window reb-buffer) |
811c2c99eed5
* emacs-lisp/re-builder.el (reb-auto-update): Remove redundant code.
Juanma Barranquero <lekktu@gmail.com>
parents:
94655
diff
changeset
|
362 (progn |
811c2c99eed5
* emacs-lisp/re-builder.el (reb-auto-update): Remove redundant code.
Juanma Barranquero <lekktu@gmail.com>
parents:
94655
diff
changeset
|
363 (setq reb-window-config (current-window-configuration)) |
811c2c99eed5
* emacs-lisp/re-builder.el (reb-auto-update): Remove redundant code.
Juanma Barranquero <lekktu@gmail.com>
parents:
94655
diff
changeset
|
364 (split-window (selected-window) (- (window-height) 4))))) |
41314
a2bce9d9c349
(re-builder): Don't re-enter RE Builder Mode.
Eli Zaretskii <eliz@gnu.org>
parents:
41313
diff
changeset
|
365 (switch-to-buffer (get-buffer-create reb-buffer)) |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
366 (reb-initialize-buffer))) |
28077 | 367 |
41313
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
368 (defun reb-change-target-buffer (buf) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
369 "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
|
370 (interactive "bSet target buffer to: ") |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
371 |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
372 (let ((buffer (get-buffer buf))) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
373 (if (not buffer) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
374 (error "No such buffer") |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
375 (reb-delete-overlays) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
376 (setq reb-target-buffer buffer) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
377 (reb-do-update |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
378 (if reb-subexp-mode reb-subexp-displayed nil)) |
2186cf9476ba
(reb-change-target-buffer): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
41225
diff
changeset
|
379 (reb-update-modestring)))) |
28077 | 380 |
381 (defun reb-force-update () | |
56698
7dbbaf3dc171
(reb-mode-map): Define within defvar.
John Paul Wallington <jpw@pobox.com>
parents:
56611
diff
changeset
|
382 "Force an update in the RE Builder target window without a match limit." |
28077 | 383 (interactive) |
384 | |
385 (let ((reb-auto-match-limit nil)) | |
386 (reb-update-overlays | |
387 (if reb-subexp-mode reb-subexp-displayed nil)))) | |
388 | |
389 (defun reb-quit () | |
390 "Quit the RE Builder mode." | |
391 (interactive) | |
392 | |
393 (setq reb-subexp-mode nil | |
394 reb-subexp-displayed nil) | |
395 (reb-delete-overlays) | |
396 (bury-buffer) | |
397 (set-window-configuration reb-window-config)) | |
398 | |
399 (defun reb-next-match () | |
400 "Go to next match in the RE Builder target window." | |
401 (interactive) | |
402 | |
403 (reb-assert-buffer-in-window) | |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
404 (with-selected-window reb-target-window |
28077 | 405 (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
|
406 (message "No more matches") |
28077 | 407 (reb-show-subexp |
408 (or (and reb-subexp-mode reb-subexp-displayed) 0) | |
409 t)))) | |
410 | |
411 (defun reb-prev-match () | |
412 "Go to previous match in the RE Builder target window." | |
413 (interactive) | |
414 | |
415 (reb-assert-buffer-in-window) | |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
416 (with-selected-window reb-target-window |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
417 (let ((p (point))) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
418 (goto-char (1- p)) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
419 (if (re-search-backward reb-regexp (point-min) t) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
420 (reb-show-subexp |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
421 (or (and reb-subexp-mode reb-subexp-displayed) 0) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
422 t) |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
423 (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
|
424 (message "No more matches"))))) |
28077 | 425 |
426 (defun reb-toggle-case () | |
427 "Toggle case sensitivity of searches for RE Builder target buffer." | |
428 (interactive) | |
429 | |
430 (with-current-buffer reb-target-buffer | |
431 (setq case-fold-search (not case-fold-search))) | |
432 (reb-update-modestring) | |
433 (reb-auto-update nil nil nil t)) | |
434 | |
435 (defun reb-copy () | |
436 "Copy current RE into the kill ring for later insertion." | |
437 (interactive) | |
438 | |
439 (reb-update-regexp) | |
440 (let ((re (with-output-to-string | |
441 (print (reb-target-binding reb-regexp))))) | |
442 (kill-new (substring re 1 (1- (length re)))) | |
443 (message "Regexp copied to kill-ring"))) | |
444 | |
445 ;; The subexpression mode is not electric because the number of | |
446 ;; matches should be seen rather than a prompt. | |
447 (defun reb-enter-subexp-mode () | |
448 "Enter the subexpression mode in the RE Builder." | |
449 (interactive) | |
450 (setq reb-subexp-mode t) | |
451 (reb-update-modestring) | |
452 (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
|
453 (message "`0'-`9' to display subexpressions `q' to quit subexp mode")) |
28077 | 454 |
455 (defun reb-show-subexp (subexp &optional pause) | |
456 "Visually show limit of subexpression SUBEXP of recent search. | |
457 On color displays this just puts point to the end of the expression as | |
458 the match should already be marked by an overlay. | |
459 On other displays jump to the beginning and the end of it. | |
460 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
|
461 (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
|
462 (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
|
463 (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
|
464 (sit-for reb-blink-delay)) |
28077 | 465 (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
|
466 (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
|
467 (sit-for reb-blink-delay)))) |
28077 | 468 |
469 (defun reb-quit-subexp-mode () | |
470 "Quit the subexpression mode in the RE Builder." | |
471 (interactive) | |
472 (setq reb-subexp-mode nil | |
473 reb-subexp-displayed nil) | |
474 (reb-update-modestring) | |
475 (use-local-map reb-mode-map) | |
476 (reb-do-update)) | |
477 | |
478 (defun reb-change-syntax (&optional syntax) | |
479 "Change the syntax used by the RE Builder. | |
480 Optional argument SYNTAX must be specified if called non-interactively." | |
481 (interactive | |
482 (list (intern | |
483 (completing-read "Select syntax: " | |
484 (mapcar (lambda (el) (cons (symbol-name el) 1)) | |
109562
9a8064e866bb
* emacs-lisp/re-builder.el: Remove references to package `lisp-re' (bug#4369).
Juanma Barranquero <lekktu@gmail.com>
parents:
106815
diff
changeset
|
485 '(read string sregex rx)) |
28077 | 486 nil t (symbol-name reb-re-syntax))))) |
487 | |
109562
9a8064e866bb
* emacs-lisp/re-builder.el: Remove references to package `lisp-re' (bug#4369).
Juanma Barranquero <lekktu@gmail.com>
parents:
106815
diff
changeset
|
488 (if (memq syntax '(read string sregex rx)) |
28077 | 489 (let ((buffer (get-buffer reb-buffer))) |
490 (setq reb-re-syntax syntax) | |
64624
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
491 (when buffer |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
492 (with-current-buffer buffer |
c3a907cbd503
(reb-with-current-window): Delete.
Juanma Barranquero <lekktu@gmail.com>
parents:
64557
diff
changeset
|
493 (reb-initialize-buffer)))) |
28077 | 494 (error "Invalid syntax: %s" syntax))) |
495 | |
496 | |
497 ;; Non-interactive functions below | |
498 (defun reb-do-update (&optional subexp) | |
499 "Update matches in the RE Builder target window. | |
500 If SUBEXP is non-nil mark only the corresponding sub-expressions." | |
501 | |
502 (reb-assert-buffer-in-window) | |
503 (reb-update-regexp) | |
504 (reb-update-overlays subexp)) | |
505 | |
506 (defun reb-auto-update (beg end lenold &optional force) | |
507 "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
|
508 BEG, END and LENOLD are passed in from the hook. |
28077 | 509 An actual update is only done if the regexp has changed or if the |
510 optional fourth argument FORCE is non-nil." | |
511 (let ((prev-valid reb-valid-string) | |
512 (new-valid | |
513 (condition-case nil | |
514 (progn | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
515 (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
|
516 (reb-do-update)) |
28077 | 517 "") |
518 (error " *invalid*")))) | |
519 (setq reb-valid-string new-valid) | |
520 (force-mode-line-update) | |
521 | |
522 ;; Through the caching of the re a change invalidating the syntax | |
523 ;; for symbolic expressions will not delete the overlays so we | |
524 ;; catch it here | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
525 (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
|
526 (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
|
527 (string= prev-valid "")) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
528 (reb-delete-overlays)))) |
28077 | 529 |
530 (defun reb-delete-overlays () | |
531 "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
|
532 (when (buffer-live-p reb-target-buffer) |
28077 | 533 (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
|
534 (mapc 'delete-overlay reb-overlays) |
28077 | 535 (setq reb-overlays nil)))) |
536 | |
537 (defun reb-assert-buffer-in-window () | |
538 "Assert that `reb-target-buffer' is displayed in `reb-target-window'." | |
539 | |
540 (if (not (eq reb-target-buffer (window-buffer reb-target-window))) | |
541 (set-window-buffer reb-target-window reb-target-buffer))) | |
542 | |
543 (defun reb-update-modestring () | |
544 "Update the variable `reb-mode-string' displayed in the mode line." | |
545 (setq reb-mode-string | |
546 (concat | |
547 (if reb-subexp-mode | |
35756
8a4490cbee15
(reb-update-modestring): Don't use concat for integers.
Eli Zaretskii <eliz@gnu.org>
parents:
28107
diff
changeset
|
548 (format " (subexp %s)" (or reb-subexp-displayed "-")) |
28077 | 549 "") |
550 (if (not (reb-target-binding case-fold-search)) | |
551 " Case" | |
552 ""))) | |
553 (force-mode-line-update)) | |
554 | |
555 (defun reb-display-subexp (&optional subexp) | |
556 "Highlight only subexpression SUBEXP in the RE Builder." | |
557 (interactive) | |
558 | |
559 (setq reb-subexp-displayed | |
101008
5aa3a4e05089
Replace last-command-char with last-command-event.
Glenn Morris <rgm@gnu.org>
parents:
100908
diff
changeset
|
560 (or subexp (string-to-number (format "%c" last-command-event)))) |
28077 | 561 (reb-update-modestring) |
562 (reb-do-update reb-subexp-displayed)) | |
563 | |
564 (defun reb-kill-buffer () | |
565 "When the RE Builder buffer is killed make sure no overlays stay around." | |
566 | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
567 (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
|
568 (reb-delete-overlays))) |
28077 | 569 |
570 | |
571 ;; The next functions are the interface between the regexp and | |
572 ;; its textual representation in the RE Builder buffer. | |
573 ;; They are the only functions concerned with the actual syntax | |
574 ;; being used. | |
575 (defun reb-read-regexp () | |
576 "Read current RE." | |
577 (save-excursion | |
578 (cond ((eq reb-re-syntax 'read) | |
579 (goto-char (point-min)) | |
580 (read (current-buffer))) | |
581 ((eq reb-re-syntax 'string) | |
582 (goto-char (point-min)) | |
583 (re-search-forward "\"") | |
584 (let ((beg (point))) | |
585 (goto-char (point-max)) | |
586 (re-search-backward "\"") | |
587 (buffer-substring-no-properties beg (point)))) | |
588 ((reb-lisp-syntax-p) | |
589 (buffer-string))))) | |
590 | |
591 (defun reb-empty-regexp () | |
592 "Return empty RE for current syntax." | |
593 (cond ((reb-lisp-syntax-p) "'()") | |
594 (t ""))) | |
595 | |
596 (defun reb-insert-regexp () | |
597 "Insert current RE." | |
598 | |
599 (let ((re (or (reb-target-binding reb-regexp) | |
600 (reb-empty-regexp)))) | |
601 (cond ((eq reb-re-syntax 'read) | |
602 (print re (current-buffer))) | |
603 ((eq reb-re-syntax 'string) | |
604 (insert "\n\"" re "\"")) | |
605 ;; For the Lisp syntax we need the "source" of the regexp | |
606 ((reb-lisp-syntax-p) | |
607 (insert (or (reb-target-binding reb-regexp-src) | |
608 (reb-empty-regexp))))))) | |
609 | |
610 (defun reb-cook-regexp (re) | |
611 "Return RE after processing it according to `reb-re-syntax'." | |
112017
db006527425b
* lisp/emacs-lisp/rx.el: Make it a superset of sregex.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
109562
diff
changeset
|
612 (cond ((memq reb-re-syntax '(sregex rx)) |
56611
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
613 (rx-to-string (eval (car (read-from-string re))))) |
28077 | 614 (t re))) |
615 | |
616 (defun reb-update-regexp () | |
617 "Update the regexp for the target buffer. | |
618 Return t if the (cooked) expression changed." | |
619 (let* ((re-src (reb-read-regexp)) | |
620 (re (reb-cook-regexp re-src))) | |
621 (with-current-buffer reb-target-buffer | |
622 (let ((oldre reb-regexp)) | |
623 (prog1 | |
624 (not (string= oldre re)) | |
625 (setq reb-regexp re) | |
626 ;; 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
|
627 (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
|
628 (setq reb-regexp-src re-src))))))) |
28077 | 629 |
630 | |
631 ;; And now the real core of the whole thing | |
632 (defun reb-count-subexps (re) | |
633 "Return number of sub-expressions in the regexp RE." | |
634 | |
635 (let ((i 0) (beg 0)) | |
636 (while (string-match "\\\\(" re beg) | |
637 (setq i (1+ i) | |
638 beg (match-end 0))) | |
639 i)) | |
640 | |
641 (defun reb-update-overlays (&optional subexp) | |
642 "Switch to `reb-target-buffer' and mark all matches of `reb-regexp'. | |
643 If SUBEXP is non-nil mark only the corresponding sub-expressions." | |
644 (let* ((re (reb-target-binding reb-regexp)) | |
645 (subexps (reb-count-subexps re)) | |
646 (matches 0) | |
647 (submatches 0) | |
648 firstmatch) | |
105813
df4934f25eef
* textmodes/two-column.el (2C-split):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
104684
diff
changeset
|
649 (with-current-buffer reb-target-buffer |
28077 | 650 (reb-delete-overlays) |
651 (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
|
652 (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
|
653 (re-search-forward re (point-max) t) |
28077 | 654 (or (not reb-auto-match-limit) |
655 (< 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
|
656 (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
|
657 (not (eobp))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
658 (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
|
659 (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
|
660 suffix max-suffix) |
28077 | 661 (setq matches (1+ matches)) |
662 (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
|
663 (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
|
664 (match-beginning i)) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
665 (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
|
666 (match-end i))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
667 ;; 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
|
668 ;; 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
|
669 ;; 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
|
670 ;; `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
|
671 (face |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
672 (cond |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
673 (max-suffix |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
674 (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
|
675 (setq suffix 1) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
676 (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
|
677 (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
|
678 ((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
|
679 ((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
|
680 (setq suffix 1) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
681 ;; `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
|
682 'reb-match-1)))) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
683 (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
|
684 (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
|
685 submatches (1+ submatches)) |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
686 (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
|
687 (overlay-put overlay 'priority i))) |
28077 | 688 (setq i (1+ i)))))) |
689 (let ((count (if subexp submatches matches))) | |
56611
8b14a3e64595
(reb-re-syntax): Add `rx' syntax.
John Paul Wallington <jpw@pobox.com>
parents:
56382
diff
changeset
|
690 (message "%s %smatch%s%s" |
28077 | 691 (if (= 0 count) "No" (int-to-string count)) |
692 (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
|
693 (if (= 1 count) "" "es") |
28077 | 694 (if (and reb-auto-match-limit |
695 (= reb-auto-match-limit count)) | |
696 " (limit reached)" ""))) | |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
697 (when firstmatch |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
698 (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
|
699 (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
|
700 |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
701 ;; The End |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
702 (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
|
703 "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
|
704 (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
|
705 (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
|
706 (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
|
707 (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
|
708 (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
|
709 (reb-delete-overlays) |
104684
b1cb2e195329
* arc-mode.el (archive-mode):
Juanma Barranquero <lekktu@gmail.com>
parents:
104682
diff
changeset
|
710 (funcall (or (default-value 'major-mode) 'fundamental-mode))))) |
93198
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
711 ;; continue standard unloading |
eb3d659f5085
(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer';
Juanma Barranquero <lekktu@gmail.com>
parents:
87649
diff
changeset
|
712 nil) |
28077 | 713 |
48379
6e55b7742a81
Add provide call.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
41371
diff
changeset
|
714 (provide 're-builder) |
6e55b7742a81
Add provide call.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
41371
diff
changeset
|
715 |
28077 | 716 ;;; re-builder.el ends here |