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

[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.grossjohann@uni-duisburg.de (Kai Großjohann) writes:

> "John Rabkin" <yoni-r@actcom.com> 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'.

>   INSERT-TOC)
>
> 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)))
  (move-to-wherever-you-want-toc)
  (loop for entry in toc
        do (insert (format entry-and-maybe-other-stuff))))

-- 
Hannu


reply via email to

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