emacs23 / elisp: how to properly autoload this library?

回眸只為那壹抹淺笑 提交于 2019-11-27 09:47:29

问题


I am upgrading to emacs23. I find that my emacs.el loads much more slowly.

It's my own fault really... I have a lot of stuff in there.

So I am also trying to autoload everything possible that is currently "required" by my emacs.el.

I have a module that exposes 12 entry points - interactive functions I can call.

Is the correct approach to have 12 calls to autoload in order to insure that the module is loaded regardless of which function I call? Are there any problems with this approach? Will it present performance issues?

If not that approach, then what?


回答1:


What you really want is to get the autoloads generated for you automatically, so that your .emacs file remains pristine. Most packages have the ;;;###autoload lines in them already, and if not, you can easily add them.

To manage this, you can put all the packages in a directory, say ~/emacs/lisp, and in there have a file named update-auto-loads.el which contains:

;; put this path into the load-path automatically
;;;###autoload
(progn
  (setq load-path (cons (file-name-directory load-file-name) load-path)))

;;;###autoload
(defun update-autoloads-in-package-area (&optional file)
  "Update autoloads for files in the diretory containing this file."
  (interactive)
  (let ((base (file-truename
       (file-name-directory
        (symbol-file 'update-autoloads-in-package-area 'defun)))))
(require 'autoload)         ;ironic, i know
(let ((generated-autoload-file (concat base "loaddefs.el")))
  (when (not (file-exists-p generated-autoload-file))
    (with-current-buffer (find-file-noselect generated-autoload-file)
      (insert ";;") ;; create the file with non-zero size to appease autoload
      (save-buffer)))
  (cd base)
  (if file
      (update-file-autoloads file)
    (update-autoloads-from-directories base)))))

;;;###autoload
(defun update-autoloads-for-file-in-package-area (file)
  (interactive "f")
  (update-autoloads-in-package-area file))

If you add 'update-autoloads-in-package-area to your kill-emacs-hook, then the loaddefs.el will automatically be updated every time you exit Emacs.

And, to tie it all together, add this to your .emacs:

(load-file "~/emacs/lisp/loaddefs.el")

Now, when you download a new package, just save it in the ~/emacs/lisp directory, update the loaddefs via M-x update-autoloads-in-package-area (or exit emacs), and it'll be available the next time you run Emacs. No more changes to your .emacs to load things.

See this question for other alternatives to speeding up Emacs startup: How can I make Emacs start-up faster?




回答2:


Well, who cares how slowly it starts?

Fire it up via emacs --daemon & and then connect using either one of

  • emacsclient -c /some/file.ext, or
  • emacsclient -nw

I created aliases for both these as emx and emt, respectively. Continuing once editing session is so much saner...




回答3:


Ideally you shouldn't have any load or require in your .emacs file.

You should be using autoload instead...

e.g.

(autoload 'slime-selector "slime" t)

You will need to use eval-after-load to do any library specific config, but the upshot is that you won't need to wait for all this to load up front, or cause errors on versions of Emacs that don't have the same functionality. (e.g. Terminal based, or a different platform etc.)

While this may not affect you right now, chances are, in future you will want to use the same config on all machines / environments where you use Emacs, so it's a very good thing to have your config ready to fly.

Also use (start-server) and open external files into Emacs using emacsclient - So you avoid restarting Emacs.



来源:https://stackoverflow.com/questions/4189159/emacs23-elisp-how-to-properly-autoload-this-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!