emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: stardiviner <numbchild@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: Org mode <emacs-orgmode@gnu.org>
Subject: Re: [PATCH v4.0] Re: [PATCH] add a function to only refresh inline images under current headline instead of global buffer
Date: Fri, 2 Aug 2024 06:58:46 +0800	[thread overview]
Message-ID: <CAL1eYuLAhvQQu8gJQ_7yxdBTuYRcHN557vvLhGAabA4tcgxD_w@mail.gmail.com> (raw)
In-Reply-To: <871q3logb9.fsf@localhost>


[-- Attachment #1.1: Type: text/plain, Size: 3178 bytes --]

I followed you upper 8 conditions to re-write my patch.
Except the 8. condition I'm not sure I understand correctly.
And I extend 1. condition to support the inline image link at point toggle
displaying.

[stardiviner]           <Hack this world!>      GPG key ID: 47C32433
IRC(freeenode): stardiviner                     Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/


On Mon, Jul 22, 2024 at 6:44 PM Ihor Radchenko <yantar92@posteo.net> wrote:

> "Christopher M. Miles" <numbchild@gmail.com> writes:
>
> >> Then, for backward compatibility, we may treat any non-nil, non-list
> >> (like '(4), '(16), '(64)), non-number (like 1, 11) value as
> >> INCLUDE-LINKED. This way, the existing calls like
> >> (org-toggle-inline-images t) will not be broken.
> > ...
> > I prefer this compromise result.
> > I updated the patch, Please review it whether it's correct.
>
> I think that C-u C-u unconditionally including linked images is
> confusing.
>
> What about using my idea with M-1/M-11 to be interpreted as
> INCLUDE-LINKED=t?
>
> Also, you did not document in the docstring what happens if ARG is
> something like t.
>
> > I did a source code statistics researching of using the function
> org-toggle-inline-images at two places:
> >
> > - GitHub:
> https://github.com/search?q=org-toggle-inline-images&ref=opensearch&type=code&p=3
> > (Only package scimax incoke this function with INCLUDE-LINKED argument t)
> > - My installed Emacs packages, NO package invoke this function with
> argument INCLUDE-LINKED.
> >
> > So lucky this change will not affect lot.
>
> I have found user configs using the argument:
>
>
> https://github.com/search?q=%22%28org-toggle-inline-images+t%29%22&type=code
> https://github.com/search?q=%22%28org-toggle-inline-images+%27%22&type=code
>
> So, please do as I advised - treat non-special ARG values as
> INCLUDE-LINKED.
>
> Also, looking at the proposed prefix arguments, I feel that it might not
> necessarily be the best idea to copy over what org-latex-preview does
> exactly. org-latex-preview is not a "toggle", unlike
> org-toggle-inline-images
>
> What about the following treatment of ARG:
>
> 1. No argument, no region selected :: toggle (display or hide dwim) images
> in current section
> 2. No argument, region selected: toggle images in region
> 3. C-u argument :: toggle images in the whole buffer
> 4. C-u C-u argument, no region selected :: unconditionally hide images in
> the buffer
> 5. M-1 argument, no region selected :: display images in current section
> with INCLUDE-LINKED
> 6. M-1 argument, region selected :: ... in region ...
> 7. M-11 argument :: ... in the whole buffer ...
> 8. Any other argument :: treat as INCLUDE-LINKED = t
>
> And please document all the new arguments in the manual and etc/ORG-NEWS
> file.
>
> --
> Ihor Radchenko // yantar92,
> Org mode contributor,
> Learn more about Org mode at <https://orgmode.org/>.
> Support Org development at <https://liberapay.com/org-mode>,
> or support my work at <https://liberapay.com/yantar92>
>

[-- Attachment #1.2: Type: text/html, Size: 4784 bytes --]

[-- Attachment #2: 0001-org-Implement-conditionally-inline-images-displaying.patch --]
[-- Type: application/octet-stream, Size: 8511 bytes --]

From 63c482d0c3782dcb328581f1d56275a131e78610 Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@gmail.com>
Date: Mon, 22 May 2023 16:25:33 +0800
Subject: [PATCH] org: Implement conditionally inline images displaying logic

* lisp/org.el (org-toggle-inline-images): Implement conditionally
inline images displaying logic based on universal-argument.
---
 etc/ORG-NEWS     |   5 ++
 lisp/org-keys.el |   2 +-
 lisp/org.el      | 117 +++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 108 insertions(+), 16 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 8a2a1ec0e..2798fb131 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -113,6 +113,11 @@ accept the INFO channel and return a string.  This makes it possible
 to dynamically generate the content of the resulting ~<head>~ tag in
 the resulting HTML document.
 
+*** ~org-toggle-inline-images~ implement conditionally inline images displaying logic
+
+You can display or hide inline images at point, region, or current
+section through universal-argument.
+
 ** Miscellaneous
 *** ~org-refile~ now saves current position to Org mark ring when jumping to heading
 
diff --git a/lisp/org-keys.el b/lisp/org-keys.el
index edd4059fc..52608b747 100644
--- a/lisp/org-keys.el
+++ b/lisp/org-keys.el
@@ -218,7 +218,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 362f0c158..83e440632 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16665,22 +16665,109 @@ 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)
+  "Toggle the display of inline images at point.
+
+The parameter ARG from `\\[universal-argument]' is used as condition in bellowing \"argument\".
+
+1. No argument, no region selected :: toggle (display or hide dwim) images in current section or image link at point
+2. No argument, region selected :: toggle images in region
+3. C-u argument :: toggle images in the whole buffer
+4. C-u C-u argument, no region selected :: unconditionally hide images in the buffer
+5. M-1 argument, no region selected :: display images in current section with INCLUDE-LINKED
+6. M-1 argument, region selected :: ... in region ...
+7. M-11 argument :: ... in the whole buffer ...
+8. Any other argument :: treat as INCLUDE-LINKED = t"
   (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)) (message "Your Emacs does not support displaying images!"))
+   ;; 1. No argument, no region selected :: toggle (display or hide dwim) images in current section or image link at point.
+   ((and (null arg) (not (use-region-p)))
+    (let ((context (org-element-context))
+          (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)))
+          (include-linked nil))
+      ;; toggle display of inline image link at point.
+      (if (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 "Remove inline image at point."))
+	      (org-display-inline-images include-linked t beg end)
+	      (message "Display inline image at point ... done.")))
+        (if (org--inline-image-overlays beg end)
+            (org-remove-inline-images beg end)
+          (message "Display inline images in section...")
+          (org-display-inline-images include-linked t beg end)
+          (message "Display inline images in section... done.")))))
+   ;; 2. No argument, region selected :: toggle images in region.
+   ((and (null arg) (use-region-p))
+    (let* ((beg (region-beginning))
+           (end (region-end))
+           (include-linked nil)
+           (inline-images (org--inline-image-overlays beg end)))
+      (if (org--inline-image-overlays beg end)
+          (progn
+            (org-remove-inline-images beg end)
+            (message "%d inline images display removed." (length inline-images)))
+        (message "Display inline images displayed in region...")
+        (org-display-inline-images include-linked t beg end)
+        (message "Display inline images displayed in region... done."))))
+   ;; 3. C-u argument :: toggle images in the whole buffer.
+   ((equal arg '(4))
+    (let ((include-linked nil))
+      (if (org--inline-image-overlays (point-min) (point-max))
+          (org-remove-inline-images (point-min) (point-max))
+        (message "Display all inline images in buffer...")
+        (org-display-inline-images include-linked nil (point-min) (point-max))
+        (message "Display all inline images in buffer... done."))))
+   ;; 4. C-u C-u argument, no region selected :: unconditionally hide images in the buffer.
+   ((and (equal arg '(16)) (not (use-region-p)))
+    (org-remove-inline-images (point-min) (point-max))
+    (message "Remove all inline images in buffer displaying."))
+   ;; 5. M-1 argument, no region selected :: display images in current section with `INCLUDE-LINKED'.
+   ((and (equal arg 1) (not (use-region-p)))
+    (let ((context (org-element-context))
+          (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)))
+          (include-linked t))
+      (message "Display inline images in section...")
+      (org-display-inline-images include-linked t beg end)
+      (message "Display inline images in section... done.")))
+   ;; 6. M-1 argument, region selected :: ... in region ...
+   ;; [M-1] / [C-1] argument for linked images like:
+   ;; [[https://orgmode.org/resources/img/org-mode-unicorn.svg][description]]
+   ((and (equal arg 1) (use-region-p))
+    (let* ((beg (region-beginning))
+	   (end (region-end))
+           (include-linked t)
+           (inline-images (org--inline-image-overlays beg end)))
+      (org-display-inline-images include-linked t beg end)
+      (message "%d inline images displayed in region... done."  (length inline-images))))
+   ;; 7. M-11 argument :: ... in the whole buffer ...
+   ((equal arg '11)
+    (let ((beg (point-min))
+          (end (point-max))
+          (include-linked t))
+      (message "Display inline images in buffer...")
+      (org-display-inline-images include-linked t beg end)
+      (message "Display inline images in buffer... done.")))
+   ;; 8. Any other argument :: treat ARG as INCLUDE-LINKED = t
+   ((not (null arg))
+    (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)))
+          (include-linked arg))
+      (message "Display inline images in section...")
+      (org-display-inline-images include-linked t beg end)
+      (message "Display inline images in section... done.")))))
 
 (defun org-redisplay-inline-images ()
   "Assure display of inline images and refresh them."
-- 
2.39.3 (Apple Git-146)


  reply	other threads:[~2024-08-01 23:00 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-15  3:28 [PATCH] add a function to only refresh inline images under current headline instead of global buffer Christopher M. Miles
2023-05-15 11:08 ` Ihor Radchenko
2023-05-15 13:01   ` Christopher M. Miles
2023-05-15 14:00   ` [PATCH v2] " Christopher M. Miles
2023-05-16  9:17     ` Ihor Radchenko
2023-05-16 12:18       ` Christopher M. Miles
2023-07-24 11:25         ` Ihor Radchenko
2023-08-01  4:40           ` [PATCH v3] " Christopher M. Miles
2023-08-01  8:04             ` Ihor Radchenko
2023-08-01 12:17               ` [PATCH v3.1] " Christopher M. Miles
2023-08-01 14:09                 ` Ihor Radchenko
2023-08-01 15:22                   ` Christopher M. Miles
2023-08-01 15:46                   ` Christopher M. Miles
2023-08-02 16:08                     ` Ihor Radchenko
2023-08-04  6:30                       ` Christopher M. Miles
2023-08-02  7:26                 ` Ihor Radchenko
2023-08-02 15:44                   ` Christopher M. Miles
2023-08-04  8:20                     ` Ihor Radchenko
2023-08-05  5:28                       ` [PATCH v3.2] " Christopher M. Miles
2024-07-22 10:46                         ` Ihor Radchenko
2024-08-01 22:58                           ` stardiviner [this message]
     [not found]                           ` <66a8b73b.170a0220.383476.996e@mx.google.com>
2024-08-12 10:18                             ` [PATCH v4.0] " Ihor Radchenko
2024-08-14  2:04                               ` stardiviner
2024-08-18 10:27                                 ` Ihor Radchenko
2024-08-20  2:02                                   ` Karthik Chikmagalur
2024-08-20 15:43                                     ` Karthik Chikmagalur
2024-08-20 18:19                                       ` Ihor Radchenko
2024-08-20 19:46                                         ` Karthik Chikmagalur
2024-08-22 13:19                                           ` Ihor Radchenko
2024-08-23  6:04                                             ` Karthik Chikmagalur
2024-08-23 23:36                                             ` [PATCH v1] Inline image display as part of a new org-link-preview system Karthik Chikmagalur
2024-08-24  1:00                                               ` Karthik Chikmagalur
2024-08-31 14:22                                               ` Ihor Radchenko
2024-08-31 16:41                                                 ` Karthik Chikmagalur
2024-08-31 16:53                                                   ` Ihor Radchenko
2024-08-31 22:37                                                     ` [PATCH v2] " Karthik Chikmagalur
2024-09-01 13:06                                                       ` Ihor Radchenko
2024-09-02 20:13                                                         ` [PATCH v3] " Karthik Chikmagalur
2024-09-08  7:43                                                           ` Ihor Radchenko
2024-09-09  3:21                                                             ` Karthik Chikmagalur
2024-09-09  6:06                                                               ` Ihor Radchenko
2024-09-09  6:30                                                                 ` Karthik Chikmagalur
2024-09-09 16:47                                                                   ` Ihor Radchenko
2024-09-09 19:14                                                                     ` Karthik Chikmagalur
2024-09-10 16:57                                                                       ` Ihor Radchenko
2024-09-10 19:53                                                                         ` Karthik Chikmagalur
2024-09-15  7:51                                                                           ` Ihor Radchenko
2024-09-09 21:45                                                                 ` Karthik Chikmagalur
2024-09-10 16:58                                                                   ` Ihor Radchenko
2024-09-10 17:38                                                                     ` Karthik Chikmagalur
2024-09-10 18:34                                                                       ` Ihor Radchenko
2024-09-10 19:43                                                             ` Karthik Chikmagalur
2024-09-15  8:12                                                               ` Ihor Radchenko
2024-09-15 20:50                                                                 ` Karthik Chikmagalur
2024-09-15 21:57                                                                 ` [PATCH v4] " Karthik Chikmagalur
2024-09-17 18:16                                                                   ` Ihor Radchenko
2024-09-18  1:44                                                                     ` [PATCH v5] " Karthik Chikmagalur
2024-09-21 10:11                                                                       ` Ihor Radchenko
2024-09-22 22:00                                                                         ` [PATCH v6] " Karthik Chikmagalur
2024-10-03 17:03                                                                           ` Ihor Radchenko
2024-10-08  0:07                                                                             ` [PATCH v7] " Karthik Chikmagalur
2024-10-10 17:15                                                                               ` Ihor Radchenko
2024-10-10 21:23                                                                                 ` Karthik Chikmagalur
2024-10-12  8:15                                                                                   ` Ihor Radchenko
2024-10-28  6:13                                                                                     ` [PATCH v8] " Karthik Chikmagalur
2024-10-28 18:16                                                                                       ` Ihor Radchenko
2024-10-28 21:53                                                                                         ` [PATCH v9] " Karthik Chikmagalur
2024-10-29 18:46                                                                                           ` Ihor Radchenko
2024-10-29 20:45                                                                                             ` [PATCH v10] " Karthik Chikmagalur
2024-10-30 17:32                                                                                               ` Ihor Radchenko
2024-10-30 19:50                                                                                                 ` Karthik Chikmagalur
2024-10-31 18:16                                                                                                   ` Ihor Radchenko
2024-08-18 10:34                                 ` [FR] Automatically display images in resutls of evaluation (was: [PATCH v4.0] Re: [PATCH] add a function to only refresh inline images under current headline instead of global buffer) Ihor Radchenko
     [not found]                                   ` <66c54dfc.a70a0220.3c823a.2899@mx.google.com>
2024-08-22 13:06                                     ` [FR] Automatically display images in resutls of evaluation Ihor Radchenko
     [not found]                                       ` <66c89411.170a0220.3255c1.0cd5@mx.google.com>
     [not found]                                         ` <87zfor7b04.fsf@localhost>
     [not found]                                           ` <CAL1eYuLOsaS43ahueN4uWiCn+Ykp=p_-t9dzAypKdy1en_53BQ@mail.gmail.com>
2024-09-15 13:33                                             ` [PATCH v3] " Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAL1eYuLAhvQQu8gJQ_7yxdBTuYRcHN557vvLhGAabA4tcgxD_w@mail.gmail.com \
    --to=numbchild@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).