From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:700:3204::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id oJP7EWb/gGVXmwAAkFu2QA (envelope-from ) for ; Tue, 19 Dec 2023 03:26:46 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id iPs4DGb/gGXbXQEAe85BDQ (envelope-from ) for ; Tue, 19 Dec 2023 03:26:46 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bFyFOsLp; 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=1702952806; 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=epMleiy+n/ihx+uOREFMTu5gCsRgmeeIEzFKwBpiJhA=; b=UU0wc0MyTDeAX0OwI3JpdKVcQBLzNcxwtdNT5cO6sodn/XXqUTPddU1Icq9bBTDz7XY+TH H+T0pN00YVhbWDyjGT7fXCAadXm0JfWyRAQeyV3XlT8BxHJakvko3GGVx6eQuj/A7HiwfH pqL6BaM9KiutXkDszY7+8bwYeFUDPTX3aTiGK+PBZUQOCt/rpvhCClf6isx2x+9h64SFMp T7tRMpUcRUP/4mx7NUzqyLAiFvrNTmX5lfMBzm19z9XQmw8SBzRwvwjMEZdV4yBMWQ88Ac 12aY0fl6bovfgpPxSF/YObQx8FYhAg9EpiklPfU34qk6xWrTAY4G8H7bkZKGag== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bFyFOsLp; 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=1702952806; a=rsa-sha256; cv=none; b=DvPxNAF0yphpMHyf2BPlEmmnDJFID7aEw34/j9JNAFv3uqXVbxyphvNhXN3kgFU4vQe58K lFFARbb1JA060WLwWZhm5xWGt2DmIJgwtS+Pe3WKIC2r7a7DSXtOKemh3d8VYgmmaW71KD yCMv4IpMMeuZjGVGnF9n0VcNws+s58yEfa2VG2p/9PcIJ4gP4H7yj1h8hmq90+w1Qgr8aO 9u4Ct53fCbEKlDm1fG6Y0V3Uyn/mJz3pXgu3hNRFNB18DKZHmZ9MwlRB9pltFm+OjshCg6 K/6HWSedv48wkCIczqC4B4IoBqbgDeXko18eGyIUoQRyINyDYpsULlUXKBvwnQ== 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 B04F54089F for ; Tue, 19 Dec 2023 03:26:45 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rFPnm-0005jc-Oa; Mon, 18 Dec 2023 21:25:46 -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 1rFPnk-0005jS-Lu for emacs-orgmode@gnu.org; Mon, 18 Dec 2023 21:25:45 -0500 Received: from mail-oi1-x22e.google.com ([2607:f8b0:4864:20::22e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rFPnh-0005aX-Co for emacs-orgmode@gnu.org; Mon, 18 Dec 2023 21:25:43 -0500 Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3b9f8c9307dso3796710b6e.0 for ; Mon, 18 Dec 2023 18:25:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702952737; x=1703557537; 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=epMleiy+n/ihx+uOREFMTu5gCsRgmeeIEzFKwBpiJhA=; b=bFyFOsLpemcCRgNylIokUjKmckP6JZFQA+YQk42Jgq3xLy0CrUK3tvCkNJ3LxhRUSw NU4pudsTLD4GKhBjOGsOVH24calG6FqoGUVkG0as3J2yy16sQmkIi38PJttiJpDhxv/0 nP3cdIqh8dhXT+UaSTejm1fFHwlC/JDUj3L3/yYp7UZKgRXSaokIjqpMeLG0MmgGlH3n wZZog1XU0oEsZltBFz9RfwpmGxmG1KDSN1ZAiL+bto/1vWsW6oa99qeaMzurEsUFr5FT C13uGgW5nDTVlqYR2T7pw0NZPZ0iWeAoAXp/7XuE2ktSd9b/5JHSUwXgsFJLnn/NEiag ID/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702952737; x=1703557537; 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=epMleiy+n/ihx+uOREFMTu5gCsRgmeeIEzFKwBpiJhA=; b=ZWmA76PdM7KDHXIf5im7quEaD94ts751X0+U07+aK6myhKhBszOlFnnB+NLk8i7F5s l8jKEaMPbJqtqRVZTbedClNw2+Ts4QrrVnI37teFJSMaBsxjwkDvafd37wfV4RS1SZ+O VeXe5St6HeSuftfhT/1v/dFkE6ATpBfnVA2wCxgvH9OKRQ6nX6twx+WiUv1Ps9byr4pb 92mXOl9Zip6WRLYCucxZI1nuosNWw5qAkuMFkl94L+9hXvJ6UmkshaqA9EEkjiHBXy9i L4Gc1L8IFx9QGTxoOOIzzhKKhRTzkd5nbomjbAQRrUKebA8mJEuhZl9A5R5OeB0Bx1QW jvNA== X-Gm-Message-State: AOJu0Yzxjamg2moQZRWCjVkrko5lBQZT4H21mf62CUsnjypjRS7BeJ+2 O4Ibyy13pp1PQGS8lUbwLoQ= X-Google-Smtp-Source: AGHT+IErWzhaGXqsmMTUmNnDq1EdpTDq6vHGSHi0XHfcpyKDMKBZX//+pIXbTeJ2fGWvrnr5lI2O4w== X-Received: by 2002:a05:6808:bc3:b0:3b9:e3a2:16ec with SMTP id o3-20020a0568080bc300b003b9e3a216ecmr23914340oik.35.1702952737607; Mon, 18 Dec 2023 18:25:37 -0800 (PST) Received: from localhost ([2600:8802:5912:d100:8aad:b80b:e77e:24ac]) by smtp.gmail.com with ESMTPSA id h16-20020a056a00219000b006d5d74cbcf9sm3390640pfi.58.2023.12.18.18.25.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 18:25:36 -0800 (PST) From: Karthik Chikmagalur To: karthikchikmagalur@gmail.com Cc: emacs-orgmode@gnu.org,yantar92@posteo.net Subject: Re: [PATCH] Justify/align image previews in org-mode In-Reply-To: <87v88v1czq.fsf@gmail.com> Date: Mon, 18 Dec 2023 18:25:35 -0800 Message-ID: <87r0jj0vz4.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::22e; envelope-from=karthikchikmagalur@gmail.com; helo=mail-oi1-x22e.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: B04F54089F X-TUID: xDGRHnopUBxV --=-=-= Content-Type: text/plain I've incorporated the following suggestions: - Order of precedence: + #+attr_org overrides #+attr_html and #+attr_latex + `:center t' overrides `:align ...' - Update doc/org-manual.org under the images section. - Add a checker for `:align nil' to org-lint. `:align nil' is not supported. - Unspecified behaviors: + The behavior of `:center nil` is undefined + #+attr_html vs #+attr_latex - Include a commit message in the patch. The included patch is based on the main branch. Karthik --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-org-Add-image-alignment.patch >From eb1b287c009c2f7eb83e7e31d64980ba79f44527 Mon Sep 17 00:00:00 2001 From: Karthik Chikmagalur Date: Mon, 18 Dec 2023 12:56:33 -0800 Subject: [PATCH] org: Add image alignment * lisp/org.el (org-image--align, org-image-align, org-toggle-inline-images): Add the ability to left-align, center or right-align inline image previews in the Emacs window. This is controlled globally using the new user option `org-image-align'. Alignment can be specified per image using the `#+ATTR.*' affiliated keywords. The function `org-image--align' determines the kind of alignment for its argument link. * lisp/org-lint.el (org-lint-invalid-image-alignment): Add an org-lint checker for the pattern ":align nil" in `#+ATTR.*' keywords. This alignment specification is not supported. * doc/org-manual.org: Document the new feature under the Images section. --- doc/org-manual.org | 34 +++++++++++++++++ lisp/org-lint.el | 17 +++++++++ lisp/org.el | 91 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 137 insertions(+), 5 deletions(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index 5217e647d..0df730f2b 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -11501,6 +11501,40 @@ command: and fall back on the original width if none is found. + #+vindex: org-image-align + Org mode can left-align, center or right-align the display of inline + images. This setting is controlled (globally) by ~org-image-align~. + Only standalone links, /i.e/ links with no surrounding text in their + paragraphs (except whitespace) are affected. Its value can be the + following: + - (default) nil, insert the image where the link appears in the + buffer. + - The symbol ~left~, which is the same as nil. + - The symbol ~center~, which will preview standalone links centered + in the Emacs window. + - The symbol ~right~, which will preview standalone links + right-aligned in the Emacs window. + + Inline image alignment can be specified for each link using the + =#+ATTR.*= keyword if it matches an alignment specification like: + #+begin_example + ,#+ATTR_HTML: :align center + #+end_example + Supported values for =:align= are =left=, =center= and =right=. + Inline image display can also be centered using =:center=, as in + #+begin_example + ,#+ATTR_HTML: :center t + #+end_example + Org will use the alignment specification from any =#+ATTR.*= + keyword, such as =#+ATTR_HTML= or =#+ATTR_LATEX=, but =#+ATTR_ORG= + (if present) will override the others. For instance, this link + #+begin_example + ,#+ATTR_HTML: :align right + ,#+ATTR_ORG: :align center + [[/path/to/image/file.png]] + #+end_example + will be displayed centered. + #+vindex: org-cycle-inline-images-display Inline images can also be displayed when cycling the folding state. When custom option ~org-cycle-inline-images-display~ is set, the diff --git a/lisp/org-lint.el b/lisp/org-lint.el index 0e2967b6c..84bca9f48 100644 --- a/lisp/org-lint.el +++ b/lisp/org-lint.el @@ -964,6 +964,18 @@ Use \"export %s\" instead" reports)))) reports)) +(defun org-lint-invalid-image-alignment (ast) + (org-element-map ast 'paragraph + (lambda (p) + (let ((bad-align-re ":align[[:space:]]+nil") + (keyword-string (mapconcat + (lambda (attr) + (or (car-safe (org-element-property attr p)) "")) + '(:attr_org :attr_latex :attr_html) " "))) + (when (string-match-p bad-align-re keyword-string) + (list (org-element-begin p) + "nil is not a supported value for keyword attribute \":align\"")))))) + (defun org-lint-extraneous-element-in-footnote-section (ast) (org-element-map ast 'headline (lambda (h) @@ -1390,6 +1402,11 @@ Use \"export %s\" instead" #'org-lint-invalid-keyword-syntax :trust 'low) +(org-lint-add-checker 'invalid-image-alignment + "Report unsupported align attribute for keyword" + #'org-lint-invalid-image-alignment + :trust 'low) + (org-lint-add-checker 'invalid-block "Report invalid blocks" #'org-lint-invalid-block diff --git a/lisp/org.el b/lisp/org.el index 59fe3d2d3..8a8bd977d 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -16175,6 +16175,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 @@ -16293,19 +16313,20 @@ buffer boundaries with possible narrowing." (expand-file-name path)))) (when (and file (file-exists-p file)) (let ((width (org-display-inline-image--width link)) - (old (get-char-property-and-overlay - (org-element-property :begin link) + (align (org-image--align link)) + (old (get-char-property-and-overlay + (org-element-begin link) 'org-image-overlay))) (if (and (car-safe old) refresh) (image-flush (overlay-get (cdr old) 'display)) (let ((image (org--create-inline-image file width))) (when image (let ((ov (make-overlay - (org-element-property :begin link) + (org-element-begin link) (progn (goto-char - (org-element-property :end link)) - (skip-chars-backward " \t") + (org-element-end link)) + (unless (eolp) (skip-chars-backward " \t")) (point))))) ;; FIXME: See bug#59902. We cannot rely ;; on Emacs to update image if the file @@ -16319,6 +16340,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 @@ -16380,6 +16410,57 @@ buffer boundaries with possible narrowing." org-image-actual-width) (t nil)))) +(defun org-image--align (link) + "Determine the alignment of the image link. + +In decreasing order of priority, this is controlled: +- Per image by the value of `:center' or ``:align' in the +affiliated keyword `#+attr_org'. +- By the `#+attr_html' or `#+attr_latex` keywords with valid + `:center' or `:align' values. +- Globally by the user option `org-image-align'. + +The result is either nil or one of the strings \"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 align 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 ":center t" or ":align left|center|right" + ;; attribute. + ;; + ;; An attr_org keyword has the highest priority, with + ;; attr_html/attr_latex next. Choosing between these two is + ;; unspecified. + (let ((center-re ":\\(center\\)[[:space:]]+t\\b") + (align-re ":align[[:space:]]+\\(left\\|center\\|right\\)\\b")) + (if-let ((attr-align + (cl-some + (lambda (attr-type) + (and-let* ((attr (car-safe (org-element-property attr-type par))) + ((or (string-match center-re attr) + (string-match align-re attr)))) + (match-string 1 attr))) + '(:attr_org :attr_html :attr_latex)))) + (when (member attr-align '("center" "right")) attr-align) + ;; No image-specific keyword, check global alignment property + (when (memq org-image-align '(center right)) + (symbol-name 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) -- 2.40.1 --=-=-=--