branch: master commit beb2ba4d7feceaa3669035ce24c23ed9e805a9a0 Author: Noam Postavsky <npost...@users.sourceforge.net> Commit: Noam Postavsky <npost...@users.sourceforge.net>
Fix fields following $0 + region insertion When inserting text due to a non-nil yas-wrap-around-region and the snippet had fields occuring later in the buffer than $0, the insertion of the text was shifting the location of later fields, thus invalidating the calculation of yas--dollar-regions. * yasnippet.el (yas--simple-mirror-parse-create): Don't insert text fo yas-wrap-around-region here. (yas--snippet-parse-create): Do it here, after deleting `yas--dollar-regions`, instead. --- yasnippet.el | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/yasnippet.el b/yasnippet.el index c1a366b..3446060 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -3862,7 +3862,22 @@ Meant to be called in a narrowed buffer, does various passes" (yas--calculate-adjacencies snippet) ;; Delete $-constructs ;; - (save-restriction (widen) (yas--delete-regions yas--dollar-regions)) + (save-restriction + (widen) + (yas--delete-regions yas--dollar-regions)) + ;; Make sure to do this insertion *after* deleting the dollar + ;; regions, otherwise we invalidate the calculated positions of + ;; all the fields following $0. + (let ((exit (yas--snippet-exit snippet))) + (goto-char (if exit (yas--exit-marker exit) (point-max)))) + (when (eq yas-wrap-around-region 'cua) + (setq yas-wrap-around-region ?0)) + (cond ((and yas-wrap-around-region yas-selected-text) + (insert yas-selected-text)) + ((and (characterp yas-wrap-around-region) + (get-register yas-wrap-around-region)) + (insert (prog1 (get-register yas-wrap-around-region) + (set-register yas-wrap-around-region nil))))) ;; restore backquoted expression values ;; (yas--restore-backquotes) @@ -4131,21 +4146,10 @@ When multiple expressions are found, only the last one counts." (while (re-search-forward yas--simple-mirror-regexp nil t) (let ((number (string-to-number (match-string-no-properties 1)))) (cond ((zerop number) - (setf (yas--snippet-exit snippet) (yas--make-exit (yas--make-marker (match-end 0)))) - (save-excursion - (goto-char (match-beginning 0)) - (when (eq yas-wrap-around-region 'cua) - (setq yas-wrap-around-region ?0)) - (cond ((and yas-wrap-around-region yas-selected-text) - (insert yas-selected-text)) - ((and (characterp yas-wrap-around-region) - (get-register yas-wrap-around-region)) - (insert (prog1 (get-register yas-wrap-around-region) - (set-register yas-wrap-around-region nil))))) - (push (cons (point) (yas--exit-marker (yas--snippet-exit snippet))) - yas--dollar-regions))) + (push (cons (match-beginning 0) (yas--exit-marker (yas--snippet-exit snippet))) + yas--dollar-regions)) (t (let ((field (yas--snippet-find-field snippet number))) (if field