AKIYAMA Kouhei
2002年09月01日
| 2009-02-19 | ff-find-other-fileという便利なものが標準であるようです……。そっちを使いましょー。 |
C++でプログラムをしていると例えばfile.cppを開いているときに対応するファイルであるfile.hへ素早く移動したいことがよくある。本稿では対応するファイルを訪問するコマンドを紹介する。
以下のコードは対応するファイルを訪問する関数my-find-cpp-header-fileを定義している。この関数は現在のバッファに関連づけられているファイルの拡張子が.cppなら同名の.hファイルを訪問する。また、拡張子が.hなら同名の.cppファイルを訪問する。
(defun my-find-cpp-header-file ()
(interactive)
(let ((file (buffer-file-name)))
(if file
(let ((newfile
(cond ((string-match ".cpp$" file)
(replace-match ".h" t t file))
((string-match ".h$" file)
(replace-match ".cpp" t t file))
(t nil)
)
))
(if newfile
(if (file-exists-p newfile)
(find-file newfile)))
)
)
)
)
これを.emacsで適当なキーに割り当てればよい。私はf3キーに割り当てている。
(define-key global-map [f3] 'my-find-cpp-header-file)
Last modified: Sun Sep 01 16:30:10 2002