unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to determine buffer change
@ 2014-04-05 11:41 Thorsten Jolitz
  2014-04-05 11:49 ` Thorsten Jolitz
  2014-04-05 11:59 ` Eli Zaretskii
  0 siblings, 2 replies; 7+ messages in thread
From: Thorsten Jolitz @ 2014-04-05 11:41 UTC (permalink / raw)
  To: help-gnu-emacs


Hi List, 

in a program I insert text in an empty temporary edit-buffer, what of
course modifies the `buffer-undo-list'.
Assume that at this moment (lets call it time T1)

,------------------------------
| M-: (length buffer-undo-list)
`------------------------------

returns 9. 

Now when there are changes to the text in the edit-buffer, these are
copied to the original buffer after quitting. But when nothing has
changed after the insertion of the original text, it would be better to
simply discard the edit-buffer and not touch the original buffer at
all. 

I could remember the length of the buffer-undo-list at time T1 and then
check if it changed when quitting, but this does not seem very robust,
since this length grows and shrinks in both directions.

I would rather store the whole buffer-undo-list in a VAR1 at time T1,
and then again in VAR2 at time T2 (when quitting), and then do

,---------------
| (eq VAR1 VAR2)
`---------------

to figure out if any edits happened. 

Would this be robust enough? Is it highly unlikely or even impossible
for the buffer-undo-list to return to the same state/value after some
do's and undo's in the edit-buffer?

Is there maybe another simple trick to achieve what I want?

-- 
cheers,
Thorsten





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

* Re: How to determine buffer change
  2014-04-05 11:41 How to determine buffer change Thorsten Jolitz
@ 2014-04-05 11:49 ` Thorsten Jolitz
  2014-04-05 11:59 ` Eli Zaretskii
  1 sibling, 0 replies; 7+ messages in thread
From: Thorsten Jolitz @ 2014-04-05 11:49 UTC (permalink / raw)
  To: help-gnu-emacs

Thorsten Jolitz <tjolitz@gmail.com> writes:

> I would rather store the whole buffer-undo-list in a VAR1 at time T1,
> and then again in VAR2 at time T2 (when quitting), and then do
>
> ,---------------
> | (eq VAR1 VAR2)
> `---------------
>
> to figure out if any edits happened. 

I meant (equal ...) of course

-- 
cheers,
Thorsten




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

* Re: How to determine buffer change
  2014-04-05 11:41 How to determine buffer change Thorsten Jolitz
  2014-04-05 11:49 ` Thorsten Jolitz
@ 2014-04-05 11:59 ` Eli Zaretskii
  2014-04-05 12:52   ` Thorsten Jolitz
  2014-04-05 14:00   ` Stefan Monnier
  1 sibling, 2 replies; 7+ messages in thread
From: Eli Zaretskii @ 2014-04-05 11:59 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Thorsten Jolitz <tjolitz@gmail.com>
> Date: Sat, 05 Apr 2014 13:41:41 +0200
> 
> in a program I insert text in an empty temporary edit-buffer, what of
> course modifies the `buffer-undo-list'.
> Assume that at this moment (lets call it time T1)
> 
> ,------------------------------
> | M-: (length buffer-undo-list)
> `------------------------------
> 
> returns 9. 
> 
> Now when there are changes to the text in the edit-buffer, these are
> copied to the original buffer after quitting. But when nothing has
> changed after the insertion of the original text, it would be better to
> simply discard the edit-buffer and not touch the original buffer at
> all. 
> 
> I could remember the length of the buffer-undo-list at time T1 and then
> check if it changed when quitting, but this does not seem very robust,
> since this length grows and shrinks in both directions.

Did you try using buffer-modified-tick?



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

* Re: How to determine buffer change
  2014-04-05 11:59 ` Eli Zaretskii
@ 2014-04-05 12:52   ` Thorsten Jolitz
  2014-04-05 13:23     ` Thorsten Jolitz
  2014-04-05 14:00   ` Stefan Monnier
  1 sibling, 1 reply; 7+ messages in thread
From: Thorsten Jolitz @ 2014-04-05 12:52 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Thorsten Jolitz <tjolitz@gmail.com>
>> Date: Sat, 05 Apr 2014 13:41:41 +0200
>> 
>> in a program I insert text in an empty temporary edit-buffer, what of
>> course modifies the `buffer-undo-list'.
>> Assume that at this moment (lets call it time T1)
>> 
>> ,------------------------------
>> | M-: (length buffer-undo-list)
>> `------------------------------
>> 
>> returns 9. 
>> 
>> Now when there are changes to the text in the edit-buffer, these are
>> copied to the original buffer after quitting. But when nothing has
>> changed after the insertion of the original text, it would be better to
>> simply discard the edit-buffer and not touch the original buffer at
>> all. 
>> 
>> I could remember the length of the buffer-undo-list at time T1 and then
>> check if it changed when quitting, but this does not seem very robust,
>> since this length grows and shrinks in both directions.
>
> Did you try using buffer-modified-tick?

Thats it probably, thanks.

Still have to figure out when to store the ticks - although I think I
set them directly after edit-buffer setup and directly before
edit-buffer quitting, the tick values differ quite a lot although no
editing took place, e.g.:

,---------------------
| tick1: 48 tick2: 97
| tick1: 76 tick2: 229
`---------------------

-- 
cheers,
Thorsten




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

* Re: How to determine buffer change
  2014-04-05 12:52   ` Thorsten Jolitz
@ 2014-04-05 13:23     ` Thorsten Jolitz
  0 siblings, 0 replies; 7+ messages in thread
From: Thorsten Jolitz @ 2014-04-05 13:23 UTC (permalink / raw)
  To: help-gnu-emacs

Thorsten Jolitz <tjolitz@gmail.com> writes:

> Eli Zaretskii <eliz@gnu.org> writes:
>
>>> From: Thorsten Jolitz <tjolitz@gmail.com>
>>> Date: Sat, 05 Apr 2014 13:41:41 +0200
>>> 
>>> in a program I insert text in an empty temporary edit-buffer, what of
>>> course modifies the `buffer-undo-list'.
>>> Assume that at this moment (lets call it time T1)
>>> 
>>> ,------------------------------
>>> | M-: (length buffer-undo-list)
>>> `------------------------------
>>> 
>>> returns 9. 
>>> 
>>> Now when there are changes to the text in the edit-buffer, these are
>>> copied to the original buffer after quitting. But when nothing has
>>> changed after the insertion of the original text, it would be better to
>>> simply discard the edit-buffer and not touch the original buffer at
>>> all. 
>>> 
>>> I could remember the length of the buffer-undo-list at time T1 and then
>>> check if it changed when quitting, but this does not seem very robust,
>>> since this length grows and shrinks in both directions.
>>
>> Did you try using buffer-modified-tick?
>
> Thats it probably, thanks.
>
> Still have to figure out when to store the ticks - although I think I
> set them directly after edit-buffer setup and directly before
> edit-buffer quitting, the tick values differ quite a lot although no
> editing took place, e.g.:
>
> ,---------------------
> | tick1: 48 tick2: 97
> | tick1: 76 tick2: 229
> `---------------------

Actually I have this as last expression in the edit-buffer set-up
function: 

#+begin_src emacs-lisp
  (defun tj/setup-edit-buffer ()
    ;; [...]
    ;; store buffer-modified-tick at time T1
    (setq edit-buffer-modified-tick-1
          (buffer-modified-tick)))
#+end_src

and this as first expression in the edit-buffer quit function:

#+begin_src emacs-lisp
    (defun tj/quit-edit-buffer ()
    ;; store buffer-modified-tick at time T2
    (setq edit-buffer-modified-tick-2 (buffer-modified-tick))
    (if (eq edit-buffer-modified-tick-1
            edit-buffer-modified-tick-2)
        (progn
          ;; [...]
          )))
#+end_src

and I did nothing else but calling and quitting the edit-buffer,
however ticks have almost doubled in numbers in between:

,--------------------
| tick1: 48 tick2: 97
`--------------------

Can that be related to buffer fontifying or other stuff that happens
lazily with a bit lag to the initial buffer set-up?

-- 
cheers,
Thorsten




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

* Re: How to determine buffer change
  2014-04-05 11:59 ` Eli Zaretskii
  2014-04-05 12:52   ` Thorsten Jolitz
@ 2014-04-05 14:00   ` Stefan Monnier
  2014-04-05 14:54     ` Thorsten Jolitz
  1 sibling, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2014-04-05 14:00 UTC (permalink / raw)
  To: help-gnu-emacs

>> in a program I insert text in an empty temporary edit-buffer, what of
>> course modifies the `buffer-undo-list'.
>> Assume that at this moment (lets call it time T1)
>> 
>> ,------------------------------
>> | M-: (length buffer-undo-list)
>> `------------------------------
>> 
>> returns 9. 
>> 
>> Now when there are changes to the text in the edit-buffer, these are
>> copied to the original buffer after quitting. But when nothing has
>> changed after the insertion of the original text, it would be better to
>> simply discard the edit-buffer and not touch the original buffer at
>> all. 
>> 
>> I could remember the length of the buffer-undo-list at time T1 and then
>> check if it changed when quitting, but this does not seem very robust,
>> since this length grows and shrinks in both directions.

> Did you try using buffer-modified-tick?

Indeed, I'd either use buffer-chars-modified-tick, or set
buffer-undo-list to nil (after all, so you really want the user to be
able to un-insert the initial text?).


        Stefan




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

* Re: How to determine buffer change
  2014-04-05 14:00   ` Stefan Monnier
@ 2014-04-05 14:54     ` Thorsten Jolitz
  0 siblings, 0 replies; 7+ messages in thread
From: Thorsten Jolitz @ 2014-04-05 14:54 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>> in a program I insert text in an empty temporary edit-buffer, what of
>>> course modifies the `buffer-undo-list'.
>>> Assume that at this moment (lets call it time T1)
>>> 
>>> ,------------------------------
>>> | M-: (length buffer-undo-list)
>>> `------------------------------
>>> 
>>> returns 9. 
>>> 
>>> Now when there are changes to the text in the edit-buffer, these are
>>> copied to the original buffer after quitting. But when nothing has
>>> changed after the insertion of the original text, it would be better to
>>> simply discard the edit-buffer and not touch the original buffer at
>>> all. 
>>> 
>>> I could remember the length of the buffer-undo-list at time T1 and then
>>> check if it changed when quitting, but this does not seem very robust,
>>> since this length grows and shrinks in both directions.
>
>> Did you try using buffer-modified-tick?

> set buffer-undo-list to nil (after all, so you really want the user to
> be able to un-insert the initial text?).

No, I don't, and the simplest solutions are often the best but don't
come to ones mind first. That works like a charm, thanks. 

-- 
cheers,
Thorsten




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

end of thread, other threads:[~2014-04-05 14:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-05 11:41 How to determine buffer change Thorsten Jolitz
2014-04-05 11:49 ` Thorsten Jolitz
2014-04-05 11:59 ` Eli Zaretskii
2014-04-05 12:52   ` Thorsten Jolitz
2014-04-05 13:23     ` Thorsten Jolitz
2014-04-05 14:00   ` Stefan Monnier
2014-04-05 14:54     ` Thorsten Jolitz

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