Emacs - 连接到守护进程(如果存在)而不使用 emacsclient

发布于 2024-12-18 22:55:27 字数 375 浏览 4 评论 0原文

如果我的系统上有 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 做些什么来复制这种行为吗?

If I have emacs running as a daemon on my system, I can connect to it easily using emacsclient. This I know. However, what I would like to know is, is there a way to tell emacs (not emacsclient) to behave like emacsclient if a daemon is already running?

e.g.

# 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

Is there anything I can do to my init.el to replicate this kind of behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬色的秋千 2024-12-25 22:55:27

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

-a 命令

--alternate-editor=命令




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

I don't think so, but can you achieve a similar effect by using emacsclient with an empty string as the the --alternate-editor option? From http://www.gnu.org/s/libtool/manual/emacs/emacsclient-Options.html#emacsclient-Options:

-a command

--alternate-editor=command

.
.
.
As a special exception, if command is the empty string, then emacsclient starts Emacs in daemon mode and then tries connecting again.

失退 2024-12-25 22:55:27

您可以使用 emacsclient 执行 -a '' 操作,但我和很多人所做的就是使用某种脚本来基本上执行 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 Frame 中。这是最重要的特征。对我来说,我将其改编版本插入到 MacOS Automator 应用程序中。当有 emacs GUI 框架时,执行 Spotlight 搜索“EmacsC”(通常只需键入“e”就足够了),将我置于 emacs 窗口中。这是切换到 emacs 窗口的超快速方法。

You can do the -a '' thing with emacsclient but what I do and a lot of people do is to have some kind of script that basically does what emacsclient '' does in multiple steps.

My version is something like this BASH script: The part you are interested in is the ensure-server-is-running function. This is the "main function" of the script, what follows is the ensure-server-is-running function and the rest is there after that for your curiosity but does not contribute to answering the question.

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

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

focus-current-frame

Ensuring that the server is running

# 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
}

And the other two function:

# 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 is something that will make the OS put you in the current Emacs Frame. This is the most important feature. For me I insert an adapted version of this into a MacOS Automator app. When there is an emacs GUI frame, doing Spotlight Search "EmacsC" (usually just typing the "e" is enough), puts me in my emacs window. It's a super fast way of switching to an emacs window.

眼泪也成诗 2024-12-25 22:55:27

这就是我所做的。这就像 @philippe-carpin 的解决方案,从某种意义上说,它是一个执行多个步骤的脚本:

  • 如果 emacs 守护进程未运行:启动它,创建一个框架
  • 如果守护进程但没有框架:创建一个框架
  • 如果守护进程有框架:焦点 在所有情况下,您都可以传递

将打开的文件名。

另外,我的脚本只尝试运行一段 elisp 一次,如果没有守护进程,它不会启动 emacs 进程来运行该 elisp。所以应该会稍微快一些。

get_emacs_daemon_state () {
    emacs_get_state_script='(if (> (length (frame-list)) 1) "daemon-with-frame" "daemon-no-frame")'
    emacsclient -e "$emacs_get_state_script" -a "echo no-daemon" 2>/dev/null |
        tr -d \" | cut -d' ' -f1
}

state=$(get_emacs_daemon_state)
create_frame_arg=""

if [[ $state = no-daemon ]]; then
    emacs --daemon
fi
if [[ $state != daemon-with-frame ]]; then
    create_frame_arg="--create-frame"
fi

client="emacsclient --no-wait $create_frame_arg"
if [[ $# -gt 0 ]]; then
    # open files passed as arguments
    $client "$@"
else
    # if no file passed, we just focus the frame
    $client --eval "(select-frame-set-input-focus (selected-frame))" >/dev/null
fi

Here's what I do. It's like the solution by @philippe-carpin, in the sense that it's a script that does multiple steps:

  • If emacs daemon is not running: start it, create a frame
  • If daemon but no frame: create a frame
  • If daemon with frame: focus that frame

In all cases you can pass filename(s) that will be opened.

In addition, my script only tries to run a piece of elisp just once and if there's no daemon it will not start an emacs process to run that elisp. So it should be slightly faster.

get_emacs_daemon_state () {
    emacs_get_state_script='(if (> (length (frame-list)) 1) "daemon-with-frame" "daemon-no-frame")'
    emacsclient -e "$emacs_get_state_script" -a "echo no-daemon" 2>/dev/null |
        tr -d \" | cut -d' ' -f1
}

state=$(get_emacs_daemon_state)
create_frame_arg=""

if [[ $state = no-daemon ]]; then
    emacs --daemon
fi
if [[ $state != daemon-with-frame ]]; then
    create_frame_arg="--create-frame"
fi

client="emacsclient --no-wait $create_frame_arg"
if [[ $# -gt 0 ]]; then
    # open files passed as arguments
    $client "$@"
else
    # if no file passed, we just focus the frame
    $client --eval "(select-frame-set-input-focus (selected-frame))" >/dev/null
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文