home *** CD-ROM | disk | FTP | other *** search
/ Internet File Formats / InternetFileFormatsCD.bin / text / latex / mac / alpha60.hqx / Tcl / SystemCode / getVarValue.tcl < prev    next >
Encoding:
Text File  |  1995-06-08  |  4.6 KB  |  161 lines

  1. #############################################################################
  2. #  Report the current value of a global variable, chosen interactively
  3. #  from a list of all active variables.
  4. #
  5. #  If the variable is an array, or its value is too big to fit in an 
  6. #  alertnote, then its contents are listed in a new window, otherwise 
  7. #  the variable's value is displayed in an alertnote.
  8. #
  9. proc getVarValue {} {
  10.     set def [getText [getPos] [selEnd]]
  11.     set var [listpick -p {Which var?} -L $def [lsort -ignore [info globals]]]
  12.     if {![string length $var]} return
  13.     showVarValue $var
  14. }
  15.  
  16. proc showVarValue {var} {
  17.     global $var
  18.     if {![catch {set $var} value]} {
  19.         if {![catch {alertnote "'$var' = $value"}]} {
  20.             return
  21.         } else {
  22.             new -n "* $var *"
  23.             insertText "'$var' = $value"
  24.         }
  25.     } else {
  26.         new -n "* $var *"
  27.         listArray $var
  28.     }
  29.     goto 0
  30. # if 'shrinkWindow' is loaded, call it to trim the output window.
  31.    catch {shrinkWindow 1}
  32.    set win [lindex [winNames -f] 0]
  33.    setWinInfo -w $win dirty 0
  34.    setWinInfo -w $win read-only 1
  35.  
  36. #############################################################################
  37. #  List the name and value of each element of the array $arrName.
  38. #  (Convenient to use as a shell command.)
  39. #
  40. proc listArray {arrName} {
  41.     global $arrName
  42.     set lines {}
  43.     if {![catch {info vars $arrName}]} {
  44.         foreach nm [array names $arrName] {
  45.             set val [expr \$$arrName\($nm\)]
  46.             append lines "\r\"$nm\"\t\{$val\}"
  47.         }
  48.         insertText $lines
  49.     } else {
  50.         alertnote "\"$arrName\" doesn't exist in this context"
  51.     }
  52. }
  53.  
  54. #############################################################################
  55. #  Write out the active definition of the proc $procName.
  56. #  (Convenient to use as a shell command.)
  57. #
  58. proc listProc {procName} {
  59.     set lines {}
  60.     if {![catch {info procs "*$procName*"} procList]} {
  61.         foreach p $procList {
  62.             set pargs [info args $p]
  63.             set arglist {}
  64.             foreach a $pargs {
  65.                 if {[info default $p $a def]} {
  66.                     append arglist " {$a $def}" 
  67.                 } else {
  68.                     append arglist " $a"
  69.                 }
  70.             }
  71.             append lines "\rproc $p {[string trim $arglist]} {"
  72.             append lines [info body $p]
  73.             append lines "}\r"
  74.         }
  75.         insertText $lines
  76.     }
  77. }
  78.  
  79. #############################################################################
  80. # Adjust the dimensions of the current window to match the length (and 
  81. # optionally the width) of the text that it contains.  If shrinkWidth is 
  82. # omitted or set to zero, then only the height of the window is adjusted.
  83. # (Finding the maximum number of characters per line can be annoyingly
  84. # time-consuming for large files).
  85.  
  86. proc shrinkWindow {{shrinkWidth 0}} {
  87.     global defHeight defWidth
  88.     # These constants work for 9-pt Monaco type
  89.     set lineht 11
  90.     set htoff 22
  91.     set chwd 6
  92.     set choff 20
  93.     
  94.     set wd [lindex [getGeometry] 2]
  95.     set ht [lindex [getGeometry] 3]
  96.     set top [lindex [getGeometry] 1]
  97.     set left [lindex [getGeometry] 0]
  98.     
  99.     set mxht [expr [lindex [getMainDevice] 3] - $top - 5 -15]
  100.     set mxwd [expr [lindex [getMainDevice] 2] - $left - 5]
  101.     set mnht 120
  102.     set mnwd 200
  103.  
  104.     set htWd [fileHtWd $shrinkWidth]
  105.     set lines [lindex $htWd 0]
  106.     set chars [lindex $htWd 1]
  107.  
  108.     if {$lines <= 1} then {set lines 10}
  109.     
  110.     
  111.     if {$lines > 0} {
  112.         set ht [expr $htoff + ( $lineht * (1 + $lines)) ]
  113.     } elseif {$ht > $defHeight} {
  114.         set ht $defHeight
  115.     }
  116.     
  117.     if {$chars > 0} {
  118.         set wd [expr $choff + ( $chwd * (2 + $chars)) ]
  119.     } elseif {$wd > $defWidth} {
  120.         set wd $defWidth
  121.     }
  122.     
  123.     if {$ht > $mxht} then {set ht $mxht}
  124.     if {$wd > $mxwd} then {set wd $mxwd}
  125.     if {$ht < $mnht} then {set ht $mnht}
  126.     if {$wd < $mnwd} then {set wd $mnwd}
  127.     sizeWin $wd $ht
  128. }
  129.  
  130. #############################################################################
  131. # Return the number of lines and the maximum number of characters in any 
  132. # line of a file.  It would be nice if there was a built-in command to
  133. # do this (i.e., compiled C code) because this is a pretty slow way to
  134. # get the maximum line width.
  135.  
  136. proc fileHtWd {{checkWidth 0}} {
  137.     set text [getText 0 [maxPos]] 
  138.     
  139.     set lines [split $text "\r"]
  140.     set nlines [llength $lines]
  141.     
  142.     set llen 0
  143.     if {$checkWidth} {
  144.         foreach line $lines {
  145.             regsub {                +░.*$} $line {} line
  146.             regsub {    } $line {    } line
  147.             set len [string length $line]
  148.             if {[set ntab [llength [split $line "\t"]]] > 1} {
  149.                 set len [expr $len + 3*($ntab-1)]
  150.             }
  151.             if { $len > $llen} {
  152.                 set llen $len
  153.             }
  154.         }
  155.     }
  156. #    alertnote "Text Height : $nlines ; Text Width : $llen "
  157.     return [list $nlines $llen]
  158. }
  159.  
  160.