annotate lisp/utils/regexp-opt.el @ 211:78478c60bfcd r20-4b4

Import from CVS: tag r20-4b4
author cvs
date Mon, 13 Aug 2007 10:05:51 +0200
parents acd284d43ca1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
1 ;;; regexp-opt.el --- generate efficient regexps to match strings.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
2
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
3 ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
4
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
5 ;; Author: Simon Marshall <simon@gnu.ai.mit.edu>
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
6 ;; Keywords: strings, regexps
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
7
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
8 ;; Modified by Karl M. Hegbloom Sep. 1997 to support the new regexp syntax
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
9 ;; with shy groups. (benchmarks pending)
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
10
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
11 ;; This file is part of XEmacs.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
12
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
13 ;; XEmacs is free software; you can redistribute it and/or modify
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
14 ;; it under the terms of the GNU General Public License as published by
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
15 ;; the Free Software Foundation; either version 2, or (at your option)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
16 ;; any later version.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
17
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
18 ;; XEmacs is distributed in the hope that it will be useful,
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
21 ;; GNU General Public License for more details.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
22
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
23 ;; You should have received a copy of the GNU General Public License
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
24 ;; along with XEmacs; see the file COPYING. If not, write to the
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
26 ;; Boston, MA 02111-1307, USA.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
27
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
28 ;;; Commentary:
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
29
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
30 ;; The "opt" in "regexp-opt" stands for "optim\\(al\\|i\\(se\\|ze\\)\\)".
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
31 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
32 ;; This package generates a regexp from a given list of strings (which matches
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
33 ;; one of those strings) so that the regexp generated by:
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
34 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
35 ;; (regexp-opt strings)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
36 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
37 ;; is equivalent to, but more efficient than, the regexp generated by:
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
38 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
39 ;; (mapconcat 'regexp-quote strings "\\|")
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
40 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
41 ;; For example:
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
42 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
43 ;; (let ((strings '("cond" "if" "when" "unless" "while"
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
44 ;; "let" "let*" "progn" "prog1" "prog2"
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
45 ;; "save-restriction" "save-excursion" "save-window-excursion"
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
46 ;; "save-current-buffer" "save-match-data"
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
47 ;; "catch" "throw" "unwind-protect" "condition-case")))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
48 ;; (concat "(" (regexp-opt strings t) "\\>"))
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
49 ;;
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
50 ;; => "(\\(?: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\\)\\)\\>"
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
51 ;;
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
52 ;;
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
53 ;; (let ((strings '("cond" "if" "when" "unless" "while"
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
54 ;; "let" "let*" "progn" "prog1" "prog2"
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
55 ;; "save-restriction" "save-excursion" "save-window-excursion"
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
56 ;; "save-current-buffer" "save-match-data"
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
57 ;; "catch" "throw" "unwind-protect" "condition-case")))
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
58 ;; (concat "(" (regexp-opt strings t t) "\\>"))
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
59 ;; ^
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
60 ;; => "(\\(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\\)\\)\\>"
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
61 ;;
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
62 ;;
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
63 ;; Searching using the above example `regexp-opt' regexp takes approximately
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
64 ;; two-thirds of the time taken using the equivalent `mapconcat' regexp.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
65
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
66 ;; Since this package was written to produce efficient regexps, not regexps
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
67 ;; efficiently, it is probably not a good idea to in-line too many calls in
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
68 ;; your code, unless you use the following trick with `eval-when-compile':
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
69 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
70 ;; (defvar definition-regexp
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
71 ;; (eval-when-compile
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
72 ;; (concat "^("
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
73 ;; (regexp-opt '("defun" "defsubst" "defmacro" "defalias"
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
74 ;; "defvar" "defconst") t)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
75 ;; "\\>")))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
76 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
77 ;; The `byte-compile' code will be as if you had defined the variable thus:
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
78 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
79 ;; (defvar definition-regexp
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
80 ;; "^(\\(def\\(alias\\|const\\|macro\\|subst\\|un\\|var\\)\\)\\>")
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
81 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
82 ;; Note that if you use this trick for all instances of `regexp-opt' and
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
83 ;; `regexp-opt-depth' in your code, regexp-opt.el would only have to be loaded
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
84 ;; at compile time. But note also that using this trick means that should
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
85 ;; regexp-opt.el be changed, perhaps to fix a bug or to add a feature to
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
86 ;; improve the efficiency of `regexp-opt' regexps, you would have to recompile
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
87 ;; your code for such changes to have effect in your code.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
88
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
89 ;; Originally written for font-lock.el, from an idea from Stig's hl319.el, with
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
90 ;; thanks for ideas also to Michael Ernst, Bob Glickstein and Dan Nicolaescu.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
91 ;; Please don't tell me that it doesn't produce optimal regexps; I know that
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
92 ;; already. For example, the above explanation for the meaning of "opt" would
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
93 ;; be more efficient as "optim\\(al\\|i[sz]e\\)", but this requires complex
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
94 ;; forward looking. But (ideas or) code to improve things (are) is welcome.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
95
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
96 ;;; Code:
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
97
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
98 ;;;###autoload
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
99 (defun regexp-opt (strings &optional paren non-shy)
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
100 "Return a regexp to match a string in STRINGS.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
101 Each string should be unique in STRINGS and should not contain any regexps,
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
102 quoted or not. If optional PAREN is non-nil, ensure that the returned
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
103 regexp is enclosed by at least one regexp match grouping construct. If
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
104 optional NON-SHY is non nil, the inner groupings will use \"\\\\( \\\\)\" grouping,
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
105 rather than the default \"\\\\(?: \\\\)\" 'shy', or non-match-capturing groups.
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
106 The returned regexp is typically more efficient than the equivalent regexp:
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
107
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
108 (let ((open-paren (if PAREN \"\\\\(\" \"\")) (close-paren (if PAREN \"\\\\)\" \"\")))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
109 (concat open-paren (mapconcat 'regexp-quote STRINGS \"\\\\|\") close-paren))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
110
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
111 but typically contains more regexp grouping constructs.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
112 Use `regexp-opt-depth' to count them."
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
113 (save-match-data
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
114 ;; Recurse on the sorted list.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
115 (let ((max-lisp-eval-depth (* 1024 1024))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
116 (completion-ignore-case nil))
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
117 (regexp-opt-group (sort (copy-sequence strings) 'string-lessp) paren nil non-shy))))
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
118
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
119 ;;;###autoload
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
120 (defun regexp-opt-depth (regexp &optional count-shy-groups-too)
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
121 "Return the depth of REGEXP.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
122 This means the number of regexp grouping constructs (parenthesised expressions)
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
123 in REGEXP, not counting the \"\\\\(?: \\\\)\" non-match-capturing groups unless
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
124 COUNT-SHY-GROUPS-TOO is non-nil.
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
125 See `regexp-opt'."
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
126 (save-match-data
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
127 ;; Hack to signal an error if REGEXP does not have balanced parentheses.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
128 (string-match regexp "")
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
129 ;; Count the number of open parentheses in REGEXP.
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
130 (let ((max (1- (length regexp)))
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
131 (count 0) start)
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
132 (while (string-match "\\\\(" regexp start)
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
133 (setq start (match-end 0))
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
134 (when (or count-shy-groups-too
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
135 (not (string= (substring regexp start (min (+ start 2) max)) "?:")))
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
136 (setq count (1+ count))))
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
137 count)))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
138
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
139 ;;; Workhorse functions.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
140
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
141 (eval-when-compile
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
142 (require 'cl))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
143
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
144 (unless (fboundp 'make-bool-vector)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
145 (defalias 'make-bool-vector 'make-vector))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
146
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
147 (defun regexp-opt-group (strings &optional paren lax non-shy)
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
148 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
149 ;; Return a regexp to match a string in STRINGS.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
150 ;; If PAREN non-nil, output regexp parentheses around returned regexp.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
151 ;; If LAX non-nil, don't output parentheses if it doesn't require them.
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
152 ;; If NON-SHY non-nil, don't use \\(?: \\) shy groups, use match capturing ones.
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
153 ;; Merges keywords to avoid backtracking in Emacs' regexp matcher.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
154 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
155 ;; The basic idea is to find the shortest common prefix, remove it and
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
156 ;; recurse. If there is no prefix, we divide the list into two so that (at
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
157 ;; least) one half will have at least a one-character common prefix.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
158 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
159 ;; Also we delay the addition of grouping parenthesis as long as possible
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
160 ;; until we're sure we need them, and try to remove one-character sequences
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
161 ;; so we can use character sets rather than grouping parenthesis.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
162 ;;
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
163 (let* ((open-group (cond
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
164 ((and paren non-shy) "\\(")
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
165 (paren "\\(?:")
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
166 (t "")))
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
167 (close-group (if paren "\\)" ""))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
168 (open-charset (if lax "" open-group))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
169 (close-charset (if lax "" close-group)))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
170 (cond
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
171 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
172 ;; If there is only one string, just return it.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
173 ((= (length strings) 1)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
174 (if (= (length (car strings)) 1)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
175 (concat open-charset (regexp-quote (car strings)) close-charset)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
176 (concat open-group (regexp-quote (car strings)) close-group)))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
177 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
178 ;; If there is an empty string, remove it and recurse on the rest.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
179 ((= (length (car strings)) 0)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
180 (concat open-charset
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
181 (regexp-opt-group (cdr strings) t t non-shy) "?"
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
182 close-charset))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
183 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
184 ;; If all are one-character strings, just return a character set.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
185 ((= (length strings) (apply '+ (mapcar 'length strings)))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
186 (concat open-charset
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
187 (regexp-opt-charset strings)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
188 close-charset))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
189 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
190 ;; We have a list of different length strings.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
191 (t
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
192 (let ((prefix (try-completion "" (mapcar 'list strings)))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
193 (letters (let ((completion-regexp-list '("^.$")))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
194 (all-completions "" (mapcar 'list strings)))))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
195 (cond
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
196 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
197 ;; If there is a common prefix, remove it and recurse on the suffixes.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
198 ((> (length prefix) 0)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
199 (let* ((length (length prefix))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
200 (suffixes (mapcar (lambda (s) (substring s length)) strings)))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
201 (concat open-group
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
202 (regexp-quote prefix) (regexp-opt-group suffixes t t non-shy)
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
203 close-group)))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
204 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
205 ;; If there are several one-character strings, remove them and recurse
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
206 ;; on the rest (first so the final regexp finds the longest match).
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
207 ((> (length letters) 1)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
208 (let ((rest (let ((completion-regexp-list '("^..+$")))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
209 (all-completions "" (mapcar 'list strings)))))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
210 (concat open-group
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
211 (regexp-opt-group rest nil nil non-shy) "\\|" (regexp-opt-charset letters)
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
212 close-group)))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
213 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
214 ;; Otherwise, divide the list into those that start with a particular
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
215 ;; letter and those that do not, and recurse on them.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
216 (t
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
217 (let* ((char (substring (car strings) 0 1))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
218 (half1 (all-completions char (mapcar 'list strings)))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
219 (half2 (nthcdr (length half1) strings)))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
220 (concat open-group
197
acd284d43ca1 Import from CVS: tag r20-3b25
cvs
parents: 189
diff changeset
221 (regexp-opt-group half1 nil nil non-shy) "\\|" (regexp-opt-group half2 nil nil non-shy)
189
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
222 close-group)))))))))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
223
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
224 (defun regexp-opt-charset (chars)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
225 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
226 ;; Return a regexp to match a character in CHARS.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
227 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
228 ;; The basic idea is to find character ranges. Also we take care in the
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
229 ;; position of character set meta characters in the character set regexp.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
230 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
231 (let* ((charwidth 256) ; Yeah, right.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
232 ;; XEmacs: use bit-vectors instead of bool-vectors
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
233 (charmap (make-bit-vector charwidth 0))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
234 (charset "")
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
235 (bracket "") (dash "") (caret ""))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
236 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
237 ;; Make a character map but extract character set meta characters.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
238 (dolist (char (mapcar 'string-to-char chars))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
239 (case char
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
240 (?\]
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
241 (setq bracket "]"))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
242 (?^
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
243 (setq caret "^"))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
244 (?-
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
245 (setq dash "-"))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
246 (otherwise
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
247 ;; XEmacs: 1
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
248 (aset charmap char 1))))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
249 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
250 ;; Make a character set from the map using ranges where applicable.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
251 (dotimes (char charwidth)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
252 (let ((start char))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
253 (while (and (< char charwidth)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
254 ;; XEmacs: (not (zerop ...))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
255 (not (zerop (aref charmap char))))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
256 (incf char))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
257 (cond ((> char (+ start 3))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
258 (setq charset (format "%s%c-%c" charset start (1- char))))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
259 ((> char start)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
260 (setq charset (format "%s%c" charset (setq char start)))))))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
261 ;;
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
262 ;; Make sure a caret is not first and a dash is first or last.
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
263 (if (and (string-equal charset "") (string-equal bracket ""))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
264 (concat "[" dash caret "]")
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
265 (concat "[" bracket charset caret dash "]"))))
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
266
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
267 (provide 'regexp-opt)
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
268
489f57a838ef Import from CVS: tag r20-3b21
cvs
parents:
diff changeset
269 ;;; regexp-opt.el ends here