To add a bit to what phils wrote. with-
is usually an indication of resource management, so your initial guess was close (btw. Emacs Lisp is no less imperative language than Python). So, for example, with-current-buffer
will enter and exit the specified buffer, i.e. create at start and clean afterwards. with-output-to-string
will allocate a buffer where it will collect all output of the forms inside the macro.
The rationale behind these kinds of macros is that sometimes you don't want the user of your macro to deal with resource allocation and deallocation. So, such macros tend to have two distinct features:
- They provide a variable bound during macro execution (the one which designates the resource being worked with).
- They release acquired resources after the user code finishes.
A typical implementation would do something like this:
(defmacro with-something (var &body body)
`(let ((var (allocate-var)))
(unwind-protect ,@body
(when var (deallocate-var)))))
Where body
forms use var
resource very similar to how in your Python code you could use the variable declared in as var
part.
with-current-buffer
is somewhat different since it doesn't expose the buffer variable, but calling buffer-related functions will be equivalent, so there's no real need.