Common Lisp defines three macros,
define-setf-method, that allow the
user to extend generalized variables in various ways.
In Emacs, these are obsolete, replaced by various features of
gv.el in Emacs 24.3.
See Adding Generalized Variables.
This macro defines a “read-modify-write” macro similar to
cl-decf. You can replace this macro with
The macro name is defined to take a place argument followed by additional arguments described by arglist. The call(name place args...)
will be expanded to(cl-callf func place args...)
which in turn is roughly equivalent to(setf place (func place args...))
For example:(define-modify-macro incf (&optional (n 1)) +) (define-modify-macro concatf (&rest args) concat)
&keyis not allowed in arglist, but
&restis sufficient to pass keywords on to the function.
Most of the modify macros defined by Common Lisp do not exactly follow the pattern of
define-modify-macro. For example,
pushtakes its arguments in the wrong order, and
popis completely irregular.
incfexample could be written using
gv-letplaceas:(defmacro incf (place &optional n) (gv-letplace (getter setter) place (macroexp-let2 nil v (or n 1) (funcall setter `(+ ,v ,getter)))))
This is the simpler of two
defsetfforms, and is replaced by
With access-fn the name of a function that accesses a place, this declares update-fn to be the corresponding store function. From now on,(setf (access-fn arg1 arg2 arg3) value)
will be expanded to(update-fn arg1 arg2 arg3 value)
The update-fn is required to be either a true function, or a macro that evaluates its arguments in a function-like way. Also, the update-fn is expected to return value as its result. Otherwise, the above expansion would not obey the rules for the way
setfis supposed to behave.
As a special (non-Common-Lisp) extension, a third argument of
defsetfsays that the return value of
update-fnis not suitable, so that the above
setfshould be expanded to something more like(let ((temp value)) (update-fn arg1 arg2 arg3 temp) temp)
Some examples are:(defsetf car setcar) (defsetf buffer-name rename-buffer t)
These translate directly to
gv-define-simple-setter:(gv-define-simple-setter car setcar) (gv-define-simple-setter buffer-name rename-buffer t)
This is the second, more complex, form of
defsetf. It can be replaced by
This form of
defsetfis rather like
defmacroexcept for the additional store-var argument. The forms should return a Lisp form that stores the value of store-var into the generalized variable formed by a call to access-fn with arguments described by arglist. The forms may begin with a string which documents the
setfmethod (analogous to the doc string that appears at the front of a function).
For example, the simple form of
defsetfis shorthand for(defsetf access-fn (&rest args) (store) (append '(update-fn) args (list store)))
The Lisp form that is returned can access the arguments from arglist and store-var in an unrestricted fashion; macros like
cl-incfthat invoke this setf-method will insert temporary variables as needed to make sure the apparent order of evaluation is preserved.
Another standard example:(defsetf nth (n x) (store) `(setcar (nthcdr ,n ,x) ,store))
You could write this using
gv-define-setteras:(gv-define-setter nth (store n x) `(setcar (nthcdr ,n ,x) ,store))
This is the most general way to create new place forms. You can replace this by
setfto access-fn with arguments described by arglist is expanded, the forms are evaluated and must return a list of five items:
- A list of temporary variables.
- A list of value forms corresponding to the temporary variables above. The temporary variables will be bound to these value forms as the first step of any operation on the generalized variable.
- A list of exactly one store variable (generally obtained from a call to
- A Lisp form that stores the contents of the store variable into the generalized variable, assuming the temporaries have been bound as described above.
- A Lisp form that accesses the contents of the generalized variable, assuming the temporaries have been bound.
This is exactly like the Common Lisp macro of the same name, except that the method returns a list of five values rather than the five values themselves, since Emacs Lisp does not support Common Lisp's notion of multiple return values. (Note that the
setfimplementation provided by gv.el does not use this five item format. Its use here is only for backwards compatibility.)
Once again, the forms may begin with a documentation string.
A setf-method should be maximally conservative with regard to temporary variables. In the setf-methods generated by
defsetf, the second return value is simply the list of arguments in the place form, and the first return value is a list of a corresponding number of temporary variables generated by
cl-gensym. Macros like
cl-incfthat use this setf-method will optimize away most temporaries that turn out to be unnecessary, so there is little reason for the setf-method itself to optimize.