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 GPt4GQDqgWXlPAAAkFu2QA (envelope-from ) for ; Tue, 19 Dec 2023 20:07:44 +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 aA18EwDqgWWzbQAAe85BDQ (envelope-from ) for ; Tue, 19 Dec 2023 20:07:44 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BlJIb3sG; 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=1703012864; 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=jkwMZG7TfGer2KACy5zXtp84NBe1TefuM2h2IrKdUaA=; b=NYoWkFsxsLj/nKNigsJp0uhX8rc3ik6JCTwGoILYZC1A4jYJdMYVSNNTjKjdJtwQcrgFMF b0drIenkxvkP5UdFybisHyfjEaxnex37WkJNfS0iw9aVrXX953ARoenxfEtS3uypRxTuMF /YrgXz4qbfzKgAqksrncPqbJzZ8cD6FabgXrK/4vnHtaT4+orhb1r9GAko3rF95GZif36o jhJ2eNFQMvMx2J9eXm0/C5m6kpyD9dnUGZMazWHuC44f1qtm7GJZBFYGlcJ22LeULpL9v6 1613ObVaJ88WaIW9K3VM9L2Qz3+Wgl0kGj2h/2wc6VPRaOZ1ay9k6iBqRoNQog== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BlJIb3sG; 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=1703012864; a=rsa-sha256; cv=none; b=uGVAh83d2yCtvDFm7LiDnUzdhU396QpCDLKmVxjICFnSPsuwW86kYfWrDXDFRTI1V0KzyS M+Mz8zasPMsco7Zq1d9nbVM/k9cZV3V/RPl5Yd7BrvIaiQjWOctyENCUlnf2s4CgOkOx1q yIZBjbQ8pMmg7Le0lAofRC39a4qLGFnxlYriMGF/Qy6lCfMn23ABSM+Iki/OoFyUnt4PUc IswlG4wlrj/YHTTcWaKpWFJvHVgu1Y4k8FughlNlZUmf/EEY8w+tHvhtEzOBW/d1TQksDr 4AgtDBawVXS1P9o0FQzVqbOpEbr5Ark5PEfsgRJj25kluoC221QFAnJnUIYaCw== 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 052C9465BE for ; Tue, 19 Dec 2023 20:07:44 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rFfPl-0005sy-3m; Tue, 19 Dec 2023 14:06:01 -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 1rFfPO-0005Rs-Tm for emacs-orgmode@gnu.org; Tue, 19 Dec 2023 14:05:44 -0500 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rFfPI-0000Co-Fl for emacs-orgmode@gnu.org; Tue, 19 Dec 2023 14:05:38 -0500 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-6d7f1109abcso1622391b3a.3 for ; Tue, 19 Dec 2023 11:05:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703012731; x=1703617531; darn=gnu.org; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=jkwMZG7TfGer2KACy5zXtp84NBe1TefuM2h2IrKdUaA=; b=BlJIb3sGGuVhhkjGUiTbhrY1djGbCQQpiaKagtTeGCaRP4f04Vr6dLFd9DYsp33CYD 6DWQAenld4M3zgE8bbHzZ9Y4Ml6Y4s5aE75NjuTHdlRyWTUgekBe9fUsQCawDKjfsHgf jIcvgTI8S7dTWuchhqhIx8hGpRPLc1cUPmayS1XHF8dxoD4Mp2qrUENFTkf3VzyxxmkF Dx4H9suAvdXDWfPy0NutM3KHJDFhyw9qS6QqUEu3SQQWB+ABSVTdAbgjrpfPQ/3F/1/Q jRuknpHMp7S3teVG6fWQUmKHJ986N/juW9eLfd8crGsB+nJzr6+zfl5XxW/9knSfgk5M B/og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703012731; x=1703617531; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jkwMZG7TfGer2KACy5zXtp84NBe1TefuM2h2IrKdUaA=; b=mNeSQZ4UOOiQ8MvaVlzVTfTCkKPSqRlOSoAgqZYk/nBtBcjrvZR07YF3GlxX0ifozT HitG2cTt/O1K8evRSWytwxYbFgER96qbks+JGQfu83FCwrQAXMrE8fsXNs9E48ob/9dR WKS5rcWuKvM6aVKdUivkEHyclU1K342fJ2AEgefi5TlWOv/0wnB1CB6Ol68yg0sj1lYF DbMJnu/Fz5pu1fiK6UYej8LVP+KXjqa7j5/bpGykS6rqAZ+yyHy17Cn7Gx1kfg8HJFuM mN3KMPk7TrGkjhJA/5+6JhBVQzMvVsPk28HgPNW327HaRMFqhIhhGkgZ/qv/AxBmKQKb +kOA== X-Gm-Message-State: AOJu0YxPbcXBGMh9Svdrnt+GfwOEfskBA7hIKvJizqISXgJZQlYhbROV NO3ugCungFU5Abf184Pn3/IPf1rMHeA= X-Google-Smtp-Source: AGHT+IFH0vrhlEtkKuTW513u6Xs4FQnK4h+GxJl6FIZTB+p0qdEU546qbu1wvTEmck9EPnUK+pHYoA== X-Received: by 2002:a17:902:6544:b0:1d2:dd72:3b30 with SMTP id d4-20020a170902654400b001d2dd723b30mr7717524pln.98.1703012730910; Tue, 19 Dec 2023 11:05:30 -0800 (PST) Received: from localhost ([2600:8802:5912:d100:103a:fe04:7e:4238]) by smtp.gmail.com with ESMTPSA id y22-20020a1709027c9600b001cff353696asm21340663pll.302.2023.12.19.11.05.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 11:05:29 -0800 (PST) From: Karthik Chikmagalur To: Ihor Radchenko Cc: emacs-orgmode@gnu.org Subject: Re: [PATCH] Justify/align image previews in org-mode In-Reply-To: <87o7empgbq.fsf@localhost> References: <87r0jj0vz4.fsf@gmail.com> <87o7empgbq.fsf@localhost> Date: Tue, 19 Dec 2023 11:05:23 -0800 Message-ID: <87o7em1098.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::42d; envelope-from=karthikchikmagalur@gmail.com; helo=mail-pf1-x42d.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.39 X-Spam-Score: -7.39 X-Migadu-Queue-Id: 052C9465BE X-TUID: nCpjUXClQV8B --=-=-= 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 ...' > > Why only html and latex? I think that it will be more consistent to > follow what we do in `org-display-inline-image--width' - any #+attr_* > attribute. And apart from html/latex, we have > beamer/hugo/koma-letter/what not is implemented or will be implemented > deriving from html/latex backend or with support of :center attribute. Changed implementation to support any #+attr_.* keyword. >> - Add a checker for `:align nil' to org-lint. `:align nil' is not >> supported. > > I meant :align being not supported in #+attr_org. Not just nil. org-lint now complains if anything other than `:align left|center|right' or `:center t' are specified in #+attr_org. >> + #+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 > > Maybe "standalone images"? > Also, please avoid i.e - this is a general Emacs documentation guideline > to avoid specialized language like i.e., e.g., and iff. Changed text, removed specialized language. >> + paragraphs (except whitespace) are affected. Its value can be the >> + following: >> + - (default) nil, insert the image where the link appears in the >> + buffer. > > Why not simply setting the default to 'left and not having nil at all? The original idea was that `left' and `nil' do the same thing, so it should be fine to leave it at `nil'. I changed the user option to default to `left', and `nil' has been removed as an available option. (It is still not an error to set it to `nil'.) >> + - 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. > > "standalone" links is redundant here - you already mentioned that Fixed. >> + 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=. > > I think that we do not need to specify the supported values here - they > are the same as for `org-image-align' just above. Removed. >> + Inline image display can also be centered using =:center=, as in > > I'd explain that inline image adjustment is also taken from :center > attribute supported by some export backends (like HTML, LaTeX, and > Beamer), when #+attr_org is not set. Tweaked the description of :center. >> + #+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. > > ... "but exported right-aligned to HTML" Added. >> +(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) " "))) > > :align nil is perfectly valid for HTML. > I thought to warn only about using anything but :align left/right/center > in #+attr_org. And about :center in #+attr_org. Yes, only the value of #+attr_org is checked now. Patch v2 attached. Karthik --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-org-Add-image-alignment-v2.patch >From 7176f43282749c06daf2756360633cc47d79b63c 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 to catch invalid ":align" and ":center" attributes in `#+attr_org' keywords. * doc/org-manual.org: Document the new feature under the Images section. --- doc/org-manual.org | 33 ++++++++++++++++ lisp/org-lint.el | 35 +++++++++++++++++ lisp/org.el | 94 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 158 insertions(+), 4 deletions(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index 5217e647d..186f08c51 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -11501,6 +11501,39 @@ 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 images are affected, corresponding to links with no + surrounding text in their paragraph except for whitespace. Its + value can be the following: + - (default) The symbol ~left~, which inserts the image where the + link appears in the buffer. + - The symbol ~center~, which will preview links centered in the + Emacs window. + - The symbol ~right~, which will preview 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 + 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 in Emacs but exported right-aligned to + HTML. + + When =#+ATTR_ORG= is not set, inline image alignment is also read + from the =:center= attribute supported by some export backends (like + HTML, LaTeX and Beamer.) + #+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..3442d3571 100644 --- a/lisp/org-lint.el +++ b/lisp/org-lint.el @@ -964,6 +964,36 @@ Use \"export %s\" instead" reports)))) reports)) +(defun org-lint-invalid-image-alignment (ast) + (apply + #'nconc + (org-element-map ast 'paragraph + (lambda (p) + (let ((center-re ":center[[:space:]]+\\(\\S-+\\)") + (align-re ":align[[:space:]]+\\(\\S-+\\)") + (keyword-string + (car-safe (org-element-property :attr_org p))) + reports) + (when keyword-string + (when (and (string-match align-re keyword-string) + (not (member (match-string 1 keyword-string) + '("left" "center" "right")))) + (push + (list (org-element-begin p) + (format + "\"%s\" not a supported value for #+ATTR_ORG keyword attribute \":align\"." + (match-string 1 keyword-string))) + reports)) + (when (and (string-match center-re keyword-string) + (not (equal (match-string 1 keyword-string) "t"))) + (push + (list (org-element-begin p) + (format + "\"%s\" not a supported value for #+ATTR_ORG keyword attribute \":center\"." + (match-string 1 keyword-string))) + reports))) + reports))))) + (defun org-lint-extraneous-element-in-footnote-section (ast) (org-element-map ast 'headline (lambda (h) @@ -1390,6 +1420,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..24e80970f 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -16175,6 +16175,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 'left + "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: + +left Insert image at specified position. +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" left) + (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,8 +16312,9 @@ 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)) @@ -16304,8 +16324,8 @@ buffer boundaries with possible narrowing." (org-element-property :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 +16339,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 +16409,63 @@ 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 + ;; any attr.* next. Choosing between these is + ;; unspecified. + (let ((center-re ":\\(center\\)[[:space:]]+t\\b") + (align-re ":align[[:space:]]+\\(left\\|center\\|right\\)\\b") + attr-align) + (catch 'exit + (org-element-properties-mapc + (lambda (propname propval) + (when (and propval + (string-match-p ":attr.*" (symbol-name propname))) + (setq propval (car-safe propval)) + (when (or (string-match center-re propval) + (string-match align-re propval)) + (setq attr-align (match-string 1 propval)) + (when (eq propname :attr_org) + (throw 'exit t))))) + par)) + (if attr-align + (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 --=-=-=--