yasnippet.elが便利そうだったので、インストールしてみた。anything-c-yasnippetはyasnippetをanythingの形式で使える便利なもの。yasnippet, anything-c-yasnippetのまとめエントリー - IMAKADO::BLOGにまとめられています。
yasnippet.elのダウンロード
Google Code Archive - Long-term storage for Google Code Project Hosting.に置いてある、yasnippet-0.6.1c.tar.bz2をダウンロードして解凍。解凍されたフォルダをemacs設定ディレクトリのどこかに置いておきます。僕の場合は、
~/.emacs.d/elisp/yasnippet
のようにディレクトリ名を変えて配置しました。
anything-c-yasnippetのダウンロード
http://svn.coderepos.org/share/lang/elisp/anything-c-yasnippet/anything-c-yasnippet.elに置いてあるので、それをダウンロードしてくる。そのあと、emacs設定ディレクトリのどこかに置いておきます。僕の場合は、
~/.emacs.d/elisp/anything/
の下に置きました。
ただし、この後注意が必要で、yasnippet-0.6.1cの場合、このままでは動きません。そこで、yasnippet-0.6.1b(c?)を使うとanything-c-yasnippet.elが動かない - 放牧日記で書かれているパッチを当てます。
diff --git a/plugins/anything-c-yasnippet.el b/plugins/anything-c-yasnippet.el
index b74c115..948d627 100644
--- a/plugins/anything-c-yasnippet.el
+++ b/plugins/anything-c-yasnippet.el
@@ -162,15 +162,20 @@ If SNIPPET-FILE does not contain directory, it is placed in default snippet dire
(yas/choose-tables-first nil)
(yas/buffer-local-condition 'always))
(let* ((result-alist '((candidates) (transformed) (template-key-alist)))
- (hash-value-alist nil)
- (cur-table (first (yas/get-snippet-tables anything-c-yas-cur-major-mode)))
- (hash-table (yas/snippet-table-hash cur-table))) ;`yas/snippet-table-hash'
- (let ((hashes (loop for table in (yas/get-snippet-tables)
+ (cur-tables
+ (if table
+ (list table)
+ (yas/get-snippet-tables anything-c-yas-cur-major-mode)))
+ (hash-value-alist nil))
+ (let ((hashes (loop for table in cur-tables
collect (yas/snippet-table-hash table))))
(loop for hash in hashes
do (maphash (lambda (k v)
- (setq hash-value-alist (append v hash-value-alist))
- )
+ (let (a)
+ (maphash (lambda (n te)
+ (setq a (append (list (cons k te)) a)))
+ v)
+ (setq hash-value-alist (append a hash-value-alist))))
hash))
(loop with transformed
with templates
@@ -306,7 +311,7 @@ space match anyword greedy"
(candidate-transformer . (lambda (candidates)
(anything-c-yas-get-transformed-list anything-c-yas-cur-snippets-alist anything-c-yas-initial-input)))
(action . (("Insert snippet" . (lambda (template)
- (yas/expand-snippet anything-c-yas-point-start anything-c-yas-point-end template)
+ (yas/expand-snippet template anything-c-yas-point-start anything-c-yas-point-end)
(when anything-c-yas-display-msg-after-complete
(message "this snippet is bound to [ %s ]"
(anything-c-yas-get-key-by-template template anything-c-yas-cur-snippets-alist)))))これを、どこかにanything-c-yasnippet.patch(名前は適当)のように保存して、anything-c-yasnippet.elの置かれているフォルダに移動してpatchコマンドを使います。僕の環境では次のコマンド。
% cd ~/.emacs.d/elisp/anything % patch < ~/anything-c-yasnippet.patch
これで大丈夫です。
設定
ファイルを置いた場所にロードパスを通してから、次のような設定をしておくと大丈夫みたいです。
(require 'yasnippet) (require 'anything-c-yasnippet) (setq anything-c-yas-space-match-any-greedy t) ;スペース区切りで絞り込めるようにする デフォルトは nil (global-set-key (kbd "C-c y") 'anything-c-yas-complete) ;C-c yで起動 (yas/initialize) (yas/load-directory "~/.emacs.d/elisp/yasnippet/snippets/") ;snippetsのディレクトリを指定(環境に合わせて)
yasnippet, anything-c-yasnippetのまとめエントリー - IMAKADO::BLOGには以下の設定も書く事になってますが、yasnippetのあるバージョンから必要なくなったようです。
(add-to-list 'yas/extra-mode-hooks 'ruby-mode-hook) (add-to-list 'yas/extra-mode-hooks 'cperl-mode-hook)