home *** CD-ROM | disk | FTP | other *** search
/ Internet File Formats / InternetFileFormatsCD.bin / text / latex / mac / alpha.6.0.sit / Tcl / SystemCode / filesets.tcl / filesets.tcl
Encoding:
Text File  |  1995-07-04  |  7.6 KB  |  278 lines  |  [TEXT/ALFA]

  1. #===============================================================================================
  2. # Alpha calls two fileset-related routines, 'getCurrFileSet', and 
  3. # 'getFileSetNames'. Alpha will also attempt to set the variable 'currFileSet'
  4. # on occasion, but this isn't critical.
  5. #===============================================================================================
  6.  
  7. #===========================================================================
  8. # The filesets.
  9. #===========================================================================
  10.  
  11. # Build some filesets on the fly.
  12. catch {unset fileSets}
  13. catch {unset currFileSet}
  14. set gfileSets(Help) "$HOME:Help:*"
  15. set gfileSets(System) "$HOME:Tcl:SystemCode:*.tcl"
  16. set gfileSets(User) "$HOME:Tcl:UserCode:*.tcl"
  17.  
  18. # Default curr fileset is the first one. Can be changed in 'userStartup.tcl'.
  19. set currFileSet [lindex [array names gfileSets] 0]
  20.  
  21. #===========================================================================
  22. # The support routines.
  23. #===========================================================================
  24. # Called from Alpha to get list of files for current file set.
  25. proc getCurrFileSet {} {
  26.     global fileSets
  27.     global currFileSet
  28.     return $fileSets($currFileSet)
  29. }
  30. # Called from Alpha to get names. The first name returned is taken to 
  31. # be the current fileset.
  32. proc getFileSetNames {} {
  33.     global fileSets
  34.     global currFileSet
  35.     set ind [lsearch [array names fileSets] $currFileSet]
  36.     if {$ind < 0} {set ind 0}
  37.     return [linsert [lsort [lreplace [array names fileSets] $ind $ind]] 0 $currFileSet]
  38. }
  39.  
  40.  
  41. # Keep 'sets' menu up to date.
  42. trace vdelete currFileSet w shadowCurrFileSet
  43. trace variable currFileSet w shadowCurrFileSet
  44. proc shadowCurrFileSet {nm1 nm2 op} {
  45.     global fileSets
  46.     global currFileSet
  47.     foreach name [array names fileSets] {
  48.         if {$name == $currFileSet} {
  49.             markMenuItem -m choose $name on
  50.         } else {
  51.             markMenuItem -m choose $name off
  52.         }
  53.     }
  54.     return $currFileSet
  55. }
  56.  
  57. # Called in response to user changing filesets from the fileset menu.
  58. proc changeFileSet {menu item} {
  59.     global currFileSet
  60.     
  61.     markMenuItem -m choose $currFileSet off
  62.     set currFileSet $item
  63.     markMenuItem -m choose $currFileSet on
  64. }
  65.  
  66.  
  67.  
  68. #===========================================================================
  69. # Add fileset.
  70. #===========================================================================
  71. proc createFileset {} {
  72.     global gfileSets fileSets currFileSet
  73.     
  74.     set name [getline "New fileset name:" ""]
  75.     if {![string length $name]} return
  76.     
  77.     set dir [string trim [get_directory -p "New fileset dir:"] ":"]
  78.     if {![string length $dir]} return
  79.     
  80.     set filePat [getline "File pattern:" "*"]
  81.     if {![string length $filePat]} return
  82.     
  83.     set "gfileSets($name)" "$dir:$filePat"
  84.     menu -n choose -m -p changeFileSet [lsort [array names fileSets]]
  85.     set currFileSet $name
  86.  
  87.     if {[askyesno "Save new fileset?"] == "yes"} {
  88.         addArrDef gfileSets $name "$dir:$filePat"
  89.     }
  90.     rebuildFilesetMenu
  91.     return $name
  92. }
  93.  
  94.  
  95. # Open entire fileset.
  96. proc openEntireFileset {} {
  97.     global fileSets
  98.     set name [eval [list prompt "Open which fileset?" [lindex [array names fileSets] 0] "FileSet:"] [lsort -ignore [array names fileSets]]]
  99.     foreach f $fileSets($name) {
  100.         edit $f
  101.     }
  102. }
  103.  
  104. # Create a fileset fromt containing all windows currently open.
  105. proc createFilesetFromWins {} {
  106.     global fileSets currFileSet
  107.     
  108.     set name [prompt "Create fileset containing current windows under what name?" "OpenWins"]
  109.     set currFileSet $name
  110.     addArrDef fileSets $name [winNames -f]
  111.     set fileSets($name) [winNames -f]
  112.     rebuildFilesetMenu
  113. }
  114.  
  115.         
  116.  
  117. #===========================================================================
  118. # Dump fileset to current window. If you dump at the end of this file,
  119. # the fileset will be reloaded the next time you run Alpha.
  120. #===========================================================================
  121. proc dumpFileset {} {
  122.     global fileSets
  123.     global currFileSet
  124.     if {![catch {prompt "Fileset name:" $currFileSet} name]} {
  125.         insertText "set \"fileSets($name)\" \{\r"
  126.         foreach file "$fileSets($name)" {
  127.             insertText "\t\"$file\"\r"
  128.         }
  129.         insertText "\}\r"
  130.     }
  131. }
  132.  
  133.  
  134.  
  135. #================================================================================
  136. # Edit a file from a fileset via list dialogs (no mousing around).
  137. #================================================================================
  138. proc editFile {} {
  139.     global fileSets
  140.     
  141.     set fset [listpick -p {Fileset?} [lsort -ignore [concat {*recent*} [array names fileSets]]]]
  142.     if {[string length $fset]} {
  143.         if {$fset == {*recent*}} {return [editRecentFile]}
  144.         foreach f $fileSets($fset) {
  145.             lappend disp [file tail $f]
  146.         }
  147.         foreach res [listpick -l -p {File?} [lsort -ignore $disp]]  {
  148.             set ind [lsearch $fileSets($fset) \*:$res]
  149.             catch {edit [lindex $fileSets($fset) $ind]}
  150.         }
  151.     }
  152. }
  153.  
  154.  
  155. proc deleteFileset {} {
  156.     global fileSets gfileSets currFileSet
  157.     
  158.     set fset [listpick -p {Fileset?} [lsort -ignore [array names fileSets]]]
  159.      if {$currFileSet == $fset} {catch {set currFileSet System}}
  160.  
  161.     if {[askyesno "Delete fileset \"$fset\"?"] == "yes"} {
  162.         catch {unset "fileSets($fset)"}
  163.         catch {unset "gfileSets($fset)"}
  164.         
  165.         if {[askyesno "Permanently?"] == "yes"} {
  166.             removeArrDef gfileSets $fset
  167.         }
  168.         rebuildFilesetMenu
  169.     }
  170. }
  171.  
  172.  
  173. #===============================================================================
  174. proc replaceInFileset {} {
  175.     global fileSets
  176.     
  177.     set from [prompt "Search string:" ""]
  178.     set to [prompt "Replace string:" ""]
  179.     set fset [listpick -p "Which fileset?" [array names fileSets]]
  180.  
  181.     if {[buttonAlert "Save all windows?" "Yes" "Cancel"] != "Yes"} return
  182.     saveAll
  183.     
  184.     set cid [scancontext create]
  185.     scanmatch $cid $from {
  186.         set matches($f) 1
  187.     }
  188.     foreach f $fileSets($fset) {
  189.         if {![catch {set fid [open $f]}]} {
  190.             message "Looking at '[file tail $f]'╔"
  191.             scanfile $cid $fid
  192.             close $fid
  193.         }
  194.     }
  195.     scancontext delete $cid
  196.  
  197.     foreach f [array names matches] {
  198.         message "Modifying $f╔"
  199.         set cid [open $f "r"]
  200.         if {[regsub -all $from [read $cid] $to out]} {
  201.             set ocid [open $f "w+"]
  202.             puts -nonewline $ocid $out
  203.             close $ocid
  204.         }
  205.         close $cid
  206.     }
  207.     
  208.     if {[buttonAlert "Revert affected windows?" "Yes" "No"] == "Yes"} {
  209.         foreach f [array names matches] {
  210.             bringToFront $f
  211.             revert
  212.         }
  213.     }
  214.     message ""
  215. }
  216.  
  217. #================================================================================
  218. # Create a heirarchical fileset menu that allows you 
  219. # to open any file in any fileset.
  220. #
  221. # Doesn't bother trying to specialcase names or pathnames that have
  222. # non-alphanumeric characters in them.
  223.  
  224. proc filesetProc {menu item} {
  225.     global fileSets
  226.     if {$item == "Edit File"} {
  227.         editFile
  228.         return
  229.     }
  230.     if {[set match [lsearch $fileSets($menu) *:$item]] >= 0} {
  231.         edit [lindex $fileSets($menu) $match]
  232.     }
  233. }    
  234.  
  235.  
  236. proc rebuildFilesetMenu {} {
  237.     global fileSets gfileSets fsetMenuName currFileSet
  238.  
  239.     foreach f [array names gfileSets] {
  240.         catch {set "fileSets($f)" [glob -t TEXT "$gfileSets($f)"]}
  241.     }
  242.     
  243.     foreach f [lsort [array names fileSets]] {
  244.         if {$f == "Help"} continue
  245.         set menu {}
  246.         foreach m "$fileSets($f)" {
  247.             lappend menu "[file tail $m]\&"
  248.         }
  249.         lappend sets [list menu -s -m -n $f -p filesetProc [lsort -i $menu]]
  250.     }
  251.     menu -m -n $fsetMenuName -p filesetProc [concat {{/'Edit File} {menu -n Utilities {}} "(-"} $sets]
  252.     
  253.     menu -n "Utilities" {
  254.         {menu -n choose -p changeFileSet {}}
  255.         "createFileset"
  256.         "deleteFileset"
  257.         "(-"
  258.         "createThinkFileset"
  259.         "createWarriorFileset"
  260.         "(-"
  261.         "createFilesetFromWins"
  262.         "openEntireFileset"
  263.         "(-"
  264.         "/T<I<OfindTag"
  265.         "createTagFile"
  266.         "(-"
  267.         "replaceInFileset"
  268.         "(-"
  269.         "rebuildFilesetMenu"
  270.         }
  271.  
  272.     menu -n choose -m -p changeFileSet [lsort [array names fileSets]]
  273.     markMenuItem -m choose $currFileSet on
  274. }
  275.  
  276.  
  277. insertMenu $fsetMenuName
  278.