What is the purpose of off-loading definitions wit

2019-03-06 03:21发布

问题:

In ELisp, you can skip the evaluation of a definition with the autoload cookie. The definition is evaluated only once it's used.

;; File foo.el

;;;###autoload
(defun foo ()
  "Doc"
  42)

(defun bar ()
  "Doc"
  43)

So, if I understand correctly the autoload functionnality is a hack to load file faster. But when I load foo.el, in order to skip the definition of foo the interpreter still has to read the whole form. I don't understand why it's faster.

回答1:

The simplest way to load the content of the file foo.el is to do a load. However, this is costly because you have to read the whole file.

With autoload you are allowed to tell emacs: "function foo is defined in file foo.el". This way emacs does not read the file and you do not pay the loading price. When you use the function foo for the first time, emacs will find the definition for you by reading foo.el.

The ;;;###autoload comment in your file is not doing anything for autoload by itself. You need to use a program that will grab all of these definitions and put them in a file foo-autoloads.el (or any other name). For each function it will put a line telling emacs which file contains it. Then in your .emacs you will load foo-autoloads.el instead of foo.el. foo.el will be read by emacs the first time you use function foo.

Note: require can also be used instead of load in the explanation above.



回答2:

Where did you get that "understanding" from? Certainly not the manual.

The autoload cookies are used to pull out skeletal function/variable definitions in a separate autoload file. This is usually done at compile time.

Without going through that compilation step the autoload cookies have no effect.



回答3:

The autoload forms have to be loaded separatly. They can be generated from the autoload cookies, but you can also write them manually. For example, in your .emacs you can put autoload forms for functions which you only use occasionally, so they are only loaded on demand.

For example:

(autoload 'ahk-mode "ahk-mode")


标签: emacs elisp