emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Iterate over list with `org-next-item'
@ 2010-09-03  0:47 Zachary Young
  2010-09-03  2:28 ` Nick Dokos
  0 siblings, 1 reply; 5+ messages in thread
From: Zachary Young @ 2010-09-03  0:47 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 349 bytes --]

Hi all,

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.

Thank you,
Zachary

[-- Attachment #1.2: Type: text/html, Size: 497 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: Iterate over list with `org-next-item'
  2010-09-03  0:47 Iterate over list with `org-next-item' Zachary Young
@ 2010-09-03  2:28 ` Nick Dokos
  2010-09-07 19:33   ` Nicolas Goaziou
  0 siblings, 1 reply; 5+ messages in thread
From: Nick Dokos @ 2010-09-03  2:28 UTC (permalink / raw)
  To: Zachary Young; +Cc: nicholas.dokos, emacs-orgmode

Zachary Young <zacharysyoung@gmail.com> 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

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

* Re: Iterate over list with `org-next-item'
  2010-09-03  2:28 ` Nick Dokos
@ 2010-09-07 19:33   ` Nicolas Goaziou
  2010-09-07 19:58     ` Nick Dokos
  0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2010-09-07 19:33 UTC (permalink / raw)
  To: nicholas.dokos; +Cc: emacs-orgmode

Hello,

>>>>> Nick Dokos writes:

> Zachary Young <zacharysyoung@gmail.com> 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

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

* Re: Iterate over list with `org-next-item'
  2010-09-07 19:33   ` Nicolas Goaziou
@ 2010-09-07 19:58     ` Nick Dokos
  2010-09-07 20:14       ` Nicolas Goaziou
  0 siblings, 1 reply; 5+ messages in thread
From: Nick Dokos @ 2010-09-07 19:58 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: nicholas.dokos, emacs-orgmode

Nicolas Goaziou <n.goaziou@gmail.com> wrote:

> > * org.el:
> >   ... 
> 
> > * org-mouse.el:
> 
> >   ...
> 
> 
> > 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.
> 

OK, thanks for the pointer: makes sense. I hadn't pulled your
reimplementation till just now. Of course, these functions didn't exist
before, so there was no choice - but maybe the few places where
org-next-item is used need to be examined and brought up to snuff, using
your new functions? Even if not strictly necessary, then at least as
examples of good practice for others to follow.

Thanks,
Nick

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

* Re: Iterate over list with `org-next-item'
  2010-09-07 19:58     ` Nick Dokos
@ 2010-09-07 20:14       ` Nicolas Goaziou
  0 siblings, 0 replies; 5+ messages in thread
From: Nicolas Goaziou @ 2010-09-07 20:14 UTC (permalink / raw)
  To: nicholas.dokos; +Cc: emacs-orgmode

>>>>> Nick Dokos writes:

> OK, thanks for the pointer: makes sense. I hadn't pulled your
> reimplementation till just now. Of course, these functions didn't
> exist before, so there was no choice - but maybe the few places
> where org-next-item is used need to be examined and brought up to
> snuff, using your new functions? Even if not strictly necessary,
> then at least as examples of good practice for others to follow.

You are right: this should be done. Speed issues may happen otherwise.
I'll look into it on Sunday.

As an example,

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

would become

--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))
  (if (looking-at "[ \t]*- State") (goto-char (org-list-bottom-point))))
--8<---------------cut here---------------end--------------->8---

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

end of thread, other threads:[~2010-09-07 20:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-03  0:47 Iterate over list with `org-next-item' Zachary Young
2010-09-03  2:28 ` Nick Dokos
2010-09-07 19:33   ` Nicolas Goaziou
2010-09-07 19:58     ` Nick Dokos
2010-09-07 20:14       ` Nicolas Goaziou

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).