annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/tcl8.6/auto.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 # auto.tcl --
jpayne@69 2 #
jpayne@69 3 # utility procs formerly in init.tcl dealing with auto execution of commands
jpayne@69 4 # and can be auto loaded themselves.
jpayne@69 5 #
jpayne@69 6 # Copyright (c) 1991-1993 The Regents of the University of California.
jpayne@69 7 # Copyright (c) 1994-1998 Sun Microsystems, Inc.
jpayne@69 8 #
jpayne@69 9 # See the file "license.terms" for information on usage and redistribution of
jpayne@69 10 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
jpayne@69 11 #
jpayne@69 12
jpayne@69 13 # auto_reset --
jpayne@69 14 #
jpayne@69 15 # Destroy all cached information for auto-loading and auto-execution, so that
jpayne@69 16 # the information gets recomputed the next time it's needed. Also delete any
jpayne@69 17 # commands that are listed in the auto-load index.
jpayne@69 18 #
jpayne@69 19 # Arguments:
jpayne@69 20 # None.
jpayne@69 21
jpayne@69 22 proc auto_reset {} {
jpayne@69 23 global auto_execs auto_index auto_path
jpayne@69 24 if {[array exists auto_index]} {
jpayne@69 25 foreach cmdName [array names auto_index] {
jpayne@69 26 set fqcn [namespace which $cmdName]
jpayne@69 27 if {$fqcn eq ""} {
jpayne@69 28 continue
jpayne@69 29 }
jpayne@69 30 rename $fqcn {}
jpayne@69 31 }
jpayne@69 32 }
jpayne@69 33 unset -nocomplain auto_execs auto_index ::tcl::auto_oldpath
jpayne@69 34 if {[catch {llength $auto_path}]} {
jpayne@69 35 set auto_path [list [info library]]
jpayne@69 36 } elseif {[info library] ni $auto_path} {
jpayne@69 37 lappend auto_path [info library]
jpayne@69 38 }
jpayne@69 39 }
jpayne@69 40
jpayne@69 41 # tcl_findLibrary --
jpayne@69 42 #
jpayne@69 43 # This is a utility for extensions that searches for a library directory
jpayne@69 44 # using a canonical searching algorithm. A side effect is to source the
jpayne@69 45 # initialization script and set a global library variable.
jpayne@69 46 #
jpayne@69 47 # Arguments:
jpayne@69 48 # basename Prefix of the directory name, (e.g., "tk")
jpayne@69 49 # version Version number of the package, (e.g., "8.0")
jpayne@69 50 # patch Patchlevel of the package, (e.g., "8.0.3")
jpayne@69 51 # initScript Initialization script to source (e.g., tk.tcl)
jpayne@69 52 # enVarName environment variable to honor (e.g., TK_LIBRARY)
jpayne@69 53 # varName Global variable to set when done (e.g., tk_library)
jpayne@69 54
jpayne@69 55 proc tcl_findLibrary {basename version patch initScript enVarName varName} {
jpayne@69 56 upvar #0 $varName the_library
jpayne@69 57 global auto_path env tcl_platform
jpayne@69 58
jpayne@69 59 set dirs {}
jpayne@69 60 set errors {}
jpayne@69 61
jpayne@69 62 # The C application may have hardwired a path, which we honor
jpayne@69 63
jpayne@69 64 if {[info exists the_library] && $the_library ne ""} {
jpayne@69 65 lappend dirs $the_library
jpayne@69 66 } else {
jpayne@69 67 # Do the canonical search
jpayne@69 68
jpayne@69 69 # 1. From an environment variable, if it exists. Placing this first
jpayne@69 70 # gives the end-user ultimate control to work-around any bugs, or
jpayne@69 71 # to customize.
jpayne@69 72
jpayne@69 73 if {[info exists env($enVarName)]} {
jpayne@69 74 lappend dirs $env($enVarName)
jpayne@69 75 }
jpayne@69 76
jpayne@69 77 # 2. In the package script directory registered within the
jpayne@69 78 # configuration of the package itself.
jpayne@69 79
jpayne@69 80 catch {
jpayne@69 81 lappend dirs [::${basename}::pkgconfig get scriptdir,runtime]
jpayne@69 82 }
jpayne@69 83
jpayne@69 84 # 3. Relative to auto_path directories. This checks relative to the
jpayne@69 85 # Tcl library as well as allowing loading of libraries added to the
jpayne@69 86 # auto_path that is not relative to the core library or binary paths.
jpayne@69 87 foreach d $auto_path {
jpayne@69 88 lappend dirs [file join $d $basename$version]
jpayne@69 89 if {$tcl_platform(platform) eq "unix"
jpayne@69 90 && $tcl_platform(os) eq "Darwin"} {
jpayne@69 91 # 4. On MacOSX, check the Resources/Scripts subdir too
jpayne@69 92 lappend dirs [file join $d $basename$version Resources Scripts]
jpayne@69 93 }
jpayne@69 94 }
jpayne@69 95
jpayne@69 96 # 3. Various locations relative to the executable
jpayne@69 97 # ../lib/foo1.0 (From bin directory in install hierarchy)
jpayne@69 98 # ../../lib/foo1.0 (From bin/arch directory in install hierarchy)
jpayne@69 99 # ../library (From unix directory in build hierarchy)
jpayne@69 100 #
jpayne@69 101 # Remaining locations are out of date (when relevant, they ought to be
jpayne@69 102 # covered by the $::auto_path seach above) and disabled.
jpayne@69 103 #
jpayne@69 104 # ../../library (From unix/arch directory in build hierarchy)
jpayne@69 105 # ../../foo1.0.1/library
jpayne@69 106 # (From unix directory in parallel build hierarchy)
jpayne@69 107 # ../../../foo1.0.1/library
jpayne@69 108 # (From unix/arch directory in parallel build hierarchy)
jpayne@69 109
jpayne@69 110 set parentDir [file dirname [file dirname [info nameofexecutable]]]
jpayne@69 111 set grandParentDir [file dirname $parentDir]
jpayne@69 112 lappend dirs [file join $parentDir lib $basename$version]
jpayne@69 113 lappend dirs [file join $grandParentDir lib $basename$version]
jpayne@69 114 lappend dirs [file join $parentDir library]
jpayne@69 115 if {0} {
jpayne@69 116 lappend dirs [file join $grandParentDir library]
jpayne@69 117 lappend dirs [file join $grandParentDir $basename$patch library]
jpayne@69 118 lappend dirs [file join [file dirname $grandParentDir] \
jpayne@69 119 $basename$patch library]
jpayne@69 120 }
jpayne@69 121 }
jpayne@69 122 # uniquify $dirs in order
jpayne@69 123 array set seen {}
jpayne@69 124 foreach i $dirs {
jpayne@69 125 # Make sure $i is unique under normalization. Avoid repeated [source].
jpayne@69 126 if {[interp issafe]} {
jpayne@69 127 # Safe interps have no [file normalize].
jpayne@69 128 set norm $i
jpayne@69 129 } else {
jpayne@69 130 set norm [file normalize $i]
jpayne@69 131 }
jpayne@69 132 if {[info exists seen($norm)]} {
jpayne@69 133 continue
jpayne@69 134 }
jpayne@69 135 set seen($norm) {}
jpayne@69 136
jpayne@69 137 set the_library $i
jpayne@69 138 set file [file join $i $initScript]
jpayne@69 139
jpayne@69 140 # source everything when in a safe interpreter because we have a
jpayne@69 141 # source command, but no file exists command
jpayne@69 142
jpayne@69 143 if {[interp issafe] || [file exists $file]} {
jpayne@69 144 if {![catch {uplevel #0 [list source $file]} msg opts]} {
jpayne@69 145 return
jpayne@69 146 }
jpayne@69 147 append errors "$file: $msg\n"
jpayne@69 148 append errors [dict get $opts -errorinfo]\n
jpayne@69 149 }
jpayne@69 150 }
jpayne@69 151 unset -nocomplain the_library
jpayne@69 152 set msg "Can't find a usable $initScript in the following directories: \n"
jpayne@69 153 append msg " $dirs\n\n"
jpayne@69 154 append msg "$errors\n\n"
jpayne@69 155 append msg "This probably means that $basename wasn't installed properly.\n"
jpayne@69 156 error $msg
jpayne@69 157 }
jpayne@69 158
jpayne@69 159
jpayne@69 160 # ----------------------------------------------------------------------
jpayne@69 161 # auto_mkindex
jpayne@69 162 # ----------------------------------------------------------------------
jpayne@69 163 # The following procedures are used to generate the tclIndex file from Tcl
jpayne@69 164 # source files. They use a special safe interpreter to parse Tcl source
jpayne@69 165 # files, writing out index entries as "proc" commands are encountered. This
jpayne@69 166 # implementation won't work in a safe interpreter, since a safe interpreter
jpayne@69 167 # can't create the special parser and mess with its commands.
jpayne@69 168
jpayne@69 169 if {[interp issafe]} {
jpayne@69 170 return ;# Stop sourcing the file here
jpayne@69 171 }
jpayne@69 172
jpayne@69 173 # auto_mkindex --
jpayne@69 174 # Regenerate a tclIndex file from Tcl source files. Takes as argument the
jpayne@69 175 # name of the directory in which the tclIndex file is to be placed, followed
jpayne@69 176 # by any number of glob patterns to use in that directory to locate all of the
jpayne@69 177 # relevant files.
jpayne@69 178 #
jpayne@69 179 # Arguments:
jpayne@69 180 # dir - Name of the directory in which to create an index.
jpayne@69 181
jpayne@69 182 # args - Any number of additional arguments giving the names of files
jpayne@69 183 # within dir. If no additional are given auto_mkindex will look
jpayne@69 184 # for *.tcl.
jpayne@69 185
jpayne@69 186 proc auto_mkindex {dir args} {
jpayne@69 187 if {[interp issafe]} {
jpayne@69 188 error "can't generate index within safe interpreter"
jpayne@69 189 }
jpayne@69 190
jpayne@69 191 set oldDir [pwd]
jpayne@69 192 cd $dir
jpayne@69 193
jpayne@69 194 append index "# Tcl autoload index file, version 2.0\n"
jpayne@69 195 append index "# This file is generated by the \"auto_mkindex\" command\n"
jpayne@69 196 append index "# and sourced to set up indexing information for one or\n"
jpayne@69 197 append index "# more commands. Typically each line is a command that\n"
jpayne@69 198 append index "# sets an element in the auto_index array, where the\n"
jpayne@69 199 append index "# element name is the name of a command and the value is\n"
jpayne@69 200 append index "# a script that loads the command.\n\n"
jpayne@69 201 if {![llength $args]} {
jpayne@69 202 set args *.tcl
jpayne@69 203 }
jpayne@69 204
jpayne@69 205 auto_mkindex_parser::init
jpayne@69 206 foreach file [lsort [glob -- {*}$args]] {
jpayne@69 207 try {
jpayne@69 208 append index [auto_mkindex_parser::mkindex $file]
jpayne@69 209 } on error {msg opts} {
jpayne@69 210 cd $oldDir
jpayne@69 211 return -options $opts $msg
jpayne@69 212 }
jpayne@69 213 }
jpayne@69 214 auto_mkindex_parser::cleanup
jpayne@69 215
jpayne@69 216 set fid [open "tclIndex" w]
jpayne@69 217 puts -nonewline $fid $index
jpayne@69 218 close $fid
jpayne@69 219 cd $oldDir
jpayne@69 220 }
jpayne@69 221
jpayne@69 222 # Original version of auto_mkindex that just searches the source code for
jpayne@69 223 # "proc" at the beginning of the line.
jpayne@69 224
jpayne@69 225 proc auto_mkindex_old {dir args} {
jpayne@69 226 set oldDir [pwd]
jpayne@69 227 cd $dir
jpayne@69 228 set dir [pwd]
jpayne@69 229 append index "# Tcl autoload index file, version 2.0\n"
jpayne@69 230 append index "# This file is generated by the \"auto_mkindex\" command\n"
jpayne@69 231 append index "# and sourced to set up indexing information for one or\n"
jpayne@69 232 append index "# more commands. Typically each line is a command that\n"
jpayne@69 233 append index "# sets an element in the auto_index array, where the\n"
jpayne@69 234 append index "# element name is the name of a command and the value is\n"
jpayne@69 235 append index "# a script that loads the command.\n\n"
jpayne@69 236 if {![llength $args]} {
jpayne@69 237 set args *.tcl
jpayne@69 238 }
jpayne@69 239 foreach file [lsort [glob -- {*}$args]] {
jpayne@69 240 set f ""
jpayne@69 241 set error [catch {
jpayne@69 242 set f [open $file]
jpayne@69 243 fconfigure $f -eofchar "\032 {}"
jpayne@69 244 while {[gets $f line] >= 0} {
jpayne@69 245 if {[regexp {^proc[ ]+([^ ]*)} $line match procName]} {
jpayne@69 246 set procName [lindex [auto_qualify $procName "::"] 0]
jpayne@69 247 append index "set [list auto_index($procName)]"
jpayne@69 248 append index " \[list source \[file join \$dir [list $file]\]\]\n"
jpayne@69 249 }
jpayne@69 250 }
jpayne@69 251 close $f
jpayne@69 252 } msg opts]
jpayne@69 253 if {$error} {
jpayne@69 254 catch {close $f}
jpayne@69 255 cd $oldDir
jpayne@69 256 return -options $opts $msg
jpayne@69 257 }
jpayne@69 258 }
jpayne@69 259 set f ""
jpayne@69 260 set error [catch {
jpayne@69 261 set f [open tclIndex w]
jpayne@69 262 puts -nonewline $f $index
jpayne@69 263 close $f
jpayne@69 264 cd $oldDir
jpayne@69 265 } msg opts]
jpayne@69 266 if {$error} {
jpayne@69 267 catch {close $f}
jpayne@69 268 cd $oldDir
jpayne@69 269 error $msg $info $code
jpayne@69 270 return -options $opts $msg
jpayne@69 271 }
jpayne@69 272 }
jpayne@69 273
jpayne@69 274 # Create a safe interpreter that can be used to parse Tcl source files
jpayne@69 275 # generate a tclIndex file for autoloading. This interp contains commands for
jpayne@69 276 # things that need index entries. Each time a command is executed, it writes
jpayne@69 277 # an entry out to the index file.
jpayne@69 278
jpayne@69 279 namespace eval auto_mkindex_parser {
jpayne@69 280 variable parser "" ;# parser used to build index
jpayne@69 281 variable index "" ;# maintains index as it is built
jpayne@69 282 variable scriptFile "" ;# name of file being processed
jpayne@69 283 variable contextStack "" ;# stack of namespace scopes
jpayne@69 284 variable imports "" ;# keeps track of all imported cmds
jpayne@69 285 variable initCommands ;# list of commands that create aliases
jpayne@69 286 if {![info exists initCommands]} {
jpayne@69 287 set initCommands [list]
jpayne@69 288 }
jpayne@69 289
jpayne@69 290 proc init {} {
jpayne@69 291 variable parser
jpayne@69 292 variable initCommands
jpayne@69 293
jpayne@69 294 if {![interp issafe]} {
jpayne@69 295 set parser [interp create -safe]
jpayne@69 296 $parser hide info
jpayne@69 297 $parser hide rename
jpayne@69 298 $parser hide proc
jpayne@69 299 $parser hide namespace
jpayne@69 300 $parser hide eval
jpayne@69 301 $parser hide puts
jpayne@69 302 foreach ns [$parser invokehidden namespace children ::] {
jpayne@69 303 # MUST NOT DELETE "::tcl" OR BAD THINGS HAPPEN!
jpayne@69 304 if {$ns eq "::tcl"} continue
jpayne@69 305 $parser invokehidden namespace delete $ns
jpayne@69 306 }
jpayne@69 307 foreach cmd [$parser invokehidden info commands ::*] {
jpayne@69 308 $parser invokehidden rename $cmd {}
jpayne@69 309 }
jpayne@69 310 $parser invokehidden proc unknown {args} {}
jpayne@69 311
jpayne@69 312 # We'll need access to the "namespace" command within the
jpayne@69 313 # interp. Put it back, but move it out of the way.
jpayne@69 314
jpayne@69 315 $parser expose namespace
jpayne@69 316 $parser invokehidden rename namespace _%@namespace
jpayne@69 317 $parser expose eval
jpayne@69 318 $parser invokehidden rename eval _%@eval
jpayne@69 319
jpayne@69 320 # Install all the registered psuedo-command implementations
jpayne@69 321
jpayne@69 322 foreach cmd $initCommands {
jpayne@69 323 eval $cmd
jpayne@69 324 }
jpayne@69 325 }
jpayne@69 326 }
jpayne@69 327 proc cleanup {} {
jpayne@69 328 variable parser
jpayne@69 329 interp delete $parser
jpayne@69 330 unset parser
jpayne@69 331 }
jpayne@69 332 }
jpayne@69 333
jpayne@69 334 # auto_mkindex_parser::mkindex --
jpayne@69 335 #
jpayne@69 336 # Used by the "auto_mkindex" command to create a "tclIndex" file for the given
jpayne@69 337 # Tcl source file. Executes the commands in the file, and handles things like
jpayne@69 338 # the "proc" command by adding an entry for the index file. Returns a string
jpayne@69 339 # that represents the index file.
jpayne@69 340 #
jpayne@69 341 # Arguments:
jpayne@69 342 # file Name of Tcl source file to be indexed.
jpayne@69 343
jpayne@69 344 proc auto_mkindex_parser::mkindex {file} {
jpayne@69 345 variable parser
jpayne@69 346 variable index
jpayne@69 347 variable scriptFile
jpayne@69 348 variable contextStack
jpayne@69 349 variable imports
jpayne@69 350
jpayne@69 351 set scriptFile $file
jpayne@69 352
jpayne@69 353 set fid [open $file]
jpayne@69 354 fconfigure $fid -eofchar "\032 {}"
jpayne@69 355 set contents [read $fid]
jpayne@69 356 close $fid
jpayne@69 357
jpayne@69 358 # There is one problem with sourcing files into the safe interpreter:
jpayne@69 359 # references like "$x" will fail since code is not really being executed
jpayne@69 360 # and variables do not really exist. To avoid this, we replace all $ with
jpayne@69 361 # \0 (literally, the null char) later, when getting proc names we will
jpayne@69 362 # have to reverse this replacement, in case there were any $ in the proc
jpayne@69 363 # name. This will cause a problem if somebody actually tries to have a \0
jpayne@69 364 # in their proc name. Too bad for them.
jpayne@69 365 set contents [string map [list \$ \0] $contents]
jpayne@69 366
jpayne@69 367 set index ""
jpayne@69 368 set contextStack ""
jpayne@69 369 set imports ""
jpayne@69 370
jpayne@69 371 $parser eval $contents
jpayne@69 372
jpayne@69 373 foreach name $imports {
jpayne@69 374 catch {$parser eval [list _%@namespace forget $name]}
jpayne@69 375 }
jpayne@69 376 return $index
jpayne@69 377 }
jpayne@69 378
jpayne@69 379 # auto_mkindex_parser::hook command
jpayne@69 380 #
jpayne@69 381 # Registers a Tcl command to evaluate when initializing the child interpreter
jpayne@69 382 # used by the mkindex parser. The command is evaluated in the parent
jpayne@69 383 # interpreter, and can use the variable auto_mkindex_parser::parser to get to
jpayne@69 384 # the child
jpayne@69 385
jpayne@69 386 proc auto_mkindex_parser::hook {cmd} {
jpayne@69 387 variable initCommands
jpayne@69 388
jpayne@69 389 lappend initCommands $cmd
jpayne@69 390 }
jpayne@69 391
jpayne@69 392 # auto_mkindex_parser::slavehook command
jpayne@69 393 #
jpayne@69 394 # Registers a Tcl command to evaluate when initializing the child interpreter
jpayne@69 395 # used by the mkindex parser. The command is evaluated in the child
jpayne@69 396 # interpreter.
jpayne@69 397
jpayne@69 398 proc auto_mkindex_parser::slavehook {cmd} {
jpayne@69 399 variable initCommands
jpayne@69 400
jpayne@69 401 # The $parser variable is defined to be the name of the child interpreter
jpayne@69 402 # when this command is used later.
jpayne@69 403
jpayne@69 404 lappend initCommands "\$parser eval [list $cmd]"
jpayne@69 405 }
jpayne@69 406
jpayne@69 407 # auto_mkindex_parser::command --
jpayne@69 408 #
jpayne@69 409 # Registers a new command with the "auto_mkindex_parser" interpreter that
jpayne@69 410 # parses Tcl files. These commands are fake versions of things like the
jpayne@69 411 # "proc" command. When you execute them, they simply write out an entry to a
jpayne@69 412 # "tclIndex" file for auto-loading.
jpayne@69 413 #
jpayne@69 414 # This procedure allows extensions to register their own commands with the
jpayne@69 415 # auto_mkindex facility. For example, a package like [incr Tcl] might
jpayne@69 416 # register a "class" command so that class definitions could be added to a
jpayne@69 417 # "tclIndex" file for auto-loading.
jpayne@69 418 #
jpayne@69 419 # Arguments:
jpayne@69 420 # name Name of command recognized in Tcl files.
jpayne@69 421 # arglist Argument list for command.
jpayne@69 422 # body Implementation of command to handle indexing.
jpayne@69 423
jpayne@69 424 proc auto_mkindex_parser::command {name arglist body} {
jpayne@69 425 hook [list auto_mkindex_parser::commandInit $name $arglist $body]
jpayne@69 426 }
jpayne@69 427
jpayne@69 428 # auto_mkindex_parser::commandInit --
jpayne@69 429 #
jpayne@69 430 # This does the actual work set up by auto_mkindex_parser::command. This is
jpayne@69 431 # called when the interpreter used by the parser is created.
jpayne@69 432 #
jpayne@69 433 # Arguments:
jpayne@69 434 # name Name of command recognized in Tcl files.
jpayne@69 435 # arglist Argument list for command.
jpayne@69 436 # body Implementation of command to handle indexing.
jpayne@69 437
jpayne@69 438 proc auto_mkindex_parser::commandInit {name arglist body} {
jpayne@69 439 variable parser
jpayne@69 440
jpayne@69 441 set ns [namespace qualifiers $name]
jpayne@69 442 set tail [namespace tail $name]
jpayne@69 443 if {$ns eq ""} {
jpayne@69 444 set fakeName [namespace current]::_%@fake_$tail
jpayne@69 445 } else {
jpayne@69 446 set fakeName [namespace current]::[string map {:: _} _%@fake_$name]
jpayne@69 447 }
jpayne@69 448 proc $fakeName $arglist $body
jpayne@69 449
jpayne@69 450 # YUK! Tcl won't let us alias fully qualified command names, so we can't
jpayne@69 451 # handle names like "::itcl::class". Instead, we have to build procs with
jpayne@69 452 # the fully qualified names, and have the procs point to the aliases.
jpayne@69 453
jpayne@69 454 if {[string match *::* $name]} {
jpayne@69 455 set exportCmd [list _%@namespace export [namespace tail $name]]
jpayne@69 456 $parser eval [list _%@namespace eval $ns $exportCmd]
jpayne@69 457
jpayne@69 458 # The following proc definition does not work if you want to tolerate
jpayne@69 459 # space or something else diabolical in the procedure name, (i.e.,
jpayne@69 460 # space in $alias). The following does not work:
jpayne@69 461 # "_%@eval {$alias} \$args"
jpayne@69 462 # because $alias gets concat'ed to $args. The following does not work
jpayne@69 463 # because $cmd is somehow undefined
jpayne@69 464 # "set cmd {$alias} \; _%@eval {\$cmd} \$args"
jpayne@69 465 # A gold star to someone that can make test autoMkindex-3.3 work
jpayne@69 466 # properly
jpayne@69 467
jpayne@69 468 set alias [namespace tail $fakeName]
jpayne@69 469 $parser invokehidden proc $name {args} "_%@eval {$alias} \$args"
jpayne@69 470 $parser alias $alias $fakeName
jpayne@69 471 } else {
jpayne@69 472 $parser alias $name $fakeName
jpayne@69 473 }
jpayne@69 474 return
jpayne@69 475 }
jpayne@69 476
jpayne@69 477 # auto_mkindex_parser::fullname --
jpayne@69 478 #
jpayne@69 479 # Used by commands like "proc" within the auto_mkindex parser. Returns the
jpayne@69 480 # qualified namespace name for the "name" argument. If the "name" does not
jpayne@69 481 # start with "::", elements are added from the current namespace stack to
jpayne@69 482 # produce a qualified name. Then, the name is examined to see whether or not
jpayne@69 483 # it should really be qualified. If the name has more than the leading "::",
jpayne@69 484 # it is returned as a fully qualified name. Otherwise, it is returned as a
jpayne@69 485 # simple name. That way, the Tcl autoloader will recognize it properly.
jpayne@69 486 #
jpayne@69 487 # Arguments:
jpayne@69 488 # name - Name that is being added to index.
jpayne@69 489
jpayne@69 490 proc auto_mkindex_parser::fullname {name} {
jpayne@69 491 variable contextStack
jpayne@69 492
jpayne@69 493 if {![string match ::* $name]} {
jpayne@69 494 foreach ns $contextStack {
jpayne@69 495 set name "${ns}::$name"
jpayne@69 496 if {[string match ::* $name]} {
jpayne@69 497 break
jpayne@69 498 }
jpayne@69 499 }
jpayne@69 500 }
jpayne@69 501
jpayne@69 502 if {[namespace qualifiers $name] eq ""} {
jpayne@69 503 set name [namespace tail $name]
jpayne@69 504 } elseif {![string match ::* $name]} {
jpayne@69 505 set name "::$name"
jpayne@69 506 }
jpayne@69 507
jpayne@69 508 # Earlier, mkindex replaced all $'s with \0. Now, we have to reverse that
jpayne@69 509 # replacement.
jpayne@69 510 return [string map [list \0 \$] $name]
jpayne@69 511 }
jpayne@69 512
jpayne@69 513 # auto_mkindex_parser::indexEntry --
jpayne@69 514 #
jpayne@69 515 # Used by commands like "proc" within the auto_mkindex parser to add a
jpayne@69 516 # correctly-quoted entry to the index. This is shared code so it is done
jpayne@69 517 # *right*, in one place.
jpayne@69 518 #
jpayne@69 519 # Arguments:
jpayne@69 520 # name - Name that is being added to index.
jpayne@69 521
jpayne@69 522 proc auto_mkindex_parser::indexEntry {name} {
jpayne@69 523 variable index
jpayne@69 524 variable scriptFile
jpayne@69 525
jpayne@69 526 # We convert all metacharacters to their backslashed form, and pre-split
jpayne@69 527 # the file name that we know about (which will be a proper list, and so
jpayne@69 528 # correctly quoted).
jpayne@69 529
jpayne@69 530 set name [string range [list \}[fullname $name]] 2 end]
jpayne@69 531 set filenameParts [file split $scriptFile]
jpayne@69 532
jpayne@69 533 append index [format \
jpayne@69 534 {set auto_index(%s) [list source [file join $dir %s]]%s} \
jpayne@69 535 $name $filenameParts \n]
jpayne@69 536 return
jpayne@69 537 }
jpayne@69 538
jpayne@69 539 if {[llength $::auto_mkindex_parser::initCommands]} {
jpayne@69 540 return
jpayne@69 541 }
jpayne@69 542
jpayne@69 543 # Register all of the procedures for the auto_mkindex parser that will build
jpayne@69 544 # the "tclIndex" file.
jpayne@69 545
jpayne@69 546 # AUTO MKINDEX: proc name arglist body
jpayne@69 547 # Adds an entry to the auto index list for the given procedure name.
jpayne@69 548
jpayne@69 549 auto_mkindex_parser::command proc {name args} {
jpayne@69 550 indexEntry $name
jpayne@69 551 }
jpayne@69 552
jpayne@69 553 # Conditionally add support for Tcl byte code files. There are some tricky
jpayne@69 554 # details here. First, we need to get the tbcload library initialized in the
jpayne@69 555 # current interpreter. We cannot load tbcload into the child until we have
jpayne@69 556 # done so because it needs access to the tcl_patchLevel variable. Second,
jpayne@69 557 # because the package index file may defer loading the library until we invoke
jpayne@69 558 # a command, we need to explicitly invoke auto_load to force it to be loaded.
jpayne@69 559 # This should be a noop if the package has already been loaded
jpayne@69 560
jpayne@69 561 auto_mkindex_parser::hook {
jpayne@69 562 try {
jpayne@69 563 package require tbcload
jpayne@69 564 } on error {} {
jpayne@69 565 # OK, don't have it so do nothing
jpayne@69 566 } on ok {} {
jpayne@69 567 if {[namespace which -command tbcload::bcproc] eq ""} {
jpayne@69 568 auto_load tbcload::bcproc
jpayne@69 569 }
jpayne@69 570 load {} tbcload $auto_mkindex_parser::parser
jpayne@69 571
jpayne@69 572 # AUTO MKINDEX: tbcload::bcproc name arglist body
jpayne@69 573 # Adds an entry to the auto index list for the given pre-compiled
jpayne@69 574 # procedure name.
jpayne@69 575
jpayne@69 576 auto_mkindex_parser::commandInit tbcload::bcproc {name args} {
jpayne@69 577 indexEntry $name
jpayne@69 578 }
jpayne@69 579 }
jpayne@69 580 }
jpayne@69 581
jpayne@69 582 # AUTO MKINDEX: namespace eval name command ?arg arg...?
jpayne@69 583 # Adds the namespace name onto the context stack and evaluates the associated
jpayne@69 584 # body of commands.
jpayne@69 585 #
jpayne@69 586 # AUTO MKINDEX: namespace import ?-force? pattern ?pattern...?
jpayne@69 587 # Performs the "import" action in the parser interpreter. This is important
jpayne@69 588 # for any commands contained in a namespace that affect the index. For
jpayne@69 589 # example, a script may say "itcl::class ...", or it may import "itcl::*" and
jpayne@69 590 # then say "class ...". This procedure does the import operation, but keeps
jpayne@69 591 # track of imported patterns so we can remove the imports later.
jpayne@69 592
jpayne@69 593 auto_mkindex_parser::command namespace {op args} {
jpayne@69 594 switch -- $op {
jpayne@69 595 eval {
jpayne@69 596 variable parser
jpayne@69 597 variable contextStack
jpayne@69 598
jpayne@69 599 set name [lindex $args 0]
jpayne@69 600 set args [lrange $args 1 end]
jpayne@69 601
jpayne@69 602 set contextStack [linsert $contextStack 0 $name]
jpayne@69 603 $parser eval [list _%@namespace eval $name] $args
jpayne@69 604 set contextStack [lrange $contextStack 1 end]
jpayne@69 605 }
jpayne@69 606 import {
jpayne@69 607 variable parser
jpayne@69 608 variable imports
jpayne@69 609 foreach pattern $args {
jpayne@69 610 if {$pattern ne "-force"} {
jpayne@69 611 lappend imports $pattern
jpayne@69 612 }
jpayne@69 613 }
jpayne@69 614 catch {$parser eval "_%@namespace import $args"}
jpayne@69 615 }
jpayne@69 616 ensemble {
jpayne@69 617 variable parser
jpayne@69 618 variable contextStack
jpayne@69 619 if {[lindex $args 0] eq "create"} {
jpayne@69 620 set name ::[join [lreverse $contextStack] ::]
jpayne@69 621 catch {
jpayne@69 622 set name [dict get [lrange $args 1 end] -command]
jpayne@69 623 if {![string match ::* $name]} {
jpayne@69 624 set name ::[join [lreverse $contextStack] ::]$name
jpayne@69 625 }
jpayne@69 626 regsub -all ::+ $name :: name
jpayne@69 627 }
jpayne@69 628 # create artifical proc to force an entry in the tclIndex
jpayne@69 629 $parser eval [list ::proc $name {} {}]
jpayne@69 630 }
jpayne@69 631 }
jpayne@69 632 }
jpayne@69 633 }
jpayne@69 634
jpayne@69 635 # AUTO MKINDEX: oo::class create name ?definition?
jpayne@69 636 # Adds an entry to the auto index list for the given class name.
jpayne@69 637 auto_mkindex_parser::command oo::class {op name {body ""}} {
jpayne@69 638 if {$op eq "create"} {
jpayne@69 639 indexEntry $name
jpayne@69 640 }
jpayne@69 641 }
jpayne@69 642 auto_mkindex_parser::command class {op name {body ""}} {
jpayne@69 643 if {$op eq "create"} {
jpayne@69 644 indexEntry $name
jpayne@69 645 }
jpayne@69 646 }
jpayne@69 647
jpayne@69 648 return