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: Eric Abrahamsen
Subject: Re: Iterating over all buffer lines
Date: Fri, 18 Jan 2013 10:43:01 +0800
User-agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.2 (gnu/linux)

Le Wang <address@hidden> writes:

> (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 <address@hidden> 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
>> )

Le Wang's solution is probably more idiomatic (for large-scale text
manipulation emacs seems better suited to working on a buffer than a
string), but you could also split the buffer substring on newlines and
map a function over the resulting list:

(mapcar (lambda (line)
          (when line (manipulate-line line)))
        (split-string
         (buffer-substring-no-properties (point-min) (point-max)) "\n"))




reply via email to

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