(require 'yasnippet) (defvar yas-original-buffer nil) (defun yas-new-snippet--with-region (&optional no-template) "Pops a new buffer for writing a snippet. Expands a snippet-writing snippet, unless the optional prefix arg NO-TEMPLATE is non-nil." (interactive "P") (let ((guessed-directories (yas--guess-snippet-directories))) (setq yas-original-buffer (current-buffer)) (switch-to-buffer "*new snippet*") (erase-buffer) (kill-all-local-variables) (snippet-mode) (yas-minor-mode 1) (set (make-local-variable 'yas--guessed-modes) (mapcar #'(lambda (d) (yas--table-mode (car d))) guessed-directories)) (when (and (not no-template) yas-new-snippet-default) (save-excursion (insert (yas-snippet-from-region))) (yas-expand-snippet yas-new-snippet-default)))) (defun yas-snippet-from-region () "Initial snippet content from region." (or (with-current-buffer yas-original-buffer (if (region-active-p) (replace-regexp-in-string "[\\$]" "\\\\\\&" (buffer-substring-no-properties (region-beginning) (region-end))))) "")) (advice-add 'yas-new-snippet :override 'yas-new-snippet--with-region)