mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-14 13:33:04 +00:00
Merge branch '0.8-fsf-changes'
This commit is contained in:
commit
f28a3df702
2
.gitignore
vendored
2
.gitignore
vendored
@ -6,3 +6,5 @@ extras/imported/**
|
|||||||
!extras/imported/*/.yas-setup.el
|
!extras/imported/*/.yas-setup.el
|
||||||
.yas-compiled-snippets.el
|
.yas-compiled-snippets.el
|
||||||
*.elc
|
*.elc
|
||||||
|
ert-x.*
|
||||||
|
ert.*
|
||||||
|
@ -424,9 +424,9 @@ TODO: be meaner"
|
|||||||
(ert-deftest test-yas-tab-binding ()
|
(ert-deftest test-yas-tab-binding ()
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
(yas-minor-mode -1)
|
(yas-minor-mode -1)
|
||||||
(should (not (eq (key-binding (yas--read-keybinding yas-trigger-key)) 'yas-expand)))
|
(should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
|
||||||
(yas-minor-mode 1)
|
(yas-minor-mode 1)
|
||||||
(should (eq (key-binding (yas--read-keybinding yas-trigger-key)) 'yas-expand))
|
(should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
|
||||||
(yas-expand-snippet "$1 $2 $3")
|
(yas-expand-snippet "$1 $2 $3")
|
||||||
(dolist (k (if (listp yas-next-field-key)
|
(dolist (k (if (listp yas-next-field-key)
|
||||||
yas-next-field-key
|
yas-next-field-key
|
||||||
@ -441,7 +441,7 @@ TODO: be meaner"
|
|||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
(org-mode)
|
(org-mode)
|
||||||
(yas-minor-mode 1)
|
(yas-minor-mode 1)
|
||||||
(should (eq (key-binding (yas--read-keybinding yas-trigger-key)) 'yas-expand))))
|
(should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))))
|
||||||
|
|
||||||
;;; Helpers
|
;;; Helpers
|
||||||
;;;
|
;;;
|
||||||
@ -515,7 +515,7 @@ TODO: be meaner"
|
|||||||
(progn
|
(progn
|
||||||
(mapc #'yas-make-file-or-dirs dirs)
|
(mapc #'yas-make-file-or-dirs dirs)
|
||||||
(funcall fn))
|
(funcall fn))
|
||||||
(when (>= emacs-major-version 23)
|
(when (>= emacs-major-version 24)
|
||||||
(delete-directory default-directory 'recursive))))))
|
(delete-directory default-directory 'recursive))))))
|
||||||
|
|
||||||
(defmacro yas-with-snippet-dirs (dirs &rest body)
|
(defmacro yas-with-snippet-dirs (dirs &rest body)
|
||||||
|
99
yasnippet.el
99
yasnippet.el
@ -236,8 +236,8 @@ Naturally this is only valid when `yas-indent-line' is `auto'"
|
|||||||
:type 'boolean
|
:type 'boolean
|
||||||
:group 'yasnippet)
|
:group 'yasnippet)
|
||||||
|
|
||||||
(defcustom yas-trigger-key "TAB"
|
(defcustom yas-trigger-key "<tab>"
|
||||||
"The key bound to `yas-expand' when function `yas-minor-mode' is active.
|
"The key bound to `yas-expand' when `yas-minor-mode' is active.
|
||||||
|
|
||||||
Value is a string that is converted to the internal Emacs key
|
Value is a string that is converted to the internal Emacs key
|
||||||
representation using `read-kbd-macro'."
|
representation using `read-kbd-macro'."
|
||||||
@ -2257,6 +2257,18 @@ expand immediately. Common gateway for
|
|||||||
end
|
end
|
||||||
(yas--template-expand-env yas--current-template)))))
|
(yas--template-expand-env yas--current-template)))))
|
||||||
|
|
||||||
|
(defun yas--trigger-key-for-fallback ()
|
||||||
|
;; When `yas-trigger-key' is <tab> it correctly overrides
|
||||||
|
;; org-mode's <tab>, for example and searching for fallbacks
|
||||||
|
;; correctly returns `org-cycle'. However, most other modes bind
|
||||||
|
;; "TAB" (which is translated from <tab>), and calling
|
||||||
|
;; (key-binding "TAB") does not place return that command into
|
||||||
|
;; our command-2 local. So we cheat.
|
||||||
|
;;
|
||||||
|
(if (string= yas-trigger-key "<tab>")
|
||||||
|
"TAB"
|
||||||
|
yas-trigger-key))
|
||||||
|
|
||||||
(defun yas--fallback (&optional from-trigger-key-p)
|
(defun yas--fallback (&optional from-trigger-key-p)
|
||||||
"Fallback after expansion has failed.
|
"Fallback after expansion has failed.
|
||||||
|
|
||||||
@ -2268,6 +2280,7 @@ Common gateway for `yas-expand-from-trigger-key' and
|
|||||||
((eq yas-fallback-behavior 'call-other-command)
|
((eq yas-fallback-behavior 'call-other-command)
|
||||||
(let* ((yas-minor-mode nil)
|
(let* ((yas-minor-mode nil)
|
||||||
(yas--direct-keymaps nil)
|
(yas--direct-keymaps nil)
|
||||||
|
(yas-trigger-key (yas--trigger-key-for-fallback))
|
||||||
(keys-1 (this-command-keys-vector))
|
(keys-1 (this-command-keys-vector))
|
||||||
(keys-2 (and yas-trigger-key
|
(keys-2 (and yas-trigger-key
|
||||||
from-trigger-key-p
|
from-trigger-key-p
|
||||||
@ -2513,29 +2526,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
|
||||||
@ -2547,26 +2545,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))))
|
||||||
@ -2577,12 +2596,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)
|
||||||
@ -4216,7 +4235,7 @@ When multiple expressions are found, only the last one counts."
|
|||||||
(let ((fallback-description
|
(let ((fallback-description
|
||||||
(cond ((eq yas-fallback-behavior 'call-other-command)
|
(cond ((eq yas-fallback-behavior 'call-other-command)
|
||||||
(let* ((yas-minor-mode nil)
|
(let* ((yas-minor-mode nil)
|
||||||
(fallback (key-binding (read-kbd-macro yas-trigger-key))))
|
(fallback (key-binding (read-kbd-macro (yas--trigger-key-for-fallback)))))
|
||||||
(or (and fallback
|
(or (and fallback
|
||||||
(format " call command `%s'." (pp-to-string fallback)))
|
(format " call command `%s'." (pp-to-string fallback)))
|
||||||
" do nothing.")))
|
" do nothing.")))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user