Command line 激活进程的自动tcl命令完成

Command line 激活进程的自动tcl命令完成,command-line,tcl,proc,Command Line,Tcl,Proc,我需要在我的tcl应用程序中为用户定义的tcl进程添加tcl命令完成 例如: tcl_shell> proc myproc {} {puts test} tcl_shell> my 按下tab键时,应完成myproc。基本思想是询问信息命令。如果在变量str中保持在制表符之前键入字符,则可以使用 info commands $str* 如果它返回多个匹配项,则您没有足够的字符串前缀来明确标识命令(您可能希望保留候选项列表,并让用户通过连续选项卡遍历它)。如果未获得任何匹配,

我需要在我的tcl应用程序中为用户定义的tcl进程添加tcl命令完成

例如:

tcl_shell> proc myproc {} {puts test}

tcl_shell> my  

按下tab键时,应完成myproc。

基本思想是询问
信息命令。如果在变量
str
中保持在制表符之前键入字符,则可以使用

info commands $str*
如果它返回多个匹配项,则您没有足够的字符串前缀来明确标识命令(您可能希望保留候选项列表,并让用户通过连续选项卡遍历它)。如果未获得任何匹配,则
str
中的文本不是任何已知命令的前缀。如果您得到一个匹配项,那么这就是要完成的命令名

如果您还想为名称空间完成,那么,
namespace children
是该操作的命令

例如:

proc findCommand str {
    set matches [info commands $str*]
    set ns [namespace qualifiers $str]
    set str [namespace tail $str]
    set nses [namespace children $ns $str*]
    foreach ns $nses {
        lappend matches {*}[info commands $ns\::*]
    }
    return $matches
}

% findCommand cl
clock close clear
% findCommand tcl::cl
::tcl::clock::GetJulianDayFromEraYearWeekDay ::tcl::clock::Oldscan ::tcl::clock::ParseFormatArgs ::tcl::clock::GetDateFields ::tcl::clock::microseconds ::tcl::clock::getenv ::tcl::clock::clicks ::tcl::clock::GetJulianDayFromEraYearMonthDay ::tcl::clock::milliseconds ::tcl::clock::ConvertLocalToUTC ::tcl::clock::seconds
文件: ,

ETA
tcl::prefix

多纳尔·费罗斯在评论中提到。它的功能大致相同,但使用起来有点复杂。允许完成的完整列表必须作为参数传递给它,这一方面意味着您必须收集它;另一方面,这意味着您可以根据需要限制允许的完成集,或者允许不存在的命令名(由
unknown
处理)。如果字符串与其中任何一个都不匹配,它还可以向用户提示完成列表。示例——用户输入“foo”和命令列表是以“b”或“c”开头的全局命令:


tcl::prefix
命令(8.6或更高版本)对这类事情很有帮助。感谢您的回答,它为我提供了过程列表。但是,您能告诉我如何绑定findCommand函数,以便在用户按下时执行吗?@sampathA:
findCommand
示例只是一个基本的演示,并不是真正有用的处理程序。实际的处理程序可能使用
findCommand
作为帮助程序。不管怎样,我不是Tk方面的专家,但似乎您会绑定到用作控制台的小部件或其类:
bind X completionhandler
,其中
X
是小部件路径名或类名,而
completionhandler
是一个回调函数,它知道控制台的状态,并有自己的逻辑(或类似于
findCommand
)来识别命令。
::tcl::prefix match -message command $cmds $str
bad command "foo": must be bgerror, binary, break, case, catch, cd, chan, clear, clock, close, concat, continue, or coroutine