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 of
8 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 # The tcl::history array holds the history list and some additional
12 # 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.
18 namespace eval ::tcl {
20 if {![info exists history]} {
28 namespace ensemble create -command ::tcl::history -map {
30 change ::tcl::HistChange
31 clear ::tcl::HistClear
32 event ::tcl::HistEvent
35 nextid ::tcl::HistNextID
42 # This is the main history command. See the man page for its interface.
43 # This does some argument checking and calls the helper ensemble in the
46 proc ::history {args} {
47 # If no command given, we're doing 'history info'. Can't be done with an
48 # ensemble unknown handler, as those don't fire when no subcommand is
51 if {![llength $args]} {
55 # Tricky stuff needed to make stack and errors come out right!
56 tailcall apply {arglist {tailcall history {*}$arglist} ::tcl} $args
61 # Callback when [::history] is destroyed. Destroys the implementation.
64 # oldName what the command was called.
65 # newName what the command is now called (an empty string).
66 # op the operation (= delete).
72 # The implementation of the [::history] command ceases to exist.
74 trace add command ::history delete [list apply {{oldName newName op} {
76 unset -nocomplain history
77 foreach c [info procs ::tcl::Hist*] {
80 rename ::tcl::history {}
85 # Add an item to the history, and optionally eval it at the global scope
88 # event the command to add
89 # exec (optional) a substring of "exec" causes the command to
92 # If executing, then the results of the command are returned
95 # Adds to the history list
97 proc ::tcl::HistAdd {event {exec {}}} {
101 [prefix longest {exec {}} $exec] eq ""
102 && [llength [info level 0]] == 3
104 return -code error "bad argument \"$exec\": should be \"exec\""
107 # Do not add empty commands to the history
108 if {[string trim $event] eq ""} {
112 # Maintain the history
113 set history([incr history(nextid)]) $event
114 unset -nocomplain history([incr history(oldest)])
116 # Only execute if 'exec' (or non-empty prefix of it) given
125 # Set or query the limit on the length of the history list
128 # limit (optional) the length of the history list
131 # If no limit is specified, the current limit is returned
134 # Updates history(keep) if a limit is specified
136 proc ::tcl::HistKeep {{count {}}} {
138 if {[llength [info level 0]] == 1} {
139 return $history(keep)
141 if {![string is integer -strict $count] || ($count < 0)} {
142 return -code error "illegal keep count \"$count\""
144 set oldold $history(oldest)
145 set history(oldest) [expr {$history(nextid) - $count}]
146 for {} {$oldold <= $history(oldest)} {incr oldold} {
147 unset -nocomplain history($oldold)
149 set history(keep) $count
154 # Erase the history list
163 # Resets the history array, except for the keep limit
165 proc ::tcl::HistClear {} {
167 set keep $history(keep)
169 array set history [list \
178 # Return a pretty-printed version of the history list
181 # num (optional) the length of the history list to return
184 # A formatted history list
186 proc ::tcl::HistInfo {{count {}}} {
188 if {[llength [info level 0]] == 1} {
189 set count [expr {$history(keep) + 1}]
190 } elseif {![string is integer -strict $count]} {
191 return -code error "bad integer \"$count\""
195 for {set i [expr {$history(nextid) - $count + 1}]} \
196 {$i <= $history(nextid)} {incr i} {
197 if {![info exists history($i)]} {
200 set cmd [string map [list \n \n\t] [string trimright $history($i) \ \n]]
201 append result $newline[format "%6d %s" $i $cmd]
209 # Fetch the previous or specified event, execute it, and then replace
210 # the current history item with that event.
213 # event (optional) index of history item to redo. Defaults to -1,
214 # which means the previous event.
217 # Those of the command being redone.
220 # Replaces the current history list item with the one being redone.
222 proc ::tcl::HistRedo {{event -1}} {
225 set i [HistIndex $event]
226 if {$i == $history(nextid)} {
227 return -code error "cannot redo the current event"
236 # Map from an event specifier to an index in the history list.
239 # event index of history item to redo.
240 # If this is a positive number, it is used directly.
241 # If it is a negative number, then it counts back to a previous
242 # event, where -1 is the most recent event.
243 # A string can be matched, either by being the prefix of a
244 # command or by matching a command with string match.
247 # The index into history, or an error if the index didn't match.
249 proc ::tcl::HistIndex {event} {
251 if {![string is integer -strict $event]} {
252 for {set i [expr {$history(nextid)-1}]} {[info exists history($i)]} \
254 if {[string match $event* $history($i)]} {
257 if {[string match $event $history($i)]} {
261 return -code error "no event matches \"$event\""
262 } elseif {$event <= 0} {
263 set i [expr {$history(nextid) + $event}]
267 if {$i <= $history(oldest)} {
268 return -code error "event \"$event\" is too far in the past"
270 if {$i > $history(nextid)} {
271 return -code error "event \"$event\" hasn't occured yet"
278 # Map from an event specifier to the value in the history list.
281 # event index of history item to redo. See index for a description of
282 # possible event patterns.
285 # The value from the history list.
287 proc ::tcl::HistEvent {{event -1}} {
289 set i [HistIndex $event]
290 if {![info exists history($i)]} {
293 return [string trimright $history($i) \ \n]
298 # Replace a value in the history list.
301 # newValue The new value to put into the history list.
302 # event (optional) index of history item to redo. See index for a
303 # description of possible event patterns. This defaults to 0,
304 # which specifies the current event.
307 # Changes the history list.
309 proc ::tcl::HistChange {newValue {event 0}} {
311 set i [HistIndex $event]
312 set history($i) $newValue
317 # Returns the number of the next history event.
325 proc ::tcl::HistNextID {} {
327 return [expr {$history(nextid) + 1}]