emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: stardiviner <numbchild@gmail.com>, Org mode <emacs-orgmode@gnu.org>
Subject: Re: [PATCH v1] Inline image display as part of a new org-link-preview system
Date: Sat, 31 Aug 2024 09:41:50 -0700	[thread overview]
Message-ID: <87msksabld.fsf@gmail.com> (raw)
In-Reply-To: <874j70n559.fsf@localhost>

Thanks Ihor.  Before I send the next iteration of the patch, I'd like
your input on some decisions marked with "Q:" below.

>> Not yet handled or final:
>>
>> - Link abbreviations: I'm using org-link-any-re to find links, and this
>>   appears to be handling link abbreviations fine.  So there is no
>>   special code to handle link abbreviations.  But I'm not sure.
>
>> - The calling convention for the :preview function is not final.  Right
>>   now it is given the overlay, the link type and the link path.  But
>>   other link details can be required -- for example, image links need to
>>   read the org keywords for the image width and alignment, so I end up
>>   calling (org-element-context) again inside the :preview function.
>
> We may simply pass the link object to :preview function.

If we pass the link object instead of the overlay, it will be the
preview function's job to create overlays as needed.  This overlay
should have special properties (like `org-image-overlay') for previewing
to work correctly.  

Q: Is this okay? Or did you mean we can pass both the link and the
overlay, like this:

(funcall preview-func ov link)

and let the previewer figure out the link type and path details?

>> Subject: [PATCH 2/2] org-link: Move inline image display to org-link
>
> Is there [PATCH 1/2]?

There is no [PATCH 1/2], sorry about the naming.  What I sent is the
complete patch.

>> * lisp/org-keys.el: Bind `C-c C-x C-v' to new command
>> `org-link-preview', which has the same prefix arg behaviors as
>> `org-latex-preview'.
>
> Didn't we discuss changes to the behavior?

Yes, those changes have been implemented, please see the docstring for
org-link-preview.  The behavior is identical to that of
org-latex-preview, but in addition the 1 and the 11 numeric prefix args
are handled specially.

>> +`:preview'
>> +
>> +  Function to run when generating an in-buffer preview for the
>> +  link.  It must accept three arguments:
>> +  - an overlay placed from the start to the end of the link.
>> +  - the link type, as a string.
>> +  - the path, as a string.
>
> Maybe we can ask the function to return non-nil when the preview is
> going to happen. The current approach with "overlay deleted then no
> preview" is not ideal.

Good idea.

>> +(defun org-link-preview--get-overlays (&optional beg end)
>> +  "Return link preview overlays between BEG and END."
>> +  (let* ((beg (or beg (point-min)))
>> +         (end (or end (point-max)))
>> +         (overlays (overlays-in beg end))
>> +         result)
>> +    (dolist (ov overlays result)
>> +      (when (memq ov org-link-preview-overlays)
>> +        (push ov result)))))
>
> I know that this is a copy-paste, but we may utilize 'org-image-overlay
> + `org-find-overlays' here.

Noted.

>> +(defun org-link-preview--remove-overlay (ov after _beg _end &optional _len)
>> +  "Remove link-preview overlay OV if a corresponding region is modified.
>> +
>> +AFTER is true when this function is called post-change."
>> +  (when (and ov after)
>> +    (setq org-link-preview-overlays (delete ov org-link-preview-overlays))
>> +    ;; Clear image from cache to avoid image not updating upon
>> +    ;; changing on disk.  See Emacs bug#59902.
>> +    (when (overlay-get ov 'org-image-overlay)
>> +      (image-flush (overlay-get ov 'display)))
>> +    (delete-overlay ov)))
>
> It implies that every :preview function _must_ put an image as 'display
> property. If it does not, we will run into errors. But should it?

This was an oversight, I meant to add

(when-let ((disp (overlay-get ov 'display))
           ((imagep disp)))
  (image-flush disp))

Will fix.

> Also, when if preview is asynchronous, and we run this function when the
> image is not yet assigned to the overlay?

Handled by the above, plus the fact that we delete the overlay.  It's up
to the async callback to handle the case where the overlay no longer exists.

>> +    (cond
>> +     ((not (display-graphic-p))
>> +      (message "Your Emacs does not support displaying images!"))
>
> May some third-party previews not require graphic?

Good point, will handle.

>> +(defun org-link-preview-clear (&optional beg end)
>> +  "Clear link previews in region BEG to END."
>> +  (interactive (and (use-region-p) (list (region-beginning) (region-end))))
>> +  (let* ((beg (or beg (point-min)))
>> +         (end (or end (point-max)))
>> +         (overlays (overlays-in beg end)))
>> +    (dolist (ov overlays)
>> +      (when (memq ov org-link-preview-overlays)
>> +        (when-let ((image (overlay-get ov 'display))
>> +                   ((imagep image)))
>> +          (image-flush image))
>> +        (setq org-link-preview-overlays (delq ov org-link-preview-overlays))
>> +        (delete-overlay ov)))
>
> In asynchronous previews, some overlays may be deleted already. We
> should be careful with this.

Noted.

>> +(defun org-link-preview-file (ov linktype path)
>> +  "Display image file PATH in overlay OV.
>> +
>> +LINKTYPE is the Org link type used to preview PATH, either
>> +\"file\" or \"attachment\".
>> +
>> +Equip each image with the keymap `image-map'.
>> +
>> +This is intended to be used as the `:preview' link property of
>> +file links, see `org-link-parameters'."
>> +  (if-let ((file-full
>> +            (if (equal "attachment" linktype)
>> +		(progn
>> +                  (require 'org-attach)
>> +		  (ignore-errors (org-attach-expand path)))
>> +              (expand-file-name path)))
>
> I'd rather put this part into org-attach, as a separate function that
> calls `org-link-preview-file'.

Q: I don't follow.  Right now `org-link-preview-file' is the :preview
org-link-parameter of file links and attachments.  Could you explain how
this indirection should work instead?

Karthik


  reply	other threads:[~2024-08-31 16:42 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-15  3:28 [PATCH] add a function to only refresh inline images under current headline instead of global buffer Christopher M. Miles
2023-05-15 11:08 ` Ihor Radchenko
2023-05-15 13:01   ` Christopher M. Miles
2023-05-15 14:00   ` [PATCH v2] " Christopher M. Miles
2023-05-16  9:17     ` Ihor Radchenko
2023-05-16 12:18       ` Christopher M. Miles
2023-07-24 11:25         ` Ihor Radchenko
2023-08-01  4:40           ` [PATCH v3] " Christopher M. Miles
2023-08-01  8:04             ` Ihor Radchenko
2023-08-01 12:17               ` [PATCH v3.1] " Christopher M. Miles
2023-08-01 14:09                 ` Ihor Radchenko
2023-08-01 15:22                   ` Christopher M. Miles
2023-08-01 15:46                   ` Christopher M. Miles
2023-08-02 16:08                     ` Ihor Radchenko
2023-08-04  6:30                       ` Christopher M. Miles
2023-08-02  7:26                 ` Ihor Radchenko
2023-08-02 15:44                   ` Christopher M. Miles
2023-08-04  8:20                     ` Ihor Radchenko
2023-08-05  5:28                       ` [PATCH v3.2] " Christopher M. Miles
2024-07-22 10:46                         ` Ihor Radchenko
2024-08-01 22:58                           ` [PATCH v4.0] " stardiviner
     [not found]                           ` <66a8b73b.170a0220.383476.996e@mx.google.com>
2024-08-12 10:18                             ` Ihor Radchenko
2024-08-14  2:04                               ` stardiviner
2024-08-18 10:27                                 ` Ihor Radchenko
2024-08-20  2:02                                   ` Karthik Chikmagalur
2024-08-20 15:43                                     ` Karthik Chikmagalur
2024-08-20 18:19                                       ` Ihor Radchenko
2024-08-20 19:46                                         ` Karthik Chikmagalur
2024-08-22 13:19                                           ` Ihor Radchenko
2024-08-23  6:04                                             ` Karthik Chikmagalur
2024-08-23 23:36                                             ` [PATCH v1] Inline image display as part of a new org-link-preview system Karthik Chikmagalur
2024-08-24  1:00                                               ` Karthik Chikmagalur
2024-08-31 14:22                                               ` Ihor Radchenko
2024-08-31 16:41                                                 ` Karthik Chikmagalur [this message]
2024-08-31 16:53                                                   ` Ihor Radchenko
2024-08-31 22:37                                                     ` [PATCH v2] " Karthik Chikmagalur
2024-09-01 13:06                                                       ` Ihor Radchenko
2024-09-02 20:13                                                         ` [PATCH v3] " Karthik Chikmagalur
2024-09-08  7:43                                                           ` Ihor Radchenko
2024-09-09  3:21                                                             ` Karthik Chikmagalur
2024-09-09  6:06                                                               ` Ihor Radchenko
2024-09-09  6:30                                                                 ` Karthik Chikmagalur
2024-09-09 16:47                                                                   ` Ihor Radchenko
2024-09-09 19:14                                                                     ` Karthik Chikmagalur
2024-09-10 16:57                                                                       ` Ihor Radchenko
2024-09-10 19:53                                                                         ` Karthik Chikmagalur
2024-09-15  7:51                                                                           ` Ihor Radchenko
2024-09-09 21:45                                                                 ` Karthik Chikmagalur
2024-09-10 16:58                                                                   ` Ihor Radchenko
2024-09-10 17:38                                                                     ` Karthik Chikmagalur
2024-09-10 18:34                                                                       ` Ihor Radchenko
2024-09-10 19:43                                                             ` Karthik Chikmagalur
2024-09-15  8:12                                                               ` Ihor Radchenko
2024-09-15 20:50                                                                 ` Karthik Chikmagalur
2024-09-15 21:57                                                                 ` [PATCH v4] " Karthik Chikmagalur
2024-08-18 10:34                                 ` [FR] Automatically display images in resutls of evaluation (was: [PATCH v4.0] Re: [PATCH] add a function to only refresh inline images under current headline instead of global buffer) Ihor Radchenko
     [not found]                                   ` <66c54dfc.a70a0220.3c823a.2899@mx.google.com>
2024-08-22 13:06                                     ` [FR] Automatically display images in resutls of evaluation Ihor Radchenko
     [not found]                                       ` <66c89411.170a0220.3255c1.0cd5@mx.google.com>
     [not found]                                         ` <87zfor7b04.fsf@localhost>
     [not found]                                           ` <CAL1eYuLOsaS43ahueN4uWiCn+Ykp=p_-t9dzAypKdy1en_53BQ@mail.gmail.com>
2024-09-15 13:33                                             ` [PATCH v3] " Ihor Radchenko

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=87msksabld.fsf@gmail.com \
    --to=karthikchikmagalur@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=numbchild@gmail.com \
    --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).