From 69979220116a0adbb24f575e5f3bd57b6523ab66 Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Mon, 14 Oct 2013 09:57:11 -0400 Subject: [PATCH 1/3] Revert "inhibit change hooks during (insert content)" This reverts commit 90d4cae1b1bbfacc612e73444f1e5a723ae4036e. --- yasnippet.el | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/yasnippet.el b/yasnippet.el index 8149acc..af45262 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -3523,11 +3523,9 @@ considered when expanding the snippet." (setq snippet (if expand-env (eval `(let* ,expand-env - (let ((inhibit-modification-hooks t)) - (insert content)) + (insert content) (yas--snippet-create (point-min)))) - (let ((inhibit-modification-hooks t)) - (insert content)) + (insert content) (yas--snippet-create (point-min))))))) ;; stacked-expansion: This checks for stacked expansion, save the From 51e9e8e53c7fe6abb58466fb8cb5a0ad133a86c6 Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Mon, 14 Oct 2013 09:57:41 -0400 Subject: [PATCH 2/3] wait till after content insertion to narrow Fixes #404 without inhibiting change hooks. --- yasnippet.el | 58 ++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/yasnippet.el b/yasnippet.el index af45262..5ca5978 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -3511,22 +3511,20 @@ considered when expanding the snippet." ;; plain text will get recorded at the end. ;; ;; stacked expansion: also shoosh the overlay modification hooks - (save-restriction - (narrow-to-region start start) - (let ((buffer-undo-list t)) - ;; snippet creation might evaluate users elisp, which - ;; might generate errors, so we have to be ready to catch - ;; them mostly to make the undo information - ;; - (setq yas--start-column (save-restriction (widen) (current-column))) - (yas--inhibit-overlay-hooks - (setq snippet - (if expand-env - (eval `(let* ,expand-env - (insert content) - (yas--snippet-create (point-min)))) - (insert content) - (yas--snippet-create (point-min))))))) + (let ((buffer-undo-list t)) + ;; snippet creation might evaluate users elisp, which + ;; might generate errors, so we have to be ready to catch + ;; them mostly to make the undo information + ;; + (setq yas--start-column (current-column)) + (yas--inhibit-overlay-hooks + (setq snippet + (if expand-env + (eval `(let* ,expand-env + (insert content) + (yas--snippet-create start (point)))) + (insert content) + (yas--snippet-create start (point)))))) ;; stacked-expansion: This checks for stacked expansion, save the ;; `yas--previous-active-field' and advance its boundary. @@ -3604,25 +3602,27 @@ After revival, push the `yas--take-care-of-redo' in the (push `(apply yas--take-care-of-redo ,beg ,end ,snippet) buffer-undo-list)))) -(defun yas--snippet-create (begin) - "Create a snippet from a template inserted at BEGIN. +(defun yas--snippet-create (begin end) + "Create a snippet from a template inserted at BEGIN to END. Returns the newly created snippet." - (let ((snippet (yas--make-snippet))) - (goto-char begin) - (yas--snippet-parse-create snippet) + (save-restriction + (narrow-to-region begin end) + (let ((snippet (yas--make-snippet))) + (goto-char begin) + (yas--snippet-parse-create snippet) - ;; Sort and link each field - (yas--snippet-sort-fields snippet) + ;; Sort and link each field + (yas--snippet-sort-fields snippet) - ;; Create keymap overlay for snippet - (setf (yas--snippet-control-overlay snippet) - (yas--make-control-overlay snippet (point-min) (point-max))) + ;; Create keymap overlay for snippet + (setf (yas--snippet-control-overlay snippet) + (yas--make-control-overlay snippet (point-min) (point-max))) - ;; Move to end - (goto-char (point-max)) + ;; Move to end + (goto-char (point-max)) - snippet)) + snippet))) ;;; Apropos adjacencies and "fom's": From 5c76a0fda29479d4566fded3eff0e55727bd5eb4 Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Mon, 14 Oct 2013 11:45:40 -0400 Subject: [PATCH 3/3] test middle-of-buffer snippet insertion --- yasnippet-tests.el | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/yasnippet-tests.el b/yasnippet-tests.el index c5d8738..fd75063 100644 --- a/yasnippet-tests.el +++ b/yasnippet-tests.el @@ -213,6 +213,15 @@ (yas-expand-snippet snippet) (should (string= (yas--buffer-contents) "#include \nmain"))))) +(ert-deftest middle-of-buffer-snippet-insertion () + (with-temp-buffer + (yas-minor-mode 1) + (insert "beginning") + (save-excursion (insert "end")) + (let ((snippet "-middle-")) + (yas-expand-snippet snippet)) + (should (string= (yas--buffer-contents) "beginning-middle-end")))) + (ert-deftest another-example-for-issue-271 () ;; expect this to fail in batch mode since `region-active-p' doesn't ;; used by `yas-expand-snippet' doesn't make sense in that context.