Several standard (e.g.,
car) and Emacs-specific
window-point) Lisp functions are
setf-able by default.
This package defines
setf handlers for several additional functions:
cl-rest cl-subseq cl-get cl-getf cl-caaar…cl-cddddr cl-first…cl-tenth
Note that for
cl-getf (as for
nthcdr), the list argument
of the function must itself be a valid place form.
buffer-file-name getenv buffer-modified-p global-key-binding buffer-name local-key-binding buffer-string mark buffer-substring mark-marker current-buffer marker-position current-case-table mouse-position current-column point current-global-map point-marker current-input-mode point-max current-local-map point-min current-window-configuration read-mouse-position default-file-modes screen-height documentation-property screen-width face-background selected-window face-background-pixmap selected-screen face-font selected-frame face-foreground standard-case-table face-underline-p syntax-table file-modes visited-file-modtime frame-height window-height frame-parameters window-width frame-visible-p x-get-secondary-selection frame-width x-get-selection get-register
Most of these have directly corresponding “set” functions, like
point. A few, like
point-min, expand to longer
sequences of code when they are used with
(narrow-to-region x (point-max)) in this case).
(substring subplace n [m]), where subplace is itself a valid generalized variable whose current value is a string, and where the value stored is also a string. The new string is spliced into the specified part of the destination string. For example:
(setq a (list "hello" "world")) ⇒ ("hello" "world") (cadr a) ⇒ "world" (substring (cadr a) 2 4) ⇒ "rl" (setf (substring (cadr a) 2 4) "o") ⇒ "o" (cadr a) ⇒ "wood" a ⇒ ("hello" "wood")
The generalized variable
buffer-substring, listed above,
also works in this way by replacing a portion of the current buffer.
setfis applied to the resulting form.
setf macro takes care to evaluate all subforms in
the proper left-to-right order; for example,
(setf (aref vec (cl-incf i)) i)
looks like it will evaluate
(cl-incf i) exactly once, before the
following access to
setf expander will insert
temporary variables as necessary to ensure that it does in fact work
this way no matter what setf-method is defined for
(In this case,
aset would be used and no such steps would
be necessary since
aset takes its arguments in a convenient
However, if the place form is a macro which explicitly evaluates its arguments in an unusual order, this unusual order will be preserved. Adapting an example from Steele, given
(defmacro wrong-order (x y) (list 'aref y x))
(setf (wrong-order a b) 17) will
evaluate b first, then a, just as in an actual call