From 9bc6c363da3b5ffc3a003b89ebcc1996f0e01cd2 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Sat, 23 Apr 2022 23:55:44 +0800 Subject: [PATCH] org-fold-core: Fix edge case when fontification goes beyond requested * lisp/org-fold-core.el (org-fold-core-fontify-region): Run `org-fold-core-first-unfold-functions' on all the regions unmarked by 'org-fold-core-fontified property instead of using incorrect heuristics with `point' position. Make sure that fontified region is registered as fontified according to the return value of `font-lock-default-fontify-region' Fixes https://orgmode.org/list/8735i5gd8n.fsf@gmail.com --- lisp/org-fold-core.el | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lisp/org-fold-core.el b/lisp/org-fold-core.el index d98eb6358..227a2a4af 100644 --- a/lisp/org-fold-core.el +++ b/lisp/org-fold-core.el @@ -1436,7 +1436,7 @@ folded regions.") (defun org-fold-core-fontify-region (beg end loudly &optional force) "Run `font-lock-default-fontify-region' in visible regions." (with-silent-modifications - (let* ((pos beg) next + (let* ((pos beg) next font-lock-return-value (force (or force org-fold-core--force-fontification)) (org-fold-core--fontifying t) (skip-specs @@ -1467,13 +1467,22 @@ folded regions.") (< next end)) (setq next (org-fold-core-next-folding-state-change nil next end)))) (save-excursion - (font-lock-default-fontify-region pos next loudly) + (setq font-lock-return-value (font-lock-default-fontify-region pos next loudly)) (save-match-data - (unless (<= pos (point) next) - (run-hook-with-args 'org-fold-core-first-unfold-functions pos next)))) + ;; Only run within regions that are not yet touched by + ;; fontification. + (let ((l pos) (r next) (c pos) nxt) + (when (get-text-property c 'org-fold-core-fontified) + (setq c (next-single-property-change c 'org-fold-core-fontified))) + (setq nxt (next-single-property-change c 'org-fold-core-fontified nil r)) + (while (< c r) + (run-hook-with-args 'org-fold-core-first-unfold-functions c nxt) + (setq c (next-single-property-change nxt 'org-fold-core-fontified nil r)) + (setq nxt (next-single-property-change c 'org-fold-core-fontified nil r)))))) (put-text-property pos next 'org-fold-core-fontified t) (put-text-property pos next 'fontified t) - (setq pos next))))) + (setq pos next)) + (or font-lock-return-value `(jit-lock-bounds ,beg . ,end))))) (defun org-fold-core-update-optimisation (beg end) "Update huge buffer optimisation between BEG and END.