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

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

Re: Iterating over all buffer lines


From: Le Wang
Subject: Re: Iterating over all buffer lines
Date: Fri, 18 Jan 2013 10:15:14 +0800

(while (not (= (point) (point-max)))
  (message (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
  (forward-line 1))


On Fri, Jan 18, 2013 at 4:55 AM, Sean McAfee <eefacm@gmail.com> wrote:
> I have some code where I want to loop over all of the lines of text in a
> buffer.  I had a prior solution that involved repeatedly searching for
> the regexp "^.+$", but that seemed a little heavyweight.  Also, it only
> found non-empty lines, and changing the "+" to a "*" to return all lines
> causes an infinite loop.
>
> I just tried my hand at writing a general iterate-all-lines construct,
> and came up with this:
>
> (loop for last-point = (point)
>       while (= 0 (forward-line))
>       for line = (buffer-substring-no-properties
>                   last-point
>                   (- (point) (if (bolp) 1 0)))
>       ;; do something with line
> )
>
> Newlines are not returned, and as can be seen, I have to take care to
> handle a final line that is missing a terminating newline.
>
> Is there a better and/or more idiomatic way to go about it?



-- 
Le



reply via email to

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