【发布时间】:2014-07-10 17:33:30
【问题描述】:
我正在寻找在 shell 模式下启动 emacs 的方法,并且我希望客户端连接到封闭的 emacs 实例,因此我不需要切换到运行服务器的实例来编辑文件。
我觉得这是可行的,因为 magit 已经在做这样的事情:提交消息由 emacsclient 编辑,并且总是在封闭的实例中打开。我什至不需要显式启动服务器!
我的主要用途是从 shell 模式执行 git commit。我不能依赖 magit,因为我必须使用一些带有特殊命令的自定义 git 版本。无论如何,我认为这将是一件很酷的事情。
更新:
我在这里找到了 magit 函数 magit-with-emacsclient 可以解决问题:
https://github.com/magit/magit/blob/master/magit.el#L1979
任何将函数转换为可以充当 git 默认编辑器的 shell 脚本的帮助将不胜感激!如果可以的话,我也会尝试发布代码。
更新 2:
这是我的解决方案。它需要预先在套接字名称中使用 pid 启动服务器,然后将您的 $EDITOR 设置为 emacsclient 连接到该服务器。
把它放在你的 init.el 中
; start a server with a pid
(require 'server)
(defun server-start-pid ()
(interactive)
(when (server-running-p server-name)
(setq server-name (format "server%s" (emacs-pid)))
(when (server-running-p server-name)
(server-force-delete server-name)))
(server-start))
并在shell-mode 中启动编辑器之前运行server-start-pid。
将此脚本emacsclient-pid 放入您的PATH。它递归查找父进程,直到找到 emacs 实例并在正确的套接字上调用 emacsclient:
#! /usr/bin/python
import os, sys
import psutil
def get_pid(name):
pid = os.getppid()
while True:
proc = psutil.Process(pid)
if name in proc.name():
break
pid = proc.ppid()
return pid
if __name__ == '__main__':
pid = get_pid("emacs")
# pass the argument to emacsclient
args = ' '.join(sys.argv[1:])
cmd = "emacsclient -s server{pid} {args}".format(**globals())
print cmd
os.system(cmd)
并将其放入您的.bashrc
if [[ $EMACS"x" == tx || $TERM"x" == dumbx ]]; then
export PAGER=/bin/cat
export EDITOR='emacsclient-pid'
else
export PAGER=/usr/bin/less
export EDITOR='emacs -q -nw'
fi
【问题讨论】:
-
您的目标是在 Emacs shell 模式缓冲区内运行一个 mini-Emacs 实例(emacsclient)吗?
-
请说明你想做什么。您可以在
shell-mode中输入emacsclient somefile,但不清楚您为什么要这样做。您不能在shell-mode中运行emacsclient -nw,但可以在ansi-term中运行。 -
为什么不直接从
shell-mode做git commit ...?不清楚为什么需要emacsclient。 -
FWIW,你可能会让 magit 为你发出正确的命令行。
-
嗨@lawlist:是的,这就是我的目标。
标签: emacs emacsclient