unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* undoing changes automatically
@ 2007-11-12  1:13 Roland Winkler
  2007-11-12 10:07 ` Johan Bockgård
  2007-11-12 20:12 ` David Kastrup
  0 siblings, 2 replies; 16+ messages in thread
From: Roland Winkler @ 2007-11-12  1:13 UTC (permalink / raw)
  To: emacs-devel

Who can please explain to me the following?

bibtex.el contains the function bibtex-format-entry that performs
various formatting operations on a BibTeX entry. Along the way, it
also checks in several ways whether syntax and content of the entry
are correct. It throws an error if any of these checks indicates
that something is incorrect. But this happens while it is in the
midst of its formatting job. This is very confusing because it
leaves the buffer in an odd state. One always needs to do an `undo'
after such an error message.

So I want to put the call to undo in the code such that it undoes
all the formatting before it throws the error.

However, one thing confuses me: a simple call of `undo' is not
enough. Instead, I need to call first `undo-boundary'. Why does
`undo' ignore the latest entries in buffer-undo-list past (i.e.,
newer than) the last undo boundary? Is this behavior intended? If
yes, is it documented somewhere?

Thanks,

Roland

PS: I am also a bit unhappy that using the buffer-undo-list for this
always makes this list grow, although there is no need to keep these
entries in buffer-undo-list if they are undone immediately. So is
there a cleaner way to achieve what I want? I have in mind something
like a (noninteractive) undo function that deletes those entries
from buffer-undo-list that are undone. (An alternative would be that
the code performed first all the error checking and did the
formatting afterwards. However, this would duplicate a lot of code
and also duplicate work done in bibtex-format-entry. So I would like
to avoid that.)

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

* Re: undoing changes automatically
  2007-11-12  1:13 undoing changes automatically Roland Winkler
@ 2007-11-12 10:07 ` Johan Bockgård
  2007-11-12 21:23   ` Leo
  2007-11-12 20:12 ` David Kastrup
  1 sibling, 1 reply; 16+ messages in thread
From: Johan Bockgård @ 2007-11-12 10:07 UTC (permalink / raw)
  To: emacs-devel

"Roland Winkler" <Roland.Winkler@physik.uni-erlangen.de> writes:

> So I want to put the call to undo in the code such that it undoes all
> the formatting before it throws the error.

In Emacs 22 this is easy

(info "(elisp) Atomic Changes")

-- 
Johan Bockgård

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

* Re: undoing changes automatically
  2007-11-12  1:13 undoing changes automatically Roland Winkler
  2007-11-12 10:07 ` Johan Bockgård
@ 2007-11-12 20:12 ` David Kastrup
  2007-11-12 21:21   ` Roland Winkler
  1 sibling, 1 reply; 16+ messages in thread
From: David Kastrup @ 2007-11-12 20:12 UTC (permalink / raw)
  To: Roland Winkler; +Cc: emacs-devel

"Roland Winkler" <Roland.Winkler@physik.uni-erlangen.de> writes:

> Who can please explain to me the following?
>
> bibtex.el contains the function bibtex-format-entry that performs
> various formatting operations on a BibTeX entry. Along the way, it
> also checks in several ways whether syntax and content of the entry
> are correct. It throws an error if any of these checks indicates
> that something is incorrect. But this happens while it is in the
> midst of its formatting job. This is very confusing because it
> leaves the buffer in an odd state. One always needs to do an `undo'
> after such an error message.
>
> So I want to put the call to undo in the code such that it undoes
> all the formatting before it throws the error.

atomic-change-group is a Lisp macro in `subr.el'.
(atomic-change-group &rest BODY)

Perform BODY as an atomic change group.
This means that if BODY exits abnormally,
all of its changes to the current buffer are undone.
This works regardless of whether undo is enabled in the buffer.

This mechanism is transparent to ordinary use of undo;
if undo is enabled in the buffer and BODY succeeds, the
user can undo the change normally.


-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

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

* Re: undoing changes automatically
  2007-11-12 20:12 ` David Kastrup
@ 2007-11-12 21:21   ` Roland Winkler
  2007-11-13 20:03     ` Richard Stallman
  0 siblings, 1 reply; 16+ messages in thread
From: Roland Winkler @ 2007-11-12 21:21 UTC (permalink / raw)
  To: David Kastrup; +Cc: emacs-devel

On Mon Nov 12 2007 David Kastrup wrote:
> > So I want to put the call to undo in the code such that it undoes
> > all the formatting before it throws the error.
> 
> atomic-change-group is a Lisp macro in `subr.el'.
> (atomic-change-group &rest BODY)
> 
> Perform BODY as an atomic change group.
> This means that if BODY exits abnormally,
> all of its changes to the current buffer are undone.
> This works regardless of whether undo is enabled in the buffer.

Perfect, thanks. It seems that this does exactly what I want.

Roland

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

* Re: undoing changes automatically
  2007-11-12 10:07 ` Johan Bockgård
@ 2007-11-12 21:23   ` Leo
  0 siblings, 0 replies; 16+ messages in thread
From: Leo @ 2007-11-12 21:23 UTC (permalink / raw)
  To: emacs-devel

On 2007-11-12 10:07 +0000, Johan Bockgård wrote:
> "Roland Winkler" <Roland.Winkler@physik.uni-erlangen.de> writes:
>
>> So I want to put the call to undo in the code such that it undoes all
>> the formatting before it throws the error.
>
> In Emacs 22 this is easy
>
> (info "(elisp) Atomic Changes")

This is nice.

-- 
.:  Leo  :.  [ sdl.web AT gmail.com ]  .:  [ GPG Key: 9283AA3F ]  :.

       Use the most powerful email client -- http://gnus.org/

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

* Re: undoing changes automatically
  2007-11-12 21:21   ` Roland Winkler
@ 2007-11-13 20:03     ` Richard Stallman
  2007-11-13 21:11       ` Stefan Monnier
                         ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Richard Stallman @ 2007-11-13 20:03 UTC (permalink / raw)
  To: Roland Winkler; +Cc: emacs-devel

I don't know if anyone has as yet used atomic-change-group.
I'd be interested to know if it actually works well for you.

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

* Re: undoing changes automatically
  2007-11-13 20:03     ` Richard Stallman
@ 2007-11-13 21:11       ` Stefan Monnier
  2007-11-15  5:06       ` Roland Winkler
  2008-01-09  1:51       ` Roland Winkler
  2 siblings, 0 replies; 16+ messages in thread
From: Stefan Monnier @ 2007-11-13 21:11 UTC (permalink / raw)
  To: rms; +Cc: emacs-devel, Roland Winkler

> I don't know if anyone has as yet used atomic-change-group.
> I'd be interested to know if it actually works well for you.

It's used in transpose, so at least in the case where there's no error
and few changes, it's well tested.

I also use it in smerge-mode, and it worked fine when I needed it.


        Stefan

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

* Re: undoing changes automatically
  2007-11-13 20:03     ` Richard Stallman
  2007-11-13 21:11       ` Stefan Monnier
@ 2007-11-15  5:06       ` Roland Winkler
  2008-01-09  1:51       ` Roland Winkler
  2 siblings, 0 replies; 16+ messages in thread
From: Roland Winkler @ 2007-11-15  5:06 UTC (permalink / raw)
  To: rms; +Cc: emacs-devel

On Tue Nov 13 2007 Richard Stallman wrote:
> I don't know if anyone has as yet used atomic-change-group.
> I'd be interested to know if it actually works well for you.

Up to now my tests suggests that it does nicely what it is supposed
to do.

Roland

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

* Re: undoing changes automatically
  2007-11-13 20:03     ` Richard Stallman
  2007-11-13 21:11       ` Stefan Monnier
  2007-11-15  5:06       ` Roland Winkler
@ 2008-01-09  1:51       ` Roland Winkler
  2008-01-09  5:24         ` Stefan Monnier
  2 siblings, 1 reply; 16+ messages in thread
From: Roland Winkler @ 2008-01-09  1:51 UTC (permalink / raw)
  To: rms; +Cc: emacs-devel

On Tue Nov 13 2007 Richard Stallman wrote:
> I don't know if anyone has as yet used atomic-change-group.
> I'd be interested to know if it actually works well for you.

While testing various error scenarios in bibtex.el that should be
handled by atomic-change-group, I once got the error message

  cancel-change-group: Undoing to some unrelated state

followed by

  cancel-change-group: Wrong type argument: listp, t

I wanted to debug this error, but I couldn't reproduce it.

I looked at the code of cancel-change-group. It's not clear to me
what "Undoing to some unrelated state" means. I'd expect that
atomic-change-group should not produce any errors by itself.

Roland

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

* Re: undoing changes automatically
  2008-01-09  1:51       ` Roland Winkler
@ 2008-01-09  5:24         ` Stefan Monnier
  2008-01-09  9:10           ` Roland Winkler
  0 siblings, 1 reply; 16+ messages in thread
From: Stefan Monnier @ 2008-01-09  5:24 UTC (permalink / raw)
  To: Roland Winkler; +Cc: rms, emacs-devel

>> I don't know if anyone has as yet used atomic-change-group.
>> I'd be interested to know if it actually works well for you.

> While testing various error scenarios in bibtex.el that should be
> handled by atomic-change-group, I once got the error message

>   cancel-change-group: Undoing to some unrelated state

> followed by

>   cancel-change-group: Wrong type argument: listp, t

> I wanted to debug this error, but I couldn't reproduce it.

> I looked at the code of cancel-change-group.  It's not clear to me
> what "Undoing to some unrelated state" means.  I'd expect that
> atomic-change-group should not produce any errors by itself.

I guess the message means that cancel-change-group couldn't find the
path (through undo entries) going from the current state to the
original state.  More specifically, it means that the current undo-log
does not contains as one of its nthcdr the head of the original
undo logs.  That might be the case if the changes to undo are too
numerous and the undo-log has been truncated to the point where the
undo-info needed to revert the change are not present any more.


        Stefan

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

* Re: undoing changes automatically
  2008-01-09  5:24         ` Stefan Monnier
@ 2008-01-09  9:10           ` Roland Winkler
  2008-01-09 10:11             ` Roland Winkler
  0 siblings, 1 reply; 16+ messages in thread
From: Roland Winkler @ 2008-01-09  9:10 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: rms, emacs-devel

On Wed Jan 9 2008 Stefan Monnier wrote:
> > I looked at the code of cancel-change-group.  It's not clear to me
> > what "Undoing to some unrelated state" means.  I'd expect that
> > atomic-change-group should not produce any errors by itself.
> 
> I guess the message means that cancel-change-group couldn't find the
> path (through undo entries) going from the current state to the
> original state.  More specifically, it means that the current undo-log
> does not contains as one of its nthcdr the head of the original
> undo logs.  That might be the case if the changes to undo are too
> numerous and the undo-log has been truncated to the point where the
> undo-info needed to revert the change are not present any more.

I thought of something like this, though I couldn't phrase it as
nicely and accurately as you did it  :-)

Inside atomic-change-group, bibtex-format-entry does its job for one
BibTeX entry at a time. So typically it operates on 10 to 15 lines
of plain text (~ 300 characters), and it adds about 100 elements to
the undo list. I'd expect that these are still sufficiently small
numbers for atomic-change-group.

I checked in the new code of bibtex.el that uses atomic-change-group.
So we'll see if anybody else runs into similar problems when using
bibtex-mode.

Roland

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

* Re: undoing changes automatically
  2008-01-09  9:10           ` Roland Winkler
@ 2008-01-09 10:11             ` Roland Winkler
  2008-01-09 15:07               ` Stefan Monnier
  0 siblings, 1 reply; 16+ messages in thread
From: Roland Winkler @ 2008-01-09 10:11 UTC (permalink / raw)
  To: Stefan Monnier, rms, emacs-devel

On Wed Jan 9 2008 Roland Winkler wrote:
> On Wed Jan 9 2008 Stefan Monnier wrote:
> > I guess the message means that cancel-change-group couldn't find the
> > path (through undo entries) going from the current state to the
> > original state.  More specifically, it means that the current undo-log
> > does not contains as one of its nthcdr the head of the original
> > undo logs.  That might be the case if the changes to undo are too
> > numerous and the undo-log has been truncated to the point where the
> > undo-info needed to revert the change are not present any more.
> 
> I thought of something like this, though I couldn't phrase it as
> nicely and accurately as you did it  :-)

If all this is true, and it is necessary to keep the error message
of cancel-chance-group in the code, then I'd be glad if this error
message could be replaced by something that is more transparent for
the average emacs user. I do not know what I could do with it except
for bringing it up here.

Roland

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

* Re: undoing changes automatically
  2008-01-09 10:11             ` Roland Winkler
@ 2008-01-09 15:07               ` Stefan Monnier
  2008-01-10 12:20                 ` Richard Stallman
  0 siblings, 1 reply; 16+ messages in thread
From: Stefan Monnier @ 2008-01-09 15:07 UTC (permalink / raw)
  To: Roland Winkler; +Cc: rms, emacs-devel

>> > I guess the message means that cancel-change-group couldn't find the
>> > path (through undo entries) going from the current state to the
>> > original state.  More specifically, it means that the current undo-log
>> > does not contains as one of its nthcdr the head of the original
>> > undo logs.  That might be the case if the changes to undo are too
>> > numerous and the undo-log has been truncated to the point where the
>> > undo-info needed to revert the change are not present any more.
>> 
>> I thought of something like this, though I couldn't phrase it as
>> nicely and accurately as you did it  :-)

> If all this is true, and it is necessary to keep the error message
> of cancel-chance-group in the code, then I'd be glad if this error
> message could be replaced by something that is more transparent for
> the average emacs user. I do not know what I could do with it except
> for bringing it up here.

If you can think of a better message, I'm all ears.

The "cancel-change-group: Wrong type argument: listp, t" error should be
fixed OTOH.  And it might indicate that the problem lies elsewhere:
maybe the undo was disabled somewhere within the atomic-change-group.


        Stefan

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

* Re: undoing changes automatically
  2008-01-09 15:07               ` Stefan Monnier
@ 2008-01-10 12:20                 ` Richard Stallman
  2008-01-10 14:43                   ` Stefan Monnier
  0 siblings, 1 reply; 16+ messages in thread
From: Richard Stallman @ 2008-01-10 12:20 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel, Roland.Winkler

If the failure mechanism is what you suggested, I think this patch is
called for.  What do you think?

*** subr.el	18 Nov 2007 17:58:04 -0500	1.554.2.7
--- subr.el	09 Jan 2008 23:26:01 -0500	
***************
*** 1802,1807 ****
--- 1802,1808 ----
    (let ((handle (make-symbol "--change-group-handle--"))
  	(success (make-symbol "--change-group-success--")))
      `(let ((,handle (prepare-change-group))
+ 	   (undo-outer-limit nil)
  	   (,success nil))
         (unwind-protect
  	   (progn

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

* Re: undoing changes automatically
  2008-01-10 12:20                 ` Richard Stallman
@ 2008-01-10 14:43                   ` Stefan Monnier
  2008-01-11 13:58                     ` Richard Stallman
  0 siblings, 1 reply; 16+ messages in thread
From: Stefan Monnier @ 2008-01-10 14:43 UTC (permalink / raw)
  To: rms; +Cc: emacs-devel, Roland.Winkler

> If the failure mechanism is what you suggested, I think this patch is
> called for.  What do you think?

Sounds good, especially if we add a comment next to it.


        Stefan


> *** subr.el	18 Nov 2007 17:58:04 -0500	1.554.2.7
> --- subr.el	09 Jan 2008 23:26:01 -0500	
> ***************
> *** 1802,1807 ****
> --- 1802,1808 ----
>     (let ((handle (make-symbol "--change-group-handle--"))
>   	(success (make-symbol "--change-group-success--")))
>       `(let ((,handle (prepare-change-group))
> + 	   (undo-outer-limit nil)
>   	   (,success nil))
>          (unwind-protect
>   	   (progn

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

* Re: undoing changes automatically
  2008-01-10 14:43                   ` Stefan Monnier
@ 2008-01-11 13:58                     ` Richard Stallman
  0 siblings, 0 replies; 16+ messages in thread
From: Richard Stallman @ 2008-01-11 13:58 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Roland.Winkler, emacs-devel

    > If the failure mechanism is what you suggested, I think this patch is
    > called for.  What do you think?

    Sounds good, especially if we add a comment next to it.

I will make it bind all the undo limit variables.

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

end of thread, other threads:[~2008-01-11 13:58 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-11-12  1:13 undoing changes automatically Roland Winkler
2007-11-12 10:07 ` Johan Bockgård
2007-11-12 21:23   ` Leo
2007-11-12 20:12 ` David Kastrup
2007-11-12 21:21   ` Roland Winkler
2007-11-13 20:03     ` Richard Stallman
2007-11-13 21:11       ` Stefan Monnier
2007-11-15  5:06       ` Roland Winkler
2008-01-09  1:51       ` Roland Winkler
2008-01-09  5:24         ` Stefan Monnier
2008-01-09  9:10           ` Roland Winkler
2008-01-09 10:11             ` Roland Winkler
2008-01-09 15:07               ` Stefan Monnier
2008-01-10 12:20                 ` Richard Stallman
2008-01-10 14:43                   ` Stefan Monnier
2008-01-11 13:58                     ` Richard Stallman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.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).