word.tcl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # word.tcl --
  2. #
  3. # This file defines various procedures for computing word boundaries in
  4. # strings. This file is primarily needed so Tk text and entry widgets behave
  5. # properly for different platforms.
  6. #
  7. # Copyright (c) 1996 by Sun Microsystems, Inc.
  8. # Copyright (c) 1998 by Scritpics Corporation.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. # The following variables are used to determine which characters are
  13. # interpreted as white space.
  14. if {$::tcl_platform(platform) eq "windows"} {
  15. # Windows style - any but a unicode space char
  16. set ::tcl_wordchars {\S}
  17. set ::tcl_nonwordchars {\s}
  18. } else {
  19. # Motif style - any unicode word char (number, letter, or underscore)
  20. set ::tcl_wordchars {\w}
  21. set ::tcl_nonwordchars {\W}
  22. }
  23. # Arrange for caches of the real matcher REs to be kept, which enables the REs
  24. # themselves to be cached for greater performance (and somewhat greater
  25. # clarity too).
  26. namespace eval ::tcl {
  27. variable WordBreakRE
  28. array set WordBreakRE {}
  29. proc UpdateWordBreakREs args {
  30. # Ignores the arguments
  31. global tcl_wordchars tcl_nonwordchars
  32. variable WordBreakRE
  33. # To keep the RE strings short...
  34. set letter $tcl_wordchars
  35. set space $tcl_nonwordchars
  36. set WordBreakRE(after) "$letter$space|$space$letter"
  37. set WordBreakRE(before) "^.*($letter$space|$space$letter)"
  38. set WordBreakRE(end) "$space*$letter+$space"
  39. set WordBreakRE(next) "$letter*$space+$letter"
  40. set WordBreakRE(previous) "$space*($letter+)$space*\$"
  41. }
  42. # Initialize the cache
  43. UpdateWordBreakREs
  44. trace add variable ::tcl_wordchars write ::tcl::UpdateWordBreakREs
  45. trace add variable ::tcl_nonwordchars write ::tcl::UpdateWordBreakREs
  46. }
  47. # tcl_wordBreakAfter --
  48. #
  49. # This procedure returns the index of the first word boundary after the
  50. # starting point in the given string, or -1 if there are no more boundaries in
  51. # the given string. The index returned refers to the first character of the
  52. # pair that comprises a boundary.
  53. #
  54. # Arguments:
  55. # str - String to search.
  56. # start - Index into string specifying starting point.
  57. proc tcl_wordBreakAfter {str start} {
  58. variable ::tcl::WordBreakRE
  59. set result {-1 -1}
  60. regexp -indices -start $start -- $WordBreakRE(after) $str result
  61. return [lindex $result 1]
  62. }
  63. # tcl_wordBreakBefore --
  64. #
  65. # This procedure returns the index of the first word boundary before the
  66. # starting point in the given string, or -1 if there are no more boundaries in
  67. # the given string. The index returned refers to the second character of the
  68. # pair that comprises a boundary.
  69. #
  70. # Arguments:
  71. # str - String to search.
  72. # start - Index into string specifying starting point.
  73. proc tcl_wordBreakBefore {str start} {
  74. variable ::tcl::WordBreakRE
  75. set result {-1 -1}
  76. regexp -indices -- $WordBreakRE(before) [string range $str 0 $start] result
  77. return [lindex $result 1]
  78. }
  79. # tcl_endOfWord --
  80. #
  81. # This procedure returns the index of the first end-of-word location after a
  82. # starting index in the given string. An end-of-word location is defined to be
  83. # the first whitespace character following the first non-whitespace character
  84. # after the starting point. Returns -1 if there are no more words after the
  85. # starting point.
  86. #
  87. # Arguments:
  88. # str - String to search.
  89. # start - Index into string specifying starting point.
  90. proc tcl_endOfWord {str start} {
  91. variable ::tcl::WordBreakRE
  92. set result {-1 -1}
  93. regexp -indices -start $start -- $WordBreakRE(end) $str result
  94. return [lindex $result 1]
  95. }
  96. # tcl_startOfNextWord --
  97. #
  98. # This procedure returns the index of the first start-of-word location after a
  99. # starting index in the given string. A start-of-word location is defined to
  100. # be a non-whitespace character following a whitespace character. Returns -1
  101. # if there are no more start-of-word locations after the starting point.
  102. #
  103. # Arguments:
  104. # str - String to search.
  105. # start - Index into string specifying starting point.
  106. proc tcl_startOfNextWord {str start} {
  107. variable ::tcl::WordBreakRE
  108. set result {-1 -1}
  109. regexp -indices -start $start -- $WordBreakRE(next) $str result
  110. return [lindex $result 1]
  111. }
  112. # tcl_startOfPreviousWord --
  113. #
  114. # This procedure returns the index of the first start-of-word location before
  115. # a starting index in the given string.
  116. #
  117. # Arguments:
  118. # str - String to search.
  119. # start - Index into string specifying starting point.
  120. proc tcl_startOfPreviousWord {str start} {
  121. variable ::tcl::WordBreakRE
  122. set word {-1 -1}
  123. regexp -indices -- $WordBreakRE(previous) [string range $str 0 $start-1] \
  124. result word
  125. return [lindex $word 0]
  126. }