0

I want to access the first entry of a :LOGBOOK: drawer independently of the visibility of a subtree: folded, partially folded, or unfolded. This function worked on 9.1.9 in the first two cases (folded and partially folded):

(defun my-org-clock-goto-next-clock ()
  (interactive)
  (org-back-to-heading)
  (search-forward "LOGBOOK:")
  (org-cycle 3)
  (forward-line)
  (move-beginning-of-line nil)
  )

I upgraded to Org mode 9.3.3 and (org-cycle 3) no longer unfolds a subtree. At a top-level heading, I see a message:

Already at the top level of the outline

I read the manual on local and global visibility cycling and could not find how to set a subtree to unfolded.

What is the command to unfold all contents of a subtree regardless of the initial state?

  • 1
    Try (outline-show-subtree) instead of (org-cycle 3). – NickD Feb 11 at 14:13
  • 1
    BTW, the doc string for org-cycle says: When there is a numeric prefix, go up to a heading with level ARG, do a ‘show-subtree’ and return to the previous cursor position. If ARG is negative, go up that many levels. That probably explains the error, but I don't know why it worked for 9.1.9 - probably an accident. – NickD Feb 11 at 14:16
  • 1
    I believe drawers are folded independently of structure: most people seem to want them folded even if the subtree is unfolded, so you have to do something extra to open them: try calling (org-cycle) when point is on the drawer name (i.e. after the (search-forward ...) or the (outline-show-subtree) since the latter returns point to where it was before the call). – NickD Feb 11 at 19:09
  • 1
    Maybe org-flag-drawer can be used instead: try (org-flag-drawer nil). org-cycle delegates to it in the case of a drawer. I'll try to get a decent answer written but it won't be before the weekend. – NickD Feb 13 at 20:50
  • Done, and I deleted my comments to keep a tidy thread. – miguelmorin Feb 15 at 9:11
1

Summarizing the comments, you are looking for (outline-show-subtree) in the general case:

(outline-show-subtree)

Show everything after this heading at deeper levels.

In case of a drawer, which most people want folded even when the tree is unfolded, use (org-flag-drawer nil):

(org-flag-drawer FLAG &optional ELEMENT BEG END)

When FLAG is non-nil, hide the drawer we are at. Otherwise make it visible.

org-cycle delegates to org-flag-drawer in the case of a drawer.

You may need to call them in a different order, or when point is at the location you want unfolded.

For my case, the solution was:

(defun my-org-clock-goto-next-clock ()
  (interactive)
  (org-back-to-heading)
  (search-forward "LOGBOOK:")
  (outline-show-subtree)
  (org-flag-drawer nil)
  (forward-line)
  (move-beginning-of-line nil)
  )

It's probably an accident that (org-cycle 3) worked for 9.1.9, since the doc string for org-cycle says:

When there is a numeric prefix, go up to a heading with level ARG, do a ‘show-subtree’ and return to the previous cursor position. If ARG is negative, go up that many levels.

That probably explains the error in 9.3.3.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.