3 # Implementation of the history command.
5 # Copyright (c) 1997 Sun Microsystems, Inc.
7 # See the file "license.terms" for information on usage and redistribution
8 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 # The tcl::history array holds the history list and
12 # some additional bookkeeping variables.
14 # nextid the index used for the next history list item.
15 # keep the max size of the history list
16 # oldest the index of the oldest item in the history.
20 if {![info exists
history]} {
31 # This is the main history command. See the man page for its interface.
32 # This does argument checking and calls helper procedures in the
36 set len
[llength $args]
38 return [tcl
::HistInfo]
40 set key
[lindex $args 0]
41 set options "add, change, clear, event, info, keep, nextid, or redo"
42 switch -glob -- $key {
46 return -code error "wrong # args: should be \"history add event ?exec?\""
48 if {![string match
$key* add
]} {
49 return -code error "bad option \"$key\": must be $options"
52 set arg
[lindex $args 2]
53 if {! ([string match e
* $arg] && [string match
$arg* exec])} {
54 return -code error "bad argument \"$arg\": should be \"exec\""
57 return [tcl
::HistAdd [lindex $args 1] [lindex $args 2]]
59 ch
* { # history change
61 if {($len > 3) ||
($len < 2)} {
62 return -code error "wrong # args: should be \"history change newValue ?event?\""
64 if {![string match
$key* change
]} {
65 return -code error "bad option \"$key\": must be $options"
70 set event [lindex $args 2]
73 return [tcl
::HistChange [lindex $args 1] $event]
78 return -code error "wrong # args: should be \"history clear\""
80 if {![string match
$key* clear
]} {
81 return -code error "bad option \"$key\": must be $options"
83 return [tcl
::HistClear]
88 return -code error "wrong # args: should be \"history event ?event?\""
90 if {![string match
$key* event]} {
91 return -code error "bad option \"$key\": must be $options"
96 set event [lindex $args 1]
98 return [tcl
::HistEvent $event]
103 return -code error "wrong # args: should be \"history info ?count?\""
105 if {![string match
$key* info]} {
106 return -code error "bad option \"$key\": must be $options"
108 return [tcl
::HistInfo [lindex $args 1]]
113 return -code error "wrong # args: should be \"history keep ?count?\""
116 return [tcl
::HistKeep]
118 set limit
[lindex $args 1]
119 if {[catch {expr {~
$limit}}] ||
($limit < 0)} {
120 return -code error "illegal keep count \"$limit\""
122 return [tcl
::HistKeep $limit]
125 n
* { # history nextid
128 return -code error "wrong # args: should be \"history nextid\""
130 if {![string match
$key* nextid
]} {
131 return -code error "bad option \"$key\": must be $options"
133 return [expr {$tcl::history(nextid
) + 1}]
138 return -code error "wrong # args: should be \"history redo ?event?\""
140 if {![string match
$key* redo
]} {
141 return -code error "bad option \"$key\": must be $options"
143 return [tcl
::HistRedo [lindex $args 1]]
146 return -code error "bad option \"$key\": must be $options"
153 # Add an item to the history, and optionally eval it at the global scope
156 # command the command to add
157 # exec (optional) a substring of "exec" causes the
158 # command to be evaled.
160 # If executing, then the results of the command are returned
163 # Adds to the history list
165 proc tcl
::HistAdd {command
{exec {}}} {
168 # Do not add empty commands to the history
169 if {[string trim
$command] eq
""} {
173 set i
[incr history(nextid
)]
174 set history($i) $command
175 set j
[incr history(oldest
)]
176 unset -nocomplain history($j)
177 if {[string match e
* $exec]} {
178 return [uplevel #0 $command]
186 # Set or query the limit on the length of the history list
189 # limit (optional) the length of the history list
192 # If no limit is specified, the current limit is returned
195 # Updates history(keep) if a limit is specified
197 proc tcl
::HistKeep {{limit
{}}} {
200 return $history(keep
)
202 set oldold
$history(oldest
)
203 set history(oldest
) [expr {$history(nextid
) - $limit}]
204 for {} {$oldold <= $history(oldest
)} {incr oldold
} {
205 unset -nocomplain history($oldold)
207 set history(keep
) $limit
213 # Erase the history list
222 # Resets the history array, except for the keep limit
224 proc tcl
::HistClear {} {
226 set keep
$history(keep
)
228 array set history [list \
237 # Return a pretty-printed version of the history list
240 # num (optional) the length of the history list to return
243 # A formatted history list
245 proc tcl
::HistInfo {{num
{}}} {
248 set num
[expr {$history(keep
) + 1}]
252 for {set i
[expr {$history(nextid
) - $num + 1}]} \
253 {$i <= $history(nextid
)} {incr i
} {
254 if {![info exists
history($i)]} {
257 set cmd
[string map
[list \n \n\t] [string trimright
$history($i) \ \n]]
258 append result
$newline[format "%6d %s" $i $cmd]
266 # Fetch the previous or specified event, execute it, and then
267 # replace the current history item with that event.
270 # event (optional) index of history item to redo. Defaults to -1,
271 # which means the previous event.
274 # Those of the command being redone.
277 # Replaces the current history list item with the one being redone.
279 proc tcl
::HistRedo {{event -1}} {
284 set i
[HistIndex
$event]
285 if {$i == $history(nextid
)} {
286 return -code error "cannot redo the current event"
295 # Map from an event specifier to an index in the history list.
298 # event index of history item to redo.
299 # If this is a positive number, it is used directly.
300 # If it is a negative number, then it counts back to a previous
301 # event, where -1 is the most recent event.
302 # A string can be matched, either by being the prefix of
303 # a command or by matching a command with string match.
306 # The index into history, or an error if the index didn't match.
308 proc tcl
::HistIndex {event} {
310 if {[catch {expr {~
$event}}]} {
311 for {set i
[expr {$history(nextid
)-1}]} {[info exists
history($i)]} \
313 if {[string match
$event* $history($i)]} {
316 if {[string match
$event $history($i)]} {
320 return -code error "no event matches \"$event\""
321 } elseif
{$event <= 0} {
322 set i
[expr {$history(nextid
) + $event}]
326 if {$i <= $history(oldest
)} {
327 return -code error "event \"$event\" is too far in the past"
329 if {$i > $history(nextid
)} {
330 return -code error "event \"$event\" hasn't occured yet"
337 # Map from an event specifier to the value in the history list.
340 # event index of history item to redo. See index for a
341 # description of possible event patterns.
344 # The value from the history list.
346 proc tcl
::HistEvent {event} {
348 set i
[HistIndex
$event]
349 if {[info exists
history($i)]} {
350 return [string trimright
$history($i) \ \n]
358 # Replace a value in the history list.
361 # cmd The new value to put into the history list.
362 # event (optional) index of history item to redo. See index for a
363 # description of possible event patterns. This defaults
364 # to 0, which specifies the current event.
367 # Changes the history list.
369 proc tcl
::HistChange {cmd
{event 0}} {
371 set i
[HistIndex
$event]