search.tcl 4.3 KB

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