简体   繁体   English

emacs23 / elisp:如何正确自动加载这个库?

[英]emacs23 / elisp: how to properly autoload this library?

I am upgrading to emacs23. 我正在升级到emacs23。 I find that my emacs.el loads much more slowly. 我发现我的emacs.el加载速度要慢得多。

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. 所以我也试图自动加载我的emacs.el目前“必需”的一切可能。

I have a module that exposes 12 entry points - interactive functions I can call. 我有一个公开12个入口点的模块 - 我可以调用的交互式功能。

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? 是否有正确的方法有12个autoload调用,以确保模块加载,无论我调用哪个函数? Are there any problems with this approach? 这种方法有什么问题吗? Will it present performance issues? 它会出现性能问题吗?

If not that approach, then what? 如果没有这种方法,那又怎样?

What you really want is to get the autoloads generated for you automatically, so that your .emacs file remains pristine. 您真正想要的是自动为您生成自动加载,以便您的.emacs文件保持原始状态。 Most packages have the ;;;###autoload lines in them already, and if not, you can easily add them. 大多数软件包已经包含;;;###autoload行,如果没有,您可以轻松添加它们。

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: 要管理它,您可以将所有软件包放在一个目录中,比如~/emacs/lisp ,并且有一个名为update-auto-loads.el的文件,其中包含:

;; 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. 如果将'update-autoloads-in-package-areakill-emacs-hook ,则每次退出Emacs时都会自动更新loaddefs.el

And, to tie it all together, add this to your .emacs : 并且,要将它们组合在一起,请将其添加到.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 Mx update-autoloads-in-package-area (or exit emacs), and it'll be available the next time you run Emacs. 现在,当您下载新软件包时,只需将其保存在~/emacs/lisp目录中,通过Mx update-autoloads-in-package-area (或退出emacs)更新loaddef,它将在下次可用你运行Emacs。 No more changes to your .emacs to load things. 不再对.emacs进行更改以加载内容。

See this question for other alternatives to speeding up Emacs startup: How can I make Emacs start-up faster? 有关加速Emacs启动的其他替代方案,请参阅此问题: 如何让Emacs启动更快?

Well, who cares how slowly it starts? 嗯,谁在乎它开始的缓慢程度?

Fire it up via emacs --daemon & and then connect using either one of 通过emacs --daemon &启动它,然后使用其中一个连接

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

I created aliases for both these as emx and emt , respectively. 我分别为emxemt创建了这些别名。 Continuing once editing session is so much saner... 继续一次编辑会议是如此理智...

Ideally you shouldn't have any load or require in your .emacs file. 理想情况下,您的.emacs文件中不应有任何loadrequire

You should be using autoload instead... 你应该使用autoload ...

eg 例如

(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. 您将需要使用eval-after-load来执行任何特定于库的配置,但结果是您不需要等待所有这些都在前面加载,或者导致错误的Emacs版本没有相同的功能。 (eg 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. 虽然现在这可能不会对您产生影响,但是将来您可能希望在使用Emacs的所有机器/环境中使用相同的配置,因此准备好配置是一件非常好的事情。

Also use (start-server) and open external files into Emacs using emacsclient - So you avoid restarting Emacs. 还可以使用(start-server)使用emacsclient外部文件打开到Emacs中 - 这样就可以避免重新启动Emacs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM