org-fold-core: Fix fontification inside folded regions

* lisp/org-fold-core.el (org-fold-core-initialize): Declare
`org-fold-core-fontified' text property for font-lock.
(org-fold-core--force-fontification): New variable controlling forced
fontification inside folded regions.
(org-fold-core-fontify-region): Fix cases when BEG is inside folded
region.  Respect `org-fold-core--force-fontification'.
* lisp/org-macs.el (org-with-forced-fontification): New macro.
(org-buffer-substring-fontified):
(org-looking-at-fontified): Do not rely on jit-lock.  Use
`org-fold-core-fontified' text property to determine whether text is
already fontified.
This commit is contained in:
Ihor Radchenko 2022-01-29 15:32:08 +08:00
parent 0bca8bc418
commit 3da1b2d240
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 72 additions and 28 deletions

View File

@ -746,7 +746,8 @@ future org buffers."
(add-hook 'clone-indirect-buffer-hook #'org-fold-core-decouple-indirect-buffer-folds nil 'local) (add-hook 'clone-indirect-buffer-hook #'org-fold-core-decouple-indirect-buffer-folds nil 'local)
;; Optimise buffer fontification to not fontify folded text. ;; Optimise buffer fontification to not fontify folded text.
(when (eq font-lock-fontify-region-function #'font-lock-default-fontify-region) (when (eq font-lock-fontify-region-function #'font-lock-default-fontify-region)
(setq-local font-lock-fontify-region-function 'org-fold-core-fontify-region)) (setq-local font-lock-fontify-region-function 'org-fold-core-fontify-region)
(add-to-list 'font-lock-extra-managed-props 'org-fold-core-fontified))
;; Setup killing text ;; Setup killing text
(setq-local filter-buffer-substring-function #'org-fold-core--buffer-substring-filter) (setq-local filter-buffer-substring-function #'org-fold-core--buffer-substring-filter)
(if (and (boundp 'isearch-opened-regions) (if (and (boundp 'isearch-opened-regions)
@ -1429,35 +1430,47 @@ The arguments and return value are as specified for `filter-buffer-substring'."
return-string)) return-string))
;;; Do not fontify folded text until needed. ;;; Do not fontify folded text until needed.
(defvar org-fold-core--force-fontification nil
"Let-bind this variable to t in order to force fontification in
folded regions.")
(defun org-fold-core-fontify-region (beg end loudly &optional force) (defun org-fold-core-fontify-region (beg end loudly &optional force)
"Run `font-lock-default-fontify-region' in visible regions." "Run `font-lock-default-fontify-region' in visible regions."
(let ((pos beg) next (with-silent-modifications
(org-fold-core--fontifying t)) (let ((pos beg) next
(while (< pos end) (force (or force org-fold-core--force-fontification))
(setq next (org-fold-core-next-folding-state-change (org-fold-core--fontifying t)
(if force nil (skip-specs
(let (result) (let (result)
(dolist (spec (org-fold-core-folding-spec-list)) (dolist (spec (org-fold-core-folding-spec-list))
(when (and (not (org-fold-core-get-folding-spec-property spec :visible)) (when (and (not (org-fold-core-get-folding-spec-property spec :visible))
(org-fold-core-get-folding-spec-property spec :font-lock-skip)) (org-fold-core-get-folding-spec-property spec :font-lock-skip))
(push spec result))) (push spec result)))
result)) result)))
pos ;; Move POS to first visible point within BEG..END.
end)) (while (and (catch :found
(while (and (not (catch :found (dolist (spec (org-fold-core-get-folding-spec 'all pos))
(dolist (spec (org-fold-core-get-folding-spec 'all next)) (when (org-fold-core-get-folding-spec-property spec :font-lock-skip)
(when (org-fold-core-get-folding-spec-property spec :font-lock-skip) (throw :found spec))))
(throw :found spec))))) (< pos end))
(< next end)) (setq pos (org-fold-core-next-folding-state-change nil pos end)))
(setq next (org-fold-core-next-folding-state-change nil next end))) (when force (setq pos beg next end))
(save-excursion (while (< pos end)
(font-lock-default-fontify-region pos next loudly) (unless force
(save-match-data (setq next (org-fold-core-next-folding-state-change skip-specs pos end)))
(unless (<= pos (point) next) ;; Move to the end of the region to be fontified.
(run-hook-with-args 'org-fold-core-first-unfold-functions pos next)))) (while (and (not (catch :found
(put-text-property pos next 'org-fold-core-fontified t) (dolist (spec (org-fold-core-get-folding-spec 'all next))
(setq pos next)))) (when (org-fold-core-get-folding-spec-property spec :font-lock-skip)
(throw :found spec)))))
(< next end))
(setq next (org-fold-core-next-folding-state-change nil next end)))
(save-excursion
(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))))
(put-text-property pos next 'org-fold-core-fontified t)
(setq pos next)))))
(defun org-fold-core-update-optimisation (beg end) (defun org-fold-core-update-optimisation (beg end)
"Update huge buffer optimisation between BEG and END. "Update huge buffer optimisation between BEG and END.

View File

@ -39,6 +39,7 @@
(declare-function org-fold-show-context "org-fold" (&optional key)) (declare-function org-fold-show-context "org-fold" (&optional key))
(declare-function org-fold-save-outline-visibility "org-fold" (use-markers &rest body)) (declare-function org-fold-save-outline-visibility "org-fold" (use-markers &rest body))
(declare-function org-fold-next-visibility-change "org-fold" (&optional pos limit ignore-hidden-p previous-p)) (declare-function org-fold-next-visibility-change "org-fold" (&optional pos limit ignore-hidden-p previous-p))
(declare-function org-fold-core-with-forced-fontification "org-fold" (&rest body))
(declare-function org-fold-folded-p "org-fold" (&optional pos limit ignore-hidden-p previous-p)) (declare-function org-fold-folded-p "org-fold" (&optional pos limit ignore-hidden-p previous-p))
(declare-function string-collate-lessp "org-compat" (s1 s2 &optional locale ignore-case)) (declare-function string-collate-lessp "org-compat" (s1 s2 &optional locale ignore-case))
@ -1178,6 +1179,36 @@ so values can contain further %-escapes if they are define later in TABLE."
org-emphasis t) org-emphasis t)
"Properties to remove when a string without properties is wanted.") "Properties to remove when a string without properties is wanted.")
(defvar org-fold-core--force-fontification)
(defmacro org-with-forced-fontification (&rest body)
"Run BODY forcing fontification of folded regions."
(declare (debug (form body)) (indent 1))
`(unwind-protect
(progn
(setq org-fold-core--force-fontification t)
,@body)
(setq org-fold-core--force-fontification nil)))
(defun org-buffer-substring-fontified (beg end)
"Return fontified region between BEG and END."
(when (bound-and-true-p jit-lock-mode)
(org-with-forced-fontification
(when (text-property-not-all beg end 'org-fold-core-fontified t)
(save-match-data (font-lock-fontify-region beg end)))))
(buffer-substring beg end))
(defun org-looking-at-fontified (re)
"Call `looking-at' RE and make sure that the match is fontified."
(prog1 (looking-at re)
(when (bound-and-true-p jit-lock-mode)
(org-with-forced-fontification
(when (text-property-not-all
(match-beginning 0) (match-end 0)
'org-fold-core-fontified t)
(save-match-data
(font-lock-fontify-region (match-beginning 0)
(match-end 0))))))))
(defsubst org-no-properties (s &optional restricted) (defsubst org-no-properties (s &optional restricted)
"Remove all text properties from string S. "Remove all text properties from string S.
When RESTRICTED is non-nil, only remove the properties listed When RESTRICTED is non-nil, only remove the properties listed