Snippets with Emacs Lisp Power

The YASnippet package for Emacs has some pretty awesome power for the developer, especially when you utilize the power of Emacs Lisp. YASnippet was inspired by TextMate, which was inspired by Emacs, in a highly-out-of-equilibrium whirlwind of self-referential creativity. The screencast above uses a set of snippets I originally took from this WordPress TextMate bundle, created by Shawn Parker and Gordon Brander The first snippet in the screencast illustrates YASnippet's mirrors with transformation, in a WordPress plugin template. The snippet calls Emacs Lisp functions as the plugin name is filled in to create the plugin URI, the "namespace" (used here as a prefix for function and variable names), and the primary class name for the plugin. The second snippet writes a function skeleton, then calls Emacs Lisp at the end to move the generated function outside the current scope into a correct position in the file. This snippet uses YASnippet's fields with transformations syntax, but to do a sneaky thing: not transform the field, but move a region of generated text! Following is the code for the snippets shown in the screencast, with no commentary. So pipe up in the comments if you're curious about how something works!
# -*- mode: snippet -*-
# name: WP Plugin
# key: plugin
# --
Plugin Name: ${1:Plugin Name}
Plugin URI: http://${}${1:$(gjg/sanitize text)}/
Description: ${3:Description}
Version: ${4:1.0}
Author: ${5:Gregory Grubbs}
Author URI: http://${}
Namespace: ${1:$(gjg/acronyminize text)}_
class ${1:$(gjg/whitespace-to-underscore text)} {
	 * constructor for $1
	 * The constructor is responsible for registering all hooks used
	 * by this class as as WordPress plugin
	function __construct() {
	} // constructor
$${1:$(gjg/acronyminize text)} = new ${1:$(gjg/whitespace-to-underscore text)}();
Next, the add_action snippet, which moves a generated function at the end:
# -*- mode: snippet -*-
# name: add_action
# key: add_action
# --
add_action('${1:init}', array($this, 'my_${2:$1}'));
function my_$1 () {
And finally, (some of) the Emacs Lisp functions that the snippets call:
(defun gjg/acronyminize (text &optional do-capitalize)
  "Make an acronym from the text 
do-capitalize: t means run text through capitalize function, nil will respect CamelCase
    (setq case-fold-search nil)
      "[^A-Z]" ""
      (if do-capitalize (capitalize text) text) nil t))))
(defun gjg/move-next-sexp-past-current-scope ()
  "kill sexp following point, move past current scope/sexp/function"
  (let ((beg (point)))
    (re-search-forward "^[ \t]*function[ \t]+[^}]+?}" (point-max) nil)
	(kill-region (point) (mark)))
  (indent-region (mark) (point)))
Tagged with: , , , , , , , , , , ,
One comment on “Snippets with Emacs Lisp Power
  1. joao says:


    Nice to see someone using more yasnippet power! You might want to turn your snippet using gjg/move-next-sexp-past-current-scope into a snippet-command, something that is available in the yasnippet trunk, and will be released in the upcoming 0.7.

    Have a look at this snippet

    # -*- mode: snippet -*-
    # type: command
    # key: marcc
    # contributor: Translated from TextMate Snippet
    # name: Add / Remove Several Columns (marcc)
    # condition: (yas/rails-intelligent-migration-snippet-condition-p)
    # —
    (yas/rails-intelligent-migration-snippet :add_remove_column_continue)

    with the supporting code available at

    or mail me for suggestions. I’m happy to give you support, with what I can, and I encourage you to create more complex snippets or contribute to the textmate-snippet-conversion effort.


1 Pings/Trackbacks for "Snippets with Emacs Lisp Power"
  1. […] see how other people were using it. Coding, of course – especially if you use Emacs Lisp to transform the text. Debugging, too. Marking up text. Remembering parameters. Wrapping regions. Writing e-mails. Adding […]

Leave a Reply

Your email address will not be published. Required fields are marked *