From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: Iterate over list with `org-next-item' Date: Tue, 07 Sep 2010 21:33:55 +0200 Message-ID: <871v95l55o.wl%n.goaziou@gmail.com> References: <9637.1283480887@gamaville.dokosmarshall.org> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Return-path: Received: from [140.186.70.92] (port=39308 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ot4NJ-00012n-BR for emacs-orgmode@gnu.org; Tue, 07 Sep 2010 16:02:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1Ot3vp-0001Nk-SY for emacs-orgmode@gnu.org; Tue, 07 Sep 2010 15:34:15 -0400 Received: from mail-ww0-f49.google.com ([74.125.82.49]:43914) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Ot3vp-0001NZ-OU for emacs-orgmode@gnu.org; Tue, 07 Sep 2010 15:34:13 -0400 Received: by wwb24 with SMTP id 24so7354184wwb.30 for ; Tue, 07 Sep 2010 12:34:12 -0700 (PDT) In-Reply-To: <9637.1283480887@gamaville.dokosmarshall.org> 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: nicholas.dokos@hp.com Cc: emacs-orgmode@gnu.org Hello, >>>>> Nick Dokos writes: > 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. I strongly advise against using `org-next-item' in a defun. It is, in its actual form, meant for interactive use only. You should have a look at `org-get-next-item' and `org-get-previous-item' instead. I left a note about it at line 874 in org-list.el (yes, I know...). There is also `org-apply-on-list' that might be of some help. Regards, -- Nicolas