From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Le Wang Newsgroups: gmane.emacs.help Subject: Re: Iterating over all buffer lines Date: Fri, 18 Jan 2013 10:15:14 +0800 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: ger.gmane.org 1358475322 12130 80.91.229.3 (18 Jan 2013 02:15:22 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 18 Jan 2013 02:15:22 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Sean McAfee Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jan 18 03:15:40 2013 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Tw1UZ-0007bU-5e for geh-help-gnu-emacs@m.gmane.org; Fri, 18 Jan 2013 03:15:39 +0100 Original-Received: from localhost ([::1]:45431 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tw1UI-00009q-Aw for geh-help-gnu-emacs@m.gmane.org; Thu, 17 Jan 2013 21:15:22 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:52901) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tw1UC-00009l-MS for help-gnu-emacs@gnu.org; Thu, 17 Jan 2013 21:15:17 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tw1UB-0006sk-KC for help-gnu-emacs@gnu.org; Thu, 17 Jan 2013 21:15:16 -0500 Original-Received: from mail-vc0-f178.google.com ([209.85.220.178]:40503) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tw1UB-0006sW-FW for help-gnu-emacs@gnu.org; Thu, 17 Jan 2013 21:15:15 -0500 Original-Received: by mail-vc0-f178.google.com with SMTP id m8so2424462vcd.23 for ; Thu, 17 Jan 2013 18:15:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=wOrTARHz5uXdbv1MhqruUgb3cY7woYm+ck6SpNGUrXE=; b=Me5b4/9VxyQPga51+sFAR1g9JxccbbhuHwFicKsnTKN5F+T0C+tH/WVQ2KAvFpNuud uCBoRqamz2uipHU4HBB59m5XHJ31qQBu6BVrHMIQWzD50uwoG49xMXjCpwjxFN/JEZ/Y 54iDqWEo35LmPgCNmz2OAC5+zi6rmnd0oWPo4rHXUwN0bLMo/fFhrCTZfOd1lWpdNv0u bUcZpshFjGrSpdSlXzGCWsYDuEYjmXDfSYOXUxCCEQL/pmcD+fUXgY+hUbz53LmdZEoE 0XUQL+iGYHTTOIhM/1XVXAOsBh+8oGaNaCIydbSsJVwh2Fe0xZTYWOZe3h5s15F6xNSD GFpw== X-Received: by 10.52.180.202 with SMTP id dq10mr6899691vdc.129.1358475314943; Thu, 17 Jan 2013 18:15:14 -0800 (PST) Original-Received: by 10.58.161.20 with HTTP; Thu, 17 Jan 2013 18:15:14 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.220.178 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:88696 Archived-At: (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 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