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.
7 # Copyright (c) 1996 by Sun Microsystems, Inc.
8 # Copyright (c) 1998 by Scritpics Corporation.
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 # The following variables are used to determine which characters are
14 # interpreted as white space.
16 if {$::tcl_platform(platform
) eq
"windows"} {
17 # Windows style - any but a unicode space char
18 set ::tcl_wordchars {\S
}
19 set ::tcl_nonwordchars {\s
}
21 # Motif style - any unicode word char (number, letter, or underscore)
22 set ::tcl_wordchars {\w
}
23 set ::tcl_nonwordchars {\W
}
26 # Arrange for caches of the real matcher REs to be kept, which enables the REs
27 # themselves to be cached for greater performance (and somewhat greater
30 namespace eval ::tcl {
32 array set WordBreakRE
{}
34 proc UpdateWordBreakREs args
{
35 # Ignores the arguments
36 global tcl_wordchars tcl_nonwordchars
39 # To keep the RE strings short...
40 set letter
$tcl_wordchars
41 set space
$tcl_nonwordchars
43 set WordBreakRE
(after) "$letter$space|$space$letter"
44 set WordBreakRE
(before
) "^.*($letter$space|$space$letter)"
45 set WordBreakRE
(end
) "$space*$letter+$space"
46 set WordBreakRE
(next
) "$letter*$space+$letter"
47 set WordBreakRE
(previous
) "$space*($letter+)$space*\$"
50 # Initialize the cache
52 trace add
variable ::tcl_wordchars write
::tcl::UpdateWordBreakREs
53 trace add
variable ::tcl_nonwordchars write
::tcl::UpdateWordBreakREs
56 # tcl_wordBreakAfter --
58 # This procedure returns the index of the first word boundary after the
59 # starting point in the given string, or -1 if there are no more boundaries in
60 # the given string. The index returned refers to the first character of the
61 # pair that comprises a boundary.
64 # str - String to search.
65 # start - Index into string specifying starting point.
67 proc tcl_wordBreakAfter {str start
} {
68 variable ::tcl::WordBreakRE
70 regexp -indices -start $start $WordBreakRE(after) $str result
71 return [lindex $result 1]
74 # tcl_wordBreakBefore --
76 # This procedure returns the index of the first word boundary before the
77 # starting point in the given string, or -1 if there are no more boundaries in
78 # the given string. The index returned refers to the second character of the
79 # pair that comprises a boundary.
82 # str - String to search.
83 # start - Index into string specifying starting point.
85 proc tcl_wordBreakBefore {str start
} {
86 variable ::tcl::WordBreakRE
88 regexp -indices $WordBreakRE(before
) [string range
$str 0 $start] result
89 return [lindex $result 1]
94 # This procedure returns the index of the first end-of-word location after a
95 # starting index in the given string. An end-of-word location is defined to be
96 # the first whitespace character following the first non-whitespace character
97 # after the starting point. Returns -1 if there are no more words after the
101 # str - String to search.
102 # start - Index into string specifying starting point.
104 proc tcl_endOfWord {str start
} {
105 variable ::tcl::WordBreakRE
107 regexp -indices -start $start $WordBreakRE(end
) $str result
108 return [lindex $result 1]
111 # tcl_startOfNextWord --
113 # This procedure returns the index of the first start-of-word location after a
114 # starting index in the given string. A start-of-word location is defined to
115 # be a non-whitespace character following a whitespace character. Returns -1
116 # if there are no more start-of-word locations after the starting point.
119 # str - String to search.
120 # start - Index into string specifying starting point.
122 proc tcl_startOfNextWord {str start
} {
123 variable ::tcl::WordBreakRE
125 regexp -indices -start $start $WordBreakRE(next
) $str result
126 return [lindex $result 1]
129 # tcl_startOfPreviousWord --
131 # This procedure returns the index of the first start-of-word location before
132 # a starting index in the given string.
135 # str - String to search.
136 # start - Index into string specifying starting point.
138 proc tcl_startOfPreviousWord {str start
} {
139 variable ::tcl::WordBreakRE
141 regexp -indices $WordBreakRE(previous
) [string range
$str 0 $start-1] \
143 return [lindex $word 0]