Version 0 of Visual cd

Updated 2006-02-21 09:57:53 by suchenwi

if 0 {Richard Suchenwirth 2006-02-21 - Here's another Sepp componentlet - an overloaded cd that keeps a least-recently-used stack of the directories visited so far. For added convenience, a menu displaying this LRU stack is added to the console so you can view it, and change to a directory by selecting it from the menu, rather than typing its name (convenient when stylus-taping on a small screen).

The script has been tested stand-alone on Win XP, but is of course intended by me for use with eTcl :-) See also Extending the eTcl console. }

 package require Tk

 console eval {
    if ![winfo exists .menubar.cd] {
        .menubar insert 4 cascade -menu [menu .menubar.cd -tearoff 0] \
                -label cd
        proc dirsel'update {m dirs} {
            $m delete 0 end
            foreach dir $dirs {
                $m add command -label $dir -command [list cd $dir]
            }
        }
    }
 }
 rename cd tcl::cd

#-- Overloaded version

 proc cd {{dir ""}} {
    global dirstack
    if {$dir eq ""} {set dir [file normalize ~]}
    if {$dir eq "-"} {cd [lindex $dirstack end-1]; puts [pwd]; return}
    tcl::cd $dir
    lrupush dirstack [pwd]
    console eval [list dirsel'update .menubar.cd $dirstack]
 }

#-- Make sure an item is last in a least-recently-used queue

 proc lrupush {_lruq value} {
    upvar 1 $_lruq lruq
    lremove lruq $value
    lappend lruq $value
 }

#-- Remove an item from a list, i f present

 proc lremove {_list value} {
    upvar 1 $_list list
    set pos [lsearch -exact $list $value]
    set list [lreplace $list $pos $pos]
 }

#-- Initialization of the directory stack

 proc visual'cd'init {} {
     set ::dirstack {}
     cd .
 }

#-- Self-test if sourced at toplevel:

 if {[file tail [info script]] eq [file tail $argv0]} {
     console show
     wm withdraw .
     visual'cd'init
     console eval {
         bind .console <Escape> {
             exec [info na] [consoleinterp eval {set argv0}] &; exit
         }
     }
 }

Arts and crafts of Tcl-Tk programming