with-emacs: 在独立的 Emacs 进程中运行 elisp 代码

1. 增加 with-emacs-server 宏(有一段时间了,感谢 @xuchunyang 的建议)

如果对运行环境不是很严苛,不必每次都启动一个全新的进程,那么用 server 可以节省不少时间:

(with-emacs-server "foo"
  :ensure t ;; 可选参数,确保 foo 存在,不存在则就地创建
  (1+ 1))

2. 给 with-emacs-server 增加超时退出机制(昨天刚想到)

这样就可以任意使用,不必担心"僵尸"进程了:

(with-emacs-server "foo"
  :ensure t
  :timeout 100 ;; 空闲 100 分钟自动退出
  (1+ 1))

或者设置默认退出时间:

(setq with-emacs-server-timeout 100)
(with-emacs-server "foo"
  :ensure t
  (1+ 1))

但仍然可以继续使用 :timeout 参数来覆盖默认值,比如当临时需要不同的超时时间或禁止超时退出:

(with-emacs-server "foo"
  :ensure t
  :timeout nil
  (1+ 1))

EDIT: 可以考虑把 :ensure t 也设成可默认

2 个赞