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: Christopher J. White
Subject: Re: New to elisp, learning by doing
Date: Sun, 16 Feb 2003 21:03:34 -0500
User-agent: Gnus/5.090014 (Oort Gnus v0.14) Emacs/21.2 (powerpc-apple-darwin)

>>>>> "john" == John Rabkin <yoni-r@actcom.com> writes:

john> After fighting with elisp for a couple of weeks in my spare time
john> I turn to anyone who can give me a clue or a push in the right
john> direction. This is what I have:

john>  (defun html-yank-header ()
john>   "Copies and yanks HTML headers"
john>   (interactive)
john>   (save-excursion
john>     (setq start_p (- (re-search-forward "\<h[12345]*\>") 4)) 
john>     (setq end_p (re-search-forward "\<\/h[12345]\>")) 
john>     (copy-region-as-kill start_p end_p)
john>     )
john>   (yank)
john>   )

john> This grabs the header closest to point and copies it to
john> point. Now I need the program to recurse N times where N is the
john> number of header tags in the buffer.

Despite your experience with loops, I would use certainly
use one for this case.  I don't really see how you could do
it without a loop unless you know how many times 
to search.  Here's a few things to investigate:

 - iterate over the return value of (re-search-forward) passing t
   as the NOERROR argument...

   (while (re-search-forward "..." nil t)
      ...
    )

 - use markers to save positions: (make-marker), (set-marker) and
   (marker-position)  You can bounce back and forth from the
   TOC point to the search point.  You need to use markers
   for this, as just using (point) won't work if you are inserting
   text at the beginning of the file because (point) is an offset,
   not a relative reference.

...cj

-- 
------------------------
-- Christopher J. White                                    
--
-- chris @
--   grierwhite.com
------------------------


reply via email to

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