yasnippetが実行できない(解決)

yasnippetがずっと起動できなくて無駄な時間が流れてしまった。だけどやっと解決ができた。原因はyasnippetというよりは、anything-c-yasnippetの更新がされてないのが原因だったみたい。解決方法はanything-c-yasnippetにパッチを当てないと悪い。起動ができないというバグが治ったことで、kが入力出来ないバグも解消した。


>>>>>>>> 追記11/05/11

kが入力できないバグが再発したorz
=> 解決法 Emacsで k が入力出来ないバグ - プログラムとかののblog

<<<<<<<<

インストー

yasnippet,anything-c-yasnippetをインストールした

ここを見るといい。

パッチ当て

patchは、

anything-c-yasnippet で、"Open snippet file” アクションが動かない場合がある

上のサイトのやつを当てることにした。詳しい説明は上のリンクへ。

--- anything-c-yasnippet.el.org	2010-05-18 11:06:44.000000000 +0900
+++ anything-c-yasnippet.el	2010-05-18 10:53:14.000000000 +0900
@@ -161,30 +161,39 @@
   (let ((yas/choose-keys-first nil)
         (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)
+    (let* ((result-alist '((candidates) (transformed) (template-key-alist)(template-file-alist)))
+           (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
               with template-key-alist
+              with template-file-alist			  
               for lst in hash-value-alist
               for key = (car lst)
               for template-struct = (cdr lst)
               for name = (yas/template-name template-struct) ;`yas/template-name'
               for template = (yas/template-content template-struct) ;`yas/template-content'
+              for file = (yas/template-file template-struct) ;`yas/template-content'			  
               do (progn (push template templates)
                         (push `(,name . ,template) transformed)
-                        (push `(,template . ,key) template-key-alist))
+						(push `(,template . ,key) template-key-alist)
+                        (push `(,template . ,file) template-file-alist))
               finally (progn (push `(candidates . ,templates) result-alist)
                              (push `(transformed . ,transformed) result-alist)
+                             (push `(template-file-alist . ,template-file-alist) result-alist)
                              (push `(template-key-alist . ,template-key-alist) result-alist)))
         result-alist)
       )))
@@ -241,7 +250,7 @@
     ;; sort
     (setq transformed-list (sort* transformed-list 'string&lt; :key 'car))
     transformed-list))
-
+ 
 (defun anything-c-yas-find-snippet-file-by-key (key)
   (let ((modes (anything-c-yas-get-modes))
         (snippet-dirs anything-c-yas-snippets-dir-list))
@@ -267,12 +276,13 @@
         finally return path))
 
 (defun anything-c-yas-find-file-snippet-by-template (template &optional other-window)
-  (let* ((path (anything-c-yas-get-path-by-template template))
+  (let* ((path (assoc-default template (assoc-default 'template-file-alist anything-c-yas-cur-snippets-alist)))
          (ff-func (if other-window 'find-file-other-window 'find-file)))
     (if path
         (funcall ff-func path)
       (message "not found snippet file"))))
 
+
 (defun anything-c-yas-get-path-by-template (template)
   (let* ((key (anything-c-yas-get-key-by-template template anything-c-yas-cur-snippets-alist))
          (path (anything-c-yas-find-snippet-file-by-key key)))
@@ -306,12 +316,12 @@
     (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)))))
                ("Open snippet file" . (lambda (template)
-                                        (anything-c-yas-find-file-snippet-by-template template)))
+										(anything-c-yas-find-file-snippet-by-template template)))
                ("Open snippet file other window" . (lambda (template)
                                                      (anything-c-yas-find-file-snippet-by-template template t)))
                ("Create new snippet on region" . (lambda (template)

これをanything-c-yasnippet.patchとか(なんでも良い.patch)いう名前で保存してanything-c-yasnippetが保存されているディレクトリに行く。patchは~/.emacs.d/とかいう場所にあったとする。パッチをpatchを当てられる元ファイルは~/.emacs.d/elispに保存しているとすると、コマンドはこうなる。

$ cd ~/.emacs.d/elisp/
$ patch < ~/.emacs.d/anything-c-yasnippet.patch

patchコマンドを入力する時はリダイレクションの記号( 「<」 ←のこと )を忘れずに。やらないと処理がいつまで立っても終わらないかも。成功すれば、

patching file anything-c-yasnippet.el

こんなのが出てきてくれるはず。これで設定をすればyasnippetを使うことが出来る。自分の設定ファイルから。

(add-to-list 'load-path "~/.emacs.d/plugins/yasnippet-0.6.1c")

(require 'yasnippet)
(require 'anything-c-yasnippet)
(setq anything-c-yas-space-match-any-greedy t)
(define-key yas/minor-mode-map (kbd "C-c y") 'anything-c-yas-complete)

;;@see http://emacs.g.hatena.ne.jp/Shinnya/20100805/1281034504
(setq yas/next-field-key "TAB")
(setq yas/prev-field-key "<s-tab>")
(define-key yas/minor-mode-map (kbd "C-x i i") 'yas/insert-snippet)
(define-key yas/minor-mode-map (kbd "C-x i f") 'yas/find-snippets)
(define-key yas/minor-mode-map (kbd "C-x i n") 'yas/new-snippet)
(define-key yas/minor-mode-map (kbd "C-x i v") 'yas/visit-snippet-file)
(define-key yas/minor-mode-map (kbd "C-x i e") 'yas/expand)
;; コメントやリテラルではスニペットを展開しない
(setq yas/buffer-local-condition
      '(or (not (or (string= "font-lock-comment-face"
			     (get-char-property (point) 'face))
		    (string= "font-lock-string-face"
			     (get-char-property (point) 'face))))
             '(require-snippet-condition . force-in-comment)))
(yas/initialize)

;;一つしかディレクトリがない場合
(setq yas/root-directory '"~/.emacs.d/plugins/yasnippet-0.6.1c/snippets")
(yas/load-directory  yas/root-directory)

;;複数のスニペットディレクトリがある場合
;(setq yas/root-directory '("~/.emacs.d/etc/mysnippets"
;			   "~/.emacs.d/plugins/yasnippet-0.6.1c/snippets"))
;(mapc 'yas/load-directory  yas/root-directory)

;;@see http://d.hatena.ne.jp/rubikitch/20101204/yasnippet
;;; [2010/07/13]
(defun yas/expand-link (key)
  "Hyperlink function for yasnippet expansion."
  (delete-region (point-at-bol) (1+ (point-at-eol)))
  (insert key)
  (yas/expand))
;;; [2010/12/02]
(defun yas/expand-link-choice (&rest keys)
  "Hyperlink to select yasnippet template."
  (yas/expand-link (completing-read "Select template: " keys nil t)))
;; (yas/expand-link-choice "defgp" "defcm")
</s-tab>

いろんなとこから設定を取ってきたので量は多い。けれど役には立つと思う。

patchが当たってない場合で、anything-c-yas-completeを実行するとこんなエラーが出る。

Debugger entered--Lisp error: (wrong-type-argument sequencep
#s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold
0.8 data ("Command that works on region or word" [cl-struct-yas/
template ";; example of a command that works on current word or
text selection
(defun down-case-word-or-region ()
\"Lower case the current word or text selection.\"
(interactive)
(let (pos1 pos2 meat)
(if (and transient-mark-mode mark-active)
(setq pos1 (region-beginning)
pos2 (region-end))
(setq pos1 (car (bounds-of-thing-at-point 'symbol))
pos2 (cdr (bounds-of-thing-at-point 'symbol))))

; now, pos1 and pos2 are the starting and ending positions
; of the current word, or current text selection if exists

;; put your code here.
$0
;; Some example of things you might want to do
(downcase-region pos1 pos2) ; example of a func that takes
region as args (setq meat (buffer-substring-no-properties pos1
pos2)) ; grab the text.
(delete-region pos1 pos2) ; get rid of it
(insert \"newText\") ; insert your new text

)
)
" "Command that works on region or word" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/word-or-region" nil])))
append(#s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("Command that works on region or word" [cl-struct-yas/template ";; example of a command that works on current word or text selection\n(defun down-case-word-or-region ()\n \"Lower case the current word or text selection.\"\n(interactive)\n(let (pos1 pos2 meat)\n (if (and transient-mark-mode mark-active)\n (setq pos1 (region-beginning)\n pos2 (region-end))\n (setq pos1 (car (bounds-of-thing-at-point 'symbol))\n pos2 (cdr (bounds-of-thing-at-point 'symbol))))\n\n ; now, pos1 and pos2 are the starting and ending positions\n ; of the current word, or current text selection if exists\n\n ;; put your code here.\n $0\n ;; Some example of things you might want to do\n (downcase-region pos1 pos2) ; example of a func that takes region as args\n (setq meat (buffer-substring-no-properties pos1 pos2)) ; grab the text.\n (delete-region pos1 pos2) ; get rid of it\n (insert \"newText\") ; insert your new text\n\n )\n)\n" "Command that works on region or word" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/word-or-region" nil] ...)) nil)
#[(k v) "\302 \"\211\207" [v hash-value-alist append] 3]("word-or-region" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("Command that works on region or word" [cl-struct-yas/template ";; example of a command that works on current word or text selection\n(defun down-case-word-or-region ()\n \"Lower case the current word or text selection.\"\n(interactive)\n(let (pos1 pos2 meat)\n (if (and transient-mark-mode mark-active)\n (setq pos1 (region-beginning)\n pos2 (region-end))\n (setq pos1 (car (bounds-of-thing-at-point 'symbol))\n pos2 (cdr (bounds-of-thing-at-point 'symbol))))\n\n ; now, pos1 and pos2 are the starting and ending positions\n ; of the current word, or current text selection if exists\n\n ;; put your code here.\n $0\n ;; Some example of things you might want to do\n (downcase-region pos1 pos2) ; example of a func that takes region as args\n (setq meat (buffer-substring-no-properties pos1 pos2)) ; grab the text.\n (delete-region pos1 pos2) ; get rid of it\n (insert \"newText\") ; insert your new text\n\n )\n)\n" "Command that works on region or word" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/word-or-region" nil] ...)))
maphash(#[(k v) "\302 \"\211\207" [v hash-value-alist append] 3] #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("word-or-region" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("Command that works on region or word" [cl-struct-yas/template ";; example of a command that works on current word or text selection\n(defun down-case-word-or-region ()\n \"Lower case the current word or text selection.\"\n(interactive)\n(let (pos1 pos2 meat)\n (if (and transient-mark-mode mark-active)\n (setq pos1 (region-beginning)\n pos2 (region-end))\n (setq pos1 (car (bounds-of-thing-at-point 'symbol))\n pos2 (cdr (bounds-of-thing-at-point 'symbol))))\n\n ; now, pos1 and pos2 are the starting and ending positions\n ; of the current word, or current text selection if exists\n\n ;; put your code here.\n $0\n ;; Some example of things you might want to do\n (downcase-region pos1 pos2) ; example of a func that takes region as args\n (setq meat (buffer-substring-no-properties pos1 pos2)) ; grab the text.\n (delete-region pos1 pos2) ; get rid of it\n (insert \"newText\") ; insert your new text\n\n )\n)\n" "Command that works on region or word" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/word-or-region" nil] ...)) "traverse_dir" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("traversing a directory" [cl-struct-yas/template ";; apply a function to all files in a dir\n(require 'find-lisp)\n(mapc 'my-process-file (find-lisp-find-files \"~/myweb/\" \"\\\\.html$\"))\n" "traversing a directory" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/traverse_dir" nil] ...)) "grabthing" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("grab word under cursor" [cl-struct-yas/template "(setq $0 (thing-at-point 'symbol))\n" "grab word under cursor" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/grabthing" nil] ...)) "grabstring" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("grab buffer substring" [cl-struct-yas/template "(setq $0 (buffer-substring-no-properties myStartPos myEndPos))\n" "grab buffer substring" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/grabstring" nil] ...)) "find-replace" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("find and replace on region" [cl-struct-yas/template "(defun replace-html-chars-region (start end)\n \"Replace “<” to “<” and other chars in HTML.\nThis works on the current region.\"\n (interactive \"r\")\n (save-restriction \n (narrow-to-region start end)\n (goto-char (point-min))\n (while (search-forward \"&\" nil t) (replace-match \"&\" nil t))\n (goto-char (point-min))\n (while (search-forward \"<\" nil t) (replace-match \"<\" nil t))\n (goto-char (point-min))\n (while (search-forward \">\" nil t) (replace-match \">\" nil t))\n )\n )\n" "find and replace on region" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/find-replace" nil] ...)) "file" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("read lines of a file" [cl-struct-yas/template "(defun read-lines (filePath)\n \"Return a list of lines in FILEPATH.\"\n (with-temp-buffer\n (insert-file-contents filePath)\n (split-string\n (buffer-string) \"\\n\" t)) )\n\n;; process all lines\n(mapc \n (lambda (aLine) \n (message aLine) ; do your stuff here\n )\n (read-lines \"inputFilePath\")\n)" "read lines of a file" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/file.read-lines" nil] "a function that process a file" [cl-struct-yas/template "(defun doThisFile (fpath)\n \"Process the file at path FPATH ...\"\n (let ()\n ;; create temp buffer without undo record or font lock. (more efficient)\n ;; first space in temp buff name is necessary\n (set-buffer (get-buffer-create \" myTemp\"))\n (insert-file-contents fpath nil nil nil t)\n\n ;; process it ...\n ;; (goto-char 0) ; move to begining of file's content (in case it was open)\n ;; ... do something here\n ;; (write-file fpath) ;; write back to the file\n\n (kill-buffer \" myTemp\")))\n" "a function that process a file" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/file.process" nil] ...)) "dired" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("process marked files in dired" [cl-struct-yas/template ";; idiom for processing a list of files in dired's marked files\n \n;; suppose myProcessFile is your function that takes a file path\n;; and do some processing on the file\n\n(defun dired-myProcessFile ()\n \"apply myProcessFile function to marked files in dired.\"\n (interactive)\n (require 'dired)\n (mapc 'myProcessFile (dired-get-marked-files))\n)\n\n;; to use it, type M-x dired-myProcessFile\n" "process marked files in dired" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/dired.process_marked" nil] ...)) "defun0000664" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("function template" [cl-struct-yas/template "(defun $1 ()\n \"thisandthat.\"\n (interactive)\n (let (var1)\n (setq var1 some)\n $0\n )\n)" "function template" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/defun0000664" nil] ...)) "defun" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("function template" [cl-struct-yas/template "(defun $1 ()\n \"thisandthat.\"\n (interactive)\n (let (var1)\n (setq var1 some)\n $0\n )\n)" "function template" nil nil "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/defun" nil] ...)) "da" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("defvar" [cl-struct-yas/template "" "defvar" nil "*1" "/home/user/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/emacs-lisp-mode/#defvar.yasnippet#" ...] ...)) ...)))
anything-c-yas-build-cur-snippets-alist()
(setq anything-c-yas-cur-snippets-alist (anything-c-yas-build-cur-snippets-alist))
(lambda nil (setq anything-c-yas-cur-major-mode major-mode) (setq anything-c-yas-selected-text (if mark-active ... "")) (multiple-value-setq (anything-c-yas-initial-input anything-c-yas-point-start anything-c-yas-point-end) (anything-c-yas-get-cmp-context)) (setq anything-c-yas-cur-snippets-alist (anything-c-yas-build-cur-snippets-alist)))()
apply*2 (multiple-value-setq (anything-c-yas-initial-input anything-c-yas-point-start anything-c-yas-point-end) (anything-c-yas-get-cmp-context)) (setq anything-c-yas-cur-snippets-alist (anything-c-yas-build-cur-snippets-alist))) nil)
anything-funcall-with-source(((match anything-exact-match anything-mp-3p-match anything-mp-3-match anything-c-yas-match) (search anything-exact-search anything-mp-3p-search anything-mp-3-search) (name . "Yasnippet") (init lambda nil (setq anything-c-yas-cur-major-mode major-mode) (setq anything-c-yas-selected-text ...) (multiple-value-setq ... ...) (setq anything-c-yas-cur-snippets-alist ...)) (candidates anything-c-yas-get-candidates anything-c-yas-cur-snippets-alist) (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 ... ... ...) ("Open snippet file" lambda ... ...) ("Open snippet file other window" lambda ... ...) ("Create new snippet on region" lambda ... ...) ("Reload All Snippts" lambda ... ... ...) ("Rename snippet file" lambda ... ...) ("Delete snippet file" lambda ... ...)) (persistent-action lambda (template) (anything-c-yas-find-file-snippet-by-template template)) (match anything-c-yas-match) (header-line . anything-persistent-help-string)) (lambda nil (setq anything-c-yas-cur-major-mode major-mode) (setq anything-c-yas-selected-text (if mark-active ... "")) (multiple-value-setq (anything-c-yas-initial-input anything-c-yas-point-start anything-c-yas-point-end) (anything-c-yas-get-cmp-context)) (setq anything-c-yas-cur-snippets-alist (anything-c-yas-build-cur-snippets-alist))))
anything-funcall-foreach(init)
ad-Orig-anything-initialize()
anything-initialize()
anything-initialize-1(nil nil anything-c-source-yasnippet)
byte-code("\306\211\211\307\206


Emacsで、yasnippetを使えない人がいたらこの記事を発見してくれることを願う。

参考サイト

yasnippet,anything-c-yasnippetをインストールした
anything-c-yasnippet で、"Open snippet file” アクションが動かない場合がある

追記11/05/11

なぜか内容が重複してて、解決できなかったので新しい記事としてうpする。

*1:some-var some-value

*2:lambda nil (setq anything-c-yas-cur-major-mode major-mode) (setq anything-c-yas-selected-text (if mark-active ... ""