[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: HTML utility functions (my first Emacs Lisp)
From: |
François Gannaz |
Subject: |
Re: HTML utility functions (my first Emacs Lisp) |
Date: |
Sat, 18 Mar 2006 18:21:35 +0100 |
User-agent: |
Mutt/1.5.11+cvs20060126 |
Your question seemed interesting. But instead of looking at your code to
find a mistake, I searched for something simpler in Emacs :
(define-skeleton xhtml-wrap-par
"Wraps a paragrah."
nil
"<p>" _ "</p>")
"define-skeleton" is defined in skeleton.el which is part of
Emacs. The above function is interactive and can be applied to a
selected region.
Hope it helps.
--
François Gannaz
Le mer 15 mar 09:59, mccutchen@gmail.com a écrit :
> 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))
>
> _______________________________________________
> help-gnu-emacs mailing list
> help-gnu-emacs@gnu.org
> http://lists.gnu.org/mailman/listinfo/help-gnu-emacs
>
---Fin du message cité---