![]() |
![]() |
AliasesAliases: user definable command sequences |
||||||||||
An alias is an user definable command sequence that can be
triggered by using the alias name as it was a single command or function. It can be used to rename the builtin kvirc commands or functions , to automatize complex tasks or as structured programming mean. Aliases can be created or destroyed by using the scriptcenter (graphic interface) or even from the commandline (or script) by using the alias command. A couple of examples will make the things clear: join is a really commonly used command. It might be a good idea to rename it to simply "j" .. just to type it faster. Nothing easier in KVirc: just try this commandline:
A common task in channel management is the kick & ban action. You first ban an user from the channel and then eventually kick him (obviously assuming that he is actually on the channel). This involves using two commands: ban and then kick. It could be a nice idea to have a single "kb" command to perform this action. Well...easy:
Our kb is a really simple example... it doesn't check for the validity of the parameters: the server will warn us if the parameters passed to kb were empty. The alias can be modified at any time by re-using the alias command. Let's make our "kb" a bit more intelligent and add a check for the parameters. TIP: It is a good idea to write the following examples in a text file and then use /parse <filename> to execute it.
The next step will be the "ban <nickname>" call. Another enchancement is the "default reason": we first assign the remaining parameters ($1- means "from $1 to the end") to a temporary variable, if the variable is empty , a default kick reason is assigned. Finally the "kick <nickname> <reason>" will be executed. Aliases can be used as a mean for structured programming. In large scripts you will SURELY have "common tasks" to perform (like having specially colored output or calculating a value from a set of other values)... Aliases are the way of writing the common tasks: they are equivalent to the "procedures" or "functions" in many high-level programming languages. The alias as a procedure (subroutine or sub-task) has been shown in the "kb" example above: it might be commonly called from complexier scripts or other aliases in case that a kick & ban action is needed. The aliases can be used also as functions. Assume that you need really often to calculate the sum of three numbers: a function-alias is the way.
The "setreturn" command sets the return value of a sequence of commands (an alias is a sequence of commands...remember ?). So setreturn $($0 + $1 + $2); will set the return value of the alias to the value computed by $($0 + $1 + $2) that actually is the sum of the first three parameters passed. You will then use it in the following way:
The function-aliases are also normal aliases.... you can use it as a command:
In fact there is no difference al all between function-aliases and normal-aliases: the caller makes the difference: by calling an alias as a command the return value just disappears in hyperspace, by calling an alias as a function , the return value is propagated (and in fact "used"). (There are some "nice" exceptions to this rule...but you don't need to care about it, for now). If setreturn is not called inside an alias body , the return value will be just an empty string. (Note that setreturn can be called multiple times inside an alias: it sets the "return buffer" that is initially empty). You might also write aliases that can work both as commands and functions.. they could perform some tasks and return a secondary result (that might be often ignored). To remove an alias use again the alias command with an empty body:
Please note that you CAN'T remove an alias from inside itself; the following form is forbidden and will throw an execution-time error:
|