help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

HTML utility functions (my first Emacs Lisp)


From: mccutchen
Subject: HTML utility functions (my first Emacs Lisp)
Date: 15 Mar 2006 09:59:05 -0800
User-agent: G2/0.2

Hi,

I'm pretty new to Emacs.  I've been trying to use it to edit Python
and, to an extremely limited extent, Common Lisp, so I know the
absolute basics.  Generally, I'm pretty clueless.

I recently (re)discovered James Clark's nxml-mode, and I would really
like to use it to finally abandon Homesite as my XHTML editor on
Windows.  The only things that keep me from switching away from
Homesite to something else (Emacs, SciTE, Vim, etc.) are Homesite's
predefined utility functions for inserting certain tags around your
current selection.  I have them mapped to shortcut keys, so that, e.g.,
C-p wraps the selection in a <p> element, C-1 wraps the selection in an
<h1> element, etc.  I also use the functions that turn a set of lines
into an ordered or unordered list pretty frequently.

I thought that implementing these would make a good introduction to
Emacs Lisp, so I've taken a shot at it.  The code I've copied below is
a first shot at an extremely simple framework for inserting HTML
elements around a selection.  It works for me inside of nxml-mode and
xml-mode.

Usage:
----
To turn a given line into a paragraph, select it and press M-x
wrap-with-element RET p RET.  If you want the contents of the element
to be indented, you can say M-x wrap-with-block-element RET p RET.

To turn a given set of lines into a list, select them and press M-x
make-ordered-list RET or M-x make-unordered-list RET.


Help!
----
I fell pretty sure that I've done some silly things with this code.
Does anyone want to give me criticism, suggestions or advice?


Thanks, and sorry for the long-winded-ness,

Will.


Code
----
(defun insert-xml-element (element-name start end &optional
indent-contents?)
  (let ((contents (delete-and-extract-region start end)))
    (insert-start-tag element-name)
    (when indent-contents?
      (newline-and-indent))
    (insert contents)
    (when indent-contents?
      (indent-according-to-mode)
      (newline-and-indent))
    (insert-end-tag element-name)
    (when indent-contents?
      (indent-according-to-mode)
      (newline-and-indent))))

(defun insert-start-tag (name)
  (insert (format "<%s>" name)))

(defun insert-end-tag (name)
  (insert (format "</%s>" name)))


(defun wrap-with-element (element-name start end)
  (interactive "sElement name: \nr")
  (insert-xml-element element-name start end))

(defun wrap-with-block-element (element-name start end)
  (interactive "sElement name: \nr")
  (insert-xml-element element-name start end t))


(defun make-html-list (list-type start end)
  (let* ((contents (delete-and-extract-region start end))
         (lines (split-string contents "\n")))
    (insert-start-tag list-type)
    (newline-and-indent)
    (dolist (line lines)
      (insert (format "<li>%s</li>" line))
      (newline-and-indent))
    (insert-end-tag list-type)
    (indent-according-to-mode)))

(defun make-ordered-list (start end)
  (interactive "r")
  (make-html-list "ol" start end))

(defun make-unordered-list (start end)
  (interactive "r")
  (make-html-list "ul" start end))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]