[Top][All Lists]

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

Re: New to elisp, learning by doing

From: Ehud Karni
Subject: Re: New to elisp, learning by doing
Date: Mon, 17 Feb 2003 20:24:19 +0200

Hash: SHA1

On Mon, 17 Feb 2003 08:16:51 +0100, wrote:
> > My goal in elisp is a .el that writes a table of contents from HTML
> > headers. The program would search for header tags in the buffer beginning
> > at point and write them in order one under the other.
> I would collect the header tags and/or their contents in a list of
> strings, I think.  Then, after reaching the end of the buffer, I'd
> write them out near the beginning of the buffer.
> (let ((toc nil))
>   (goto-char (point-max))
>   (while (re-search-backward REGEXP nil t)
>     (setq toc (cons TOC-ENTRY toc)))

I would do it with strings in a straight forward logic:

(let ((toc "")   ;; this the same as nil (but gives its usage intent)
      (RGXP "<[hH][1-6]>.*</[hH][1-6]>"))
   (goto-char (point-min))
   (while (re-search-forward RGXP nil t)
          (setq toc (concat toc (match-string 0) "\n")))
   (goto-char (point-min))
   (insert toc))

Please note:
1. The search will not cross lines (it will not find a header if the
   opening tag is on one line and the closing tag on another line).
2. The search does check that the opening and closing tags are the 
   same level (e.g. <H1> .... </H3>).

If you want to overcome these problem its is better to do the search
in 2 steps, finding the opening tag and THEN searching its MATCHING
closing tag.


- -- 
 Ehud Karni           Tel: +972-3-7966-561  /"\
 Mivtach - Simon      Fax: +972-3-7966-667  \ /  ASCII Ribbon Campaign
 Insurance agencies   (USA) voice mail and   X   Against   HTML   Mail  FAX:  1-815-5509341  / \                  Better  Safe  Than  Sorry
Comment: use to get my key (and others)


reply via email to

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