Emacs の略語展開のメモ
実を言うと僕は略語展開をあまり使わない人です。というのは略語展開結果が予測しづらいし、それで迷うぐらいならそのまま打ってしまったほうが早いことが多々あるからです。
しかし良質な略語展開機能を使えば入力速度もあがると思うので略語展開に関して調べてみました。とりあえず pabbrev.el を使ってみようかと思います。
hippie-expand
hippie-expand はいろいろな略語展開機能を統一してくれるすごいやつです。具体的には以下のように略語展開機能を列挙しておいて、 hippie-expand を実行すると hippie-expand が上から順に略語展開を起動していき、最初に展開に成功したものを略語展開結果にします。 hippie-expand は略語展開を使うか使わないかに関わらず設定しておくべきです。
(setq hippie-expand-try-functions-list
'(try-complete-file-name-partially
try-complete-file-name
try-expand-all-abbrevs
try-expand-list try-expand-line
try-expand-dabbrev
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-complete-lisp-symbol-partially
try-complete-lisp-symbol))
hippie-expand を使えば静的略語展開だとか動的略語展開だとか迷わずに済みます。
たぶん使い方は間違ってますが hippie-expand は tempo や tempo-snippets とも共存できます。
(defun try-expand-tempo (old)
(tempo-expand-if-complete))
(defun try-expand-tempo-snippets (old)
(tempo-snippets-complete-tag))
(setq hippie-expand-try-functions-list
'(try-expand-tempo
try-expand-tempo-snippets))
ほとんどネタですが、実は略語展開以外のこともできます。
(defun try-increment-number (old)
(he-init-string (he-lisp-symbol-beg) (point))
(if (string-match "^[0-9]+$" he-search-string)
(he-substitute-string (number-to-string (1+ (string-to-number he-search-string))))))
(setq hippie-expand-try-functions-list
'(try-increment-number))
数値の後ろにカーソルをもっていておもむろに hippie-expand すると数値がインクリメントされます。
このように hippie-expand ができるのは略語展開だけではないのでアイデア次第でかなりいろいろな事ができると思います。
ちなみに僕は以下のような設定にしています。
(global-set-key "\M-/" 'hippie-expand)
(defun try-expand-abbrev (old)
(if (expand-abbrev) t nil))
(defun try-expand-tempo (old)
(tempo-expand-if-complete))
(defun try-expand-tempo-snippets (old)
(tempo-snippets-complete-tag))
(setq hippie-expand-try-functions-list
'(;try-complete-file-name-partially
;try-complete-file-name
try-expand-tempo
try-expand-tempo-snippets
try-expand-abbrev
try-expand-all-abbrevs
;try-expand-list try-expand-line
try-expand-dabbrev
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-complete-lisp-symbol-partially
try-complete-lisp-symbol))
pabbrev.el
pabbrev.el (*) は略語展開候補をちょうど iswitchb のようにカーソルの横に出してくれるので略語展開後を予測しやすくなります。
(*) http://homepages.cs.ncl.ac.uk/phillip.lord/download/emacs/pabbrev.el
(require 'pabbrev)
しておいて、 M-x pabbrev-mode で動作確認できます。
リアルタイムに略語展開候補がでるので大変わかりやすいのですが、略語展開が複数個ある場合の使いやすさがいまいちなので今後に期待です。
snippet.el
snippet.el (*) は tempo.el に比べてテンプレートが書きやすいように作られているのですが、インデントやカーソル移動の命令が少なすぎて正直使いものになりません。
tempo-snippet.el
いまいちな snippet.el のかわりに作られたのが tempo-snippet.el (*) です。 tempo.el と snippet.el の良いところを集めた感じの出来になっています。
(*) http://emacswiki.org/cgi-bin/emacs-en/TempoSnippets
リンク先にデモがあります。
JavaScript のコーディングのために以下のような設定を .emacs に書いたけど全然使ってないという(そもそも最近 JavaScript 書いてない)。 hippie-expand との組合せが最強なんですがね。
(add-hook 'javascript-mode-hook '(lambda ()
(tempo-define-snippet "javascript-for-by-length"
'(> "for (var " (p "i: " i) " = 0, " (p "len: " len) " = " (p "array: " array) ".length; "
(s i) " < " (s len) "; "
(s i) "++) {"
> n>
"var " (p "x: " x) " = " (s array) "[" (s i) "];"
> n> r n "}" >)
"forl")
(tempo-define-snippet "javascript-for-by-property"
'(> "for (var " (p "i: " i) " = 0, " (p "x: " x) "; "
(s x) " = " (p "array: " array) "[" (s i) "]; "
(s i) "++) {"
> n> r n "}" >)
"forp")
(tempo-define-snippet "javascript-function-with-args"
'(> "function (" (p "args: " args) ") {"
> n> r n "}" r >)
"fun")
(tempo-define-snippet "javascript-function-without-args"
'(> "function () {"
> n> r n "}" r >)
"fn")
(tempo-define-snippet "javascript-short-function"
'(> "function (" (p "x: " x) ") { " r " }" r)
"f")))
msf-abbrev.el
モードごとに略語を簡単に定義できたりするのが msf-abbrev.el (*) 。
(*) http://www.emacswiki.org/cgi-bin/wiki/MsfAbbrev
リンク先にデモがあります。
company-mode
かなり期待してるのが company-mode (*) 。略語展開機構が汎用的に作られているらしいので Semantic や Global をバックエンドとして使うことができるのだそうです。
(*) http://nschum.de/src/emacs/company-mode/
略語展開インターフェースも Visual Studio とか vi のあれと同じなのでわかりやすいです。
リンク先にデモがあります。
- Category(s)
- emacs
- The URL to Trackback this entry is:
- http://dev.ariel-networks.com/Members/matsuyama/emacs-abbrev/tbping
dabbrev-hover.el :リアルタイムに補間結果をTooltipに表示してくれる
http://www.bookshelf.jp/pukiwiki/pukiwiki.php?%A5%A2%A5%A4%A5%C7%A5%A2%BD%B8#content_1_18
dabbrev-expand-multiple :候補を複数表示して, その中から補完できるようにするもの
http://d.hatena.ne.jp/khiker/20070817/emacs_dabbrev