emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
To: yantar92@posteo.net
Cc: emacs-orgmode@gnu.org,karthikchikmagalur@gmail.com
Subject: Re: [PATCH] Justify/align image previews in org-mode
Date: Mon, 18 Dec 2023 12:18:01 -0800	[thread overview]
Message-ID: <87v88v1czq.fsf@gmail.com> (raw)
In-Reply-To: <87y1drphg1.fsf@localhost>

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

  reply	other threads:[~2023-12-18 20:18 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87v88v1czq.fsf@gmail.com \
    --to=karthikchikmagalur@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).