lesscode.org


An Emacs kid-mode  

By Ryan Tomayko under Python on 01. July 2005

I’ve been meaning to take a look at using mmm-mode to get support for < ?python?> blocks in kid templates.

After playing around with wordpress for a few hours, I grabbed php-mode and saw just how easy it is to do sub-modes in emacs. Here’s a simple variation on php-mode’s suggested emacs setup that enables python block support in templates (note that you’ll also need python-mode.el):

;; kid-html-mode
(mmm-add-mode-ext-class nil "\\.kid?\\'" 'html-kid)
(mmm-add-classes
 '((html-kid
    :submode python-mode
    :front "< \\?\\(python\\)?"
    :back "\\?>")))
(add-to-list 'auto-mode-alist '("\\.kid?\\'" . html-mode))

Add that to your .emacs file and then load up a kid template.

Not only do you get syntax coloring but all the other great python-mode.

What sucks about this is that I can’t seem to get it to work reliably with James Clark’s excellent nxml-mode, which I prefer when working with kid templates. I’m trying something like this but it seems flaky:

;; kid-nxml-mode
(mmm-add-mode-ext-class nil "\\.kid?\\'" 'nxml-kid)
(mmm-add-classes
 '((nxml-kid
    :submode python-mode
    :front "< \\?\\(python\\)?"
    :back "\\?>")))
(add-to-list 'auto-mode-alist '("\\.kid?\\'" . nxml-mode))

My suspicion is that this has something to do with the way nxml-mode is loaded, which is kind of weird because I believe it sniffs for a DOCTYPE and/or < ?xml?> declaration.

3 Responses to “An Emacs kid-mode”

  1. Bryan:

    Is it possible to get nxml-mode to declare a kid template “valid,” and if so, how did you do it?

    comment at 21. October 2005

  2. Graham Fawcett:

    Thanks, Ryan!

    I didn’t get much flakiness on Emacs/win32. I did simplify the regexp for the [?python?] PIs, though. Here’s a snippet from my .emacs file (also includes a rule for [style type=”text/css”] using css-mode).

    The Markdown implementation here seems to over-escape double quotes, so I put my .emacs snippet at: http://paste.lisp.org/display/13581.

    comment at 14. November 2005

  3. Jorge Godoy:

    This last piece of code works like a charm for me ;-) Thanks! I’ve been looking for it for a while. :-)

    Now it is just a matter of making Kid tags valid to have nxml fully working.

    comment at 28. February 2006