如何从shell提示符中检测到emacs-server正在运行?

Chr*_*s R 11 bash shell

我想让我的.bashrc检测运行emacsclient是否可以代替运行emacs.基本上:

if emacs_server_is_running; then
    EDITOR=emacsclient
    VISUAL=emacsclient
else
    EDITOR=emacs
    VISUAL=emacs
fi
Run Code Online (Sandbox Code Playgroud)

我将在emacs_server_is_running实现这一目标的功能中加入什么?

Kir*_*ser 11

你这太难了.从emacsclient(1)手册页:

-a, - alternate-editor = EDITOR如果Emacs服务器未运行,则改为运行指定的编辑器.这也可以通过`ALTERNATE_EDITOR'环境变量来指定.如果EDITOR的值是空字符串,则Emacs以守护进程模式启动,emacsclient将尝试连接到它.


mad*_*lao 8

从shell脚本中,您可以执行以下操作:

emacsclient -ca false -e '(delete-frame)'
Run Code Online (Sandbox Code Playgroud)

这将打开一个新的emacs窗口,然后快速关闭它,成功时返回true.如果不存在emacs服务器,则备用编辑器为/ bin/false,返回false.


Ily*_*rov 8

@madumlao 在他的回答中提出了一个好主意。不过,我们可能会做得更好一些:

emacsclient -a false -e 't'
Run Code Online (Sandbox Code Playgroud)

这不需要打开新框架,并且适用于以下情况:

sh-4.4$ emacsclient -a false -e '(delete-frame)'
*ERROR*: Terminal type "dumb" is not powerful enough to run Emacs
Run Code Online (Sandbox Code Playgroud)


Mar*_*189 6

我读了答案,但这些都没有适合我的用例,我不想在没有运行的情况下启动服务器,也想避免永远阻塞.

查看server-force-stopemacs 中的函数,它只是删除服务器文件中的任何一个server-auth-dirserver-socket-dir在我的情况下是/tmp/emacs1000/server~/.emacs.d/server.

知道这一点,要测试服务器是否正在运行,我们可以简单地做:

test -e "/tmp/emacs1000/server" || test -e "~/.emacs.d/server"