emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Christopher M. Miles" <numbchild@gmail.com>
To: Ihor Radchenko <yantar92@gmail.com>
Cc: "Christopher M. Miles" <numbchild@gmail.com>, emacs-orgmode@gnu.org
Subject: Re: [PATCH 2-v2] New: auto display inline images under subtree when `org-cycle'.
Date: Fri, 30 Sep 2022 16:27:24 +0800	[thread overview]
Message-ID: <m2zgehl0vv.fsf@numbchild@gmail.com> (raw)
In-Reply-To: <87o7ux4khz.fsf@localhost>


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


Ihor Radchenko <yantar92@gmail.com> writes:

> "Christopher M. Miles" <numbchild@gmail.com> writes:
>
>> Ihor Radchenko <yantar92@gmail.com> writes:
>>
>> I got a new problem in patch, the (point-max) in function
>> ~org-cycle-display-inline-images~ SOMETIMES return EOF error instead of
>> correct max point value when org-cycle `state' is ~'folded~. I can't
>> find out what caused this even using Edebug. Do you have any clue?
>
> Maybe because the 'folded branch in that function is not wrapped into
> `save-restriction'?
>

Ah, Indeed, I forgot to wrap it. Now I fixed this error.

I attached new version patch.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: 0001-org.el-Support-auto-display-inline-images-when-cycli.patch --]
[-- Type: text/x-patch, Size: 8922 bytes --]

From 570551a39038cf4190cd7cb453a8d059326b9dcb Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@gmail.com>
Date: Wed, 28 Sep 2022 20:46:52 +0800
Subject: [PATCH] org.el: Support auto display inline images when cycling

* lisp/org.el (org-toggle-inline-images): Support region.
(org-display-inline-images): Fix refresh argument logic.
(org-remove-inline-images): Support region.

* lisp/org-keys.el (org-toggle-inline-images): Update arguments.

* lisp/org-cycle.el (org-cycle-inline-images-display): Add new option to
control whether auto display inline images when cycling.
(org-cycle-display-inline-images): Add new hook function to auto display
inline images when cycling.
(org-cycle-hook): Add `org-cycle-display-inline-images' into cycling
hook by default.

* testing/lisp/test-org-fold.el (test-org-fold/org-fold-display-inline-images):
Add test for inline image displaying when cycling.
---
 lisp/org-cycle.el             | 41 ++++++++++++++++++++++++++++--
 lisp/org-keys.el              |  2 +-
 lisp/org.el                   | 21 +++++++++------
 testing/lisp/test-org-fold.el | 48 +++++++++++++++++++++++++++++++++++
 4 files changed, 101 insertions(+), 11 deletions(-)

diff --git a/lisp/org-cycle.el b/lisp/org-cycle.el
index 656ca83f2..6204b8a4f 100644
--- a/lisp/org-cycle.el
+++ b/lisp/org-cycle.el
@@ -208,8 +208,9 @@ the values `folded', `children', or `subtree'."
   :type 'hook)
 
 (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
-		   org-cycle-show-empty-lines
-		   org-cycle-optimize-window-after-visibility-change)
+                            org-cycle-show-empty-lines
+                            org-cycle-optimize-window-after-visibility-change
+                            org-cycle-display-inline-images)
   "Hook that is run after `org-cycle' has changed the buffer visibility.
 The function(s) in this hook must accept a single argument which indicates
 the new state that was set by the most recent `org-cycle' command.  The
@@ -229,6 +230,12 @@ normal outline commands like `show-all', but not with the cycling commands."
   :group 'org-cycle
   :type 'boolean)
 
+(defcustom org-cycle-inline-images-display nil
+  "Non-nil means auto display inline images under subtree when cycling."
+  :group 'org-startup
+  :group 'org-cycle
+  :type 'boolean)
+
 (defvar org-cycle-tab-first-hook nil
   "Hook for functions to attach themselves to TAB.
 See `org-ctrl-c-ctrl-c-hook' for more information.
@@ -776,6 +783,36 @@ STATE should be one of the symbols listed in the docstring of
 		       "Subtree is archived and stays closed.  Use \
 `\\[org-cycle-force-archived]' to cycle it anyway."))))))
 
+(defun org-cycle-display-inline-images (state)
+  "Auto display inline images under subtree when cycling.
+It works when `org-cycle-inline-images-display' is non-nil."
+  (when org-cycle-inline-images-display
+    (pcase state
+      ('children
+       (save-excursion
+         (save-restriction
+           (org-narrow-to-subtree)
+           ;; If has nested headlines, beg,end only from parent
+           ;; headline to first child headline which reference to
+           ;; upper let-binding `org-next-visible-heading'.
+           (org-display-inline-images
+            nil nil
+            (point-min) (progn (org-next-visible-heading 1) (point))))))
+      ('subtree
+       (save-excursion
+         (save-restriction
+           (org-narrow-to-subtree)
+           ;; If has nested headlines, also inline display images under all sub-headlines.
+           (org-display-inline-images nil nil (point-min) (point-max)))))
+      ('folded
+       (save-excursion
+         (save-restriction
+           (org-narrow-to-subtree)
+           ;; FIXME: sometimes `point-max' is EOF.
+           (if (numberp (point-max))
+               (org-remove-inline-images (point-min) (point-max))
+             (ignore))))))))
+
 (provide 'org-cycle)
 
 ;;; org-cycle.el ends here
diff --git a/lisp/org-keys.el b/lisp/org-keys.el
index d65379a72..79e34cbd1 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))
+(declare-function org-toggle-inline-images "org" (&optional include-linked beg end))
 (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 036384a04..36db62f0c 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16071,16 +16071,16 @@ SNIPPETS-P indicates if this is run to create snippet images for HTML."
 
 (defvar-local org-inline-image-overlays nil)
 
-(defun org-toggle-inline-images (&optional include-linked)
+(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'."
   (interactive "P")
   (if org-inline-image-overlays
       (progn
-	(org-remove-inline-images)
+	(org-remove-inline-images beg end)
 	(when (called-interactively-p 'interactive)
 	  (message "Inline image display turned off")))
-    (org-display-inline-images include-linked)
+    (org-display-inline-images include-linked nil beg end)
     (when (called-interactively-p 'interactive)
       (message (if org-inline-image-overlays
 		   (format "%d images displayed inline"
@@ -16170,8 +16170,8 @@ BEG and END define the considered part.  They default to the
 buffer boundaries with possible narrowing."
   (interactive "P")
   (when (display-graphic-p)
-    (unless refresh
-      (org-remove-inline-images)
+    (when refresh
+      (org-remove-inline-images beg end)
       (when (fboundp 'clear-image-cache) (clear-image-cache)))
     (let ((end (or end (point-max))))
       (org-with-point-at (or beg (point-min))
@@ -16322,11 +16322,16 @@ buffer boundaries with possible narrowing."
       (delete ov org-inline-image-overlays)
       (delete-overlay ov))))
 
-(defun org-remove-inline-images ()
+(defun org-remove-inline-images (&optional beg end)
   "Remove inline display of images."
   (interactive)
-  (mapc #'delete-overlay org-inline-image-overlays)
-  (setq org-inline-image-overlays nil))
+  (let* ((beg (or beg (point-min)))
+         (end (or end (point-max)))
+         (overlays (overlays-in beg end)))
+    (dolist (ov overlays)
+      (when (memq ov org-inline-image-overlays)
+        (setq org-inline-image-overlays (delq ov org-inline-image-overlays))
+        (delete-overlay ov)))))
 
 (defvar org-self-insert-command-undo-counter 0)
 (defvar org-speed-command nil)
diff --git a/testing/lisp/test-org-fold.el b/testing/lisp/test-org-fold.el
index 0fa800dee..5c47cafb4 100644
--- a/testing/lisp/test-org-fold.el
+++ b/testing/lisp/test-org-fold.el
@@ -604,6 +604,54 @@ Unfolded Paragraph.
             (org-fold-check-before-invisible-edit kind)
             (should (org-invisible-p (1- (point))))))))))
 
+;;; TODO: reference `test-org-fold/org-fold-reveal-broken-structure'
+(ert-deftest test-org-fold/org-fold-display-inline-images ()
+  "Test inline images displaying when cycling."
+  (let ((org-fold-core-style 'text-properties)
+        (org-cycle-inline-images-display t))
+    ;; `org-cycle' -(state)-> `'children' display child inline images.
+    (org-test-with-temp-text
+     "<point>* Heading 1
+[[file:image.png]]"
+     (org-overview)
+     (org-cycle)
+     (org-next-link)
+     (should (overlays-at (point)))
+     (org-toggle-inline-images)
+     (should-not (overlays-at (point))))
+    ;; `org-cycle' -(state)-> `'subtree' display subtrees inline images.
+    (org-test-with-temp-text
+     "<point>* Heading 1
+[[file:image.png]]
+** Subheading 1
+[[file:image2.png]]
+** Subheading 2
+[[file:image3.png]]"
+     (org-overview)
+     (org-cycle)
+     (org-cycle)
+     (org-next-link)
+     (org-next-link)
+     (should (overlays-at (point)))
+     (org-toggle-inline-images)
+     (should-not (overlays-at (point))))
+    ;; `org-cycle' -(state)-> `'folded' remove inline image overlays.
+    (org-test-with-temp-text
+     "<point>* Heading 1
+[[file:image.png]]
+** Subheading 1
+[[file:image2.png]]
+** Subheading 2
+[[file:image3.png]]"
+     (org-overview)
+     (org-cycle)
+     (org-cycle)
+     (org-cycle)
+     ;; TODO: how to detect inline image overlays are deleted after folded?
+     (should (overlays-at (point)))
+     (org-toggle-inline-images)
+     (should-not (overlays-at (point))))))
+
 (provide 'test-org-fold)
 
 ;;; test-org-fold.el ends here
-- 
2.37.2


[-- Attachment #1.3: Type: text/plain, Size: 1005 bytes --]


>>> You are providing both hook and a customization. It is redundant. Users
>>> can already remove the hook when desired.
>>
>> I mock this style from a very similar existing hook function
>> ~org-cycle-hide-archived-subtrees~ and custom variable
>> ~org-cycle-open-archived-trees~. I think removing a hook function from
>> ~org-cycle-hook~ is a way, but not as convenient as defcustom option.
>> (P.S: I think users prefer this defcustom customization style.)
>
> I see your point.
> I agree that having a variable is better and also leaves an option to
> introduce in-buffer setting for this.
>
> Let's leave the hook part for now - if we want to alter it, we may need
> to re-consider all the default hooks.

Thanks

-- 

[ 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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

  reply	other threads:[~2022-09-30  8:31 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-22  6:34 [QUESTION] How to specific image size attributes under headline scope? Christopher M. Miles
2021-11-22 18:31 ` Timothy
2022-08-18 12:44   ` Christopher M. Miles
     [not found]   ` <m2o7whra7j.fsf@numbchild>
2022-08-22 17:10     ` Timothy
2022-08-23  0:24       ` Christopher M. Miles
2022-09-11  2:20 ` [SOLVED] " Christopher M. Miles
2022-09-11  2:25 ` Christopher M. Miles
     [not found] ` <631d472b.c80a0220.2b4b2.bf86SMTPIN_ADDED_BROKEN@mx.google.com>
2022-09-11 12:34   ` Ihor Radchenko
2022-09-12  1:54     ` [PATCH] " Christopher M. Miles
2022-09-12  1:59     ` [PATCH] New: auto display inline images under subtree when `org-cycle' Christopher M. Miles
     [not found]     ` <m2wna9bbc2.fsf@numbchild>
2022-09-12  6:03       ` [ISSUE] " Christopher M. Miles
     [not found]     ` <631e92ee.050a0220.f9c18.92f5SMTPIN_ADDED_BROKEN@mx.google.com>
2022-09-12 10:51       ` Ihor Radchenko
2022-09-13  1:09         ` [PATCH 2] " Christopher M. Miles
     [not found]         ` <m2leqogiz8.fsf@numbchild>
2022-09-13  1:48           ` [PATCH 3] " Christopher M. Miles
     [not found]           ` <631fe1c9.050a0220.3ab2b.3f52SMTPIN_ADDED_BROKEN@mx.google.com>
2022-09-15  3:27             ` Ihor Radchenko
2022-09-15  4:53               ` [PATCH 4] " Christopher M. Miles
     [not found]               ` <6322b0a8.050a0220.59bb8.6923SMTPIN_ADDED_BROKEN@mx.google.com>
2022-09-15  8:47                 ` Ihor Radchenko
2022-09-15  9:43                   ` [PATCH 5] " Christopher M. Miles
     [not found]                   ` <6322f5ad.c80a0220.5e936.823eSMTPIN_ADDED_BROKEN@mx.google.com>
2022-09-20  7:01                     ` [PATCH v6] " Ihor Radchenko
2022-09-20 11:32                       ` [PATCH v6] " Christopher M. Miles
     [not found]                       ` <6329c8b0.050a0220.412d.0a6cSMTPIN_ADDED_BROKEN@mx.google.com>
2022-09-21  7:54                         ` Ihor Radchenko
2022-09-28 12:55                           ` [PATCH 2-v1] " Christopher M. Miles
     [not found]                           ` <633454e3.050a0220.7278b.1fa5SMTPIN_ADDED_BROKEN@mx.google.com>
2022-09-29  3:05                             ` Ihor Radchenko
2022-09-29  6:06                               ` Christopher M. Miles
2022-09-29  6:57                               ` [PATCH 2-v1 (test v1)] " Christopher M. Miles
     [not found]                               ` <63353c69.370a0220.67788.e8a1SMTPIN_ADDED_BROKEN@mx.google.com>
2022-09-30  3:19                                 ` [PATCH 2-v1] " Ihor Radchenko
2022-09-30  8:27                                   ` Christopher M. Miles [this message]
     [not found]                                   ` <6336a955.050a0220.4e72e.2b23SMTPIN_ADDED_BROKEN@mx.google.com>
2022-10-01  3:16                                     ` [PATCH 2-v2] " Ihor Radchenko
2022-10-01  9:51                                       ` [PATCH 2-v3] " Christopher M. Miles
     [not found]                                       ` <63380f57.370a0220.a9d9a.dee8SMTPIN_ADDED_BROKEN@mx.google.com>
2022-10-03  3:21                                         ` Ihor Radchenko
2022-10-03  4:37                                           ` [PATCH 2-v4] " Christopher M. Miles
     [not found]                                           ` <633a67d8.050a0220.733e8.e57dSMTPIN_ADDED_BROKEN@mx.google.com>
2022-10-04  4:36                                             ` Ihor Radchenko
2022-10-04  7:27                                               ` Christopher M. Miles
     [not found]                                               ` <633be6d3.370a0220.4060.bacdSMTPIN_ADDED_BROKEN@mx.google.com>
2022-10-08  7:53                                                 ` Ihor Radchenko
2022-10-08  9:50                                                   ` Christopher M. Miles
     [not found]                                                   ` <634149f4.c80a0220.1376.e564SMTPIN_ADDED_BROKEN@mx.google.com>
2022-10-09  7:21                                                     ` Ihor Radchenko
2022-10-10  8:40                                                       ` Christopher M. Miles
     [not found]                                                       ` <m2v8os5aqy.fsf@numbchild>
2022-10-22  1:29                                                         ` Christopher M. Miles
2022-10-23  4:33                                                           ` Ihor Radchenko
2022-10-23  7:14                                                             ` Christopher M. Miles
2022-10-25  7:23                                                               ` Ihor Radchenko
2022-10-25 14:22                                                                 ` [PATCH v3] " Christopher M. Miles
2022-10-26  4:50                                                                   ` 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=m2zgehl0vv.fsf@numbchild@gmail.com \
    --to=numbchild@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@gmail.com \
    /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).