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

发布于 2024-10-02 05:47:39 字数 281 浏览 4 评论 0原文

我正在升级到 emacs23。我发现我的 emacs.el 加载速度慢得多。

确实是我自己的错...我里面有很多东西。

因此,我也尝试自动加载 emacs.el 当前“需要”的所有可能内容。

我有一个公开 12 个入口点的模块 - 我可以调用的交互式函数。

为了确保无论我调用哪个函数都加载模块,正确的方法是调用 12 次 autoload 吗?这种方法有什么问题吗?它会带来性能问题吗?

如果不是那种方法,那么又会怎样呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕色琉璃 2024-10-09 05:47:39

您真正想要的是自动为您生成自动加载,以便您的 .emacs 文件保持原始状态。大多数包中已经有 ;;;###autoload 行,如果没有,您可以轻松添加它们。

要管理它,您可以将所有包放在一个目录中,例如 ~/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))

如果将 'update-autoloads-in-package-area 添加到 kill-emacs-hook 中,则 loaddefs.el 将每次退出 Emacs 时都会自动更新。

并且,为了将它们结合在一起,请将其添加到您的 .emacs 中:

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

现在,当您下载新包时,只需将其保存在 ~/emacs/lisp 目录中,通过 Mx update-autoloads-in-package-area 更新 loaddef(或退出 emacs),下次运行 Emacs 时即可使用。无需再更改 .emacs 来加载内容。

请参阅此问题以了解加速 Emacs 启动的其他替代方法: How can I make Emacs 启动速度更快?

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?

夜司空 2024-10-09 05:47:39

好吧,谁在乎它开始得有多慢呢?

之一进行连接

  • 通过 emacs --daemon & 启动它,然后使用emacsclient -c /some/file.ext
  • emacsclient -nw code>

我分别为它们创建了别名 emxemt。继续一次编辑会话变得更加理智......

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...

泛泛之交 2024-10-09 05:47:39

理想情况下,您的 .emacs 文件中不应该有任何 loadrequire

您应该使用autoload。 .

例如,

(autoload 'slime-selector "slime" t)

您将需要使用 eval-after-load 来执行任何特定于库的配置,但结果是您不需要等待所有这些预先加载,或者在不具有相同功能的 Emacs 版本上导致错误。 (例如基于终端,或不同的平台等)

虽然这现在可能不会影响您,但将来您可能会希望在使用 Emacs 的所有计算机/环境上使用相同的配置,所以这是一件非常好的事情让您的配置准备好飞行。

还可以使用 (start-server) 并使用 emacsclient 将外部文件打开到 Emacs - 这样就可以避免重新启动 Emacs。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文