Purpose: to document the use of the new '''namespace ensemble''' functions. http://tip.tcl.tk/112 ---- From the Tcl 8.5 namespace manpage http://www.tcl.tk/man/tcl8.5/TclCmd/namespace.htm: '''ENSEMBLES''' The namespace ensemble is used to create and manipulate ensemble commands, which are commands formed by grouping subcommands together. The commands typically come from the current namespace when the ensemble was created, though this is configurable. Note that there may be any number of ensembles associated with any namespace (including none, which is true of all namespaces by default), though all the ensembles associated with a namespace are deleted when that namespace is deleted. The link between an ensemble command and its namespace is maintained however the ensemble is renamed. Three subcommands of the namespace ensemble command are defined: '''namespace ensemble create''' ''?option value ...?'': Creates a new ensemble command linked to the current namespace, returning the fully qualified name of the command created. The arguments to namespace ensemble create allow the configuration of the command as if with the namespace ensemble configure command. If not overridden with the -command option, this command creates an ensemble with exactly the same name as the linked namespace. See the section ENSEMBLE OPTIONS below for a full list of options supported and their effects. '''namespace ensemble configure''' ''command ?option? ?value ...?'': Retrieves the value of an option associated with the ensemble command named command, or updates some options associated with that ensemble command. See the section ENSEMBLE OPTIONS below for a full list of options supported and their effects. '''namespace ensemble exists''' ''command'': Returns a boolean value that describes whether the command command exists and is an ensemble command. This command only ever returns an error if the number of arguments to the command is wrong. When called, an ensemble command takes its first argument and looks it up (according to the rules described below) to discover a list of words to replace the ensemble command and subcommand with. The resulting list of words is then evaluated (with no further substitutions) as if that was what was typed originally (i.e. by passing the list of words through Tcl_EvalObjv) and returning the result of the command. Note that it is legal to make the target of an ensemble rewrite be another (or even the same) ensemble command. The ensemble command will not be visible through the use of the uplevel or info level commands. '''ENSEMBLE OPTIONS''' The following options, supported by the namespace ensemble create and namespace ensemble configure commands, control how an ensemble command behaves: -map: When non-empty, this option supplies a dictionary that provides a mapping from subcommand names to a list of prefix words to substitute in place of the ensemble command and subcommand words (in a manner similar to an alias created with [interp alias]; the words are not reparsed after substitution). When this option is empty, the mapping will be from the local name of the subcommand to its fully-qualified name. Note that when this option is non-empty and the -subcommands option is empty, the ensemble subcommand names will be exactly those words that have mappings in the dictionary. -prefixes: This option (which is enabled by default) controls whether the ensemble command recognizes unambiguous prefixes of its subcommands. When turned off, the ensemble command requires exact matching of subcommand names. -subcommands: When non-empty, this option lists exactly what subcommands are in the ensemble. The mapping for each of those commands will either whatever is defined in the -map option, or to the command with the same name in the namespace linked to the ensemble. If this option is empty, the subcommands of the namespace will either be the keys of the dictionary listed in the -map option or the exported commands of the linked namespace at the time of the invocation of the ensemble command. -unknown: When non-empty, this option provides a partial command (to which all the words that are arguments to the ensemble command, including the fully-qualified name of the ensemble, are appended) to handle the case where an ensemble subcommand is not recognized and would otherwise generate an error. When empty (the default) an error (in the style of Tcl_GetIndexFromObj) is generated whenever the ensemble is unable to determine how to implement a particular subcommand. See UNKNOWN HANDLER BEHAVIOUR for more details. The following extra option is allowed by '''namespace ensemble create''': -command: This write-only option allows the name of the ensemble created by '''namespace ensemble create''' to be anything in any existing namespace. The default value for this option is the fully-qualified name of the namespace in which the namespace ensemble create command is invoked. The following extra option is allowed by '''namespace ensemble configure''': -namespace: This read-only option allows the retrieval of the fully-qualified name of the namespace which the ensemble was created within. '''UNKNOWN HANDLER BEHAVIOUR''' If an unknown handler is specified for an ensemble, that handler is called when the ensemble command would otherwise return an error due to it being unable to decide which subcommand to invoke. The exact conditions under which that occurs are controlled by the -subcommands, -map and -prefixes options as described above. To execute the unknown handler, the ensemble mechanism takes the specified -unknown option and appends each argument of the attempted ensemble command invocation (including the ensemble command itself, expressed as a fully qualified name). It invokes the resulting command in the scope of the attempted call. If the execution of the unknown handler terminates normally, the ensemble engine reparses the subcommand (as described below) and tries to dispatch it again, which is ideal for when the ensemble's configuration has been updated by the unknown subcommand handler. Any other kind of termination of the unknown handler is treated as an error. The result of the unknown handler is expected to be a list (it is an error if it is not). If the list is an empty list, the ensemble command attempts to look up the original subcommand again and, if it is not found this time, an error will be generated just as if the -unknown handler was not there (i.e. for any particular invocation of an ensemble, its unknown handler will be called at most once.) This makes it easy for the unknown handler to update the ensemble or its backing namespace so as to provide an implementation of the desired subcommand and reparse. When the result is a non-empty list, the words of that list are used to replace the ensemble command and subcommand, just as if they had been looked up in the -map. It is up to the unknown handler to supply all namespace qualifiers if the implementing subcommand is not in the namespace of the caller of the ensemble command. Also note that when ensemble commands are chained (e.g. if you make one of the commands that implement an ensemble subcommand into an ensemble, in a manner similar to the [text] widget's tag and mark subcommands) then the rewrite happens in the context of the caller of the outermost ensemble. That is to say that ensembles do not in themselves place any namespace contexts on the Tcl call stack. Where an empty -unknown handler is given (the default), the ensemble command will generate an error message based on the list of commands that the ensemble has defined (formatted similarly to the error message from Tcl_GetIndexFromObj). This is the error that will be thrown when the subcommand is still not recognized during reparsing. It is also an error for an -unknown handler to delete its namespace. ---- [LV] Anyone feel up to writing an example of using this facility to add a new command to one of the Tcl core namespaces, so that the command was available? Perhaps adding a new math function to [expr]? ---- [Lars H]: Does anyone feel up to writing a [namespace ensemble] emulation for Tcl 8.4? The following is a start, but it lacks most of the features. proc "namespace ensemble" {subcmd args} { switch -- $subcmd "configure" { error "namespace ensemble configure not supported (sorry!)" } "exists" { error "namespace ensemble exists not supported (sorry!)" } "create" {} default { error "unknown subcommand $subcmd, must be create" } array set Opt $args foreach o {-map -prefixes -subcommands -unknown} { if {[info exists Opt($o)]} then { error "$o option not supported (sorry!)" } } set ns [uplevel 1 {::namespace current}] if {![info exists Opt(-command)]} then { set Opt(-command) $ns } elseif {![string match ::* $Opt(-command)]} then { set Opt(-command) ${ns}::$Opt(-command) } interp alias {} $Opt(-command) {} ::ensemble_dispatch_0 $ns } proc ensemble_dispatch_0 {ns subcmd args} { set cmdL [list] foreach pat [namespace eval $ns {::namespace export}] { eval [list lappend cmdL] [ namespace eval $ns [list ::info commands $pat] ] } set matchL [lsearch -all -inline -glob $cmdL\ [string map {* \\* [ \\[ ] \\] ? \\?} $subcmd]*] if {[llength $matchL] == 1} then { uplevel 1 [list ${ns}::[lindex $matchL 0]] $args } elseif {[llength $matchL] > 1} then { error "ambiguous subcommand \"$subcmd\": matches\ [join [lsort -dictionary $matchL] ", "]" } else { error "unknown subcommand \"$subcmd\": must be\ [join [lsort -dictionary $cmdL] ", "]" } } ---- ''[escargo] 28 Mar 2005'' - Reading the description above about the '''-command''' option, and seeing it described as a "write-only option" seems to me to be a problem. Does it make sense to specify an option whose value cannot be read? Is there some other way to determine what value was passed via -command? [WHD]: In order to retrieve any of the options, you need the name of the ensemble, i.e., the command name. So reading the value of -command would just give you back the command name you already had. ''[escargo] 29 Mar 2005'' - OK. So it's not so much a write-only option as an option that would be pointless to read. (Unless there was something that could be renamed, and you wanted to find its original name, which may not be the case here.) ---- [Category Command]