"Christopher M. Miles" <numbch...@gmail.com> writes:

Ihor, My new patch still have some issue in the 'fold state which I
added "FIXME" marker. Hope can get some solution from you.

From 4a56ec6b09e09d92a81e3aa72f4197d716d0ca87 Mon Sep 17 00:00:00 2001
From: stardiviner <numbch...@gmail.com>
Date: Mon, 12 Sep 2022 09:45:09 +0800
Subject: [PATCH 1/2] org.el: Support subtree-level image width specification

* lisp/org.el (org-display-inline-image--width): Detect subtree-level
property "ORG-IMAGE-ACTUAL-WIDTH" value as image width before fallback.
---
 lisp/org.el | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 4eef56f45..07c86229b 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -12128,7 +12128,8 @@ but in some other way.")
     "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
     "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
     "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
-    "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
+    "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS"
+    "ORG-IMAGE-ACTUAL-WIDTH")
   "Some properties that are used by Org mode for various purposes.
 Being in this list makes sure that they are offered for completion.")
 
@@ -16164,14 +16165,19 @@ buffer boundaries with possible narrowing."
                                (org-element-property :begin par)
                              (re-search-forward attr-re par-end t)))
               (match-string 1)))
+           ;; support subtree-level property "ORG-IMAGE-ACTUAL-WIDTH" specified width.
+           (subtree-property-width
+            (ignore-errors (org-property-or-variable-value 'ORG-IMAGE-ACTUAL-WIDTH)))
            (width
             (cond
              ;; Treat :width t as if `org-image-actual-width' were t.
              ((string= attr-width "t") nil)
-             ;; Fallback to `org-image-actual-width' if no interprable width is given.
              ((or (null attr-width)
                   (string-match-p "\\`[^0-9]" attr-width))
-              (car org-image-actual-width))
+              ;; Try to use subtree-level width before fallback.
+              (or subtree-property-width
+                  ;; Fallback to `org-image-actual-width' if no interprable width is given.
+                  (car org-image-actual-width)))
              ;; Convert numeric widths to numbers, converting percentages.
              ((string-match-p "\\`[0-9.]+%" attr-width)
               (/ (string-to-number attr-width) 100.0))
-- 
2.37.2

From 2fc76c4581b60c6631186bfdc489732075432c26 Mon Sep 17 00:00:00 2001
From: stardiviner <numbch...@gmail.com>
Date: Tue, 13 Sep 2022 09:04:03 +0800
Subject: [PATCH 2/2] org.el: Add hook function to auto display inline images
 of subtree

* lisp/org.el (org-display-subtree-with-inline-images): Add an option
and hook function to auto display of inline images under current
expanded visible subtree.
---
 lisp/org.el | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/lisp/org.el b/lisp/org.el
index 07c86229b..0bd942e07 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -1104,6 +1104,13 @@ the following lines anywhere in the buffer:
   :version "24.1"
   :type 'boolean)
 
+(defcustom org-cycle-display-inline-images nil
+  "Non-nil means auto display inline images under subtree when `org-cycle'
+by the function `org-display-subtree-with-inline-images' on hook `org-cycle-hook'."
+  :group 'org-startup
+  :version "24.1"
+  :type 'boolean)
+
 (defcustom org-startup-with-latex-preview nil
   "Non-nil means preview LaTeX fragments when loading a new Org file.
 
@@ -16139,6 +16146,42 @@ buffer boundaries with possible narrowing."
 				(overlay-put ov 'keymap image-map))
 			      (push ov org-inline-image-overlays))))))))))))))))
 
+(defun org-display-subtree-inline-images (&optional state)
+  "Toggle the display of inline images under current expanded visible subtree.
+This hook function will auto display or hide inline images after `org-cycle'.
+It is used to hook on `org-cycle-hook'.
+The function behavior is controlled by `org-cycle-display-inline-images'."
+  (interactive)
+  (when (and org-cycle-display-inline-images
+             ;; not global state change from `org-cycle-global'.
+             (not (memq state '(overview contents all))))
+    (pcase state
+      ('children
+       (save-excursion
+         (save-restriction
+           (org-narrow-to-subtree)
+           ;; If has nested headlines, beg,end only from parent headling
+           ;; to first child headline which reference to upper let-binding
+           ;; `org-next-visible-heading'.
+           (org-display-inline-images t t (point-min) (progn (org-next-visible-heading 1) (point))))))
+      ('subtree
+       (save-excursion
+         (save-restriction
+           (org-narrow-to-subtree)
+           ;; If has nested headlines, also display inline images under all subtrees.
+           (org-display-inline-images t t (point-min) (point-max)))))
+      ('folded
+       ;; (save-excursion
+       ;;   (save-restriction
+       ;;     (org-narrow-to-subtree)
+       ;;     ;; FIXME: this will auto expand heading caused `org-cycle' folded failed.
+       ;;     (mapc #'delete-overlay (overlays-in (point-min) (point-max)))))
+       )
+      ('nil (funcall 'org-toggle-inline-images))
+      (t nil))))
+
+(add-hook 'org-cycle-hook #'org-display-subtree-inline-images)
+
 (defvar visual-fill-column-width) ; Silence compiler warning
 (defun org-display-inline-image--width (link)
   "Determine the display width of the image LINK, in pixels.
-- 
2.37.2


-- 

[ 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