From 3ae91e4233bf442f08eb2dbef72d95798b3870bf Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Fri, 15 Jun 2018 10:31:13 +0200 Subject: Upgrade yasnippet --- .emacs.d/lisp/yasnippet.el | 52 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/.emacs.d/lisp/yasnippet.el b/.emacs.d/lisp/yasnippet.el index d478073..d06165f 100644 --- a/.emacs.d/lisp/yasnippet.el +++ b/.emacs.d/lisp/yasnippet.el @@ -2987,6 +2987,18 @@ The last element of POSSIBILITIES may be a list of strings." (funcall fn "Choose: " possibilities)) yas-prompt-functions))) +(defun yas--auto-next () + "Helper for `yas-auto-next'." + (remove-hook 'post-command-hook #'yas--auto-next t) + (yas-next-field)) + +(defmacro yas-auto-next (&rest body) + "Automatically advance to next field after eval'ing BODY." + (declare (indent 0) (debug t)) + `(unless yas-moving-away-p + (prog1 ,@body + (add-hook 'post-command-hook #'yas--auto-next nil t)))) + (defun yas-key-to-value (alist) (unless (or yas-moving-away-p yas-modified-p) @@ -3058,6 +3070,10 @@ other fields." (defvar yas--active-field-overlay nil "Overlays the currently active field.") +(defvar yas--active-snippets nil + "List of currently active snippets") +(make-variable-buffer-local 'yas--active-snippets) + (defvar yas--field-protection-overlays nil "Two overlays protect the current active field.") @@ -3261,12 +3277,19 @@ equivalent to a range covering the whole buffer." (setq beg (point-min) end (point-max))) ((not end) (setq end (1+ beg)))) - (cl-sort - (delete-dups ;; Snippets have multiple overlays. - (delq nil - (mapcar (lambda (ov) (overlay-get ov 'yas--snippet)) - (overlays-in beg end)))) - #'>= :key #'yas--snippet-id)) + (if (and (eq beg (point-min)) + (eq end (point-max))) + yas--active-snippets + ;; Note: don't use `mapcar' here, since it would allocate in + ;; proportion to the amount of overlays, even though the list of + ;; active snippets should be very small. + (let ((snippets nil)) + (dolist (ov (overlays-in beg end)) + (let ((snippet (overlay-get ov 'yas--snippet))) + ;; Snippets have multiple overlays, so check for dups. + (when (and snippet (not (memq snippet snippets))) + (push snippet snippets)))) + (cl-sort snippets #'>= :key #'yas--snippet-id)))) (define-obsolete-function-alias 'yas--snippets-at-point 'yas-active-snippets "0.12") @@ -3426,6 +3449,9 @@ This renders the snippet as ordinary text." ;; (yas--markers-to-points snippet) + ;; It's no longer an active snippet. + (cl-callf2 delq snippet yas--active-snippets) + ;; Take care of snippet revival on undo. (if (and yas-snippet-revival (listp buffer-undo-list)) (push `(apply yas--snippet-revive ,yas-snippet-beg ,yas-snippet-end ,snippet) @@ -3519,13 +3545,12 @@ HOOK should be a symbol, a hook variable, as in `run-hooks'." "Check if point exited the currently active field of the snippet. If so cleans up the whole snippet up." - (let* ((snippets (yas-active-snippets 'all)) - (snippets-left snippets) - (snippet-exit-transform) + (let* ((snippet-exit-transform nil) + (exited-snippets-p nil) ;; Record the custom snippet `yas-after-exit-snippet-hook' ;; set in the expand-env field. (snippet-exit-hook yas-after-exit-snippet-hook)) - (dolist (snippet snippets) + (dolist (snippet yas--active-snippets) (let ((active-field (yas--snippet-active-field snippet))) (yas--letenv (yas--snippet-expand-env snippet) ;; Note: the `force-exit' field could be a transform in case of @@ -3533,10 +3558,10 @@ If so cleans up the whole snippet up." (setq snippet-exit-transform (yas--snippet-force-exit snippet)) (cond ((or snippet-exit-transform (not (and active-field (yas--field-contains-point-p active-field)))) - (setq snippets-left (delete snippet snippets-left)) (setf (yas--snippet-force-exit snippet) nil) (setq snippet-exit-hook yas-after-exit-snippet-hook) - (yas--commit-snippet snippet)) + (yas--commit-snippet snippet) + (setq exited-snippets-p t)) ((and active-field (or (not yas--active-field-overlay) (not (overlay-buffer yas--active-field-overlay)))) @@ -3550,7 +3575,7 @@ If so cleans up the whole snippet up." (yas--update-mirrors snippet))) (t nil))))) - (unless (or (null snippets) snippets-left) + (unless (or yas--active-snippets (not exited-snippets-p)) (when snippet-exit-transform (yas--eval-for-effect snippet-exit-transform)) (let ((yas-after-exit-snippet-hook snippet-exit-hook)) @@ -4043,6 +4068,7 @@ Returns the newly created snippet." ;; Move to end (goto-char (point-max)) + (push snippet yas--active-snippets) snippet)))) -- cgit v1.3