File: snit_tcl83_utils.tcl

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (231 lines) | stat: -rw-r--r-- 6,356 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#--------------------------------------------------------------------------
# TITLE:
#	snit_tcl83_utils.tcl
#
# AUTHOR:
#	Kenneth Green, 28 Aug 2004
#
# DESCRIPTION:
#       Utilities to support the back-port of snit from Tcl 8.4 to 8.3
#
#--------------------------------------------------------------------------
# Copyright
#
# Copyright (c) 2005 Kenneth Green
# Modified by Andreas Kupries.
# All rights reserved. This code is licensed as described in license.txt.
#--------------------------------------------------------------------------
# This code is freely distributable, but is provided as-is with
# no warranty expressed or implied.
#--------------------------------------------------------------------------
# Acknowledgements
#   The changes described in this file are made to the awesome 'snit'
#   library as provided by William H. Duquette under the terms
#   defined in the associated 'license.txt'.
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Namespace

namespace eval ::snit83 {}

#-----------------------------------------------------------------------
# Some Snit83 variables

namespace eval ::snit83 {
    variable  cmdTraceTable
    array set cmdTraceTable {}

    namespace eval private {}
}


#-----------------------------------------------------------------------
# Initialisation

#
# Override Tcl functions so we can mimic some behaviours. This is
# conditional on not having been done already. Otherwise loading snit
# twice will fail the second time.
#

if [info exists tk_version] {
    if {
	![llength [info procs destroy]] ||
	![regexp snit83 [info body destroy]]
    } {
	rename destroy __destroy__
    }
}
if {
    ![llength [info procs namespace]] ||
    ![regexp snit83 [info body namespace]]
} {
    rename namespace __namespace__
    rename rename    __rename__ ;# must be last one renamed!
}

#-----------------------------------------------------------------------
# Global namespace functions


# destroy -
#
# Perform delete tracing and then invoke the actual Tk destroy command

if [info exists tk_version] {
    proc destroy { w } {
	variable ::snit83::cmdTraceTable

	set index "delete,$w"
	if [info exists cmdTraceTable($index)] {
	    set cmd $cmdTraceTable($index)
	    ::unset cmdTraceTable($index) ;# prevent recursive tracing
	    if [catch {eval $cmd $oldName \"$newName\" delete} err] { ; # "
		error $err
	    }
	}

	return [__destroy__ $w]
    }
}

# namespace -
#
# Add limited support for 'namespace exists'. Must be a fully
# qualified namespace name (pattern match support not provided).

proc namespace { cmd args } {
    if {[string equal $cmd "exists"]} {
        set ptn [lindex $args 0]
        return [::snit83::private::NamespaceIsDescendantOf :: $ptn]
    } elseif {[string equal $cmd "delete"]} {
        if [namespace exists [lindex $args 0]] {
            return [uplevel 1 [subst {__namespace__ $cmd $args}]]
        }
    } else {
        return [uplevel 1 [subst {__namespace__ $cmd $args}]]
    }
}

# rename -
#
# Perform rename tracing and then invoke the actual Tcl rename command

proc rename { oldName newName } {
    variable ::snit83::cmdTraceTable

    # Get caller's namespace since rename must be performed
    # in the context of the caller's namespace
    set callerNs "::"
    set callerLevel [expr {[info level] - 1}]
    if { $callerLevel > 0 } {
        set callerInfo [info level $callerLevel]
        set procName   [lindex $callerInfo 0]
        set callerNs   [namespace qualifiers $procName]
    }

    #puts "rename: callerNs: $callerNs"
    #puts "rename: '$oldName' -> '$newName'"
    #puts "rename: rcds - [join [array names cmdTraceTable] "\nrename: rcds - "]"

    set result [namespace eval $callerNs [concat __rename__ [list $oldName $newName]]]

    set index1 "rename,$oldName"
    set index2 "rename,::$oldName"

    foreach index [list $index1 $index2] {
        if [info exists cmdTraceTable($index)] {
            set cmd $cmdTraceTable($index)

	    #puts "rename: '$cmd' { $oldName -> $newName }"

            ::unset cmdTraceTable($index) ;# prevent recursive tracing
            if {![string equal $newName ""]} {
                # Create a new trace record under the new name
                set cmdTraceTable(rename,$newName) $cmd
            }
            if [catch {eval $cmd $oldName \"$newName\" rename} err] {
                error $err
            }
            break
        }
    }

    return $result
}


#-----------------------------------------------------------------------
# Private functions

proc ::snit83::private::NamespaceIsDescendantOf { parent child } {
    set result 0

    foreach ns [__namespace__ children $parent] {
        if [string match $ns $child] {
            set result 1
            break;
        } else {
            if [set result [NamespaceIsDescendantOf $ns $child]] {
                break
            }
        }
    }
    return $result
}


#-----------------------------------------------------------------------
# Utility functions

proc ::snit83::traceAddCommand {name ops command} {
    variable cmdTraceTable

    #puts "::snit83::traceAddCommand n/$name/ o/$ops/ c/$command/"
    #puts "XX [join [array names cmdTraceTable] "\nXX "]"

    foreach op $ops {
        set index "$op,$name"
	#puts "::snit83::traceAddCommand: index = $index cmd = $command"

        set cmdTraceTable($index) $command
    }
}

proc ::snit83::traceRemoveCommand {name ops command} {
    variable cmdTraceTable

    #puts "::snit83::traceRemoveCommand n/$name/ o/$ops/ c/$command/"
    #puts "YY [join [array names cmdTraceTable] "\nYY "]"

    foreach op $ops {
        set index "$op,$name"
	#puts "::snit83::traceRemoveCommand: index = $index cmd = $command"

	catch { ::unset cmdTraceTable($index) }
    }
}

# Add support for 'unset -nocomplain'
proc ::snit83::unset { args } {

    #puts "::snit83::unset - args: '$args'"

    set noComplain 0
    if {[string equal [lindex $args 0] "-nocomplain"]} {
        set noComplain 1
        set args [lrange $args 1 end]
    }
    if {[string equal [lindex $args 0] "--"]} {
        set args [lrange $args 1 end]
    }

    if [catch {
	uplevel 1 [linsert $args 0 ::unset]
    } err] {
        if { !$noComplain } {
            error $err
        }
    }
}