auto-activating-snippets
auto-activating-snippets copied to clipboard
Snippets for Emacs that expand as you type
#+title: Auto Activating Snippets #+author: ymarco
This package implements an engine for auto-expanding snippets. It is done by tracking your inputted chars along a tree until you complete a registered key sequence.
Its like running a long prefix command, but the keys you type are not 'consumed' and appear in the buffer until you complete the whole command - and then the snippet is triggered!
- Usage & Basic Configuration We didn't implement a complex file type like yasnippet. Declare your snippets with ~aas-set-snippets~ straight away, using ~:cond~ to set conditions.
Example configuration using [[https://github.com/jwiegley/use-package][use-package]]: #+begin_src emacs-lisp (use-package aas :hook (LaTeX-mode . aas-activate-for-major-mode) :hook (org-mode . aas-activate-for-major-mode) :config (aas-set-snippets 'text-mode ;; expand unconditionally ";o-" "ō" ";i-" "ī" ";a-" "ā" ";u-" "ū" ";e-" "ē") (aas-set-snippets 'latex-mode ;; set condition! :cond #'texmathp ; expand only while in math "supp" "\supp" "On" "O(n)" "O1" "O(1)" "Olog" "O(\log n)" "Olon" "O(n \log n)" ;; Use YAS/Tempel snippets with ease! "amin" '(yas "\argmin_{$1}") ; YASnippet snippet shorthand form "amax" '(tempel "\argmax_{" p "}") ; Tempel snippet shorthand form ;; bind to functions! ";ig" #'insert-register ";call-sin" (lambda (angle) ; Get as fancy as you like (interactive "sAngle: ") (insert (format "%s" (sin (string-to-number angle)))))) ;; disable snippets by redefining them with a nil expansion (aas-set-snippets 'latex-mode "supp" nil)) #+end_src There's also ~aas-global-mode~ which activates snippets from the keymap ~global~, e.g #+begin_src emacs-lisp (aas-set-snippets 'global ";--" "—" ";->" "→") (aas-global-mode) #+end_src
(If you want more LaTeX snippets, take a look at [[https://github.com/tecosaur/LaTeX-auto-activating-snippets][LaTeX-auto-activating-snippets]])
- Comparison to other snippet engines ** [[https://github.com/SirVer/ultisnips][UltiSnips]] UltiSnips does its automatic expansion by matching regexes on the current line. We do it by tracking the last characters you typed. This means that if you have a snippet expanding ~sin~ to ~\sin~ but you typed ~s~ ~e~ ~←backspace~ ~i~ ~n~, the snippet wont expand because the mistyped key broke the sequence. It does mean though that having many many snippets shouldn't impact performance (just like having big keymaps doesn't). ** [[https://github.com/joaotavora/yasnippet][Yasnippet]] Yasnippet [[https://github.com/joaotavora/yasnippet/issues/998#issuecomment-496449546][can]] auto-expand snippets, but that method is expansive with a large number of snippets - it calls all the conditions of all the snippets after each expansion trial. But no reason to carry on using it for only a few snippets if you already have it set up!
(I also learned of that method only after writing this engine.)
** [[https://github.com/cdominik/cdlatex][cdlatex]]
cdlatex requires you to have 1 or 2 keys sacrificed (usually ~~ for mathematical symbols and ~'~ for accents) in order to trigger its entry functions and expand a snippet. This package can 'emulate' cdlatex entirely, while not sacrificing any input keys! If you have a snippet with the key ~
a~,
you can type ~~ as much as you want and as long as an ~a~ doesn't follow nothing unusual would happen. Many of the snippets in [[https://github.com/tecosaur/LaTeX-auto-activating-snippets][LaTeX-auto-activating-snippets]] were 'inspired' by cdlatex, replacing ~
~ as a
prefix by ~;~.