3

I am adding Customize support for a package that provides a couple of global mode-independent commands. As I do not want to load the package unless the user explicitly invokes the commands through keybindings yet I want to allow customization right after the user installs the package, I tried to use code like:

;;;###autoload
(defcustom foo-bar nil 
 "bar setting for for"
 :type boolean)

;;;###autoload
(defun foo-command-1 () ...)

With that after I install the package I can invoke foo-command-1. I can also use customize-variable to set and save foo-bar. However, when I start Emacs again, the value of foo-bar is reset to default and Emacs complains that the value is changed outside customize.

AFAICS the reason for this is that the code that Emacs puts into the autoloads file for defcustom assumes that it will run before Emacs calls custom-set-variables in the init.el. However, this is not the case with a package which autoloads are run after the init file.

Is this a known problem? To workaround I replaced the above with something like:

;;;###autoload
(unless (fboundp 'foo-command-1)
 (defcustom foo-bar nil 
  "bar setting for for"
  :type boolean))

;;;###autoload
(defun foo-command-1 () ...)

That copies the whole defcustom definition into the autoloads and prevents running it the second time when the package is loaded for real. This works and saved options are properly restored. Still I am puzzled why ###autoload for defcustom does not do the right thing on its own.

2 Answers 2

2

Yes, what you did is a reasonable way to accomplish what you want. If you want to autoload a defcustom and you care about when it will be evaluated relative to other code, then you need to control that timing in some way. The way you chose is reasonable.

A user's init file can load custom-file at any point the user chooses. And if no custom-file is used it is still not sure at what point the part of the init-file code that is managed by Customize will be evaluated with respect to other parts that might depend on values that it sets.

1

As a general rule, you don't want to autoload variables.

Yes, it's a known problem, and there's no really good solution to it (each solution I could come up with had its own set of bugs/misbehaviors), which is why all I can say is "don't autoload variables, please".

Even in the case where it happens to do what you want, it's undesirable in my experience. It just has too many quirks and corner cases.

5
  • This is more of a comment than an answer. And you mention the existence of unnamed problems without describing any of them. Guidance to do or not do something, with no explanation of why, is at best a comment.
    – Drew
    Sep 21, 2015 at 14:01
  • Can you give an example of misbehavior that can be expected with the code sample from the question? I could not find one so far with my limited testing. Sep 22, 2015 at 7:48
  • @IgorBukanov: For this particular case, one thing that comes to mind is that the autoloaded copy of the code is moved outside of its context, so it won't inherit the "implicit :group" in the same way.
    – Stefan
    Sep 22, 2015 at 12:26
  • That's only an argument to use explicit :group, which makes sense anyway (including for "this particular case"), and especially when there is more than one :group present in the previous defcustom. Plus, the byte-compiler issues a warning if you do not explicitly supply :group. Plus, the "feature" of :group inheritance is not even documented. And AFAICT, your opposition to autoloaded defcustoms predates the :group "inheritance" feature.
    – Drew
    Sep 22, 2015 at 14:33
  • @Drew: Oh, yes, in general there are many more problems. I was just replying to the request to point out a problem in this specific use-case. If you want more problems, either general ones or specific, of course, I can find more. E.g. for this specific case, other problems include the fact that the "unless" test is ugly, that it artificially links the var to the function's name and existence, and that M-C-x won't properly reset the var to its default value.
    – Stefan
    Sep 22, 2015 at 18:11

Your Answer

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

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