Function: rx-let

rx-let is an autoloaded macro defined in rx.el.gz.

Signature

(rx-let BINDINGS BODY...)

Documentation

Evaluate BODY with local BINDINGS for rx.

BINDINGS is an unevaluated list of bindings each on the form
(NAME [(ARGS...)] RX).
They are bound lexically and are available in rx expressions in BODY only.

For bindings without an ARGS list, NAME is defined as an alias for the rx expression RX. Where ARGS is supplied, NAME is defined as an rx form with ARGS as argument list. The parameters are bound from the values in the (NAME ...) form and are substituted in RX. ARGS can contain &rest parameters, whose values are spliced into RX where the parameter name occurs.

Any previous definitions with the same names are shadowed during the expansion of BODY only. For local extensions to rx-to-string, use rx-let-eval. To make global rx extensions, use rx-define. For more details, see Info node (elisp) Extending Rx.

Other relevant functions are documented in the regexp group.

View in manual

Probably introduced at or before Emacs version 27.1.

Shortdoc

;; regexp
(rx-let ((comma-separated (item) (seq item (0+ "," item)))
           (number (1+ digit))
           (numbers (comma-separated number)))
    (rx "(" numbers ")"))
    => "([[:digit:]]+\\(?:,[[:digit:]]+\\)*)"
    => "([[:digit:]]+\\(?:,[[:digit:]]+\\)*)"

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/rx.el.gz
;;;###autoload
(defmacro rx-let (bindings &rest body)
  "Evaluate BODY with local BINDINGS for `rx'.
BINDINGS is an unevaluated list of bindings each on the form
(NAME [(ARGS...)] RX).
They are bound lexically and are available in `rx' expressions in
BODY only.

For bindings without an ARGS list, NAME is defined as an alias
for the `rx' expression RX.  Where ARGS is supplied, NAME is
defined as an `rx' form with ARGS as argument list.  The
parameters are bound from the values in the (NAME ...) form and
are substituted in RX.  ARGS can contain `&rest' parameters,
whose values are spliced into RX where the parameter name occurs.

Any previous definitions with the same names are shadowed during
the expansion of BODY only.
For local extensions to `rx-to-string', use `rx-let-eval'.
To make global rx extensions, use `rx-define'.
For more details, see Info node `(elisp) Extending Rx'.

\(fn BINDINGS BODY...)"
  (declare (indent 1) (debug (sexp body)))
  (let ((prev-locals (cdr (assq :rx-locals macroexpand-all-environment)))
        (new-locals (mapcar #'rx--make-named-binding bindings)))
    (macroexpand-all (cons 'progn body)
                     (cons (cons :rx-locals (append new-locals prev-locals))
                           macroexpand-all-environment))))