unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* guaranteed undo boundaries
@ 2013-02-07  2:53 Roland Winkler
  2013-02-07 13:45 ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Roland Winkler @ 2013-02-07  2:53 UTC (permalink / raw)
  To: emacs-devel

Is it possible to set up a command or function foo such that one may
rely upon the fact that all changes performed by foo in a buffer
when foo is called once, will be undone calling undo exactly once in
this buffer?

I know the function undo-boundary. I also know the macro
atomic-change-group. Yet in both cases I am not sure from the
docstring / the elisp manual whether one may rely on the fact that
each call of foo gives exactly one undo boundary.

The context of my question is BBDB. Here it is crucial that each
change to the buffer visiting the database must be carried out such
that the database remains in a well-defined state. Half a change
gives a corrupted database. Therefore, each function performing any
such changes in this buffer is doing its job such that when the
function has completed its job BBDB is again in a well-defined
state. (For modifying the buffer visiting the database, these
functions only use insert, insert-before-markers, delete-region and
delete-char.)

Yet currently BBDB does not provide any possibility to undo changes
in its database (meaning first of all: undoing changes in the buffer
visiting the database, which then need to propagate back into BBDB).
I thought that I could add at least some rudimentary undo feature if
each function that changes the database gives exactly one boundary
in buffer-undo-list. So if one undoes changes of the buffer one by
one, the database always remains in a well-defined state.

Possibly this is already guaranteed, merely this could be spelled out
more explicitly in some docstring / the elisp manual. Possibly, I am
missing something else.

Roland

PS: A proper command bbdb-undo will require yet more than the above.
Please do not expect that such a feature will find its way into BBDB
in the near future.



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

* Re: guaranteed undo boundaries
  2013-02-07  2:53 guaranteed undo boundaries Roland Winkler
@ 2013-02-07 13:45 ` Stefan Monnier
  2013-02-07 14:52   ` Roland Winkler
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2013-02-07 13:45 UTC (permalink / raw)
  To: Roland Winkler; +Cc: emacs-devel

> Possibly this is already guaranteed, merely this could be spelled out
> more explicitly in some docstring / the elisp manual. Possibly, I am
> missing something else.

By default, undo boundaries are pushed after each command run by
the user.  But every command/function is free to adjust this behavior by
calling undo-boundary internally (or by removing the boundary, as does
self-insert-command).

So there's no such guarantee.
What you can do, OTOH, is something like

    (undo-boundary)
    (let ((bottom buffer-undo-list))
      <do-db-update>
      (remove-all-boundaries-between buffer-undo-list bottom))


-- Stefan



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

* Re: guaranteed undo boundaries
  2013-02-07 13:45 ` Stefan Monnier
@ 2013-02-07 14:52   ` Roland Winkler
  2013-02-07 16:28     ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Roland Winkler @ 2013-02-07 14:52 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

On Thu Feb 7 2013 Stefan Monnier wrote:
> > Possibly this is already guaranteed, merely this could be spelled out
> > more explicitly in some docstring / the elisp manual. Possibly, I am
> > missing something else.
> 
> By default, undo boundaries are pushed after each command run by
> the user.  But every command/function is free to adjust this behavior by
> calling undo-boundary internally (or by removing the boundary, as does
> self-insert-command).
> 
> So there's no such guarantee.
> What you can do, OTOH, is something like
> 
>     (undo-boundary)
>     (let ((bottom buffer-undo-list))
>       <do-db-update>
>       (remove-all-boundaries-between buffer-undo-list bottom))

...where I need to come up with some sensible code for a new
function remove-all-boundaries-between.

Thanks, that strategy makes sense!

Roland



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

* Re: guaranteed undo boundaries
  2013-02-07 14:52   ` Roland Winkler
@ 2013-02-07 16:28     ` Stefan Monnier
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2013-02-07 16:28 UTC (permalink / raw)
  To: Roland Winkler; +Cc: emacs-devel

> ...where I need to come up with some sensible code for a new
> function remove-all-boundaries-between.

Something like:

  (let ((list buffer-undo-list))
    (while (and (cdr list) (not (eq (cdr list) bottom)))
      (if (null (cadr list))
          (setcdr list (cddr list))
        (setq list (cdr list)))))

-- Stefan



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

end of thread, other threads:[~2013-02-07 16:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-07  2:53 guaranteed undo boundaries Roland Winkler
2013-02-07 13:45 ` Stefan Monnier
2013-02-07 14:52   ` Roland Winkler
2013-02-07 16:28     ` Stefan Monnier

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