Hello again,

> I’ve prepared a patch which implements this logic

I've just noticed that I had (when x (if (floatp x) ..)) which is a bit
silly, so I've removed the unnecessary when. Here's the updated patch.

>From 2d8f151bb996e0159793b590baea50c530e3ed11 Mon Sep 17 00:00:00 2001
From: TEC <t...@tecosaur.com>
Date: Fri, 24 Sep 2021 01:39:31 +0800
Subject: [PATCH] org: Display proportional image widths

* lisp/org.el (org-display-inline-images): When the image width is given
as a float less than 2, interpret the value as that portion of the text
area width.  This works well with cases such as "#+attr_latex: :width
0.6\linewidth" as this will now be interpreted as 60% of the text area
width.  The upper bound is set to 2 not 1, as more than 100% of the text
width can be realistic, e.g. "1.2\linewidth" in LaTeX, but more than
200% seems unrealistic.
---
 lisp/org.el | 46 ++++++++++++++++++++++++++++++----------------
 1 file changed, 30 insertions(+), 16 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 66ca73d5e..1a1feda78 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16622,22 +16622,36 @@ (defun org-display-inline-images (&optional include-linked refresh beg end)
 			   (cond
 			    ((eq org-image-actual-width t) nil)
 			    ((listp org-image-actual-width)
-			     (or
-			      ;; First try to find a width among
-			      ;; attributes associated to the paragraph
-			      ;; containing link.
-			      (pcase (org-element-lineage link '(paragraph))
-				(`nil nil)
-				(p
-				 (let* ((case-fold-search t)
-					(end (org-element-property :post-affiliated p))
-					(re "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"))
-				   (when (org-with-point-at
-					     (org-element-property :begin p)
-					   (re-search-forward re end t))
-				     (string-to-number (match-string 1))))))
-			      ;; Otherwise, fall-back to provided number.
-			      (car org-image-actual-width)))
+                             (let ((width
+                                    (or
+                                     ;; First try to find a width among
+                                     ;; attributes associated to the paragraph
+                                     ;; containing link.
+                                     (pcase (org-element-lineage link '(paragraph))
+                                       (`nil nil)
+                                       (par (let* ((case-fold-search t)
+                                                   (end (org-element-property :post-affiliated par))
+                                                   (re "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"))
+                                              (when (org-with-point-at
+                                                        (org-element-property :begin par)
+                                                      (re-search-forward re end t))
+                                                (string-to-number (match-string 1)))))))
+                                    ;; Otherwise, fall-back to provided number.
+                                    (car org-image-actual-width))))
+                             (if (and (floatp width) (<= 0 width 2.0))
+                                 ;; A float in [0,2] should be interpereted as this portion of
+                                 ;; the text width in the window.  This works well with cases like
+                                 ;; #+attr_latex: :width 0.X\{line,page,column,etc.}width,
+                                 ;; as the "0.X" is pulled out as a float.  We use 2 as the upper
+                                 ;; bound as cases such as 1.2\linewidth are feasible.
+                                 (round (* width
+                                           (window-pixel-width)
+                                           (/ (or (and (bound-and-true-p visual-fill-column-mode)
+                                                       (or visual-fill-column-width auto-fill-function))
+                                                  (when auto-fill-function fill-column)
+                                                  (window-text-width))
+                                              (float (window-total-width)))))
+                               width))
 			    ((numberp org-image-actual-width)
 			     org-image-actual-width)
 			    (t nil)))
-- 
2.33.0

Reply via email to