branch: externals/compat commit adbf07f9f1ee421fd450c5e3c410a1222e592022 Author: Daniel Mendler <m...@daniel-mendler.de> Commit: Daniel Mendler <m...@daniel-mendler.de>
compat-25: Add macroexp-parse-body and macroexp-quote --- NEWS.org | 1 + compat-25.el | 23 +++++++++++++++++++++++ compat-tests.el | 16 ++++++++++++++++ compat.texi | 14 ++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/NEWS.org b/NEWS.org index cbcc0c5c65..eb9171fdde 100644 --- a/NEWS.org +++ b/NEWS.org @@ -3,6 +3,7 @@ * Development - compat-25: Add ~hash-table-empty-p~. +- compat-25: Add ~macroexp-parse-body~ and ~macroexp-quote~. - compat-27: Add ~date-ordinal-to-time~. - compat-27: Add ~make-decoded-time~. - compat-28: Add ~color-dark-p~. diff --git a/compat-25.el b/compat-25.el index d12d09fc38..b25288a29d 100644 --- a/compat-25.el +++ b/compat-25.el @@ -196,6 +196,29 @@ threading." ;;;; Defined in macroexp.el +(compat-defun macroexp-parse-body (body) ;; <compat-tests:macroexp-parse-body> + "Parse a function BODY into (DECLARATIONS . EXPS)." + (let ((decls ())) + (while (and (cdr body) + (let ((e (car body))) + (or (stringp e) + (memq (car-safe e) + '(:documentation declare interactive cl-declare))))) + (push (pop body) decls)) + (cons (nreverse decls) body))) + +(compat-defun macroexp-quote (v) ;; <compat-tests:macroexp-quote> + "Return an expression E such that `(eval E)' is V. + +E is either V or (quote V) depending on whether V evaluates to +itself or not." + (if (and (not (consp v)) + (or (keywordp v) + (not (symbolp v)) + (memq v '(nil t)))) + v + (list 'quote v))) + (compat-defun macroexpand-1 (form &optional environment) ;; <compat-tests:macroexpand-1> "Perform (at most) one step of macro expansion." (cond diff --git a/compat-tests.el b/compat-tests.el index 9ff6425444..1a1b2ddc84 100644 --- a/compat-tests.el +++ b/compat-tests.el @@ -2493,6 +2493,22 @@ (ert-deftest macroexp-warn-and-return () (should-equal (macroexp-warn-and-return "test warning" '(some form)) '(some form))) +(ert-deftest macroexp-parse-body () + (should-equal '(((declare test)) . (a b c)) + (macroexp-parse-body '((declare test) a b c))) + (should-equal '(((interactive)) . (a b c)) + (macroexp-parse-body '((interactive) a b c))) + (should-equal '(((interactive) (cl-declare)) . (a b c)) + (macroexp-parse-body '((interactive) (cl-declare) a b c)))) + +(ert-deftest macroexp-quote () + (should-equal nil (macroexp-quote nil)) + (should-equal t (macroexp-quote t)) + (should-equal :key (macroexp-quote :key)) + (should-equal "str" (macroexp-quote "str")) + (should-equal ''sym (macroexp-quote 'sym)) + (should-equal ''(1 2 3) (macroexp-quote '(1 2 3)))) + (ert-deftest macroexpand-1 () (should-equal '(if a b c) (macroexpand-1 '(if a b c))) (should-equal '(if a (progn b)) (macroexpand-1 '(when a b))) diff --git a/compat.texi b/compat.texi index 094788c4da..ea0e020d00 100644 --- a/compat.texi +++ b/compat.texi @@ -394,6 +394,16 @@ performs one step of the expansion: if the result is another macro call, @xref{Expansion,Expansion,,elisp}. @end defun +@c based on lisp/emacs-lisp/macroexp.el +@defun macroexp-quote e +Return an expression @var{e} such that @code{(eval e)} is @var{v}. +@end defun + +@c based on lisp/emacs-lisp/macroexp.el +@defun macroexp-parse body +Parse a function @var{body} into @code{(declarations . exps)}. +@end defun + @defun bool-vector &rest objects This function creates and returns a bool-vector whose elements are the arguments, @var{objects}. @@ -427,6 +437,10 @@ implemented in 25.1: @itemize @item +The function @code{macroexp-macroexpand}. +@item +The macro @code{macroexp-let2*}. +@item The function @code{directory-files-recursively}. @item New @code{pcase} patterns.