spaces
Differences
This shows you the differences between two versions of the page.
spaces [2011/10/17 21:51] – external edit 127.0.0.1 | spaces [2013/11/24 11:28] (current) – saturn | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== The spaces problem, part 1: the art of preserving spaces ====== | ||
+ | //Author// --- // | ||
+ | It's a well known fact that mIRC's scripting language handles spaces in a somewhat ' | ||
+ | |||
+ | What is less well known, is that it's still possible to preserve spaces to a certain extent. This article describes the boundaries of space preservation within mIRC's scripting language. | ||
+ | A follow-up article discusses [[spacealt|workarounds and their disadvantages]]. | ||
+ | |||
+ | ===== Where spaces are stripped ===== | ||
+ | |||
+ | In general, there are only a few places in mIRC's script parsing system where spaces are actually lost: | ||
+ | |||
+ | - During tokenization of parameters for commands | ||
+ | - When tokenizing input passed to functions | ||
+ | - While parsing original, literal script lines | ||
+ | \\ | ||
+ | The **first point** is the major problem. Aside from the space-preserving commands listed in the following section, mIRC evaluates and then space-tokenizes the text following every script command, stripping the spaces in this process. This makes it impossible to call any command (again, bar the exceptions listed below) or alias with spaces preserved. | ||
+ | |||
+ | The **second point** only affects certain events and identifiers; | ||
+ | |||
+ | The **third point** roughly means that it is impossible to put leading, trailing or multiple consecutive spaces // | ||
+ | |||
+ | < | ||
+ | |||
+ | will always result in " | ||
+ | |||
+ | ===== Space-preserving commands ===== | ||
+ | |||
+ | The following is an exhaustive list of **built-in commands** that do preserve spaces: | ||
+ | |||
+ | * /set | ||
+ | * /var | ||
+ | * /returnex | ||
+ | |||
+ | The fact that /set and /var preserve spaces, imply that it's possible to store and use text in variables without losing spaces. The reason for this behaviour is most likely the fact that /set is evaluated in a different way from other commands, as the variable name in its line must not be evaluated to the contents. As all /var commands are internally rewritten as /set commands, it is no surprise that /var behaves exactly as /set when it comes to spaces. Similarly to /set and /var, the ' | ||
+ | |||
+ | There is just one problem with all these assignment commands: if the given text contains exactly one single trailing space, this trailing space will be stripped off: | ||
+ | |||
+ | < | ||
+ | var %x = foo | echo -ag $len(%x) equals 3 | ||
+ | var %x = foo $+ $chr(32) | echo -ag $len(%x) equals 3 | ||
+ | var %x = foo $+ $chr(32) $+ $chr(32) | echo -ag $len(%x) equals 5 | ||
+ | var %x = foo $+ $chr(32) $+ $chr(32) $+ $chr(32) | echo -ag $len(%x) equals 6 | ||
+ | </ | ||
+ | |||
+ | One method to get around this, as suggested by qwerty, is by adding one or more characters after the actual variable contents when using /set or /var, and then using $left(%var, | ||
+ | |||
+ | Finally, the third command - /returnex - is relatively new; it was introduced in mIRC 6.2 as undocumented tool to preserve spaces during internal evaluation of the subtext part of $regsubex calls. It is useful beyond just this context as well, as it is simply a space-preserving version of /return, and can therefore be used to construct custom identifiers that preserve spaces. | ||
+ | |||
+ | Besides these three commands, it is possible to construct **custom aliases** for which the input has all spaces preserved, but these aliases must then be called as identifiers instead of as commands; typically one can use the /noop command to prefix them. For example, instead of: | ||
+ | |||
+ | < | ||
+ | |||
+ | One can use: | ||
+ | |||
+ | < | ||
+ | |||
+ | Obviously this affects the way the parameter are tokenized; all input will be in $1 instead of being space-tokenized over $1, $2 etcetera. The individual parameters will have all spaces preserved though, i.e. in this case, $1 in the myalias alias will contain exactly the same as $rawmsg does. More about such custom identifiers in the next section. | ||
+ | |||
+ | |||
+ | ===== Space-preserving identifiers ===== | ||
+ | |||
+ | As far as **built-in identifiers** are concerned, in general spaces are fully preserved. As exceptions, out of the built-in string manipulation identifiers, | ||
+ | |||
+ | ^ Identifier ^ Problem | ||
+ | | $noqt | Removes leading spaces after the first quote character | | ||
+ | | $strip | ||
+ | | $read | Removes leading spaces, even with the ' | ||
+ | |||
+ | It may be useful to know that the token functions //do// preserve all spaces as long as a token separator other than a space is used. | ||
+ | |||
+ | Constructing **custom identifiers** that preserve spaces is, although a little tricky, entirely possible: as indicated in the previous section, parameters passed to custom identifers are not space-stripped, | ||
+ | |||
+ | As a simple example, the following custom identifier $align(text, | ||
+ | |||
+ | < | ||
+ | |||
+ | This identifier could help align text for displaying it in a custom format. | ||
+ | |||
+ | ===== Space-preserving DLLs ===== | ||
+ | |||
+ | However, to actually display text within mIRC, one needs the /echo or /aline commands, which obviously do not preserve spaces. This is slightly problematic, | ||
+ | |||
+ | On the upside, one can call DLLs using the $dll identifier, and as this built-in identifier preserves the spaces in its parameters when called, it allows for DLLs to provide some of the functionality that mIRC does not offer: for instance, there is [[http:// | ||
+ | |||
+ | For dialogs, it is possible to use DLLs like [[http:// | ||
+ | |||
+ | ===== Conclusion ===== | ||
+ | |||
+ | To recap: the main problem of mIRC's spaces stripping lies with the execution of commands, while the evaluation of identifiers (including by far most built-in identifiers) and manipulation of variables can be done in a space preserving way. Space-preserving custom identifiers can be made constructed as well, although real work will have to be done through DLL calls. | ||
+ | |||
+ | Even though mIRC's way of dealing with spaces is restrictive enough that many things cannot be accomplished without outside help from DLLs, it is still fully possible to manipulate text in any way desired before passing it to such a DLL. |