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: [PATCH 2] New: auto display inline images under subtree when `org-cycle'.
Date: Tue, 13 Sep 2022 09:09:08 +0800	[thread overview]
Message-ID: <m2leqogiz8.fsf@numbchild@gmail.com> (raw)
In-Reply-To: <87y1uovp9w.fsf@localhost>


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


Ihor Radchenko <yantar92@gmail.com> writes:

> "Christopher M. Miles" <numbchild@gmail.com> writes:
>
>> Ihor Radchenko <yantar92@gmail.com> writes:
>>
>>> "Christopher M. Miles" <numbchild@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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: subtree-level property width support --]
[-- Type: text/x-patch, Size: 2436 bytes --]

From cc322145c2248e95bc76fee7e6d33f575d93e79f Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.3: org-cycle-hook function support --]
[-- Type: text/x-patch, Size: 2997 bytes --]

From 98babf12b2d9dbc38250186e0284fad6d937e3f3 Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@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


[-- Attachment #1.4: Type: text/plain, Size: 269 bytes --]



-- 

[ 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-13  1:30 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         ` Christopher M. Miles [this message]
     [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                                   ` [PATCH 2-v2] " Christopher M. Miles
     [not found]                                   ` <6336a955.050a0220.4e72e.2b23SMTPIN_ADDED_BROKEN@mx.google.com>
2022-10-01  3:16                                     ` 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=m2leqogiz8.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).