all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] Justify/align image previews in org-mode
@ 2023-12-17 21:19 Karthik Chikmagalur
  2023-12-18  9:19 ` Karthik Chikmagalur
  0 siblings, 1 reply; 12+ messages in thread
From: Karthik Chikmagalur @ 2023-12-17 21:19 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1023 bytes --]

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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-image-align.patch --]
[-- Type: text/x-patch, Size: 4413 bytes --]

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)

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] Justify/align image previews in org-mode
  2023-12-17 21:19 [PATCH] Justify/align image previews in org-mode Karthik Chikmagalur
@ 2023-12-18  9:19 ` Karthik Chikmagalur
  2023-12-18 12:47   ` Ihor Radchenko
  0 siblings, 1 reply; 12+ messages in thread
From: Karthik Chikmagalur @ 2023-12-18  9:19 UTC (permalink / raw)
  To: karthikchikmagalur; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 121 bytes --]

Please ignore the previous patch and use this one instead.  I've fixed a
bug and a couple of formatting errors.

Karthik

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-image-align.patch --]
[-- Type: text/x-patch, Size: 4407 bytes --]

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)

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] Justify/align image previews in org-mode
  2023-12-18  9:19 ` Karthik Chikmagalur
@ 2023-12-18 12:47   ` Ihor Radchenko
  2023-12-18 16:49     ` Karthik Chikmagalur
  0 siblings, 1 reply; 12+ messages in thread
From: Ihor Radchenko @ 2023-12-18 12:47 UTC (permalink / raw)
  To: Karthik Chikmagalur; +Cc: emacs-orgmode

Karthik Chikmagalur <karthikchikmagalur@gmail.com> writes:

> Please ignore the previous patch and use this one instead.  I've fixed a
> bug and a couple of formatting errors.

Thanks for the patch!
See my comments inline.

> +(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).

It would make sense to allow 'left value as well (same as nil).

> +                              (when align
> +                                (overlay-put
> +                                 ov 'before-string
> +                                 (propertize
> +                                  " " 'face 'default
> +                                  'display
> +                                  (pcase align
> +                                    ((or 'center 'justify)

I do not think that we need to consider 'justify value at this point.
Maybe in future, when (or if) we add proper justification support to
text. But not now.

> +(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

This will not work when the image paragraph is indented:

* This is test
  #+attr_org: :align center
  [[file:~/Downloads/wallpaper.png]]

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Justify/align image previews in org-mode
  2023-12-18 12:47   ` Ihor Radchenko
@ 2023-12-18 16:49     ` Karthik Chikmagalur
  2023-12-18 17:07       ` Ihor Radchenko
  0 siblings, 1 reply; 12+ messages in thread
From: Karthik Chikmagalur @ 2023-12-18 16:49 UTC (permalink / raw)
  To: yantar92; +Cc: emacs-orgmode, karthikchikmagalur

> It would make sense to allow 'left value as well (same as nil).

Done.

> I do not think that we need to consider 'justify value at this point.
> Maybe in future, when (or if) we add proper justification support to
> text. But not now.

Removed support for 'justify.

> This will not work when the image paragraph is indented:

* This is test
  #+attr_org: :align center
  [[file:~/Downloads/wallpaper.png]]

- Is there a better way to address this than checking if there is only
whitespace behind the link until the start of the paragraph?
- 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.

Karthik


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Justify/align image previews in org-mode
  2023-12-18 16:49     ` Karthik Chikmagalur
@ 2023-12-18 17:07       ` Ihor Radchenko
  2023-12-18 20:18         ` Karthik Chikmagalur
  0 siblings, 1 reply; 12+ messages in thread
From: Ihor Radchenko @ 2023-12-18 17:07 UTC (permalink / raw)
  To: Karthik Chikmagalur; +Cc: emacs-orgmode

Karthik Chikmagalur <karthikchikmagalur@gmail.com> writes:

>> This will not work when the image paragraph is indented:
>
> * This is test
>   #+attr_org: :align center
>   [[file:~/Downloads/wallpaper.png]]
>
> - Is there a better way to address this than checking if there is only
> whitespace behind the link until the start of the paragraph?

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)))

> - 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")

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Justify/align image previews in org-mode
  2023-12-18 17:07       ` Ihor Radchenko
@ 2023-12-18 20:18         ` Karthik Chikmagalur
  2023-12-19  2:25           ` Karthik Chikmagalur
  0 siblings, 1 reply; 12+ messages in thread
From: Karthik Chikmagalur @ 2023-12-18 20:18 UTC (permalink / raw)
  To: yantar92; +Cc: emacs-orgmode, karthikchikmagalur

[-- Attachment #1: Type: text/plain, Size: 1374 bytes --]

> 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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-image-align-v3.patch --]
[-- Type: text/x-patch, Size: 4925 bytes --]

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)

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] Justify/align image previews in org-mode
  2023-12-18 20:18         ` Karthik Chikmagalur
@ 2023-12-19  2:25           ` Karthik Chikmagalur
  2023-12-19 11:44             ` Ihor Radchenko
  2023-12-19 11:56             ` Ihor Radchenko
  0 siblings, 2 replies; 12+ messages in thread
From: Karthik Chikmagalur @ 2023-12-19  2:25 UTC (permalink / raw)
  To: karthikchikmagalur; +Cc: emacs-orgmode, yantar92

[-- Attachment #1: Type: text/plain, Size: 505 bytes --]

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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-Add-image-alignment.patch --]
[-- Type: text/x-patch, Size: 10044 bytes --]

From eb1b287c009c2f7eb83e7e31d64980ba79f44527 Mon Sep 17 00:00:00 2001
From: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
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


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] Justify/align image previews in org-mode
  2023-12-19  2:25           ` Karthik Chikmagalur
@ 2023-12-19 11:44             ` Ihor Radchenko
  2023-12-19 19:05               ` Karthik Chikmagalur
  2023-12-19 11:56             ` Ihor Radchenko
  1 sibling, 1 reply; 12+ messages in thread
From: Ihor Radchenko @ 2023-12-19 11:44 UTC (permalink / raw)
  To: Karthik Chikmagalur; +Cc: emacs-orgmode

Karthik Chikmagalur <karthikchikmagalur@gmail.com> writes:

> 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.

> - Add a checker for `:align nil' to org-lint.  `:align nil' is not
>   supported.

I meant :align <anything> being not supported in #+attr_org. Not just nil.

> +  #+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.

> +  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 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  

> +  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.

> +  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.

> +  #+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"

> +(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 <anything> in #+attr_org.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Justify/align image previews in org-mode
  2023-12-19  2:25           ` Karthik Chikmagalur
  2023-12-19 11:44             ` Ihor Radchenko
@ 2023-12-19 11:56             ` Ihor Radchenko
  2023-12-25  8:38               ` Bastien Guerry
  1 sibling, 1 reply; 12+ messages in thread
From: Ihor Radchenko @ 2023-12-19 11:56 UTC (permalink / raw)
  To: Karthik Chikmagalur, Bastien; +Cc: emacs-orgmode

Karthik Chikmagalur <karthikchikmagalur@gmail.com> writes:

> From eb1b287c009c2f7eb83e7e31d64980ba79f44527 Mon Sep 17 00:00:00 2001
> From: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
> Date: Mon, 18 Dec 2023 12:56:33 -0800
> Subject: [PATCH] org: Add image alignment

Bastien, may you please confirm that Karthik's FSF assignment is in order?

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Justify/align image previews in org-mode
  2023-12-19 11:44             ` Ihor Radchenko
@ 2023-12-19 19:05               ` Karthik Chikmagalur
  2023-12-21 13:42                 ` Ihor Radchenko
  0 siblings, 1 reply; 12+ messages in thread
From: Karthik Chikmagalur @ 2023-12-19 19:05 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 4077 bytes --]

>> 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 <anything> 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 <anything> in #+attr_org.

Yes, only the value of #+attr_org is checked now.

Patch v2 attached.

Karthik

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-Add-image-alignment-v2.patch --]
[-- Type: text/x-patch, Size: 10738 bytes --]

From 7176f43282749c06daf2756360633cc47d79b63c Mon Sep 17 00:00:00 2001
From: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
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


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] Justify/align image previews in org-mode
  2023-12-19 19:05               ` Karthik Chikmagalur
@ 2023-12-21 13:42                 ` Ihor Radchenko
  0 siblings, 0 replies; 12+ messages in thread
From: Ihor Radchenko @ 2023-12-21 13:42 UTC (permalink / raw)
  To: Karthik Chikmagalur; +Cc: emacs-orgmode

Karthik Chikmagalur <karthikchikmagalur@gmail.com> writes:

> Patch v2 attached.
>
> Karthik
> From 7176f43282749c06daf2756360633cc47d79b63c Mon Sep 17 00:00:00 2001
> From: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
> Date: Mon, 18 Dec 2023 12:56:33 -0800
> Subject: [PATCH] org: Add image alignment

Thanks!
Applied, onto main, after resolving merge conflicts and changing the
linter trust level to 'high.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=6011e7a48

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Justify/align image previews in org-mode
  2023-12-19 11:56             ` Ihor Radchenko
@ 2023-12-25  8:38               ` Bastien Guerry
  0 siblings, 0 replies; 12+ messages in thread
From: Bastien Guerry @ 2023-12-25  8:38 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Karthik Chikmagalur, emacs-orgmode

Ihor Radchenko <yantar92@posteo.net> writes:

>> From eb1b287c009c2f7eb83e7e31d64980ba79f44527 Mon Sep 17 00:00:00 2001
>> From: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
>> Date: Mon, 18 Dec 2023 12:56:33 -0800
>> Subject: [PATCH] org: Add image alignment
>
> Bastien, may you please confirm that Karthik's FSF assignment is in
> order?

Yes it is, since 2021.  Thanks for contributing!

-- 
 Bastien Guerry


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-12-25  8:39 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-17 21:19 [PATCH] Justify/align image previews in org-mode Karthik Chikmagalur
2023-12-18  9:19 ` Karthik Chikmagalur
2023-12-18 12:47   ` Ihor Radchenko
2023-12-18 16:49     ` Karthik Chikmagalur
2023-12-18 17:07       ` Ihor Radchenko
2023-12-18 20:18         ` Karthik Chikmagalur
2023-12-19  2:25           ` Karthik Chikmagalur
2023-12-19 11:44             ` Ihor Radchenko
2023-12-19 19:05               ` Karthik Chikmagalur
2023-12-21 13:42                 ` Ihor Radchenko
2023-12-19 11:56             ` Ihor Radchenko
2023-12-25  8:38               ` Bastien Guerry

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.