home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09962.iso / vrml / cp2b2x.exe / DATA.Z / search.tcl < prev    next >
Text File  |  1996-04-23  |  4KB  |  138 lines

  1. # search.tcl --
  2. #
  3. # This demonstration script creates a collection of widgets that
  4. # allow you to load a file into a text widget, then perform searches
  5. # on that file.
  6. #
  7. # @(#) search.tcl 1.2 95/06/21 09:17:11
  8.  
  9. # textLoadFile --
  10. # This procedure below loads a file into a text widget, discarding
  11. # the previous contents of the widget. Tags for the old widget are
  12. # not affected, however.
  13. #
  14. # Arguments:
  15. # w -        The window into which to load the file.  Must be a
  16. #        text widget.
  17. # file -    The name of the file to load.  Must be readable.
  18.  
  19. proc textLoadFile {w file} {
  20.     set f [open $file]
  21.     $w delete 1.0 end
  22.     while {![eof $f]} {
  23.     $w insert end [read $f 10000]
  24.     }
  25.     close $f
  26. }
  27.  
  28. # textSearch --
  29. # Search for all instances of a given string in a text widget and
  30. # apply a given tag to each instance found.
  31. #
  32. # Arguments:
  33. # w -        The window in which to search.  Must be a text widget.
  34. # string -    The string to search for.  The search is done using
  35. #        exact matching only;  no special characters.
  36. # tag -        Tag to apply to each instance of a matching string.
  37.  
  38. proc textSearch {w string tag} {
  39.     $w tag remove search 0.0 end
  40.     if {$string == ""} {
  41.     return
  42.     }
  43.     set cur 1.0
  44.     while 1 {
  45.     set cur [$w search -count length $string $cur end]
  46.     if {$cur == ""} {
  47.         break
  48.     }
  49.     $w tag add $tag $cur "$cur + $length char"
  50.     set cur [$w index "$cur + $length char"]
  51.     }
  52. }
  53.  
  54. # textToggle --
  55. # This procedure is invoked repeatedly to invoke two commands at
  56. # periodic intervals.  It normally reschedules itself after each
  57. # execution but if an error occurs (e.g. because the window was
  58. # deleted) then it doesn't reschedule itself.
  59. #
  60. # Arguments:
  61. # cmd1 -    Command to execute when procedure is called.
  62. # sleep1 -    Ms to sleep after executing cmd1 before executing cmd2.
  63. # cmd2 -    Command to execute in the *next* invocation of this
  64. #        procedure.
  65. # sleep2 -    Ms to sleep after executing cmd2 before executing cmd1 again.
  66.  
  67. proc textToggle {cmd1 sleep1 cmd2 sleep2} {
  68.     catch {
  69.     eval $cmd1
  70.     after $sleep1 [list textToggle $cmd2 $sleep2 $cmd1 $sleep1]
  71.     }
  72. }
  73.  
  74. set w .search
  75. catch {destroy $w}
  76. toplevel $w
  77. wm title $w "Text Demonstration - Search and Highlight"
  78. wm iconname $w "search"
  79. positionWindow $w
  80.  
  81. frame $w.buttons
  82. pack  $w.buttons -side bottom -expand y -fill x -pady 2m
  83. button $w.buttons.dismiss -text Dismiss -command "destroy $w"
  84. button $w.buttons.code -text "See Code" -command "showCode $w"
  85. pack $w.buttons.dismiss $w.buttons.code -side left -expand 1
  86.  
  87. frame $w.file
  88. label $w.file.label -text "File name:" -width 13 -anchor w
  89. entry $w.file.entry -width 40 -textvariable fileName
  90. button $w.file.button -text "Load File" \
  91.     -command "textLoadFile $w.text \$fileName"
  92. pack $w.file.label $w.file.entry -side left
  93. pack $w.file.button -side left -pady 5 -padx 10
  94. bind $w.file.entry <Return> "
  95.     textLoadFile $w.text \$fileName
  96.     focus $w.string.entry
  97. "
  98. focus $w.file.entry
  99.  
  100. frame $w.string
  101. label $w.string.label -text "Search string:" -width 13 -anchor w
  102. entry $w.string.entry -width 40 -textvariable searchString
  103. button $w.string.button -text "Highlight" \
  104.     -command "textSearch $w.text \$searchString search"
  105. pack $w.string.label $w.string.entry -side left
  106. pack $w.string.button -side left -pady 5 -padx 10
  107. bind $w.string.entry <Return> "textSearch $w.text \$searchString search"
  108.  
  109. text $w.text -yscrollcommand "$w.scroll set" -setgrid true
  110. scrollbar $w.scroll -command "$w.text yview"
  111. pack $w.file $w.string -side top -fill x
  112. pack $w.scroll -side right -fill y
  113. pack $w.text -expand yes -fill both
  114.  
  115. # Set up display styles for text highlighting.
  116.  
  117. if {[winfo depth $w] > 1} {
  118.     textToggle "$w.text tag configure search -background \
  119.         #ce5555 -foreground white" 800 "$w.text tag configure \
  120.         search -background {} -foreground {}" 200
  121. } else {
  122.     textToggle "$w.text tag configure search -background \
  123.         black -foreground white" 800 "$w.text tag configure \
  124.         search -background {} -foreground {}" 200
  125. }
  126. $w.text insert 1.0 \
  127. {This window demonstrates how to use the tagging facilities in text
  128. widgets to implement a searching mechanism.  First, type a file name
  129. in the top entry, then type <Return> or click on "Load File".  Then
  130. type a string in the lower entry and type <Return> or click on
  131. "Load File".  This will cause all of the instances of the string to
  132. be tagged with the tag "search", and it will arrange for the tag's
  133. display attributes to change to make all of the strings blink.}
  134. $w.text mark set insert 0.0
  135.  
  136. set fileName ""
  137. set searchString ""
  138.