0

In my package I have the following:

(defcustom foobar-on-exit t
"Boolean variable, enabled by default."
:type 'boolean
:group 'foobar)

(if foobar-on-exit 
    (add-hook 'kill-emacs-hook 'some-message))

So I get a message when I close Emacs.

Then in my init.el I set the value of foobar-on-exit on nil.

(setq foobar-on-exit 'nil)

After evaluating or restarting, I still got the message while the variable foobar-on-exit is still false.

So somewhere I went wrong. Any hint?

5
  • 1
    Is this a simple evaluation-order problem? If the if statement is in your package, then foobar-on-exit will be t when that form is evaluated and the message will be added to kill-emacs-hook regardless of what you do with the variable afterwards. So if your package is loaded (via autoload or otherwise) prior to the setq, you'll always get the message. Aug 19, 2015 at 13:34
  • Ah, I get it. :Let's assume the boolean variable is in the package by default true, and that hook will be evalauted. How could I make it false in my init.el, then it will be evaluated afterwards in the package?
    – ReneFroger
    Aug 19, 2015 at 13:43
  • Well, if you set it prior to loading the package, it should work as intended, but you may be subject to a "reference to free variable" warning should you byte-compile your init file. The correct sequencing is defcustom, setq, if. The only way to do that is to have the defcustom in the package, and the setq and if in your init file. A better way is to put the if in the form that gets added to kill-emacs-hook, so it will always use the current value at shutdown time. Aug 19, 2015 at 14:18
  • @TreborRude You're right, especially with your last sentence. I added the if into a function, that will do evaluating, and I added that function to a hook. Thanks for your suggestion, it's appreciated. Wish I could mark your answer as the right one.
    – ReneFroger
    Aug 19, 2015 at 15:00
  • Now you can, @ReneFroger. :-) Aug 19, 2015 at 15:10

1 Answer 1

2

I believe this is an order-of-evaluation problem. Since the defcustom and the if are both in the package, and the setq is in the init file (presumably some time after the loading of the package), when the package gets loaded (via autoload or otherwise), foobar-on-exit will be set to t, then the if form will be evaluated and some-message will get added to kill-emacs-hook.

The correct sequence of events is defcustom, setq, if. The only way to do that presently is to have both the setq and the if in your init file.

If you want to leave the if in the package, then it needs to be in a function that gets added to kill-emacs-hook, that way the current value of foobar-on-exit will always be used when emacs is killed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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