annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/tcl8.6/word.tcl @ 69:33d812a61356

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