org-latex-preview: Rework auto toggle behaviour

* lisp/org-latex-preview.el (org-latex-preview--auto-aware-toggle):
Adjust the toggle behaviour to hopefully behave more intuitively.
This commit is contained in:
TEC 2023-02-07 02:42:04 +08:00
parent 58b2400e00
commit 4e623d4593
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 35 additions and 27 deletions

View File

@ -880,38 +880,46 @@ fragments in the buffer."
"Toggle the preview of the LaTeX fragment/environment DATUM. "Toggle the preview of the LaTeX fragment/environment DATUM.
This is done with care to work nicely with `org-latex-preview-auto-mode', This is done with care to work nicely with `org-latex-preview-auto-mode',
should it be enabled." should it be enabled."
(let ((beg (org-element-property :begin datum)) (let* ((beg (org-element-property :begin datum))
(end (org-element-property :end datum))) (end (org-element-property :end datum))
(ov (cl-some
(lambda (o)
(and (eq (overlay-get o 'org-overlay-type)
'org-latex-overlay)
o))
(overlays-at beg))))
;; If using auto-mode, an overlay will already exist but ;; If using auto-mode, an overlay will already exist but
;; not be showing an image. We can detect this ;; not be showing an image. We can detect this
;; situtation via the preview-state overlay property, and ;; situtation via the preview-state overlay property, and
;; in such cases the most reasonable action is to just ;; in such cases the most reasonable action is to just
;; (re)generate the preview image. ;; (re)generate the preview image.
(pcase (cl-some (cond
(lambda (o) ;; When not using auto-mode.
(and (eq (overlay-get o 'org-overlay-type) ((not org-latex-preview-auto-mode)
'org-latex-overlay) (if (org-latex-preview-clear-overlays beg end)
o)) (message "LaTeX preview removed")
(overlays-at beg)) (org-latex-preview--place-from-elements
;; When not using auto-mode, or there is no overlay. org-latex-preview-default-process (list datum))))
((or (pred not) (guard (not org-latex-preview-auto-mode))) ;; When using auto-mode, but no current preview.
(if (org-latex-preview-clear-overlays beg end) ((not ov)
(message "LaTeX preview removed") (org-latex-preview--place-from-elements
(org-latex-preview--preview-region beg end))) org-latex-preview-default-process (list datum))
;; When on a just written/edited fragment that should be previewed. (message "Creating LaTeX preview"))
((and ov (guard (eq (overlay-get ov 'preview-state) 'modified))) ;; When on a just written/edited fragment that should be previewed.
(org-latex-preview-auto--regenerate-overlay ov) ((eq (overlay-get ov 'preview-state) 'modified)
(overlay-put ov 'view-text t)) (org-latex-preview-auto--regenerate-overlay ov)
;; When on an unmodified fragment that is currently showing an image, (overlay-put ov 'view-text t))
;; hide the image. ;; When on an unmodified fragment that is currently showing an image,
((and ov (guard (overlay-get ov 'display))) ;; clear the image.
(org-latex-preview-auto--open-this-overlay)) ((overlay-get ov 'display)
;; Since we're on an unmodified fragment but not showing an image, (org-latex-preview-clear-overlays beg end)
;; let's try to show the image if possible. (message "LaTeX preview removed"))
(ov ;; Since we're on an unmodified fragment but not showing an image,
(overlay-put ov 'view-text t) ;; let's try to show the image if possible.
(overlay-put ov 'face (overlay-get ov 'hidden-face)) (ov
(overlay-put ov 'display (overlay-get ov 'preview-image)))))) (overlay-put ov 'view-text t)
(overlay-put ov 'face (overlay-get ov 'hidden-face))
(overlay-put ov 'display (overlay-get ov 'preview-image))))))
(defun org-latex-preview-collect-fragments (&optional beg end) (defun org-latex-preview-collect-fragments (&optional beg end)
"Collect all LaTeX maths fragments/environments between BEG and END." "Collect all LaTeX maths fragments/environments between BEG and END."