Re: Tool for inserting code for keybindings. (elisp)



apatheticagnostic wrote:
Without further ado, here's the code.

(defun with-nil-var (var &rest body)
"Set var to nil in body, and return it to it's older value
afterwards.
This exists because of the variable print-level, and me not being sure
if emacs has a bunch of other variables that users would probably not
appreciate having clobbered."
(let ((old-var (make-symbol "old")))
`(let ((,old-var ,var))
(setf ,var nil)
,@body
(setf ,var ,old-var))))

(defun with-safe-print-level (&rest body)
"Set print-level to nil in body, and restore its value afterwards."
(with-nil-var print-level body))

this one should certainly be a macro. by defining it as a function, the
body of the code is going to be executed *before* with-safe-print-level is
called: the result of BODY is going to be passed as an argument, not the
body code itself.

but more importantly, you don't need all this. lisp already has a special
form that does this: let.

(progn
(insert (format "\n\nPRINT-LEVEL is now %s\n" print-level))
(let ((print-level 20))
(insert (format "\nPRINT-LEVEL is now %s\n" print-level)))
(insert (format "\nPRINT-LEVEL is now %s\n" print-level)))

execute that code in the *scratch* buffer, and you'll see that after the
let, print-level has its old value again.


--
Joost Kremers joostkremers@xxxxxxxxx
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)
.



Relevant Pages