Refactor: yas-load-snippet-buffer can be called non-interactively

This commit is contained in:
Joao Tavora 2012-08-06 18:39:11 +01:00
parent 32c65dff47
commit b4ccb6e956

View File

@ -2512,29 +2512,14 @@ neither do the elements of PARENTS."
(defvar yas--guessed-modes nil (defvar yas--guessed-modes nil
"List of guessed modes supporting `yas-load-snippet-buffer'.") "List of guessed modes supporting `yas-load-snippet-buffer'.")
(defun yas-load-snippet-buffer (&optional kill) (defun yas--read-table ()
"Parse and load current buffer's snippet definition. "Ask user for a snippet table, help with some guessing."
(let ((prompt (if (and (featurep 'ido)
With optional prefix argument KILL quit the window and buffer."
(interactive "P")
(cond
;; We have `yas--editing-template', this buffer's
;; content comes from a template which is already loaded and
;; neatly positioned,...
;;
(yas--editing-template
(yas-define-snippets-1 (yas--parse-template (yas--template-file yas--editing-template))
(yas--template-table yas--editing-template)))
;; Try to use `yas--guessed-modes'. If we don't have that use the
;; value from `yas--compute-major-mode-and-parents'
;;
(t
(unless yas--guessed-modes
(set (make-local-variable 'yas--guessed-modes) (or (yas--compute-major-mode-and-parents buffer-file-name))))
(let* ((prompt (if (and (featurep 'ido)
ido-mode) ido-mode)
'ido-completing-read 'completing-read)) 'ido-completing-read 'completing-read)))
(table (yas--table-get-create (unless yas--guessed-modes
(set (make-local-variable 'yas--guessed-modes)
(or (yas--compute-major-mode-and-parents buffer-file-name))))
(intern (intern
(funcall prompt (format "Choose or enter a table (yas guesses %s): " (funcall prompt (format "Choose or enter a table (yas guesses %s): "
(if yas--guessed-modes (if yas--guessed-modes
@ -2546,26 +2531,47 @@ With optional prefix argument KILL quit the window and buffer."
nil nil
nil nil
(if (first yas--guessed-modes) (if (first yas--guessed-modes)
(symbol-name (first yas--guessed-modes)))))))) (symbol-name (first yas--guessed-modes)))))))
(defun yas-load-snippet-buffer (table &optional interactive)
"Parse and load current buffer's snippet definition into TABLE.
TABLE is a symbol naming a passed to `yas--table-get-create'.
When called interactively, prompt for the table name and
whether (and where) to save the snippet, then quit the window."
(interactive (list (yas--read-table) t))
(cond
;; We have `yas--editing-template', this buffer's content comes from a
;; template which is already loaded and neatly positioned,...
;;
(yas--editing-template
(yas-define-snippets-1 (yas--parse-template (yas--template-file yas--editing-template))
(yas--template-table yas--editing-template)))
;; Try to use `yas--guessed-modes'. If we don't have that use the
;; value from `yas--compute-major-mode-and-parents'
;;
(t
(unless yas--guessed-modes
(set (make-local-variable 'yas--guessed-modes) (or (yas--compute-major-mode-and-parents buffer-file-name))))
(let* ((table (yas--table-get-create table)))
(set (make-local-variable 'yas--editing-template) (set (make-local-variable 'yas--editing-template)
(yas-define-snippets-1 (yas--parse-template buffer-file-name) (yas-define-snippets-1 (yas--parse-template buffer-file-name)
table))))) table)))))
;; Now, offer to save this iff:
(when (and interactive
(or
;; Only offer to save this if it looks like a library or new
;; snippet (loaded from elisp, from a dir in `yas-snippet-dirs'
;; which is not the first, or from an unwritable file)
;; ;;
;; 1) `yas-snippet-dirs' is a list and its first element does not (not (yas--template-file yas--editing-template))
;; match this template's file (i.e. this is a library snippet, not
;; a user snippet) OR
;;
;; 2) yas--editing-template comes from a file that we cannot write to...
;;
(when (or (not (yas--template-file yas--editing-template))
(not (file-writable-p (yas--template-file yas--editing-template))) (not (file-writable-p (yas--template-file yas--editing-template)))
(and (listp yas-snippet-dirs) (and (listp yas-snippet-dirs)
(second yas-snippet-dirs) (second yas-snippet-dirs)
(not (string-match (expand-file-name (first yas-snippet-dirs)) (not (string-match (expand-file-name (first yas-snippet-dirs))
(yas--template-file yas--editing-template))))) (yas--template-file yas--editing-template)))))
(y-or-n-p (yas--format "Looks like a library or new snippet. Save to new file? ")))
(when (y-or-n-p (yas--format "Looks like a library or new snippet. Save to new file? "))
(let* ((option (first (yas--guess-snippet-directories (yas--template-table yas--editing-template)))) (let* ((option (first (yas--guess-snippet-directories (yas--template-table yas--editing-template))))
(chosen (and option (chosen (and option
(yas--make-directory-maybe option)))) (yas--make-directory-maybe option))))
@ -2576,12 +2582,12 @@ With optional prefix argument KILL quit the window and buffer."
(write-file (concat chosen "/" (write-file (concat chosen "/"
(read-from-minibuffer (format "File name to create in %s? " chosen) (read-from-minibuffer (format "File name to create in %s? " chosen)
default-file-name))) default-file-name)))
(setf (yas--template-file yas--editing-template) buffer-file-name)))))) (setf (yas--template-file yas--editing-template) buffer-file-name)))))
(when kill (when interactive
(quit-window kill)) (quit-window interactive)
(yas--message 3 "Snippet \"%s\" loaded for %s." (yas--message 3 "Snippet \"%s\" loaded for %s."
(yas--template-name yas--editing-template) (yas--template-name yas--editing-template)
(yas--table-name (yas--template-table yas--editing-template)))) (yas--table-name (yas--template-table yas--editing-template)))))
(defun yas-tryout-snippet (&optional debug) (defun yas-tryout-snippet (&optional debug)