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