【问题标题】:Emacs: load-file .emacs when savedEmacs:保存时加载文件 .emacs
【发布时间】:2015-01-21 12:50:34
【问题描述】:

我是 emacs 和 lisp 的新手。

我想在保存点文件时自动加载它。意思是,当我保存我的 .emacs 文件时,它会自动调用 load-file (因此如果我搞砸了,我会立即知道)。

但我看不到能够在 emacs 中找到有关钩子的综合教程。

这是我想出的:

(defun load-init-after-save ()
  "After saving this file, load it"
  (if (eq bname this) ('load-file this) (nil))
)
(add-hook 'after-save-hook 'load-init-after-save)

当然,这是不正确的:bnamethis 只是占位符。而且我不希望这个函数在所有保存时都运行,就在 .emacs 文件被保存时。

有人知道怎么做吗?有没有更好、更简单的方法?

【问题讨论】:

    标签: emacs buffer hook


    【解决方案1】:

    方式 1

    一种方法是从 MELPA 安装 auto-compile-mode 并启用它:

    (defun my-emacs-lisp-hook ()
      (auto-compile-mode 1))
    
    (add-hook 'emacs-lisp-mode-hook 'my-emacs-lisp-hook)
    

    现在每次你保存一个字节编译的 Elisp 文件时,它将是 重新编译。编译通常会捕获一些严重的错误。

    要编译任何 Elisp 文件,请在 dired 中选择它 (C-x d) 并按 B (dired-do-byte-compile)。

    方式 2

    使用我编写的自定义代码。

    (defun test-emacs ()
      (interactive)
      (require 'async)
      (async-start
       (lambda () (shell-command-to-string "emacs --batch --eval \"(condition-case e (progn (load \\\"~/.emacs\\\") (message \\\"-OK-\\\")) (error (message \\\"ERROR!\\\") (signal (car e) (cdr e))))\""))
       `(lambda (output)
          (if (string-match "-OK-" output)
              (when ,(called-interactively-p 'any)
                (message "All is well"))
            (switch-to-buffer-other-window "*startup error*")
            (delete-region (point-min) (point-max))
            (insert output)
            (search-backward "ERROR!")))))
    
    (defun auto-test-emacs ()
      (when (eq major-mode 'emacs-lisp-mode))
      (test-emacs))
    
    (add-hook 'after-save-hook 'auto-test-emacs)
    

    这将在您每次运行时在后台启动一个新的 Emacs 实例 保存文件。如果出现问题,它会抱怨。 第二种方法使用async

    如果你真的想为.emacs 用户这样做只是

    (defun auto-test-emacs ()
      (when (and (eq major-mode 'emacs-lisp-mode)
                 (equal (file-truename user-init-file)
                        (expand-file-name
                         buffer-file-truename)))
        (test-emacs)))
    

    【讨论】:

    • 是的,auto-compile 非常棒。我彻底推荐使用它。
    • abo-abo:你应该链接到async
    • 听起来这会自动编译我保存的任何 lisp 文件。我只想在保存.emacs 文件(并且只有它)时重新编译它。这目前可以工作(因为我没有修改任何其他 lisp 文件)但最终不会好。
    【解决方案2】:

    以下代码会在保存后加载您的.emacs~/.emacs.d/init.el 文件:

    (defun my-load-user-init-file-after-save ()
      (when (string= (file-truename user-init-file)
                     (file-truename (buffer-file-name)))
        (let ((debug-on-error t))
          (load (buffer-file-name)))))
    
    (add-hook 'after-save-hook #'my-load-user-init-file-after-save)
    

    由于您的预期用途是错误检查,因此代码还会在加载 init 文件时启用调试器,以便在出现错误时获得良好的回溯。

    对于您的初始化文件的错误检查,您可能还会发现Flycheck 很有用。它使用字节编译器即时检查您的 init 文件,突出显示缓冲区中的任何错误和警告,并且(可选)为您提供所有错误和警告的列表。

    免责声明:我是这个库的维护者。

    【讨论】:

    • 是的,这正是我所需要的。谢谢!我还要看看你的 Flycheck。
    • @Nacht 不客气。如果您尝试 Flycheck,我很想听听一些关于它的反馈。特别是,我想知道设置说明是否有帮助,以及您成功启动和运行它的效果如何。
    猜你喜欢
    • 1970-01-01
    • 2011-02-05
    • 2012-08-26
    • 1970-01-01
    • 2015-06-11
    • 2013-12-30
    • 2012-10-15
    • 2014-05-02
    • 1970-01-01
    相关资源
    最近更新 更多