Windows 下脚本启动 Emacs

文章目录

  1. 1. 实现
    1. 1.1. 启动脚本
    2. 1.2. 右键使用 emacs
    3. 1.3. 设置 emacs 服务文件环境变量
  2. 2. 使用

编写这个脚本有 2 个目的:

  1. 能通过脚本启动 emacs 服务,但不打开任何文件。
  2. 能通过鼠标右键使用 emacs 打开文件,如果 emacs 服务没有启动,则启动 emacs 服务 并打开文件;如果 emacs 服务启动了,则使用 emacs 客户端打开文件,此时只有一个 emacs 程序运行。

实现

启动脚本

#@echo off
call "d:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat"

set emacs=d:\emacs-25
if exist %1 goto hasfile
goto nofile

:hasfile
start %emacs%\bin\emacsclientw.exe --no-wait -alternate-editor=%emacs%\bin\runemacs.exe %1
goto end

:nofile
start %emacs%\bin\runemacs.exe --no-splash %*

:end

解释一下,脚本首先要判断是否有参数。

如果有,说明要打开文件,所以调用

start %HOME%\bin\emacsclientw.exe --no-wait -alternate-editor=%HOME%\bin\runemacs.exe %1

  • –no-wait 告诉 client 不用等到 server 的返回,而是直接退出
  • –alternate-editor 当 server 不存在时,变为使用 runemacs 这个程序去打开文件,即相当于启动了一个 server,当然此时的 init 文件中有`(server-start)`这个语句。

如果没有参数,说明只启动 emacs 服务,所以调用

start %HOME%\bin\runemacs.exe --no-splash %*

使用 runemacs 启动 emacs 服务,`–no-splash`参数指定无启动闪屏。

右键使用 emacs

打开注册表编辑器。在启动栏中运行 regedit

在注册表中添加以下键值

HKEY_CLASSES_ROOT\*\shell\Edit With Emacs\command

修改 Default 值为:

"d:\emacs-25\bin\emacs.bat"  "%1"

设置成功之后,在需要打开的文件上右键菜单,就会出现 Edit With Emacs 这个菜单项。

设置 emacs 服务文件环境变量

打开环境变量设置,添加 EMACS_SERVER_FILE 环境变量,设置为 d:\home\.emacs.d\server\server

使用

将上面的脚本保存成 emacs.bat,并将此脚本路径放到环境变量 PATH 中。

在启动栏中输入 emacs.bat 就能启动 emacs 了。


Render by hexo-renderer-org with Emacs 25.0.50.1 (Org mode 8.2.10)

版权声明

本博客 采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可