Changeset 8388 for lang/elisp

Show
Ignore:
Timestamp:
03/26/08 00:24:25 (8 months ago)
Author:
imakado
Message:

lang/elisp/anything-c-yasnippet/anything-c-yasnippet.el: 補完候補を自前で用意する方法からyasnippet.elから得る方法に変更。

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/elisp/anything-c-yasnippet/anything-c-yasnippet.el

    r8348 r8388  
    88;; Actions: Insert snippet, Open snippet file, Open snippet file other window 
    99;; C-z: execute-persistent-action 
     10 
     11;; here's my yasnippet's configuration 
     12;; (require 'yasnippet) 
     13;; (require 'anything-c-yasnippet) 
     14;; (setq anything-c-yas-space-match-any-greedy t) ;[default: nil] 
     15;; (global-set-key (kbd "C-c y") 'anything-c-yas-complete) 
     16;; (yas/initialize) 
     17;; (yas/load-directory "<path>/<to>/snippets/") 
     18;; (add-to-list 'yas/extra-mode-hooks 'ruby-mode-hook) 
     19;; (add-to-list 'yas/extra-mode-hooks 'cperl-mode-hook) 
    1020 
    1121(require 'cl) 
     
    4454 
    4555(defvar anything-c-yas-appear-rule nil) ;; customizable e.i (add-to-list 'anything-c-yas-appear-rule '(ruby-mode . (text-mode html-mode))) 
    46 (defvar anything-c-yas-not-display-dups nil) ;; customizable 
     56(defvar anything-c-yas-not-display-dups t) ;; customizable 
    4757(defvar anything-c-yas-display-msg-after-complete t) ;; customizable 
    48  
    49 ;;; advise to get data for completion 
    50 (defvar anything-c-yas-snippets-alist nil) 
    51 (defvar anything-c-yas-load-directory-histry nil) 
     58(defvar anything-c-yas-snippets-dir-list nil) ;;customizable list of path(string) for finding snippet file in Action 
     59(defvar anything-c-yas-space-match-any-greedy nil 
     60  "non-nil anything pattern, space match anyword greedy. 
     61pattern regexp: \"if else\" replace to \"if.*else\" 
     62so match \"if (...) { ... } else { ... }\" and \"if, elsif, else ...\" 
     63quite convenience") ;; customizable 
     64 
     65 
     66(defvar anything-c-yas-snippets-dir-list nil) 
    5267(defadvice yas/load-directory-1 (around anything-yas-build-alist activate) 
    53   (let* ((directory (ad-get-arg 0)) 
    54          (mode-sym (intern (file-name-nondirectory directory))) 
    55          (snippets nil)) 
    56     (ignore-errors 
    57       (add-to-list 'anything-c-yas-load-directory-histry directory) 
    58       (with-temp-buffer 
    59         (dolist (file (yas/directory-files directory t)) 
    60           (when (file-readable-p file) 
    61             (insert-file-contents file nil nil nil t) 
    62             (push (cons (file-name-nondirectory file) 
    63                         (yas/parse-template)) 
    64                   snippets)))) 
    65       ;; build alist for anything-c-yasnippet 
    66       (let ((mode-alist (assq mode-sym anything-c-yas-snippets-alist))) 
    67         (cond 
    68          (mode-alist 
    69           (setcdr mode-alist (nconc snippets (cdr mode-alist)))) 
    70          (t 
    71           (push `(,mode-sym . ,snippets) anything-c-yas-snippets-alist)))))) 
     68  (let ((directory (ad-get-arg 0))) 
     69    (when (stringp directory) 
     70      (add-to-list 'anything-c-yas-snippets-dir-list directory))) 
    7271  ad-do-it) 
    7372 
    7473 
    75 (defun anything-c-yas-build-cur-snippets-alist () 
    76   (let* ((result-alist nil) 
    77          (modes (anything-c-yas-get-modes)) 
    78          (snippets (loop for mode in modes 
    79                          append (assoc-default mode anything-c-yas-snippets-alist 'eq)))) 
    80     ;; build result-alist 
     74(defun anything-c-yas-build-cur-snippets-alist (&optional table) 
     75  (let* ((result-alist '((candidates) (transformed) (template-key-alist))) 
     76         (hash-value-alist nil) 
     77         (cur-table (or table (yas/snippet-table anything-c-yas-cur-major-mode))) 
     78         (parent-table (yas/snippet-table-parent cur-table)) ;`yas/snippet-table-parent' 
     79         (hash-table (yas/snippet-table-hash cur-table))) ;`yas/snippet-table-hash' 
     80    (maphash (lambda (k v) (setq hash-value-alist (append v hash-value-alist))) hash-table) 
     81    (setq hsh hash-value-alist) 
    8182    (loop with transformed 
    8283          with templates 
    83           for snippet in snippets 
    84           for name = (third snippet) 
    85           and template = (second snippet) 
    86           when (and (not (null name)) 
    87                     (not (null template)) 
    88                     (stringp name) 
    89                     (stringp template)) 
     84          with template-key-alist 
     85          for lst in hash-value-alist 
     86          for key = (car lst) 
     87          for template-struct = (cdr lst) 
     88          for name = (yas/template-name template-struct) ;`yas/template-name' 
     89          for template = (yas/template-content template-struct) ;`yas/template-content' 
    9090          do (progn (push template templates) 
    91                     (push `(,name . ,template) transformed)) 
     91                    (push `(,name . ,template) transformed) 
     92                    (push `(,template . ,key) template-key-alist)) 
    9293          finally (progn (push `(candidates . ,templates) result-alist) 
    9394                         (push `(transformed . ,transformed) result-alist) 
    94                          (push `(snippets . ,snippets) result-alist))) 
     95                         (push `(template-key-alist . ,template-key-alist) result-alist))) 
     96    ;; if cur-table has parent build recursively 
     97    (when parent-table 
     98      (let ((rec-ret (anything-c-yas-build-cur-snippets-alist parent-table)) 
     99            (alist-keys '(candidates transformed template-key-alist))) 
     100        (mapc (lambda (key) 
     101                (let ((res-list (assq key result-alist)) 
     102                      (rec-val (assoc-default key rec-ret))) 
     103                  (setcdr res-list (nconc rec-val (cdr res-list))))) 
     104              alist-keys))) 
    95105    result-alist)) 
    96106 
     
    114124(defun anything-c-yas-get-key-by-template (template alist) ;str template 
    115125  "Return key" 
    116   (let ((snippets (assoc-default 'snippets alist 'eq))) 
    117     (car 
    118      (rassoc-if (lambda (ls) (equal template (car ls))) snippets)))) 
     126  (assoc-default template (assoc-default 'template-key-alist alist))) 
    119127 
    120128(defun anything-c-yas-get-candidates (alist) 
     
    136144(defun anything-c-yas-find-snippet-file-by-key (key) 
    137145  (let ((modes (anything-c-yas-get-modes)) 
    138         (done nil) 
    139         (path nil)) 
    140     (loop for mode in modes 
    141           for snippet-path-re = (concat (symbol-name mode) "/" key "$") 
    142           unless done 
    143           do (loop for directory in anything-c-yas-load-directory-histry 
    144                    for files = (directory-files directory t) 
    145                    unless done 
    146                    do (loop for file in files 
    147                             when (string-match snippet-path-re file) 
    148                             return (setq done t 
    149                                          path file))) 
    150           finally return path))) 
     146        (snippet-dirs (add-to-list 'anything-c-yas-snippets-dir-list yas/root-directory))) 
     147    (let ((found-path (loop for mode in modes 
     148                            for test-re = (concat (symbol-name mode) "/" key "$") 
     149                            for path =  (anything-c-yas-find-snippet-file-aux test-re snippet-dirs) 
     150                            when path return path))) 
     151      ;; if not found in major-mode try to find in all dirs 
     152      (unless found-path 
     153        (setq found-path (anything-c-yas-find-snippet-file-aux (concat "/" key "$") snippet-dirs))) 
     154      found-path))) 
     155 
     156(defun anything-c-yas-find-snippet-file-aux (test-re dirs) 
     157  (loop with done 
     158        with path 
     159        for directory in dirs 
     160        for files = (directory-files directory t) 
     161        unless done 
     162        do (loop for file in files 
     163                 when (string-match test-re file) 
     164                 return (setq done t 
     165                              path file)) 
     166        finally return path)) 
    151167 
    152168(defun anything-c-yas-find-file-snippet-by-template (template &optional other-window) 
     
    157173      (funcall ff-func path)))) 
    158174 
     175(defun anything-c-yas-match (candidate) 
     176  "if customize variable `anything-c-yas-space-match-any-greedy' is non-nil 
     177space match anyword greedy" 
     178  (cond 
     179   (anything-c-yas-space-match-any-greedy 
     180    (let ((re (replace-regexp-in-string "[ \t]+" ".*" anything-pattern))) 
     181      (string-match re candidate))) 
     182   (t 
     183    (string-match anything-pattern candidate)))) 
    159184 
    160185(defvar anything-c-yas-cur-snippets-alist nil) 
     
    184209                                                (anything-c-yas-find-file-snippet-by-template template t))))) 
    185210    (persistent-action . (lambda (template) 
    186                            (anything-c-yas-find-file-snippet-by-template template))))) 
     211                           (anything-c-yas-find-file-snippet-by-template template))) 
     212    (match . (anything-c-yas-match)))) 
     213 
    187214 
    188215 
     
    193220    (anything))) 
    194221 
    195  
    196222(provide 'anything-c-yasnippet) 
    197223;; anything-c-yasnippet.el ends here