From f8c1678317747bb88774a6cb0c4290c4d8a5a21b Mon Sep 17 00:00:00 2001 From: Karthik Chikmagalur Date: Mon, 15 Apr 2024 20:28:51 -0700 Subject: [PATCH] org-latex-preview: Fix live previews org-src setup * lisp/org-latex-preview.el (org-latex-preview-live--src-buffer-setup): Ensure that live LaTeX previews of org-src buffer contents are created in the src buffer. Since this runs on a timer, the active buffer can be different from the org-src LaTeX buffer when the preview run fires. Ensure that `org-element-context' is not called from the org-src buffer by setting the element type persistently when it is set up. --- lisp/org-latex-preview.el | 76 +++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/lisp/org-latex-preview.el b/lisp/org-latex-preview.el index c8bde0062..db85232d2 100644 --- a/lisp/org-latex-preview.el +++ b/lisp/org-latex-preview.el @@ -1223,54 +1223,62 @@ This is meant to be called via `org-src-mode-hook'." (setq-local org-latex-preview-live--generator (thread-first (lambda (&rest _) - (let* ((content - (string-trim (buffer-substring-no-properties - (point-min) (point-max))))) - (with-current-buffer org-buf - (org-latex-preview-place - org-latex-preview-process-default - (list (list (overlay-start orig-ov) - (overlay-end orig-ov) - content)) - numbering-offsets)))) + (when (eq (current-buffer) src-buf) + (let* ((content + (string-trim (buffer-substring-no-properties + (point-min) (point-max))))) + (with-current-buffer org-buf + (org-latex-preview-place + org-latex-preview-process-default + (list (list (overlay-start orig-ov) + (overlay-end orig-ov) + content)) + numbering-offsets))))) (org-latex-preview-live--throttle) (org-latex-preview-live--debounce org-latex-preview-live-debounce))) (add-hook 'after-change-functions org-latex-preview-live--generator 90 'local)) ;; Source Org buffer not visible: display live previews in org-src buffer - ;; Set the element type ahead of time since we cannot call - ;; org-element-context in the org-src buffer - (setq org-latex-preview-live--element-type - (with-current-buffer org-buf - (or (and (string-prefix-p - "\\[" (org-element-property :value element)) - 'latex-environment) - (org-element-type element)))) - ;; Show live preview if available - (org-latex-preview-live--ensure-overlay ov) ;; Set up hooks for live preview updates in the org-src buffer + (let* ((element-type + (with-current-buffer org-buf + (or (and (string-prefix-p + "\\[" (org-element-property :value element)) + 'latex-environment) + (org-element-type element)))) + (preview-clearout-func + (lambda (ov) + (org-latex-preview-live--clearout ov) + (setq org-latex-preview-live--element-type element-type)))) + ;; Set the element type ahead of time since we cannot call + ;; org-element-context in the org-src buffer + (setq org-latex-preview-live--element-type element-type) + (add-hook 'org-latex-preview-overlay-close-functions + preview-clearout-func nil 'local)) + (add-hook 'org-latex-preview-overlay-open-functions + #'org-latex-preview-live--ensure-overlay nil 'local) (add-hook 'org-latex-preview-overlay-update-functions - #'org-latex-preview-live--update-overlay - nil 'local) + #'org-latex-preview-live--update-overlay nil 'local) (setq-local org-latex-preview-live--generator (thread-first (lambda (&rest _) - (org-latex-preview-place - org-latex-preview-process-default - (list (list (save-excursion (goto-char (point-min)) - (skip-chars-forward "\n \t\r") - (point)) - (save-excursion (goto-char (point-max)) - (skip-chars-backward "\n \t\r") - (point)))) - numbering-offsets preamble)) + (when (eq (current-buffer) src-buf) + (org-latex-preview-place + org-latex-preview-process-default + (list (list (save-excursion (goto-char (point-min)) + (skip-chars-forward "\n \t\r") + (point)) + (save-excursion (goto-char (point-max)) + (skip-chars-backward "\n \t\r") + (point)))) + numbering-offsets preamble))) (org-latex-preview-live--throttle) (org-latex-preview-live--debounce org-latex-preview-live-debounce))) - (add-hook 'org-latex-preview-overlay-open-functions #'org-latex-preview-live--ensure-overlay nil 'local) - (add-hook 'org-latex-preview-overlay-close-functions #'org-latex-preview-live--clearout nil 'local) - (add-hook 'after-change-functions org-latex-preview-live--generator 90 'local))) + (add-hook 'after-change-functions org-latex-preview-live--generator 90 'local) + ;; Show live preview if available + (org-latex-preview-live--ensure-overlay ov))) ;; Turn on auto-mode behavior in the org-src buffer (add-hook 'pre-command-hook #'org-latex-preview-auto--handle-pre-cursor nil 'local) (add-hook 'post-command-hook #'org-latex-preview-auto--handle-post-cursor nil 'local)))))