2

什么 tcl 命令返回最后评估的命令?我认为它应该有类似 bash 历史的东西。

我试过这个,但它打印空字符串。

set aaa bbb
puts [history info]

我有创建 Tcl 解释器并使用它评估命令的 c++ 程序。在特定时刻,我需要知道最后评估的命令是什么。是否可以使用 Tcl 或 C 接口?

set a bbb
set b ccc
puts eee
set hh [history redo 1]

现在出现错误

event "1" hasn't occured yet
    while executing
"HistIndex $event"
    (procedure "tcl::HistRedo" line 6)
    invoked from within
"tcl::HistRedo [lindex $args 1]"
    (procedure "history" line 109)
    invoked from within
"history redo 1"
    invoked from within
"set hh [history redo 1]"
    (file "./a.itcl" line 17)
4

1 回答 1

3

使用history命令可以在 tclsh 中查看和操作以前输入的命令。这使您可以查看先前的命令列表和重做事件。

在交互式 tclsh 中,还有一个!!!N快捷方式history redo N来重做最后输入的命令或重做命令 N(其中 N 是事件编号)。

It is not bound to up-arrow or ctrl-p or anything normal though. For that, you probably want some wrapper like rlwrap or socat READLINE to give readline-style line editing. If you have an X Windows session, then tkcon is more use and provides reasonable command line editing. On Windows, the tclsh gets to use the builting line editing from the cmd.exe prompt - including use of uparrow to get to previous commands.

In my test session:

% info pa
8.5.13
% history z
bad option "z": must be add, change, clear, event, info, keep, nextid, or redo
% history info
     1  info pa
     2  history z
     3  history info
% history redo 1
8.5.13
% exit

Followup

Additional comments from the original poster state that the code is not running in a standard tclsh interpreter. The history functionality is implemented in generic/tclHistory.c and the library/history.tcl library script. As noted in the C file header comment:

This module and the Tcl library file history.tcl together implement Tcl command history. Tcl_RecordAndEval(Obj) can be called to record commands ("events") before they are executed. Commands defined in history.tcl may be used to perform history substitutions.

so we can deduce that the custom interpreter must use the Tcl_RecordAndEval API call when evaluating commands that we want entered into the history. Presumably the current custom implementation is just using Tcl_Eval or one of the related functions.

于 2013-11-05T07:56:52.990 回答