From 4281e5b34d47052f3f8aa07295032ba3a764c54e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Mon, 23 Aug 2021 17:02:51 +0200 Subject: [PATCH] Add example of advanced user-defined Rx form to manual * doc/lispref/searching.texi (Extending Rx): Add example illustrating how to define a user-defined Rx form that performs computation, from a discussion with Michael Herdeegen (bug#50136). * lisp/emacs-lisp/rx.el (rx): Clarify evaluation time for `eval`. --- doc/lispref/searching.texi | 18 ++++++++++++++++++ lisp/emacs-lisp/rx.el | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/doc/lispref/searching.texi b/doc/lispref/searching.texi index 4d5ae3cb437..68061f0b09f 100644 --- a/doc/lispref/searching.texi +++ b/doc/lispref/searching.texi @@ -1649,6 +1649,24 @@ extra actual argument values not matched by any other parameter in Since the definition is global, it is recommended to give @var{name} a package prefix to avoid name clashes with definitions elsewhere, as is usual when naming non-local variables and functions. + +Forms defined this way only perform simple template substitution. +For arbitrary computations, use them together with with the @code{rx} +forms @code{eval}, @code{regexp} or @code{literal}. Example: + +@example +@group +(defun n-tuple-rx (n element) + `(seq "<" + (group-n 1 ,element) + ,@@(mapcar (lambda (i) `(seq ?, (group-n ,i ,element))) + (number-sequence 2 n)) + ">")) +(rx-define n-tuple (n element) (eval (n-tuple-rx n 'element))) +(rx (n-tuple 3 (+ (in "0-9")))) + @result{} "<\\(?1:[0-9]+\\),\\(?2:[0-9]+\\),\\(?3:[0-9]+\\)>" +@end group +@end example @end defmac @defmac rx-let (bindings@dots{}) body@dots{} diff --git a/lisp/emacs-lisp/rx.el b/lisp/emacs-lisp/rx.el index 071d390f0e4..c48052dee9e 100644 --- a/lisp/emacs-lisp/rx.el +++ b/lisp/emacs-lisp/rx.el @@ -1266,7 +1266,8 @@ Zero-width assertions: these all match the empty string in specific places. (literal EXPR) Match the literal string from evaluating EXPR at run time. (regexp EXPR) Match the string regexp from evaluating EXPR at run time. -(eval EXPR) Match the rx sexp from evaluating EXPR at compile time. +(eval EXPR) Match the rx sexp from evaluating EXPR at macro-expansion + (compile) time. Additional constructs can be defined using `rx-define' and `rx-let', which see. -- 2.39.5