Home > Archive > Tcl > June 2005 > Range of characters changed by edit undo
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Range of characters changed by edit undo
|
|
| Francois Vogel 2005-06-04, 8:57 pm |
| Hi all,
I'm making use of the edit undo capability of the text widget.
Iwould like to know which range(s) of characters were modified by an undo.
In other words, I would like to know at which index in the text the internal
undo stuff started to insert or delete characters, and at which index it
stopped.
I tried to use a tag the following way (simplified, should work only to undo
a delete near the middle of the text):
$w tag add unchanged 1.0 end
event generate $w <<Undo>>
set range1 [$w tag nextrange unchanged 1.0]
set i1 [lindex $range1 1]
set range2 [$w tag nextrange unchanged $i1]
set i2 [lindex $range2 0]
$w tag remove unchanged 1.0 end
# here I do my stuff with the characters that were changed, e.g. colorize
them, and I thought they would be located between $i1 and $i2
But this does not work since apparently the characters inserted by undo also
has the "unchanged" tag (surprising!), which ends up with $range1 being the
full text and $range2 being empty.
What else could I do?
It would be nice if [.t edit undo] would return this range, but maybe there
is another way.
Thanks for any hint,
Francois
| |
| Francois Vogel 2005-06-05, 8:56 am |
| Hmm, I found the following implementation, that seems to work:
set bef [$w get 1.0 end]
event generate $w <<Undo>>
set aft [$w get 1.0 end]
set pref [commonPrefix $bef $aft]
set i1 [$w index "1.0 + [string length $pref] chars"]
set rbef [srevert $bef]
set raft [srevert $aft]
set pref [commonPrefix $rbef $raft]
set i2 [$w index "end - [string length $pref] chars"]
# here colorization between $i1 and $i2
Procs srevert and commonPrefix can be found at http://wiki.tcl.tk/44
(additional string functions).
I was a bit afraid wrt performance, but it seems to be quite reasonable even
for large (15000 lines) text widget content.
If anybody has a better idea, please share.
Francois
|
|
|
|
|