Fix #597; use SAVE-FILE to visit compiled snippets

Rename the existing FILE field to LOAD-FILE, and add a new SAVE-FILE to
yas--template struct.  Normally they will be the same, but compiled
snippets have only SAVE-FILE because they are loaded by "compiled" code.

* yasnippet.el (yas--template): Remove field file, add load-file and
  save-file.
(yas--parse-template): Update snippet-definition list in docstrings.
(yas-define-snippets): Move LOAD-FILE value to SAVE-FILE when compiling.
(yas--template-get-file): New function.
(yas--visit-snippet-file-1, yas-load-snippet-buffer,
yas-load-snippet-buffer-and-close): Use yas--template-load-file,
yas--template-save-file, or yas--template-get-file, as appropriate.
* doc/snippet-organization.org (The =.yas-compiled-snippet.el= file):
  Remove caveat that is no longer relevant.
This commit is contained in:
Noam Postavsky 2015-09-04 20:19:52 -04:00
parent cc1c758ab7
commit 215ad9bcf2
2 changed files with 44 additions and 41 deletions

View File

@ -123,14 +123,6 @@
Alternatively, you may compile all directories in the list Alternatively, you may compile all directories in the list
=yas-snippet-dirs= with the =yas-recompile-all= function. =yas-snippet-dirs= with the =yas-recompile-all= function.
*Caveat.* At the moment, when you try to use the
=yas-visit-snippet-file= function to edit a compiled snippet loaded
from a =.yas-compiled-snippets.el= file, the content of the snippet
will be opened in a buffer, but it might not be a verbatim copy of
your original snippet, and =yas-load-snippet-buffer-and-close=
won't offer to save to the original snippet file. See
[[https://github.com/capitaomorte/yasnippet/issues/597][#597]].
* TODO The =.yas-skip= file * TODO The =.yas-skip= file
** TODO ** TODO

View File

@ -923,10 +923,11 @@ Honour `yas-dont-activate', which see."
(table (table
key content key content
&optional xname condition group &optional xname condition group
expand-env file xkeybinding xuuid expand-env load-file xkeybinding xuuid save-file
&aux &aux
(name (or xname (name (or xname
(and file (file-name-directory file)) (and load-file (file-name-directory load-file))
(and save-file (file-name-directory save-file))
key)) key))
(keybinding (yas--read-keybinding xkeybinding)) (keybinding (yas--read-keybinding xkeybinding))
(uuid (or xuuid name)) (uuid (or xuuid name))
@ -941,7 +942,8 @@ Honour `yas-dont-activate', which see."
name name
condition condition
expand-env expand-env
file load-file
save-file
keybinding keybinding
uuid uuid
menu-binding-pair menu-binding-pair
@ -1406,7 +1408,7 @@ otherwise we attempt to calculate it from FILE.
Return a snippet-definition, i.e. a list Return a snippet-definition, i.e. a list
(KEY TEMPLATE NAME CONDITION GROUP VARS FILE KEYBINDING UUID) (KEY TEMPLATE NAME CONDITION GROUP VARS LOAD-FILE KEYBINDING UUID)
If the buffer contains a line of \"# --\" then the contents above If the buffer contains a line of \"# --\" then the contents above
this line are ignored. Directives can set most of these with the syntax: this line are ignored. Directives can set most of these with the syntax:
@ -1633,7 +1635,7 @@ Optional PROMPT sets the prompt to use."
SNIPPETS is a list of snippet definitions, each taking the SNIPPETS is a list of snippet definitions, each taking the
following form following form
(KEY TEMPLATE NAME CONDITION GROUP EXPAND-ENV FILE KEYBINDING UUID) (KEY TEMPLATE NAME CONDITION GROUP EXPAND-ENV LOAD-FILE KEYBINDING UUID SAVE-FILE)
Within these, only KEY and TEMPLATE are actually mandatory. Within these, only KEY and TEMPLATE are actually mandatory.
@ -1658,10 +1660,11 @@ the current buffers contents."
(let ((print-length nil)) (let ((print-length nil))
(insert ";;; Snippet definitions:\n;;;\n") (insert ";;; Snippet definitions:\n;;;\n")
(dolist (snippet snippets) (dolist (snippet snippets)
;; We omit file because the snippet will be loaded from ;; Move LOAD-FILE to SAVE-FILE because we will load from the
;; the compiled file instead, so deleting or changing ;; compiled file, not LOAD-FILE.
;; the original won't have any effect. (let ((load-file-cell (nthcdr 6 snippet)))
(setcar (nthcdr 6 snippet) nil)) (setcdr (last snippet) (list (car load-file-cell)))
(setcar (nthcdr 6 snippet) nil)))
(insert (pp-to-string (insert (pp-to-string
`(yas-define-snippets ',mode ',snippets))) `(yas-define-snippets ',mode ',snippets)))
(insert "\n\n")) (insert "\n\n"))
@ -1676,6 +1679,15 @@ the current buffers contents."
;;; Loading snippets from files ;;; Loading snippets from files
(defun yas--template-get-file (template)
"Return TEMPLATE's LOAD-FILE or SAVE-FILE."
(or (yas--template-load-file template)
(let ((file (yas--template-save-file template)))
(when file
(yas--message 2 "%s has no load file, use save file, %s, instead."
(yas--template-name template) file))
file)))
(defun yas--load-yas-setup-file (file) (defun yas--load-yas-setup-file (file)
(if (not yas--creating-compiled-snippets) (if (not yas--creating-compiled-snippets)
;; Normal case. ;; Normal case.
@ -2346,7 +2358,7 @@ visited file in `snippet-mode'."
(defun yas--visit-snippet-file-1 (template) (defun yas--visit-snippet-file-1 (template)
"Helper for `yas-visit-snippet-file'." "Helper for `yas-visit-snippet-file'."
(let ((file (yas--template-file template))) (let ((file (yas--template-get-file template)))
(cond ((and file (file-readable-p file)) (cond ((and file (file-readable-p file))
(find-file-other-window file) (find-file-other-window file)
(snippet-mode) (snippet-mode)
@ -2520,7 +2532,7 @@ When called interactively, prompt for the table name."
;; template which is already loaded and neatly positioned,... ;; template which is already loaded and 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-load-file yas--editing-template))
(yas--template-table yas--editing-template))) (yas--template-table yas--editing-template)))
;; Try to use `yas--guessed-modes'. If we don't have that use the ;; Try to use `yas--guessed-modes'. If we don't have that use the
;; value from `yas--compute-major-mode-and-parents' ;; value from `yas--compute-major-mode-and-parents'
@ -2550,28 +2562,27 @@ 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) (yas-load-snippet-buffer table t)
(when (and (or (let ((file (yas--template-get-file yas--editing-template)))
;; Only offer to save this if it looks like a library or new (when (and (or
;; snippet (loaded from elisp, from a dir in `yas-snippet-dirs' ;; Only offer to save this if it looks like a library or new
;; which is not the first, or from an unwritable file) ;; 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))) (not file)
(and (cdr-safe yas-snippet-dirs) (not (file-writable-p file))
(not (string-prefix-p (expand-file-name (car yas-snippet-dirs)) (and (cdr-safe yas-snippet-dirs)
(yas--template-file yas--editing-template))))) (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? "))) (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))))
(when chosen (when chosen
(let ((default-file-name (or (and (yas--template-file yas--editing-template) (let ((default-file-name (or (and file (file-name-nondirectory file))
(file-name-nondirectory (yas--template-file yas--editing-template))) (yas--template-name yas--editing-template))))
(yas--template-name yas--editing-template)))) (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-load-file yas--editing-template) buffer-file-name))))))
(setf (yas--template-file yas--editing-template) buffer-file-name)))))
(when buffer-file-name (when buffer-file-name
(save-buffer) (save-buffer)
(quit-window kill))) (quit-window kill)))