Emacsを使っていると,編集しているfileのdirectoryをFinder上に表示したいときがあります.
そんな時用のTipsです.
shell-commandを使おう
Emacsにはshell-commandという便利な関数があります.
これは引数に文字列を取り,shell上で実行してくれる関数です.
M-x shell-command RET "open ."
これでshell上でopen .と入力した時と同じ結果が得られます.
自分で関数を定義しよう
いちいちM-x shell-commandなどするのは面倒ですので,これを1つのユーザ定義関数にしましょう.
;; current directory open
(defun finder-current-dir-open()
(interactive)
(shell-command "open ."))
;; directory open
(defun finder-open(dirname)
(interactive "DDirectoryName:")
(shell-command (concat "open " dirname)))
;; set the keybind
(global-set-key (kbd "M-f") 'finder-current-dir-open)
これでM-fと入力するだけで,現在のdirectoryがFinderで開けます.
補足1 : interactiveとは?
(defun finder-current-dir-open()
(interactive) <---- これ!!
(shell-command "open ."))
ここに書いてあるinteractiveの意味についてちょっとだけ補足します.
定義した関数の中(今回だったらfinder-current-dir-open)にinteractiveと記述することによって,M-xでその関数を呼び出すことができます.
(なので,もし上記の関数にinteractiveが記述されていないと,M-xしても関数を呼べません)
補足2 : interactive "D"とは?
(defun finder-open(dirname)
(interactive "DDirectoryName:") <---- これ!
(shell-command (concat "open " dirname)))
ここではinteractive "D"ではなくDDirectoryName:となっていますが,最初の文字"D"以降はMinibufferに表示される文字なので無視して問題ないです.
さて,ここで言う"D"とは,DirectoryのDですね.
そして"D"が記述されることで,EmacsはdirectoryのリストをMinibufferに表示してくれます.
その後,開きたいdirectoryを入力することで,Finder上でopenできます.
その他にもinteractive "P"とかinteractive "n"とかあります.
詳しくはこちらを参考ください!
参考
Emacsでマイナーモードを定義してみたり、 M-x で動くインタラクティブ関数を定義してみたりする
Emacs Interactive Codes

