all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* HTML utility functions (my first Emacs Lisp)
@ 2006-03-15 17:59 mccutchen
  2006-03-18 17:21 ` François Gannaz
  2006-03-21 16:43 ` Stefan Monnier
  0 siblings, 2 replies; 4+ messages in thread
From: mccutchen @ 2006-03-15 17:59 UTC (permalink / raw)


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))

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: HTML utility functions (my first Emacs Lisp)
  2006-03-15 17:59 HTML utility functions (my first Emacs Lisp) mccutchen
@ 2006-03-18 17:21 ` François Gannaz
  2006-03-19 17:58   ` Will McCutchen
  2006-03-21 16:43 ` Stefan Monnier
  1 sibling, 1 reply; 4+ messages in thread
From: François Gannaz @ 2006-03-18 17:21 UTC (permalink / raw)
  Cc: mccutchen

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é---

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: HTML utility functions (my first Emacs Lisp)
  2006-03-18 17:21 ` François Gannaz
@ 2006-03-19 17:58   ` Will McCutchen
  0 siblings, 0 replies; 4+ messages in thread
From: Will McCutchen @ 2006-03-19 17:58 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 617 bytes --]

Hi François,

On 3/18/06, François Gannaz <mytskine@laposte.net> wrote:
> (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.

Ahh... I've never come across define-skeleton before.  It looks like
that will be a solution for at least some of my problems.  I'll have
to try to work with it some to figure out if I can make it do some of
the more advanced tasks I need.

> Hope it helps.

It does, I appreciate the help!


Will.

[-- Attachment #2: Type: text/plain, Size: 152 bytes --]

_______________________________________________
help-gnu-emacs mailing list
help-gnu-emacs@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: HTML utility functions (my first Emacs Lisp)
  2006-03-15 17:59 HTML utility functions (my first Emacs Lisp) mccutchen
  2006-03-18 17:21 ` François Gannaz
@ 2006-03-21 16:43 ` Stefan Monnier
  1 sibling, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2006-03-21 16:43 UTC (permalink / raw)


> 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 don't know about nxml, but Emacs's builtin HTML mode has C-c C-t to insert
a <foo></foo> tag pair and if you use transient-mark-mode (which
I recommend) and you've selected a region before hitting C-c C-t then the
tags will be placed around the region.


        Stefan

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-03-21 16:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-15 17:59 HTML utility functions (my first Emacs Lisp) mccutchen
2006-03-18 17:21 ` François Gannaz
2006-03-19 17:58   ` Will McCutchen
2006-03-21 16:43 ` Stefan Monnier

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.