From a2707a0f92c76188c3ebb412fe2b57788b50ca9a Mon Sep 17 00:00:00 2001 From: stardiviner Date: Mon, 22 May 2023 16:25:33 +0800 Subject: [PATCH] org: Improve inline images displaying like LaTeX previewing * lisp/org.el (org-toggle-inline-images): Implement LaTeX previewing same logic in inline images toggle displaying. --- lisp/org-keys.el | 2 +- lisp/org.el | 95 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 81 insertions(+), 16 deletions(-) diff --git a/lisp/org-keys.el b/lisp/org-keys.el index 38fac57d8..7b3b84af4 100644 --- a/lisp/org-keys.el +++ b/lisp/org-keys.el @@ -204,7 +204,7 @@ (declare-function org-toggle-radio-button "org" (&optional arg)) (declare-function org-toggle-comment "org" ()) (declare-function org-toggle-fixed-width "org" ()) -(declare-function org-toggle-inline-images "org" (&optional include-linked beg end)) +(declare-function org-toggle-inline-images "org" (&optional arg beg end include-linked)) (declare-function org-latex-preview "org" (&optional arg)) (declare-function org-toggle-narrow-to-subtree "org" ()) (declare-function org-toggle-ordered-property "org" ()) diff --git a/lisp/org.el b/lisp/org.el index e72cf056a..c01952b97 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -16160,22 +16160,87 @@ SNIPPETS-P indicates if this is run to create snippet images for HTML." (when (memq ov org-inline-image-overlays) (push ov result))))) -(defun org-toggle-inline-images (&optional include-linked beg end) - "Toggle the display of inline images. -INCLUDE-LINKED is passed to `org-display-inline-images'." +(defun org-toggle-inline-images (&optional arg beg end include-linked) + "Toggle the display of inline images at point. +INCLUDE-LINKED is passed to `org-display-inline-images'. + +If cursor is on an inline image link, display the inline image. +If there is no inline image link at point, display all inline images in the current section. +With an active region, display inline images in the region. + +With a `\\[universal-argument]' prefix argument ARG, clear inline +images in the current section. + +With a `\\[universal-argument] \\[universal-argument]' prefix + argument ARG, display all inline images in the buffer. + +With a `\\[universal-argument] \\[universal-argument] \ +\\[universal-argument]' prefix argument ARG, clear all inline +images in the buffer." (interactive "P") - (if (org--inline-image-overlays beg end) - (progn - (org-remove-inline-images beg end) - (when (called-interactively-p 'interactive) - (message "Inline image display turned off"))) - (org-display-inline-images include-linked nil beg end) - (when (called-interactively-p 'interactive) - (let ((new (org--inline-image-overlays beg end))) - (message (if new - (format "%d images displayed inline" - (length new)) - "No images to display inline")))))) + (cond + ((not (display-graphic-p)) nil) + ;; Clear whole buffer inline images. + ((equal arg '(64)) + (org-remove-inline-images (point-min) (point-max)) + (message "Inline images preview disabled in buffer.")) + ;; Display whole buffer inline images. + ((equal arg '(16)) + (message "Displaying all inline images in buffer...") + (let ((include-linked t)) ; assume INCLUDE-LINKED be t here for backward compatibility. + (org-display-inline-images include-linked nil (point-min) (point-max))) + (message "Displaying all inline images in buffer... done.")) + ;; Clear current section. + ((equal arg '(4)) + (let* ((beg (if (use-region-p) + (region-beginning) + (if (org-before-first-heading-p) (point-min) + (save-excursion + (org-with-limited-levels (org-back-to-heading t) (point)))))) + (end (if (use-region-p) + (region-end) + (org-with-limited-levels (org-entry-end-position)))) + (inline-images (org--inline-image-overlays beg end))) + (org-remove-inline-images beg end) + (message "%d inline images display removed." (length inline-images)))) + ;; [M-1] / [C-1] argument for linked images like: + ;; [[https://orgmode.org/resources/img/org-mode-unicorn.svg][description]] + ((equal arg 1) + (let ((current-prefix-arg nil) + (include-linked t)) ; assume INCLUDE-LINKED be t here for backward compatibility. + (org-toggle-inline-images nil nil nil include-linked))) + ;; Display region selected inline images. + ((use-region-p) + (let ((beg (region-beginning)) + (end (region-end))) + (if (org--inline-image-overlays beg end) + (progn + (org-remove-inline-images beg end) + (message "Inline images in region removed.")) + (message "Displaying inline images in region...") + (org-display-inline-images include-linked t beg end) + (message "Displaying inline images in region... done.")))) + ;; Toggle display of inline image link at point. + ((let ((context (org-element-context))) + (and (memq (org-element-type context) '(link)) + (let ((beg (org-element-property :begin context)) + (end (org-element-property :end context))) + (if (org--inline-image-overlays beg end) + (progn + (org-remove-inline-images beg end) + (message "Display inline image at point removed.")) + (org-display-inline-images include-linked t beg end) + (message "Displaying inline image at point ... done.")) + t)))) + ;; Display inline images under current section. + (t + (let ((beg (if (org-before-first-heading-p) (point-min) + (save-excursion + (org-with-limited-levels (org-back-to-heading t) (point))))) + (end (org-with-limited-levels (org-entry-end-position)))) + (message "Displaying inline images in section...") + (org-display-inline-images include-linked t beg end) + (message "Displaying inline images in section... done."))))) (defun org-redisplay-inline-images () "Assure display of inline images and refresh them." -- 2.39.2 (Apple Git-143)