Emacs-在不使用EmacClient的情况下连接到守护进程(如果存在)

Emacs-在不使用EmacClient的情况下连接到守护进程(如果存在),emacs,emacsclient,Emacs,Emacsclient,如果我的系统上有作为守护进程运行的emacs,我可以使用EmacClient轻松地连接到它。这我知道。然而,我想知道的是,如果守护进程已经在运行,是否有方法告诉emacs(而不是EmacClient)像EmacClient一样运行 e、 g 我可以对init.el做些什么来复制这种行为吗?我不这么认为,但是通过使用带有空字符串的EmacClient作为--alternate editor选项,可以实现类似的效果吗?发件人: -命令 --alternate editor=命令 。 . . 作为一个

如果我的系统上有作为守护进程运行的emacs,我可以使用EmacClient轻松地连接到它。这我知道。然而,我想知道的是,如果守护进程已经在运行,是否有方法告诉emacs(而不是EmacClient)像EmacClient一样运行

e、 g


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

我不这么认为,但是通过使用带有空字符串的
EmacClient
作为
--alternate editor
选项,可以实现类似的效果吗?发件人:

-命令

--alternate editor=命令

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

您可以使用
EmacClient
执行
-a'
操作,但我和很多人所做的是使用某种脚本,它基本上完成了
EmacClient'
在多个步骤中所做的事情

我的版本类似于这个BASH脚本:您感兴趣的部分是
确保服务器正在运行
功能。这是脚本的“主要功能”,下面是
确保服务器正在运行
功能,其余的都在后面,这是出于好奇,但不会有助于回答问题

#!/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)))"
}

聚焦当前帧
会让操作系统将您置于当前的Emacs帧中。这是最重要的特性。对我来说,我在MacOS Automator应用程序中插入了一个经过修改的版本。当有一个emacs GUI框架时,执行Spotlight搜索“EmacsC”(通常只需键入“e”就足够了)将我放在emacs窗口中。这是一种切换到emacs窗口的超快速方式。

这不是我真正的问题,尽管这是其中的一部分。我更改了标题以更好地反映我真正想要的。
# 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)))"
}