summaryrefslogtreecommitdiff
path: root/devdocs/elisp/defining-macros.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/elisp/defining-macros.html')
-rw-r--r--devdocs/elisp/defining-macros.html20
1 files changed, 20 insertions, 0 deletions
diff --git a/devdocs/elisp/defining-macros.html b/devdocs/elisp/defining-macros.html
new file mode 100644
index 00000000..1ee20b0c
--- /dev/null
+++ b/devdocs/elisp/defining-macros.html
@@ -0,0 +1,20 @@
+ <h3 class="section">Defining Macros</h3> <p>A Lisp macro object is a list whose <small>CAR</small> is <code>macro</code>, and whose <small>CDR</small> is a function. Expansion of the macro works by applying the function (with <code>apply</code>) to the list of <em>unevaluated</em> arguments from the macro call. </p> <p>It is possible to use an anonymous Lisp macro just like an anonymous function, but this is never done, because it does not make sense to pass an anonymous macro to functionals such as <code>mapcar</code>. In practice, all Lisp macros have names, and they are almost always defined with the <code>defmacro</code> macro. </p> <dl> <dt id="defmacro">Macro: <strong>defmacro</strong> <em>name args [doc] [declare] body…</em>
+</dt> <dd>
+<p><code>defmacro</code> defines the symbol <var>name</var> (which should not be quoted) as a macro that looks like this: </p> <div class="example"> <pre class="example">(macro lambda <var>args</var> . <var>body</var>)
+</pre>
+</div> <p>(Note that the <small>CDR</small> of this list is a lambda expression.) This macro object is stored in the function cell of <var>name</var>. The meaning of <var>args</var> is the same as in a function, and the keywords <code>&amp;rest</code> and <code>&amp;optional</code> may be used (see <a href="argument-list">Argument List</a>). Neither <var>name</var> nor <var>args</var> should be quoted. The return value of <code>defmacro</code> is undefined. </p> <p><var>doc</var>, if present, should be a string specifying the macro’s documentation string. <var>declare</var>, if present, should be a <code>declare</code> form specifying metadata for the macro (see <a href="declare-form">Declare Form</a>). Note that macros cannot have interactive declarations, since they cannot be called interactively. </p>
+</dd>
+</dl> <p>Macros often need to construct large list structures from a mixture of constants and nonconstant parts. To make this easier, use the ‘<samp>`</samp>’ syntax (see <a href="backquote">Backquote</a>). For example: </p> <div class="example"> <pre class="example">(defmacro t-becomes-nil (variable)
+ `(if (eq ,variable t)
+ (setq ,variable nil)))
+</pre>
+
+<pre class="example">(t-becomes-nil foo)
+ ≡ (if (eq foo t) (setq foo nil))
+</pre>
+</div><div class="_attribution">
+ <p class="_attribution-p">
+ Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
+ <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Macros.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Macros.html</a>
+ </p>
+</div>