2

Prologue

I heavily use the :PROPERTIES: drawer. Because of this, I want to create a function that would allow me to toggle the visibility of the :PROPERTIES: of the current subtree. I want the function to also work at the top level of the document (recall that a :PROPERTIES: drawer can be used in the top level of the document to set a property for all the subtrees in the current document).

I could use org-cycle, but in order to use this function, I need the cursor to be on the drawer, and I want the function to work regardless of the position of the cursor.

The question

  1. Is there any native function that accomplishes this behavior?
  2. If that's not the case, what functions could I use to accomplish this desired behavior?
2
  • Doesn't TAB (bound to org-cycle) work?
    – NickD
    Aug 10, 2021 at 12:48
  • Yes, but to use org-cycle, you need to make sure that the cursor is on the drawer, and I was wondering how to do this regardless of the position of the cursor. I've edited the question so that that is explicitly mentioned.
    – rdrg109
    Aug 10, 2021 at 15:35

1 Answer 1

1

I don't think there is a native function for doing that, but you can define your custom function.

(defun my/org-toggle-properties-drawer-current-subtree ()
  "Toggles the visibility of the PROPERTIES drawer of the current subtree."
  (interactive)
  (cond
   ;; If the cursor is on the top level of the document
   ((null (org-current-level))
    (save-excursion
      (beginning-of-buffer)
      (unless (org-at-property-drawer-p)
        (error "The top level of the document doesn't have a PROPERTIES drawer."))
      (org-hide-drawer-toggle)))
   ;; If the cursor is on a headline
   ((eq (car (org-element-at-point)) 'headline)
    (save-excursion
      (next-line)
      (unless (org-at-property-drawer-p)
        (error "This heading doesn't have a PROPERTIES drawer."))
      (org-hide-drawer-toggle)))
   ;; If the cursor is not on a headline and is not in the top level
   ;; of the document, then it is inside a subtree.
   (t (save-excursion
        (org-previous-visible-heading 1)
        (next-line)
        (unless (org-at-property-drawer-p)
          (error "This heading doesn't have a PROPERTIES drawer."))
        (org-hide-drawer-toggle)))))

(define-key org-mode-map (kbd "C-c t p") 'my/org-toggle-properties-drawer-current-subtree)
1
  • 1
    I have not run your function, I've just read the code, but I think there are a couple of potential problems: 1) there may be scheduling information between a headline and a property drawer, so (next-line) is probably not enough: you have to search for the drawer - that applies to both the second and the third case of the cond; 2) in the subtree case, did you only want to go to the previous visible heading or to the previous visible heading with a property drawer?
    – NickD
    Aug 11, 2021 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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