Version 0 of Command completion

Updated 2002-07-29 13:56:39

Richard Suchenwirth 2002-07-29 - The behavior of some shells (even including cmd.exe!) to complete commands or filenames on <Tab> is occasionally asked from Tcl too. Here is a quick shot of a mini-console with an entry, that does command completion, a label where multiple alternatives are shown, and a text widget where the command and its result (or error message) are displayed when <Return> was hit:


 proc complete {varName infoName} {
     upvar 1 $varName var
     upvar 1 $infoName info
     if {[llength $var]==1} {
         set cmds [info commands $var*]
         if {[llength $cmds]==1} {
             set var "$cmds "
         } elseif {[llength $cmds]>1} {
             set info [lsort $cmds]
         } else bell
     }
 }

 entry .e -textvar entry
 bind .e <Tab> {complete entry info; %W icursor end; break}
 bind .e <Return> {catch $entry res; .t insert end "%% $entry\n$res\n"}
 label .info -textvar info -anchor w -width 60
 text .t
 pack .e .info .t -fill x -expand 1

Arts and crafts of Tcl-Tk programming