File: expander.n

package info (click to toggle)
tcllib 2.0%2Bdfsg-4
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 83,572 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (766 lines) | stat: -rw-r--r-- 22,546 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
'\"
'\" Generated from file 'expander\&.man' by tcllib/doctools with format 'nroff'
'\" Copyright (c) William H\&. Duquette, http://www\&.wjduquette\&.com/expand
'\"
.TH "textutil::expander" n 1\&.3\&.2 tcllib "Text and string utilities, macro processing"
.\" The -*- nroff -*- definitions below are for supplemental macros used
.\" in Tcl/Tk manual entries.
.\"
.\" .AP type name in/out ?indent?
.\"	Start paragraph describing an argument to a library procedure.
.\"	type is type of argument (int, etc.), in/out is either "in", "out",
.\"	or "in/out" to describe whether procedure reads or modifies arg,
.\"	and indent is equivalent to second arg of .IP (shouldn't ever be
.\"	needed;  use .AS below instead)
.\"
.\" .AS ?type? ?name?
.\"	Give maximum sizes of arguments for setting tab stops.  Type and
.\"	name are examples of largest possible arguments that will be passed
.\"	to .AP later.  If args are omitted, default tab stops are used.
.\"
.\" .BS
.\"	Start box enclosure.  From here until next .BE, everything will be
.\"	enclosed in one large box.
.\"
.\" .BE
.\"	End of box enclosure.
.\"
.\" .CS
.\"	Begin code excerpt.
.\"
.\" .CE
.\"	End code excerpt.
.\"
.\" .VS ?version? ?br?
.\"	Begin vertical sidebar, for use in marking newly-changed parts
.\"	of man pages.  The first argument is ignored and used for recording
.\"	the version when the .VS was added, so that the sidebars can be
.\"	found and removed when they reach a certain age.  If another argument
.\"	is present, then a line break is forced before starting the sidebar.
.\"
.\" .VE
.\"	End of vertical sidebar.
.\"
.\" .DS
.\"	Begin an indented unfilled display.
.\"
.\" .DE
.\"	End of indented unfilled display.
.\"
.\" .SO ?manpage?
.\"	Start of list of standard options for a Tk widget. The manpage
.\"	argument defines where to look up the standard options; if
.\"	omitted, defaults to "options". The options follow on successive
.\"	lines, in three columns separated by tabs.
.\"
.\" .SE
.\"	End of list of standard options for a Tk widget.
.\"
.\" .OP cmdName dbName dbClass
.\"	Start of description of a specific option.  cmdName gives the
.\"	option's name as specified in the class command, dbName gives
.\"	the option's name in the option database, and dbClass gives
.\"	the option's class in the option database.
.\"
.\" .UL arg1 arg2
.\"	Print arg1 underlined, then print arg2 normally.
.\"
.\" .QW arg1 ?arg2?
.\"	Print arg1 in quotes, then arg2 normally (for trailing punctuation).
.\"
.\" .PQ arg1 ?arg2?
.\"	Print an open parenthesis, arg1 in quotes, then arg2 normally
.\"	(for trailing punctuation) and then a closing parenthesis.
.\"
.\"	# Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
.if t .wh -1.3i ^B
.nr ^l \n(.l
.ad b
.\"	# Start an argument description
.de AP
.ie !"\\$4"" .TP \\$4
.el \{\
.   ie !"\\$2"" .TP \\n()Cu
.   el          .TP 15
.\}
.ta \\n()Au \\n()Bu
.ie !"\\$3"" \{\
\&\\$1 \\fI\\$2\\fP (\\$3)
.\".b
.\}
.el \{\
.br
.ie !"\\$2"" \{\
\&\\$1	\\fI\\$2\\fP
.\}
.el \{\
\&\\fI\\$1\\fP
.\}
.\}
..
.\"	# define tabbing values for .AP
.de AS
.nr )A 10n
.if !"\\$1"" .nr )A \\w'\\$1'u+3n
.nr )B \\n()Au+15n
.\"
.if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
.nr )C \\n()Bu+\\w'(in/out)'u+2n
..
.AS Tcl_Interp Tcl_CreateInterp in/out
.\"	# BS - start boxed text
.\"	# ^y = starting y location
.\"	# ^b = 1
.de BS
.br
.mk ^y
.nr ^b 1u
.if n .nf
.if n .ti 0
.if n \l'\\n(.lu\(ul'
.if n .fi
..
.\"	# BE - end boxed text (draw box now)
.de BE
.nf
.ti 0
.mk ^t
.ie n \l'\\n(^lu\(ul'
.el \{\
.\"	Draw four-sided box normally, but don't draw top of
.\"	box if the box started on an earlier page.
.ie !\\n(^b-1 \{\
\h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
.\}
.el \}\
\h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
.\}
.\}
.fi
.br
.nr ^b 0
..
.\"	# VS - start vertical sidebar
.\"	# ^Y = starting y location
.\"	# ^v = 1 (for troff;  for nroff this doesn't matter)
.de VS
.if !"\\$2"" .br
.mk ^Y
.ie n 'mc \s12\(br\s0
.el .nr ^v 1u
..
.\"	# VE - end of vertical sidebar
.de VE
.ie n 'mc
.el \{\
.ev 2
.nf
.ti 0
.mk ^t
\h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
.sp -1
.fi
.ev
.\}
.nr ^v 0
..
.\"	# Special macro to handle page bottom:  finish off current
.\"	# box/sidebar if in box/sidebar mode, then invoked standard
.\"	# page bottom macro.
.de ^B
.ev 2
'ti 0
'nf
.mk ^t
.if \\n(^b \{\
.\"	Draw three-sided box if this is the box's first page,
.\"	draw two sides but no top otherwise.
.ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
.el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
.\}
.if \\n(^v \{\
.nr ^x \\n(^tu+1v-\\n(^Yu
\kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
.\}
.bp
'fi
.ev
.if \\n(^b \{\
.mk ^y
.nr ^b 2
.\}
.if \\n(^v \{\
.mk ^Y
.\}
..
.\"	# DS - begin display
.de DS
.RS
.nf
.sp
..
.\"	# DE - end display
.de DE
.fi
.RE
.sp
..
.\"	# SO - start of list of standard options
.de SO
'ie '\\$1'' .ds So \\fBoptions\\fR
'el .ds So \\fB\\$1\\fR
.SH "STANDARD OPTIONS"
.LP
.nf
.ta 5.5c 11c
.ft B
..
.\"	# SE - end of list of standard options
.de SE
.fi
.ft R
.LP
See the \\*(So manual entry for details on the standard options.
..
.\"	# OP - start of full description for a single option
.de OP
.LP
.nf
.ta 4c
Command-Line Name:	\\fB\\$1\\fR
Database Name:	\\fB\\$2\\fR
Database Class:	\\fB\\$3\\fR
.fi
.IP
..
.\"	# CS - begin code excerpt
.de CS
.RS
.nf
.ta .25i .5i .75i 1i
..
.\"	# CE - end code excerpt
.de CE
.fi
.RE
..
.\"	# UL - underline word
.de UL
\\$1\l'|0\(ul'\\$2
..
.\"	# QW - apply quotation marks to word
.de QW
.ie '\\*(lq'"' ``\\$1''\\$2
.\"" fix emacs highlighting
.el \\*(lq\\$1\\*(rq\\$2
..
.\"	# PQ - apply parens and quotation marks to word
.de PQ
.ie '\\*(lq'"' (``\\$1''\\$2)\\$3
.\"" fix emacs highlighting
.el (\\*(lq\\$1\\*(rq\\$2)\\$3
..
.\"	# QR - quoted range
.de QR
.ie '\\*(lq'"' ``\\$1''\\-``\\$2''\\$3
.\"" fix emacs highlighting
.el \\*(lq\\$1\\*(rq\\-\\*(lq\\$2\\*(rq\\$3
..
.\"	# MT - "empty" string
.de MT
.QW ""
..
.BS
.SH NAME
textutil::expander \- Procedures to process templates and expand text\&.
.SH SYNOPSIS
package require \fBTcl 8\&.5 9\fR
.sp
package require \fBtextutil::expander ?1\&.3\&.2?\fR
.sp
\fB::textutil::expander\fR \fIexpanderName\fR
.sp
\fIexpanderName\fR \fBcappend\fR \fItext\fR
.sp
\fIexpanderName\fR \fBcget\fR \fIvarname\fR
.sp
\fIexpanderName\fR \fBcis\fR \fIcname\fR
.sp
\fIexpanderName\fR \fBcname\fR
.sp
\fIexpanderName\fR \fBcpop\fR \fIcname\fR
.sp
\fIexpanderName\fR \fBctopandclear\fR
.sp
\fIexpanderName\fR \fBcpush\fR \fIcname\fR
.sp
\fIexpanderName\fR \fBcset\fR \fIvarname\fR \fIvalue\fR
.sp
\fIexpanderName\fR \fBcvar\fR \fIvarname\fR
.sp
\fIexpanderName\fR \fBerrmode\fR \fInewErrmode\fR
.sp
\fIexpanderName\fR \fBevalcmd\fR ?\fInewEvalCmd\fR?
.sp
\fIexpanderName\fR \fBexpand\fR \fIstring\fR ?\fIbrackets\fR?
.sp
\fIexpanderName\fR \fBlb\fR ?\fInewbracket\fR?
.sp
\fIexpanderName\fR \fBrb\fR ?\fInewbracket\fR?
.sp
\fIexpanderName\fR \fBreset\fR
.sp
\fIexpanderName\fR \fBsetbrackets\fR \fIlbrack rbrack\fR
.sp
\fIexpanderName\fR \fBtextcmd\fR ?\fInewTextCmd\fR?
.sp
\fIexpanderName\fR \fBwhere\fR
.sp
.BE
.SH DESCRIPTION
.PP
The Tcl \fBsubst\fR command is often used to support a kind of
template processing\&. Given a string with embedded variables or
function calls, \fBsubst\fR will interpolate the variable and function
values, returning the new string:
.PP
.CS


    % set greeting "Howdy"
    Howdy
    % proc place {} {return "World"}
    % subst {$greeting, [place]!}
    Howdy, World!
    %

.CE
.PP
By defining a suitable set of Tcl commands, \fBsubst\fR can be used to
implement a markup language similar to HTML\&.
.PP
The \fBsubst\fR command is efficient, but it has three drawbacks for
this kind of template processing:
.IP \(bu
There's no way to identify and process the plain text between two
embedded Tcl commands; that makes it difficult to handle plain text in
a context-sensitive way\&.
.IP \(bu
Embedded commands are necessarily bracketed by \fB[\fR and
\fB]\fR; it's convenient to be able to choose different brackets
in special cases\&.  Someone producing web pages that include a large
quantity of Tcl code examples might easily prefer to use \fB<<\fR
and \fB>>\fR as the embedded code delimiters instead\&.
.IP \(bu
There's no easy way to handle incremental input, as one might wish to
do when reading data from a socket\&.
.PP
.PP
At present, expander solves the first two problems; eventually it will
solve the third problem as well\&.
.PP
The following section describes the command API to the expander; this
is followed by the tutorial sections, see \fBTUTORIAL\fR\&.
.SH "EXPANDER API"
.PP
The \fBtextutil::expander\fR package provides only one command,
described below\&. The rest of the section is taken by a description of
the methods for the expander objects created by this command\&.
.TP
\fB::textutil::expander\fR \fIexpanderName\fR
The command creates a new expander object with an associated Tcl
command whose name is \fIexpanderName\fR\&. This command may be used to
invoke various operations on the graph\&. If the \fIexpanderName\fR is
not fully qualified it is interpreted as relative to the current
namespace\&.  The command has the following general form:
.CS


\fIexpanderName\fR option ?\fIarg arg \&.\&.\&.\fR?

.CE
.IP
\fIOption\fR and the \fIarg\fRs determine the exact behavior of the
command\&.
.PP
.PP
The following commands are possible for expander objects:
.TP
\fIexpanderName\fR \fBcappend\fR \fItext\fR
Appends a string to the output in the current context\&.  This command
should rarely be used by macros or application code\&.
.TP
\fIexpanderName\fR \fBcget\fR \fIvarname\fR
Retrieves the value of variable \fIvarname\fR, defined in the current
context\&.
.TP
\fIexpanderName\fR \fBcis\fR \fIcname\fR
Determines whether or not the name of the current context is
\fIcname\fR\&.
.TP
\fIexpanderName\fR \fBcname\fR
Returns the name of the current context\&.
.TP
\fIexpanderName\fR \fBcpop\fR \fIcname\fR
Pops a context from the context stack, returning all accumulated
output in that context\&.  The context must be named \fIcname\fR, or an
error results\&.
.TP
\fIexpanderName\fR \fBctopandclear\fR
Returns the output currently captured in the topmost context and
clears that buffer\&. This is similar to a combination of \fBcpop\fR
followed by \fBcpush\fR, except that internal state (brackets) is
preserved here\&.
.TP
\fIexpanderName\fR \fBcpush\fR \fIcname\fR
Pushes a context named \fIcname\fR onto the context stack\&.  The
context must be popped by \fBcpop\fR before expansion ends or an
error results\&.
.TP
\fIexpanderName\fR \fBcset\fR \fIvarname\fR \fIvalue\fR
Sets variable \fIvarname\fR to \fIvalue\fR in the current context\&.
.TP
\fIexpanderName\fR \fBcvar\fR \fIvarname\fR
Retrieves the internal variable name of context variable
\fIvarname\fR; this allows the variable to be passed to commands like
\fBlappend\fR\&.
.TP
\fIexpanderName\fR \fBerrmode\fR \fInewErrmode\fR
Sets the macro expansion error mode to one of \fBnothing\fR,
\fBmacro\fR, \fBerror\fR, or \fBfail\fR; the default value is
\fBfail\fR\&.  The value determines what the expander does if an
error is detected during expansion of a macro\&.
.RS
.TP
\fBfail\fR
The error propagates normally and can be caught or ignored by the
application\&.
.TP
\fBerror\fR
The macro expands into a detailed error message, and expansion
continues\&.
.TP
\fBmacro\fR
The macro expands to itself; that is, it is passed along to the output
unchanged\&.
.TP
\fBnothing\fR
The macro expands to the empty string, and is effectively ignored\&.
.RE
.sp
.TP
\fIexpanderName\fR \fBevalcmd\fR ?\fInewEvalCmd\fR?
Returns the current evaluation command, which defaults to
\fBuplevel #0\fR\&.  If specified, \fInewEvalCmd\fR will be saved for
future use and then returned; it must be a Tcl command expecting one
additional argument: the macro to evaluate\&.
.TP
\fIexpanderName\fR \fBexpand\fR \fIstring\fR ?\fIbrackets\fR?
Expands the input string, replacing embedded macros with their
expanded values, and returns the expanded string\&.
.sp
Note that this method pushes a new (empty) context on the stack of
contexts while it is running, and removes it on return\&.
.sp
If \fIbrackets\fR is given, it must be a list of two strings; the
items will be used as the left and right macro expansion bracket
sequences for this expansion only\&.
.TP
\fIexpanderName\fR \fBlb\fR ?\fInewbracket\fR?
Returns the current value of the left macro expansion bracket; this is
for use as or within a macro, when the bracket needs to be included in
the output text\&.  If \fInewbracket\fR is specified, it becomes the new
bracket, and is returned\&.
.TP
\fIexpanderName\fR \fBrb\fR ?\fInewbracket\fR?
Returns the current value of the right macro expansion bracket; this
is for use as or within a macro, when the bracket needs to be included
in the output text\&.  If \fInewbracket\fR is specified, it becomes the
new bracket, and is returned\&.
.TP
\fIexpanderName\fR \fBreset\fR
Resets all expander settings to their initial values\&.  Unusual results
are likely if this command is called from within a call to
\fBexpand\fR\&.
.TP
\fIexpanderName\fR \fBsetbrackets\fR \fIlbrack rbrack\fR
Sets the left and right macro expansion brackets\&.  This command is for
use as or within a macro, or to permanently change the bracket
definitions\&.  By default, the brackets are \fB[\fR and
\fB]\fR, but any non-empty string can be used; for example,
\fB<\fR and \fB>\fR or \fB(*\fR and \fB*)\fR or even
\fBHello,\fR and \fBWorld!\fR\&.
.TP
\fIexpanderName\fR \fBtextcmd\fR ?\fInewTextCmd\fR?
Returns the current command for processing plain text, which defaults
to the empty string, meaning \fIidentity\fR\&. If specified,
\fInewTextCmd\fR will be saved for future use and then returned; it
must be a Tcl command expecting one additional argument: the text to
process\&. The expander object will this command for all plain text it
encounters, giving the user of the object the ability to process all
plain text in some standard way before writing it to the output\&. The
object expects that the command returns the processed plain text\&.
.sp
\fINote\fR that the combination of "\fBtextcmd\fR \fIplaintext\fR"
is run through the \fIevalcmd\fR for the actual evaluation\&. In other
words, the \fItextcmd\fR is treated as a special macro implicitly
surrounding all plain text in the template\&.
.TP
\fIexpanderName\fR \fBwhere\fR
Returns a three-element list containing the current character
position, line, and column the expander is at in the processing of the
current input string\&.
.PP
.SH TUTORIAL
.SS BASICS
To begin, create an expander object:
.PP
.CS


    % package require textutil::expander
    1\&.2
    % ::textutil::expander myexp
    ::myexp
    %

.CE
.PP
The created \fB::myexp\fR object can be used to expand text strings
containing embedded Tcl commands\&.  By default, embedded commands are
delimited by square brackets\&.  Note that expander doesn't attempt to
interpolate variables, since variables can be referenced by embedded
commands:
.PP
.CS


    % set greeting "Howdy"
    Howdy
    % proc place {} {return "World"}
    % ::myexp expand {[set greeting], [place]!}
    Howdy, World!
    %

.CE
.SS "EMBEDDING MACROS"
An expander macro is simply a Tcl script embedded within a text
string\&.  Expander evaluates the script in the global context, and
replaces it with its result string\&.  For example,
.PP
.CS


    % set greetings {Howdy Hi "What's up"}
    Howdy Hi "What's up"
    % ::myexp expand {There are many ways to say "Hello, World!":
    [set result {}
    foreach greeting $greetings {
	append result "$greeting, World!\\\\n"
    }
    set result]
    And that's just a small sample!}
    There are many ways to say "Hello, World!":
    Howdy, World!
    Hi, World!
    What's up, World!

    And that's just a small sample!
    %

.CE
.SS "WRITING MACRO COMMANDS"
More typically, \fImacro commands\fR are used to create a markup
language\&.  A macro command is just a Tcl command that returns an
output string\&.  For example, expand can be used to implement a generic
document markup language that can be retargeted to HTML or any other
output format:
.PP
.CS


    % proc bold {} {return "<b>"}
    % proc /bold {} {return "</b>"}
    % ::myexp expand {Some of this text is in [bold]boldface[/bold]}
    Some of this text is in <b>boldface</b>
    %

.CE
.PP
The above definitions of \fBbold\fR and \fB/bold\fR returns HTML, but
such commands can be as complicated as needed; they could, for
example, decide what to return based on the desired output format\&.
.SS "CHANGING THE EXPANSION BRACKETS"
By default, embedded macros are enclosed in square brackets,
\fB[\fR and \fB]\fR\&.  If square brackets need to be
included in the output, the input can contain the \fBlb\fR and
\fBrb\fR commands\&.  Alternatively, or if square brackets are
objectionable for some other reason, the macro expansion brackets can
be changed to any pair of non-empty strings\&.
.PP
The \fBsetbrackets\fR command changes the brackets permanently\&.
For example, you can write pseudo-html by change them to \fB<\fR
and \fB>\fR:
.PP
.CS


    % ::myexp setbrackets < >
    % ::myexp expand {<bold>This is boldface</bold>}
    <b>This is boldface</b>

.CE
.PP
Alternatively, you can change the expansion brackets temporarily by
passing the desired brackets to the \fBexpand\fR command:
.PP
.CS


    % ::myexp setbrackets "\\\\[" "\\\\]"
    % ::myexp expand {<bold>This is boldface</bold>} {< >}
    <b>This is boldface</b>
    %

.CE
.SS "CUSTOMIZED MACRO EXPANSION"
By default, macros are evaluated using the Tcl \fBuplevel #0\fR
command, so that the embedded code executes in the global context\&.
The application can provide a different evaluation command using
\fBevalcmd\fR; this allows the application to use a safe
interpreter, for example, or even to evaluated something other than
Tcl code\&.  There is one caveat: to be recognized as valid, a macro
must return 1 when passed to Tcl's "info complete" command\&.
.PP
For example, the following code "evaluates" each macro by returning
the macro text itself\&.
.PP
.CS


    proc identity {macro} {return $macro}
    ::myexp evalcmd identity

.CE
.SS "USING THE CONTEXT STACK"
Often it's desirable to define a pair of macros which operate in some
way on the plain text between them\&.  Consider a set of macros for
adding footnotes to a web page: one could have implement something
like this:
.PP
.CS


    Dr\&. Pangloss, however, thinks that this is the best of all
    possible worlds\&.[footnote "See Candide, by Voltaire"]

.CE
.PP
The \fBfootnote\fR macro would, presumably, assign a number to this
footnote and save the text to be formatted later on\&.  However, this
solution is ugly if the footnote text is long or should contain
additional markup\&.  Consider the following instead:
.PP
.CS


    Dr\&. Pangloss, however, thinks that this is the best of all
    possible worlds\&.[footnote]See [bookTitle "Candide"], by
    [authorsName "Voltaire"], for more information\&.[/footnote]

.CE
.PP
Here the footnote text is contained between \fBfootnote\fR and
\fB/footnote\fR macros, continues onto a second line, and contains
several macros of its own\&.  This is both clearer and more flexible;
however, with the features presented so far there's no easy way to do
it\&.  That's the purpose of the context stack\&.
.PP
All macro expansion takes place in a particular context\&.  Here, the
\fBfootnote\fR macro pushes a new context onto the context stack\&.
Then, all expanded text gets placed in that new context\&.
\fB/footnote\fR retrieves it by popping the context\&.  Here's a
skeleton implementation of these two macros:
.PP
.CS


    proc footnote {} {
        ::myexp cpush footnote
    }

    proc /footnote {} {
        set footnoteText [::myexp cpop footnote]

        # Save the footnote text, and return an appropriate footnote
        # number and link\&.
    }

.CE
.PP
The \fBcpush\fR command pushes a new context onto the stack; the
argument is the context's name\&.  It can be any string, but would
typically be the name of the macro itself\&.  Then, \fBcpop\fR
verifies that the current context has the expected name, pops it off
of the stack, and returns the accumulated text\&.
.PP
Expand provides several other tools related to the context stack\&.
Suppose the first macro in a context pair takes arguments or computes
values which the second macro in the pair needs\&.  After calling
\fBcpush\fR, the first macro can define one or more context
variables; the second macro can retrieve their values any time before
calling \fBcpop\fR\&.  For example, suppose the document must specify
the footnote number explicitly:
.PP
.CS


    proc footnote {footnoteNumber} {
        ::myexp cpush footnote
        ::myexp csave num $footnoteNumber
        # Return an appropriate link
    }

    proc /footnote {} {
        set footnoteNumber [::myexp cget num]
        set footnoteText [::myexp cpop footnote]

        # Save the footnote text and its footnoteNumber for future
        # output\&.
    }

.CE
.PP
At times, it might be desirable to define macros that are valid only
within a particular context pair; such macros should verify that they
are only called within the correct context using either \fBcis\fR
or \fBcname\fR\&.
.SH HISTORY
\fBexpander\fR was written by William H\&. Duquette; it is a repackaging
of the central algorithm of the expand macro processing tool\&.
.SH "BUGS, IDEAS, FEEDBACK"
This document, and the package it describes, will undoubtedly contain
bugs and other problems\&.
Please report such in the category \fItextutil\fR of the
\fITcllib Trackers\fR [http://core\&.tcl\&.tk/tcllib/reportlist]\&.
Please also report any ideas for enhancements you may have for either
package and/or documentation\&.
.PP
When proposing code changes, please provide \fIunified diffs\fR,
i\&.e the output of \fBdiff -u\fR\&.
.PP
Note further that \fIattachments\fR are strongly preferred over
inlined patches\&. Attachments can be made by going to the \fBEdit\fR
form of the ticket immediately after its creation, and then using the
left-most button in the secondary navigation bar\&.
.SH "SEE ALSO"
[uri, http://www\&.wjduquette\&.com/expand, regexp, split, string
.SH KEYWORDS
string, template processing, text expansion
.SH CATEGORY
Documentation tools
.SH COPYRIGHT
.nf
Copyright (c) William H\&. Duquette, http://www\&.wjduquette\&.com/expand

.fi