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.
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 # RCS: @(#) $Id: word.tcl,v 1.7.2.1 2005/07/22 21:59:41 dgp Exp $
15 # The following variables are used to determine which characters are
16 # interpreted as white space.
18 if {$::tcl_platform(platform
) eq
"windows"} {
19 # Windows style - any but a unicode space char
20 set tcl_wordchars
"\\S"
21 set tcl_nonwordchars
"\\s"
23 # Motif style - any unicode word char (number, letter, or underscore)
24 set tcl_wordchars
"\\w"
25 set tcl_nonwordchars
"\\W"
28 # tcl_wordBreakAfter --
30 # This procedure returns the index of the first word boundary
31 # after the starting point in the given string, or -1 if there
32 # are no more boundaries in the given string. The index returned refers
33 # to the first character of the pair that comprises a boundary.
36 # str - String to search.
37 # start - Index into string specifying starting point.
39 proc tcl_wordBreakAfter {str start
} {
40 global tcl_nonwordchars tcl_wordchars
41 set str
[string range
$str $start end
]
42 if {[regexp -indices "$tcl_wordchars$tcl_nonwordchars|$tcl_nonwordchars$tcl_wordchars" $str result
]} {
43 return [expr {[lindex $result 1] + $start}]
48 # tcl_wordBreakBefore --
50 # This procedure returns the index of the first word boundary
51 # before the starting point in the given string, or -1 if there
52 # are no more boundaries in the given string. The index returned
53 # refers to the second character of the pair that comprises a boundary.
56 # str - String to search.
57 # start - Index into string specifying starting point.
59 proc tcl_wordBreakBefore {str start
} {
60 global tcl_nonwordchars tcl_wordchars
61 if {$start eq
"end"} {
62 set start
[string length
$str]
64 if {[regexp -indices "^.*($tcl_wordchars$tcl_nonwordchars|$tcl_nonwordchars$tcl_wordchars)" [string range
$str 0 $start] result
]} {
65 return [lindex $result 1]
72 # This procedure returns the index of the first end-of-word location
73 # after a starting index in the given string. An end-of-word location
74 # is defined to be the first whitespace character following the first
75 # non-whitespace character after the starting point. Returns -1 if
76 # there are no more words after the starting point.
79 # str - String to search.
80 # start - Index into string specifying starting point.
82 proc tcl_endOfWord {str start
} {
83 global tcl_nonwordchars tcl_wordchars
84 if {[regexp -indices "$tcl_nonwordchars*$tcl_wordchars+$tcl_nonwordchars" \
85 [string range
$str $start end
] result
]} {
86 return [expr {[lindex $result 1] + $start}]
91 # tcl_startOfNextWord --
93 # This procedure returns the index of the first start-of-word location
94 # after a starting index in the given string. A start-of-word
95 # location is defined to be a non-whitespace character following a
96 # whitespace character. Returns -1 if there are no more start-of-word
97 # locations after the starting point.
100 # str - String to search.
101 # start - Index into string specifying starting point.
103 proc tcl_startOfNextWord {str start
} {
104 global tcl_nonwordchars tcl_wordchars
105 if {[regexp -indices "$tcl_wordchars*$tcl_nonwordchars+$tcl_wordchars" \
106 [string range
$str $start end
] result
]} {
107 return [expr {[lindex $result 1] + $start}]
112 # tcl_startOfPreviousWord --
114 # This procedure returns the index of the first start-of-word location
115 # before a starting index in the given string.
118 # str - String to search.
119 # start - Index into string specifying starting point.
121 proc tcl_startOfPreviousWord {str start
} {
122 global tcl_nonwordchars tcl_wordchars
123 if {$start eq
"end"} {
124 set start
[string length
$str]
126 if {[regexp -indices \
127 "$tcl_nonwordchars*($tcl_wordchars+)$tcl_nonwordchars*\$" \
128 [string range
$str 0 [expr {$start - 1}]] result word
]} {
129 return [lindex $word 0]