mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-13 13:13:03 +00:00
Fix #692: Simplify `yas-new-snippet' load&save logic
Two significant changes: 1. The criteria for offering to save in upon `yas-load-snippet-buffer-and-close' is much simplified, and relies on `buffer-modified-p' solely. 2. C-x C-s snippet after `yas-new-snippet' or `yas-visit-snippet' suggests at least a sensible directory. Still missing in this commit is the suggestion for loading upon saving. The `after-save-hook' could be used for something like that. * yasnippet.el (yas-new-snippet): Set `default-directory' based on result of `yas--guess-snippet-directories'. (yas-load-snippet-buffer): Return template object. (yas-load-snippet-buffer-and-close): Simplify. (yas--visit-snippet-file-1): Also set `default-directory' here.
This commit is contained in:
parent
497867cea5
commit
87a7cd6f37
45
yasnippet.el
45
yasnippet.el
@ -2412,7 +2412,9 @@ visited file in `snippet-mode'."
|
|||||||
(pp-to-string (yas--template-content template))
|
(pp-to-string (yas--template-content template))
|
||||||
(yas--template-content template))))
|
(yas--template-content template))))
|
||||||
(snippet-mode)
|
(snippet-mode)
|
||||||
(set (make-local-variable 'yas--editing-template) template)))))
|
(set (make-local-variable 'yas--editing-template) template)
|
||||||
|
(set (make-local-variable 'default-directory)
|
||||||
|
(car (cdr (car (yas--guess-snippet-directories (yas--template-table template))))))))))
|
||||||
|
|
||||||
(defun yas--guess-snippet-directories-1 (table)
|
(defun yas--guess-snippet-directories-1 (table)
|
||||||
"Guess possible snippet subdirectories for TABLE."
|
"Guess possible snippet subdirectories for TABLE."
|
||||||
@ -2483,6 +2485,7 @@ NO-TEMPLATE is non-nil."
|
|||||||
(set (make-local-variable 'yas--guessed-modes) (mapcar #'(lambda (d)
|
(set (make-local-variable 'yas--guessed-modes) (mapcar #'(lambda (d)
|
||||||
(yas--table-mode (car d)))
|
(yas--table-mode (car d)))
|
||||||
guessed-directories))
|
guessed-directories))
|
||||||
|
(set (make-local-variable 'default-directory) (car (cdr (car guessed-directories))))
|
||||||
(if (and (not no-template) yas-new-snippet-default)
|
(if (and (not no-template) yas-new-snippet-default)
|
||||||
(yas-expand-snippet yas-new-snippet-default))))
|
(yas-expand-snippet yas-new-snippet-default))))
|
||||||
|
|
||||||
@ -2549,7 +2552,8 @@ neither do the elements of PARENTS."
|
|||||||
(defun yas-load-snippet-buffer (table &optional interactive)
|
(defun yas-load-snippet-buffer (table &optional interactive)
|
||||||
"Parse and load current buffer's snippet definition into TABLE.
|
"Parse and load current buffer's snippet definition into TABLE.
|
||||||
TABLE is a symbol name passed to `yas--table-get-create'. When
|
TABLE is a symbol name passed to `yas--table-get-create'. When
|
||||||
called interactively, prompt for the table name."
|
called interactively, prompt for the table name.
|
||||||
|
Return the `yas--template' object created"
|
||||||
(interactive (list (yas--read-table) t))
|
(interactive (list (yas--read-table) t))
|
||||||
(cond
|
(cond
|
||||||
;; We have `yas--editing-template', this buffer's content comes from a
|
;; We have `yas--editing-template', this buffer's content comes from a
|
||||||
@ -2571,12 +2575,13 @@ called interactively, prompt for the table name."
|
|||||||
(when interactive
|
(when 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))))
|
||||||
|
yas--editing-template)
|
||||||
|
|
||||||
(defun yas-load-snippet-buffer-and-close (table &optional kill)
|
(defun yas-load-snippet-buffer-and-close (table &optional kill)
|
||||||
"Load and save the snippet, then `quit-window' if saved.
|
"Load and save the snippet, then `quit-window' if saved.
|
||||||
Loading is performed by `yas-load-snippet-buffer'. If the
|
Loading is performed by `yas-load-snippet-buffer'. If the
|
||||||
snippet is new, ask the user whether (and where) to save it. If
|
snippet is new, ask then user whether (and where) to save it. If
|
||||||
the snippet already has a file, just save it.
|
the snippet already has a file, just save it.
|
||||||
|
|
||||||
The prefix argument KILL is passed to `quit-window'.
|
The prefix argument KILL is passed to `quit-window'.
|
||||||
@ -2584,31 +2589,13 @@ The prefix argument KILL is passed to `quit-window'.
|
|||||||
Don't use this from a Lisp program, call `yas-load-snippet-buffer'
|
Don't use this from a Lisp program, call `yas-load-snippet-buffer'
|
||||||
and `kill-buffer' instead."
|
and `kill-buffer' instead."
|
||||||
(interactive (list (yas--read-table) current-prefix-arg))
|
(interactive (list (yas--read-table) current-prefix-arg))
|
||||||
(yas-load-snippet-buffer table t)
|
(let ((template (yas-load-snippet-buffer table t)))
|
||||||
(let ((file (yas--template-get-file yas--editing-template)))
|
(when (and (buffer-modified-p)
|
||||||
(when (and (or
|
(y-or-n-p
|
||||||
;; Only offer to save this if it looks like a library or new
|
(format "[yas] Loaded for %s. Also save snippet buffer?"
|
||||||
;; snippet (loaded from elisp, from a dir in `yas-snippet-dirs'
|
(yas--table-name (yas--template-table template)))))
|
||||||
;; which is not the first, or from an unwritable file)
|
(let ((default-directory (car (cdr (car (yas--guess-snippet-directories (yas--template-table template)))))))
|
||||||
;;
|
(save-buffer)))
|
||||||
(not file)
|
|
||||||
(not (file-writable-p file))
|
|
||||||
(and (cdr-safe yas-snippet-dirs)
|
|
||||||
(not (string-prefix-p (expand-file-name (car yas-snippet-dirs)) file))))
|
|
||||||
(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 file (file-name-nondirectory file))
|
|
||||||
(yas--template-name yas--editing-template))))
|
|
||||||
(write-file (expand-file-name
|
|
||||||
(read-file-name (format "File name to create in %s? " chosen)
|
|
||||||
chosen default-file-name)
|
|
||||||
chosen))
|
|
||||||
(setf (yas--template-load-file yas--editing-template) buffer-file-name))))))
|
|
||||||
(when buffer-file-name
|
|
||||||
(save-buffer)
|
|
||||||
(quit-window kill)))
|
(quit-window kill)))
|
||||||
|
|
||||||
(defun yas-tryout-snippet (&optional debug)
|
(defun yas-tryout-snippet (&optional debug)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user