all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How can I undo a change from a modification-hook?
@ 2017-04-26  7:02 Clément Pit--Claudel
  2017-04-26 12:26 ` Stefan Monnier
  0 siblings, 1 reply; 12+ messages in thread
From: Clément Pit--Claudel @ 2017-04-26  7:02 UTC (permalink / raw)
  To: Emacs developers

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

Hey emacs-devel,

I'm using overlays to keep track of buffer regions that have been sent to a subprocess.  Edits in a previously sent ("processed") region are generally forbidden — the only two permissible kinds of changes in processed regions are editing a comment and adding or removing of space at the end of a line.

Adding a modification-hook to my overlays makes it easy to disallow changes (by raising an error), and to allow changes when in a comment.  I can't find a good way to allow whitespace edits at the end of a line, though.  I can detect such changes from the second call to the modification hook (during which it's easy to know whether the change is only adding or removing space at the end of a line), but I can't easily undo the changes and error out if they involve more than whitespace, can I?

What's the right way do disallow certain modifications in text covered by an overlay, in particular when the determination of whether to allow a given modification cannot be made during the first call to the overlay's modification hook?  For example, how can I make an overlay that allows insertion or deletion of space at the end of the lines that it covers, but no other modifications?

Thanks!
Clément.


[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 1979 bytes --]

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

* Re: How can I undo a change from a modification-hook?
  2017-04-26  7:02 How can I undo a change from a modification-hook? Clément Pit--Claudel
@ 2017-04-26 12:26 ` Stefan Monnier
  2017-04-26 13:45   ` Clément Pit-Claudel
  0 siblings, 1 reply; 12+ messages in thread
From: Stefan Monnier @ 2017-04-26 12:26 UTC (permalink / raw)
  To: emacs-devel

> (by raising an error), and to allow changes when in a comment.  I can't
> find a good way to allow whitespace edits at the end of a line, though.
> I can detect such changes from the second call to the modification hook
> (during which it's easy to know whether the change is only adding or
> removing space at the end of a line), but I can't easily undo the changes
> and error out if they involve more than whitespace, can I?

Technically you can, but it's a bit messy and will likely not be
100% reliable.  Personally, in your case, I wouldn't bother doing it,
but here's how I'd do it otherwise:

- in the before-hook stash the value of buffer-undo-list somewhere.
  [ While you're at it, stash the beg/end there as well.  ]
- in the after-hook check the change (using beg/end/len) [ and while
  you're there, check that the beg/end/len is within the announces
  beg/end stashed earlier, and if it's not just don't do anything
  since it means the stash buffer-undo-list is likely unrelated.  ]
  and if it doesn't fit your criteria, call undo until you reach the
  previously stashed value of buffer-undo-list, and then signal the error.


-- Stefan




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

* Re: How can I undo a change from a modification-hook?
  2017-04-26 12:26 ` Stefan Monnier
@ 2017-04-26 13:45   ` Clément Pit-Claudel
  2017-04-26 13:52     ` Stefan Monnier
  0 siblings, 1 reply; 12+ messages in thread
From: Clément Pit-Claudel @ 2017-04-26 13:45 UTC (permalink / raw)
  To: Stefan Monnier, emacs-devel

> Technically you can, but it's a bit messy and will likely not be
> 100% reliable.  Personally, in your case, I wouldn't bother doing it,
> but here's how I'd do it otherwise:

Thanks for the answer! The context is Proof General: it's very annoying that running whitespace-cleanup retracts what looks like arbitrary sections of the buffer.

On 2017-04-26 08:26, Stefan Monnier wrote:
> [ and while
>   you're there, check that the beg/end/len is within the announces
>   beg/end stashed earlier, and if it's not just don't do anything
>   since it means the stash buffer-undo-list is likely unrelated.  ]

This part worries me.  Could you clarify when this would happen?
Not doing anything is risky, because it puts the buffer in an inconsistent state wrt the subprocess :/

Thanks!



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

* Re: How can I undo a change from a modification-hook?
  2017-04-26 13:45   ` Clément Pit-Claudel
@ 2017-04-26 13:52     ` Stefan Monnier
  2017-04-26 14:23       ` Clément Pit-Claudel
  0 siblings, 1 reply; 12+ messages in thread
From: Stefan Monnier @ 2017-04-26 13:52 UTC (permalink / raw)
  To: emacs-devel

>> [ and while you're there, check that the beg/end/len is within the
>> announces beg/end stashed earlier, and if it's not just don't do
>> anything since it means the stash buffer-undo-list is likely
>> unrelated.  ]
> This part worries me.  Could you clarify when this would happen?

In rare corner cases (most of them bugs).

> Not doing anything is risky, because it puts the buffer in an inconsistent
> state wrt the subprocess :/

In the case of PG, I'd suggest you treat such a case as
a non-whitespace change.


        Stefan




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

* Re: How can I undo a change from a modification-hook?
  2017-04-26 13:52     ` Stefan Monnier
@ 2017-04-26 14:23       ` Clément Pit-Claudel
  2017-04-26 14:51         ` Stefan Monnier
  0 siblings, 1 reply; 12+ messages in thread
From: Clément Pit-Claudel @ 2017-04-26 14:23 UTC (permalink / raw)
  To: emacs-devel

On 2017-04-26 09:52, Stefan Monnier wrote:
>>> [ and while you're there, check that the beg/end/len is within the
>>> announces beg/end stashed earlier, and if it's not just don't do
>>> anything since it means the stash buffer-undo-list is likely
>>> unrelated.  ]
>> This part worries me.  Could you clarify when this would happen?
> 
> In rare corner cases (most of them bugs).
> 
>> Not doing anything is risky, because it puts the buffer in an inconsistent
>> state wrt the subprocess :/
> 
> In the case of PG, I'd suggest you treat such a case as
> a non-whitespace change.

But I can't, can I? The damage (the change) is already done at that point, and I can't revert it. Am I missing something?

Clément.




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

* Re: How can I undo a change from a modification-hook?
  2017-04-26 14:23       ` Clément Pit-Claudel
@ 2017-04-26 14:51         ` Stefan Monnier
  2017-04-26 16:38           ` Clément Pit-Claudel
  0 siblings, 1 reply; 12+ messages in thread
From: Stefan Monnier @ 2017-04-26 14:51 UTC (permalink / raw)
  To: emacs-devel

>> In the case of PG, I'd suggest you treat such a case as
>> a non-whitespace change.
> But I can't, can I? The damage (the change) is already done at that point,
> and I can't revert it.

But you can "retract" until the beginning of the changed area.


        Stefan




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

* Re: How can I undo a change from a modification-hook?
  2017-04-26 14:51         ` Stefan Monnier
@ 2017-04-26 16:38           ` Clément Pit-Claudel
  2017-04-26 18:43             ` Stefan Monnier
  0 siblings, 1 reply; 12+ messages in thread
From: Clément Pit-Claudel @ 2017-04-26 16:38 UTC (permalink / raw)
  To: emacs-devel

On 2017-04-26 10:51, Stefan Monnier wrote:
>>> In the case of PG, I'd suggest you treat such a case as
>>> a non-whitespace change.
>> But I can't, can I? The damage (the change) is already done at that point,
>> and I can't revert it.
> 
> But you can "retract" until the beginning of the changed area.

No, not if the current subprocess is busy.




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

* Re: How can I undo a change from a modification-hook?
  2017-04-26 16:38           ` Clément Pit-Claudel
@ 2017-04-26 18:43             ` Stefan Monnier
  2017-04-26 19:02               ` Clément Pit-Claudel
  0 siblings, 1 reply; 12+ messages in thread
From: Stefan Monnier @ 2017-04-26 18:43 UTC (permalink / raw)
  To: emacs-devel

>>>> In the case of PG, I'd suggest you treat such a case as
>>>> a non-whitespace change.
>>> But I can't, can I? The damage (the change) is already done at that point,
>>> and I can't revert it.
>> But you can "retract" until the beginning of the changed area.
> No, not if the current subprocess is busy.

Not sure why it matters: you queue the retraction commands for execution
when the subprocess is done with its current command(s).


        Stefan




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

* Re: How can I undo a change from a modification-hook?
  2017-04-26 18:43             ` Stefan Monnier
@ 2017-04-26 19:02               ` Clément Pit-Claudel
  2017-04-27 12:48                 ` Stefan Monnier
  0 siblings, 1 reply; 12+ messages in thread
From: Clément Pit-Claudel @ 2017-04-26 19:02 UTC (permalink / raw)
  To: emacs-devel

On 2017-04-26 14:43, Stefan Monnier wrote:
>>>>> In the case of PG, I'd suggest you treat such a case as
>>>>> a non-whitespace change.
>>>> But I can't, can I? The damage (the change) is already done at that point,
>>>> and I can't revert it.
>>> But you can "retract" until the beginning of the changed area.
>> No, not if the current subprocess is busy.
> 
> Not sure why it matters: you queue the retraction commands for execution
> when the subprocess is done with its current command(s).

No, that's not something that I can currently do: I can't queue a retraction before the processing of that section is complete (because that processing may fail, and then there wouldn't be anything left to retract).

Of course, I could rewrite the queuing logic to be more resilient to this kind of changes.  But the question is about ways to prevent edits, and I think it's more general than this particular example.





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

* Re: How can I undo a change from a modification-hook?
  2017-04-26 19:02               ` Clément Pit-Claudel
@ 2017-04-27 12:48                 ` Stefan Monnier
  2017-04-27 12:58                   ` Clément Pit-Claudel
  0 siblings, 1 reply; 12+ messages in thread
From: Stefan Monnier @ 2017-04-27 12:48 UTC (permalink / raw)
  To: emacs-devel

> Of course, I could rewrite the queuing logic to be more resilient to this
> kind of changes.  But the question is about ways to prevent edits, and
> I think it's more general than this particular example.

Then I woulnd't worry too much about it: the cases where the after-hook
is called "outside" of the before hook's beg/end are basically bugs and
IIUC there are no known remaining cases in Emacs-26 (IIRC there are some
known cases in Emacs-25 where this occurs, mostly in
insert-file-contents (i.e. revert-buffer)).


        Stefan




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

* Re: How can I undo a change from a modification-hook?
  2017-04-27 12:48                 ` Stefan Monnier
@ 2017-04-27 12:58                   ` Clément Pit-Claudel
  2017-04-27 17:36                     ` Stefan Monnier
  0 siblings, 1 reply; 12+ messages in thread
From: Clément Pit-Claudel @ 2017-04-27 12:58 UTC (permalink / raw)
  To: emacs-devel

On 2017-04-27 08:48, Stefan Monnier wrote:
>> Of course, I could rewrite the queuing logic to be more resilient to this
>> kind of changes.  But the question is about ways to prevent edits, and
>> I think it's more general than this particular example.
>
> Then I woulnd't worry too much about it: the cases where the after-hook
> is called "outside" of the before hook's beg/end are basically bugs and
> IIUC there are no known remaining cases in Emacs-26 (IIRC there are some
> known cases in Emacs-25 where this occurs, mostly in
> insert-file-contents (i.e. revert-buffer)).

Got it, thanks.  I already have specialized code for revert-buffer, so that should be fine.
What happens if a deletion causes an overlay to evaporate? The undo list doesn't record that, does it? Or will the evaporation be delayed until all modification hooks have run?

Clément.



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

* Re: How can I undo a change from a modification-hook?
  2017-04-27 12:58                   ` Clément Pit-Claudel
@ 2017-04-27 17:36                     ` Stefan Monnier
  0 siblings, 0 replies; 12+ messages in thread
From: Stefan Monnier @ 2017-04-27 17:36 UTC (permalink / raw)
  To: emacs-devel

> What happens if a deletion causes an overlay to evaporate? The undo list
> doesn't record that, does it?

Indeed, the undo list doesn't record it currently, AFAIK.  It would
probably make sense to change that.


        Stefan




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

end of thread, other threads:[~2017-04-27 17:36 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-26  7:02 How can I undo a change from a modification-hook? Clément Pit--Claudel
2017-04-26 12:26 ` Stefan Monnier
2017-04-26 13:45   ` Clément Pit-Claudel
2017-04-26 13:52     ` Stefan Monnier
2017-04-26 14:23       ` Clément Pit-Claudel
2017-04-26 14:51         ` Stefan Monnier
2017-04-26 16:38           ` Clément Pit-Claudel
2017-04-26 18:43             ` Stefan Monnier
2017-04-26 19:02               ` Clément Pit-Claudel
2017-04-27 12:48                 ` Stefan Monnier
2017-04-27 12:58                   ` Clément Pit-Claudel
2017-04-27 17:36                     ` Stefan Monnier

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.