2016-10-31 20:31:22 -04:00
|
|
|
|
;;; regexp-opt.el --- generate efficient regexps to match strings -*- lexical-binding: t -*-
|
1997-05-29 03:01:51 +00:00
|
|
|
|
|
2022-01-01 02:45:51 -05:00
|
|
|
|
;; Copyright (C) 1994-2022 Free Software Foundation, Inc.
|
1997-05-29 03:01:51 +00:00
|
|
|
|
|
1999-08-16 04:04:27 +00:00
|
|
|
|
;; Author: Simon Marshall <simon@gnu.org>
|
2019-05-25 13:43:06 -07:00
|
|
|
|
;; Maintainer: emacs-devel@gnu.org
|
2000-03-30 11:00:35 +00:00
|
|
|
|
;; Keywords: strings, regexps, extensions
|
1997-05-29 03:01:51 +00:00
|
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
2008-05-06 03:21:21 +00:00
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
1997-05-29 03:01:51 +00:00
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 03:21:21 +00:00
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
1997-05-29 03:01:51 +00:00
|
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 15:52:52 -07:00
|
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
1997-05-29 03:01:51 +00:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
1999-10-08 23:06:15 +00:00
|
|
|
|
;; The "opt" in "regexp-opt" stands for "optim\\(al\\|i[sz]e\\)".
|
1997-05-29 03:01:51 +00:00
|
|
|
|
;;
|
1997-06-06 07:10:24 +00:00
|
|
|
|
;; This package generates a regexp from a given list of strings (which matches
|
|
|
|
|
;; one of those strings) so that the regexp generated by:
|
1997-05-29 03:01:51 +00:00
|
|
|
|
;;
|
1997-06-06 07:10:24 +00:00
|
|
|
|
;; (regexp-opt strings)
|
|
|
|
|
;;
|
|
|
|
|
;; is equivalent to, but more efficient than, the regexp generated by:
|
|
|
|
|
;;
|
|
|
|
|
;; (mapconcat 'regexp-quote strings "\\|")
|
1997-05-29 03:01:51 +00:00
|
|
|
|
;;
|
|
|
|
|
;; For example:
|
|
|
|
|
;;
|
|
|
|
|
;; (let ((strings '("cond" "if" "when" "unless" "while"
|
|
|
|
|
;; "let" "let*" "progn" "prog1" "prog2"
|
|
|
|
|
;; "save-restriction" "save-excursion" "save-window-excursion"
|
|
|
|
|
;; "save-current-buffer" "save-match-data"
|
|
|
|
|
;; "catch" "throw" "unwind-protect" "condition-case")))
|
|
|
|
|
;; (concat "(" (regexp-opt strings t) "\\>"))
|
|
|
|
|
;; => "(\\(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\\)\\)\\>"
|
|
|
|
|
;;
|
1997-06-06 07:10:24 +00:00
|
|
|
|
;; Searching using the above example `regexp-opt' regexp takes approximately
|
|
|
|
|
;; two-thirds of the time taken using the equivalent `mapconcat' regexp.
|
|
|
|
|
|
1997-05-29 03:01:51 +00:00
|
|
|
|
;; Since this package was written to produce efficient regexps, not regexps
|
|
|
|
|
;; efficiently, it is probably not a good idea to in-line too many calls in
|
|
|
|
|
;; your code, unless you use the following trick with `eval-when-compile':
|
|
|
|
|
;;
|
|
|
|
|
;; (defvar definition-regexp
|
|
|
|
|
;; (eval-when-compile
|
|
|
|
|
;; (concat "^("
|
|
|
|
|
;; (regexp-opt '("defun" "defsubst" "defmacro" "defalias"
|
|
|
|
|
;; "defvar" "defconst") t)
|
|
|
|
|
;; "\\>")))
|
|
|
|
|
;;
|
|
|
|
|
;; The `byte-compile' code will be as if you had defined the variable thus:
|
|
|
|
|
;;
|
|
|
|
|
;; (defvar definition-regexp
|
|
|
|
|
;; "^(\\(def\\(alias\\|const\\|macro\\|subst\\|un\\|var\\)\\)\\>")
|
|
|
|
|
;;
|
1997-06-06 07:10:24 +00:00
|
|
|
|
;; Note that if you use this trick for all instances of `regexp-opt' and
|
|
|
|
|
;; `regexp-opt-depth' in your code, regexp-opt.el would only have to be loaded
|
|
|
|
|
;; at compile time. But note also that using this trick means that should
|
|
|
|
|
;; regexp-opt.el be changed, perhaps to fix a bug or to add a feature to
|
|
|
|
|
;; improve the efficiency of `regexp-opt' regexps, you would have to recompile
|
|
|
|
|
;; your code for such changes to have effect in your code.
|
|
|
|
|
|
|
|
|
|
;; Originally written for font-lock.el, from an idea from Stig's hl319.el, with
|
1999-10-08 23:06:15 +00:00
|
|
|
|
;; thanks for ideas also to Michael Ernst, Bob Glickstein, Dan Nicolaescu and
|
|
|
|
|
;; Stefan Monnier.
|
|
|
|
|
;; No doubt `regexp-opt' doesn't always produce optimal regexps, so code, ideas
|
|
|
|
|
;; or any other information to improve things are welcome.
|
2000-03-09 00:45:15 +00:00
|
|
|
|
;;
|
|
|
|
|
;; One possible improvement would be to compile '("aa" "ab" "ba" "bb")
|
|
|
|
|
;; into "[ab][ab]" rather than "a[ab]\\|b[ab]". I'm not sure it's worth
|
|
|
|
|
;; it but if someone knows how to do it without going through too many
|
|
|
|
|
;; contortions, I'm all ears.
|
1997-05-29 03:01:51 +00:00
|
|
|
|
|
2000-03-09 00:45:15 +00:00
|
|
|
|
;;; Code:
|
1997-05-29 03:01:51 +00:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
2020-02-13 20:06:48 +01:00
|
|
|
|
(defun regexp-opt (strings &optional paren)
|
2006-11-19 17:49:47 +00:00
|
|
|
|
"Return a regexp to match a string in the list STRINGS.
|
2019-03-27 11:36:13 -07:00
|
|
|
|
Each member of STRINGS is treated as a fixed string, not as a regexp.
|
|
|
|
|
Optional PAREN specifies how the returned regexp is surrounded by
|
|
|
|
|
grouping constructs.
|
2016-02-07 12:46:37 +03:00
|
|
|
|
|
2019-02-25 15:22:02 +01:00
|
|
|
|
If STRINGS is the empty list, the return value is a regexp that
|
|
|
|
|
never matches anything.
|
|
|
|
|
|
2016-02-07 12:46:37 +03:00
|
|
|
|
The optional argument PAREN can be any of the following:
|
|
|
|
|
|
|
|
|
|
a string
|
|
|
|
|
the resulting regexp is preceded by PAREN and followed by
|
|
|
|
|
\\), e.g. use \"\\\\(?1:\" to produce an explicitly numbered
|
|
|
|
|
group.
|
|
|
|
|
|
|
|
|
|
`words'
|
|
|
|
|
the resulting regexp is surrounded by \\=\\<\\( and \\)\\>.
|
|
|
|
|
|
|
|
|
|
`symbols'
|
|
|
|
|
the resulting regexp is surrounded by \\_<\\( and \\)\\_>.
|
|
|
|
|
|
|
|
|
|
non-nil
|
|
|
|
|
the resulting regexp is surrounded by \\( and \\).
|
|
|
|
|
|
|
|
|
|
nil
|
|
|
|
|
the resulting regexp is surrounded by \\(?: and \\), if it is
|
|
|
|
|
necessary to ensure that a postfix operator appended to it will
|
|
|
|
|
apply to the whole expression.
|
|
|
|
|
|
2020-02-13 20:06:48 +01:00
|
|
|
|
The returned regexp is ordered in such a way that it will always
|
|
|
|
|
match the longest string possible.
|
2019-02-24 22:12:52 +01:00
|
|
|
|
|
|
|
|
|
Up to reordering, the resulting regexp is equivalent to but
|
|
|
|
|
usually more efficient than that of a simplified version:
|
2016-02-07 12:46:37 +03:00
|
|
|
|
|
|
|
|
|
(defun simplified-regexp-opt (strings &optional paren)
|
|
|
|
|
(let ((parens
|
|
|
|
|
(cond ((stringp paren) (cons paren \"\\\\)\"))
|
2018-06-24 10:57:12 -04:00
|
|
|
|
((eq paren \\='words) \\='(\"\\\\\\=<\\\\(\" . \"\\\\)\\\\>\"))
|
|
|
|
|
((eq paren \\='symbols) \\='(\"\\\\_<\\\\(\" . \"\\\\)\\\\_>\"))
|
|
|
|
|
((null paren) \\='(\"\\\\(?:\" . \"\\\\)\"))
|
|
|
|
|
(t \\='(\"\\\\(\" . \"\\\\)\")))))
|
2019-02-22 10:12:14 +02:00
|
|
|
|
(concat (car parens)
|
2018-06-24 10:57:12 -04:00
|
|
|
|
(mapconcat \\='regexp-quote strings \"\\\\|\")
|
2019-02-22 10:12:14 +02:00
|
|
|
|
(cdr parens))))"
|
1997-05-29 03:01:51 +00:00
|
|
|
|
(save-match-data
|
|
|
|
|
;; Recurse on the sorted list.
|
2008-04-09 15:38:32 +00:00
|
|
|
|
(let* ((max-lisp-eval-depth 10000)
|
2000-10-08 18:50:43 +00:00
|
|
|
|
(completion-ignore-case nil)
|
2001-12-01 22:04:12 +00:00
|
|
|
|
(completion-regexp-list nil)
|
2000-10-08 18:50:43 +00:00
|
|
|
|
(open (cond ((stringp paren) paren) (paren "\\(")))
|
2020-02-13 20:06:48 +01:00
|
|
|
|
(re (if strings
|
|
|
|
|
(regexp-opt-group
|
|
|
|
|
(delete-dups (sort (copy-sequence strings) 'string-lessp))
|
|
|
|
|
(or open t) (not open))
|
|
|
|
|
;; No strings: return an unmatchable regexp.
|
|
|
|
|
(concat (or open "\\(?:") regexp-unmatchable "\\)"))))
|
2010-10-07 16:24:21 +09:00
|
|
|
|
(cond ((eq paren 'words)
|
|
|
|
|
(concat "\\<" re "\\>"))
|
|
|
|
|
((eq paren 'symbols)
|
|
|
|
|
(concat "\\_<" re "\\_>"))
|
|
|
|
|
(t re)))))
|
1997-05-29 03:01:51 +00:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun regexp-opt-depth (regexp)
|
|
|
|
|
"Return the depth of REGEXP.
|
2004-12-06 15:12:46 +00:00
|
|
|
|
This means the number of non-shy regexp grouping constructs
|
2005-06-16 16:14:56 +00:00
|
|
|
|
\(parenthesized expressions) in REGEXP."
|
1997-05-29 03:01:51 +00:00
|
|
|
|
(save-match-data
|
|
|
|
|
;; Hack to signal an error if REGEXP does not have balanced parentheses.
|
|
|
|
|
(string-match regexp "")
|
|
|
|
|
;; Count the number of open parentheses in REGEXP.
|
2004-12-06 15:12:46 +00:00
|
|
|
|
(let ((count 0) start last)
|
New syntax-propertize functionality.
* lisp/font-lock.el (font-lock-syntactic-keywords): Make obsolete.
(font-lock-fontify-syntactic-keywords-region): Move handling of
font-lock-syntactically-fontified to...
(font-lock-default-fontify-region): ...here.
Let syntax-propertize-function take precedence.
(font-lock-fontify-syntactically-region): Cal syntax-propertize.
* lisp/emacs-lisp/regexp-opt.el (regexp-opt-depth): Skip named groups.
* lisp/emacs-lisp/syntax.el (syntax-propertize-function)
(syntax-propertize-chunk-size, syntax-propertize--done)
(syntax-propertize-extend-region-functions): New vars.
(syntax-propertize-wholelines, syntax-propertize-multiline)
(syntax-propertize--shift-groups, syntax-propertize-via-font-lock)
(syntax-propertize): New functions.
(syntax-propertize-rules): New macro.
(syntax-ppss-flush-cache): Set syntax-propertize--done.
(syntax-ppss): Call syntax-propertize.
* lisp/progmodes/ada-mode.el (ada-set-syntax-table-properties)
(ada-after-change-function, ada-initialize-syntax-table-properties)
(ada-handle-syntax-table-properties): Only define when
syntax-propertize is not available.
(ada-mode): Use syntax-propertize-function.
* lisp/progmodes/autoconf.el (autoconf-mode):
Use syntax-propertize-function.
(autoconf-font-lock-syntactic-keywords): Remove.
* lisp/progmodes/cfengine.el (cfengine-mode):
Use syntax-propertize-function.
(cfengine-font-lock-syntactic-keywords): Remove.
* lisp/progmodes/cperl-mode.el (cperl-mode): Use syntax-propertize-function.
* lisp/progmodes/fortran.el (fortran-mode): Use syntax-propertize-function.
(fortran--font-lock-syntactic-keywords): New var.
(fortran-line-length): Update syntax-propertize-function and
fortran--font-lock-syntactic-keywords.
* lisp/progmodes/gud.el (gdb-script-syntax-propertize-function): New var;
replaces gdb-script-font-lock-syntactic-keywords.
(gdb-script-mode): Use it.
* lisp/progmodes/js.el (js--regexp-literal): Define while compiling.
(js-syntax-propertize-function): New var; replaces
js-font-lock-syntactic-keywords.
(js-mode): Use it.
* lisp/progmodes/make-mode.el (makefile-syntax-propertize-function):
New var; replaces makefile-font-lock-syntactic-keywords.
(makefile-mode): Use it.
(makefile-imake-mode): Adjust.
* lisp/progmodes/mixal-mode.el (mixal-syntax-propertize-function): New var;
replaces mixal-font-lock-syntactic-keywords.
(mixal-mode): Use it.
* lisp/progmodes/octave-mod.el (octave-syntax-propertize-sqs): New function
to replace octave-font-lock-close-quotes.
(octave-syntax-propertize-function): New function to replace
octave-font-lock-syntactic-keywords.
(octave-mode): Use it.
* lisp/progmodes/perl-mode.el (perl-syntax-propertize-function): New fun to
replace perl-font-lock-syntactic-keywords.
(perl-syntax-propertize-special-constructs): New fun to replace
perl-font-lock-special-syntactic-constructs.
(perl-font-lock-syntactic-face-function): New fun.
(perl-mode): Use it.
* lisp/progmodes/python.el (python-syntax-propertize-function): New var to
replace python-font-lock-syntactic-keywords.
(python-mode): Use it.
(python-quote-syntax): Simplify and adjust to new use.
* lisp/progmodes/ruby-mode.el (ruby-here-doc-beg-re):
Define while compiling.
(ruby-here-doc-end-re, ruby-here-doc-beg-match)
(ruby-font-lock-syntactic-keywords, ruby-comment-beg-syntax)
(syntax-ppss, ruby-in-ppss-context-p, ruby-in-here-doc-p)
(ruby-here-doc-find-end, ruby-here-doc-beg-syntax)
(ruby-here-doc-end-syntax): Only define when
syntax-propertize is not available.
(ruby-syntax-propertize-function, ruby-syntax-propertize-heredoc):
New functions.
(ruby-in-ppss-context-p): Update to new syntax of heredocs.
(electric-indent-chars): Silence bytecompiler.
(ruby-mode): Use prog-mode, syntax-propertize-function, and
electric-indent-chars.
* lisp/progmodes/sh-script.el (sh-st-symbol): Remove.
(sh-font-lock-close-heredoc, sh-font-lock-open-heredoc): Add eol arg.
(sh-font-lock-flush-syntax-ppss-cache, sh-font-lock-here-doc): Remove.
(sh-font-lock-quoted-subshell): Assume we've already matched $(.
(sh-font-lock-paren): Set syntax-multiline.
(sh-font-lock-syntactic-keywords): Remove.
(sh-syntax-propertize-function): New function to replace it.
(sh-mode): Use it.
* lisp/progmodes/simula.el (simula-syntax-propertize-function): New var to
replace simula-font-lock-syntactic-keywords.
(simula-mode): Use it.
* lisp/progmodes/tcl.el (tcl-syntax-propertize-function): New var to
replace tcl-font-lock-syntactic-keywords.
(tcl-mode): Use it.
* lisp/progmodes/vhdl-mode.el (vhdl-mode): Use syntax-propertize-function
if available.
(vhdl-fontify-buffer): Adjust.
* lisp/textmodes/bibtex.el (bibtex-mode): Use syntax-propertize-function.
* lisp/textmodes/reftex.el (font-lock-syntactic-keywords): Don't declare
since we don't use it.
* lisp/textmodes/sgml-mode.el (sgml-syntax-propertize-function): New var to
replace sgml-font-lock-syntactic-keywords.
(sgml-mode): Use it.
* lisp/textmodes/tex-mode.el (tex-common-initialization, doctex-mode):
Use syntax-propertize-function.
* lisp/textmodes/texinfo.el (texinfo-syntax-propertize-function): New fun
to replace texinfo-font-lock-syntactic-keywords.
(texinfo-mode): Use it.
* test/indent/octave.m: Remove some `fixindent' not needed any more.
2010-09-11 01:13:42 +02:00
|
|
|
|
(while (string-match "\\\\(\\(\\?[0-9]*:\\)?" regexp start)
|
2004-12-06 15:12:46 +00:00
|
|
|
|
(setq start (match-end 0)) ; Start of next search.
|
|
|
|
|
(when (and (not (match-beginning 1))
|
|
|
|
|
(subregexp-context-p regexp (match-beginning 0) last))
|
|
|
|
|
;; It's not a shy group and it's not inside brackets or after
|
|
|
|
|
;; a backslash: it's really a group-open marker.
|
|
|
|
|
(setq last start) ; Speed up next regexp-opt-re-context-p.
|
|
|
|
|
(setq count (1+ count))))
|
1997-05-29 03:01:51 +00:00
|
|
|
|
count)))
|
|
|
|
|
|
|
|
|
|
;;; Workhorse functions.
|
|
|
|
|
|
|
|
|
|
(defun regexp-opt-group (strings &optional paren lax)
|
2010-10-17 14:59:58 -04:00
|
|
|
|
"Return a regexp to match a string in the sorted list STRINGS.
|
|
|
|
|
If PAREN non-nil, output regexp parentheses around returned regexp.
|
|
|
|
|
If LAX non-nil, don't output parentheses if it doesn't require them.
|
2012-02-28 00:17:21 -08:00
|
|
|
|
Merges keywords to avoid backtracking in Emacs's regexp matcher."
|
2003-01-20 21:37:02 +00:00
|
|
|
|
;; The basic idea is to find the shortest common prefix or suffix, remove it
|
|
|
|
|
;; and recurse. If there is no prefix, we divide the list into two so that
|
2015-09-17 16:08:20 -07:00
|
|
|
|
;; (at least) one half will have at least a one-character common prefix.
|
2003-01-20 21:37:02 +00:00
|
|
|
|
|
|
|
|
|
;; Also we delay the addition of grouping parenthesis as long as possible
|
|
|
|
|
;; until we're sure we need them, and try to remove one-character sequences
|
|
|
|
|
;; so we can use character sets rather than grouping parenthesis.
|
2000-03-09 00:45:15 +00:00
|
|
|
|
(let* ((open-group (cond ((stringp paren) paren) (paren "\\(?:") (t "")))
|
1997-05-29 03:01:51 +00:00
|
|
|
|
(close-group (if paren "\\)" ""))
|
|
|
|
|
(open-charset (if lax "" open-group))
|
2000-03-09 00:45:15 +00:00
|
|
|
|
(close-charset (if lax "" close-group)))
|
1997-05-29 03:01:51 +00:00
|
|
|
|
(cond
|
1999-10-08 23:06:15 +00:00
|
|
|
|
;;
|
|
|
|
|
;; If there are no strings, just return the empty string.
|
|
|
|
|
((= (length strings) 0)
|
|
|
|
|
"")
|
|
|
|
|
;;
|
1997-05-29 03:01:51 +00:00
|
|
|
|
;; If there is only one string, just return it.
|
|
|
|
|
((= (length strings) 1)
|
|
|
|
|
(if (= (length (car strings)) 1)
|
|
|
|
|
(concat open-charset (regexp-quote (car strings)) close-charset)
|
|
|
|
|
(concat open-group (regexp-quote (car strings)) close-group)))
|
|
|
|
|
;;
|
|
|
|
|
;; If there is an empty string, remove it and recurse on the rest.
|
|
|
|
|
((= (length (car strings)) 0)
|
|
|
|
|
(concat open-charset
|
|
|
|
|
(regexp-opt-group (cdr strings) t t) "?"
|
|
|
|
|
close-charset))
|
|
|
|
|
;;
|
2000-03-09 00:45:15 +00:00
|
|
|
|
;; If there are several one-char strings, use charsets
|
|
|
|
|
((and (= (length (car strings)) 1)
|
|
|
|
|
(let ((strs (cdr strings)))
|
|
|
|
|
(while (and strs (/= (length (car strs)) 1))
|
|
|
|
|
(pop strs))
|
|
|
|
|
strs))
|
|
|
|
|
(let (letters rest)
|
|
|
|
|
;; Collect one-char strings
|
|
|
|
|
(dolist (s strings)
|
2000-08-09 15:49:33 +00:00
|
|
|
|
(if (= (length s) 1) (push (string-to-char s) letters) (push s rest)))
|
2000-03-09 00:45:15 +00:00
|
|
|
|
|
|
|
|
|
(if rest
|
|
|
|
|
;; several one-char strings: take them and recurse
|
|
|
|
|
;; on the rest (first so as to match the longest).
|
|
|
|
|
(concat open-group
|
|
|
|
|
(regexp-opt-group (nreverse rest))
|
|
|
|
|
"\\|" (regexp-opt-charset letters)
|
|
|
|
|
close-group)
|
|
|
|
|
;; all are one-char strings: just return a character set.
|
|
|
|
|
(concat open-charset
|
|
|
|
|
(regexp-opt-charset letters)
|
|
|
|
|
close-charset))))
|
1997-05-29 03:01:51 +00:00
|
|
|
|
;;
|
|
|
|
|
;; We have a list of different length strings.
|
|
|
|
|
(t
|
2002-06-18 21:52:42 +00:00
|
|
|
|
(let ((prefix (try-completion "" strings)))
|
2000-03-09 00:45:15 +00:00
|
|
|
|
(if (> (length prefix) 0)
|
|
|
|
|
;; common prefix: take it and recurse on the suffixes.
|
|
|
|
|
(let* ((n (length prefix))
|
|
|
|
|
(suffixes (mapcar (lambda (s) (substring s n)) strings)))
|
2000-10-01 00:08:49 +00:00
|
|
|
|
(concat open-group
|
2000-03-09 00:45:15 +00:00
|
|
|
|
(regexp-quote prefix)
|
|
|
|
|
(regexp-opt-group suffixes t t)
|
2000-10-01 00:08:49 +00:00
|
|
|
|
close-group))
|
2000-03-09 00:45:15 +00:00
|
|
|
|
|
2014-05-23 11:54:44 -04:00
|
|
|
|
(let* ((sgnirts (mapcar #'reverse strings))
|
2002-06-18 21:52:42 +00:00
|
|
|
|
(xiffus (try-completion "" sgnirts)))
|
2000-03-09 00:45:15 +00:00
|
|
|
|
(if (> (length xiffus) 0)
|
|
|
|
|
;; common suffix: take it and recurse on the prefixes.
|
|
|
|
|
(let* ((n (- (length xiffus)))
|
2000-11-05 19:07:07 +00:00
|
|
|
|
(prefixes
|
|
|
|
|
;; Sorting is necessary in cases such as ("ad" "d").
|
|
|
|
|
(sort (mapcar (lambda (s) (substring s 0 n)) strings)
|
|
|
|
|
'string-lessp)))
|
2000-10-01 00:08:49 +00:00
|
|
|
|
(concat open-group
|
2000-03-09 00:45:15 +00:00
|
|
|
|
(regexp-opt-group prefixes t t)
|
2014-05-23 11:54:44 -04:00
|
|
|
|
(regexp-quote (nreverse xiffus))
|
2000-10-01 00:08:49 +00:00
|
|
|
|
close-group))
|
2003-02-04 13:24:35 +00:00
|
|
|
|
|
2000-03-09 00:45:15 +00:00
|
|
|
|
;; Otherwise, divide the list into those that start with a
|
|
|
|
|
;; particular letter and those that do not, and recurse on them.
|
2008-04-09 04:29:43 +00:00
|
|
|
|
(let* ((char (substring-no-properties (car strings) 0 1))
|
2002-06-18 21:52:42 +00:00
|
|
|
|
(half1 (all-completions char strings))
|
2003-01-20 21:37:02 +00:00
|
|
|
|
(half2 (nthcdr (length half1) strings)))
|
2000-03-09 00:45:15 +00:00
|
|
|
|
(concat open-group
|
|
|
|
|
(regexp-opt-group half1)
|
|
|
|
|
"\\|" (regexp-opt-group half2)
|
|
|
|
|
close-group))))))))))
|
|
|
|
|
|
1997-05-29 03:01:51 +00:00
|
|
|
|
|
|
|
|
|
(defun regexp-opt-charset (chars)
|
2012-08-15 00:28:23 +08:00
|
|
|
|
"Return a regexp to match a character in CHARS.
|
2019-06-29 11:10:36 +02:00
|
|
|
|
CHARS should be a list of characters.
|
|
|
|
|
If CHARS is the empty list, the return value is a regexp that
|
|
|
|
|
never matches anything."
|
1997-05-29 03:01:51 +00:00
|
|
|
|
;; The basic idea is to find character ranges. Also we take care in the
|
|
|
|
|
;; position of character set meta characters in the character set regexp.
|
|
|
|
|
;;
|
2016-09-05 20:01:23 +02:00
|
|
|
|
(let* ((charmap (make-char-table 'regexp-opt-charset))
|
2000-08-09 15:49:33 +00:00
|
|
|
|
(start -1) (end -2)
|
1997-05-29 03:01:51 +00:00
|
|
|
|
(charset "")
|
|
|
|
|
(bracket "") (dash "") (caret ""))
|
|
|
|
|
;;
|
|
|
|
|
;; Make a character map but extract character set meta characters.
|
2000-08-09 15:49:33 +00:00
|
|
|
|
(dolist (char chars)
|
2012-04-16 11:47:43 +08:00
|
|
|
|
(cond
|
|
|
|
|
((eq char ?\])
|
|
|
|
|
(setq bracket "]"))
|
|
|
|
|
((eq char ?^)
|
|
|
|
|
(setq caret "^"))
|
|
|
|
|
((eq char ?-)
|
|
|
|
|
(setq dash "-"))
|
|
|
|
|
(t
|
|
|
|
|
(aset charmap char t))))
|
1997-05-29 03:01:51 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Make a character set from the map using ranges where applicable.
|
2000-08-09 15:49:33 +00:00
|
|
|
|
(map-char-table
|
|
|
|
|
(lambda (c v)
|
|
|
|
|
(when v
|
2002-03-01 02:01:36 +00:00
|
|
|
|
(if (consp c)
|
|
|
|
|
(if (= (1- (car c)) end) (setq end (cdr c))
|
|
|
|
|
(if (> end (+ start 2))
|
|
|
|
|
(setq charset (format "%s%c-%c" charset start end))
|
|
|
|
|
(while (>= end start)
|
|
|
|
|
(setq charset (format "%s%c" charset start))
|
2012-04-16 11:47:43 +08:00
|
|
|
|
(setq start (1+ start))))
|
2002-03-01 02:01:36 +00:00
|
|
|
|
(setq start (car c) end (cdr c)))
|
|
|
|
|
(if (= (1- c) end) (setq end c)
|
|
|
|
|
(if (> end (+ start 2))
|
2000-08-09 15:49:33 +00:00
|
|
|
|
(setq charset (format "%s%c-%c" charset start end))
|
|
|
|
|
(while (>= end start)
|
|
|
|
|
(setq charset (format "%s%c" charset start))
|
2012-04-16 11:47:43 +08:00
|
|
|
|
(setq start (1+ start))))
|
2002-03-01 02:01:36 +00:00
|
|
|
|
(setq start c end c)))))
|
2000-08-09 15:49:33 +00:00
|
|
|
|
charmap)
|
|
|
|
|
(when (>= end start)
|
|
|
|
|
(if (> end (+ start 2))
|
|
|
|
|
(setq charset (format "%s%c-%c" charset start end))
|
|
|
|
|
(while (>= end start)
|
|
|
|
|
(setq charset (format "%s%c" charset start))
|
2012-04-16 11:47:43 +08:00
|
|
|
|
(setq start (1+ start)))))
|
2019-06-29 11:10:36 +02:00
|
|
|
|
|
|
|
|
|
;; Make sure that ] is first, ^ is not first, - is first or last.
|
|
|
|
|
(let ((all (concat bracket charset caret dash)))
|
|
|
|
|
(pcase (length all)
|
|
|
|
|
(0 regexp-unmatchable)
|
|
|
|
|
(1 (regexp-quote all))
|
|
|
|
|
(_ (if (string-equal all "^-")
|
|
|
|
|
"[-^]"
|
|
|
|
|
(concat "[" all "]")))))))
|
1997-05-29 03:01:51 +00:00
|
|
|
|
|
2019-02-24 22:12:52 +01:00
|
|
|
|
|
1997-05-29 03:01:51 +00:00
|
|
|
|
(provide 'regexp-opt)
|
|
|
|
|
|
|
|
|
|
;;; regexp-opt.el ends here
|