# Description: enhanced command line history procs HistoryUp HistoryDown Command proc ShowHistory {window dir} { global history after cancel [namespace current]::idle after 2000 [namespace current]::idle wm geometry .ht [winfo width $window]x[winfo reqheight .ht]+[winfo rootx $window]+[expr [winfo rooty $window] - [winfo reqheight .ht]] wm geometry .hb [winfo width $window]x[winfo reqheight .hb]+[winfo rootx $window]+[expr [winfo rooty $window] + [winfo height $window]] foreach w {.ht .hb} { wm deiconify $w raise $w foreach x {1 2 3} {$w.$x delete 0 end} } set window [winfo toplevel $window] if {$dir == "up"} {set cur $history($window,cur)} if {$dir == "down"} {set cur [expr $history($window,cur) - 2]} if {$cur < -2} {set cur -2} if {$cur > [llength $history($window,list)] - 2} {set cur [expr [llength $history($window,list)] - 2]} .ht.1 insert 0 [lindex $history($window,list) [expr $cur + 4]] .ht.2 insert 0 [lindex $history($window,list) [expr $cur + 3]] .ht.3 insert 0 [lindex $history($window,list) [expr $cur + 2]] .hb.1 insert 0 [lindex $history($window,list) $cur] .hb.2 insert 0 [lindex $history($window,list) [expr $cur - 1]] .hb.3 insert 0 [lindex $history($window,list) [expr $cur - 2]] } proc HistoryUp {window} { global history ShowHistory $window up set top [winfo toplevel $window] if {[lindex $history($top,list) [expr {$history($top,cur) + 1}]] != ""} { if {$history($top,cur) == -1} { set history($top,tmp) [$window get] } $window delete 0 end incr history($top,cur) $window insert end [lindex $history($top,list) $history($top,cur)] } else { bell } } proc HistoryDown {window} { global history ShowHistory $window down set top [winfo toplevel $window] if {$history($top,cur) != -1} { $window delete 0 end if {$history($top,cur) == 0} { $window insert end $history($top,tmp) set history($top,cur) -1 } else { incr history($top,cur) -1 $window insert end [lindex $history($top,list) $history($top,cur)] } } else { bell } } proc Command {window} { global away prefs autoaway line wm withdraw .ht wm withdraw .hb update idletasks after cancel autoaway if {!$away && $prefs(autoaway) > 0} {after [expr $prefs(autoaway) * 60000] autoaway} if {$away && ($prefs(autounaway) == 1 || ($prefs(autounaway) == 2 && [info exists autoaway]))} {Send "AWAY"} if {[set line [$window get]] == ""} { unset line return } $window delete 0 end set window [winfo toplevel $window] AddToHistory $window $line set command [trim [lindex [split $line] 0] /] if {[string index $line 0] == "/"} { set line [string range $line [expr [string length $command] + 2] end] if {[info commands command_$command] != ""} { command_$command $window $line } else { set tmp [info commands command_$command*] switch [llength $tmp] { 1 {$tmp $window $line} 0 {Echo $window "\[ error \] Unknown command /$command $line" {error default}} default {Echo $window "\[ info \] Ambigous command /$command $line" {info default}} } } } else { event generate $window <> } catch {unset line} } proc idle {} { wm withdraw .ht wm withdraw .hb } proc CreateWindows {} { catch {bind .ht.1 {}} foreach x {.ht .hb} { catch {destroy $x} toplevel $x -bd 2 -relief sunken -class Dialog foreach e {1 2 3} {entry $x.$e -bd 0 -highlightthickness 0} pack $x.1 $x.2 $x.3 -side top -fill x wm overrideredirect $x 1 wm withdraw $x wm title $x "" } bind .ht.1 [list after idle [namespace current]::CreateWindows] } proc unload {} { bind .ht.1 {} destroy .ht destroy .hb bind cmdline {} } CreateWindows bind cmdline [namespace current]::idle