From: Chong Yidong Date: Sat, 18 Jun 2011 21:12:33 +0000 (-0400) Subject: Add rx.el support for numbered groups (Bug#8776). X-Git-Tag: emacs-pretest-24.0.90~104^2~518 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=6420d28b9ab9c09b69992e05e0e63c3bbaf2646d;p=emacs.git Add rx.el support for numbered groups (Bug#8776). * lisp/emacs-lisp/rx.el (rx-constituents): Add support for numbered groups. (rx-submatch-n): New function. (rx): Document it. --- diff --git a/etc/NEWS b/etc/NEWS index dad42e14f02..f8d6e590835 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1051,6 +1051,9 @@ deferring warnings until the main command loop is executed. ** `set-auto-mode' now respects mode: local variables at the end of files, as well as those in the -*- line. +--- +** rx.el has a new `group-n' construct for explicitly numbered groups. + * Changes in Emacs 24.1 on non-free operating systems diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 1d66ebe8039..e1a23e53649 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,5 +1,10 @@ 2011-06-18 Chong Yidong + * emacs-lisp/rx.el (rx-constituents): Add support for numbered + groups (Bug#8776). + (rx-submatch-n): New function. + (rx): Document it. + * dired-x.el (dired-mark-unmarked-files): Fix interactive spec (Bug#8768). diff --git a/lisp/emacs-lisp/rx.el b/lisp/emacs-lisp/rx.el index 7122de4789c..56efd142198 100644 --- a/lisp/emacs-lisp/rx.el +++ b/lisp/emacs-lisp/rx.el @@ -130,6 +130,8 @@ (** . (rx-** 2 nil)) ; SRE (submatch . (rx-submatch 1 nil)) ; SRE (group . submatch) ; sregex + (submatch-n . (rx-submatch-n 2 nil)) + (group-n . submatch-n) (zero-or-more . (rx-kleene 1 nil)) (one-or-more . (rx-kleene 1 nil)) (zero-or-one . (rx-kleene 1 nil)) @@ -690,6 +692,16 @@ FORM is either `(repeat N FORM1)' or `(repeat N M FORMS...)'." (mapconcat (lambda (re) (rx-form re ':)) (cdr form) nil)) "\\)")) +(defun rx-submatch-n (form) + "Parse and produce code from FORM, which is `(submatch-n N ...)'." + (let ((n (nth 1 form))) + (concat "\\(?" (number-to-string n) ":" + (if (= 3 (length form)) + ;; Only one sub-form. + (rx-form (nth 2 form)) + ;; Several sub-forms implicitly concatenated. + (mapconcat (lambda (re) (rx-form re ':)) (cddr form) nil)) + "\\)"))) (defun rx-backref (form) "Parse and produce code from FORM, which is `(backref N)'." @@ -1072,6 +1084,11 @@ CHAR like `and', but makes the match accessible with `match-end', `match-beginning', and `match-string'. +`(submatch-n N SEXP1 SEXP2 ...)' +`(group-n N SEXP1 SEXP2 ...)' + like `group', but make it an explicitly-numbered group with + group number N. + `(or SEXP1 SEXP2 ...)' `(| SEXP1 SEXP2 ...)' matches anything that matches SEXP1 or SEXP2, etc. If all