vcanim.gif (8357 bytes)backquote
Home ] Sales ] Products ] Services ] Support ] Downloads ]

 

 


backquote

backquote, written by Hume Smith, is a Tcl version of  the lisp backquote macro notation.  The following is documentation from Smiths implementation.

NAME
` - Do partial evaluations within a string

SYNOPSIS
` ?array? string

DESCRIPTION
This procedure is a fancier version of subst. array is the name of a local array defining quoting schemes. If omitted, the global array ` is used instead. It is initialized so:
array set ` {, `quote @ {}}
string is searched left-to-right for c$ and c[, where c is any single-character key of the scheme array (other keys are ignored). When such patterns are found, the usual $- or [-form following is extracted and evaluated in the calling context. Then the quoter named by array(c) is applied and the result replaces the extracted form. A null quoter name indicates insert as-is.
Other occurrences of the array indices alone are ignored; they are only noticed when paired with $ and [. I therefore felt the need for an escaping mechanism was tiny enough to give it a miss, especially since you can create your own schemes.
EXAMPLES
This procedure is intended as way to make code fragments which need a few creation-time evaluations, while avoiding the quoting hell often required by basic TCL. It was inspired by Lisp's backquote macro, but don't read too much into that; TCL has its own peculiar requirements.
Consider this from one of my own programs:
set $foo [list okay $var(f$ind) $var(s$ind)]
where I want to replace $foo and $ind before storing this as a Button's -command. To do so robustly (accounting for the possibility of nasty characters) with plain TCL requires something along these lines:
... -command " set [list $foo] \[list okay \[set [list var(f$ind)]\] \[set [list var(s$ind)]\]\] "
which I think you'll agree is pretty difficult to read. With `, I can cut it down to
... -command [` { set ,$foo [list okay $var(f,$ind) $var(s,$ind)] }]
My , quoter (proc `quote) replaces nasty characters with octals so the replacement string will generally read as one word. If we've
set foo bar
set ind x)y
then the above -command will be
set bar [list okay $var(fx\051y) $var(sx\051y)]
If you don't want nasties quoted, think about using @. Suppose you've built a list that you want "spliced" into your code fragment:
set z [list foo bar baz]
... -command [` { lappend collection @$z }]
would set the -command to
lappend collection foo bar baz
BUGS
Global array `. It has the usual problems of a global variable, but the advantage of it being very easy to create your own default schemes.
Global array ascii, used by proc `quote. How I wish there were a faster way to get character codes.
Can't use - and ] as quoting scheme names.
I suppose the schemes should be like tracers and -xscrollcommands; a list which gets another element appended and then eval-ed. So, for instance, I could easily use {format 0x%04x} for something I expect to be an integer.
There should be a way to define schemes with commandline arguments alone, without an array.
According to the TCL manpage, the REs for $ forms are \${[^}]*} and \$[a-zA-Z0-9_]+(\(...\))?, except that I have no idea what can go into an array index. A little experimentation hints it has to satisfy info complete, but I have no definite word.
Hume Smith TCL/Tk Stuff backquote.tcl
1996 Dec 20, updated 199- --- --


Behaviors Overview ] Behavior Evaluate ] Behavior Edit ] Debugger ] Console ] Find ] SAA Library ] SW library ] SWC library ] SWN library index ] SWU library ] Attributes Index ] Scripting Examples ]

copyright Video-Collage Inc.