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 " ;# added complementary blank } elseif {[llength $cmds]>1} { set info [lsort $cmds] ;# make choices available for display set var [longestCommonPrefix $cmds] ;# partial completion (1) } else bell } }
# (1): longestCommonPrefix is available on Additional string functions
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 -fill x pack .t -fill both -expand 1
Further extensions that one could add:
Use tclreadline to enable TAB command, variable and arguments complition in your interpreter. For tclsh put to your ~/.tclshrc (you MUST replace ^A, ^B and ^ with corresponding Control-character, use C-q C-a, etc in [emacs editor):
if {$tcl_interactive} { catch { package require tclreadline namespace eval tclreadline { if ([regexp "^(xterm|eterm-color)" "$::env(TERM)"]) { proc prompt1 {} { return "^A^[\[0;31m^[\[1m^Btcl>^A^[\[0m^B " } proc prompt2 {} { return "^A^[\[0;31m^B...> ^A^[\[0m^B" } } { proc prompt1 {} { return "tcl> " } proc prompt2 {} { return "...> " } } } ::tclreadline::Loop } }
tkcon has routines for expanding pathnames, command names and variable names (::tkcon::Expand* in the code). These have also been carried over into the updated console of the Tcl core.
a variant with instant completion Command completion in the iFile console