2009-10-02 75 views
9

我想让我的.bashrc检测是否运行emacsclient可以代替运行emacs。基本上:如何检测emacs-server是否从shell提示符运行?

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

我会在emacs_server_is_running函数中做什么来完成这个目标?

回答

9

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

-a,--alternate编辑器= EDITOR 如果Emacs的服务器未运行,运行指定的编辑器来代替。这也可以通过`ALTERNATE_EDITOR'环境变量来指定。 如果EDITOR的值是空字符串,那么Emacs以守护进程模式启动,emacsclient会尝试连接它。
+0

如果那是真的,那就不复存在了。 – yPhil 2018-03-08 18:35:55

+0

首先,这是真的,否则我就不会提到它。 其次,它仍然是。用'emacsclient --version' 25。3,帮助文本显示如下:“-a EDITOR,--alternate-editor = EDITOR编辑器,如果服务器没有运行,则编辑器退回。如果EDITOR是空字符串,请在守护进程模式下启动Emacs并尝试再次连接。 – 2018-03-08 19:50:36

2

我能想到几种方法的,他们都没有做到万无一失:

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 
} 

,而不是设置此功能时,外壳程序启动(毕竟,服务器可能会死或服务器能够或启动您登录后),为什么不把当时你需要编辑一些检查:

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

其中$HOME/emacs_try_client_first可以像

一样简单
#!/bin/sh 
# emacs_try_client_first 
emacsclient [email protected] || emacs [email protected] 
+2

你不需要做'grep -v grep' - 你可以让你的第一个看起来像这样:'grep [e] macsserver'这通常是在对'ps'的输出进行清理时完成的。 – 2009-10-02 17:08:39

+0

@DW你知道吗?每个人总是抱怨这个'| grep -v grep'成语,但现在终于有人做了一些事情! – mob 2009-10-03 01:30:19

4

从一个shell脚本,你可以做这样的事情:

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

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

0

这里是我的设置

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

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

5

我阅读了答案,但没有一个符合我的用例,我不想在不运行的情况下启动服务器,并且还希望避免永远阻塞。

望着在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"

+0

正是我一直在寻找的东西! – 2015-12-17 11:52:50

+0

这是有效的,但是依赖(可能是安全的)这些变量('server-auth-dir'或'server-socket-dir')没有被改变它们的默认值。 [ocodo的回答](https://stackoverflow.com/a/28561282/915044)(下面)是更好的恕我直言,因为它会找到开放的套接字,无论用户设置的值如何。 – TomRoche 2017-10-04 00:06:19

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 "[email protected]" 2>/dev/null & 
else 
    emacs "[email protected]" 2>/dev/null & 
fi 

它还午夜指挥官,其中F3是视图工作得很好,F4是编辑。在MC中,如果您想使用emacs,请转到配置并关闭内部视图和内部编辑。

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

0

我的解决方案基于Jabba公开的第一个解决方案,但使用emacs --script,这对我在bash脚本中具有emacs服务器状态(运行或不运行)很有用。我用这个多emacs的服务器,这就是为什么我这样做,我试图寻找套接字文件,并解析ps命令的输出之前对服务器名称

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" 

的变量。前者有时会在计算机崩溃时失败,套接字文件仍然存在,但没有emacs进程。后者并不容易,因为emacs服务器可以以各种方式启动。

1

试试这个。

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

,我觉得是很方便的通过添加以下到~/bin/emacs(必须是在你的路径)重新定义emacs命令:

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

这是到位后,可以简单地输入emacs到启动emacs,如果需要,服务器将自动启动。

相关问题