From ba401fc2d5ee090ed065e9f0c0d90ef8a3b1fea4 Mon Sep 17 00:00:00 2001 From: immerrr Date: Sun, 7 Feb 2016 12:46:37 +0300 Subject: [PATCH] Fix regexp-opt documentation (bug #17862) * lisp/emacs-lisp/regexp-opt.el (regexp-opt): update PAREN doc * doc/lispref/searching.texi (Regexp Functions): update PAREN doc --- doc/lispref/searching.texi | 33 ++++++++++++++++++++++----------- lisp/emacs-lisp/regexp-opt.el | 29 +++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/doc/lispref/searching.texi b/doc/lispref/searching.texi index 1243d72..e6de768 100644 --- a/doc/lispref/searching.texi +++ b/doc/lispref/searching.texi @@ -946,23 +946,34 @@ Regexp Functions more efficient, but is almost never worth the effort.}. @c E.g., see http://debbugs.gnu.org/2816 -If the optional argument @var{paren} is non-@code{nil}, then the -returned regular expression is always enclosed by at least one -parentheses-grouping construct. If @var{paren} is @code{words}, then -that construct is additionally surrounded by @samp{\<} and @samp{\>}; -alternatively, if @var{paren} is @code{symbols}, then that construct -is additionally surrounded by @samp{\_<} and @samp{\_>} -(@code{symbols} is often appropriate when matching -programming-language keywords and the like). +The optional argument @var{paren} can be any of the following: + +a string + the resulting regexp is surrounded by @code{paren} and @samp{\)}, + e.g. use @samp{"\\(?1:"} to produce an explicitly numbered group. + +@code{words} + the resulting regexp is surrounded by @samp{\<\(} and @samp{\)\>}. + +@code{symbols} + the resulting regexp is surrounded by @samp{\_<\(} and @samp{\)\_>} + (this is often appropriate when maching programming-language + keywords and the like). + +non-@code{nil} + the resulting regexp is surrounded by @samp{\(} and @samp{\)}. + +Otherwise the resulting regexp is surrounded by @samp{\(?:} and +@samp{\)}. This simplified definition of @code{regexp-opt} produces a regular expression which is equivalent to the actual value -(but not as efficient): +(but typically more efficient): @example (defun regexp-opt (strings &optional paren) - (let ((open-paren (if paren "\\(" "")) - (close-paren (if paren "\\)" ""))) + (let ((open-paren (make-open-paren paren)) + (close-paren (make-close-paren paren))) (concat open-paren (mapconcat 'regexp-quote strings "\\|") close-paren))) diff --git a/lisp/emacs-lisp/regexp-opt.el b/lisp/emacs-lisp/regexp-opt.el index b1e132a..6a3912c 100644 --- a/lisp/emacs-lisp/regexp-opt.el +++ b/lisp/emacs-lisp/regexp-opt.el @@ -86,18 +86,31 @@ ;;;###autoload (defun regexp-opt (strings &optional paren) "Return a regexp to match a string in the list STRINGS. -Each string should be unique in STRINGS and should not contain any regexps, -quoted or not. If optional PAREN is non-nil, ensure that the returned regexp -is enclosed by at least one regexp grouping construct. +Each string should be unique in STRINGS and should not contain +any regexps, quoted or not. Optional PAREN specifies how the +returned regexp is surrounded by grouping constructs. + The returned regexp is typically more efficient than the equivalent regexp: - (let ((open (if PAREN \"\\\\(\" \"\")) (close (if PAREN \"\\\\)\" \"\"))) + (let ((open (make-open-paren PAREN)) (close (make-close-paren PAREN))) (concat open (mapconcat \\='regexp-quote STRINGS \"\\\\|\") close)) -If PAREN is `words', then the resulting regexp is additionally surrounded -by \\=\\< and \\>. -If PAREN is `symbols', then the resulting regexp is additionally surrounded -by \\=\\_< and \\_>." +PAREN can be any of the following: + +a string + the resulting regexp is surrounded by PAREN and \\), 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 \\). + +Otherwise the resulting regexp is surrounded by \\(?: and \\)." (save-match-data ;; Recurse on the sorted list. (let* ((max-lisp-eval-depth 10000) -- 2.7.0