From 51d94bf20b043be635af3719464dccd0cc589f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Bidar?= 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 | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/lisp/ol.el b/lisp/ol.el index 43fac7433..0429e257c 100644 --- a/lisp/ol.el +++ b/lisp/ol.el @@ -2178,23 +2178,39 @@ (defun org-link-preview-file (ov path link) This is intended to be used as the `:preview' link property of file links, see `org-link-parameters'." - (when (display-graphic-p) - (require 'image) (when-let* ((file-full (expand-file-name path)) (file (substitute-in-file-name file-full)) ((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 + (org-link-preview-image-data ov file link))) + +(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 ALIGN and/or WIDTH are not given, they are derived from LINK object +\(ast node\). +If LINK is nil, 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) + (when (display-graphic-p) + (require 'image) + (when-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) - (overlay-put ov 'display image) - (overlay-put ov 'face 'default) - (overlay-put ov 'keymap image-map) + (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 @@ -2202,9 +2218,9 @@ (defun org-link-preview-file (ov path link) " " 'face 'default 'display (pcase align - ("center" `(space :align-to (- center (0.5 . ,image)))) - ("right" `(space :align-to (- right ,image))))))) - t))))) + ("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 _) -- 2.45.2