diff options
Diffstat (limited to 'lisp/emacs-lisp/rx.el')
-rw-r--r-- | lisp/emacs-lisp/rx.el | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/lisp/emacs-lisp/rx.el b/lisp/emacs-lisp/rx.el index 1e3eb9c12b1..43bd84d9990 100644 --- a/lisp/emacs-lisp/rx.el +++ b/lisp/emacs-lisp/rx.el @@ -1445,12 +1445,23 @@ following constructs: (regexp (rx--to-expr (rx--pcase-transform (cons 'seq regexps)))) (nvars (length rx--pcase-vars))) `(and (pred stringp) - ,(if (zerop nvars) - ;; No variables bound: a single predicate suffices. - `(pred (string-match ,regexp)) + ,(pcase nvars + (0 + ;; No variables bound: a single predicate suffices. + `(pred (string-match ,regexp))) + (1 + ;; Create a match value that on a successful regexp match + ;; is the submatch value, 0 on failure. We can't use nil + ;; for failure because it is a valid submatch value. + `(app (lambda (s) + (if (string-match ,regexp s) + (match-string 1 s) + 0)) + (and ,(car rx--pcase-vars) (pred (not numberp))))) + (_ ;; Pack the submatches into a dotted list which is then ;; immediately destructured into individual variables again. - ;; This is of course slightly inefficient when NVARS > 1. + ;; This is of course slightly inefficient. ;; A dotted list is used to reduce the number of conses ;; to create and take apart. `(app (lambda (s) @@ -1463,7 +1474,7 @@ following constructs: (rx--reduce-right #'cons (mapcar (lambda (name) (list '\, name)) - (reverse rx--pcase-vars))))))))) + (reverse rx--pcase-vars)))))))))) ;; Obsolete internal symbol, used in old versions of the `flycheck' package. (define-obsolete-function-alias 'rx-submatch-n 'rx-to-string "27.1") |