mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-13 21:13:04 +00:00
Refactor: yas-load-snippet-buffer
can be called non-interactively
This commit is contained in:
parent
32c65dff47
commit
b4ccb6e956
114
yasnippet.el
114
yasnippet.el
@ -2512,15 +2512,38 @@ 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)
|
||||||
|
ido-mode)
|
||||||
|
'ido-completing-read 'completing-read)))
|
||||||
|
(unless yas--guessed-modes
|
||||||
|
(set (make-local-variable 'yas--guessed-modes)
|
||||||
|
(or (yas--compute-major-mode-and-parents buffer-file-name))))
|
||||||
|
(intern
|
||||||
|
(funcall prompt (format "Choose or enter a table (yas guesses %s): "
|
||||||
|
(if yas--guessed-modes
|
||||||
|
(first yas--guessed-modes)
|
||||||
|
"nothing"))
|
||||||
|
(mapcar #'symbol-name yas--guessed-modes)
|
||||||
|
nil
|
||||||
|
nil
|
||||||
|
nil
|
||||||
|
nil
|
||||||
|
(if (first yas--guessed-modes)
|
||||||
|
(symbol-name (first yas--guessed-modes)))))))
|
||||||
|
|
||||||
With optional prefix argument KILL quit the window and buffer."
|
(defun yas-load-snippet-buffer (table &optional interactive)
|
||||||
(interactive "P")
|
"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
|
(cond
|
||||||
;; We have `yas--editing-template', this buffer's
|
;; We have `yas--editing-template', this buffer's content comes from a
|
||||||
;; content comes from a template which is already loaded and
|
;; template which is already loaded and neatly positioned,...
|
||||||
;; neatly positioned,...
|
|
||||||
;;
|
;;
|
||||||
(yas--editing-template
|
(yas--editing-template
|
||||||
(yas-define-snippets-1 (yas--parse-template (yas--template-file yas--editing-template))
|
(yas-define-snippets-1 (yas--parse-template (yas--template-file yas--editing-template))
|
||||||
@ -2531,57 +2554,40 @@ With optional prefix argument KILL quit the window and buffer."
|
|||||||
(t
|
(t
|
||||||
(unless yas--guessed-modes
|
(unless yas--guessed-modes
|
||||||
(set (make-local-variable 'yas--guessed-modes) (or (yas--compute-major-mode-and-parents buffer-file-name))))
|
(set (make-local-variable 'yas--guessed-modes) (or (yas--compute-major-mode-and-parents buffer-file-name))))
|
||||||
(let* ((prompt (if (and (featurep 'ido)
|
(let* ((table (yas--table-get-create table)))
|
||||||
ido-mode)
|
|
||||||
'ido-completing-read 'completing-read))
|
|
||||||
(table (yas--table-get-create
|
|
||||||
(intern
|
|
||||||
(funcall prompt (format "Choose or enter a table (yas guesses %s): "
|
|
||||||
(if yas--guessed-modes
|
|
||||||
(first yas--guessed-modes)
|
|
||||||
"nothing"))
|
|
||||||
(mapcar #'symbol-name yas--guessed-modes)
|
|
||||||
nil
|
|
||||||
nil
|
|
||||||
nil
|
|
||||||
nil
|
|
||||||
(if (first yas--guessed-modes)
|
|
||||||
(symbol-name (first yas--guessed-modes))))))))
|
|
||||||
(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:
|
|
||||||
;;
|
|
||||||
;; 1) `yas-snippet-dirs' is a list and its first element does not
|
|
||||||
;; 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)))
|
|
||||||
(and (listp yas-snippet-dirs)
|
|
||||||
(second yas-snippet-dirs)
|
|
||||||
(not (string-match (expand-file-name (first yas-snippet-dirs))
|
|
||||||
(yas--template-file yas--editing-template)))))
|
|
||||||
|
|
||||||
(when (y-or-n-p (yas--format "Looks like a library or new snippet. Save to new file? "))
|
(when (and interactive
|
||||||
(let* ((option (first (yas--guess-snippet-directories (yas--template-table yas--editing-template))))
|
(or
|
||||||
(chosen (and option
|
;; Only offer to save this if it looks like a library or new
|
||||||
(yas--make-directory-maybe option))))
|
;; snippet (loaded from elisp, from a dir in `yas-snippet-dirs'
|
||||||
(when chosen
|
;; which is not the first, or from an unwritable file)
|
||||||
(let ((default-file-name (or (and (yas--template-file yas--editing-template)
|
;;
|
||||||
(file-name-nondirectory (yas--template-file yas--editing-template)))
|
(not (yas--template-file yas--editing-template))
|
||||||
(yas--template-name yas--editing-template))))
|
(not (file-writable-p (yas--template-file yas--editing-template)))
|
||||||
(write-file (concat chosen "/"
|
(and (listp yas-snippet-dirs)
|
||||||
(read-from-minibuffer (format "File name to create in %s? " chosen)
|
(second yas-snippet-dirs)
|
||||||
default-file-name)))
|
(not (string-match (expand-file-name (first yas-snippet-dirs))
|
||||||
(setf (yas--template-file yas--editing-template) buffer-file-name))))))
|
(yas--template-file yas--editing-template)))))
|
||||||
(when kill
|
(y-or-n-p (yas--format "Looks like a library or new snippet. Save to new file? ")))
|
||||||
(quit-window kill))
|
(let* ((option (first (yas--guess-snippet-directories (yas--template-table yas--editing-template))))
|
||||||
(yas--message 3 "Snippet \"%s\" loaded for %s."
|
(chosen (and option
|
||||||
(yas--template-name yas--editing-template)
|
(yas--make-directory-maybe option))))
|
||||||
(yas--table-name (yas--template-table yas--editing-template))))
|
(when chosen
|
||||||
|
(let ((default-file-name (or (and (yas--template-file yas--editing-template)
|
||||||
|
(file-name-nondirectory (yas--template-file yas--editing-template)))
|
||||||
|
(yas--template-name yas--editing-template))))
|
||||||
|
(write-file (concat chosen "/"
|
||||||
|
(read-from-minibuffer (format "File name to create in %s? " chosen)
|
||||||
|
default-file-name)))
|
||||||
|
(setf (yas--template-file yas--editing-template) buffer-file-name)))))
|
||||||
|
(when interactive
|
||||||
|
(quit-window interactive)
|
||||||
|
(yas--message 3 "Snippet \"%s\" loaded for %s."
|
||||||
|
(yas--template-name yas--editing-template)
|
||||||
|
(yas--table-name (yas--template-table yas--editing-template)))))
|
||||||
|
|
||||||
|
|
||||||
(defun yas-tryout-snippet (&optional debug)
|
(defun yas-tryout-snippet (&optional debug)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user