annotate lisp/emacs-lisp/regexp-opt.el @ 111012:2b6e7f137773

Fix the MSDOS build broken by last revisions. msdos/sed1v2.inp: Use $(..) instead of ${..} in all edit commands. Needed because of changes in revno 101897. msdos/sed6.inp (mkinfodir): Edit to avoid Unix shell-isms. Needed because of changes in revno 101876. src/unexcoff.c (make_hdr): Fix prototype according to changes in revno 101757.
author Eli Zaretskii <eliz@gnu.org>
date Fri, 15 Oct 2010 20:45:12 +0200
parents 37b955157790
children 1e7d8f405703
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38412
253f761ad37b Some fixes to follow coding conventions in files maintained by FSF.
Pavel Janík <Pavel@Janik.cz>
parents: 33209
diff changeset
1 ;;; regexp-opt.el --- generate efficient regexps to match strings
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
74466
1d4b1a32fd66 Update copyright years.
Glenn Morris <rgm@gnu.org>
parents: 74081
diff changeset
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
106815
1d1d5d9bd884 Add 2010 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 100908
diff changeset
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5
25278
cbe304a26771 Fix maintainer address.
Karl Heuer <kwzh@gnu.org>
parents: 24810
diff changeset
6 ;; Author: Simon Marshall <simon@gnu.org>
27589
ab0d5f2bb751 Change maintainer.
Dave Love <fx@gnu.org>
parents: 25938
diff changeset
7 ;; Maintainer: FSF
28420
854590466b7b Add `extensions' to Keywords.
Dave Love <fx@gnu.org>
parents: 28067
diff changeset
8 ;; Keywords: strings, regexps, extensions
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10 ;; This file is part of GNU Emacs.
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11
94655
90a2847062be Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 93920
diff changeset
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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: 93920
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: 93920
diff changeset
15 ;; (at your option) any later version.
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; GNU Emacs is distributed in the hope that it will be useful,
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20 ;; GNU General Public License for more details.
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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: 93920
diff changeset
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25 ;;; Commentary:
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26
25938
6f591e2d9c0d (regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 25278
diff changeset
27 ;; The "opt" in "regexp-opt" stands for "optim\\(al\\|i[sz]e\\)".
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 ;;
18149
2fec7f622b82 emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents: 18014
diff changeset
29 ;; 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
30 ;; one of those strings) so that the regexp generated by:
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 ;;
18149
2fec7f622b82 emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents: 18014
diff changeset
32 ;; (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
33 ;;
2fec7f622b82 emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents: 18014
diff changeset
34 ;; 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
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 ;; (mapconcat 'regexp-quote strings "\\|")
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38 ;; For example:
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 ;; (let ((strings '("cond" "if" "when" "unless" "while"
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41 ;; "let" "let*" "progn" "prog1" "prog2"
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 ;; "save-restriction" "save-excursion" "save-window-excursion"
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 ;; "save-current-buffer" "save-match-data"
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 ;; "catch" "throw" "unwind-protect" "condition-case")))
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 ;; (concat "(" (regexp-opt strings t) "\\>"))
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 ;; => "(\\(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\\)\\)\\>"
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 ;;
18149
2fec7f622b82 emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents: 18014
diff changeset
48 ;; 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
49 ;; 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
50
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 ;; Since this package was written to produce efficient regexps, not regexps
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 ;; efficiently, it is probably not a good idea to in-line too many calls in
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 ;; your code, unless you use the following trick with `eval-when-compile':
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 ;; (defvar definition-regexp
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 ;; (eval-when-compile
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 ;; (concat "^("
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 ;; (regexp-opt '("defun" "defsubst" "defmacro" "defalias"
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 ;; "defvar" "defconst") t)
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 ;; "\\>")))
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 ;; The `byte-compile' code will be as if you had defined the variable thus:
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 ;; (defvar definition-regexp
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 ;; "^(\\(def\\(alias\\|const\\|macro\\|subst\\|un\\|var\\)\\)\\>")
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 ;;
18149
2fec7f622b82 emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents: 18014
diff changeset
67 ;; 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
68 ;; `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
69 ;; 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
70 ;; 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
71 ;; 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
72 ;; 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
73
2fec7f622b82 emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents: 18014
diff changeset
74 ;; 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
75 ;; 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
76 ;; Stefan Monnier.
6f591e2d9c0d (regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 25278
diff changeset
77 ;; 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
78 ;; 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
79 ;;
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
80 ;; 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
81 ;; 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
82 ;; 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
83 ;; contortions, I'm all ears.
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
85 ;;; Code:
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 ;;;###autoload
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 (defun regexp-opt (strings &optional paren)
74081
9faf984a83f8 (regexp-opt): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents: 68648
diff changeset
89 "Return a regexp to match a string in the list STRINGS.
19782
013cea3890cb (regexp-opt): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 18465
diff changeset
90 Each string should be unique in STRINGS and should not contain any regexps,
013cea3890cb (regexp-opt): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 18465
diff changeset
91 quoted or not. If optional PAREN is non-nil, ensure that the returned regexp
013cea3890cb (regexp-opt): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 18465
diff changeset
92 is enclosed by at least one regexp grouping construct.
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93 The returned regexp is typically more efficient than the equivalent regexp:
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94
32304
e8fca08bb4cc (regexp-opt): Add \< and \> if PAREN=`words'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32041
diff changeset
95 (let ((open (if PAREN \"\\\\(\" \"\")) (close (if PAREN \"\\\\)\" \"\")))
e8fca08bb4cc (regexp-opt): Add \< and \> if PAREN=`words'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32041
diff changeset
96 (concat open (mapconcat 'regexp-quote STRINGS \"\\\\|\") close))
e8fca08bb4cc (regexp-opt): Add \< and \> if PAREN=`words'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32041
diff changeset
97
e8fca08bb4cc (regexp-opt): Add \< and \> if PAREN=`words'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32041
diff changeset
98 If PAREN is `words', then the resulting regexp is additionally surrounded
110801
37b955157790 (regexp-opt): Add `symbols' mode.
Miles Bader <miles@gnu.org>
parents: 110305
diff changeset
99 by \\=\\< and \\>.
37b955157790 (regexp-opt): Add `symbols' mode.
Miles Bader <miles@gnu.org>
parents: 110305
diff changeset
100 If PAREN is `symbols', then the resulting regexp is additionally surrounded
37b955157790 (regexp-opt): Add `symbols' mode.
Miles Bader <miles@gnu.org>
parents: 110305
diff changeset
101 by \\=\\_< and \\_>."
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 (save-match-data
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 ;; Recurse on the sorted list.
93920
22cfb604a455 (regexp-opt): Reduce max-lisp-eval-depth and max-specpdl-size to
Chong Yidong <cyd@stupidchicken.com>
parents: 93892
diff changeset
104 (let* ((max-lisp-eval-depth 10000)
22cfb604a455 (regexp-opt): Reduce max-lisp-eval-depth and max-specpdl-size to
Chong Yidong <cyd@stupidchicken.com>
parents: 93892
diff changeset
105 (max-specpdl-size 10000)
32304
e8fca08bb4cc (regexp-opt): Add \< and \> if PAREN=`words'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32041
diff changeset
106 (completion-ignore-case nil)
41754
e78fbcf9b878 (regexp-opt): Bind completion-regexp-list to nil.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 41617
diff changeset
107 (completion-regexp-list nil)
32304
e8fca08bb4cc (regexp-opt): Add \< and \> if PAREN=`words'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32041
diff changeset
108 (open (cond ((stringp paren) paren) (paren "\\(")))
58802
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
109 (sorted-strings (delete-dups
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
110 (sort (copy-sequence strings) 'string-lessp)))
95267
4ec6f3d46f27 (regexp-opt): Always return a properly-grouped regexp.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 94655
diff changeset
111 (re (regexp-opt-group sorted-strings (or open t) (not open))))
110801
37b955157790 (regexp-opt): Add `symbols' mode.
Miles Bader <miles@gnu.org>
parents: 110305
diff changeset
112 (cond ((eq paren 'words)
37b955157790 (regexp-opt): Add `symbols' mode.
Miles Bader <miles@gnu.org>
parents: 110305
diff changeset
113 (concat "\\<" re "\\>"))
37b955157790 (regexp-opt): Add `symbols' mode.
Miles Bader <miles@gnu.org>
parents: 110305
diff changeset
114 ((eq paren 'symbols)
37b955157790 (regexp-opt): Add `symbols' mode.
Miles Bader <miles@gnu.org>
parents: 110305
diff changeset
115 (concat "\\_<" re "\\_>"))
37b955157790 (regexp-opt): Add `symbols' mode.
Miles Bader <miles@gnu.org>
parents: 110305
diff changeset
116 (t re)))))
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 ;;;###autoload
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 (defun regexp-opt-depth (regexp)
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 "Return the depth of REGEXP.
58802
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
121 This means the number of non-shy regexp grouping constructs
63512
8d59a5d179f2 (regexp-opt-depth): Fix spelling in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 58802
diff changeset
122 \(parenthesized expressions) in REGEXP."
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 (save-match-data
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 ;; Hack to signal an error if REGEXP does not have balanced parentheses.
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 (string-match regexp "")
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 ;; Count the number of open parentheses in REGEXP.
58802
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
127 (let ((count 0) start last)
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 106815
diff changeset
128 (while (string-match "\\\\(\\(\\?[0-9]*:\\)?" regexp start)
58802
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
129 (setq start (match-end 0)) ; Start of next search.
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
130 (when (and (not (match-beginning 1))
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
131 (subregexp-context-p regexp (match-beginning 0) last))
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
132 ;; It's not a shy group and it's not inside brackets or after
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
133 ;; a backslash: it's really a group-open marker.
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
134 (setq last start) ; Speed up next regexp-opt-re-context-p.
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
135 (setq count (1+ count))))
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 count)))
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 ;;; Workhorse functions.
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 (eval-when-compile
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 (require 'cl))
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 (defun regexp-opt-group (strings &optional paren lax)
49352
8bfc6a0f6b3e (regexp-opt-group): Undo last change. Fix the docstring instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49335
diff changeset
144 ;; Return a regexp to match a string in the sorted list STRINGS.
8bfc6a0f6b3e (regexp-opt-group): Undo last change. Fix the docstring instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49335
diff changeset
145 ;; If PAREN non-nil, output regexp parentheses around returned regexp.
8bfc6a0f6b3e (regexp-opt-group): Undo last change. Fix the docstring instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49335
diff changeset
146 ;; If LAX non-nil, don't output parentheses if it doesn't require them.
8bfc6a0f6b3e (regexp-opt-group): Undo last change. Fix the docstring instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49335
diff changeset
147 ;; Merges keywords to avoid backtracking in Emacs' regexp matcher.
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
148
49352
8bfc6a0f6b3e (regexp-opt-group): Undo last change. Fix the docstring instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49335
diff changeset
149 ;; The basic idea is to find the shortest common prefix or suffix, remove it
8bfc6a0f6b3e (regexp-opt-group): Undo last change. Fix the docstring instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49335
diff changeset
150 ;; and recurse. If there is no prefix, we divide the list into two so that
8bfc6a0f6b3e (regexp-opt-group): Undo last change. Fix the docstring instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49335
diff changeset
151 ;; \(at least) one half will have at least a one-character common prefix.
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
152
49352
8bfc6a0f6b3e (regexp-opt-group): Undo last change. Fix the docstring instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49335
diff changeset
153 ;; Also we delay the addition of grouping parenthesis as long as possible
8bfc6a0f6b3e (regexp-opt-group): Undo last change. Fix the docstring instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49335
diff changeset
154 ;; until we're sure we need them, and try to remove one-character sequences
8bfc6a0f6b3e (regexp-opt-group): Undo last change. Fix the docstring instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49335
diff changeset
155 ;; so we can use character sets rather than grouping parenthesis.
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
156 (let* ((open-group (cond ((stringp paren) paren) (paren "\\(?:") (t "")))
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 (close-group (if paren "\\)" ""))
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 (open-charset (if lax "" open-group))
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
159 (close-charset (if lax "" close-group)))
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 (cond
25938
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 ;; 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
163 ((= (length strings) 0)
6f591e2d9c0d (regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 25278
diff changeset
164 "")
6f591e2d9c0d (regexp-opt-try-suffix): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 25278
diff changeset
165 ;;
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 ;; If there is only one string, just return it.
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 ((= (length strings) 1)
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 (if (= (length (car strings)) 1)
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 (concat open-charset (regexp-quote (car strings)) close-charset)
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 (concat open-group (regexp-quote (car strings)) close-group)))
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 ;; If there is an empty string, remove it and recurse on the rest.
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 ((= (length (car strings)) 0)
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 (concat open-charset
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 (regexp-opt-group (cdr strings) t t) "?"
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 close-charset))
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 ;;
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
178 ;; 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
179 ((and (= (length (car strings)) 1)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
180 (let ((strs (cdr strings)))
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
181 (while (and strs (/= (length (car strs)) 1))
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
182 (pop strs))
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
183 strs))
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
184 (let (letters rest)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
185 ;; Collect one-char strings
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
186 (dolist (s strings)
30719
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
187 (if (= (length s) 1) (push (string-to-char s) letters) (push s rest)))
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
188
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
189 (if rest
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
190 ;; several one-char strings: take them and recurse
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
191 ;; 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
192 (concat open-group
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
193 (regexp-opt-group (nreverse rest))
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
194 "\\|" (regexp-opt-charset letters)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
195 close-group)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
196 ;; 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
197 (concat open-charset
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
198 (regexp-opt-charset letters)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
199 close-charset))))
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 ;; We have a list of different length strings.
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 (t
45905
20781c152651 (regexp-opt-group): Don't cons uselessly.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42625
diff changeset
203 (let ((prefix (try-completion "" strings)))
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
204 (if (> (length prefix) 0)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
205 ;; 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
206 (let* ((n (length prefix))
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
207 (suffixes (mapcar (lambda (s) (substring s n)) strings)))
32041
a055173cadf8 (regexp-opt-group): Put more parenthesis.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30719
diff changeset
208 (concat open-group
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
209 (regexp-quote prefix)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
210 (regexp-opt-group suffixes t t)
32041
a055173cadf8 (regexp-opt-group): Put more parenthesis.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30719
diff changeset
211 close-group))
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
212
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
213 (let* ((sgnirts (mapcar (lambda (s)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
214 (concat (nreverse (string-to-list s))))
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
215 strings))
45905
20781c152651 (regexp-opt-group): Don't cons uselessly.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42625
diff changeset
216 (xiffus (try-completion "" sgnirts)))
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
217 (if (> (length xiffus) 0)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
218 ;; 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
219 (let* ((n (- (length xiffus)))
33209
f94f82069336 (regexp-opt-group): Sort the strings when extracting a suffix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32304
diff changeset
220 (prefixes
f94f82069336 (regexp-opt-group): Sort the strings when extracting a suffix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32304
diff changeset
221 ;; Sorting is necessary in cases such as ("ad" "d").
f94f82069336 (regexp-opt-group): Sort the strings when extracting a suffix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32304
diff changeset
222 (sort (mapcar (lambda (s) (substring s 0 n)) strings)
f94f82069336 (regexp-opt-group): Sort the strings when extracting a suffix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 32304
diff changeset
223 'string-lessp)))
32041
a055173cadf8 (regexp-opt-group): Put more parenthesis.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30719
diff changeset
224 (concat open-group
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
225 (regexp-opt-group prefixes t t)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
226 (regexp-quote
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
227 (concat (nreverse (string-to-list xiffus))))
32041
a055173cadf8 (regexp-opt-group): Put more parenthesis.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30719
diff changeset
228 close-group))
49598
0d8b17d428b5 Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49352
diff changeset
229
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
230 ;; 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
231 ;; particular letter and those that do not, and recurse on them.
93892
877d364e5ff6 (regexp-opt-group): Use substring-no-properties for correct handling
Chong Yidong <cyd@stupidchicken.com>
parents: 91327
diff changeset
232 (let* ((char (substring-no-properties (car strings) 0 1))
45905
20781c152651 (regexp-opt-group): Don't cons uselessly.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42625
diff changeset
233 (half1 (all-completions char strings))
49352
8bfc6a0f6b3e (regexp-opt-group): Undo last change. Fix the docstring instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49335
diff changeset
234 (half2 (nthcdr (length half1) strings)))
28067
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
235 (concat open-group
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
236 (regexp-opt-group half1)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
237 "\\|" (regexp-opt-group half2)
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
238 close-group))))))))))
e09db52da018 Update copyright and leading comment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 27589
diff changeset
239
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 (defun regexp-opt-charset (chars)
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 ;; Return a regexp to match a character in CHARS.
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 ;; The basic idea is to find character ranges. Also we take care in the
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 ;; position of character set meta characters in the character set regexp.
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247 ;;
30719
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
248 (let* ((charmap (make-char-table 'case-table))
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
249 (start -1) (end -2)
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 (charset "")
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 (bracket "") (dash "") (caret ""))
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 ;; Make a character map but extract character set meta characters.
30719
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
254 (dolist (char chars)
18149
2fec7f622b82 emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents: 18014
diff changeset
255 (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
256 (?\]
2fec7f622b82 emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents: 18014
diff changeset
257 (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
258 (?^
2fec7f622b82 emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents: 18014
diff changeset
259 (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
260 (?-
2fec7f622b82 emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents: 18014
diff changeset
261 (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
262 (otherwise
2fec7f622b82 emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents: 18014
diff changeset
263 (aset charmap char t))))
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 ;; Make a character set from the map using ranges where applicable.
30719
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
266 (map-char-table
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
267 (lambda (c v)
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
268 (when v
88401
8eba780a3a36 (regexp-opt-charset): Adjusted for the
Kenichi Handa <handa@m17n.org>
parents: 42625
diff changeset
269 (if (consp c)
8eba780a3a36 (regexp-opt-charset): Adjusted for the
Kenichi Handa <handa@m17n.org>
parents: 42625
diff changeset
270 (if (= (1- (car c)) end) (setq end (cdr c))
8eba780a3a36 (regexp-opt-charset): Adjusted for the
Kenichi Handa <handa@m17n.org>
parents: 42625
diff changeset
271 (if (> end (+ start 2))
8eba780a3a36 (regexp-opt-charset): Adjusted for the
Kenichi Handa <handa@m17n.org>
parents: 42625
diff changeset
272 (setq charset (format "%s%c-%c" charset start end))
8eba780a3a36 (regexp-opt-charset): Adjusted for the
Kenichi Handa <handa@m17n.org>
parents: 42625
diff changeset
273 (while (>= end start)
8eba780a3a36 (regexp-opt-charset): Adjusted for the
Kenichi Handa <handa@m17n.org>
parents: 42625
diff changeset
274 (setq charset (format "%s%c" charset start))
8eba780a3a36 (regexp-opt-charset): Adjusted for the
Kenichi Handa <handa@m17n.org>
parents: 42625
diff changeset
275 (incf start)))
8eba780a3a36 (regexp-opt-charset): Adjusted for the
Kenichi Handa <handa@m17n.org>
parents: 42625
diff changeset
276 (setq start (car c) end (cdr c)))
8eba780a3a36 (regexp-opt-charset): Adjusted for the
Kenichi Handa <handa@m17n.org>
parents: 42625
diff changeset
277 (if (= (1- c) end) (setq end c)
8eba780a3a36 (regexp-opt-charset): Adjusted for the
Kenichi Handa <handa@m17n.org>
parents: 42625
diff changeset
278 (if (> end (+ start 2))
30719
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
279 (setq charset (format "%s%c-%c" charset start end))
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
280 (while (>= end start)
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
281 (setq charset (format "%s%c" charset start))
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
282 (incf start)))
88401
8eba780a3a36 (regexp-opt-charset): Adjusted for the
Kenichi Handa <handa@m17n.org>
parents: 42625
diff changeset
283 (setq start c end c)))))
30719
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
284 charmap)
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
285 (when (>= end start)
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
286 (if (> end (+ start 2))
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
287 (setq charset (format "%s%c-%c" charset start end))
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
288 (while (>= end start)
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
289 (setq charset (format "%s%c" charset start))
fd7db1cf7adf (make-bool-vector): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 28864
diff changeset
290 (incf start))))
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 ;;
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292 ;; Make sure a caret is not first and a dash is first or last.
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 (if (and (string-equal charset "") (string-equal bracket ""))
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 (concat "[" dash caret "]")
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295 (concat "[" bracket charset caret dash "]"))))
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
297 (provide 'regexp-opt)
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298
58802
da163770bb89 (regexp-opt-depth): Use subregexp-context-p.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 52401
diff changeset
299 ;; arch-tag: 6c5a66f4-29af-4fd6-8c3b-4b554d5b4370
18014
9492b5c8485a Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 ;;; regexp-opt.el ends here