Преглед изворни кода

org-element-cache: Setup before/after-change-functions in indirect buffers

* lisp/org-element.el (org-element--cache-setup-change-functions): New
function setting up `org-element--cache-before-change' and
`org-element--cache-after-change'.
(org-element-cache-reset): Setup before/after-change-functions inside
indirect buffers.
Ihor Radchenko пре 2 година
родитељ
комит
197d500607
1 измењених фајлова са 20 додато и 4 уклоњено
  1. 20 4
      lisp/org-element.el

+ 20 - 4
lisp/org-element.el

@@ -6782,6 +6782,16 @@ that range.  See `after-change-functions' for more information."
         ;; Activate a timer to process the request during idle time.
         (org-element--cache-set-timer (current-buffer))))))
 
+(defun org-element--cache-setup-change-functions ()
+  "Setup `before-change-functions' and `after-change-functions'."
+  (when (and (derived-mode-p 'org-mode) org-element-use-cache)
+    (add-hook 'before-change-functions
+	      #'org-element--cache-before-change nil t)
+    ;; Run `org-element--cache-after-change' early to handle cases
+    ;; when other `after-change-functions' require element cache.
+    (add-hook 'after-change-functions
+	      #'org-element--cache-after-change -1 t)))
+
 (defvar org-element--cache-avoid-synchronous-headline-re-parsing nil
   "This variable controls how buffer changes are handled by the cache.
 
@@ -7285,10 +7295,16 @@ buffers."
 	(setq-local org-element--cache-change-warning nil)
 	(setq-local org-element--cache-sync-requests nil)
 	(setq-local org-element--cache-sync-timer nil)
-	(add-hook 'before-change-functions
-		  #'org-element--cache-before-change nil t)
-	(add-hook 'after-change-functions
-		  #'org-element--cache-after-change nil t)))))
+        (org-element--cache-setup-change-functions)
+        ;; Make sure that `org-element--cache-after-change' and
+        ;; `org-element--cache-before-change' are working inside properly created
+        ;; indirect buffers.  Note that `clone-indirect-buffer-hook'
+        ;; will not work inside indirect buffers not created by
+        ;; calling `clone-indirect-buffer'.  We consider that the code
+        ;; not using `clone-indirect-buffer' to be written with
+        ;; awareness about possible consequences.
+        (add-hook 'clone-indirect-buffer-hook
+                  #'org-element--cache-setup-change-functions)))))
 
 ;;;###autoload
 (defun org-element-cache-refresh (pos)