#!/usr/misc/.tcl/bin/wish -f
#
# This code is a modification of the alex tcl browser.
# It searches archia (new.archia to be precise) for files matching
# its parameter(s) and lets you browse them. As with the browser on
# which it's based, files and directories can be opened by double-
# clicking.
#       Brad Broom
#
# This code is a slight modification of the demo browser included
# with the tcl release found in /alex/edu/berkeley/sprite/tcl
#       Vince Cate    
#
# This script generates a directory browser, which lists the working
# directory and allows you to open files or subdirectories by
# double-clicking.

# Create a scrollbar on the right side of the main window and a listbox
# on the left side.

scrollbar .scroll -command  "myscroll"
listbox .list -yscroll ".scroll set" -relief raised -geometry 80x30
listbox .list2 -yscroll ".scroll set" -relief raised -geometry 40x30

# Use constant width fonts to make the directory listing readable.
.list  configure -font "-Adobe-courier-medium-R-Normal--*-120-*"
.list2 configure -font "-Adobe-courier-medium-R-Normal--*-120-*"

pack append . .scroll {right filly} .list2 {left} .list {left expand fill}

# The procedure below scrolls both lists to the specified position.
proc myscroll {loc} {
    .list yview $loc
    .list2 yview $loc
}

# The procedure below is invoked to open a browser on a given file;  if the
# file is a directory then another instance of this program is invoked; if
# the file is a regular file then the Mx editor is invoked to display
# the file.

proc mybrowser {dir file} {
    if {[string compare $dir "."] != 0} {set file $dir/$file}

    puts stdout "$file"
    flush stdout

    if [file isdirectory $file] {
	exec mybrowser $file &

    } else {
        case $file {
           {*.gif}   {exec xloadimage $file       & }
           {*.gif.Z} {exec xloadimage $file       & }
           {*.pbm}   {exec xloadimage $file       & }
           {*.pbm.Z} {exec xloadimage $file       & }
           {*.ps}    {exec gv $file               & }
           {*.PS}    {exec gv $file               & }
           {*.dvi}   {exec xdvi $file             & }
           {*.au}    {exec cat $file > /dev/audio & }
           {default} {
                    if [file isfile $file] {
                        exec xedit $file &
    
                    } else {
                        puts stdout "\"$file\" ERROR - probably symlink to nowhere " 
                        flush stdout
                    }
            }
        }
    }
}

# Give ourselves an information title.

if { $argc == 0 } { puts stderr "Usage: xarchia \[archia options\] searchstring ..."; exit }

wm title . "xarchia $argv"

# Fill the listbox with a list of all the files matching the parameters (run
# the "archia" command and filter its output to get that information).
# The filter's output is a list of 2-tuples. The 0'th element of each is
# the alex file name. The 1'st element is the directory listing concerned.

foreach i [eval [list exec archia] $argv | xarchia-filter ] {
    .list insert end [ lindex $i 0 ]
    .list2 insert end [ lindex $i 1 ]
}

# Set up bindings for the browser.

bind .list <Control-q> {destroy .}
bind .list <Control-c> {destroy .}
focus .list
bind .list <Double-Button-1> {foreach i [selection get] {mybrowser "." $i}}
bind .list <Button-2> {destroy .}
bind .list <Button-3> {destroy .}

# Set up bindings for the directory list.

bind .list2 <Double-Button-1> {foreach i [.list2 curselection] {mybrowser "." [.list get $i]}}
bind .list2 <Control-q> {destroy .}
bind .list2 <Control-c> {destroy .}
bind .list2 <Button-2> {destroy .}
bind .list2 <Button-3> {destroy .}

