Skip to content

Instantly share code, notes, and snippets.

@coldnew
Created September 14, 2012 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coldnew/3722722 to your computer and use it in GitHub Desktop.
Save coldnew/3722722 to your computer and use it in GitHub Desktop.
a org-mode easy-template like plugin for yasnippet
(defun coldnew/major-mode-expand ()
"Try to complete a structure template before point like org-mode does.
This looks for strings like \"<e\" on an otherwise empty line and
expands them.
Before use this function, you muse setup `major-mode-name'-expand-alist variable.
Take emacs-lisp-mode as example, if you wand to use <r to expand your snippet `require'
in yasnippet, you muse setup the emacs-lisp-mode-expand-alist variable.
(setq emacs-lisp-expand-alist '((\"r\" . \"require\")))"
(let* ((l (buffer-substring (point-at-bol) (point)))
(expand-symbol (intern (concat (symbol-name major-mode) "-expand-alist")))
(expand-alist (if (boundp expand-symbol) (symbol-value expand-symbol) nil))
a)
(when (and (looking-at "[ \t]*$")
(string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
(setq a (assoc (match-string 1 l) expand-alist)))
(backward-delete-char (1+ (length (car-safe a))))
(insert (cdr-safe a))
t)))
(defadvice yas-expand (before coldnew/major-mode-expand activate) (coldnew/major-mode-expand))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment