1

When an item is refiled to a target which is collapsed and contains other items, the refiled items are visible beneath the collapsed target. I would like these refiled items to instead be hidden along with the other items within the collapsed target.

So instead of:
* Target...
** Refiled item 1
** Refiled item 2

one would just see:
* Target...

Is this possible?

0

Of course! Everything is possible in Emacs!

Edit: revised solution

If the collapsed status of the parent is all that you want to preserve, then maybe something like the following would be adequate:

(defun my/org-refile-preserve-collapsed-parent ()
  (org-up-heading-safe)
  (when (save-excursion
          (end-of-line)
          (org-invisible-p))
    (outline-hide-subtree)))

(add-hook 'org-after-refile-insert-hook #'my/org-refile-preserve-collapsed-parent) 

original solution

The easiest solution would probably be just to advise org-paste-subtree (this doesn't quite work):

(defun my-advice/org-save-outline-visibility (orig-fun &rest args)
  (org-save-outline-visibility t
    (apply orig-fun args)))

(advice-add 'org-paste-subtree :around #'my-advice/org-save-outline-visibility)

If you don't want to have permanent advice on org-paste-subtree you could advise org-refile that adds and removes the advice for org-paste-subtree.

  • This solution is close. If the refile target is hidden within another collapsed heading, the parent heading remains collapsed (which I do want). If the refile target is visible, however, the refiled item is still displayed beneath the collapsed target item, as in my first example. – Max Pfleger Feb 8 at 3:21
  • @MaxPfleger Darn! Outline visibility is tricky-- especially in this case when you're potentially refiling multiple headings. I looked into using org-after-refile-insert-hook with the region delimiters which appear to still reflect the inserted text. I ran into issues trying to use (outline-flag-region (region-beginning) (region-end) t) with outline visibility breaking in the document. – ebpa Feb 8 at 6:53
  • That's great! Your revised solution is exactly what I was looking for. I have come across one small glitch. If you undo a refile, the heading beneath the target heading moves up beside the target target. So it looks like * Heading 1...* Heading 2 all in one line. – Max Pfleger Feb 8 at 18:10
  • @MaxPfleger Interesting. Maybe it just undoes the part of org-paste-subtree that shifts the outline level? Was the refiled subtree ("Heading 2") originally a level 1 heading? – ebpa Feb 8 at 18:16
  • The undo behaviour seems to happen for all levels of headings. It is actually more than just a visual issue. The result of undoing a refile seems to confuse org-mode and lock that node so that it cannot be expanded. Expanding all the buffer's collapsed nodes corrects the problem. – Max Pfleger Feb 8 at 19:40

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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