From: Jeff Hobbs Date: Tue, 9 Sep 2014 10:46:15 +0000 (+0000) Subject: * tkcon.tcl (::tkcon::SaveHistory): save history at each command X-Git-Url: http://privyetmir.co.uk/gitweb.cgi?a=commitdiff_plain;h=d67cf46373d8aab638040e653ae84e5b6d8425de;p=tkcon * tkcon.tcl (::tkcon::SaveHistory): save history at each command to prevent loss during abnormal termination. [bachmann] --- diff --git a/ChangeLog b/ChangeLog index 666ca94..8900970 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2014-09-09 Jeff Hobbs + + * tkcon.tcl (::tkcon::SaveHistory): save history at each command + to prevent loss during abnormal termination. [bachmann] + 2014-07-09 Jeff Hobbs * tkcon.tcl (idebug): allow multi-char patterns as debug id [Lama] diff --git a/tkcon.tcl b/tkcon.tcl index 09fd9f4..f3ad06f 100755 --- a/tkcon.tcl +++ b/tkcon.tcl @@ -1121,6 +1121,10 @@ proc ::tkcon::AddSlaveHistory cmd { set code [catch {EvalSlave history event $ev} lastCmd] if {$code || $cmd ne $lastCmd} { EvalSlave history add $cmd + # Save history every time so it's not lost in case of an abnormal termination. + # Do not warn in case of an error: we don't want an error message + # after each command if the history file is not writable. + catch {SaveHistory} } } @@ -2600,30 +2604,40 @@ proc ::tkcon::MainInit {} { } proc ::exit args { if {$::tkcon::OPT(usehistory)} { - if {[catch {open $::tkcon::PRIV(histfile) w} fid]} { - puts stderr "unable to save history file:\n$fid" + if {[catch {::tkcon::SaveHistory} msg]} { + puts stderr "unable to save history file:\n$msg" # pause a moment, because we are about to die finally... after 1000 - } else { - set max [::tkcon::EvalSlave history nextid] - set id [expr {$max - $::tkcon::OPT(history)}] - if {$id < 1} { set id 1 } - ## FIX: This puts history in backwards!! - while {($id < $max) && ![catch \ - {::tkcon::EvalSlave history event $id} cmd]} { - if {$cmd ne ""} { - puts $fid "::tkcon::EvalSlave\ - history add [list $cmd]" - } - incr id - } - close $fid } } uplevel 1 ::tkcon::FinalExit $args } } + ## ::tkcon::SaveHistory - saves history to history file + ## If the history file is not writable it raises an error + proc ::tkcon::SaveHistory {} { + if {$::tkcon::OPT(usehistory)} { + if {[catch {open $::tkcon::PRIV(histfile) w} fid]} { + error $fid + } else { + set max [::tkcon::EvalSlave history nextid] + set id [expr {$max - $::tkcon::OPT(history)}] + if {$id < 1} { set id 1 } + ## FIX: This puts history in backwards!! + while {($id < $max) && ![catch \ + {::tkcon::EvalSlave history event $id} cmd]} { + if {$cmd ne ""} { + puts $fid "::tkcon::EvalSlave\ + history add [list $cmd]" + } + incr id + } + close $fid + } + } + } + ## ::tkcon::InterpEval - passes evaluation to another named interpreter ## If the interpreter is named, but no args are given, it returns the ## [tk appname] of that interps master (not the associated eval slave).