0

I have seen the use of ;;;###autoload when defining minor modes. Why do some modes define foo-enable and foo-disable, and could be the purpose of ;;;###autoload before all of them ?

;;;###autoload
(define-minor-mode foo-minor-mode
  "Enhances visibility of heading levels and text emphasis."
  :lighter " Foo"
  (do-this)
  (do-that)
)

;;;###autoload
(defun foo-enable ()
  "Enables `foo-minor-mode'."
  (foo-minor-mode 1))

;;;###autoload
(defun foo-disable ()
  "Disables `foo-minor-mode'."
  (foo-minor-mode 0))
Dilna
  • 1,173
  • 3
  • 10
  • 2
    Does this answer your question? [When should I use autoload instead of require?](https://emacs.stackexchange.com/questions/364/when-should-i-use-autoload-instead-of-require) – NickD Jun 09 '23 at 18:22

1 Answers1

0

Emacs defers loading of large portion of elisp code in emacs packages until it's called by the user. It's an optimization technique that speeds up start up times. On start you can only call these ;;;###autoload functions (functions that have ;;;###autoload header in the code) and they call the rest of the packaged code.

It is expected that the package authors will follow this conventions (and it's usually the case...).

From the manual:

The autoload facility lets you register the existence of a function or macro, but put off loading the file that defines it. The first call to the function automatically loads the proper library

NickD
  • 27,023
  • 3
  • 23
  • 42
  • What are the customary functions that would require `;;;###autoload`. Besides the aforementioned for `foo-minor-mode`. Would a function that adds hooks to specific major modes would also benefit from `;;;###autoload` ? – Dilna Jun 09 '23 at 19:00
  • From what I've seen all interactive functions that you want to provide to the user when emacs starts fall under this category and as you mentioned all "enable", "start" and "major/minor-mode"-like functions. – Daniel Krajnik Jun 09 '23 at 19:14
  • The functions with autoload are not loaded except when called. Why are the other functions related to the minor mode not autoloaded as well ? Would one not want to load them except on demand too ? – Dilna Jun 09 '23 at 19:53
  • Someone may need to correct me on this, but I think that there may be more advanced techniques to expose functions to the user other than the `;;;###autoload` header, but by default it seems like the package authors need to choose which functions to expose on start explicitely. I agree though that this can get confusing when a function in some .el file doesn't initially show up in emacs. – Daniel Krajnik Jun 09 '23 at 20:05