50

我想使用 emacsclient 在 Mutt 中编辑电子邮件。

我在 .emacs 中添加了这个

(server-start)

在 .muttrc 我添加了

set editor="emacsclient -nw %s"

看来他们工作。当我启动第二个 Emacs 时,它抱怨已经有一个服务器在运行,所以它会发出错误。如何确保(server-start)仅在服务器尚未启动时才执行?

谢谢

4

5 回答 5

68

此代码仅在服务器未运行时启动服务器:

(load "server")
(unless (server-running-p) (server-start))
于 2011-04-06T19:35:49.313 回答
54

emacs 守护进程可以以非常简单的方式自动启动。只需将其添加到您的 .bashrc/.zshrc/whatever

export ALTERNATE_EDITOR=""

现在,当您调用emacsclient(使用--tty或)时,如果服务器尚未运行,--create-frame则将启动(使用)。emacs --daemon

我还发现这个 shell 别名很方便:

alias e='emacsclient --tty'

请注意,从 Emacs 23 开始,这是在守护程序模式下使用 Emacs 的首选方式。(start-server)现在大部分已被弃用。

于 2011-04-07T09:14:59.733 回答
15

答案有点晚,但这是适合我的解决方案。每当我启动 emacsclient 时,我都会使用emacsclient -a '' -c The-a ''告诉 emacsclient 尝试连接到现有服务器,如果不存在服务器,则启动一个然后连接到它。

于 2014-02-04T14:59:12.573 回答
8

通过以下方式共同避免问题

emacs --daemon

在任何 shell 或终端中,以便 Emacs 在后台运行。这种方式emacsclient总是很愉快,因为总是有一个 Emacs 服务器可以连接。

这是 Emacs,还有一个仅在需要时启动服务器的功能,但我现在不太记得它的名称。--daemon我自己很高兴地使用选项。

于 2011-04-06T17:27:04.593 回答
1

将此添加到您的.bashrc/.zshrc

if ! ps -e -o args | grep -q '^emacs --daemon$'; then
  emacs --daemon
else
  echo "Emacs server Online"
fi

更新:现在我更喜欢使用这条线:

if ! ps -e -o args | grep -i 'emacs' | grep 'daemon'; then

因为进程名称将取决于您的机器或安装 emacs 的方式。


现在您的 shell 将在启动时启动守护程序,但前提是它尚未运行。首次运行时等待时间更短emacsclient -t,并且比让emacs --daemon检查它是否已经运行更快。

作为替代方案,您可以简单地添加:

eval 'emacsclient -e "(server-running-p)"'
于 2021-02-23T19:47:13.140 回答