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://${2:dynapse.com/plugins/}${1:$(gjg/sanitize text)}/
Description: ${3:Description}
Version: ${4:1.0}
Author: ${5:Gregory Grubbs}
Author URI: http://${6:gregorygrubbs.com/}
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() {
		 $0
	} // 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}'));
${3:$$(gjg/move-next-sexp-past-current-scope)}
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
"
  (save-excursion
    (setq case-fold-search nil)
    (downcase
     (replace-regexp-in-string
      "[^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"
  (beginning-of-line)
  (let ((beg (point)))
    (re-search-forward "^[ \t]*function[ \t]+[^}]+?}" (point-max) nil)
    (mark-defun)
	(kill-region (point) (mark)))
  (forward-line)
  (yank)
  (indent-region (mark) (point)))
Posted in Development, emacs, wordpress | Tagged , , , , , , , , , , , | Leave a comment

FAST file access with Emacs and ido-mode

One of the things that makes daily Emacs use so enjoyable is the availability of brilliant add-ons designed to make you work faster.

Emacs with ido-mode fuzzy matching (or flex matching) makes it incredibly quick to navigate the file system using only the keyboard. But it does far more than that, allowing the emacs pilot to quickly find help, commands, variables and much more. This video shows the finer points of using ido-mode with flex matching.

It’s all in the video – enjoy.

Here are my current settings for ido-mode

;; do not confirm a new file or buffer
(setq confirm-nonexistent-file-or-buffer nil)
(require 'ido)
(ido-mode 1)
(ido-everywhere 1)
(setq ido-enable-flex-matching t)
(setq ido-create-new-buffer 'always)
(setq ido-enable-tramp-completion nil)
(setq ido-enable-last-directory-history nil)
(setq ido-confirm-unique-completion nil) ;; wait for RET, even for unique?
(setq ido-show-dot-for-dired t) ;; put . as the first item
(setq ido-use-filename-at-point t) ;; prefer file names near point
Posted in Development, emacs | Tagged , , , , | 1 Comment

Get remote X Windows working in Ubuntu Karmic

One thing that troubled me moving to Ubuntu 9.10 Karmic Koala: my remote X applications stopped working with the error
X11 connection rejected because of wrong authentication

Read More »

Posted in Development, emacs | 2 Comments

Emacs Power: remote servers and shell commands

Emacs file and directory browsing

Emacs has Dired, a great method for browsing directories; especially in combination with ido-mode, I prefer it to Windows Explorer, OS X Finder, Gnome Nautilus, or anything else I’ve used over the decades to browse file systems. You can quickly begin entering paths, and Dired helps you with directory and file name completion.
Read More »

Posted in Development, emacs, wordpress | Tagged , , , , , , | 4 Comments