Mercurial > emacs
annotate lisp/emacs-lisp/regexp-opt.el @ 27967:7d61ff2d1530
(note_mouse_highlight): Return quickly if frame's
glyph matrices have been freed.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Thu, 02 Mar 2000 20:08:20 +0000 |
parents | ab0d5f2bb751 |
children | e09db52da018 |
rev | line source |
---|---|
18014 | 1 ;;; regexp-opt.el --- generate efficient regexps to match strings. |
2 | |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
3 ;; Copyright (C) 1994, 95, 96, 97, 98, 1999 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. |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
52 ;; |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
53 ;; Note that this package will also find common suffix strings if this does not |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
54 ;; increase the number of grouping constructs. For example: |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
55 ;; |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
56 ;; (regexp-opt '("these" "those")) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
57 ;; => "th[eo]se" |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
58 ;; |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
59 ;; but: |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
60 ;; |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
61 ;; (regexp-opt '("barfly" "housefly")) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
62 ;; => "barfly\\|housefly" rather than "\\(bar\\|house\\)fly" |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
63 |
18014 | 64 ;; Since this package was written to produce efficient regexps, not regexps |
65 ;; efficiently, it is probably not a good idea to in-line too many calls in | |
66 ;; your code, unless you use the following trick with `eval-when-compile': | |
67 ;; | |
68 ;; (defvar definition-regexp | |
69 ;; (eval-when-compile | |
70 ;; (concat "^(" | |
71 ;; (regexp-opt '("defun" "defsubst" "defmacro" "defalias" | |
72 ;; "defvar" "defconst") t) | |
73 ;; "\\>"))) | |
74 ;; | |
75 ;; The `byte-compile' code will be as if you had defined the variable thus: | |
76 ;; | |
77 ;; (defvar definition-regexp | |
78 ;; "^(\\(def\\(alias\\|const\\|macro\\|subst\\|un\\|var\\)\\)\\>") | |
79 ;; | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
80 ;; 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
|
81 ;; `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
|
82 ;; 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
|
83 ;; 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
|
84 ;; 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
|
85 ;; 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
|
86 |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
87 ;; 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
|
88 ;; 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
|
89 ;; Stefan Monnier. |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
90 ;; 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
|
91 ;; or any other information to improve things are welcome. |
18014 | 92 |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
93 ;;; Code. |
18014 | 94 |
95 ;;;###autoload | |
96 (defun regexp-opt (strings &optional paren) | |
97 "Return a regexp to match a string in STRINGS. | |
19782 | 98 Each string should be unique in STRINGS and should not contain any regexps, |
99 quoted or not. If optional PAREN is non-nil, ensure that the returned regexp | |
100 is enclosed by at least one regexp grouping construct. | |
18014 | 101 The returned regexp is typically more efficient than the equivalent regexp: |
102 | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
103 (let ((open-paren (if PAREN \"\\\\(\" \"\")) (close-paren (if PAREN \"\\\\)\" \"\"))) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
104 (concat open-paren (mapconcat 'regexp-quote STRINGS \"\\\\|\") close-paren)) |
18014 | 105 |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
106 but typically contains more regexp grouping constructs. |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
107 Use `regexp-opt-depth' to count them." |
18014 | 108 (save-match-data |
109 ;; Recurse on the sorted list. | |
110 (let ((max-lisp-eval-depth (* 1024 1024)) | |
111 (completion-ignore-case nil)) | |
112 (regexp-opt-group (sort (copy-sequence strings) 'string-lessp) paren)))) | |
113 | |
114 ;;;###autoload | |
115 (defun regexp-opt-depth (regexp) | |
116 "Return the depth of REGEXP. | |
117 This means the number of regexp grouping constructs (parenthesised expressions) | |
118 in REGEXP." | |
119 (save-match-data | |
120 ;; Hack to signal an error if REGEXP does not have balanced parentheses. | |
121 (string-match regexp "") | |
122 ;; Count the number of open parentheses in REGEXP. | |
123 (let ((count 0) start) | |
124 (while (string-match "\\\\(" regexp start) | |
125 (setq count (1+ count) start (match-end 0))) | |
126 count))) | |
127 | |
128 ;;; Workhorse functions. | |
129 | |
130 (eval-when-compile | |
131 (require 'cl)) | |
132 | |
133 (unless (fboundp 'make-bool-vector) | |
134 (defalias 'make-bool-vector 'make-vector)) | |
135 | |
136 (defun regexp-opt-group (strings &optional paren lax) | |
137 ;; | |
138 ;; Return a regexp to match a string in STRINGS. | |
139 ;; If PAREN non-nil, output regexp parentheses around returned regexp. | |
140 ;; If LAX non-nil, don't output parentheses if it doesn't require them. | |
141 ;; Merges keywords to avoid backtracking in Emacs' regexp matcher. | |
142 ;; | |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
143 ;; The basic idea is to find the shortest common prefix or suffix, remove it |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
144 ;; and recurse. If there is no prefix, we divide the list into two so that |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
145 ;; (at least) one half will have at least a one-character common prefix. |
18014 | 146 ;; |
147 ;; Also we delay the addition of grouping parenthesis as long as possible | |
148 ;; until we're sure we need them, and try to remove one-character sequences | |
149 ;; so we can use character sets rather than grouping parenthesis. | |
150 ;; | |
151 (let* ((open-group (if paren "\\(" "")) | |
152 (close-group (if paren "\\)" "")) | |
153 (open-charset (if lax "" open-group)) | |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
154 (close-charset (if lax "" close-group)) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
155 (open-presuf open-charset) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
156 (close-presuf close-charset)) |
18014 | 157 (cond |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
158 ;; |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
159 ;; 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
|
160 ((= (length strings) 0) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
161 "") |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
162 ;; |
18014 | 163 ;; If there is only one string, just return it. |
164 ((= (length strings) 1) | |
165 (if (= (length (car strings)) 1) | |
166 (concat open-charset (regexp-quote (car strings)) close-charset) | |
167 (concat open-group (regexp-quote (car strings)) close-group))) | |
168 ;; | |
169 ;; If there is an empty string, remove it and recurse on the rest. | |
170 ((= (length (car strings)) 0) | |
171 (concat open-charset | |
172 (regexp-opt-group (cdr strings) t t) "?" | |
173 close-charset)) | |
174 ;; | |
175 ;; If all are one-character strings, just return a character set. | |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
176 ((= (apply 'max (mapcar 'length strings)) 1) |
18014 | 177 (concat open-charset |
178 (regexp-opt-charset strings) | |
179 close-charset)) | |
180 ;; | |
181 ;; We have a list of different length strings. | |
182 (t | |
183 (let ((prefix (try-completion "" (mapcar 'list strings))) | |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
184 (suffix (regexp-opt-try-suffix strings)) |
18014 | 185 (letters (let ((completion-regexp-list '("^.$"))) |
186 (all-completions "" (mapcar 'list strings))))) | |
187 (cond | |
188 ;; | |
189 ;; If there is a common prefix, remove it and recurse on the suffixes. | |
190 ((> (length prefix) 0) | |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
191 (let* ((end (length prefix)) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
192 (suffixes (mapcar (lambda (s) (substring s end)) strings))) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
193 (concat open-presuf |
18014 | 194 (regexp-quote prefix) (regexp-opt-group suffixes t t) |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
195 close-presuf))) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
196 ;; |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
197 ;; If there is a common suffix, remove it and recurse on the prefixes. |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
198 ((> (length suffix) (if lax |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
199 0 |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
200 (- (apply 'max (mapcar 'length strings)) 2))) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
201 (let* ((end (- (length suffix))) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
202 (prefixes (sort (mapcar (lambda (s) (substring s 0 end)) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
203 strings) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
204 'string-lessp))) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
205 (concat open-presuf |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
206 (regexp-opt-group prefixes t t) (regexp-quote suffix) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
207 close-presuf))) |
18014 | 208 ;; |
209 ;; If there are several one-character strings, remove them and recurse | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
210 ;; on the rest (first so the final regexp finds the longest match). |
18014 | 211 ((> (length letters) 1) |
212 (let ((rest (let ((completion-regexp-list '("^..+$"))) | |
213 (all-completions "" (mapcar 'list strings))))) | |
214 (concat open-group | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
215 (regexp-opt-group rest) "\\|" (regexp-opt-charset letters) |
18014 | 216 close-group))) |
217 ;; | |
218 ;; Otherwise, divide the list into those that start with a particular | |
219 ;; letter and those that do not, and recurse on them. | |
220 (t | |
221 (let* ((char (substring (car strings) 0 1)) | |
222 (half1 (all-completions char (mapcar 'list strings))) | |
223 (half2 (nthcdr (length half1) strings))) | |
224 (concat open-group | |
225 (regexp-opt-group half1) "\\|" (regexp-opt-group half2) | |
226 close-group))))))))) | |
227 | |
228 (defun regexp-opt-charset (chars) | |
229 ;; | |
230 ;; Return a regexp to match a character in CHARS. | |
231 ;; | |
232 ;; The basic idea is to find character ranges. Also we take care in the | |
233 ;; position of character set meta characters in the character set regexp. | |
234 ;; | |
235 (let* ((charwidth 256) ; Yeah, right. | |
236 (charmap (make-bool-vector charwidth nil)) | |
237 (charset "") | |
238 (bracket "") (dash "") (caret "")) | |
239 ;; | |
240 ;; 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
|
241 (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
|
242 (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
|
243 (?\] |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
244 (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
|
245 (?^ |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
246 (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
|
247 (?- |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
248 (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
|
249 (otherwise |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
250 (aset charmap char t)))) |
18014 | 251 ;; |
252 ;; Make a character set from the map using ranges where applicable. | |
18465 | 253 (dotimes (char charwidth) |
254 (let ((start char)) | |
255 (while (and (< char charwidth) (aref charmap char)) | |
256 (incf char)) | |
257 (cond ((> char (+ start 3)) | |
258 (setq charset (format "%s%c-%c" charset start (1- char)))) | |
259 ((> char start) | |
260 (setq charset (format "%s%c" charset (setq char start))))))) | |
18014 | 261 ;; |
262 ;; Make sure a caret is not first and a dash is first or last. | |
263 (if (and (string-equal charset "") (string-equal bracket "")) | |
264 (concat "[" dash caret "]") | |
265 (concat "[" bracket charset caret dash "]")))) | |
266 | |
25938
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
267 (defun regexp-opt-try-suffix (strings) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
268 ;; |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
269 ;; Return common suffix of each string in STRINGS. See `try-completion'. |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
270 ;; |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
271 (let* ((chars (mapcar (lambda (s) (mapcar 'identity s)) strings)) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
272 (srahc (mapcar 'reverse chars)) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
273 (sgnirts (mapcar (lambda (c) (mapconcat 'char-to-string c "")) srahc)) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
274 (xiffus (try-completion "" (mapcar 'list sgnirts)))) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
275 (mapconcat 'char-to-string (reverse (mapcar 'identity xiffus)) ""))) |
6f591e2d9c0d
(regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
25278
diff
changeset
|
276 |
18014 | 277 (provide 'regexp-opt) |
278 | |
279 ;;; regexp-opt.el ends here |