valign icon indicating copy to clipboard operation
valign copied to clipboard

jit-lock-refontify when called on folded text causes misalignment

Open akashpal-21 opened this issue 10 months ago • 1 comments

Suppose the org file starts with org-startup-folded != 'showeverything, and valign-mode is called as part of an org-mode-hook

Then,

(if valign-mode
        (progn
          (add-hook 'jit-lock-functions #'valign-region 98 t)
         ... [code block]
          (jit-lock-refontify))

This causes tables that are currently under invisible headlines to be crooked. User has to press TAB on the table to align again manually.

Currently doing the following as a temporary solution

(with-eval-after-load 'valign
  (defun valign-org-cycle-refontification (state)
    "Refontify only the visible part affected by `org-cycle`.
STATE is a symbol indicating the new visibility state."
    (when (bound-and-true-p valign-mode)
      (cond
       ;; Expand direct children: Only refontify from current headline to its children
       ((eq state 'children)
	(let ((start (point))
              (end (save-excursion (outline-next-heading) (point))))
	  (jit-lock-refontify start end)))
       
       ;; Expand full subtree: Refontify from headline to subtree end
       ((eq state 'subtree)
	(let ((start (point))
              (end (save-excursion (org-end-of-subtree t t))))
	  (jit-lock-refontify start end)))
       
       ;; Fully expanded: Refontify everything
       ((eq state 'all)
	(jit-lock-refontify (point-min) (point-max))))))

  (defun valign--refontification-advice ()
    (if valign-mode
	(add-hook 'org-cycle-hook #'valign-org-cycle-refontification 98 'local)
      (remove-hook 'org-cycle-hook #'valign-org-cycle-refontification 'local)))
  (add-hook 'valign-mode-hook #'valign--refontification-advice))

probably not the most efficient or elegant way to solve it, but it works for me currently. Please let me know if any elegant other solution exists.

org-fold-core-style = overlays)

akashpal-21 avatar Feb 10 '25 17:02 akashpal-21

After thinking a bit about it, I actually think your solution is right! If we can't properly align the tables when it's hidden, then all that we can do is refontify it when it appears. jit-lock-refontify is actually very cheap to call, it merely marks the region as "unfontified", and redisplay will fontify the parts that are visible on-demand.

If you make a PR I'll gladly merge it.

casouri avatar Feb 15 '25 01:02 casouri