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

> "Christopher M. Miles" <numbch...@gmail.com> writes:
>
>> 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?
>>
>> The is the new patch which updated the defcustom variable docstring has 
>> correct description.
>
> Thanks for the patch!
> Before extending org-cycle functionality, let's first try to sort out
> subtree-level setting of the image size.

This is implemented by retrieve subtree-level property value in function
~~org-display-inline-image--width~~.

I submitted two separated patches.

>
> In the patch, you are suggesting to use INLINE-IMAGE-WIDTH property.
> However, if we apply the patch as is, this property will only be useful
> when calling a single function -
> `org-display-subtree-with-inline-images'.
> It is not ideal.

The patch's purpose is auto display inline images when [TAB] (~org-cycle~) 
expanding heading or
subtrees. Not for global ~org-toggle-inline-images~. Also, introduce a new 
extra argument "~state~" for
~org-display-inline-images~ like bellowing is not ideal. I think should 
separate the ~org-cycle~ logic
instead of put in ~org-display-inline-images~. In global scope, 
~org-toggle-inline-images~ already
respect the updated function ~org-display-inline-image--width~, it should 
respect the subtree property width.

#+begin_src emacs-lisp
;;                                           V
;;                                          -----
(defun org-display-inline-images (&optional state include-linked refresh beg 
end)
  ...)
#+end_src

*NOTE*: Because ~org-cycle-hook~ will pass a ~state~ variable to hook function.

>
> If we introduce subtree-level setting for inline image size, it should
> also work when displaying inline images by other existing means
> (org-toggle-inline-images).

This is implemented by retrieve subtree-level property value in function
~~org-display-inline-image--width~~.

>
> Instead of working around the existing code of
> `org-display-inline-images' and setting a custom
> `org-image-actual-width' variable in buffer, it is much better to change
> `org-display-inline-images' directly. At the end, the patch is not bound
> to your personal config - feel free to alter the existing Org functions.

This is implemented by retrieve subtree-level property value in function
~~org-display-inline-image--width~~.

>
> Similarly, you do not need to do
> (add-to-list 'org-default-properties "INLINE-IMAGE-WIDTH")
> Simply change the default value of `org-default-properties'.

Updated

>
> Finally, it will be more consistent to name the property closer to the
> variable name: ORG-IMAGE-ACTUAL-WIDTH.

Updated

From cc322145c2248e95bc76fee7e6d33f575d93e79f 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 6e6c437d5..70333c609 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -12199,7 +12199,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.")
 
@@ -16241,14 +16242,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 98babf12b2d9dbc38250186e0284fad6d937e3f3 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 | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/lisp/org.el b/lisp/org.el
index 70333c609..30716c04e 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -1108,6 +1108,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.
 
@@ -16216,6 +16223,38 @@ 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
+       (org-narrow-to-subtree)
+       (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