home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 November / CPNL0711.ISO / beeld / teken / scribus-1.3.3.9-win32-install.exe / tcl / tcl8.4 / word.tcl < prev   
Text File  |  2002-10-31  |  4KB  |  133 lines

  1. # word.tcl --
  2. #
  3. # This file defines various procedures for computing word boundaries
  4. # in strings.  This file is primarily needed so Tk text and entry
  5. # widgets behave 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. # RCS: @(#) $Id: word.tcl,v 1.7 2002/11/01 00:28:51 andreas_kupries Exp $
  13.  
  14. # The following variables are used to determine which characters are
  15. # interpreted as white space.  
  16.  
  17. if {[string equal $::tcl_platform(platform) "windows"]} {
  18.     # Windows style - any but a unicode space char
  19.     set tcl_wordchars "\\S"
  20.     set tcl_nonwordchars "\\s"
  21. } else {
  22.     # Motif style - any unicode word char (number, letter, or underscore)
  23.     set tcl_wordchars "\\w"
  24.     set tcl_nonwordchars "\\W"
  25. }
  26.  
  27. # tcl_wordBreakAfter --
  28. #
  29. # This procedure returns the index of the first word boundary
  30. # after the starting point in the given string, or -1 if there
  31. # are no more boundaries in the given string.  The index returned refers
  32. # to the first character of the pair that comprises a boundary.
  33. #
  34. # Arguments:
  35. # str -        String to search.
  36. # start -    Index into string specifying starting point.
  37.  
  38. proc tcl_wordBreakAfter {str start} {
  39.     global tcl_nonwordchars tcl_wordchars
  40.     set str [string range $str $start end]
  41.     if {[regexp -indices "$tcl_wordchars$tcl_nonwordchars|$tcl_nonwordchars$tcl_wordchars" $str result]} {
  42.     return [expr {[lindex $result 1] + $start}]
  43.     }
  44.     return -1
  45. }
  46.  
  47. # tcl_wordBreakBefore --
  48. #
  49. # This procedure returns the index of the first word boundary
  50. # before the starting point in the given string, or -1 if there
  51. # are no more boundaries in the given string.  The index returned
  52. # refers to the second character of the pair that comprises a boundary.
  53. #
  54. # Arguments:
  55. # str -        String to search.
  56. # start -    Index into string specifying starting point.
  57.  
  58. proc tcl_wordBreakBefore {str start} {
  59.     global tcl_nonwordchars tcl_wordchars
  60.     if {[string equal $start end]} {
  61.     set start [string length $str]
  62.     }
  63.     if {[regexp -indices "^.*($tcl_wordchars$tcl_nonwordchars|$tcl_nonwordchars$tcl_wordchars)" [string range $str 0 $start] result]} {
  64.     return [lindex $result 1]
  65.     }
  66.     return -1
  67. }
  68.  
  69. # tcl_endOfWord --
  70. #
  71. # This procedure returns the index of the first end-of-word location
  72. # after a starting index in the given string.  An end-of-word location
  73. # is defined to be the first whitespace character following the first
  74. # non-whitespace character after the starting point.  Returns -1 if
  75. # there are no more words after the starting point.
  76. #
  77. # Arguments:
  78. # str -        String to search.
  79. # start -    Index into string specifying starting point.
  80.  
  81. proc tcl_endOfWord {str start} {
  82.     global tcl_nonwordchars tcl_wordchars
  83.     if {[regexp -indices "$tcl_nonwordchars*$tcl_wordchars+$tcl_nonwordchars" \
  84.         [string range $str $start end] result]} {
  85.     return [expr {[lindex $result 1] + $start}]
  86.     }
  87.     return -1
  88. }
  89.  
  90. # tcl_startOfNextWord --
  91. #
  92. # This procedure returns the index of the first start-of-word location
  93. # after a starting index in the given string.  A start-of-word
  94. # location is defined to be a non-whitespace character following a
  95. # whitespace character.  Returns -1 if there are no more start-of-word
  96. # locations after the starting point.
  97. #
  98. # Arguments:
  99. # str -        String to search.
  100. # start -    Index into string specifying starting point.
  101.  
  102. proc tcl_startOfNextWord {str start} {
  103.     global tcl_nonwordchars tcl_wordchars
  104.     if {[regexp -indices "$tcl_wordchars*$tcl_nonwordchars+$tcl_wordchars" \
  105.         [string range $str $start end] result]} {
  106.     return [expr {[lindex $result 1] + $start}]
  107.     }
  108.     return -1
  109. }
  110.  
  111. # tcl_startOfPreviousWord --
  112. #
  113. # This procedure returns the index of the first start-of-word location
  114. # before a starting index in the given string.
  115. #
  116. # Arguments:
  117. # str -        String to search.
  118. # start -    Index into string specifying starting point.
  119.  
  120. proc tcl_startOfPreviousWord {str start} {
  121.     global tcl_nonwordchars tcl_wordchars
  122.     if {[string equal $start end]} {
  123.     set start [string length $str]
  124.     }
  125.     if {[regexp -indices \
  126.         "$tcl_nonwordchars*($tcl_wordchars+)$tcl_nonwordchars*\$" \
  127.         [string range $str 0 [expr {$start - 1}]] result word]} {
  128.     return [lindex $word 0]
  129.     }
  130.     return -1
  131. }
  132.