OSX 下 Emacs 伺服器的基本設置

OSX 下 Emacs 伺服器的基本設置

我最近購買了十年來的第一台 Mac,並且正在對其進行配置。我是 Emacs 用戶並且已經確定我想要只是普通的 Emacs不是 Aquamacs,這樣我就可以使用其他作業系統的現有配置。我嘗試遵循我在網上找到的許多操作方法(例如這個,這個這個來自我下載 Emacs 的網站),並嘗試以多種方式組合這些部分,但從未使事情達到甚至接近我習慣/希望的狀態:

  1. Emacs 守護程式開始執行啟動時或者一旦我需要它;兩者都可以。任何新的 Emacs 框架都由同一台伺服器支援。
  2. 當我從 Finder 打開文字檔案時,該檔案會在 Emacs 中打開,如果可能的話,會在現有的圖形框架中開啟。
  3. 如果我執行 Emacs.app,系統會重複使用在背景執行的 emacs 守護進程,而不是啟動一個新實例並在其上開啟一個新視窗。 (帽尖
  4. 當我從終端運行 Emacs 時,終端機會開始執行其業務,而無需等待我完成 Emacs(除非我使用“-nw”)
  5. 據我所知,在上述操作過程中,Emacs 絕對不會處於等待我輸入的狀態(“伺服器編輯模式?”)C-x #,儘管我對此沒有深入的了解。
  6. (我認為這是一個額外的好處)我可以透過某種方式從終端呼叫 Emacs,以便 Emacs 在可能的情況下重複使用現有框架。

我如何配置 Emacs 使其表現得像這樣?

我對讓 Finder 以我想要的方式在 Emacs 中打開內容感到特別困惑(#2),因為我找到的指南通常會創建許多別名、Applescripts、bash 腳本等,而我不確定我想告訴Finder 用於開啟文字檔案。

謝謝!

A。@lawlist 詢問:“即使存在現有框架,當您從終端打開新文件時,預設是否會打開新框架?”答案是,如果我按照描述在啟動時啟動伺服器這裡,開啟一個框架,然後透過鍵入 開啟文件/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n <whatever>,我可以在現有框架中開啟文件。如果目前沒有開啟的框架,我必須使用“-nc”,或者當我嘗試開啟檔案時什麼也不會發生。

B.@lawlist 詢問,“您希望守護進程在登入時運行,還是在登入後手動加載 Emacs 就足夠了?”手動運行就可以了。

答案1

要使 Emacs.app 在現有框架而不是新框架中開啟文件,請新增(setq ns-pop-up-frames nil)至設定文件,例如~/.emacs.

open -a emacs file您可以使用或從終端機開啟文件emacsclient -n file。如果 Emacs.app 未開啟但有一個Emacs --daemon進程,由於某種原因emacsclient -n file無法運行但emacsclient -nc file可以運行。

確保使用emacsclient您使用的 Emacs 版本中包含的二進位文件,例如/Applications/Emacs.app/Contents/MacOS/bin/emacsclient,或者/usr/local/bin/emacsclient如果您使用 Homebrew 安裝了 Emacs。

若要在登入時啟動 Emacs 伺服器,例如將此 plist 儲存為~/Library/LaunchAgents/my.emacsdaemon.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>my.emacsdaemon</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Applications/Emacs.app/Contents/MacOS/Emacs</string>
    <string>--daemon</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/> <!-- run the program again if it terminates -->
</dict>
</plist>

該 plist 會在下次登入時自動加載,但您可以透過執行 立即加載它launchctl load ~/Library/LaunchAgents/my.emacsdaemon.plist

編輯:我仍然不知道為什麼人們將文件類型與 AppleScript 應用程式相關聯,而不僅僅是 Emacs.app。 kuzzooroo 的答案中的腳本也可以寫為 shell 腳本:

macos=/Applications/Emacs.app/Contents/MacOS
if pgrep -qf 'Emacs.*--daemon'; then
  [[ $($macos/bin/emacsclient -e '(<= 2 (length (visible-frame-list)))') = t ]] && args=-nc || args=-n
else
  $macos/Emacs --daemon
  args=-nc
fi
$macos/bin/emacsclient $args "$@"
open -a /Applications/Emacs.app

您可以使用鴨嘴獸將腳本儲存為應用程式:

我剛開始使用 Emacs,但我有一個完全不同的設定。我透過製作 iTerm.app 的副本、更改 Info.plist 中的 CFBundleIdentifier 以便應用程式使用不同的首選項檔案並將預設命令設為 來製作自己的 Emacs 應用程式/usr/local/bin/emacs。我已新增(server-start)~/.emacs在登入時開啟自訂 Emacs 應用程式。我使用 Platypus 創建了一個可以運行的應用程序emacsclient -n "$@";open -b my.emacs,並將其設定為文字檔案的預設應用程式。

答案2

勞裡的解決方案結合評論中的建議基本上解決了我的問題。我在下面貼了一些我從網路上找到的東西縫合在一起的 Applescript。該腳本有助於消除一些剩餘的問題,例如,如果當前沒有 Emacs 框架,它的行為會更好一些。

編輯:該腳本的目的是與 Finder 中的 .txt 檔案關聯。

-- http://superuser.com/questions/457484/how-to-open-emacs-from-macs-finder
-- https://gist.github.com/ambethia/304964#comment-799519
on run {input}
    set filepath to quoted form of POSIX path of input
    tell application "Terminal"
        try
            -- we look for <= 2 because Emacs --daemon seems to always have an entry in visibile-frame-list even if there isn't
            set frameVisible to do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -e '(<= 2 (length (visible-frame-list)))'"
            if frameVisible is "t" then
                do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n " & filepath
            else
                -- there is a not a visible frame, launch one
                do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n " & filepath
            end if
        on error
            -- daemon is not running, start the daemon and open a frame     
            do shell script "/Applications/Emacs.app/Contents/MacOS/Emacs --daemon"
            do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n"
        end try
    end tell

    -- bring the visible frame to the front
    tell application "Emacs" to activate

    return input
end run

相關內容