Warn about backquote exprs modifying the buffer

* yasnippet.el (yas--save-backquotes): Show a warning if evaluating the
backquote expression modifies the buffer.
This commit is contained in:
Noam Postavsky 2016-06-21 22:06:30 -04:00
parent ee4efdbbde
commit 85f39cec2b

View File

@ -4017,20 +4017,33 @@ With optional string TEXT do it in string instead of the buffer."
(defun yas--save-backquotes () (defun yas--save-backquotes ()
"Save all the \"`(lisp-expression)`\"-style expressions "Save all the \"`(lisp-expression)`\"-style expressions
with their evaluated value into `yas--backquote-markers-and-strings'." with their evaluated value into `yas--backquote-markers-and-strings'."
(while (re-search-forward yas--backquote-lisp-expression-regexp nil t) (let* ((yas--change-detected nil)
(let ((current-string (match-string-no-properties 1)) transformed) (detect-change (lambda (_beg _end) (setq yas--change-detected t))))
(save-restriction (widen) (while (re-search-forward yas--backquote-lisp-expression-regexp nil t)
(delete-region (match-beginning 0) (match-end 0))) (let ((current-string (match-string-no-properties 1)) transformed)
(setq transformed (yas--eval-lisp (yas--read-lisp (yas--restore-escapes current-string '(?`))))) (save-restriction (widen)
(goto-char (match-beginning 0)) (delete-region (match-beginning 0) (match-end 0)))
(when transformed (let ((before-change-functions
(let ((marker (make-marker))) (cons detect-change before-change-functions)))
(save-restriction (setq transformed (yas--eval-lisp (yas--read-lisp
(widen) (yas--restore-escapes
(insert "Y") ;; quite horrendous, I love it :) current-string '(?`))))))
(set-marker marker (point)) (goto-char (match-beginning 0))
(insert "Y")) (when transformed
(push (cons marker transformed) yas--backquote-markers-and-strings)))))) (let ((marker (make-marker))
(before-change-functions (cdr before-change-functions)))
(save-restriction
(widen)
(insert "Y") ;; quite horrendous, I love it :)
(set-marker marker (point))
(insert "Y"))
(push (cons marker transformed) yas--backquote-markers-and-strings)))))
(when yas--change-detected
(lwarn '(yasnippet backquote-change) :warning
"`%s' modified buffer in a backquote expression."
(if yas--current-template
(yas--template-name yas--current-template)
"Snippet")))))
(defun yas--restore-backquotes () (defun yas--restore-backquotes ()
"Replace markers in `yas--backquote-markers-and-strings' with their values." "Replace markers in `yas--backquote-markers-and-strings' with their values."