Dana Vrajitoru
C311 Programming Languages
Elisp Special Functions, Buffers
Special Lisp Functions
- Special cases of functions: inline, macros, primitives, special
forms, (keystroke) commands, byte-code functions.
- Inline functions: use defsubst instead of defun in the function
definition.
- They can be used just like functions after they are declared, but
they can speed up the program.
- Macro: defined like a function with defmacro instead of defun, but
instead of computing a value, it computes another Lisp expression
which in turn computes the value.
- The expression is called the expansion of the macro.
- Macros can simulate functions with reference parameters, but large
macros can be difficult to write.
Built-in Functions
- Primitive - A function written in C that can be called from
Lisp. It becomes a built-in function or a subr. One can add their own
primitives to Emacs but Emacs must be recompiled to add them.
- Special form – a function that doesn't evaluate all the arguments
the usual way.
- Example: the conditional.
(if cond then-expr else-expr1 else-expr2 ...)
- If the condition has the value nil, then the then-expr is not
evaluated at all.
Commands
- A command is a function that can be invoked interactively with
command-execute or interactively with M-x.
- A keystroke command is a command that can be invoked with a
combination of keys.
- A command also works as a regular function and can be called from
anywhere.
- For a function to be a command, it must contain a call to the
function interactive as the first expression in its definition.
Examples of Commands
(defun move2 ()
(interactive)
(forward-word 2))
(defun move-line (L)
(interactive "nNr of lines:")
(forward-line L))
In the second function the argument in the call to interactive
means that the first argument will be input from the user (minibuffer)
and the text "Nr of lines:" will be displayed in the operation.
More examples
here
Buffers in Emacs
- When writing commands, it might be useful to access the current
buffer, or selected region in the current buffer.
- A buffer is a Lisp object. The current buffer can be obtained by
calling the function (current-buffer).
- Some useful information about the buffer: buffer-name,
buffer-modified-p.
- Some operations to be done on buffers:
kill-buffer, save-buffer,
et.
Examples
(defun save-all ()
(interactive)
(let ((BL (buffer-list)))
(mapc 'save-buffer BL)))
(setq B (current-buffer))
#<buffer macro.el>
(buffer-name B)
"macro.el"
(buffer-modified-p B) ; t
(save-buffer B) ; nil
Customizing Emacs