reddit / emacs

Store auto-yasnippets in registers and expand on insert

eleven_cupfuls · 2022-11-06 00:15
I found myself wanting more than one auto-yasnippet readily available the other day, so I integrated them with registers. I'm not sure how often I will need this again, but it's quite a simple bit of code, and it seemed worth sharing: (require 'cl-lib) (require 'register) (require 'auto-yasnippet) (defun my-aya/current-to-register (snippet register) "Put the current aya snippet into REGISTER. When that register is later inserted or jumped to, the stored snippet will be expanded." (interactive (list (or aya-current (user-error "There is no current auto snippet")) (register-read-with-preview "Snippet to register: "))) (set-register register (cons 'aya snippet))) (cl-defmethod register-val-insert ((snippet (head aya))) (let ((aya-current (cdr snippet))) (aya-expand))) (cl-defmethod register-val-jump-to ((snippet (head aya)) _arg) (let ((aya-current (cdr snippet))) (aya-expand))) (cl-defmethod register-val-describe ((snippet (head aya)) _verbose) (princ "an auto-yasnippet: ") (princ (cdr snippet))) Since `copy-to-register` has two bindings in `ctl-x-r-map` reusing 's' seemed okay to me, but pick whatever binding you like. (define-key ctl-x-r-map (kbd "s") #'my-aya/current-to-register)
eleven_cupfuls · 2022-11-06 00:19
After this I actually decided to [replace auto-yasnippet altogether in my config](https://gitlab.com/woolsweater/dotemacs.d/-/blob/7b5822e451846da75445006fefb5b058818782a4/startup/externals.el#L1002-1066) :) but hopefully this is useful for those who do use it.