4

如果我的系统上有 emacs 作为守护程序运行,我可以使用 emacsclient 轻松连接到它。这个我知道。但是,我想知道的是,如果守护程序已经在运行,是否有办法告诉 emacs(不是 emacsclient)表现得像 emacsclient?

例如

# emacs daemon is not running
emacs # should start a new frame

# ...

# emacs daemon IS running
emacs # should actually behave like emacsclient, i.e. connect to my daemon

我可以对我的 init.el 做些什么来复制这种行为吗?

4

2 回答 2

11

我不这么认为,但是您可以通过使用emacsclient空字符串作为--alternate-editor选项来实现类似的效果吗?来自http://www.gnu.org/s/libtool/manual/emacs/emacsclient-Options.html#emacsclient-Options

-a command

--alternate-editor=command

. . . 作为一个特殊的例外,如果 command 是空字符串,那么 emacsclient 会以守护程序模式启动 Emacs,然后再次尝试连接。

于 2011-11-30T16:07:46.483 回答
0

你可以用它来做这-a ''件事,emacsclient但我和很多人所做的是拥有某种脚本,它基本上可以emacsclient ''在多个步骤中完成。

我的版本类似于这个 BASH 脚本:您感兴趣的部分是ensure-server-is-running函数。这是脚本的“主要功能”,接下来是该ensure-server-is-running功能,其余的是出于您的好奇心,但无助于回答问题。

#!/bin/bash
# ec.sh
#
# [function definitions]
#

ensure-server-is-running
ensure-frame-exists

focus-current-frame

确保服务器正在运行

# ec.sh function definition
# From https://emacs.stackexchange.com/a/12896/19972
function server-is-running() {
    emacsclient -e '(+ 1 0)' > /dev/null 2>&1
}

function ensure-server-is-running(){
    if ! server-is-running ; then
        echo "Need to start daemon, press enter to continue, C-c to abort"
        read
        emacs --daemon
    fi
}

还有另外两个功能:

# ec.sh function definition
# From https://superuser.com/a/862809
function frame-exists() {
    emacsclient -n -e "(if (> (length (frame-list)) 1) 't)" 2>/dev/null | grep -v nil >/dev/null 2>&1
}

function ensure-frame-exists() {
    if ! frame-exists ; then
        emacsclient -c --no-wait
    fi
}

# From https://emacs.stackexchange.com/a/54139/19972
function focus-current-frame() {
    # Doesn't work a frame exists and is in a terminal
    emacsclient --eval "(progn (select-frame-set-input-focus (selected-frame)))"
}

focus-current-frame是让操作系统将您置于当前 Emacs 框架中的东西。这是最重要的特点。对我来说,我将它的改编版本插入到 MacOS Automator 应用程序中。当有一个 emacs GUI 框架时,执行 Spotlight Search“EmacsC”(通常只需输入“e”就足够了),将我放在我的 emacs 窗口中。这是切换到 emacs 窗口的超快速方式。

于 2021-05-01T15:27:59.450 回答