Ihor Radchenko <yanta...@gmail.com> writes:

> "Christopher M. Miles" <numbch...@gmail.com> writes:
>
>>> When I put it on beginning of org document, then preview inline images, but 
>>> those global attributes
>>> not affected. The inline images still display in actual image size. I want 
>>> to specify image size
>>> under headline properties. Is it possible to do this?
>>
>> I implemented this feature by written a hook. Hope this can be helpful
>> for someone who also has this needs. Here is the code:
>
> This looks useful.
> Would you be interested to write a patch?

From 2ba9c048dd2609eb22a3924dd15f041b648ef859 Mon Sep 17 00:00:00 2001
From: stardiviner <numbch...@gmail.com>
Date: Mon, 12 Sep 2022 09:45:09 +0800
Subject: [PATCH] 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 | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/lisp/org.el b/lisp/org.el
index 6e6c437d5..594006551 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -1108,6 +1108,14 @@ the following lines anywhere in the buffer:
   :version "24.1"
   :type 'boolean)
 
+(defcustom org-cycle-display-inline-images nil
+  "Non-nil means show inline images when `org-cycle'.
+When this is t, org-mode will add 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.
 
@@ -16284,6 +16292,57 @@ buffer boundaries with possible narrowing."
   (mapc #'delete-overlay org-inline-image-overlays)
   (setq org-inline-image-overlays nil))
 
+(defun org-display-subtree-with-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)
+  ;; (not (memq state '(overview folded contents))) ; global state change
+  (when org-cycle-display-inline-images
+    (let ((display-inline-images-local
+           (lambda (beg end)
+             (org-display-inline-images t t beg end)
+             ;; display number of inline images which is intersection of two image overlays lists.
+             ;; (message "%d images displayed inline"
+             ;;          (length (cl-intersection org-inline-image-overlays (overlays-in beg end))))
+             ))
+          (hide-inline-images-local
+           (lambda ()
+             (setq org-inline-image-overlays nil) ; (org-remove-inline-images)
+             (message "Inline image display turned off")))
+          (inline-image-width-override    ; get headline property value as inline image width.
+           (lambda ()
+             ;; The property "INLINE-IMAGE-WIDTH" value should be integer. Reference `org-display-inline-image--width'.
+             (let* ((property-value (ignore-errors (org-property-or-variable-value 'INLINE-IMAGE-WIDTH)))
+                    (width-integer (or (and property-value
+                                            (if (numberp property-value) property-value (string-to-number property-value)))
+                                       (- (/ (window-width (selected-window) t) 2) 100))))
+               (setq-local org-image-actual-width width-integer)))))
+      (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'.
+             (funcall display-inline-images-local (point-min) (progn (org-next-visible-heading 1) (point))))))
+        ('subtree
+         (save-excursion
+           (save-restriction
+             (org-narrow-to-subtree)
+             (funcall inline-image-width-override)
+             ;; If has nested headlines, also inline display images under all sub-headlines.
+             (funcall display-inline-images-local (point-min) (point-max)))))
+        ('folded
+         (funcall hide-inline-images-local))
+        ('nil (funcall 'org-toggle-inline-images))))))
+
+(add-hook 'org-cycle-hook #'org-display-subtree-with-inline-images)
+
+(add-to-list 'org-default-properties "INLINE-IMAGE-WIDTH")
+
 (defvar org-self-insert-command-undo-counter 0)
 (defvar org-speed-command nil)
 
-- 
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