From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id S29jNFmpUWGqFQEAgWs5BA (envelope-from ) for ; Mon, 27 Sep 2021 13:22:01 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id MK99L1mpUWEnLQAA1q6Kng (envelope-from ) for ; Mon, 27 Sep 2021 11:22:01 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 19F3F2D59 for ; Mon, 27 Sep 2021 13:22:01 +0200 (CEST) Received: from localhost ([::1]:33068 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mUohr-0007AJ-Od for larch@yhetil.org; Mon, 27 Sep 2021 07:21:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49182) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mUoh8-0007A8-HZ for emacs-orgmode@gnu.org; Mon, 27 Sep 2021 07:21:14 -0400 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]:37427) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mUoh6-00051R-JE; Mon, 27 Sep 2021 07:21:14 -0400 Received: by mail-pf1-x42c.google.com with SMTP id n23so12687195pfv.4; Mon, 27 Sep 2021 04:21:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:references:user-agent:in-reply-to :message-id:mime-version; bh=z1D+mm5SY7UhdqGMdd06zdLyp7zzKOxukKpe57OMaO0=; b=NOGeE9hIVTfWDowQi0KZqFU0wsNBnjUWJUgG8ukivMIqYHi3pA+bKADYEGSvpD7TDz 3BpDVwAw78XyQSSXxPqzpNxoDAjmk1QrLB2XsiDiUk5lKyaSOZttjlrSl6w/P105bKhV F7o//BWktQeDvuGAyyZ4/hJ5xzWADdZCLh48vUlPzX4Ice5lPz2uu+GEpy21FBA3clGe dSHlmbl92V611BwcisfZlYFNmL7fM3TN5OZRIDYpoQ5KLTPhK7mMuGVt5sONJA2qEG57 tjbiItEIwJYOjJ3qPJa+dNcVEysj7a/atZiulC5/p16FRrzvP1HAQPf18xupcihexNS1 q42A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:references:user-agent :in-reply-to:message-id:mime-version; bh=z1D+mm5SY7UhdqGMdd06zdLyp7zzKOxukKpe57OMaO0=; b=BTRxu7TOxh/9vBm9t9krwSFcNwxNBvxfkEA9IbGf1kn7WMdVUAQ9MxQanxsXo/suKk fbbbBrIOuSD4MzGhTx2lLSAOZDMIJZWU7NrfzkBWKd0MVx+wrqndM6IT/5ehy+OSFio5 rBIqOAdgDcSDC/hPXyGm4o2Qk/hslkLr+jC/buwJbvpsBIr1v14SJejw8G9fvJPXuVCw 1LIuX9Dmmr974DF9L4EcPt5a2MYE54yF/f7yuexq8/VSgICjgA/9mrh8+vK1y2e9kX3o mz4bhauR2TSS1rfpW9QH7987T5qzDZsX90Hy2bGZflhc6h6uSpxuJi4sd0xTLfd5aJS4 +HQg== X-Gm-Message-State: AOAM5338gB3q3zle7wCLGVQjKfjerqAkl8v2LD97QgCdJILp+SXxQZvC OWcH0JOJp3rSdReBgsnuslHZZNxEBec= X-Google-Smtp-Source: ABdhPJzDGi/keh0ZCYR/40swIJBAJx0VCbp9yQJgMEBqxYQFgdxHlT4xNtoGXXIJ2nJdmLcvbg++BQ== X-Received: by 2002:aa7:94aa:0:b0:43e:2cf:d326 with SMTP id a10-20020aa794aa000000b0043e02cfd326mr22793089pfl.62.1632741670097; Mon, 27 Sep 2021 04:21:10 -0700 (PDT) Received: from localhost (61-245-128-160.3df580.per.nbn.aussiebb.net. [61.245.128.160]) by smtp.gmail.com with ESMTPSA id t68sm18183854pgc.59.2021.09.27.04.21.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 04:21:09 -0700 (PDT) From: Timothy To: Bastien Subject: Re: [PATCH] Date: Mon, 27 Sep 2021 18:35:12 +0800 References: <87fstreec8.fsf@gmail.com> <877df3edm6.fsf@gmail.com> <87ee9asa6f.fsf@gnu.org> User-agent: mu4e 1.6.5; emacs 28.0.50 In-reply-to: <87ee9asa6f.fsf@gnu.org> Message-ID: <87sfxqcli6.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=tecosaur@gmail.com; helo=mail-pf1-x42c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Org Mode List Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1632741721; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=z1D+mm5SY7UhdqGMdd06zdLyp7zzKOxukKpe57OMaO0=; b=lX5CMz5EFqN4EjztLC2XL96jUnHXihhylZpdWZ8opfcaK69B47dIn2IpW5dZ5oE1fpVQ8I n7uoPwViQUlw7RSwDyFn41sQccHmO03Fhv060IGQy8EWzCIibll1qXnsnqrRb5IwPKqikD xYmHDBwkPcO4M4xf694JIXMCQ/wogACNGSmSn/vQrITbzuoiYHqAcdwCKShYwehz3MfOcD 7CMDnKdeEGgrFDs7Mpv5N0fszhrOKAaabUjHHk2QyrlkuBM9Lh8sWazaXlMRPFpydmD2cT 0j0orMbkUFejGEI8UvX9PBWExfcyPBzI5cgvcJ0I3rjLMkturiigie0mat21Tw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1632741721; a=rsa-sha256; cv=none; b=dHtARAlENSY71/TNjPuLfpL4LgCfZntTRkiO7yybmMGL3Qk7sz5k37/OPmcHqMweieHQD4 C2ge3yw2Wk2/66g3v24fC6IX9x2GnslvA0p6PM95dkEl6yS/Sm8/M8VXx2I2hOsNr15nI4 8MjUYL1/729N+zfEFmvi36Znqd2oNj+vu38ACJQ1PbCjGoGi/KnwA+zhrBs7mFF01AJAjF c/D4BHYPa/k1MGiFLorjt4eiBgy9KTmaDTduXuIi5FdKFeFbxe6qbQfHOBb3y4xW/O3fg6 nnSQNrjYEkui+/XlmHoRk/ERCQor1qGM++ApDnU+1y/vOPydfLaiowJjyLlaBg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NOGeE9hI; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Spam-Score: -2.57 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NOGeE9hI; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: 19F3F2D59 X-Spam-Score: -2.57 X-Migadu-Scanner: scn0.migadu.com X-TUID: vmARY4uiQong --=-=-= Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi Bastien, Thanks for taking a look at my patch. Bastien writes: > On a first look, it seems a bit hackish, but this is probably useful. Taking a look at the commit, I can see how it doesn=E2=80=99t look particul= arly tidy. I think this is more a function of the way that function is structured than my change though. As such, I=E2=80=99ve had a second look at the code, and giv= en how large that function is, split off the width calculation into a new function, where I=E2=80=99ve re-implemented the logic in (IMO) a cleaner way. So now, in the original function there=E2=80=99s just =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80 =E2=94=82 (let ((width (org-display-inline-image--width link)) =E2=94=94=E2=94=80=E2=94=80=E2=94=80=E2=94=80 See the attached patch for the new function `org-display-inline-image--widt= h' (to be applied on top of my previous patch). Oh, and I=E2=80=99ve also added su= pport for `:width 70%'. All the best, Timothy --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0003-org-Support-displaying-X-width-images.patch >From d9c83a962c0ce26e3d7baf2a5b7a58ba054ef275 Mon Sep 17 00:00:00 2001 From: TEC Date: Mon, 27 Sep 2021 19:16:58 +0800 Subject: [PATCH 3/3] org: Support displaying X% width images * lisp/org.el (org-display-inline-image--width): Instead of interpreting an image :width of X% as X pixels, take it as X% of the text width of the buffer. --- lisp/org.el | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index d61e74572..829df8cae 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -16647,7 +16647,8 @@ (defun org-display-inline-image--width (link) - When `org-image-actual-width' is t, the image's pixel width is used. - When `org-image-actual-width' is a number, that value will is used. - When `org-image-actual-width' is nil or a list, the first :width attribute - set (if it exists) is used to set the image width. + set (if it exists) is used to set the image width. A width of X% is + divided by 100. If no :width attribute is given and `org-image-actual-width' is a list with a number as the car, then that number is used as the default value. If the value is a float between 0 and 2, it interpreted as that proportion @@ -16667,7 +16668,11 @@ (defun org-display-inline-image--width (link) (re-search-forward attr-re par-end t))) (string-to-number (match-string 1)))) (attr-width-val - (when attr-width (string-to-number attr-width))) + (cond + ((null attr-width) nil) + ((string-match-p "\\`[0-9.]+%") + (/ (string-to-number attr-width) 100.0)) + (t (string-to-number attr-width)))) ;; Fallback to `org-image-actual-width' if no explicit width is given. (width (or attr-width-val (car org-image-actual-width)))) (if (and (floatp width) (<= 0 width 2.0)) -- 2.33.0 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0002-org-Refactor-width-in-org-display-inline-images.patch >From 1fd5e43137a34418c149240b15fd4bdc311f4fd3 Mon Sep 17 00:00:00 2001 From: TEC Date: Mon, 27 Sep 2021 18:46:03 +0800 Subject: [PATCH 2/3] org: Refactor width in `org-display-inline-images' * lisp/org.el (org-display-inline-images, org-display-inline-image--width): Extract the width determination in `org-display-inline-images' into a new function `org-display-inline-image--width' where I have taken the opportunity to refactor the width-determination code. --- lisp/org.el | 85 +++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 1a1feda78..d61e74572 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -16617,44 +16617,7 @@ (defun org-display-inline-images (&optional include-linked refresh beg end) (ignore-errors (org-attach-expand path))) (expand-file-name path)))) (when (and file (file-exists-p file)) - (let ((width - ;; Apply `org-image-actual-width' specifications. - (cond - ((eq org-image-actual-width t) nil) - ((listp 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))) + (let ((width (org-display-inline-image--width link)) (old (get-char-property-and-overlay (org-element-property :begin link) 'org-image-overlay))) @@ -16679,6 +16642,52 @@ (defun org-display-inline-images (&optional include-linked refresh beg end) (overlay-put ov 'keymap image-map)) (push ov org-inline-image-overlays)))))))))))))))) +(defun org-display-inline-image--width (link) + "Determine the display width of the image LINK, in pixels. +- When `org-image-actual-width' is t, the image's pixel width is used. +- When `org-image-actual-width' is a number, that value will is used. +- When `org-image-actual-width' is nil or a list, the first :width attribute + set (if it exists) is used to set the image width. + If no :width attribute is given and `org-image-actual-width' is a list with + a number as the car, then that number is used as the default value. + If the value is a float between 0 and 2, it interpreted as that proportion + of the text width in the buffer." + ;; Apply `org-image-actual-width' specifications. + (cond + ((eq org-image-actual-width t) nil) + ((listp org-image-actual-width) + (let* ((case-fold-search t) + (par (org-element-lineage link '(paragraph))) + (attr-re "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)") + (par-end (org-element-property :post-affiliated par)) + ;; Try to find an attribute providing a :width. + (attr-width + (when (and par (org-with-point-at + (org-element-property :begin par) + (re-search-forward attr-re par-end t))) + (string-to-number (match-string 1)))) + (attr-width-val + (when attr-width (string-to-number attr-width))) + ;; Fallback to `org-image-actual-width' if no explicit width is given. + (width (or attr-width-val (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))) + (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len) "Remove inline-display overlay if a corresponding region is modified." (let ((inhibit-modification-hooks t)) -- 2.33.0 --=-=-=--