The ins and outs of setf24 Aug 2014, by Artur Malabarba.
Common-Lisp in Emacs post series
setf is a modest name for a macro that does much more work than it
gets paid for. Quoting the doc page:
This is a generalized version of `setq'; the PLACEs may be symbolic
references such as (car x) or (aref x i), as well as plain symbols.
For example, (setf (cadr x) y) is equivalent to (setcar (cdr x) y).
The return value is the last VAL in the list.
Upon reading that, you'd be excused for thinking something mediocre such as “That's neat!” or “I'll try to remember that”. That understatement of a doc page merely touches on the power that lies beneath the surface.
Let's start with an example from the manual, depending on your Emacs
version you may need to
(require 'cl) first.
Let's get slightly more practical. Do you know which function changes the buffer being displayed by a given window? How about changing the height of a window? You don't need to!
And finally, if we just want to get cute,
For a wider list, have a look at cl package manual page, which lists what you
get by requiring
cl-lib, or see the elisp manual page, which lists
what's loaded by default on recent versions of Emacs. My thanks to Christopher
Wellons and Rob Thorpe for the links, and it was Wellons' post on string
mutability which inspired this post.