1

因为这些天我在很多事情上都使用 Emacs,所以我只想在打开 ac/C++ 源代码或标头时加载 cedet.el,而不是每次启动 emacs 时加载,因为它会占用大量启动时间。

现在我的初始化文件的开头是这样的:

  (load-file "~/.emacs.d/plugins/cedet/common/cedet.el")

(semantic-load-enable-excessive-code-helpers)
;;(semantic-load-enable-semantic-debugging-helpers)

(setq senator-minor-mode-name "SN")
(setq semantic-imenu-auto-rebuild-directory-indexes nil)
(global-srecode-minor-mode 1)
(global-semantic-mru-bookmark-mode 1)

而且它还在继续。有没有办法做到这一点?

4

2 回答 2

3

在我学会使用eval-after-loadautoload.

如果您有一个只希望在打开该类型的文件时加载的模式,请在您的 .emacs 中添加类似的内容(假设 foo-mode 在加载路径上的 foo-mode.el 中定义):

(autoload 'foo-mode "foo-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.foo\\'" . foo-mode))

如果您有一些只希望在加载“主”库后加载的辅助库,请在 .emacs 中添加类似的内容(假设 bar-mode 是增强 foo-mode 的辅助模式):

(eval-after-load "foo-mode"
  '(progn
    (require 'bar-mode)
    ;; ... do other bar-mode setup here ...
    ))

因此,在您的情况下,您可能希望使用eval-after-load c++-mode.

于 2011-11-17T13:53:28.273 回答
0

你可以这样做:

(add-hook 'c-mode-common-hook (lambda ()
  (load-file "~/.emacs.d/plugins/cedet/common/cedet.el")
  ;; any code dependent on having this file loaded
))

如果多次加载文件(或执行其他命令)有问题,您当然应该首先检查该文件是否已经加载(测试 cedet.el 中定义的内容,或者自己维护一个 is-loaded 标志)。

编辑:这样的标志可能如下所示:

(setq need-to-load-cedet-p t)
(add-hook 'c-mode-common-hook (lambda ()
  (if need-to-load-cedet-p
    (progn (load-file "~/.emacs.d/plugins/cedet/common/cedet.el")
           (setq need-to-load-cedet-p nil))
    ;; code that should only be executed once after cedet is loaded goes here
  )
  ;; code that should be executed every time a new buffer is opened goes here
))
于 2011-11-15T22:33:51.067 回答