rcVar 1.0
by RabidCow (rabidcow@juno.com)

This module exists to demonstrate the new variable format I will
be putting in my modules in the future, to give people a better
idea of how they work, and hopefully to get feedback from any
scripter/themer that it inconveniences.

There are a few useful things you can do with it:
- late evaluation of $vars$, to get values such as SystrayCurrentWidth
- character replacement, like rot13 (or removal, "item12"->"12")

Note that the <> in the commands below are part of the variable
names, you shouldn't include them when you call the commands.


##### Bang Commands:                                            #####

!rcVarExec <command>
    Executes <command> after variable replacement.

!rcVarHash <token> <command>
    Calculates the Fowler/Noll/Vo or One-At-A-Time hash of <token> and
    executes <command> after variable replacement.  Variables are not
    replaced in <token>.

    This exists so that I can calculate the appropriate hash values
    for matching variable names, and as a better example of context
    reliant vars.


##### Module-Specific Variables:                                #####

{@module}
    This is replaced with the name of the module:
    "{@module}" -> "rcVar"

{@version}
    This is replaced with the module's version number:
    "{@version}" -> "1.0"

{@author}
    This is replaced with the module's author:
    "{@author}" -> "RabidCow"

Variables only available with !rcVarHash:
{}
{(<algorithm>)}
{@value}
{@value(<algorithm>)}
    replaced with the 32-bit hash in hex, <algorithm> can be:
    fnv - Fowler/Noll/Vo (fnv-1a)
    oaat - One-At-A-Time

    If <algorithm> is not supplied, it defaults to fnv.

    !rcVarHash "doughnuts" !alert "{@value}"
      -> !alert "56C7B32C"
    !rcVarHash "doughnuts" !alert "{(fnv)}"
      -> !alert "56C7B32C"
    !rcVarHash "doughnuts" !alert "{@value(oaat)}"
      -> !alert "C3323E0B"

{@string}
    is replaced with <token>:
    !rcVarHash "doughnuts" !alert "{@string}"
      -> !alert "doughnuts"
    !rcVarHash "doughnuts" !alert "{@string}: {}"
      -> !alert "doughnuts: 56C7B32C"

{@match(<value>)}
{@match(<value> <algorithm>)}
    is replaced with 1 if the hash matches <value>, or 0 if it
    doesn't match.  <algorithm> is the same as for {@value}

    !rcVarHash "doughnuts" !alert "{@match(22222222)}"
      -> !alert "0"
    !rcVarHash "doughnuts" !alert "{@match(56C7B32C)}"
      -> !alert "1"

    ; in step.rc
    yesno0 no
    yesno1 yes
    ; bang command
    !rcVarHash "doughnuts" !alert "\dyesno{@match(2)}\d"
      -> !alert "no"
    !rcVarHash "doughnuts" !alert "\dyesno{@match(56C7B32C)}\d"
      -> !alert "yes"


##### Variable Syntax Errors:                                   #####

If you make a mistake or give the module a string that confuses it,
it will insert a very short error message into the command to give
you some idea of what happened:

#@  This means there was an error in a {@...} variable.  It could be
    that you mistyped the variable name, didn't supply enough
    parameters, or that the resulting expansion would make the
    command string longer than 4096 characters.

#E  This will usually mean that it couldn't find a matching end
    brace.  Starting from where #E is inserted in your command,
    count all of the { and } and make sure they match.
    Attempting to use math expressions in {...} will sometimes
    result in a #E, you should use \d...\d instead for that.
    E means Error

#Q  This is the same as #E, but for the {{ ... }} quote.  In this
    case, it will paste the end of the string on, starting from
    where it was expecting to get }}.  Note that you can't have
    spaces between the two ending braces.
    Q for Quote

#L  This is the same as #E, but for the {[ ... ]} quote.
    It's L because this is a Literal quote.

#P  This means something went wrong during the lookup of a {<var>}
    variable.  I don't think it will ever happen, because LiteStep
    will pop up an error message instead.
    "Plain" variables.

#R  Most punctuation at the beginning of variable names is reserved
    for special meanings.  You can't, for example, use a variable
    named $^stuff$ as {^stuff}, because {^...} may someday mean
    something.
    That's R for Reserved.

#?  This should never happen, it means something is wrong in the
    variable expansion code.
    ? as in "How did this happen??????"


##### More Variable Stuff:                                      #####

{<var>}
\d<var>\d
    These will both perform "late evaluation" on the LiteStep
    variable $<var>$.  This means that if the value is set sometime
    after LiteStep starts, it will evaluate to the new value rather
    than the original.

    There are some subtil differences between the two:
    - \d<var>\d will allow math expressions, {<var>} will not
    - \d<var>\d will allow you to expand other {@...} and {...}
      vars inside with the same command, while {<var>} will not.
    - {<var>} is evaluated during variable expansion, \d<var>\d is
      evaluated afterwards.  This means that \d<var>\d will be
      evaluated by the current command, but \{<var>\} will wait
      until the next one.  (note the backslashes)

    ; note that $...$ will do late evaluation from lsxCommand,
    ; so this is only different if it's loaded from step.rc
    !rcVarExec !alert "{SystrayCurrentWidth}, $SystrayCurrentWidth$"
      -> !alert "177, 24"
    ; see, from lsxCommand, it's:
      -> !alert "177, 177"

    !rcVarExec !alert "{200-SystrayCurrentWidth}, \d200-SystrayCurrentWidth\d"
      -> !alert "#E, 23"
    ; no math expressions in {...}

    ; in step.rc:
    yesno0 no
    yesno1 yes
    ; bang commands:
    !rcVarHash "french fries" !alert "{yesno{@match(12)}}"
      -> pops up an error: "Uninitialized variable: yesno{"
    !rcVarHash "french fries" !alert "\dyesno{@match(12)}\d"
      -> !alert "no"



##### Escapes:                                                  #####

Sometimes you need to be able to pass characters in to a module
without their special meaning.  The traditional approach is escapes,
and that's what you get here.

Simple escapes:
\\  backslash \
\{  left curly brace {
\}  right curly brace }
\[  left square bracket [
\]  right square bracket ]
\a  apostrophe  '
\c  caret ^
\d  dollar sign $
\n  newline
\q  double quote "
\r  carriage return
\s  semicolon ;
\t  tab

\xNN
\uNNNN
    any character, using either a 2-digit hex code for the ascii
    value, or a 4-digit hex code for unicode.  LiteStep doesn't
    actually use Unicode in bang commands, so this Unicode character
    is converted to the corresponding MCBS version.  I don't really
    understand that whole translation, suffice it to say that it
    should work.

    If you try to insert \x00, it will truncate the string.  (but
    the remaining variables and escapes are still evaluated.)
    I might change this to just remove the \x00 without inserting
    a character to replace it.


Block escapes:
{[...]} - the literal block quote
    This will cause the special meaning of all characters in the
    enclosed block to be ignored.  In some cases, dollar signs,
    quotes, apostrophes, and brackets may not be ignored because
    they're interpreted by LiteStep before they are sent to the
    module.

    You *can* nest literal block escapes, the only characters it
    recognizes are the sequences {[ and ]}, which it will match in
    pairs.  Unmatched single braces are ignored and left as-is.

{{...}} - the semi-intelligent block quote
    The meaning of this block quote is not final, since I'm not
    sure it's very useful in its current form.

    Currently, it will evaluate all top-level simple escapes and
    ignore all variables and escapes in {...} blocks, as well as
    nested block quotes.  I suspect it may be more useful to do
    almost the opposite: ignore escapes and only evaluate variables.

    You can nest these block escapes too, all braces, single or in
    groups, must match.  If they don't, you'll probably get a #Q.

VarName "..."
{VarName}
\dVarName\d
    Values inside results of variable expansion are not further
    evaluated, so splitting the contents into a variable and using
    late evaluation will effectively escape the contents.

    I recommend this method for paths and for long sequences that
    are used in several places.  If you find yourself using
    {[$something$]}, shorten it to {something} instead.  The result
    is almost the same, and you don't have to worry about embedded
    {[ or ]}, as unlikely as that may be.


Multi-level escapes:

The main way to get information from one module to another is to
call one bang command from within another, like all the examples
I've used that make use of !alert.  If the two commands use
different characters to mark variables, or have none at all, there
is no problem, but when they use the same characters, you need to
escape them.  Complicated things can end up with bang commands
nested several levels deep, which can make escaping ugly and hard
to understand:

    ; one long command
    DOWDispOnCreate !DateExec !SkinBoxExec DOWCache !SkinBoxBitmap
        DOWDisp :\\\{@ik\\\} 0 \\d\{@da\}*16\\d 16 16
    ; (the modules involved are currently not available for comment.)

Are there the right number of backslashes on everything?  Will the
variables be interpreted by the correct bang command, and in the
right context?

To solve this issue for all time, you can insert a number between
the backslash and the escape code, or between the two openning
characters of a block escape, to indicate how many escape levels it
should pass through before being evaluated.  (this number must not
start with a 0)

    ; one long command
    DOWDispOnCreate !DateExec !SkinBoxExec DOWCache !SkinBoxBitmap
        DOWDisp :\1{@ik\1} 0 \1d\{@da\}*16\1d 16 16

In "\{@da\}", the escaped braces will be evaluated by OnCreate, which
will turn it into "{@da}".  !DateExec will evaluate this varaible.

In "\1{@ik\1}", the escaped braces will pass through OnCreate and be
evaluated by !DateExec, turning them into braces as "{@ik}", which
will then be interpreted as a variable by !SkinBoxExec.

Similarly, the two "\1d"'s will be skipped by OnCreate and turned
into dollar signs by !DateExec, at which point they will be
evaluated, multiplying the result of {@da} by 16 before !SkinBoxExec
gets it.  (remember that \d<var>\d is evaluated after regular var
replacement.)

There is no limit to how large the number of skipped evaluations
can be, you could use \100000000000000000000000000000000000000{
if you wanted to.  (for the curious, this is decremented as a string,
not converted to an integer, decremented, and converted back.)

This can be used with block escapes:
    {4[ ... ]}
    {22{ ... }}



##### Transforms:                                               #####

When people started using label to display windows' titles as
tooltips from RabidVWM, it became apparent that I would have to
have a way to automatically escape special characters in the
inserted text.  With transforms, you have a more generic, and thus
more powerful, way to do the same thing.

A transform can be applied to any {...} or {@...} variable by
adding a backslash and a transform name just before the ending
brace.


Built-in transforms:

{...\|}
    Adds a backslash to escape everything that label would otherwise
    interpret specially, square brackets and backslashes.  All
    other characters are left unchanged.
    [ ] \

{...\\}
    Escapes everything that this variable system can unescape.
    That's brackets, braces, backslashes, quotes, apostrophes,
    dollar signs, semicolons, carets, newlines, and tabs.
    [ ] { } \ " ' $ ; ^


User defined transforms:

User defined transforms are named, you define them in step.rc with
a line starting with Transform followed by the name (no spaces).
You use them by placing the name after a blackslash at the end of
the variable: {...\name}.

Currently, there are two types of user transforms available:

Transform<name> esc <chars>
Transform<name> esc <chars> <replacements>
    Any characters matching something in the first list are replaced
    with the corresponding character in <replacements> (if there is
    one) and preceded by a backslash:

    ; in step.rc:
    TransformA esc abcde
    TransformB esc abcde def
    Var abcdefg

    ; bang commands:
    !rcVarExec !alert "{Var}"
        -> !alert "abcdefg"
    !rcVarExec !alert "{Var\A}"
        -> !alert "\a\b\c\d\efg"
    !rcVarExec !alert "{Var\B}"
        -> !alert "\d\e\f\d\efg"

Transform<name> tr <from>
Transform<name> tr <from> <replacements>
    Any characters matching something in the first list are replaced
    with the corresponding character in <replacements>.  If there is
    no corresponding character, they're just removed.  If you don't
    give any replacements, all characters in <from> are removed from
    the result.

    ; in step.rc:
    TransformC tr abcde 12345
    TransformD tr abcde 123
    Var abcdefg

    ; bang commands:
    !rcVarExec !alert "{Var}"
        -> !alert "abcdefg"
    !rcVarExec !alert "{Var\C}"
        -> !alert "12345fg"
    !rcVarExec !alert "{Var\D}"
        -> !alert "123fg"


Problems with transforms:

There are two:  first, escapes are not evaluated in the lists of
characters, which makes it difficult to make user-defined transforms
that process quotes, apostrophes, newlines, tabs, and sometimes
dollar signs.

Secondly, you can combine a user transform with one of the built-in
ones, but they're applied in the wrong order.  The user defined one
*should* be applied first, but it isn't.

These shouldn't be too difficult to fix and I hope to change them
soon.



#####                                                           #####
