[PATCH 3] New: auto display inline images under subtree when `org-cycle'.

2022-09-12 Thread Christopher M. Miles

"Christopher M. Miles"  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 
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 
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)
+  

Re: [PATCH 3] New: auto display inline images under subtree when `org-cycle'.

2022-09-14 Thread Ihor Radchenko
"Christopher M. Miles"  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.

I will comment only on the first patch for now.

>  "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")

This change should also be mentioned in the commit message.

> @@ -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)))

You are checking the property too late. I suggest to let-bind
org-image-actual-width around the whole `cond' instead.

That way, subtree-local value of ORG-IMAGE-ACTUAL-WIDTH will have an
effect of setting `org-image-actual-width' value.

Also, is there any particular reason why you wrap
`org-property-or-variable-value' into `ignore-errors'?

Finally, you can simply do

  (org-property-or-variable-value 'org-image-actual-width) 

It will return the property value _or_ org-image-actual-width value.

> -  (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)))

With my last suggestion, this code will not be needed.

Also, this change should be documented in etc/NEWS and also in
"12.7 Images" section of the manual.

-- 
Ihor Radchenko,
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