minad / org-modern

:unicorn: Modern Org Style
GNU General Public License v3.0
1.54k stars 48 forks source link

Add: (org-modern-heading-folding-indicators) #191

Closed alphapapa closed 6 months ago

alphapapa commented 6 months ago

Hi Daniel,

Today I saw Nicolas Rougier's Reddit post and was inspired to add a similar feature to org-modern to optionally show a folding/expanded indicator in place of heading stars. I've wanted this feature for a long time, and until I saw Nicolas's example, I didn't realize how simple it would be to implement.

There are various ways it could be implemented in org-modern, but I tried to do so with the minimal changes needed. Hopefully it will be according to your taste, but I will understand if you prefer to do it differently. :)

Here's a screenshot of what it looks like:

Screenshot_20240427_220541

Thanks for your work on org-modern!

minad commented 6 months ago

Thanks Adam! This works great. The feature has been requested before.

There are various ways it could be implemented in org-modern, but I tried to do so with the minimal changes needed. Hopefully it will be according to your taste, but I will understand if you prefer to do it differently. :)

You hit my taste with your minimal change. I will pull this now as is and maybe reorganize the customization options slightly to avoid overlap.

minad commented 6 months ago

See https://github.com/minad/org-modern/commit/a06443c1d0251decada41a3bdca30f712cf9a96e for the reworked options.

alphapapa commented 6 months ago

See a06443c for the reworked options.

Very nice. Thank you!

drishal commented 6 months ago

is there a way to switch back to the old style ? since I am not really used to this style

minad commented 6 months ago

@alphapapa I have a question regarding empty sections - do you have a suggestion on how one could detect those? In a TODO list with many headings (some empty), it will look a little bit odd if the headings over empty sections are displayed as expanded. Maybe we need a separate indicator glyph for those, e.g., a box?

minad commented 6 months ago

@drishal Yes, see the option org-modern-star.

alphapapa commented 6 months ago

@alphapapa I have a question regarding empty sections - do you have a suggestion on how one could detect those? In a TODO list with many headings (some empty), it will look a little bit odd if the headings over empty sections are displayed as expanded. Maybe we need a separate indicator glyph for those, e.g., a box?

I wouldn't object to that. There are these two packages which appear to do exactly that, so maybe their code can show how to do it well:

minad commented 6 months ago

Doesn't look cheap: https://github.com/tonyaldon/org-bars/blob/59ff8976ce789c0c594fd4756cbeb5354b6178d2/org-bars.el#L306-L319

alphapapa commented 6 months ago

Doesn't look cheap: https://github.com/tonyaldon/org-bars/blob/59ff8976ce789c0c594fd4756cbeb5354b6178d2/org-bars.el#L306-L319

That doesn't, no. But we might be able to do it more cheaply. AFAIK all we need to know is whether, after the end of the line, the next non-whitespace content is a heading. This function seems to work correctly except for the case of the last entry in the buffer having no content:

(defun org--entry-empty-p ()
  "Return non-nil if current entry is empty.
Expects to be called with point on heading line.  Note: If the
heading at point is the last in a buffer and is empty, this
function incorrectly returns nil."
  (save-excursion
    (goto-char (pos-eol))
    (looking-at-p (rx (0+ space) bol (1+ "*") (1+ blank)))))

Edit: This one seems to cover that case as well:

(defun org--entry-empty-p ()
  "Return non-nil if current entry is empty.
Expects to be called with point on heading line."
  (save-excursion
    (goto-char (pos-eol))
    (or (looking-at-p (rx (0+ space) bol (1+ "*") (1+ blank)))
        (when (re-search-forward (rx (1+ space)) nil t)
          (= (point) (point-max))))))
minad commented 6 months ago

@alphapapa Thanks! I think one also has to check the star level, such that headings with direct subheadings are not treated as empty. If you have time to integrate it, I'd appreciate a PR.

minad commented 6 months ago

I made a first attempt in https://github.com/minad/org-modern/commit/cd54e7d8d9d8a9da68ce81d6ec260687b88834bf. Unfortunately it creates the next problem, since the creation of subheadings will not trigger refontification of the parent heading, such that the heading will stay marked as empty.

alphapapa commented 6 months ago

I made a first attempt in cd54e7d. Unfortunately it creates the next problem, since the creation of subheadings will not trigger refontification of the parent heading, such that the heading will stay marked as empty.

AFAICT that's why the other packages add more hooks. If one wants perfection, I guess that will be needed; though not only that, but also something in either an after-command-function or another font-lock keyword to catch when subheadings are inserted manually by typing the stars. For myself, I can live without that perfection; the folding indicators themselves are a huge, long-awaited boon, and I don't usually have empty entries, anyway.