I understand what autoload does for functions (register file to load when such function is called or its documentation string is retrieved). However, it's not clear how to use autoload facility in conjunction with variables and macros.
I have two questions:
What happens when package has parameter, implemented as a variable that user can set, but it's not autoloaded? Should such variables be autoloaded? If not, it turns out that such variables do not exist, Lisp environment knows nothing about them, including their default values, until some autoloaded function from the package is used (typically after loading of configuration files), then if user sets them in his/her configuration file, it's like setting non-existing variable. If value of the variable is a non-empty list and user uses
push
oradd-to-list
to change its value, what exactly happens? Are default values lost?What happens when a macro is autoloaded? When should we autoload a macro?
setq
ed (i.e. it doesn't matter what values they had before) can have default specified indefvar
ordefcustom
form, but in case of list that can be extended by user it's best to useeval-after-load
right? Also, sometimes good defaults are good, even if they take form of a list ;-) – Mark Karpov Jul 13 '15 at 07:33eval-after-load
, it's a bug in the package implementation. Typically,eval-after-load
should be used for bug fixes or highly unusual customizations. By the way, it may be better to use a hook if the package makes one available. – Harald Hanche-Olsen Jul 13 '15 at 08:12eval-after-load
way, users still won't be able to remove elements from the list! This makes me wonder if I should abandon the defaults at all. – Mark Karpov Jul 13 '15 at 08:16eval-after-load
should be needed only for unusual situations. It's one of the standard tools in the deferred-loading toolkit. If people don't want to use such tools, they canrequire
the library up front (which is indeed what newcomers are invariably recommended to do -- by the time they get around to thinking "I wish Emacs started a little faster", they've probably learned enough not to be scared off by the new syntax :) – phils Jul 13 '15 at 08:56setq
the relevant variable up front. – Harald Hanche-Olsen Jul 13 '15 at 09:36;;;###autoload
cookie adds declaration of the variable to corresponding autoload file (when installed via package manager, at least) thus it can be used to "predeclare" variables and their values (I think it's also kinda cheap operation). Did you know that? Can you add the fact into your answer for future readers? – Mark Karpov Jul 13 '15 at 11:52defcustom
form, you should be careful about its initialization code (e.g., make sure that any functions and variables it uses are autoloaded first or are otherwise available without loading the library). And autoloading a long variable definition can contribute to autoload-file bloat. IOW, (1) it is entirely possible and (2) use it judiciously, if you use it. – Drew Feb 01 '16 at 02:03defcustom
. If a user sets the option value before thedefcustom
is evaluated, thedefcustom
default value is not used. "If OPTION already has a default value, it is left unchanged. If the user has already saved a customization for OPTION, the user’s customized value is installed as the default value." – Drew Feb 01 '16 at 02:09setq
for user options. Usecustomize-set-variable
or use the Customize UI. Why? Becausesetq
knows nothing about:set
and initialization triggers (functions). See Advantages of setting variables with setq instead of custom.el?. – Drew Feb 01 '16 at 02:13custom-set-variables
does have some of the features I associate with autoloading, i.e., it's not evaluated until needed. – Harald Hanche-Olsen Feb 01 '16 at 12:46eval-after-load
. If possible and meaningful, it might be better to provide a customizable variable containing additions to the given list. Perhaps this is better solved with some of the more advanced features of the customize feature; I haven't studied it in that much detail. – Harald Hanche-Olsen Feb 01 '16 at 12:52customize-set-variable
rather thansetq
may be a good one. But I don't have the time now for the careful study required to improve my answer in this regard. If you think this is important, why don't you write your own answer? It will be more visible than this long comment thread. – Harald Hanche-Olsen Feb 01 '16 at 12:55