2009-10-02 36 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,如果需要,服務器將自動啓動。

相關問題