在 Windows 下只启动一个 emacs 进程

下载的 emacs 包里,有两个用得着的可执行文件:runemacs.exe、emacsclientw.exe。平时运行 runemacs.exe 就可以启动 emacs 了,但每运行一次都打开一个新的窗口,文件一多,屏幕上就会很乱。为了让每次都在已经打开的 emacs 中新开一个 buffer 来处理文件,需要运行 emacs 的 server 模式。

1.设置环境变量

新建注册表项:HKEY_LOCAL_MACHINE\SOFTWARE\GNU\Emacs,新建字符串键 HOME,值设置为你自己的 HOME 目录就可以,如 E:\MyDoc

2.编辑 .emacs 文件

在 emacs 里 C-x C-f ~/.emacs,然后写入:

(server-start)
(add-hook 'kill-emacs-hook
(lambda()
(if (file-exists-p "~/.emacs.d/server/server")
(delete-file "~/.emacs.d/server/server"))))

3.创建 myemacs.vbs,这是最后我们调用的文件

Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
 
dim runcmd
 
if fso.FileExists(WshShell.ExpandEnvironmentStrings("E:\MyDoc\.emacs.d\server\server")) then
    runcmd = "d:\emacs-22.2\bin\emacsclientw.exe"
else
    runcmd = "d:\emacs-22.2\bin\runemacs.exe"
end if
 
if WScript.Arguments.Count > 0 then
    runcmd = runcmd & " """ & WScript.Arguments.Item(0) & """"
end if
 
WshShell.run runcmd
 
set WshShell = nothing
set fso = nothing

上面 emacs 的 bin 和 E:\MyDoc 目录要根据自己的情况修改。

4.把 myemacs.vbs 的快捷方式放到发送菜单里,或者以自己的方式运行 “myemacs.vbs 文件名” 就可以了。

运行之前最好重新启动一下。