1

I called add-hook multiple times because I had a few typos in the hook function, and now there are a bunch of broken copies of it in the hook.

I want to do (setq after-save-hook nil), but after doing describe-variable on after-save-hook, I see this:

Original value was nil
Local in buffer driver.z80; global value is 
((lambda nil
   (if
       (string=
        (buffer-name)
        "driver.z80")
       (compile "build.bat")))
 (lambda nil
   (if
       (string=
        (buffer-name "driver.z80"))
       (compile "build.bat")))
 (lambda nil
   (if
       (string=\?
        (buffer-name "driver.z80"))
       (compile "build.bat")))
 (lambda nil
   (if
       (string=\?
        (buffer-name "driver.z80")
        (compile "build.bat"))))
 rmail-after-save-hook)

And no matter what I do I can't seem to clear this out. Any suggestions?

  • 1
    For buffer-local-variables, I like to use setq-default for the global value and setq-local for the buffer-local value. – lawlist Jun 12 at 3:21
  • Looks like setq-default was what I was after. Thanks! – user1122107 Jun 12 at 3:22
  • 1
    Many users recommend adding/removing functions to hooks that have their own names, rather than (lambda ...) as it makes inspecting and managing a little easier with add-hook / remove-hook.... – lawlist Jun 12 at 3:23
  • Is buffer-local really relevant to the problem you encountered? If so, how so? – Drew Jun 12 at 13:54
0

What @lawlist said in a comment:

Do not put lambda forms on hooks (avoid that, at least). It is so much easier to provide a named function to remove-hook than to try to provide it the exact lambda form you provided to add-hook.


That said, you've already done it. ;-) What you need to do now is to do C-h v, as you've done, and then select the printed text for one of the lambda forms and pass it to remove-hook. Then do the same with each of the other lambda forms.


One thing I do sometimes when I'm experimenting with some code: For each add-hook I write and play with I put a corresponding remove-hook in a buffer (e.g. *scratch*), to be able to easily evaluate to cancel that hook function.

(Similarly, sometimes when I use debug-on-entry I put a (cancel-debug-on-entry) sexp somewhere easy to evaluate.)

  • Any reason it was so hard to reset the variable to just nil? Using setq seemed to do nothing, and only setq-default actually changed the value seen when I used C-h v, and I think it screwed somethings up because then add-hook wouldn't add stuff to it. – user1122107 Jun 13 at 1:43
  • setq changes only the buffer-local value of a variable, if there is a local value, which it is here. Did you use a non-nil 4th arg to add-hook, to affect only the local value? – Drew Jun 13 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.