all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Iterating over all buffer lines
@ 2013-01-17 20:55 Sean McAfee
  2013-01-18  2:15 ` Le Wang
  0 siblings, 1 reply; 3+ messages in thread
From: Sean McAfee @ 2013-01-17 20:55 UTC (permalink / raw)
  To: help-gnu-emacs

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?


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Iterating over all buffer lines
  2013-01-17 20:55 Iterating over all buffer lines Sean McAfee
@ 2013-01-18  2:15 ` Le Wang
  2013-01-18  2:43   ` Eric Abrahamsen
  0 siblings, 1 reply; 3+ messages in thread
From: Le Wang @ 2013-01-18  2:15 UTC (permalink / raw)
  To: Sean McAfee; +Cc: help-gnu-emacs

(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



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Iterating over all buffer lines
  2013-01-18  2:15 ` Le Wang
@ 2013-01-18  2:43   ` Eric Abrahamsen
  0 siblings, 0 replies; 3+ messages in thread
From: Eric Abrahamsen @ 2013-01-18  2:43 UTC (permalink / raw)
  To: help-gnu-emacs

Le Wang <l26wang@gmail.com> 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 <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
>> )

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"))




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-01-18  2:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-17 20:55 Iterating over all buffer lines Sean McAfee
2013-01-18  2:15 ` Le Wang
2013-01-18  2:43   ` Eric Abrahamsen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.