nvim-orgmode / orgmode

Orgmode clone written in Lua for Neovim 0.9+.
https://nvim-orgmode.github.io/
MIT License
3.03k stars 134 forks source link

Implement `org-toggle-heading` functionality #164

Closed grepinsight closed 2 years ago

grepinsight commented 2 years ago

Does this feature exist in Emacs orgmode core?

Yes

Orgmode link

https://orgmode.org/manual/Structure-Editing.html

Feature value

Ability to quickly turn arbitrary text into org headings will improve UX

Additional context

org-toggle-item which is C-c - in emacs would be also nice to have. This converts text into a list.

jgollenz commented 2 years ago

@kristijanhusak I started working on this and am stuck on how to determine the parent headline of a paragraph. Is there an easy way from the current line of the cursor to the treesitter node that line belongs to?

PS: I thought of mapping this to <Leader>oh, since it's still free. Let me know if you think a different mapping would make more sense (since e.g. checkbox toggling is done with <C-Space>).

kristijanhusak commented 2 years ago

@jgollenz This should give you the closest headline from your current position: https://github.com/nvim-orgmode/orgmode/blob/master/lua/orgmode/org/mappings.lua#L61

jgollenz commented 2 years ago

the closest headline from your current position

Ah, the naming of that function confused me. I though it will return the headline with the lowest line diff from my current position. Thank you!

jgollenz commented 2 years ago

@grepinsight do you mind having a look at this #198? One thing I'm not sure how to handle is tags. Would you expect tags to disappear or should they just remain at the end of the line without any impact? I'd prefer the latter, because it is non-destructive (should you decide to turn the line back into a headline) and already works

jgollenz commented 2 years ago

Org-mode also allows toggling a checkbox item into a headline:

- [ ] item becomes * TODO item but then * TODO item becomes TODO item

In general: toggling a list item to a headline and toggling that headline again will result in a paragraph line. Can you think of any other cases besides paragraph lines and list items that I need to consider?

Aside from that, Org-mode does not insert any spaces in front of a headline that was toggled to a paragraph line. However, I think it makes sense to insert them according to org_indent_mode

grepinsight commented 2 years ago

Thank you for implementing this @jgollenz ! Turning simple text into heading works and vice versa.

Would you expect tags to disappear or should they just remain at the end of the line without any impact? I'd prefer the latter, because it is non-destructive (should you decide to turn the line back into a headline) and already works

I just confirmed with my emacs that emacs does the latter (at least in my setting, which is doom emacs). I think as a user, that's a sensible default because it's non-destructive as you said

In general: toggling a list item to a headline and toggling that headline again will result in a paragraph line.

This is also the behavior I observed in emacs as well.

One thing I wished the feature branch had that was missing was ability to toggle multiple lines of text into multiple headings by visual mode

Screen Shot 2022-01-08 at 2 01 17 PM Screen Shot 2022-01-08 at 2 01 35 PM

Other than this, it looks pretty solid. Thank you!

jgollenz commented 2 years ago

@grepinsight Thank you for testing it :)

toggle multiple lines of text into multiple headings by visual mode

I assume selecting multiple lines in visual mode is the Vim equivalent of what Emacs/Orgmode calls "regions", correct? I have a look at this, but please tell me what happens in Emacs when there is both normal lines and headlines in the region: toggle_region

grepinsight commented 2 years ago

case1: normal line and headlines

Screen Shot 2022-01-09 at 8 37 06 PM Screen Shot 2022-01-09 at 8 37 21 PM Screen Shot 2022-01-09 at 8 37 30 PM

case2 : normal lines and list

Screen Shot 2022-01-09 at 8 38 38 PM Screen Shot 2022-01-09 at 8 38 57 PM Screen Shot 2022-01-09 at 8 39 06 PM

case2b: list and normal lines

Screen Shot 2022-01-09 at 8 40 03 PM Screen Shot 2022-01-09 at 8 40 16 PM Screen Shot 2022-01-09 at 8 40 25 PM

conclusion: Looks like if there are mixed types in visual selection, only first type in the visual selection is affected

cc @jgollenz

jgollenz commented 2 years ago

@grepinsight thanks for clarifying :) I'll see what I can do

jgollenz commented 2 years ago

@grepinsight one more thing: could you please let me know how it behaves when you have a blank line as the first line in the visual selection?

grepinsight commented 2 years ago

Thanks for being thorough @jgollenz

When you have a blank line(s) as the first line in the visual selection, they are simply ignored and the first non blank lines are used for toggle

Screen Shot 2022-01-11 at 6 36 21 PM Screen Shot 2022-01-11 at 6 36 35 PM
jgollenz commented 2 years ago

@kristijanhusak support for toggling multiple lines would entail a visual mode mapping. As far as I can see we are only using normal mode mappings right now, hard-coded in the setup_mappings function if I'm not mistaken. I don't know how many emacs orgmode features rely on regions/visual selections, but IF we want to support them, we should think of how to do it properly. I'd prefer to do that in a different PR then. What's your opinion on this?

kristijanhusak commented 2 years ago

@jgollenz for now implement only the normal mode mapping. At one point I'll figure out how to set up multiple mode mappings and we can introduce it then. I don't think it's a big of an issue without the visual mapping here.