Posted to tcl by aspect at Thu Sep 11 01:37:22 GMT 2014view raw
- # uses [info cmdexists]
- proc newcmd {{prefix ::newcmd}} {
- if {![string match ::* $prefix]} {
- set prefix [uplevel 1 namespace current]::$prefix
- debug log {newcmd implicitly using caller's namespace: $prefix}
- }
- set i [llength [info commands ${prefix}*]]
- while {[info cmdexists ${prefix}${i}]} {incr i}
- return ${prefix}${i}
- }
- proc newvar {{prefix ::newvar}} {
- if {![string match ::* $prefix]} {
- set prefix [uplevel 1 namespace current]::$prefix
- debug log {newcmd implicitly using caller's namespace: $prefix}
- }
- set i [llength [info vars ${prefix}*]]
- while {[info exists ${prefix}${i}]} {incr i}
- return ${prefix}${i}
- }
- # gensym for namespaces. uses [namespace prefix]
- proc newns {{prefix ::newns}} {
- if {![string match ::* $prefix]} {
- set prefix [uplevel 1 namespace current]::$prefix
- debug log {newcmd implicitly using caller's namespace: $prefix}
- }
- set pfx [namespace prefix $prefix]
- set name [namespace tail $prefix]
- set i [llength [namespace children $pfx ${name}*]]
- while {[namespace exists ${prefix}${i}]} {incr i}
- namespace eval ${prefix}${i} {namespace current}
- }