Changeset 8612

Show
Ignore:
Timestamp:
04/01/08 22:21:43 (5 years ago)
Author:
imakado
Message:

lang/elisp/anything-c-yasnippet/anything-c-yasnippet.el:
defvar -> defcustom
ActionsにCreate? new snippet on region、Reload All Snippts、Rename snippet file、Delete snippet fileを追加。
ライセンスを書いた。
バージョンを書いた。(0.3)

Files:
1 modified

Legend:

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

    r8389 r8612  
    11;;; anything-c-yasnippet.el --- anything config for yasnippet.el 
    2 ;; TODO: defvar => defcustom 
     2 
     3;; Author: Kenji.I (Kenji Imakado) <ken.imakaado@gmail.com> 
     4;; Version: 0.3 
     5;; Keywords: anything yasnippet 
     6 
     7;; This file is free software; you can redistribute it and/or modify 
     8;; it under the terms of the GNU General Public License as published by 
     9;; the Free Software Foundation; either version 2, or (at your option) 
     10;; any later version. 
     11 
     12;; This file is distributed in the hope that it will be useful, 
     13;; but WITHOUT ANY WARRANTY; without even the implied warranty of 
     14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     15;; GNU General Public License for more details. 
     16 
     17;; You should have received a copy of the GNU General Public License 
     18;; along with GNU Emacs; see the file COPYING.  If not, write to the 
     19;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
     20;; Boston, MA 02110-1301, USA. 
    321 
    422;;; Commentary: 
     
    5371;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
    5472 
    55 (defvar anything-c-yas-appear-rule nil) ;; customizable e.i (add-to-list 'anything-c-yas-appear-rule '(ruby-mode . (text-mode html-mode))) 
    56 (defvar anything-c-yas-not-display-dups t) ;; customizable 
    57 (defvar anything-c-yas-display-msg-after-complete t) ;; customizable 
    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. 
     73;;; Code 
     74(defvar anything-c-yas-version "0.3" "Version of anything-c-yasnippet") 
     75 
     76(defgroup anything-c-yasnippet nil 
     77  "anything config yasnippet") 
     78 
     79(defcustom anything-c-yas-not-display-dups t 
     80  "if non-nil not display duplicate snippet otherwise display all snippet" 
     81  :type 'boolean 
     82  :group 'anything-c-yasnippet) 
     83 
     84(defcustom anything-c-yas-display-msg-after-complete t 
     85  "if non-nil display snippet key message in minibuffer after Complete" 
     86  :type 'boolean 
     87  :group 'anything-c-yasnippet) 
     88 
     89;(defvar anything-c-yas-snippets-dir-list nil) ;;customizable list of path(string) for finding snippet file in Action 
     90(defcustom anything-c-yas-snippets-dir-list nil 
     91  "list of directory used to find snippet file" 
     92  :type '(repeat (directory 
     93                  :tag "snippet-directory")) 
     94  :group 'anything-c-yasnippet) 
     95 
     96(defcustom anything-c-yas-space-match-any-greedy nil 
     97  "if non-nil anything pattern space match anyword greedy. 
    6198pattern regexp: \"if else\" replace to \"if.*else\" 
    62 so match \"if (...) { ... } else { ... }\" and \"if, elsif, else ...\" 
    63 quite convenience") ;; customizable 
     99match \"if (...) { ... } else { ... }\" and \"if, elsif, else ...\" 
     100quite convenience 
     101Default: nil" 
     102  :type 'boolean 
     103  :group 'anything-c-yasnippet) 
    64104 
    65105 
     
    71111  ad-do-it) 
    72112 
     113  
     114(defun anything-c-yas-create-new-snippet (selected-text) 
     115  (let* ((mode-name (symbol-name anything-c-yas-cur-major-mode)) 
     116         (root-dir (expand-file-name yas/root-directory)) 
     117         (default-snippet-dir (anything-c-yas-find-recursively mode-name root-dir 'dir)) 
     118         (dir (read-file-name "create snippet : " default-snippet-dir default-snippet-dir))) 
     119    (condition-case e 
     120        (progn (when (file-exists-p dir) 
     121                 (error "can't create file [%s] already exists" (file-name-nondirectory dir))) 
     122               ;; create buffer, insert template 
     123               (find-file dir) 
     124               (insert "#name : \n# --\n " selected-text)) 
     125      (message "%s" (error-message-string e))))) 
     126 
     127(defun anything-c-yas-find-recursively (regexp &optional directory predicate) 
     128  (let ((directory (or directory default-directory)) 
     129        (predfunc (case predicate 
     130                    (dir 'file-directory-p) 
     131                    (file 'file-regular-p) 
     132                    (otherwise 'identity))) 
     133        (files (remove-if (lambda (s) (string-match "^\\." (file-name-nondirectory  s))) (directory-files directory t))) 
     134        (found nil) 
     135        (result nil)) 
     136    (loop for file in files 
     137          unless found 
     138          do (if (and (funcall predfunc file) 
     139                      (string-match regexp file)) 
     140                 (progn (setq found t) 
     141                        (return (file-name-as-directory file))) 
     142               (when (file-directory-p file) 
     143                 (setq result (anything-c-yas-find-recursively regexp file predicate)))) 
     144          finally (return result)))) 
    73145 
    74146(defun anything-c-yas-build-cur-snippets-alist (&optional table) 
     
    106178(defun anything-c-yas-get-modes () 
    107179  (let ((cur-major-mode anything-c-yas-cur-major-mode)) 
    108     (delete-dups (cons cur-major-mode (assoc-default cur-major-mode anything-c-yas-appear-rule))))) 
     180    (list cur-major-mode))) 
    109181 
    110182(defun anything-c-yas-get-cmp-context () 
     
    116188    (condition-case nil 
    117189        (save-excursion 
     190          (when mark-active 
     191            (error "")) 
    118192          (skip-syntax-backward syntax) 
    119193          (setq start (point)) 
     
    166240 
    167241(defun anything-c-yas-find-file-snippet-by-template (template &optional other-window) 
     242  (let* ((path (anything-c-yas-get-path-by-template template)) 
     243         (ff-func (if other-window 'find-file-other-window 'find-file))) 
     244    (if path 
     245        (funcall ff-func path) 
     246      (message "not found snippet file")))) 
     247 
     248(defun anything-c-yas-get-path-by-template (template) 
    168249  (let* ((key (anything-c-yas-get-key-by-template template anything-c-yas-cur-snippets-alist)) 
    169          (path (anything-c-yas-find-snippet-file-by-key key)) 
    170          (ff-func (if other-window 'find-file-other-window 'find-file))) 
    171     (when path 
    172       (funcall ff-func path)))) 
     250         (path (anything-c-yas-find-snippet-file-by-key key))) 
     251    path)) 
    173252 
    174253(defun anything-c-yas-match (candidate) 
     
    187266(defvar anything-c-yas-point-end nil) 
    188267(defvar anything-c-yas-cur-major-mode nil) 
     268(defvar anything-c-yas-selected-text "" "region text if mark-active otherwise \"\"")  
    189269(defvar anything-c-source-yasnippet 
    190270  `((name . "Yasnippet") 
    191271    (init . (lambda () 
    192272              (setq anything-c-yas-cur-major-mode major-mode) 
    193               (multiple-value-setq (anything-c-yas-initial-input anything-c-yas-point-start anything-c-yas-point-end) 
    194                                    (anything-c-yas-get-cmp-context)) ;return values(str point point) 
     273              (setq anything-c-yas-selected-text (if mark-active (buffer-substring-no-properties (region-beginning) (region-end)) "")) 
     274              (multiple-value-setq 
     275                  (anything-c-yas-initial-input anything-c-yas-point-start anything-c-yas-point-end) (anything-c-yas-get-cmp-context)) ;return values(str point point) 
    195276              (setq anything-c-yas-cur-snippets-alist (anything-c-yas-build-cur-snippets-alist)))) 
    196     (candidates . (lambda () 
    197                     (anything-c-yas-get-candidates anything-c-yas-cur-snippets-alist))) 
     277;;     (candidates . (lambda () 
     278;;                     (anything-c-yas-get-candidates anything-c-yas-cur-snippets-alist))) 
     279    (candidates . (anything-c-yas-get-candidates anything-c-yas-cur-snippets-alist)) 
    198280    (candidate-transformer . (lambda (candidates) 
    199281                               (anything-c-yas-get-transformed-list anything-c-yas-cur-snippets-alist anything-c-yas-initial-input))) 
     
    204286                                                (anything-c-yas-get-key-by-template template anything-c-yas-cur-snippets-alist))))) 
    205287               ("Open snippet file" . (lambda (template) 
    206                                    (anything-c-yas-find-file-snippet-by-template template))) 
     288                                        (anything-c-yas-find-file-snippet-by-template template))) 
    207289               ("Open snippet file other window" . (lambda (template) 
    208                                                 (anything-c-yas-find-file-snippet-by-template template t))))) 
     290                                                     (anything-c-yas-find-file-snippet-by-template template t))) 
     291               ("Create new snippet on region" . (lambda (template) 
     292                                                   (anything-c-yas-create-new-snippet anything-c-yas-selected-text))) 
     293               ("Reload All Snippts" . (lambda (template) 
     294                                         (yas/reload-all) 
     295                                         (message "Reload All Snippts done"))) 
     296               ("Rename snippet file" . (lambda (template) 
     297                                       (let* ((path (or (anything-c-yas-get-path-by-template template) "")) 
     298                                              (dir (file-name-directory path)) 
     299                                              (filename (file-name-nondirectory path)) 
     300                                              (rename-to (read-string (concat "rename [" filename "] to: ")))) 
     301                                         (rename-file path (concat dir rename-to)) 
     302                                         (yas/reload-all)))) 
     303               ("Delete snippet file" . (lambda (template) 
     304                                          (let ((path (or (anything-c-yas-get-path-by-template template) ""))) 
     305                                            (when (y-or-n-p "really delete?") 
     306                                              (delete-file path) 
     307                                              (yas/reload-all))))))) 
    209308    (persistent-action . (lambda (template) 
    210309                           (anything-c-yas-find-file-snippet-by-template template))) 
    211310    (match . (anything-c-yas-match)))) 
    212311 
    213  
    214  
    215 ;;; Command 
     312  
     313;;; Commands 
    216314(defun anything-c-yas-complete () 
    217315  (interactive) 
     
    219317    (anything))) 
    220318 
     319(defun anything-c-yas-create-snippet-on-regin (&optional start end) 
     320  (interactive "r") 
     321  (let ((str (buffer-substring-no-properties start end))) 
     322    (anything-c-yas-create-new-snippet str))) 
     323   
     324 
    221325(provide 'anything-c-yasnippet) 
    222326;; anything-c-yasnippet.el ends here