emacs の minibuffer にホスト名を書く

リモートで作業しているときは、今いるマシンのホスト名がわからないとかなり困る。

普段は tmux を使っているので、そのステータスラインにホスト名を表示すれば十分だったりする。 でも、例えば多段に ssh してたりしてわざわざ tmux を張るほどじゃない場合もあって、そういう時に emacs を起動してそこがどこだかわからなくなることが意外とある。

なので、 emacs 上のどこかでホスト名を表示したいと思った。 表示する場所の候補の一つとしては mode-line とかあるけど、もう日付加えてるしあんまり色々書いても読みづらくなる。 その上 mode-line は window 毎に用意されるので無駄が多い感じが強い。

そこで、 advice を使って minibuffer に書くことにした。 find-file とか minibuffer を使った対話的な入力のときに、 [USER@HOST] が一緒に表示される(M-x での表示には emacs24 から対応してる)。

(defvar my-system-info
  nil
  "System info in the form of \"[user@host] \".")
(setq my-system-info
      (concat "["
              user-login-name
              "@"
              (car (split-string system-name
                                 "\\."))
              "] "))

(defadvice read-from-minibuffer (before info-in-prompt activate)
  (ad-set-arg 0
              (concat my-system-info
                      (ad-get-arg 0))))

(defadvice read-string (before info-in-prompt activate)
  (ad-set-arg 0
              (concat my-system-info
                      (ad-get-arg 0))))

(when (< emacs-major-version 24)
  (defadvice completing-read (before info-in-prompt activate)
    (ad-set-arg 0
                (concat my-system-info
                        (ad-get-arg 0)))))