emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Link preview generation with new link preview property


From: Björn Bidar
Subject: Re: Link preview generation with new link preview property
Date: Sun, 19 Jan 2025 17:08:26 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

Ihor Radchenko <yantar92@posteo.net> writes:

> Björn Bidar <bjorn.bidar@thaodan.de> writes:
>
>>> IMHO, it would be best to pass image file name/raw image data to
>>> `org-link-preview-image-data', not an image object and call
>>> `org--create-inline-image' from inside `org-link-preview-image-data'.
>>> That way, we can automatically obey Org customization wrt image
>>> alignment, image width, and image max width.
>>
>> For that case using org-link-preview-file should be sufficient? 
>
> Sorry, I am lost.
> AFAIU, your original motivation was:
>
>    >> Would it be possible to also handle image data in the function or
>    >> refactor the org-link-preview-file function in a way that the geometry
>    >> handling is done in a helper function which can be reused by other
>    >> handlers.
>
> Did it change?

It didn't but if the handler which would the uses a raw (temporary) file it 
could have
used org-link-preview-file, which essentially meant that the handler
which uses org-link-preview file is a proxy for it.

The thing that confused me, but I think I wasn't aware of not getting it
yesterday was that org--create-inline-image creates the actual image
object. I was thinking that the image object would have been created in the
handler and then passed to the helper function.

I think what also confused me that org--create-inline-image talks about
files not image-data. Do I understand correctly that for image-data the
handling of files in line 1035 is just skipped?

Another that I just came to mind what is better for such a use case the
use of a buffer or a variable to store the image data? Url (and eww) for example
usually uses buffers.

I updated my patch with the recommended changes and updated the
docstring accordingly.

>From ee0d30fac48d1f3ee86a653fe537badde5e3174d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Bidar?= <bjorn.bidar@thaodan.de>
Date: Fri, 20 Dec 2024 01:00:46 +0200
Subject: [PATCH] org-link: Split link preview into two functions

* lisp/ol.el (org-link-preview-file,
org-link-preview-image-data): Split up the actual preview part into
a separate function.  The new preview function can be called by
:preview handler to display the raw image according to the correct
alignment and with for the link to be previewed. Passing the link can
be skipped if alignment and with are given.
---
 lisp/ol.el | 57 ++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 38 insertions(+), 19 deletions(-)

diff --git a/lisp/ol.el b/lisp/ol.el
index 032610bad..ebfe926a1 100644
--- a/lisp/ol.el
+++ b/lisp/ol.el
@@ -2177,27 +2177,46 @@ (defun org-link-preview-file (ov path link)
                 ((string-match-p (image-file-name-regexp) file))
                 ((file-exists-p file)))
       (let* ((width (org-display-inline-image--width link))
-            (align (org-image--align link))
-             (image (org--create-inline-image file width)))
-        (when image            ; Add image to overlay
-         ;; See bug#59902.  We cannot rely
-          ;; on Emacs to update image if the file
-          ;; has changed.
-          (image-flush image)
-         (overlay-put ov 'display image)
-         (overlay-put ov 'face 'default)
-         (overlay-put ov 'keymap image-map)
-          (when align
-            (overlay-put
-             ov 'before-string
-             (propertize
-              " " 'face 'default
-              'display
-              (pcase align
-                ("center" `(space :align-to (- center (0.5 . ,image))))
-                ("right"  `(space :align-to (- right ,image)))))))
+             (image-data (org--create-inline-image file width)))
+        (when image-data
+         (org-link-preview-image-data ov image-data link)
           t)))))
 
+(defun org-link-preview-image-data (ov image-data &optional link align width)
+  "Display raw image data IMAGE-DATA in overlay OV for LINK.
+
+If not given ALIGN and WIDTH is derived from LINK.
+If LINK was not passed ALIGN and WIDTH have to be given.
+
+An image object is created from IMAGE-DATA to be displayed in overlay.
+
+This intended to be used by functions which provide the :preview link property
+of links such as in `org-link-preview-file'"
+  (if (or (and align width)
+          link)
+      (let* ((align (or align
+                        (org-image--align link)))
+             (width (or width
+                        (org-display-inline-image--width link)))
+             (image-object (org--create-inline-image image-data width)))
+        ;; See bug#59902.  We cannot rely
+        ;; on Emacs to update image if the file
+        ;; has changed.
+        (image-flush image-object)
+        (overlay-put ov 'display image-object)
+        (overlay-put ov 'face 'default)
+        (overlay-put ov 'keymap image-map)
+        (when align
+          (overlay-put
+           ov 'before-string
+           (propertize
+            " " 'face 'default
+            'display
+            (pcase align
+              ("center" `(space :align-to (- center (0.5 . ,image-data))))
+              ("right"  `(space :align-to (- right ,image-data))))))))
+    (error "Either align and width or link have to be passed")))
+
 ;;;; "help" link type
 (defun org-link--open-help (path _)
   "Open a \"help\" type link.
-- 
2.45.2


reply via email to

[Prev in Thread] Current Thread [Next in Thread]