Ihor Radchenko <yanta...@posteo.net> writes:

> "Christopher M. Miles" <numbch...@gmail.com> writes:
>
>>> Unlike `org-latex-preview', here we need to (1) respect active region;
>>> (2) keep the backward compatibility for INCLUDE-LINKED.
>>>
>>> For (1), for example, it would make sense to respect region when prefix
>>> argument is C-u and clear images only there. For (2), we need to at
>>> least allow toggling images with description using some prefix argument
>>> (previously, any prefix argument would do).
>>
>> I indeed implemented all same behavior like `org-latex-preview' in new
>> `org-toggle-inline-images'.
>>
>> ...
>> And I also implement the toggle inline images in region logic in
>> `org-toggle-inline-images' as bellowing:
>>
>> ;; Display region selected inline images.
>>    ((use-region-p)
>>     (message "Displaying inline images in region...")
>>     (org-display-inline-images include-linked t (region-beginning) 
>> (region-end))
>>     (message "Displaying inline images in region... done."))
>
> This is not a toggle. This is unconditional refresh.
>
Yes, indeed it's a unconditional refresh. It does not match the meaning of word 
"toggle".

Seems need to detect whether has image overlays in region. I added this
detection in new patch. Like bellowing:

#+begin_src emacs-lisp
...
((use-region-p)
    (if (seq-contains-p
         (mapcar
          (lambda (ov)
            (plist-get (overlay-properties ov) 'org-image-overlay))
          (overlays-in beg end))
         t)
        (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 nil (region-beginning) 
(region-end))
      (message "Displaying inline images in region... done.")))
....
#+end_src

>> For (2), It's working. The code passed the parameter `include-linked' to
>> `org-display-inline-images'. So they works in any case of [C-u] prefix.
>
> My concern is that previously C-u M-x org-toggle-inline-images would
> "display links with a text description part". With your patch, it is no
> longer the case because INCLUDE-LINKED is not affected by the prefix
> argument.
>
> And there is no clean way to allow INCLUDE-LINKED while keeping
> consistency with latex preview commands.

About the INCLUDE-LINKED argument, I don't know how to process it. In
theory, it should be handled by function org-display-inline-images
instead of org-toggle-inline-images. If you have improvements on it, can
you add code on my patch?

>
> What we might do here is making a new defcustom that will control
> whether linked images should be displayed. Then, something like C-1
> org-toggle-inline-images could toggle that defcustom and refresh all the
> image previews in buffer (if any).
>
> WDYT?

Refreshing all image previews in buffer is same as old behavior. My
patch's purpose is to improve function org-toggle-inline-images behavior
which refresh only in current level scope instead of whole buffer with
lot of inline images especially heavy and suspend Emacs. Because
function org-toggle-inline-images is used in of lot Emacs ob-* related
packages on after babel execution hook. Old behavior caused execute one
source block need to refresh whole buffer all images. Instead of only
current headline images, or just results image.

From 635624cb8446791b9e39f2803077ac9fa6d17225 Mon Sep 17 00:00:00 2001
From: stardiviner <numbch...@gmail.com>
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.el | 92 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 77 insertions(+), 15 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index e72cf056a..f847a42e7 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16160,22 +16160,84 @@ 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 include-linked beg end)
+  "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 none, remove it otherwise.
+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...")
+    (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))))
+   ;; Display region selected inline images.
+   ((use-region-p)
+    (if (seq-contains-p
+         (mapcar
+          (lambda (ov)
+            (plist-get (overlay-properties ov) 'org-image-overlay))
+          (overlays-in beg end))
+         t)
+        (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 nil (region-beginning) (region-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)

-- 

[ stardiviner ]
I try to make every word tell the meaning that I want to express without 
misunderstanding.

Blog: https://stardiviner.github.io/
IRC(libera.chat, freenode): stardiviner, Matrix: stardiviner
GPG: F09F650D7D674819892591401B5DF1C95AE89AC3

Attachment: signature.asc
Description: PGP signature

Reply via email to