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 IHoWCfIOgGVvZgAAkFu2QA (envelope-from ) for ; Mon, 18 Dec 2023 10:20:50 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id AFRRBfIOgGW4NgAAqHPOHw (envelope-from ) for ; Mon, 18 Dec 2023 10:20:50 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="nmzLva/4"; 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=1702891249; 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=vxnKeLgFcZ8CJv5lvomgQz8k2Q2uPKOhuA2CRKkEiaY=; b=ITMSXn3osBqapsU5AJz+Bf+LCqFnjoIMI6HaRn2fUfihywYLGN/opHJ9Jlpk7mHlFSeGri tSKg34L5auYjrdsBXvv53N2xzOmNo0zKQ8u+rKzopwTOneZcCMvVxwtevr7fwTnvlG13lf 93y4keFpYgUpQ/A64l7mDEDHltUvWuAfrKQIaXkUnuu3mX7PAjLVx6y4RJ21R347M+0J0f YU9XpStll3nwMuxdMhWg7hoBVz98/K9Ng5aiJQHGj5Y2EsozZ4ANmotGkG1lhbnT0/EPz8 h9coH+nj9hzusDQ21IHV/+yLtyqJKgMdYYi5e+6v3MZtM1926hJ2ldoTdkQoWA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="nmzLva/4"; 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-Seal: i=1; s=key1; d=yhetil.org; t=1702891249; a=rsa-sha256; cv=none; b=PMNMnyxwIRb6VXFU7PCn08vAQDdYR1Ei7N+H8LDtdLrJJbmQv+hmvAoQxkzo5MQtGx/mKX 70tMpKZzYvBEUvy2leQOkm6taMDn6GeCo2P8rjg6Luq0zfg/dSyYzz/F3eeWsM2dyJVgEP 5qKKzUy0dKhwowMtYBiYBRJeaQzIzEn3h0tFmN9C49PeSjrpvzENXq/1uBT+mvbEhaQ6EP k/Qbqrq6ebN/raM/BBtpu51tpi/kWCUdZNUIYC2+xWmoLFbnu54Vs/2+dbc7NQ/BAoCeIi OMkp1TMPbDp/LVnN/F061VHiPHjwIEgW5VyKeF5jAuD4wJRFU1O42NmG4lqFOg== 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 BCEA752B1F for ; Mon, 18 Dec 2023 10:20:49 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rF9n1-0006Mq-5n; Mon, 18 Dec 2023 04:19:55 -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 1rF9mz-0006MW-U1 for emacs-orgmode@gnu.org; Mon, 18 Dec 2023 04:19:53 -0500 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rF9my-0000I0-4a for emacs-orgmode@gnu.org; Mon, 18 Dec 2023 04:19:53 -0500 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-28b4563a03aso486475a91.0 for ; Mon, 18 Dec 2023 01:19:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702891190; x=1703495990; 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=vxnKeLgFcZ8CJv5lvomgQz8k2Q2uPKOhuA2CRKkEiaY=; b=nmzLva/4ftIcuIrV9pj4bs914dCq3EImqk2GK5SKk5v7Jjzq+90n9gpjJ5Aaknb5Ya 0I8cNY614zla72rBghV8Yk9Iagw97M2o9hXGcckMyeA3WO0mzfl/SgwDi5gKrWwodPxK I7UID70pT2Om3juvxfSfQlUkbqrwOzLuFuKm4d1o+A7H5WNwEJmPV9dzWyry07qgGh20 m+b4trXnO7mVEXr+FmdtbhOffrvUgFh2b6Mn1EgaNfqaVZhnt2FidY1eQtEUBsaD21N7 4j4p6Rphz30F/9ExbH+T+Ap4Et9VhodLMDHmKJtMsye1Tb4L9pOUWKOAHr4tJyAv0hjO RXHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702891190; x=1703495990; 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=vxnKeLgFcZ8CJv5lvomgQz8k2Q2uPKOhuA2CRKkEiaY=; b=k4Kp5gAAUtYg5kyrSZFfoikvPRI7kYlM929+bLLvJ2Du/HaFZmvp7laIY53TXVfRUe ylnufDH4SS2zDkDfwor0H6NNzyepgfDbPPfwuYLOUByzWUNHRZwca//ERq4WE8X03oiL 6OiM5lY1bfiISdJG0vrNtBGQpoxGJCBYbUSyNzhVFI1MrOB+BUlAsUoAq9O25wT2jzzj 3h6Fi4BBHGS49I0bk2e0VTwYLQyZRA3kdHCTJGBvPT5L03o4+2CX2/ikaBbMe4gGhZIe Tblj5+hdbdvL3/TafMOJN15IytJoGLf3x57p31Kli19700RwJEhEcmt+FrqcrCR0BhuW oBiA== X-Gm-Message-State: AOJu0YzHgqoonBQ/QsG1kbOMxoPexCvRTvppacFnQY7Nl9R2HwI7SOJh HyGNHJ5aAazk5K7y3y/3MP8= X-Google-Smtp-Source: AGHT+IHqqOPAIh8gUs3IVbYFNtglzLzJJfEacbou0tYCRTuVSz1x2WQsjQN33lRS/77PeRL6dgr3ww== X-Received: by 2002:a17:90b:33cf:b0:28b:730a:f122 with SMTP id lk15-20020a17090b33cf00b0028b730af122mr330377pjb.83.1702891190556; Mon, 18 Dec 2023 01:19:50 -0800 (PST) Received: from localhost ([2600:8802:5912:d100:901d:678b:108:3216]) by smtp.gmail.com with ESMTPSA id nr21-20020a17090b241500b0028b8b269aedsm1272437pjb.57.2023.12.18.01.19.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 01:19:49 -0800 (PST) From: Karthik Chikmagalur To: karthikchikmagalur@gmail.com Cc: emacs-orgmode@gnu.org Subject: Re: [PATCH] Justify/align image previews in org-mode In-Reply-To: <877clc34u3.fsf@gmail.com> Date: Mon, 18 Dec 2023 01:19:49 -0800 Message-ID: <8734vz3m16.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::1034; envelope-from=karthikchikmagalur@gmail.com; helo=mail-pj1-x1034.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-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: mx13.migadu.com X-Migadu-Spam-Score: -7.38 X-Spam-Score: -7.38 X-Migadu-Queue-Id: BCEA752B1F X-TUID: mUPSHjorUsbH --=-=-= Content-Type: text/plain Please ignore the previous patch and use this one instead. I've fixed a bug and a couple of formatting errors. Karthik --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-org-image-align.patch diff --git a/lisp/org.el b/lisp/org.el index 30a4e7aef..ad2ad2332 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -15673,6 +15673,25 @@ 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 (same as left-alignment). +center Center image previews. +right Right-align image previews." + :group 'org-appearance + :package-version '(Org . "9.7") + :type '(choice + (const :tag "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 +15826,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) @@ -15833,6 +15853,16 @@ 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 + ((or 'center 'justify) + `(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,37 @@ 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 one of the symbols center, justify or right. The +first two will cause the image preview to be centered, the last +will cause it to be right-aligned. A return value of nil implies +no special alignment -- the image preview is overlaid on the link +exactly where it appears in the buffer." + (let ((par (org-element-lineage link 'paragraph))) + ;; Only apply when image is not surrounded by paragraph text: + (when (and (= (org-element-property :begin link) + (org-element-property :contents-begin par)) + (<= (- (org-element-property :contents-end par) + (org-element-property :end link)) + 1)) ;account for trailing newline + (save-match-data + ;; Look for a valid :align keyword (left, center, justify 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 justify right))))) + (unless (eq attr-align 'left) attr-align) + ;; No image-specific keyword, check global alignment property + (when (memq org-image-align '(center justify 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) --=-=-=--