Circular history for entry widgets

This came up when I was building a tcl/tk application for running linux commands and capturing the output. I had an entry widget where I was inputting the commands and I wanted to add history alike shells. Johannes Kuhn helped me to build the history function and I added the history to be circular (like a circular linked-list). Here is proc:

proc histwalk i {
   incr ::eventcnt $i
   if {$::eventcnt == [history nextid]} {set ::eventcnt 1}
   if {$::eventcnt == -[expr [history nextid]-1]} {set ::eventcnt 0}
   set ::(.entry,stdin) [history event $::eventcnt]

To this procedure you need to add the bind statements:

bind $entrywidget_textvariable <Return> {  history add $stdin } 
bind $entrywidget_textvariable <Up> {histwalk -1}
bind $entrywidget_textvariable <Down> {histwalk 1}

This is much more simple than An entry with a history.

See Also