0

Calling evil-delete-line from a headline is deleting hidden children. Same behavior with evil-change-line.

On a collapsed outline tree how can I delete from the cursor to the end of line while keeping children?

edit: based on the comments below, I've realized I could've phrased this better.

calling evil-delete-line only deletes the entire headline if the cursor is placed on the first character of the headline. Otherwise the expected behavior is to delete from the cursor to the end of the line.

If I have the following tree structure, where ^ represents the cursor, what is the proper way to delete from the cursor to the end of line when the tree is collapsed?

* headline 1^i_want_this_text_deleted
** child1
** child2
** child3

If I call evil-delete-line while the tree is expanded I correctly delete the text but leave the children. When the tree is collapsed, the function also deletes the children.

If I'm expecting the wrong behavior from this function is there an alternate function I should be calling to achieve the desired behavior?

5
  • This makes me think of the recent bug #65734 thread.
    – Drew
    Commented Jan 12 at 3:39
  • Doesn't it make sense that deleting a collapsed headline also deletes its children? To me this is a 'feature'. I guess the simplest really is to 'unfold', and subsequently fold again (you could automate that in some custom command). Commented Jan 12 at 9:50
  • @Drew It looks like your link does not link correctly (it links to here, not to the bug). Commented Jan 12 at 9:51
  • 1
    This should be the right URL: debbugs.gnu.org/cgi/bugreport.cgi?bug=65734. I probably made a copy/paste error. Thx.
    – Drew
    Commented Jan 12 at 23:05
  • @dalanicolai is correct that deleting a headline while deleting folded children would be a feature. Maybe I didn't explain this as clearly as I could have. The expected behavior of evil-delete-line is to delete from the cursor to the end of the line while keeping the headline and all text before the cursor in place.
    – cochrane12
    Commented Jan 13 at 15:33

1 Answer 1

0

EDIT

Actually, due to advices it quite easy to 'adapt' evil-delete as follows:

(advice-add 'evil-delete :around #'org-evil-delete-adv)

(defun org-evil-delete-adv (fn beg end &rest args)
  (let ((on-folded-headline (and (eq major-mode 'org-mode)
                 (org-at-heading-p)
                 (org-fold-folded-p (line-end-position)))))
    (when on-folded-headline
      (setq end (line-end-position))
      (org-show-subtree))
    (apply fn beg end args)
    (when on-folded-headline
      (org-fold-hide-subtree))))

After evaluating the code above code, you should get the behavior you ask for. However, I am not sure if that advice breaks some other evil functionality.

END EDIT

The behavior of killing the children in Emacs (state) is similar when using kill-line. The cumbersome way to fix it, is to define a custom motion (or redefine the evil-end-of-line motion) to not kill the 'folded part', e.g. by defining end of line on an org-headline via next-property-change.

The easy and straightforward solution is to define a very simple keyboard macro for it, and use it for killing (to the end of) a headline. When recording the macro, simply start from the folded headline, unfold it first, kill (to the end of) the line, and fold again. You can find plenty of info about defining vim keyboard macro's on the web.

Subsequently, you could save the macro and bind it to your preferred key sequence. Info about saving a keyboard macro you can find in the Emacs manual here. It works the same for evil macro's that, despite being evil, prevent killing children.

5
  • I tend to lean towards "easy and straightforward" but am I right that a limitation of defining a macro this way would be that if run on any tree in an expanded state it would collapse tree?
    – cochrane12
    Commented Jan 15 at 15:12
  • Don't use it on expanded trees. When a tree is not collapsed, you can just use the commands mentioned in your question (your macro would be some separate command, bound to some separate custom keybinding). Commented Jan 15 at 15:44
  • Definitely worth a try but I'm not sure I could get used to having to think if a tree is expanded or not before entering a command. Is this behavior a bug that I should be reporting somewhere or is it just an unfortunate consequence of the interaction between org and evil-mode? I can't imagine there's much of a use case for deleting half of a line and taking the children with it.
    – cochrane12
    Commented Jan 16 at 21:03
  • As I mentioned in the answer, Emacs its own kill-line shows the same behavior, so I would say it is not an 'unfortunate consequence'. I guess it's debatable if this is a bug, it depends on how well you can convince the maintainers of your expected behavior. Anyway, if it is decided to be a bug, then I guess it will be considered one of low priority. I have added an alternative solution, hope it works well. Commented Jan 17 at 1:05
  • This gave the ability I was looking for perfectly, but you guessed correctly that it may effect other evil functionality: it seems to also effect dd so that (1) hidden children are not deleted even when deleting the entire headline and (2) oddly, sometimes other sibling headlines are deleted (although I can't reproduce this reliably). Unusual behavior only exhibited when using dd - both shift+d and shift+c work perfectly.
    – cochrane12
    Commented Jan 24 at 2:16

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.