emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] add a function to only refresh inline images under current headline instead of global buffer
@ 2023-05-15  3:28 Christopher M. Miles
  2023-05-15 11:08 ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Christopher M. Miles @ 2023-05-15  3:28 UTC (permalink / raw)
  To: Org mode

[-- Attachment #1: Type: text/plain, Size: 2369 bytes --]


I found a lot of third-part Emacs packages refresh Org source block image result using the API function like this:

#+begin_src emacs-lisp
;; Automatically refresh inline images.
  (add-hook 'org-babel-after-execute-hook
            (defun ob-dall-e--refresh-inline-images ()
              (when org-inline-image-overlays
                (org-redisplay-inline-images))))
#+end_src

The `org-redisplay-inline-images' will refresh whole buffer inline
images. When the buffer is a big Org file, and not all inline images are
display already by default (still image file links under fold status).
Invoking `org-redisplay-inline-images' will cause Emacs suspend a long
time.

So I suggest to add an variant local function of
`org-redisplay-inline-images' which named
`org-redisplay-inline-images-under-headline' that only redisplay inline
images under current headline to solve the issue.

Here is the diff code prototype, Ihor, can you review it? If it's ok, I
will send patch update then.

#+begin_src diff
 (defun org-redisplay-inline-images ()
-  "Assure display of inline images and refresh them."
+  "Assure display of global all inline images in buffer and refresh them.
+
+NOTE: This function will refresh whole buffer inline images, if
+you only want to refresh inline images under headline, suggest to
+use `org-redisplay-inline-images-under-headline' in your hook or advice."
   (interactive)
   (org-toggle-inline-images)
   (unless org-inline-image-overlays
     (org-toggle-inline-images)))
 
+(defun org-redisplay-inline-images-under-headline ()
+  "Assure display of images under current headline and refresh them.
+This function is the suggested to be used in hook or advice."
+  (interactive)
+  (org-with-wide-buffer
+   (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'.
+   (org-display-inline-images
+    nil nil
+    (point-min) (progn (org-next-visible-heading 1) (point)))))
+
#+end_src

-- 

[ 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 --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] add a function to only refresh inline images under current headline instead of global buffer
  2023-05-15  3:28 [PATCH] add a function to only refresh inline images under current headline instead of global buffer Christopher M. Miles
@ 2023-05-15 11:08 ` Ihor Radchenko
  2023-05-15 13:01   ` Christopher M. Miles
  2023-05-15 14:00   ` [PATCH v2] " Christopher M. Miles
  0 siblings, 2 replies; 6+ messages in thread
From: Ihor Radchenko @ 2023-05-15 11:08 UTC (permalink / raw)
  To: numbchild; +Cc: Org mode

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

> The `org-redisplay-inline-images' will refresh whole buffer inline
> images. When the buffer is a big Org file, and not all inline images are
> display already by default (still image file links under fold status).
> Invoking `org-redisplay-inline-images' will cause Emacs suspend a long
> time.
>
> So I suggest to add an variant local function of
> `org-redisplay-inline-images' which named
> `org-redisplay-inline-images-under-headline' that only redisplay inline
> images under current headline to solve the issue.
>
> Here is the diff code prototype, Ihor, can you review it? If it's ok, I
> will send patch update then.

I'd prefer something more closely resembling `org-latex-preview'
approach with prefix arguments:

    Toggle preview of the LaTeX fragment at point.
    
    If the cursor is on a LaTeX fragment, create the image and
    overlay it over the source code, if there is none.  Remove it
    otherwise.  If there is no fragment at point, display images for
    all fragments in the current section.  With an active region,
    display images for all fragments in the region.
    
    With a C-u prefix argument ARG, clear images for all fragments
    in the current section.
    
    With a C-u C-u prefix argument ARG, display image for all
    fragments in the buffer.
    
    With a C-u C-u C-u prefix argument ARG, clear image for all
    fragments in the buffer.

`org-toggle-inline-images' should also be changed.

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] add a function to only refresh inline images under current headline instead of global buffer
  2023-05-15 11:08 ` Ihor Radchenko
@ 2023-05-15 13:01   ` Christopher M. Miles
  2023-05-15 14:00   ` [PATCH v2] " Christopher M. Miles
  1 sibling, 0 replies; 6+ messages in thread
From: Christopher M. Miles @ 2023-05-15 13:01 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: numbchild, Org mode

[-- Attachment #1: Type: text/plain, Size: 2019 bytes --]


Ihor Radchenko <yantar92@posteo.net> writes:

> "Christopher M. Miles" <numbchild@gmail.com> writes:
>
>> The `org-redisplay-inline-images' will refresh whole buffer inline
>> images. When the buffer is a big Org file, and not all inline images are
>> display already by default (still image file links under fold status).
>> Invoking `org-redisplay-inline-images' will cause Emacs suspend a long
>> time.
>>
>> So I suggest to add an variant local function of
>> `org-redisplay-inline-images' which named
>> `org-redisplay-inline-images-under-headline' that only redisplay inline
>> images under current headline to solve the issue.
>>
>> Here is the diff code prototype, Ihor, can you review it? If it's ok, I
>> will send patch update then.
>
> I'd prefer something more closely resembling `org-latex-preview'
> approach with prefix arguments:
>
>     Toggle preview of the LaTeX fragment at point.
>     
>     If the cursor is on a LaTeX fragment, create the image and
>     overlay it over the source code, if there is none.  Remove it
>     otherwise.  If there is no fragment at point, display images for
>     all fragments in the current section.  With an active region,
>     display images for all fragments in the region.
>     
>     With a C-u prefix argument ARG, clear images for all fragments
>     in the current section.
>     
>     With a C-u C-u prefix argument ARG, display image for all
>     fragments in the buffer.
>     
>     With a C-u C-u C-u prefix argument ARG, clear image for all
>     fragments in the buffer.
>
> `org-toggle-inline-images' should also be changed.

I agree this solution. I will check out latex fragment preview source
code whether I can implement this.

-- 

[ 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 --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] Re: [PATCH] add a function to only refresh inline images under current headline instead of global buffer
  2023-05-15 11:08 ` Ihor Radchenko
  2023-05-15 13:01   ` Christopher M. Miles
@ 2023-05-15 14:00   ` Christopher M. Miles
  2023-05-16  9:17     ` Ihor Radchenko
  1 sibling, 1 reply; 6+ messages in thread
From: Christopher M. Miles @ 2023-05-15 14:00 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: numbchild, Org mode

[-- Attachment #1: Type: text/plain, Size: 5505 bytes --]


Ihor Radchenko <yantar92@posteo.net> writes:

> "Christopher M. Miles" <numbchild@gmail.com> writes:
>
>> The `org-redisplay-inline-images' will refresh whole buffer inline
>> images. When the buffer is a big Org file, and not all inline images are
>> display already by default (still image file links under fold status).
>> Invoking `org-redisplay-inline-images' will cause Emacs suspend a long
>> time.
>>
>> So I suggest to add an variant local function of
>> `org-redisplay-inline-images' which named
>> `org-redisplay-inline-images-under-headline' that only redisplay inline
>> images under current headline to solve the issue.
>>
>> Here is the diff code prototype, Ihor, can you review it? If it's ok, I
>> will send patch update then.
>
> I'd prefer something more closely resembling `org-latex-preview'
> approach with prefix arguments:
>
>     Toggle preview of the LaTeX fragment at point.
>     
>     If the cursor is on a LaTeX fragment, create the image and
>     overlay it over the source code, if there is none.  Remove it
>     otherwise.  If there is no fragment at point, display images for
>     all fragments in the current section.  With an active region,
>     display images for all fragments in the region.
>     
>     With a C-u prefix argument ARG, clear images for all fragments
>     in the current section.
>     
>     With a C-u C-u prefix argument ARG, display image for all
>     fragments in the buffer.
>     
>     With a C-u C-u C-u prefix argument ARG, clear image for all
>     fragments in the buffer.
>
> `org-toggle-inline-images' should also be changed.

Here is the source code of `org-toggle-inline-images'. I implement this
by reference `org-latex-preview'. I have not write testing, but only
manually tested with Edebug and on actual Org buffer displaying and
disable inline images. The command works fine. Ihor, can you review the code?

#+begin_src emacs-lisp
(defun org-toggle-inline-images (&optional arg include-linked beg end)
  "Toggle the display of inline images at point.
INCLUDE-LINKED is passed to `org-display-inline-images'.

If cursor is on an inline image link, display the inline image.
If there is none, remove it otherwise.
If there is no inline image link at point, display all inline images in the current section.
With an active region, display inline images in the region.

With a `\\[universal-argument]' prefix argument ARG, clear inline
images in the current section.

With a `\\[universal-argument] \\[universal-argument]' prefix
 argument ARG, display all inline images in the buffer.

With a `\\[universal-argument] \\[universal-argument] \
\\[universal-argument]' prefix argument ARG, clear all inline
images in the buffer."
  (interactive "P")
  (cond
   ((not (display-graphic-p)) nil)
   ;; Clear whole buffer inline images.
   ((equal arg '(64))
    (org-remove-inline-images (point-min) (point-max))
    (message "Inline images preview disabled in buffer."))
   ;; Display whole buffer inline images.
   ((equal arg '(16))
    (message "Displaying all inline images in buffer...")
    (org-display-inline-images include-linked nil (point-min) (point-max))
    (message "Displaying all inline images in buffer... done."))
   ;; Clear current section.
   ((equal arg '(4))
    (let* ((beg (if (use-region-p)
                    (region-beginning)
                  (if (org-before-first-heading-p) (point-min)
                    (save-excursion
	              (org-with-limited-levels (org-back-to-heading t) (point))))))
           (end (if (use-region-p)
                    (region-end)
                  (org-with-limited-levels (org-entry-end-position))))
           (inline-images (org--inline-image-overlays beg end)))
      (org-remove-inline-images beg end)
      (message "%d inline images display removed." (length inline-images))))
   ;; Display region selected inline images.
   ((use-region-p)
    (message "Displaying inline images in region...")
    (org-display-inline-images include-linked t (region-beginning) (region-end))
    (message "Displaying inline images in region... done."))
   ;; Toggle display of inline image link at point.
   ((let ((context (org-element-context)))
      (and (memq (org-element-type context) '(link))
	   (let ((beg (org-element-property :begin context))
		 (end (org-element-property :end context)))
	     (if (org--inline-image-overlays beg end)
                 (progn
                   (org-remove-inline-images beg end)
	           (message "Display inline image at point removed."))
	       (org-display-inline-images include-linked t beg end)
	       (message "Displaying inline image at point ... done."))
	     t))))
   ;; Display inline images under current section.
   (t
    (let ((beg (if (org-before-first-heading-p) (point-min)
		 (save-excursion
		   (org-with-limited-levels (org-back-to-heading t) (point)))))
	  (end (org-with-limited-levels (org-entry-end-position))))
      (message "Displaying inline images in section...")
      (org-display-inline-images include-linked t beg end)
      (message "Displaying inline images in section... done.")))))
#+end_src

-- 

[ 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 --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Re: [PATCH] add a function to only refresh inline images under current headline instead of global buffer
  2023-05-15 14:00   ` [PATCH v2] " Christopher M. Miles
@ 2023-05-16  9:17     ` Ihor Radchenko
  2023-05-16 12:18       ` Christopher M. Miles
  0 siblings, 1 reply; 6+ messages in thread
From: Ihor Radchenko @ 2023-05-16  9:17 UTC (permalink / raw)
  To: Christopher M. Miles; +Cc: Org mode

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

>> I'd prefer something more closely resembling `org-latex-preview'
>> approach with prefix arguments:
>> ...
>> `org-toggle-inline-images' should also be changed.
>
> Here is the source code of `org-toggle-inline-images'. I implement this
> by reference `org-latex-preview'. I have not write testing, but only
> manually tested with Edebug and on actual Org buffer displaying and
> disable inline images. The command works fine. Ihor, can you review the code?

Unlike `org-latex-preview', here we need to (1) respect active region;
(2) keep the backward compatibility for INCLUDE-LINKED.

For (1), for example, it would make sense to respect region when prefix
argument is C-u and clear images only there. For (2), we need to at
least allow toggling images with description using some prefix argument
(previously, any prefix argument would do).

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Re: [PATCH] add a function to only refresh inline images under current headline instead of global buffer
  2023-05-16  9:17     ` Ihor Radchenko
@ 2023-05-16 12:18       ` Christopher M. Miles
  0 siblings, 0 replies; 6+ messages in thread
From: Christopher M. Miles @ 2023-05-16 12:18 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Christopher M. Miles, Org mode

[-- Attachment #1: Type: text/plain, Size: 3125 bytes --]


Ihor Radchenko <yantar92@posteo.net> writes:

> "Christopher M. Miles" <numbchild@gmail.com> writes:
>
>>> I'd prefer something more closely resembling `org-latex-preview'
>>> approach with prefix arguments:
>>> ...
>>> `org-toggle-inline-images' should also be changed.
>>
>> Here is the source code of `org-toggle-inline-images'. I implement this
>> by reference `org-latex-preview'. I have not write testing, but only
>> manually tested with Edebug and on actual Org buffer displaying and
>> disable inline images. The command works fine. Ihor, can you review the code?
>
> Unlike `org-latex-preview', here we need to (1) respect active region;
> (2) keep the backward compatibility for INCLUDE-LINKED.
>
> For (1), for example, it would make sense to respect region when prefix
> argument is C-u and clear images only there. For (2), we need to at
> least allow toggling images with description using some prefix argument
> (previously, any prefix argument would do).

I indeed implemented all same behavior like `org-latex-preview' in new
`org-toggle-inline-images'.

For (1), org-latex-preview has a `cond' logic to toggle preview in
region. Here is the code from `org-latex-preview'.

((use-region-p)
    (message "Creating LaTeX previews in region...")
    (org--latex-preview-region (region-beginning) (region-end))
    (message "Creating LaTeX previews in region... done."))

+

;; Clear current section.
   ((equal arg '(4))
    (org-clear-latex-preview
     (if (use-region-p)
         (region-beginning)
       (if (org-before-first-heading-p) (point-min)
         (save-excursion
	   (org-with-limited-levels (org-back-to-heading t) (point)))))
     (if (use-region-p)
         (region-end)
       (org-with-limited-levels (org-entry-end-position)))))

And I also implement the toggle inline images in region logic in
`org-toggle-inline-images' as bellowing:

;; Display region selected inline images.
   ((use-region-p)
    (message "Displaying inline images in region...")
    (org-display-inline-images include-linked t (region-beginning) (region-end))
    (message "Displaying inline images in region... done."))

+

;; Clear current section.
   ((equal arg '(4))
    (org-clear-latex-preview
     (if (use-region-p)
         (region-beginning)
       (if (org-before-first-heading-p) (point-min)
         (save-excursion
	   (org-with-limited-levels (org-back-to-heading t) (point)))))
     (if (use-region-p)
         (region-end)
       (org-with-limited-levels (org-entry-end-position)))))

------------------------------------------------------------------------

For (2), It's working. The code passed the parameter `include-linked' to
`org-display-inline-images'. So they works in any case of [C-u] prefix.

Maybe you want to eval the patch code to test.

-- 

[ 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 --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-05-16 12:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-15  3:28 [PATCH] add a function to only refresh inline images under current headline instead of global buffer Christopher M. Miles
2023-05-15 11:08 ` Ihor Radchenko
2023-05-15 13:01   ` Christopher M. Miles
2023-05-15 14:00   ` [PATCH v2] " Christopher M. Miles
2023-05-16  9:17     ` Ihor Radchenko
2023-05-16 12:18       ` Christopher M. Miles

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