Posted to tcl by aspect at Thu Sep 11 01:37:22 GMT 2014view raw

  1. # uses [info cmdexists]
  2. proc newcmd {{prefix ::newcmd}} {
  3. if {![string match ::* $prefix]} {
  4. set prefix [uplevel 1 namespace current]::$prefix
  5. debug log {newcmd implicitly using caller's namespace: $prefix}
  6. }
  7. set i [llength [info commands ${prefix}*]]
  8. while {[info cmdexists ${prefix}${i}]} {incr i}
  9. return ${prefix}${i}
  10. }
  11.  
  12. proc newvar {{prefix ::newvar}} {
  13. if {![string match ::* $prefix]} {
  14. set prefix [uplevel 1 namespace current]::$prefix
  15. debug log {newcmd implicitly using caller's namespace: $prefix}
  16. }
  17. set i [llength [info vars ${prefix}*]]
  18. while {[info exists ${prefix}${i}]} {incr i}
  19. return ${prefix}${i}
  20. }
  21.  
  22. # gensym for namespaces. uses [namespace prefix]
  23. proc newns {{prefix ::newns}} {
  24. if {![string match ::* $prefix]} {
  25. set prefix [uplevel 1 namespace current]::$prefix
  26. debug log {newcmd implicitly using caller's namespace: $prefix}
  27. }
  28. set pfx [namespace prefix $prefix]
  29. set name [namespace tail $prefix]
  30. set i [llength [namespace children $pfx ${name}*]]
  31. while {[namespace exists ${prefix}${i}]} {incr i}
  32. namespace eval ${prefix}${i} {namespace current}
  33. }
  34.  
  35.