This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,33 @@
# Define the history machinery
proc histvar {varName operation} {
upvar 1 $varName v ___history($varName) history
switch -- $operation {
start {
set history {}
if {[info exist v]} {
lappend history $v
}
trace add variable v write [list histvar.write $varName]
trace add variable v read [list histvar.read $varName]
}
list {
return $history
}
undo {
set history [lrange $history 0 end-1]
}
stop {
unset history
trace remove variable v write [list histvar.write $varName]
trace remove variable v read [list histvar.read $varName]
}
}
}
proc histvar.write {key varName args} {
upvar 1 $varName v ___history($key) history
lappend history $v
}
proc histvar.read {key varName args} {
upvar 1 $varName v ___history($key) history
set v [lindex $history end]
}

View file

@ -0,0 +1,13 @@
# Enable history for foo
histvar foo start
set foo {a b c d}
set foo 123
set foo "quick brown fox"
puts $foo
puts foo-history=[join [histvar foo list] ", "]
puts $foo
histvar foo undo
puts $foo
histvar foo undo
puts $foo
histvar foo stop