From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:5f26::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id mBxuLTKpgGWpJAEAkFu2QA (envelope-from ) for ; Mon, 18 Dec 2023 21:18:58 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id ABtEKDKpgGWAIwEAqHPOHw (envelope-from ) for ; Mon, 18 Dec 2023 21:18:58 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=SygCuw9U; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1702930738; 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:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=JtbsRCsJx8iyIdEQK/GuCK8CQX/yKdPVde+as+pJ9Ws=; b=nqeQf5apnFuPUjuayWJyUUri6OHRmf65ax/YkyQ1bAD5HpIWovPcC4xevoJC2fnbDuNIOC yvjwxj7XsDVBdGr8M0cRPTuRqUJEvfDWbG424YWogX6OcaRuaF0Lg5uGbEnsaoleE1ZsA/ Y32jKxlqgWMTv2t0xyGUJ90Tp2OfStXqdXz10BelGDcoLZLPHT/VrDZc+0yyHUCwAG5WSX 1MqieF6q3CphcsMOBResOP27F/XDnmn+HWy9cmg0rZ4Sz4QgR0tZZBQrKkk4WuW05fo4Lk jQG+Wc8PUBG/wZpwtYN+WWldeXpfPgox5ccSUZIZQ6UhSteHjtjw6aC/Z+Yzzg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1702930738; a=rsa-sha256; cv=none; b=Z8GZu/vbhzCgYcKGy8hlWKkY/8evojfKdys9d2POj0/5LVA8Z/zIQEnTGeHCIwUbCVvoin atWuAKqAIWift/YRUFKkkKvY3A8TrN3+BmOKGG5IM4wzUBN1/5+0byEombaIuh39uAHvSB M9urYlWzRrpedvswwbtpHvhccbPppaqiEAm2AxsB5RIQREkl9uW1L32JuUWNqbqNT0y5h+ kbooD/AMcExNCm9X8Fqh/XyBknQw88NkzI9TGxk5mlI81cbXhS0oYwGuihlkasw/+v8Qwx KDutE0KRDrbz3J6ZQM2Qzu6drR4q92tkePs8FXNuBYNDLQiO8F6e6ohQnx84NQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=SygCuw9U; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gmail.com 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 231EF42431 for ; Mon, 18 Dec 2023 21:18:58 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rFK40-0000mR-8R; Mon, 18 Dec 2023 15:18:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rFK3z-0000m5-0g for emacs-orgmode@gnu.org; Mon, 18 Dec 2023 15:18:07 -0500 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rFK3x-0006Mv-7Y for emacs-orgmode@gnu.org; Mon, 18 Dec 2023 15:18:06 -0500 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1d075392ff6so25589155ad.1 for ; Mon, 18 Dec 2023 12:18:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702930683; x=1703535483; darn=gnu.org; h=mime-version:message-id:date:in-reply-to:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=JtbsRCsJx8iyIdEQK/GuCK8CQX/yKdPVde+as+pJ9Ws=; b=SygCuw9UV14UAYsB5tMC4F42coc7j9nG/0XilqzKAMBuBiQS0b40EAX02F+Mdruxr3 pbEGV23hVz2kKk1Wf338c0aMnyUIK+SAQoI/O6prZBZ0RcZbDZLwKRuy1VV4bdmAd7o/ UEoZs6+uOP2C+5S3wcMxquWgGSjhr0OJ+K1IM8RVHaqMzDkn73FjkXsYpgAC60v2pRXI QotAUeaAs+YDFthQFtR/WOSNsSVyH0YjEDYRScc+DNAmVHIq0coXVtw8Cyq0913T19oK CabnAADzBgMDWco2izitq/qtHlpR6ywxj8uM+KnlTpmH8014HuWeGCGpx17Ldh2+akSa nlXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702930683; x=1703535483; h=mime-version:message-id:date:in-reply-to:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JtbsRCsJx8iyIdEQK/GuCK8CQX/yKdPVde+as+pJ9Ws=; b=C6H4PAvxFxSNZCFWr55tBR0cLYKfjFUclHA3JbRqWqblBHCRz4wz5Xru3Mh6Bi5Rb2 D+jlcMIwb2fW64tIxvrMwKoGHRbV4fs/5/cFFlalquY6uauCYs9srXEazOY1zbhq/QNb k8vW536puMftakzhUr1j3NJpTzhsBE0/MKe86DCBFaAaxYukFiGfaHdK41hpTvKq8FAK 3U0u5DztTFNcUgtfbeSH/uIXYCp/RGKA37Au9gQJqRBM1/tIMnv2kQUev7FXwEXf2LOO GLDd2AfkSApYhIpdOU50teGJYIYgh7+Ocyrwodx2w/4A9H3sYxfDXiprXQHRkMyAycPf JjTQ== X-Gm-Message-State: AOJu0YzDvQ0yu+mT9Jqvp0tKsFS8c3+zvWys8KchM8O2tcB/qjVooY5E wse5ignM/N71PU7UdlF7Kk4= X-Google-Smtp-Source: AGHT+IEjL0KzeAQhTNGIZMs0R59YKUrixabDSuAWpkBG3GKfh2rNjYgvA3Pjzm0/Ou8P1Wz+YwDplw== X-Received: by 2002:a17:902:a511:b0:1d0:6ffd:6112 with SMTP id s17-20020a170902a51100b001d06ffd6112mr15413340plq.52.1702930683415; Mon, 18 Dec 2023 12:18:03 -0800 (PST) Received: from localhost ([2600:8802:5912:d100:8b3d:8fea:533c:3a1e]) by smtp.gmail.com with ESMTPSA id p21-20020a170902ead500b001cfcf3b6de7sm3282021pld.52.2023.12.18.12.18.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 12:18:02 -0800 (PST) From: Karthik Chikmagalur To: yantar92@posteo.net Cc: emacs-orgmode@gnu.org,karthikchikmagalur@gmail.com Subject: Re: [PATCH] Justify/align image previews in org-mode In-Reply-To: <87y1drphg1.fsf@localhost> Date: Mon, 18 Dec 2023 12:18:01 -0800 Message-ID: <87v88v1czq.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::62f; envelope-from=karthikchikmagalur@gmail.com; helo=mail-pl1-x62f.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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: -9.53 X-Spam-Score: -9.53 X-Migadu-Queue-Id: 231EF42431 X-Migadu-Scanner: mx11.migadu.com X-TUID: mG+t9MXo0gAp --=-=-= Content-Type: text/plain > I can only suggest something like > > (equal (org-element-begin link) > (save-excursion > (goto-char (org-element-contents-begin paragraph)) > (skip-chars-forward "\t ") > (point))) > > (equal (org-element-end link) > (save-excursion > (goto-char (org-element-contents-end paragraph)) > (skip-chars-backward "\t ") > (point))) This should be fine, it's what we do in many places in org-latex-preview. > > - Does org-element provide a property that can help here? I tried > > :pre-blank but it was nil, I'm not sure what it does. > > :pre-blank is for blank lines before contents. Leading whitespace is > considered a part of the paragraph: > (paragraph (...) " " (link ...) "\n") Good to know. I've attached the latest version of the patch. - 'justify is no longer an option - 'left is now supported as a value of `org-image-align', although of course this is a noop. - Leading and trailing whitespace are handled correctly. The image overlay swallows up any trailing whitespace if there is nothing else on that line. - Right alignment will still fail in some odd circumstances, such as if there's an overlay with a display property to the right of the image. To see an example of this, turn on whitespace-mode and try to right-align a standalone image. I don't think it's worth handling cases like these at least for now. Karthik --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-org-image-align-v3.patch diff --git a/lisp/org.el b/lisp/org.el index 30a4e7aef..1efd2f31b 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -15673,6 +15673,26 @@ cache Display remote images, and open them in separate buffers (const :tag "Display and silently update remote images" cache)) :safe #'symbolp) +(defcustom org-image-align nil + "How to align images previewed using `org-display-inline-images'. + +Only stand-alone image links are affected by this setting. These +are links without surrounding text. + +Possible values of this option are: + +nil Insert image at specified position. +left Insert image at specified position (same as nil). +center Center image previews. +right Right-align image previews." + :group 'org-appearance + :package-version '(Org . "9.7") + :type '(choice + (const :tag "Left align (or don\\='t align) image previews" nil) + (const :tag "Center image previews" center) + (const :tag "Right align image previews" right)) + :safe #'symbolp) + (defun org--create-inline-image (file width) "Create image located at FILE, or return nil. WIDTH is the width of the image. The image may not be created @@ -15807,7 +15827,8 @@ buffer boundaries with possible narrowing." (when file (setq file (substitute-in-file-name file))) (when (and file (file-exists-p file)) (let ((width (org-display-inline-image--width link)) - (old (get-char-property-and-overlay + (align (org-image--align link)) + (old (get-char-property-and-overlay (org-element-begin link) 'org-image-overlay))) (if (and (car-safe old) refresh) @@ -15819,7 +15840,7 @@ buffer boundaries with possible narrowing." (progn (goto-char (org-element-end link)) - (skip-chars-backward " \t") + (unless (eolp) (skip-chars-backward " \t")) (point))))) ;; FIXME: See bug#59902. We cannot rely ;; on Emacs to update image if the file @@ -15833,6 +15854,15 @@ buffer boundaries with possible narrowing." (list 'org-display-inline-remove-overlay)) (when (boundp 'image-map) (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))))))) (push ov org-inline-image-overlays)))))))))))))))) (defvar visual-fill-column-width) ; Silence compiler warning @@ -15894,6 +15924,43 @@ buffer boundaries with possible narrowing." org-image-actual-width) (t nil)))) +(defun org-image--align (link) + "Determine the alignment of the image link. + +This is controlled globally by the option `org-image-align', and +per image by the value of `:align' in the affiliated keyword +`#+attr_org'. + +The result is either nil or one of the symbols left, center or +right. + +center will cause the image preview to be centered, right will +cause it to be right-aligned. A value of left or nil +implies no special alignment." + (let ((par (org-element-lineage link 'paragraph))) + ;; Only apply when image is not surrounded by paragraph text: + (when (and (= (org-element-begin link) + (save-excursion + (goto-char (org-element-contents-begin par)) + (skip-chars-forward "\t ") + (point))) ;account for leading space + ;before link + (<= (- (org-element-contents-end par) + (org-element-end link)) + 1)) ;account for trailing newline + ;at end of paragraph + (save-match-data + ;; Look for a valid :align keyword (left, center or right) + (if-let* ((attr-org (car-safe (org-element-property :attr_org par))) + ((string-match ":align[[:space:]]+\\(\\w+\\)" attr-org)) + (attr-align (car-safe + (memq (intern (match-string 1 attr-org)) + '(left center right))))) + (unless (eq attr-align 'left) attr-align) + ;; No image-specific keyword, check global alignment property + (when (memq org-image-align '(center right)) + org-image-align)))))) + (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len) "Remove inline-display overlay if a corresponding region is modified." (when (and ov after) --=-=-=--