all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Le Wang <l26wang@gmail.com>
To: Sean McAfee <eefacm@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Iterating over all buffer lines
Date: Fri, 18 Jan 2013 10:15:14 +0800	[thread overview]
Message-ID: <CAM=K+irk4Mg4nL26qwYY0PPkzOFinQ-4Z=OcZYeQ2bJTCMOJVw@mail.gmail.com> (raw)
In-Reply-To: <bp8a9s7lecb.fsf@usca1uw-JZWWPM1.sanmateo.corp.akamai.com>

(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	other threads:[~2013-01-18  2:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-17 20:55 Iterating over all buffer lines Sean McAfee
2013-01-18  2:15 ` Le Wang [this message]
2013-01-18  2:43   ` Eric Abrahamsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAM=K+irk4Mg4nL26qwYY0PPkzOFinQ-4Z=OcZYeQ2bJTCMOJVw@mail.gmail.com' \
    --to=l26wang@gmail.com \
    --cc=eefacm@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.