all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to temporarily show an image in an Emacs buffer?
@ 2023-03-08  4:53 Marcin Borkowski
  2023-03-08  5:23 ` Marcin Borkowski
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Marcin Borkowski @ 2023-03-08  4:53 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi all,

I need to insert an image in the buffer in a way that doesn't interfere
with the undo mechanism.  In other words, `insert-image' won't help me
because (as I understand it) it needs to insert a space or some other
string so that it can put display property on it.  How to achieve this?

To be more precise, imagine I have this buffer:

xxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyy

(two lines of text).  I want it to look like this:

xxxxxxxxxxxxxxxxxxxxxxxx
+----------------------+
|                      |
+----------------------+
yyyyyyyyyyyyyyyyyyyyyyyy

in other words, I want a rectangular image (inserted by `insert-image'
or actually `svg-insert-image') to appear between them, but in a way
that doesn't actually change the buffer contents.

Alternatively, it _may_ change the buffer contents but in a way that is
not recorded in `buffer-undo-list'.  Is that possible?

TIA,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-08  4:53 How to temporarily show an image in an Emacs buffer? Marcin Borkowski
@ 2023-03-08  5:23 ` Marcin Borkowski
  2023-03-08  6:19   ` Ruijie Yu via Users list for the GNU Emacs text editor
  2023-03-08 16:41   ` Michael Heerdegen
  2023-03-08 13:38 ` Eli Zaretskii
  2023-03-10 16:26 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2 siblings, 2 replies; 15+ messages in thread
From: Marcin Borkowski @ 2023-03-08  5:23 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list


On 2023-03-08, at 05:53, Marcin Borkowski <mbork@mbork.pl> wrote:

> Alternatively, it _may_ change the buffer contents but in a way that is
> not recorded in `buffer-undo-list'.  Is that possible?

I did some experimenting, and this

(defvar no-undo-marker (make-marker))
(defun no-undo-insert ()
  "Test undo."
  (save-excursion
    (let ((buffer-undo-list))
      (set-marker no-undo-marker (point))
      (insert "this can't be undone\n"))))

(defun no-undo-uninsert ()
  (save-excursion
    (goto-char no-undo-marker)
    (let ((buffer-undo-list))
      (when (looking-at-p "this can't be undone\n")
	(delete-char 21)))))

seems to do the trick, but depending on the order of typing and calling
these functions, the undo list becomes corrupted (I get "primitive-undo:
Changes to be undone are outside visible portion of buffer").

Any hints?

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-08  5:23 ` Marcin Borkowski
@ 2023-03-08  6:19   ` Ruijie Yu via Users list for the GNU Emacs text editor
  2023-03-08 16:01     ` Marcin Borkowski
  2023-03-08 16:41   ` Michael Heerdegen
  1 sibling, 1 reply; 15+ messages in thread
From: Ruijie Yu via Users list for the GNU Emacs text editor @ 2023-03-08  6:19 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs


Marcin Borkowski <mbork@mbork.pl> writes:

> On 2023-03-08, at 05:53, Marcin Borkowski <mbork@mbork.pl> wrote:
>
>> Alternatively, it _may_ change the buffer contents but in a way that is
>> not recorded in `buffer-undo-list'.  Is that possible?
>
> I did some experimenting, and this
>
> (defvar no-undo-marker (make-marker))
> (defun no-undo-insert ()
>   "Test undo."
>   (save-excursion
>     (let ((buffer-undo-list))
>       (set-marker no-undo-marker (point))
>       (insert "this can't be undone\n"))))
>
> (defun no-undo-uninsert ()
>   (save-excursion
>     (goto-char no-undo-marker)
>     (let ((buffer-undo-list))
>       (when (looking-at-p "this can't be undone\n")
>   (delete-char 21)))))
>
> seems to do the trick, but depending on the order of typing and calling
> these functions, the undo list becomes corrupted (I get "primitive-undo:
> Changes to be undone are outside visible portion of buffer").
>
> Any hints?

Maybe you can make use of overlays [1]?  That sounds like a great fit
according to the description of your need.

[1]: (info "(elisp) Managing Overlays")

--
Best,


RY



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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-08  4:53 How to temporarily show an image in an Emacs buffer? Marcin Borkowski
  2023-03-08  5:23 ` Marcin Borkowski
@ 2023-03-08 13:38 ` Eli Zaretskii
  2023-03-08 16:00   ` Marcin Borkowski
  2023-03-10 16:26 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2 siblings, 1 reply; 15+ messages in thread
From: Eli Zaretskii @ 2023-03-08 13:38 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Marcin Borkowski <mbork@mbork.pl>
> Date: Wed, 08 Mar 2023 05:53:46 +0100
> 
> I need to insert an image in the buffer in a way that doesn't interfere
> with the undo mechanism.  In other words, `insert-image' won't help me
> because (as I understand it) it needs to insert a space or some other
> string so that it can put display property on it.  How to achieve this?

You can put a display property on the existing text in the buffer
without inserting any new text.  See the node "Other Display
Properties" in the ELisp manual, where it describes the
‘(image . IMAGE-PROPS)’ display property spec.



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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-08 13:38 ` Eli Zaretskii
@ 2023-03-08 16:00   ` Marcin Borkowski
  2023-03-13  8:29     ` Jean Louis
  0 siblings, 1 reply; 15+ messages in thread
From: Marcin Borkowski @ 2023-03-08 16:00 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs


On 2023-03-08, at 14:38, Eli Zaretskii <eliz@gnu.org> wrote:

>> From: Marcin Borkowski <mbork@mbork.pl>
>> Date: Wed, 08 Mar 2023 05:53:46 +0100
>>
>> I need to insert an image in the buffer in a way that doesn't interfere
>> with the undo mechanism.  In other words, `insert-image' won't help me
>> because (as I understand it) it needs to insert a space or some other
>> string so that it can put display property on it.  How to achieve this?
>
> You can put a display property on the existing text in the buffer
> without inserting any new text.  See the node "Other Display
> Properties" in the ELisp manual, where it describes the
> ‘(image . IMAGE-PROPS)’ display property spec.

You're right, of course, but it's a bit more complex than that.  Since
an overlay with the `image' property "hides" the text beneath it, the
key is to create an overlay with a `before-string' property whose value
is a dummy string (say, a space), and put the `image' /text/ property on
that very string.  I did some experiments and it seems to work, though
I'll have a (hopefully) working prototype by the end of the week (no
more time for this today).

Thanks a lot!  (Also, once I have it working, I will definitely describe
it on my blog; I may also copy it here if someone is interested.)

Best,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-08  6:19   ` Ruijie Yu via Users list for the GNU Emacs text editor
@ 2023-03-08 16:01     ` Marcin Borkowski
  0 siblings, 0 replies; 15+ messages in thread
From: Marcin Borkowski @ 2023-03-08 16:01 UTC (permalink / raw)
  To: Ruijie Yu; +Cc: help-gnu-emacs


On 2023-03-08, at 07:19, Ruijie Yu <ruijie@netyu.xyz> wrote:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> On 2023-03-08, at 05:53, Marcin Borkowski <mbork@mbork.pl> wrote:
>>
>>> Alternatively, it _may_ change the buffer contents but in a way that is
>>> not recorded in `buffer-undo-list'.  Is that possible?
>>
>> I did some experimenting, and this
>>
>> (defvar no-undo-marker (make-marker))
>> (defun no-undo-insert ()
>>   "Test undo."
>>   (save-excursion
>>     (let ((buffer-undo-list))
>>       (set-marker no-undo-marker (point))
>>       (insert "this can't be undone\n"))))
>>
>> (defun no-undo-uninsert ()
>>   (save-excursion
>>     (goto-char no-undo-marker)
>>     (let ((buffer-undo-list))
>>       (when (looking-at-p "this can't be undone\n")
>>   (delete-char 21)))))
>>
>> seems to do the trick, but depending on the order of typing and calling
>> these functions, the undo list becomes corrupted (I get "primitive-undo:
>> Changes to be undone are outside visible portion of buffer").
>>
>> Any hints?
>
> Maybe you can make use of overlays [1]?  That sounds like a great fit
> according to the description of your need.
>
> [1]: (info "(elisp) Managing Overlays")

As I said in my reply to Eli, you were right.  (I knew about overlays,
but I didn't know that apparently you can use an overlay with
a `before-string' property /and/ put another display property on the
/string/ being the value of `before-string', which is kind of cool!)

Thanks,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-08  5:23 ` Marcin Borkowski
  2023-03-08  6:19   ` Ruijie Yu via Users list for the GNU Emacs text editor
@ 2023-03-08 16:41   ` Michael Heerdegen
  2023-03-11  8:14     ` Marcin Borkowski
  1 sibling, 1 reply; 15+ messages in thread
From: Michael Heerdegen @ 2023-03-08 16:41 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:

> On 2023-03-08, at 05:53, Marcin Borkowski <mbork@mbork.pl> wrote:
>
> > Alternatively, it _may_ change the buffer contents but in a way that is
> > not recorded in `buffer-undo-list'.  Is that possible?
>
> I did some experimenting, and this
>
> (defvar no-undo-marker (make-marker))
> (defun no-undo-insert ()
>   "Test undo."
>   (save-excursion
>     (let ((buffer-undo-list))
>       (set-marker no-undo-marker (point))
>       (insert "this can't be undone\n"))))
> [...]

Regardless of what has already been said, just for the sake of
completeness: AFAIK the intended way to inhibit the recording of undo
is to bind buffer-undo-list to t (not to nil).

There are lots of use case in the sources - see e.g.
`image-toggle-display-text'.

Michael.




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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-08  4:53 How to temporarily show an image in an Emacs buffer? Marcin Borkowski
  2023-03-08  5:23 ` Marcin Borkowski
  2023-03-08 13:38 ` Eli Zaretskii
@ 2023-03-10 16:26 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2023-03-11  8:15   ` Marcin Borkowski
  2 siblings, 1 reply; 15+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2023-03-10 16:26 UTC (permalink / raw)
  To: help-gnu-emacs

> I need to insert an image in the buffer in a way that doesn't interfere
> with the undo mechanism.  In other words, `insert-image' won't help me
> because (as I understand it) it needs to insert a space or some other
> string so that it can put display property on it.  How to achieve this?

`put-image` maybe?


        Stefan




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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-08 16:41   ` Michael Heerdegen
@ 2023-03-11  8:14     ` Marcin Borkowski
  2023-03-12  3:37       ` Michael Heerdegen
  0 siblings, 1 reply; 15+ messages in thread
From: Marcin Borkowski @ 2023-03-11  8:14 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs


On 2023-03-08, at 17:41, Michael Heerdegen <michael_heerdegen@web.de> wrote:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> On 2023-03-08, at 05:53, Marcin Borkowski <mbork@mbork.pl> wrote:
>>
>> > Alternatively, it _may_ change the buffer contents but in a way that is
>> > not recorded in `buffer-undo-list'.  Is that possible?
>>
>> I did some experimenting, and this
>>
>> (defvar no-undo-marker (make-marker))
>> (defun no-undo-insert ()
>>   "Test undo."
>>   (save-excursion
>>     (let ((buffer-undo-list))
>>       (set-marker no-undo-marker (point))
>>       (insert "this can't be undone\n"))))
>> [...]
>
> Regardless of what has already been said, just for the sake of
> completeness: AFAIK the intended way to inhibit the recording of undo
> is to bind buffer-undo-list to t (not to nil).

Thanks, I missed that.

> There are lots of use case in the sources - see e.g.
> `image-toggle-display-text'.

I didn't experiment with this, but I'm afraid that interlacing changes
to the buffer done by the user (when recording undo changes) and by the
Elisp code (while `buffer-undo-list' is bound to `t') may result in
undoing being impossible.

Anyway, I solved /my/ particular problem using overlays.  Thanks!

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-10 16:26 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2023-03-11  8:15   ` Marcin Borkowski
  0 siblings, 0 replies; 15+ messages in thread
From: Marcin Borkowski @ 2023-03-11  8:15 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On 2023-03-10, at 17:26, Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:

>> I need to insert an image in the buffer in a way that doesn't interfere
>> with the undo mechanism.  In other words, `insert-image' won't help me
>> because (as I understand it) it needs to insert a space or some other
>> string so that it can put display property on it.  How to achieve this?
>
> `put-image` maybe?

Oh, thanks!  It looks like I reinvented it (although in a less flexible
form, sufficient for my purposes).  I guess I'll switch to `put-image'.

Best,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-11  8:14     ` Marcin Borkowski
@ 2023-03-12  3:37       ` Michael Heerdegen
  2023-03-12  7:46         ` Marcin Borkowski
  0 siblings, 1 reply; 15+ messages in thread
From: Michael Heerdegen @ 2023-03-12  3:37 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:

> I didn't experiment with this, but I'm afraid that interlacing changes
> to the buffer done by the user (when recording undo changes) and by the
> Elisp code (while `buffer-undo-list' is bound to `t') may result in
> undoing being impossible.

Yes, what is allowed is very limited.  AFAIU anything changing any
positions is already very bad (undo positions are numbers, not markers).

Michael.




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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-12  3:37       ` Michael Heerdegen
@ 2023-03-12  7:46         ` Marcin Borkowski
  0 siblings, 0 replies; 15+ messages in thread
From: Marcin Borkowski @ 2023-03-12  7:46 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs


On 2023-03-12, at 04:37, Michael Heerdegen <michael_heerdegen@web.de> wrote:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> I didn't experiment with this, but I'm afraid that interlacing changes
>> to the buffer done by the user (when recording undo changes) and by the
>> Elisp code (while `buffer-undo-list' is bound to `t') may result in
>> undoing being impossible.
>
> Yes, what is allowed is very limited.  AFAIU anything changing any
> positions is already very bad (undo positions are numbers, not markers).

Thanks, that confirms my reservations, and shows that overlays are the
way to go.

Best,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-08 16:00   ` Marcin Borkowski
@ 2023-03-13  8:29     ` Jean Louis
  2023-03-13  9:27       ` Marcin Borkowski
  2023-04-01 15:57       ` Marcin Borkowski
  0 siblings, 2 replies; 15+ messages in thread
From: Jean Louis @ 2023-03-13  8:29 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: Eli Zaretskii, help-gnu-emacs

* Marcin Borkowski <mbork@mbork.pl> [2023-03-08 19:02]:
> Thanks a lot!  (Also, once I have it working, I will definitely describe
> it on my blog; I may also copy it here if someone is interested.)

I am interested in feature:

1. To temporary show image in buffer

2. To remove that image, as it was temporary shown.

Did you make functions to do that?

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-13  8:29     ` Jean Louis
@ 2023-03-13  9:27       ` Marcin Borkowski
  2023-04-01 15:57       ` Marcin Borkowski
  1 sibling, 0 replies; 15+ messages in thread
From: Marcin Borkowski @ 2023-03-13  9:27 UTC (permalink / raw)
  To: Jean Louis; +Cc: Eli Zaretskii, help-gnu-emacs


On 2023-03-13, at 09:29, Jean Louis <bugs@gnu.support> wrote:

> * Marcin Borkowski <mbork@mbork.pl> [2023-03-08 19:02]:
>> Thanks a lot!  (Also, once I have it working, I will definitely describe
>> it on my blog; I may also copy it here if someone is interested.)
>
> I am interested in feature:
>
> 1. To temporary show image in buffer
>
> 2. To remove that image, as it was temporary shown.
>
> Did you make functions to do that?

Not yet.  But it will be blogged about (no sooner than in April, though
– I have a buffer until the end of March).  I can mention it here, too,
unless I forget.

Best,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: How to temporarily show an image in an Emacs buffer?
  2023-03-13  8:29     ` Jean Louis
  2023-03-13  9:27       ` Marcin Borkowski
@ 2023-04-01 15:57       ` Marcin Borkowski
  1 sibling, 0 replies; 15+ messages in thread
From: Marcin Borkowski @ 2023-04-01 15:57 UTC (permalink / raw)
  To: Jean Louis; +Cc: Eli Zaretskii, help-gnu-emacs


On 2023-03-13, at 09:29, Jean Louis <bugs@gnu.support> wrote:

> * Marcin Borkowski <mbork@mbork.pl> [2023-03-08 19:02]:
>> Thanks a lot!  (Also, once I have it working, I will definitely describe
>> it on my blog; I may also copy it here if someone is interested.)
>
> I am interested in feature:
>
> 1. To temporary show image in buffer
>
> 2. To remove that image, as it was temporary shown.
>
> Did you make functions to do that?

https://mbork.pl/2023-04-01_Showing_an_image_between_lines_in_Emacs

As for removing, `remove-overlays' is the nuclear option, and
`delete-overlay' a standard way to remove just one overlay (of course,
for that you need a reference to it).

Hth,

-- 
Marcin Borkowski
http://mbork.pl



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

end of thread, other threads:[~2023-04-01 15:57 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-08  4:53 How to temporarily show an image in an Emacs buffer? Marcin Borkowski
2023-03-08  5:23 ` Marcin Borkowski
2023-03-08  6:19   ` Ruijie Yu via Users list for the GNU Emacs text editor
2023-03-08 16:01     ` Marcin Borkowski
2023-03-08 16:41   ` Michael Heerdegen
2023-03-11  8:14     ` Marcin Borkowski
2023-03-12  3:37       ` Michael Heerdegen
2023-03-12  7:46         ` Marcin Borkowski
2023-03-08 13:38 ` Eli Zaretskii
2023-03-08 16:00   ` Marcin Borkowski
2023-03-13  8:29     ` Jean Louis
2023-03-13  9:27       ` Marcin Borkowski
2023-04-01 15:57       ` Marcin Borkowski
2023-03-10 16:26 ` Stefan Monnier via Users list for the GNU Emacs text editor
2023-03-11  8:15   ` Marcin Borkowski

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.