ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  PHP Code Generation with Elisp
Subject:   LISP????
Date:   2005-11-23 16:39:44
From:   nferrier
So what happened to the famous "no lisp" stance of O'Reilly?


Only kidding - I love elisp. This technique can be used in all sorts of ways, it's *really* easy to hack together good stuff in elisp, particularly for this kind of text processing.


Try it with verbose languages, like XSLT for example. Here's some elisp to write XSLT that turns an XML attribute into an XSLT attribute statement:


(defun xslt-attributize (start)
"Turn the XML attribute at point into an XSLT attribute decl."
(interactive (list (point)))
(unless (looking-at "\\([A-Za-z][A-Za-z0-9_-]+\\)=\"\\(.*?\\)\"")
(error "not at attribute"))
(let* ((end (match-end 0))
(attrib-name (match-string 1))
(attrib-value (match-string 2)))
(delete-region start end)
(save-excursion
(re-search-forward "\\(.\\)>")
(let (was-closed)
(when (equal "/" (match-string 1))
(setq was-closed 't)
(replace-match ">"))
(insert "\n<xsl:attribute name=\"" attrib-name "\">"
attrib-value
"</xsl:attribute>\n")
(if was-closed
(sgml-close-tag))
(indent-region start (point))))))


It's pretty easy and very useful.