[Top][All Lists]

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

Iterating over all buffer lines

From: Sean McAfee
Subject: Iterating over all buffer lines
Date: Thu, 17 Jan 2013 12:55:00 -0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

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
                  (- (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?

reply via email to

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