Mercurial > emacs
annotate lisp/emacs-lisp/sregex.el @ 105381:0c7122fb98eb
*** empty log message ***
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Fri, 02 Oct 2009 12:52:17 +0000 |
parents | a9dc0e7c3f2b |
children | 1d1d5d9bd884 |
rev | line source |
---|---|
22537 | 1 ;;; sregex.el --- symbolic regular expressions |
2 | |
74466 | 3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004, |
100908 | 4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
22537 | 5 |
6 ;; Author: Bob Glickstein <bobg+sregex@zanshin.com> | |
7 ;; Maintainer: Bob Glickstein <bobg+sregex@zanshin.com> | |
29212 | 8 ;; Keywords: extensions |
22537 | 9 |
10 ;; This file is part of GNU Emacs. | |
11 | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify |
22537 | 13 ;; 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
|
14 ;; 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
|
15 ;; (at your option) any later version. |
22537 | 16 |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; 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
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
22537 | 24 |
25 ;;; Commentary: | |
26 | |
27 ;; This package allows you to write regular expressions using a | |
28 ;; totally new, Lisp-like syntax. | |
29 | |
30 ;; A "symbolic regular expression" (sregex for short) is a Lisp form | |
31 ;; that, when evaluated, produces the string form of the specified | |
32 ;; regular expression. Here's a simple example: | |
33 | |
34 ;; (sregexq (or "Bob" "Robert")) => "Bob\\|Robert" | |
35 | |
36 ;; As you can see, an sregex is specified by placing one or more | |
37 ;; special clauses in a call to `sregexq'. The clause in this case is | |
38 ;; the `or' of two strings (not to be confused with the Lisp function | |
39 ;; `or'). The list of allowable clauses appears below. | |
40 | |
41 ;; With sregex, it is never necessary to "escape" magic characters | |
42 ;; that are meant to be taken literally; that happens automatically. | |
43 ;; For example: | |
44 | |
45 ;; (sregexq "M*A*S*H") => "M\\*A\\*S\\*H" | |
46 | |
47 ;; It is also unnecessary to "group" parts of the expression together | |
48 ;; to overcome operator precedence; that also happens automatically. | |
49 ;; For example: | |
50 | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
51 ;; (sregexq (opt (or "Bob" "Robert"))) => "\\(?:Bob\\|Robert\\)?" |
22537 | 52 |
53 ;; It *is* possible to group parts of the expression in order to refer | |
54 ;; to them with numbered backreferences: | |
55 | |
56 ;; (sregexq (group (or "Go" "Run")) | |
57 ;; ", Spot, " | |
58 ;; (backref 1)) => "\\(Go\\|Run\\), Spot, \\1" | |
59 | |
60 ;; `sregexq' is a macro. Each time it is used, it constructs a simple | |
61 ;; Lisp expression that then invokes a moderately complex engine to | |
62 ;; interpret the sregex and render the string form. Because of this, | |
63 ;; I don't recommend sprinkling calls to `sregexq' throughout your | |
64 ;; code, the way one normally does with string regexes (which are | |
65 ;; cheap to evaluate). Instead, it's wiser to precompute the regexes | |
66 ;; you need wherever possible instead of repeatedly constructing the | |
67 ;; same ones over and over. Example: | |
68 | |
69 ;; (let ((field-regex (sregexq (opt "resent-") | |
70 ;; (or "to" "cc" "bcc")))) | |
71 ;; ... | |
72 ;; (while ... | |
73 ;; ... | |
74 ;; (re-search-forward field-regex ...) | |
75 ;; ...)) | |
76 | |
77 ;; The arguments to `sregexq' are automatically quoted, but the | |
78 ;; flipside of this is that it is not straightforward to include | |
79 ;; computed (i.e., non-constant) values in `sregexq' expressions. So | |
80 ;; `sregex' is a function that is like `sregexq' but which does not | |
81 ;; automatically quote its values. Literal sregex clauses must be | |
82 ;; explicitly quoted like so: | |
83 | |
84 ;; (sregex '(or "Bob" "Robert")) => "Bob\\|Robert" | |
85 | |
86 ;; but computed clauses can be included easily, allowing for the reuse | |
87 ;; of common clauses: | |
88 | |
89 ;; (let ((dotstar '(0+ any)) | |
90 ;; (whitespace '(1+ (syntax ?-))) | |
91 ;; (digits '(1+ (char (?0 . ?9))))) | |
92 ;; (sregex 'bol dotstar ":" whitespace digits)) => "^.*:\\s-+[0-9]+" | |
93 | |
94 ;; To use this package in a Lisp program, simply (require 'sregex). | |
95 | |
96 ;; Here are the clauses allowed in an `sregex' or `sregexq' | |
97 ;; expression: | |
98 | |
99 ;; - a string | |
100 ;; This stands for the literal string. If it contains | |
101 ;; metacharacters, they will be escaped in the resulting regex | |
102 ;; (using `regexp-quote'). | |
103 | |
104 ;; - the symbol `any' | |
105 ;; This stands for ".", a regex matching any character except | |
106 ;; newline. | |
107 | |
108 ;; - the symbol `bol' | |
109 ;; Stands for "^", matching the empty string at the beginning of a line | |
110 | |
111 ;; - the symbol `eol' | |
112 ;; Stands for "$", matching the empty string at the end of a line | |
113 | |
114 ;; - (group CLAUSE ...) | |
115 ;; Groups the given CLAUSEs using "\\(" and "\\)". | |
116 | |
117 ;; - (sequence CLAUSE ...) | |
118 | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
119 ;; Groups the given CLAUSEs; may or may not use "\\(?:" and "\\)". |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
120 ;; Clauses grouped by `sequence' do not count for purposes of |
22537 | 121 ;; numbering backreferences. Use `sequence' in situations like |
122 ;; this: | |
123 | |
124 ;; (sregexq (or "dog" "cat" | |
125 ;; (sequence (opt "sea ") "monkey"))) | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
126 ;; => "dog\\|cat\\|\\(?:sea \\)?monkey" |
22537 | 127 |
128 ;; where a single `or' alternate needs to contain multiple | |
129 ;; subclauses. | |
130 | |
131 ;; - (backref N) | |
132 ;; Matches the same string previously matched by the Nth "group" in | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
133 ;; the same sregex. N is a positive integer. |
22537 | 134 |
135 ;; - (or CLAUSE ...) | |
136 ;; Matches any one of the CLAUSEs by separating them with "\\|". | |
137 | |
138 ;; - (0+ CLAUSE ...) | |
139 ;; Concatenates the given CLAUSEs and matches zero or more | |
140 ;; occurrences by appending "*". | |
141 | |
142 ;; - (1+ CLAUSE ...) | |
143 ;; Concatenates the given CLAUSEs and matches one or more | |
144 ;; occurrences by appending "+". | |
145 | |
146 ;; - (opt CLAUSE ...) | |
147 ;; Concatenates the given CLAUSEs and matches zero or one occurrence | |
148 ;; by appending "?". | |
149 | |
150 ;; - (repeat MIN MAX CLAUSE ...) | |
151 ;; Concatenates the given CLAUSEs and constructs a regex matching at | |
152 ;; least MIN occurrences and at most MAX occurrences. MIN must be a | |
153 ;; non-negative integer. MAX must be a non-negative integer greater | |
154 ;; than or equal to MIN; or MAX can be nil to mean "infinity." | |
155 | |
156 ;; - (char CHAR-CLAUSE ...) | |
157 ;; Creates a "character class" matching one character from the given | |
158 ;; set. See below for how to construct a CHAR-CLAUSE. | |
159 | |
160 ;; - (not-char CHAR-CLAUSE ...) | |
161 ;; Creates a "character class" matching any one character not in the | |
162 ;; given set. See below for how to construct a CHAR-CLAUSE. | |
163 | |
164 ;; - the symbol `bot' | |
165 ;; Stands for "\\`", matching the empty string at the beginning of | |
166 ;; text (beginning of a string or of a buffer). | |
167 | |
168 ;; - the symbol `eot' | |
169 ;; Stands for "\\'", matching the empty string at the end of text. | |
170 | |
171 ;; - the symbol `point' | |
172 ;; Stands for "\\=", matching the empty string at point. | |
173 | |
174 ;; - the symbol `word-boundary' | |
175 ;; Stands for "\\b", matching the empty string at the beginning or | |
176 ;; end of a word. | |
177 | |
178 ;; - the symbol `not-word-boundary' | |
179 ;; Stands for "\\B", matching the empty string not at the beginning | |
180 ;; or end of a word. | |
181 | |
182 ;; - the symbol `bow' | |
183 ;; Stands for "\\<", matching the empty string at the beginning of a | |
184 ;; word. | |
185 | |
186 ;; - the symbol `eow' | |
187 ;; Stands for "\\>", matching the empty string at the end of a word. | |
188 | |
189 ;; - the symbol `wordchar' | |
190 ;; Stands for the regex "\\w", matching a word-constituent character | |
191 ;; (as determined by the current syntax table) | |
192 | |
193 ;; - the symbol `not-wordchar' | |
194 ;; Stands for the regex "\\W", matching a non-word-constituent | |
195 ;; character. | |
196 | |
197 ;; - (syntax CODE) | |
198 ;; Stands for the regex "\\sCODE", where CODE is a syntax table code | |
199 ;; (a single character). Matches any character with the requested | |
200 ;; syntax. | |
201 | |
202 ;; - (not-syntax CODE) | |
203 ;; Stands for the regex "\\SCODE", where CODE is a syntax table code | |
204 ;; (a single character). Matches any character without the | |
205 ;; requested syntax. | |
206 | |
207 ;; - (regex REGEX) | |
208 ;; This is a "trapdoor" for including ordinary regular expression | |
209 ;; strings in the result. Some regular expressions are clearer when | |
210 ;; written the old way: "[a-z]" vs. (sregexq (char (?a . ?z))), for | |
211 ;; instance. However, see the note under "Bugs," below. | |
212 | |
213 ;; Each CHAR-CLAUSE that is passed to (char ...) and (not-char ...) | |
214 ;; has one of the following forms: | |
215 | |
216 ;; - a character | |
217 ;; Adds that character to the set. | |
218 | |
219 ;; - a string | |
220 ;; Adds all the characters in the string to the set. | |
221 | |
222 ;; - A pair (MIN . MAX) | |
223 ;; Where MIN and MAX are characters, adds the range of characters | |
224 ;; from MIN through MAX to the set. | |
225 | |
226 ;;; To do: | |
227 | |
228 ;; An earlier version of this package could optionally translate the | |
229 ;; symbolic regex into other languages' syntaxes, e.g. Perl. For | |
230 ;; instance, with Perl syntax selected, (sregexq (or "ab" "cd")) would | |
231 ;; yield "ab|cd" instead of "ab\\|cd". It might be useful to restore | |
232 ;; such a facility. | |
233 | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
234 ;; - handle multibyte chars in sregex--char-aux |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
235 ;; - add support for character classes ([:blank:], ...) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
236 ;; - add support for non-greedy operators *? and +? |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
237 ;; - bug: (sregexq (opt (opt ?a))) returns "a??" which is a non-greedy "a?" |
22537 | 238 |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
239 ;;; Bugs: |
22537 | 240 |
241 ;;; Code: | |
242 | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
243 (eval-when-compile (require 'cl)) |
22537 | 244 |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
245 ;; Compatibility code for when we didn't have shy-groups |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
246 (defvar sregex--current-sregex nil) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
247 (defun sregex-info () nil) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
248 (defmacro sregex-save-match-data (&rest forms) (cons 'save-match-data forms)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
249 (defun sregex-replace-match (r &optional f l str subexp x) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
250 (replace-match r f l str subexp)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
251 (defun sregex-match-string (c &optional i x) (match-string c i)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
252 (defun sregex-match-string-no-properties (count &optional in-string sregex) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
253 (match-string-no-properties count in-string)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
254 (defun sregex-match-beginning (count &optional sregex) (match-beginning count)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
255 (defun sregex-match-end (count &optional sregex) (match-end count)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
256 (defun sregex-match-data (&optional sregex) (match-data)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
257 (defun sregex-backref-num (n &optional sregex) n) |
22537 | 258 |
259 | |
260 (defun sregex (&rest exps) | |
261 "Symbolic regular expression interpreter. | |
262 This is exactly like `sregexq' (q.v.) except that it evaluates all its | |
263 arguments, so literal sregex clauses must be quoted. For example: | |
264 | |
265 (sregex '(or \"Bob\" \"Robert\")) => \"Bob\\\\|Robert\" | |
266 | |
267 An argument-evaluating sregex interpreter lets you reuse sregex | |
268 subexpressions: | |
269 | |
270 (let ((dotstar '(0+ any)) | |
271 (whitespace '(1+ (syntax ?-))) | |
272 (digits '(1+ (char (?0 . ?9))))) | |
273 (sregex 'bol dotstar \":\" whitespace digits)) => \"^.*:\\\\s-+[0-9]+\"" | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
274 (sregex--sequence exps nil)) |
22537 | 275 |
276 (defmacro sregexq (&rest exps) | |
277 "Symbolic regular expression interpreter. | |
278 This macro allows you to specify a regular expression (regexp) in | |
279 symbolic form, and converts it into the string form required by Emacs's | |
280 regex functions such as `re-search-forward' and `looking-at'. Here is | |
281 a simple example: | |
282 | |
283 (sregexq (or \"Bob\" \"Robert\")) => \"Bob\\\\|Robert\" | |
284 | |
285 As you can see, an sregex is specified by placing one or more special | |
286 clauses in a call to `sregexq'. The clause in this case is the `or' | |
287 of two strings (not to be confused with the Lisp function `or'). The | |
288 list of allowable clauses appears below. | |
289 | |
290 With `sregex', it is never necessary to \"escape\" magic characters | |
291 that are meant to be taken literally; that happens automatically. | |
292 For example: | |
293 | |
294 (sregexq \"M*A*S*H\") => \"M\\\\*A\\\\*S\\\\*H\" | |
295 | |
296 It is also unnecessary to \"group\" parts of the expression together | |
297 to overcome operator precedence; that also happens automatically. | |
298 For example: | |
299 | |
300 (sregexq (opt (or \"Bob\" \"Robert\"))) => \"\\\\(Bob\\\\|Robert\\\\)?\" | |
301 | |
302 It *is* possible to group parts of the expression in order to refer | |
303 to them with numbered backreferences: | |
304 | |
305 (sregexq (group (or \"Go\" \"Run\")) | |
306 \", Spot, \" | |
307 (backref 1)) => \"\\\\(Go\\\\|Run\\\\), Spot, \\\\1\" | |
308 | |
309 If `sregexq' needs to introduce its own grouping parentheses, it will | |
310 automatically renumber your backreferences: | |
311 | |
312 (sregexq (opt \"resent-\") | |
313 (group (or \"to\" \"cc\" \"bcc\")) | |
314 \": \" | |
315 (backref 1)) => \"\\\\(resent-\\\\)?\\\\(to\\\\|cc\\\\|bcc\\\\): \\\\2\" | |
316 | |
317 `sregexq' is a macro. Each time it is used, it constructs a simple | |
318 Lisp expression that then invokes a moderately complex engine to | |
319 interpret the sregex and render the string form. Because of this, I | |
320 don't recommend sprinkling calls to `sregexq' throughout your code, | |
321 the way one normally does with string regexes (which are cheap to | |
322 evaluate). Instead, it's wiser to precompute the regexes you need | |
323 wherever possible instead of repeatedly constructing the same ones | |
324 over and over. Example: | |
325 | |
326 (let ((field-regex (sregexq (opt \"resent-\") | |
327 (or \"to\" \"cc\" \"bcc\")))) | |
328 ... | |
329 (while ... | |
330 ... | |
331 (re-search-forward field-regex ...) | |
332 ...)) | |
333 | |
334 The arguments to `sregexq' are automatically quoted, but the | |
335 flipside of this is that it is not straightforward to include | |
336 computed (i.e., non-constant) values in `sregexq' expressions. So | |
337 `sregex' is a function that is like `sregexq' but which does not | |
338 automatically quote its values. Literal sregex clauses must be | |
339 explicitly quoted like so: | |
340 | |
341 (sregex '(or \"Bob\" \"Robert\")) => \"Bob\\\\|Robert\" | |
342 | |
343 but computed clauses can be included easily, allowing for the reuse | |
344 of common clauses: | |
345 | |
346 (let ((dotstar '(0+ any)) | |
347 (whitespace '(1+ (syntax ?-))) | |
348 (digits '(1+ (char (?0 . ?9))))) | |
349 (sregex 'bol dotstar \":\" whitespace digits)) => \"^.*:\\\\s-+[0-9]+\" | |
350 | |
351 Here are the clauses allowed in an `sregex' or `sregexq' expression: | |
352 | |
353 - a string | |
354 This stands for the literal string. If it contains | |
355 metacharacters, they will be escaped in the resulting regex | |
356 (using `regexp-quote'). | |
357 | |
358 - the symbol `any' | |
359 This stands for \".\", a regex matching any character except | |
360 newline. | |
361 | |
362 - the symbol `bol' | |
363 Stands for \"^\", matching the empty string at the beginning of a line | |
364 | |
365 - the symbol `eol' | |
366 Stands for \"$\", matching the empty string at the end of a line | |
367 | |
368 - (group CLAUSE ...) | |
369 Groups the given CLAUSEs using \"\\\\(\" and \"\\\\)\". | |
370 | |
371 - (sequence CLAUSE ...) | |
372 | |
373 Groups the given CLAUSEs; may or may not use \"\\\\(\" and \"\\\\)\". | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
374 Clauses grouped by `sequence' do not count for purposes of |
22537 | 375 numbering backreferences. Use `sequence' in situations like |
376 this: | |
377 | |
378 (sregexq (or \"dog\" \"cat\" | |
379 (sequence (opt \"sea \") \"monkey\"))) | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
380 => \"dog\\\\|cat\\\\|\\\\(?:sea \\\\)?monkey\" |
22537 | 381 |
382 where a single `or' alternate needs to contain multiple | |
383 subclauses. | |
384 | |
385 - (backref N) | |
386 Matches the same string previously matched by the Nth \"group\" in | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
387 the same sregex. N is a positive integer. |
22537 | 388 |
389 - (or CLAUSE ...) | |
390 Matches any one of the CLAUSEs by separating them with \"\\\\|\". | |
391 | |
392 - (0+ CLAUSE ...) | |
393 Concatenates the given CLAUSEs and matches zero or more | |
394 occurrences by appending \"*\". | |
395 | |
396 - (1+ CLAUSE ...) | |
397 Concatenates the given CLAUSEs and matches one or more | |
398 occurrences by appending \"+\". | |
399 | |
400 - (opt CLAUSE ...) | |
401 Concatenates the given CLAUSEs and matches zero or one occurrence | |
402 by appending \"?\". | |
403 | |
404 - (repeat MIN MAX CLAUSE ...) | |
405 Concatenates the given CLAUSEs and constructs a regex matching at | |
406 least MIN occurrences and at most MAX occurrences. MIN must be a | |
407 non-negative integer. MAX must be a non-negative integer greater | |
408 than or equal to MIN; or MAX can be nil to mean \"infinity.\" | |
409 | |
410 - (char CHAR-CLAUSE ...) | |
411 Creates a \"character class\" matching one character from the given | |
412 set. See below for how to construct a CHAR-CLAUSE. | |
413 | |
414 - (not-char CHAR-CLAUSE ...) | |
415 Creates a \"character class\" matching any one character not in the | |
416 given set. See below for how to construct a CHAR-CLAUSE. | |
417 | |
418 - the symbol `bot' | |
419 Stands for \"\\\\`\", matching the empty string at the beginning of | |
420 text (beginning of a string or of a buffer). | |
421 | |
422 - the symbol `eot' | |
423 Stands for \"\\\\'\", matching the empty string at the end of text. | |
424 | |
425 - the symbol `point' | |
76829 | 426 Stands for \"\\\\=\\=\", matching the empty string at point. |
22537 | 427 |
428 - the symbol `word-boundary' | |
429 Stands for \"\\\\b\", matching the empty string at the beginning or | |
430 end of a word. | |
431 | |
432 - the symbol `not-word-boundary' | |
433 Stands for \"\\\\B\", matching the empty string not at the beginning | |
434 or end of a word. | |
435 | |
436 - the symbol `bow' | |
77520
8dd3e56c2212
(sregexq): Fix doc string quoting.
Andreas Schwab <schwab@suse.de>
parents:
76829
diff
changeset
|
437 Stands for \"\\\\=\\<\", matching the empty string at the beginning of a |
22537 | 438 word. |
439 | |
440 - the symbol `eow' | |
77520
8dd3e56c2212
(sregexq): Fix doc string quoting.
Andreas Schwab <schwab@suse.de>
parents:
76829
diff
changeset
|
441 Stands for \"\\\\=\\>\", matching the empty string at the end of a word. |
22537 | 442 |
443 - the symbol `wordchar' | |
444 Stands for the regex \"\\\\w\", matching a word-constituent character | |
445 (as determined by the current syntax table) | |
446 | |
447 - the symbol `not-wordchar' | |
448 Stands for the regex \"\\\\W\", matching a non-word-constituent | |
449 character. | |
450 | |
451 - (syntax CODE) | |
452 Stands for the regex \"\\\\sCODE\", where CODE is a syntax table code | |
453 (a single character). Matches any character with the requested | |
454 syntax. | |
455 | |
456 - (not-syntax CODE) | |
457 Stands for the regex \"\\\\SCODE\", where CODE is a syntax table code | |
458 (a single character). Matches any character without the | |
459 requested syntax. | |
460 | |
461 - (regex REGEX) | |
462 This is a \"trapdoor\" for including ordinary regular expression | |
463 strings in the result. Some regular expressions are clearer when | |
464 written the old way: \"[a-z]\" vs. (sregexq (char (?a . ?z))), for | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
465 instance. |
22537 | 466 |
467 Each CHAR-CLAUSE that is passed to (char ...) and (not-char ...) | |
468 has one of the following forms: | |
469 | |
470 - a character | |
471 Adds that character to the set. | |
472 | |
473 - a string | |
474 Adds all the characters in the string to the set. | |
475 | |
476 - A pair (MIN . MAX) | |
477 Where MIN and MAX are characters, adds the range of characters | |
478 from MIN through MAX to the set." | |
479 `(apply 'sregex ',exps)) | |
480 | |
481 (defun sregex--engine (exp combine) | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
482 (cond |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
483 ((stringp exp) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
484 (if (and combine |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
485 (eq combine 'suffix) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
486 (/= (length exp) 1)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
487 (concat "\\(?:" (regexp-quote exp) "\\)") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
488 (regexp-quote exp))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
489 ((symbolp exp) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
490 (ecase exp |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
491 (any ".") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
492 (bol "^") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
493 (eol "$") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
494 (wordchar "\\w") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
495 (not-wordchar "\\W") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
496 (bot "\\`") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
497 (eot "\\'") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
498 (point "\\=") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
499 (word-boundary "\\b") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
500 (not-word-boundary "\\B") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
501 (bow "\\<") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
502 (eow "\\>"))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
503 ((consp exp) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
504 (funcall (intern (concat "sregex--" |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
505 (symbol-name (car exp)))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
506 (cdr exp) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
507 combine)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
508 (t (error "Invalid expression: %s" exp)))) |
22537 | 509 |
510 (defun sregex--sequence (exps combine) | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
511 (if (= (length exps) 1) (sregex--engine (car exps) combine) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
512 (let ((re (mapconcat |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
513 (lambda (e) (sregex--engine e 'concat)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
514 exps ""))) |
22537 | 515 (if (eq combine 'suffix) |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
516 (concat "\\(?:" re "\\)") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
517 re)))) |
22537 | 518 |
519 (defun sregex--or (exps combine) | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
520 (if (= (length exps) 1) (sregex--engine (car exps) combine) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
521 (let ((re (mapconcat |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
522 (lambda (e) (sregex--engine e 'or)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
523 exps "\\|"))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
524 (if (not (eq combine 'or)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
525 (concat "\\(?:" re "\\)") |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
526 re)))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
527 |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
528 (defun sregex--group (exps combine) (concat "\\(" (sregex--sequence exps nil) "\\)")) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
529 |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
530 (defun sregex--backref (exps combine) (concat "\\" (int-to-string (car exps)))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
531 (defun sregex--opt (exps combine) (concat (sregex--sequence exps 'suffix) "?")) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
532 (defun sregex--0+ (exps combine) (concat (sregex--sequence exps 'suffix) "*")) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
533 (defun sregex--1+ (exps combine) (concat (sregex--sequence exps 'suffix) "+")) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
534 |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
535 (defun sregex--char (exps combine) (sregex--char-aux nil exps)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
536 (defun sregex--not-char (exps combine) (sregex--char-aux t exps)) |
22537 | 537 |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
538 (defun sregex--syntax (exps combine) (format "\\s%c" (car exps))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
539 (defun sregex--not-syntax (exps combine) (format "\\S%c" (car exps))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
540 |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
541 (defun sregex--regex (exps combine) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
542 (if combine (concat "\\(?:" (car exps) "\\)") (car exps))) |
22537 | 543 |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
544 (defun sregex--repeat (exps combine) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
545 (let* ((min (or (pop exps) 0)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
546 (minstr (number-to-string min)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
547 (max (pop exps))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
548 (concat (sregex--sequence exps 'suffix) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
549 (concat "\\{" minstr "," |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
550 (when max (number-to-string max)) "\\}")))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
551 |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
552 (defun sregex--char-range (start end) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
553 (let ((startc (char-to-string start)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
554 (endc (char-to-string end))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
555 (cond |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
556 ((> end (+ start 2)) (concat startc "-" endc)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
557 ((> end (+ start 1)) (concat startc (char-to-string (1+ start)) endc)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
558 ((> end start) (concat startc endc)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
559 (t startc)))) |
22537 | 560 |
561 (defun sregex--char-aux (complement args) | |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
562 ;; regex-opt does the same, we should join effort. |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
563 (let ((chars (make-bool-vector 256 nil))) ; Yeah, right! |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
564 (dolist (arg args) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
565 (cond ((integerp arg) (aset chars arg t)) |
84904
e2e245301b8c
(sregex--char-aux): Use `mapc' rather than `mapcar'.
Juanma Barranquero <lekktu@gmail.com>
parents:
78217
diff
changeset
|
566 ((stringp arg) (mapc (lambda (c) (aset chars c t)) arg)) |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
567 ((consp arg) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
568 (let ((start (car arg)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
569 (end (cdr arg))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
570 (when (> start end) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
571 (let ((tmp start)) (setq start end) (setq end tmp))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
572 ;; now start <= end |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
573 (let ((i start)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
574 (while (<= i end) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
575 (aset chars i t) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
576 (setq i (1+ i)))))))) |
22537 | 577 ;; now chars is a map of the characters in the class |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
578 (let ((caret (aref chars ?^)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
579 (dash (aref chars ?-)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
580 (class (if (aref chars ?\]) "]" ""))) |
22537 | 581 (aset chars ?^ nil) |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
582 (aset chars ?- nil) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
583 (aset chars ?\] nil) |
22537 | 584 |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
585 (let (start end) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
586 (dotimes (i 256) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
587 (if (aref chars i) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
588 (progn |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
589 (unless start (setq start i)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
590 (setq end i) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
591 (aset chars i nil)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
592 (when start |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
593 (setq class (concat class (sregex--char-range start end))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
594 (setq start nil)))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
595 (if start |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
596 (setq class (concat class (sregex--char-range start end))))) |
22537 | 597 |
29069
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
598 (if (> (length class) 0) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
599 (setq class (concat class (if caret "^") (if dash "-"))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
600 (setq class (concat class (if dash "-") (if caret "^")))) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
601 (if (and (not complement) (= (length class) 1)) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
602 (regexp-quote class) |
cb028b1d6345
Rewritten to take advantage of shy-groups and
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
22974
diff
changeset
|
603 (concat "[" (if complement "^") class "]"))))) |
22537 | 604 |
605 (provide 'sregex) | |
606 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
607 ;; arch-tag: 460c1f5a-eb6e-42ec-a451-ffac78bdf492 |
22537 | 608 ;;; sregex.el ends here |