3 ms·
setf is one of the most underrated Lisp powers. In Javascript, it'd look like this: // save current cursor position var x = point(); // set the cursor p
by sillysaurusx 5y ago
setf is one of the most underrated Lisp powers. In Javascript, it'd look like this:
// save current cursor position
var x = point();
// set the cursor position
point() = 42;
// insert some text
insert("Hi there")
// reset cursor position
point() = x;
Notice you're assigning a value to a function call.
Under the hood, point() = x; gets transformed to set_point(x); or whatever else you tell it to do -- it's programmable, and you have full access to the right hand side arguments.
This might not seem too compelling. The key is that it composes.
I was just getting to the good part, but our puppy is demanding my attention. Duty calls.
Suffice to say, nested calls like
point(find_buffer("foo.txt")) = 21
can be rewritten to
prev_buffer = current_buffer();
set_buffer(find_buffer("foo.txt"));
set_point(21);
set_buffer(prev_buffer);
automatically.