【问题标题】:Activating auto tcl command completion for procs为 procs 激活自动 tcl 命令完成
【发布时间】:2018-01-12 10:14:20
【问题描述】:

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

例如:

tcl_shell> proc myproc {} {puts test}

tcl_shell> my  

当按下 tab 时,它应该完成 myproc。

【问题讨论】:

    标签: command-line tcl proc


    【解决方案1】:

    基本思路是问info commands。如果您在变量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
    

    文档: info, namespace

    预计到达时间tcl::prefix

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

    ::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
    

    【讨论】:

    • tcl::prefix 命令(8.6 或更高版本)对这类事情很有帮助。
    • 感谢您的回答,它给了我 procs 列表。但是你能告诉我如何绑定 findCommand 函数以在用户按下 时执行吗?
    • @sampathA:findCommand 示例是一个基本演示,作为处理程序并不是很有用。实际的处理程序可能会使用findCommand 作为助手。无论如何,我不是 Tk 方面的专家,但您似乎可以通过以下方式绑定到您用作控制台的小部件或其类:bind X <Key-Tab> completitionHandler,其中X 是小部件路径名或类名,completitionHandler 是一个回调,它知道控制台的状态,并有自己的逻辑(或类似于findCommand 的助手)来识别命令。
    猜你喜欢
    • 2014-05-22
    • 2011-10-06
    • 2011-12-17
    • 2021-12-11
    • 2021-05-27
    • 2012-03-09
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多