4 # This file defines the command dialog procedure.
6 # Copyright (c) 1995-1996 Christian Werner
8 # See the file "license.terms" for information on usage and redistribution
9 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 # Create command window e.g. for interactive use of cwsh
14 proc ckCommand {{w .ckCommand}} {
16 if [winfo exists $w] {
20 toplevel $w -class CommandDialog \
21 -border { ulcorner hline urcorner vline lrcorner hline llcorner vline }
22 place $w -relx 0.5 -rely 0.5 -relwidth 0.5 -relheight 0.5 -anchor center
24 label $w.title -text "Command dialog"
25 place $w.title -y 0 -relx 0.5 -bordermode ignore -anchor center
28 frame $w.sep0 -border hline -height 1
29 scrollbar $w.scroll -command "$w.output yview" -takefocus 0
30 text $w.output -yscrollcommand "$w.scroll set"
31 frame $w.sep1 -border hline -height 1
32 button $w.close -command "lower $w" -text Dismiss
34 pack $w.entry -side top -fill x
35 pack $w.sep0 -side top -fill x
36 pack $w.close -side bottom -ipadx 1
37 pack $w.sep1 -side bottom -fill x
38 pack $w.scroll -side right -fill y
39 pack $w.output -side left -fill both -expand 1
41 bind $w.entry <Return> "ckCommandRun $w"
42 bind $w.entry <Linefeed> "ckCommandRun $w"
43 bind $w.entry <Up> "ckCmdHist $w 1"
44 bind $w.entry <Down> "ckCmdHist $w -1"
45 bind $w.output <Tab> {focus [ck_focusNext %W] ; break}
46 bind $w.output <Control-X> "ckCommandRun $w \[$w.output get 1.0 end\]"
47 bind $w <Escape> "lower $w ; break"
48 bind $w <Control-U> "ckCmdToggleSize $w"
49 bind $w <Control-L> {update screen}
53 set ckPriv(cmdHistory) {}
54 set ckPriv(cmdHistCnt) -1
55 set ckPriv(cmdHistMax) 32
58 proc ckCmdToggleSize w {
59 if {[string first "-relwidth 1" [place info $w]] >= 0} {
60 place $w -relx 0.5 -rely 0.5 -relwidth 0.5 -relheight 0.5 \
63 place $w -relx 0.5 -rely 0.5 -relwidth 1.0 -relheight 1.0 \
68 proc ckCmdHist {w dir} {
70 incr ckPriv(cmdHistCnt) $dir
71 if {$ckPriv(cmdHistCnt) < 0} {
73 set ckPriv(cmdHistCnt) -1
75 if {$ckPriv(cmdHistCnt) >= [llength $ckPriv(cmdHistory)]} {
76 set ckPriv(cmdHistCnt) [expr [llength $ckPriv(cmdHistory)] - 1]
79 set cmd [lindex $ckPriv(cmdHistory) $ckPriv(cmdHistCnt)]
82 $w.entry insert end $cmd
85 proc ckCommandRun {w {cmd {}}} {
86 global errorInfo ckPriv
88 set cmd [string trim [$w.entry get]]
93 set code [catch {uplevel #0 $cmd} result]
95 set ckPriv(cmdHistory) [lrange [concat [list $cmd] \
96 $ckPriv(cmdHistory)] 0 $ckPriv(cmdHistMax)]
97 set ckPriv(cmdHistCnt) -1
99 $w.output delete 1.0 end
100 $w.output insert 1.0 $result
101 if $code { $w.output insert end "\n----\n$errorInfo" }
102 $w.output mark set insert 1.0
104 $w.entry delete 0 end