| 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. |
| | 61 | pattern regexp: \"if else\" replace to \"if.*else\" |
| | 62 | so match \"if (...) { ... } else { ... }\" and \"if, elsif, else ...\" |
| | 63 | quite convenience") ;; customizable |
| | 64 | |
| | 65 | |
| | 66 | (defvar anything-c-yas-snippets-dir-list nil) |
| 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))) |
| 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) |
| 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))) |
| 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)) |