We've expanded our news coverage and improved our search!
for the latest or search for all things across O'Reilly!
PHP Code Generation with Elisp
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)
(when (equal "/" (match-string 1))
(setq was-closed 't)
(insert "\n<xsl:attribute name=\"" attrib-name "\">"
(indent-region start (point))))))
It's pretty easy and very useful.