itcl.tcl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #
  2. # itcl.tcl
  3. # ----------------------------------------------------------------------
  4. # Invoked automatically upon startup to customize the interpreter
  5. # for [incr Tcl].
  6. # ----------------------------------------------------------------------
  7. # AUTHOR: Michael J. McLennan
  8. # Bell Labs Innovations for Lucent Technologies
  9. # mmclennan@lucent.com
  10. # http://www.tcltk.com/itcl
  11. # ----------------------------------------------------------------------
  12. # Copyright (c) 1993-1998 Lucent Technologies, Inc.
  13. # ======================================================================
  14. # See the file "license.terms" for information on usage and
  15. # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  16. proc ::itcl::delete_helper { name args } {
  17. ::itcl::delete object $name
  18. }
  19. # ----------------------------------------------------------------------
  20. # USAGE: local <className> <objName> ?<arg> <arg>...?
  21. #
  22. # Creates a new object called <objName> in class <className>, passing
  23. # the remaining <arg>'s to the constructor. Unlike the usual
  24. # [incr Tcl] objects, however, an object created by this procedure
  25. # will be automatically deleted when the local call frame is destroyed.
  26. # This command is useful for creating objects that should only remain
  27. # alive until a procedure exits.
  28. # ----------------------------------------------------------------------
  29. proc ::itcl::local {class name args} {
  30. set ptr [uplevel [list $class $name] $args]
  31. uplevel [list set itcl-local-$ptr $ptr]
  32. set cmd [uplevel namespace which -command $ptr]
  33. uplevel [list trace variable itcl-local-$ptr u \
  34. "::itcl::delete_helper $cmd"]
  35. return $ptr
  36. }
  37. # ----------------------------------------------------------------------
  38. # auto_mkindex
  39. # ----------------------------------------------------------------------
  40. # Define Itcl commands that will be recognized by the auto_mkindex
  41. # parser in Tcl...
  42. #
  43. #
  44. # USAGE: itcl::class name body
  45. # Adds an entry for the given class declaration.
  46. #
  47. foreach __cmd {itcl::class class itcl::type type ictl::widget widget itcl::widgetadaptor widgetadaptor itcl::extendedclass extendedclass} {
  48. auto_mkindex_parser::command $__cmd {name body} {
  49. variable index
  50. variable scriptFile
  51. append index "set [list auto_index([fullname $name])]"
  52. append index " \[list source \[file join \$dir [list $scriptFile]\]\]\n"
  53. variable parser
  54. variable contextStack
  55. set contextStack [linsert $contextStack 0 $name]
  56. $parser eval $body
  57. set contextStack [lrange $contextStack 1 end]
  58. }
  59. }
  60. #
  61. # USAGE: itcl::body name arglist body
  62. # Adds an entry for the given method/proc body.
  63. #
  64. foreach __cmd {itcl::body body} {
  65. auto_mkindex_parser::command $__cmd {name arglist body} {
  66. variable index
  67. variable scriptFile
  68. append index "set [list auto_index([fullname $name])]"
  69. append index " \[list source \[file join \$dir [list $scriptFile]\]\]\n"
  70. }
  71. }
  72. #
  73. # USAGE: itcl::configbody name arglist body
  74. # Adds an entry for the given method/proc body.
  75. #
  76. foreach __cmd {itcl::configbody configbody} {
  77. auto_mkindex_parser::command $__cmd {name body} {
  78. variable index
  79. variable scriptFile
  80. append index "set [list auto_index([fullname $name])]"
  81. append index " \[list source \[file join \$dir [list $scriptFile]\]\]\n"
  82. }
  83. }
  84. #
  85. # USAGE: ensemble name ?body?
  86. # Adds an entry to the auto index list for the given ensemble name.
  87. #
  88. foreach __cmd {itcl::ensemble ensemble} {
  89. auto_mkindex_parser::command $__cmd {name {body ""}} {
  90. variable index
  91. variable scriptFile
  92. append index "set [list auto_index([fullname $name])]"
  93. append index " \[list source \[file join \$dir [list $scriptFile]\]\]\n"
  94. }
  95. }
  96. #
  97. # USAGE: public arg ?arg arg...?
  98. # protected arg ?arg arg...?
  99. # private arg ?arg arg...?
  100. #
  101. # Evaluates the arguments as commands, so we can recognize proc
  102. # declarations within classes.
  103. #
  104. foreach __cmd {public protected private} {
  105. auto_mkindex_parser::command $__cmd {args} {
  106. variable parser
  107. $parser eval $args
  108. }
  109. }
  110. # SF bug #246 unset variable __cmd to avoid problems in user programs!!
  111. unset __cmd
  112. # ----------------------------------------------------------------------
  113. # auto_import
  114. # ----------------------------------------------------------------------
  115. # This procedure overrides the usual "auto_import" function in the
  116. # Tcl library. It is invoked during "namespace import" to make see
  117. # if the imported commands reside in an autoloaded library. If so,
  118. # stubs are created to represent the commands. Executing a stub
  119. # later on causes the real implementation to be autoloaded.
  120. #
  121. # Arguments -
  122. # pattern The pattern of commands being imported (like "foo::*")
  123. # a canonical namespace as returned by [namespace current]
  124. proc auto_import {pattern} {
  125. global auto_index
  126. set ns [uplevel namespace current]
  127. set patternList [auto_qualify $pattern $ns]
  128. auto_load_index
  129. foreach pattern $patternList {
  130. foreach name [array names auto_index $pattern] {
  131. if {"" == [info commands $name]} {
  132. ::itcl::import::stub create $name
  133. }
  134. }
  135. }
  136. }