Search code examples
emacsmelpa

Emacs replacing require with autoload


After profiling my Emacs init file, I saw that many of my modes are taking a long time to load, explaining why I've been having slow start times.

I am trying to use after-load or autoload to decrease the load time but have been unsuccessful in many modes.

For example, I have a mode called multiple-cursors.el that I downloaded manually and placed in my .emacs.d directory. Here is the code I have now:

;; Multiple Cursors                                                               
(add-to-list 'load-path "~/.emacs.d/multiple-cursors.el/")
(require 'multiple-cursors)  
(global-set-key (kbd "C-c c") 'mc/edit-lines)                                     
(global-set-key (kbd "C-c .") 'mc/mark-next-like-this)
(global-set-key (kbd "C-c ,") 'mc/mark-previous-like-this)
(global-set-key (kbd "C-c /") 'mc/mark-all-like-this)

I tried to replace the require line with (autoload 'multiple-cursors-mode "multiple-cursors.el" "Multiple cursors mode") but that did not work.

This format of the autoload seems to work well only with Melpa-installed packages. How can I do the equivalent for manually installed packages, such as the example above?


Solution

  • You need to write autoloads for the functions that you are actually calling through the key bindings (i.e. mc/edit-lines, mc/mark-next-like-this, mc/mark-previous-like-this and mc/mark-all-like-this), since that's how the loading of the file is triggered. The autoloads need to refer to the files where the respective functions are defined, which is mc-edit-lines for mc/edit-lines, and mc-mark-more for the others.

    So after setting the load path and binding the keys, add something like this:

    (autoload 'mc/edit-lines "mc-edit-lines" "" t)
    (autoload 'mc/mark-next-like-this "mc-mark-more" "" t)
    (autoload 'mc/mark-previous-like-this "mc-mark-more" "" t)
    (autoload 'mc/mark-all-like-this "mc-mark-more" "" t)