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
|
||||
"List of guessed modes supporting `yas-load-snippet-buffer'.")
|
||||
|
||||
(defun yas-load-snippet-buffer (&optional kill)
|
||||
"Parse and load current buffer's snippet definition.
|
||||
(defun yas--read-table ()
|
||||
"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."
|
||||
(interactive "P")
|
||||
(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,...
|
||||
;; 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))
|
||||
@ -2531,57 +2554,40 @@ With optional prefix argument KILL quit the window and buffer."
|
||||
(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-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))))))))
|
||||
(let* ((table (yas--table-get-create table)))
|
||||
(set (make-local-variable 'yas--editing-template)
|
||||
(yas-define-snippets-1 (yas--parse-template buffer-file-name)
|
||||
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? "))
|
||||
(let* ((option (first (yas--guess-snippet-directories (yas--template-table yas--editing-template))))
|
||||
(chosen (and option
|
||||
(yas--make-directory-maybe option))))
|
||||
(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 kill
|
||||
(quit-window kill))
|
||||
(yas--message 3 "Snippet \"%s\" loaded for %s."
|
||||
(yas--template-name yas--editing-template)
|
||||
(yas--table-name (yas--template-table yas--editing-template))))
|
||||
(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)
|
||||
;;
|
||||
(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)))))
|
||||
(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))))
|
||||
(chosen (and option
|
||||
(yas--make-directory-maybe option))))
|
||||
(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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user