/auto-activating-snippets

Snippets for Emacs that expand as you type

Primary LanguageEmacs LispGNU General Public License v3.0GPL-3.0

Auto Activating Snippets

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 use-package:

(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))

There’s also aas-global-mode which activates snippets from the keymap global, e.g

(aas-set-snippets 'global
  ";--" ""
  ";->" "")
(aas-global-mode)

(If you want more LaTeX snippets, take a look at LaTeX-auto-activating-snippets)

Comparison to other snippet engines

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).

Yasnippet 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.)

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 LaTeX-auto-activating-snippets were ‘inspired’ by cdlatex, replacing ` as a prefix by ;.