From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nick Dokos Subject: Re: Iterate over list with `org-next-item' Date: Thu, 02 Sep 2010 22:28:07 -0400 Message-ID: <9637.1283480887@gamaville.dokosmarshall.org> References: Reply-To: nicholas.dokos@hp.com Return-path: Received: from [140.186.70.92] (port=53554 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OrM0t-0003Sn-C4 for emacs-orgmode@gnu.org; Thu, 02 Sep 2010 22:28:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OrM0r-0006pt-R9 for emacs-orgmode@gnu.org; Thu, 02 Sep 2010 22:28:22 -0400 Received: from vms173001pub.verizon.net ([206.46.173.1]:57742) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OrM0r-0006pk-Mv for emacs-orgmode@gnu.org; Thu, 02 Sep 2010 22:28:21 -0400 Received: from gamaville.dokosmarshall.org ([unknown] [173.76.32.106]) by vms173001.mailsrvcs.net (Sun Java(tm) System Messaging Server 7u2-7.02 32bit (built Apr 16 2009)) with ESMTPA id <0L8500AQAG6WNER0@vms173001.mailsrvcs.net> for emacs-orgmode@gnu.org; Thu, 02 Sep 2010 21:28:08 -0500 (CDT) In-reply-to: Message from Zachary Young of "Thu, 02 Sep 2010 17:47:38 PDT." List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Zachary Young Cc: nicholas.dokos@hp.com, emacs-orgmode@gnu.org Zachary Young wrote: > I am trying to iterate over a list with `org-next-item'. I just tried: > > (ignore-errors (while (equal nil (org-next-item)))) > > and it worked. > > Is there a better way to do this? I'm not very versed in Elisp, and > `org-next-item' returning `nil' on success, and throwing an error at the end > of the list is throwing me a bit. > It's always a good idea to browse the org-mode code itself for examples: after all it's been written by (or vetted by) the experts, so it should provide a good foundation. I found three examples of org-next-item usage, two of which are shown here (the third one is a bit subtler): * org.el: --8<---------------cut here---------------start------------->8--- (defun org-skip-over-state-notes () "Skip past the list of State notes in an entry." (if (looking-at "\n[ \t]*- State") (forward-char 1)) (while (looking-at "[ \t]*- State") (condition-case nil (org-next-item) (error (org-end-of-item))))) --8<---------------cut here---------------end--------------->8--- * org-mouse.el: --8<---------------cut here---------------start------------->8--- (defun org-mouse-for-each-item (function) (save-excursion (ignore-errors (while t (org-previous-item))) (ignore-errors (while t (funcall function) (org-next-item))))) --8<---------------cut here---------------end--------------->8--- As you can see, the second almost matches what you came up with, but the condition is simpler: the code *uses* the error raised to get out of the (otherwise infinite) loop, so there is no need to check what org-next-item returns. HTH Nick