----------------------------------------------------------------------
--- Knud van Eeden --- 18 August 2008 - 04:53 pm ---------------------
TSE: Macro: Template.s: Expansion: Variable: Operation: Create: How to possible create a variable expansion for TSE macro template.s? [\r1 \r2 \r3 ...]
---
1. I wanted to check if the standard TSE template expansion file
template.s
had a command line option
( Query( MacroCmdLine() )
so that I could pass the name of templates I wanted to expand to be
supplied as a parameter.
But that was not present (but I had already added that possibility
myself earlier in my own template.s version)
2. Then by chance I saw in the original template.s the option '\f' from
Peter Birch. It expands to the current filename.
3. I thought, he, that idea is very usuable, in order to create a
'variable' expansion.
4. So lets e.g. introduce a new expansion symbol \r
(similar to \c, \f, ...).
Here the 'r' stands for 'r'eplace
\r
and this possibly followed by a number, like \r1, \r2, \r3
so that you could have several different expansions in the same template.
5. So you make your template now like this, and add this running template.s
e.g. as your template 'thisisatest'
--- cut here: begin --------------------------------------------------
Hello \r1 world
Now \r1 is finished
--- cut here: end ----------------------------------------------------
6. To have this replaced to something of your choice,
add this to ..\mac\template.s
and recompile (make a backup first)
--- cut here: begin --------------------------------------------------
proc ExpandSpecialChars()
STRING s[255] = ""
// Change any \f into the filename
lreplace("\f", SplitPath(CurrFilename(), _NAME_), "ln")
//
PushPosition()
//
IF LFind( "\r1", "gl" )
IF Ask( "\r1: replace with which string? (test)", s )
lreplace("\r1", s, "gln")
ENDIF
ENDIF
PopPosition()
//
end
--- cut here: end ----------------------------------------------------
7. If you expand your template 'thisisatest' now,
it asks you for a replacement of your choice.
Answer arbitrarily e.g. with 'test',
in that case the expansion becomes:
--- cut here: begin --------------------------------------------------
Hello test world
Now test is finished
--- cut here: end ----------------------------------------------------
8. To further generalize this
By using e.g. a FOR ENDFOR loop you can similarly handle \r2, \r3, \r4, ..., \r...
--- cut here: begin --------------------------------------------------
proc ExpandSpecialChars()
STRING s[255] = ""
STRING r[255] = ""
INTEGER I = 0
INTEGER minI = 1
INTEGER maxI = 50
// Change any \f into the filename
lreplace("\f", SplitPath(CurrFilename(), _NAME_), "ln")
//
PushPosition()
//
FOR I = minI TO maxI
r = Format( "\r", Str( I ) )
IF LFind( r, "gl" )
IF Ask( Format( r, " ", "replace with which string? (test)" ), s )
lreplace( r, s, "gln")
ENDIF
ENDIF
PopPosition()
//
end
--- cut here: end ----------------------------------------------------
9. If you now create for example the following template 'thisisatest'
--- cut here: begin --------------------------------------------------
This is a \r1 \r2 \r3 \r4 test
--- cut here: end ----------------------------------------------------
10. It will, depending on your input, expand to e.g.
--- cut here: begin --------------------------------------------------
This is a somewords someotherwords yetanotherwords thelastwords test
--- cut here: end ----------------------------------------------------
11. Typical applications for this are e.g. when expanding a class in Java, C++, C#, ...
where 1 (or more) substring has to be replaced.
12. -E.g. for a template for a Java class
--- cut here: begin --------------------------------------------------
class CLASS \r1 EXTENDS \r2 {
public static void main( String[] args ) {
}
}
--- cut here: end ----------------------------------------------------
13. -Which is then after your input expanded to
--- cut here: begin --------------------------------------------------
class CLASS MyClass EXTENDS MySuperClass {
public static void main( String[] args ) {
}
}
--- cut here: end ----------------------------------------------------
===
Book: see also:
===
Diagram: see also:
===
File: see also:
===
Help: see also:
===
Image: see also:
===
Internet: see also:
===
Podcast: see also:
===
Screencast: see also:
===
Table: see also:
===
Video: see also:
---
----------------------------------------------------------------------