From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:1008:1e59::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id APR4AQBmf2XFdQEAkFu2QA (envelope-from ) for ; Sun, 17 Dec 2023 22:20:00 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id SMqwN/9lf2XSJQAA62LTzQ (envelope-from ) for ; Sun, 17 Dec 2023 22:20:00 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="edf+/Mum"; 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=1702847999; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=vovKlzTZ2QFoF9p6VqTo682Tp/S4Bybja8ky4b56ovY=; b=TBoqX3BL8JrNOz0UuCAWLptbIAz7l0kWp7pXnm6Q9K3YBGQ8qcJB4MWq4lJSwY96Sds0rY XOJbN39ehxeCawY7AIzm9o/hwcsKIY0FuU38LHgHcgGELMJp1balkJZWVPVG3qvI6eRjRi BOachiBjt1QPIeHaCF0wZtNUgBOaQXThpqyYetZniZ2p1/gwQf0bJ6Ty+j/Iv5/nishn9g Yfg4FojD5dQj3HPXjvGQPBOniOsuNaEpTLla0tXlaQULxmIr1gFXNL2pZeGrnx302n4iLn NooAR4C0lih97v4rTODqLJtAbgLzIFtsqQr6XeFpGOUCYyDQCO1ilAU22g0bkw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1702847999; a=rsa-sha256; cv=none; b=R9TUi6RPfV6kl8FvLPEQAuA+BNO50jRu+kPu3JS6SuLjtQLKOSd75VS9HMydShMQamScC/ IW/3mAVydhJOwZDiIVUXyKCUnq0WBPmMs1lnKLxFFdzrrVf5T+2YbP+lhmD1fnk8Kqh5TW wxEOcfYdGTn+lQKmIlabzgPmukhYzQU2W067o3IuxNB1zapuqMsGgSTERO+pUxKTzQ0QuG nsYQDmnGhRPrPR6sUq37l1Bw8omz6fyxDmmkn5DsgsrjuazTJkOHLzntR6al6sQgg1MNfH GTqC7kmcLF/f18xGXQ6Z9ZeHkBzQNoXNsb+jB+M6rS8OtJRrBcuTzJ6vYn9N6g== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="edf+/Mum"; 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 418064252E for ; Sun, 17 Dec 2023 22:19:59 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rEyXU-0005Oa-Pz; Sun, 17 Dec 2023 16:19: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 1rEyXT-0005OS-H1 for emacs-orgmode@gnu.org; Sun, 17 Dec 2023 16:19:07 -0500 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rEyXR-0008Hq-LI for emacs-orgmode@gnu.org; Sun, 17 Dec 2023 16:19:07 -0500 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1d38bedd799so19569225ad.0 for ; Sun, 17 Dec 2023 13:19:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702847943; x=1703452743; darn=gnu.org; h=mime-version:message-id:date:subject:to:from:from:to:cc:subject :date:message-id:reply-to; bh=vovKlzTZ2QFoF9p6VqTo682Tp/S4Bybja8ky4b56ovY=; b=edf+/MumaY6LSCWE4KK3w2elAYoG9iFMhmO+aXRjkdYItj8MmiMuFqNMUiaPSJQWXO 1KssnVPkt5+CkPhWEUuDjeoy5tocqLVWFQ8KIHwu+piqqWZlkFYQ6Swa4939Jq4XyOmA xvz0GOD4BjC/1qzh6hXkWBax80+28B3+ETHzjfnmzcDJb5ZYcHH57XX51XhxPglVcODK A/iYSgsiRlfeje7BXkutqic/kipjMbHkc822Otvm39wAZK6kISY3m+Ge0dMZ+I1FtXoe Dum/qK1ZlOyB5/2zCZTeo7Ik9JhMpMJc5Y7m4oSaXxcAH8IGoav1stWqZ7rnnXCJsKJi aobA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702847943; x=1703452743; h=mime-version:message-id:date:subject:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=vovKlzTZ2QFoF9p6VqTo682Tp/S4Bybja8ky4b56ovY=; b=J4USWUDlMPBBiFEBumHZTEDCfkHRVp37asFs2pGn0RpOkTJo2mcjTUkNWNja+p96Hx 3gVnipgQI6P7hf2xPzbRRRKBdofyc/CGiZtynbdAwcX50yCfrtXdJEG3/lgBI6X4pNSG 8+YvqaJ5dx8gIjfkKKL2VDncf3t8WK8SsazDxS7U2TNxbghj08tTQMxrhD8b+QiFkkW7 yrhEAale1tPEfMO0JBqqLDUyr5u479WZUlk66YyvRR3JL+n0ltLyzO3cMqe4+jF9OKht eyL2kSpQGhCBtjAqCD+hGqQ+OSzfQGK6WyIRdtmHWnm0/d9iCJb9aQFkhkfrM7oSfkx1 I0ZQ== X-Gm-Message-State: AOJu0YzUXZivoVZoN+yvs/bSWisJcMRxUpL1huNg4Mj6+inzlYPc2UCf WtkOsHuC4xXYqxIL3HxD4jwrUlNoIVQ= X-Google-Smtp-Source: AGHT+IE++UHpxrCyyTY+cja1mxeIgiTDJXeekPF2mx35lCDdR8o6iaRPpzL4C9h/GwW1adVgvQVt4w== X-Received: by 2002:a17:902:a517:b0:1d0:aa79:6ef4 with SMTP id s23-20020a170902a51700b001d0aa796ef4mr14862002plq.113.1702847943168; Sun, 17 Dec 2023 13:19:03 -0800 (PST) Received: from localhost ([2600:8802:5912:d100:901d:678b:108:3216]) by smtp.gmail.com with ESMTPSA id v21-20020a17090a899500b0028b73564d7dsm1272379pjn.24.2023.12.17.13.19.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 13:19:02 -0800 (PST) From: Karthik Chikmagalur To: emacs-orgmode@gnu.org Subject: [PATCH] Justify/align image previews in org-mode Date: Sun, 17 Dec 2023 13:19:00 -0800 Message-ID: <877clc34u3.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=karthikchikmagalur@gmail.com; helo=mail-pl1-x632.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.52 X-Spam-Score: -9.52 X-Migadu-Queue-Id: 418064252E X-Migadu-Scanner: mx11.migadu.com X-TUID: +1ydSB0m2bwb --=-=-= Content-Type: text/plain Hi, This patch allows image link previews in Org to be left-aligned, centered or right-aligned in the Emacs window. "Inline" images that are surrounded by text are unaffected. Here is an example of what this looks like: Image: https://abode.karthinks.com/share/org-image-align.png The alignment is persistent as the window (or frame) is resized: Video demo: https://abode.karthinks.com/share/org-image-align-demo.mp4 Alignment can be set globally for all image previews using the (new) user option `org-image-align'. It can be set locally for each image link using the `:align' parameter of the `#+attr_org' affiliated keyword, for example: #+attr_org: :width 400px :align center Allowed values for both settings are `center', `justify' and `right'. Center and justify have the same effect, and a value of `left', while not an error, does nothing since this is the default behavior. This patch does not update org-manual.org since it is a work in progress. Please let me know if you have suggestions. Karthik --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-org-image-align.patch diff --git a/lisp/org.el b/lisp/org.el index 30a4e7aef..2b82277a7 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 . (,width))))) + ('right `(space :align-to (- right (,width)))))))) (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) --=-=-=--