[Top][All Lists]

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

Re: New to elisp, learning by doing

From: Hannu Koivisto
Subject: Re: New to elisp, learning by doing
Date: Mon, 17 Feb 2003 11:27:39 +0200
User-agent: Gnus/5.090014 (Oort Gnus v0.14) Emacs/21.2 (i386-debian-linux-gnu) (Kai Großjohann) writes:

> "John Rabkin" <> writes:
>> 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 suggest you look up `push'.

> Note that I'm searching backwards because the entries are added to
> the beginning of the list.  If searching backwards proves
> inconvenient, then you could reverse the list (using nreverse) after
> you're done.  Or you implement a little queue.  Queues are not a

You could do those things if you were a C programmer who only has
primitive iteration constructs like while and for.

(require 'cl)

(let ((toc (loop while (re-search-forward regexp nil t)
                 collect toc-entry)))
  (loop for entry in toc
        do (insert (format entry-and-maybe-other-stuff))))


reply via email to

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