Mercurial > emacs
annotate lisp/emacs-lisp/regexp-opt.el @ 28394:7640cbe85ab7
(format-alist): Use iso-cvt functions for SGML/HTML.
author | Dave Love <fx@gnu.org> |
---|---|
date | Wed, 29 Mar 2000 18:35:01 +0000 |
parents | e09db52da018 |
children | 854590466b7b |
rev | line source |
---|---|
18014 | 1 ;;; regexp-opt.el --- generate efficient regexps to match strings. |
2 | |
28067
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
3 ;; Copyright (C) 1994,95,96,97,98,99,2000 Free Software Foundation, Inc. |
18014 | 4 |
25278 | 5 ;; Author: Simon Marshall <simon@gnu.org> |
27589 | 6 ;; Maintainer: FSF |
18014 | 7 ;; Keywords: strings, regexps |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
8 ;; Version: 1.07 |
18014 | 9 |
10 ;; This file is part of GNU Emacs. | |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation; either version 2, or (at your option) | |
15 ;; any later version. | |
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 | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 ;; Boston, MA 02111-1307, USA. | |
26 | |
27 ;;; Commentary: | |
28 | |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
29 ;; The "opt" in "regexp-opt" stands for "optim\\(al\\|i[sz]e\\)". |
18014 | 30 ;; |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
31 ;; This package generates a regexp from a given list of strings (which matches |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
32 ;; one of those strings) so that the regexp generated by: |
18014 | 33 ;; |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
34 ;; (regexp-opt strings) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
35 ;; |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
36 ;; is equivalent to, but more efficient than, the regexp generated by: |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
37 ;; |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
38 ;; (mapconcat 'regexp-quote strings "\\|") |
18014 | 39 ;; |
40 ;; For example: | |
41 ;; | |
42 ;; (let ((strings '("cond" "if" "when" "unless" "while" | |
43 ;; "let" "let*" "progn" "prog1" "prog2" | |
44 ;; "save-restriction" "save-excursion" "save-window-excursion" | |
45 ;; "save-current-buffer" "save-match-data" | |
46 ;; "catch" "throw" "unwind-protect" "condition-case"))) | |
47 ;; (concat "(" (regexp-opt strings t) "\\>")) | |
48 ;; => "(\\(c\\(atch\\|ond\\(ition-case\\)?\\)\\|if\\|let\\*?\\|prog[12n]\\|save-\\(current-buffer\\|excursion\\|match-data\\|restriction\\|window-excursion\\)\\|throw\\|un\\(less\\|wind-protect\\)\\|wh\\(en\\|ile\\)\\)\\>" | |
49 ;; | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
50 ;; Searching using the above example `regexp-opt' regexp takes approximately |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
51 ;; two-thirds of the time taken using the equivalent `mapconcat' regexp. |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
52 |
18014 | 53 ;; Since this package was written to produce efficient regexps, not regexps |
54 ;; efficiently, it is probably not a good idea to in-line too many calls in | |
55 ;; your code, unless you use the following trick with `eval-when-compile': | |
56 ;; | |
57 ;; (defvar definition-regexp | |
58 ;; (eval-when-compile | |
59 ;; (concat "^(" | |
60 ;; (regexp-opt '("defun" "defsubst" "defmacro" "defalias" | |
61 ;; "defvar" "defconst") t) | |
62 ;; "\\>"))) | |
63 ;; | |
64 ;; The `byte-compile' code will be as if you had defined the variable thus: | |
65 ;; | |
66 ;; (defvar definition-regexp | |
67 ;; "^(\\(def\\(alias\\|const\\|macro\\|subst\\|un\\|var\\)\\)\\>") | |
68 ;; | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
69 ;; Note that if you use this trick for all instances of `regexp-opt' and |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
70 ;; `regexp-opt-depth' in your code, regexp-opt.el would only have to be loaded |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
71 ;; at compile time. But note also that using this trick means that should |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
72 ;; regexp-opt.el be changed, perhaps to fix a bug or to add a feature to |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
73 ;; improve the efficiency of `regexp-opt' regexps, you would have to recompile |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
74 ;; your code for such changes to have effect in your code. |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
75 |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
76 ;; Originally written for font-lock.el, from an idea from Stig's hl319.el, with |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
77 ;; thanks for ideas also to Michael Ernst, Bob Glickstein, Dan Nicolaescu and |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
78 ;; Stefan Monnier. |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
79 ;; No doubt `regexp-opt' doesn't always produce optimal regexps, so code, ideas |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
80 ;; or any other information to improve things are welcome. |
28067
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
81 ;; |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
82 ;; One possible improvement would be to compile '("aa" "ab" "ba" "bb") |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
83 ;; into "[ab][ab]" rather than "a[ab]\\|b[ab]". I'm not sure it's worth |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
84 ;; it but if someone knows how to do it without going through too many |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
85 ;; contortions, I'm all ears. |
18014 | 86 |
28067
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
87 ;;; Code: |
18014 | 88 |
89 ;;;###autoload | |
90 (defun regexp-opt (strings &optional paren) | |
91 "Return a regexp to match a string in STRINGS. | |
19782 | 92 Each string should be unique in STRINGS and should not contain any regexps, |
93 quoted or not. If optional PAREN is non-nil, ensure that the returned regexp | |
94 is enclosed by at least one regexp grouping construct. | |
18014 | 95 The returned regexp is typically more efficient than the equivalent regexp: |
96 | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
97 (let ((open-paren (if PAREN \"\\\\(\" \"\")) (close-paren (if PAREN \"\\\\)\" \"\"))) |
28067
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
98 (concat open-paren (mapconcat 'regexp-quote STRINGS \"\\\\|\") close-paren))" |
18014 | 99 (save-match-data |
100 ;; Recurse on the sorted list. | |
101 (let ((max-lisp-eval-depth (* 1024 1024)) | |
102 (completion-ignore-case nil)) | |
28067
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
103 (setq paren (cond ((stringp paren) paren) (paren "\\("))) |
18014 | 104 (regexp-opt-group (sort (copy-sequence strings) 'string-lessp) paren)))) |
105 | |
106 ;;;###autoload | |
107 (defun regexp-opt-depth (regexp) | |
108 "Return the depth of REGEXP. | |
109 This means the number of regexp grouping constructs (parenthesised expressions) | |
110 in REGEXP." | |
111 (save-match-data | |
112 ;; Hack to signal an error if REGEXP does not have balanced parentheses. | |
113 (string-match regexp "") | |
114 ;; Count the number of open parentheses in REGEXP. | |
115 (let ((count 0) start) | |
28067
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
116 (while (string-match "\\\\\\(\\\\\\\\\\)*([^?]" regexp start) |
18014 | 117 (setq count (1+ count) start (match-end 0))) |
118 count))) | |
119 | |
120 ;;; Workhorse functions. | |
121 | |
122 (eval-when-compile | |
123 (require 'cl)) | |
124 | |
125 (unless (fboundp 'make-bool-vector) | |
126 (defalias 'make-bool-vector 'make-vector)) | |
127 | |
128 (defun regexp-opt-group (strings &optional paren lax) | |
28067
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
129 "Return a regexp to match a string in STRINGS. |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
130 If PAREN non-nil, output regexp parentheses around returned regexp. |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
131 If LAX non-nil, don't output parentheses if it doesn't require them. |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
132 Merges keywords to avoid backtracking in Emacs' regexp matcher. |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
133 |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
134 The basic idea is to find the shortest common prefix or suffix, remove it |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
135 and recurse. If there is no prefix, we divide the list into two so that |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
136 \(at least) one half will have at least a one-character common prefix. |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
137 |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
138 Also we delay the addition of grouping parenthesis as long as possible |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
139 until we're sure we need them, and try to remove one-character sequences |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
140 so we can use character sets rather than grouping parenthesis." |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
141 (let* ((open-group (cond ((stringp paren) paren) (paren "\\(?:") (t ""))) |
18014 | 142 (close-group (if paren "\\)" "")) |
143 (open-charset (if lax "" open-group)) | |
28067
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
144 (close-charset (if lax "" close-group))) |
18014 | 145 (cond |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
146 ;; |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
147 ;; If there are no strings, just return the empty string. |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
148 ((= (length strings) 0) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
149 "") |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
150 ;; |
18014 | 151 ;; If there is only one string, just return it. |
152 ((= (length strings) 1) | |
153 (if (= (length (car strings)) 1) | |
154 (concat open-charset (regexp-quote (car strings)) close-charset) | |
155 (concat open-group (regexp-quote (car strings)) close-group))) | |
156 ;; | |
157 ;; If there is an empty string, remove it and recurse on the rest. | |
158 ((= (length (car strings)) 0) | |
159 (concat open-charset | |
160 (regexp-opt-group (cdr strings) t t) "?" | |
161 close-charset)) | |
162 ;; | |
28067
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
163 ;; If there are several one-char strings, use charsets |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
164 ((and (= (length (car strings)) 1) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
165 (let ((strs (cdr strings))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
166 (while (and strs (/= (length (car strs)) 1)) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
167 (pop strs)) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
168 strs)) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
169 (let (letters rest) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
170 ;; Collect one-char strings |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
171 (dolist (s strings) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
172 (if (= (length s) 1) (push s letters) (push s rest))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
173 |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
174 (if rest |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
175 ;; several one-char strings: take them and recurse |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
176 ;; on the rest (first so as to match the longest). |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
177 (concat open-group |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
178 (regexp-opt-group (nreverse rest)) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
179 "\\|" (regexp-opt-charset letters) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
180 close-group) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
181 ;; all are one-char strings: just return a character set. |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
182 (concat open-charset |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
183 (regexp-opt-charset letters) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
184 close-charset)))) |
18014 | 185 ;; |
186 ;; We have a list of different length strings. | |
187 (t | |
28067
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
188 (let ((prefix (try-completion "" (mapcar 'list strings)))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
189 (if (> (length prefix) 0) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
190 ;; common prefix: take it and recurse on the suffixes. |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
191 (let* ((n (length prefix)) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
192 (suffixes (mapcar (lambda (s) (substring s n)) strings))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
193 (concat open-charset |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
194 (regexp-quote prefix) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
195 (regexp-opt-group suffixes t t) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
196 close-charset)) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
197 |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
198 (let* ((sgnirts (mapcar (lambda (s) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
199 (concat (nreverse (string-to-list s)))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
200 strings)) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
201 (xiffus (try-completion "" (mapcar 'list sgnirts)))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
202 (if (> (length xiffus) 0) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
203 ;; common suffix: take it and recurse on the prefixes. |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
204 (let* ((n (- (length xiffus))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
205 (prefixes (mapcar (lambda (s) (substring s 0 n)) strings))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
206 (concat open-charset |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
207 (regexp-opt-group prefixes t t) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
208 (regexp-quote |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
209 (concat (nreverse (string-to-list xiffus)))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
210 close-charset)) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
211 |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
212 ;; Otherwise, divide the list into those that start with a |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
213 ;; particular letter and those that do not, and recurse on them. |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
214 (let* ((char (char-to-string (string-to-char (car strings)))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
215 (half1 (all-completions char (mapcar 'list strings))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
216 (half2 (nthcdr (length half1) strings))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
217 (concat open-group |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
218 (regexp-opt-group half1) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
219 "\\|" (regexp-opt-group half2) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
220 close-group)))))))))) |
e09db52da018
Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
27589
diff
changeset
|
221 |
18014 | 222 |
223 (defun regexp-opt-charset (chars) | |
224 ;; | |
225 ;; Return a regexp to match a character in CHARS. | |
226 ;; | |
227 ;; The basic idea is to find character ranges. Also we take care in the | |
228 ;; position of character set meta characters in the character set regexp. | |
229 ;; | |
230 (let* ((charwidth 256) ; Yeah, right. | |
231 (charmap (make-bool-vector charwidth nil)) | |
232 (charset "") | |
233 (bracket "") (dash "") (caret "")) | |
234 ;; | |
235 ;; Make a character map but extract character set meta characters. | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
236 (dolist (char (mapcar 'string-to-char chars)) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
237 (case char |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
238 (?\] |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
239 (setq bracket "]")) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
240 (?^ |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
241 (setq caret "^")) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
242 (?- |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
243 (setq dash "-")) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
244 (otherwise |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
245 (aset charmap char t)))) |
18014 | 246 ;; |
247 ;; Make a character set from the map using ranges where applicable. | |
18465 | 248 (dotimes (char charwidth) |
249 (let ((start char)) | |
250 (while (and (< char charwidth) (aref charmap char)) | |
251 (incf char)) | |
252 (cond ((> char (+ start 3)) | |
253 (setq charset (format "%s%c-%c" charset start (1- char)))) | |
254 ((> char start) | |
255 (setq charset (format "%s%c" charset (setq char start))))))) | |
18014 | 256 ;; |
257 ;; Make sure a caret is not first and a dash is first or last. | |
258 (if (and (string-equal charset "") (string-equal bracket "")) | |
259 (concat "[" dash caret "]") | |
260 (concat "[" bracket charset caret dash "]")))) | |
261 | |
262 (provide 'regexp-opt) | |
263 | |
264 ;;; regexp-opt.el ends here |