alphapapa / org-sidebar

A helpful sidebar for Org mode
GNU General Public License v3.0
530 stars 16 forks source link

tree-sidebar: Text inserted into entries appears in sidebar #36

Open alphapapa opened 3 years ago

alphapapa commented 3 years ago

See https://github.com/alphapapa/org-sidebar/blob/master/notes.org#maybe-a-use-font-locking-to-hide-non-heading-text-in-tree-buffer

Here's a function that modifies the font-lock keywords to attempt this. It almost works, although it doesn't seem to take effect in an Org buffer unless I use font-lock-studio, then the keywords are applied again. (Even (font-lock-flush) doesn't seem to force it.) Also, typing more text afterward does not make the new text invisible, even though it should. So maybe this could be a starting point, but it doesn't seem as easy as I hoped, and it seems like a lot of trial-and-error is required.

(defun test-font-lock-keywords ()
  (setq-local argh-invisible '(face font-lock-warning-face invisible t))
  (setq-local argh-visible '(face font-lock-warning-face invisible nil))
  (let ((keywords (list (cons (rx bol (or blank (seq "*" (1+ (not blank)))
                                          (not (any "*")))
                                  (1+ not-newline) eol)
                              'argh-invisible)
                        (cons (rx bol (1+ blank) eol)
                              'argh-invisible)
                        (cons (rx bol eol)
                              'argh-invisible)
                        (cons (rx bol (1+ "*") (1+ blank) (minimal-match (1+ not-newline)) eol)
                              'argh-visible)
                        )))
    (font-lock-add-keywords nil keywords 'append)
    (set (make-local-variable 'font-lock-extra-managed-props)
         (cons 'invisible font-lock-extra-managed-props))))
legalnonsense commented 3 years ago

Doesn't setting an invisible face via text properties/font lock mean the face will apply to both buffers? That's what is happening when I try it. If I active this and then type in the body of a heading in the org buffer, it will hide that text automatically.

alphapapa commented 3 years ago

Use a cloned indirect buffer.

localauthor commented 1 year ago

It seems that indirect buffers cannot have different font-locking from the base buffer. (According to this discussion).

Perhaps overlays can work instead?

Calling this in the tree-buffer seems to have the desired effects, more or less.

(defun org-sidebar-tree-hide-entries ()
  (interactive)
  (remove-overlays (point-min) (point-max) 'tree 't)
  (let* ((heading-regexp (concat "^\\(?:"
                                 org-outline-regexp
                                 "\\)")))
    (goto-char (point-min))
    (while (re-search-forward heading-regexp nil t)
      (save-excursion
        (let* ((beg (line-end-position))
               (end (progn
                      (re-search-forward heading-regexp nil t)
                      (forward-line -1)
                      (line-end-position)))
               (ol (make-overlay beg end nil t t)))
          (overlay-put ol 'invisible t)
          (overlay-put ol 'tree t))))))