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

时间:2009-10-02 16:06:54

标签: shell bash

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

if emacs_server_is_running; then
    EDITOR=emacsclient
    VISUAL=emacsclient
else
    EDITOR=emacs
    VISUAL=emacs
fi

我将emacs_server_is_running功能用于实现此目标?

11 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:7)

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

emacsclient -ca false -e '(delete-frame)'

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

答案 2 :(得分:6)

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

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

知道这一点,为了测试服务器是否正在运行,我们可以这样做:

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

答案 3 :(得分:3)

我可以想到几种方法,它们都不是万无一失的:

function emacs_server_is_running()
{
    ps -ef | grep emacsserver | grep -v grep > /dev/null
}

function emacs_server_is_running()
{
    netstat -a | grep esrv > /dev/null
}

或者不是在shell启动时设置它(毕竟,服务器可能会死或服务器可能在您登录后启动),为什么不在您需要编辑的时候进行检查:

EDITOR=$HOME/emacs_try_client_first
VISUAL=$HOME/emacs_try_client_first

其中$HOME/emacs_try_client_first可以像

一样简单
#!/bin/sh
# emacs_try_client_first
emacsclient $@ || emacs $@

答案 4 :(得分:1)

我遇到了同样的问题。当我启动emacs时,它会检查是否已有服务器。如果这是emacs的第一个实例,它将以服务器模式启动。 然后,当我查看/编辑文件时,我想连接到服务器。这样就可以非常快速地打开文件,因为没有启动新的emacs实例。重用上面的@mob结果,我提出了以下工作解决方案:

(1).emacs

我的.emacs文件中有以下代码段:

;; start emacs-server if not running
    (add-hook 'after-init-hook
        (lambda ()
          (require 'server)
          (unless (server-running-p)
            (server-start))))

如果这是第一个emacs实例,它将以服务器模式启动。

(2).bashrc

#########################
# START: Emacs settings #
#########################
export EDITOR=/home/jabba/Dropbox/home/jabba/bin/emacs_try_client_first
export VIEWER=$EDITOR

alias vi=$EDITOR
alias e=vi  # i.e., $EDITOR
alias em=vi # same as above

export TERM="xterm-256color"
#######################
# END: Emacs settings #
#######################

我使用vim超过15年,所以当我想编辑文件时,我会自动编写vi file。由于我想切换到emacs,我定义了这个别名:)命令“vim”和“emacs”启动一个新实例。使用别名“vi”,“e”和“em”,您可以连接到正在运行的emacs实例(即服务器)。如果没有运行emacs服务器,将打开一个新实例(在服务器模式下)。

(3)emacs_try_client_first

#!/usr/bin/env bash

function emacs_server_is_running()
{
    ps ux | grep "\bemacs\b" | grep -v grep >/dev/null
}

if emacs_server_is_running; then
    emacsclient -n "$@" 2>/dev/null &
else
    emacs "$@" 2>/dev/null &
fi

它也适用于Midnight Commander,其中F3是视图,F4是编辑。在MC中,如果要使用emacs,请转到“配置”并关闭内部视图和内部编辑。

修改:添加了after宏。编辑#2:毕竟没有必要。

答案 5 :(得分:1)

试试这个。

emacs_server_is_running(){
    lsof -c emacs | grep server | tr -s " " | cut -d' ' -f8
}

答案 6 :(得分:1)

@madumlao在answer中提出了一个好主意。不过,我们可能会做得稍微好一些:

emacsclient -a false -e 't'

这并不需要打开一个新的框架,并在以下情况下工作:

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

答案 7 :(得分:0)

这是我的设置

#!/bin/sh
emacsclient -s IRC -c $* || (emacs --daemon=IRC && emacsclient -s IRC -c --eval "rcirc" $*)

它连接到IRC服务器并打开一个框架;如果服务器关闭,则启动它,运行elisp代码,然后再次尝试。

答案 8 :(得分:0)

我的解决方案基于Jabba公开的第一个解决方案,但是使用emacs --script,这对于我在bash脚本中具有emacs服务器状态(运行或未运行)非常有用。我将它用于多个emacs服务器,这就是为什么我有一个服务器名称的变量

server_ts="servername"

tmp_file=`mktemp --tmpdir=/tmp emacs-manager.XXXXXX`

echo ";;
(require 'server)
(progn (setq server-name \"$server_ts\")           
   (if     (server-running-p)
           (princ \"1\")
           (princ \"0\")))
" > $tmp_file
isrunning=$(emacs --script $tmp_file 2>/dev/null)
rm $tmp_file
echo "is running: $isrunning"

在执行此操作之前,我尝试查找套接字文件,并解析ps命令的输出。当计算机崩溃时,前者有时会失败,套接字文件仍然存在,但没有emacs进程。后者并不容易,因为emacs服务器可以通过多种方式启动。

答案 9 :(得分:0)

通过将以下内容添加到emacs(必须在您的路径中),我发现重新定义~/bin/emacs命令很方便:

#!/bin/sh
# if emacsclient can't connect to the daemon, try to launch it first
emacsclient -c "$@" || (/usr/bin/emacs --daemon; emacsclient -c "$@")

在此之后,只需键入emacs即可启动emacs,并在需要时自动启动服务器。

答案 10 :(得分:0)

  1. 在 emacs 中检查 server-socket-dir 的值。
  2. 检查这个目录下是否有一些文件。如果是,则服务器正在运行。

这是我用来将 org-pomodoro 计时器添加到我的 xfce 面板的示例:

#!/bin/bash

if [ -z "$(ls -A /run/user/1000/emacs/)" ]; then
   echo "Emacs server is down"
else
   emacsclient --eval '(pomodoro-string-for-menu-bar)'| sed 's/["()]//g'
fi