How do I replace something in the text I type

From Scriptwiki
Jump to: navigation, search

This tutorial will teach you how you can automatically replace certain words in the text you type with something else. For example, you could always manually write "laughing out loud" instead of "lol", but scripting's purpose is to make your life easier so why not use it... To automate it, you need basically only two things: the input event and $replace.

Let's start with $replace. mIRC's help says the following:

Replaces any occurrence of substring in string with newstring.

For example, if you use $replace(here is the requested text, re, ER) , it will return: heER is the ERquested text . You can also do multiple replacements with one identifier: $replace(here is the requested text, re, ER, rq, QR) returns: heER is the EQRuested text. Notice that:

  • 1) first it does the first replacement, and only after that it starts looking for the second replacement.
  • 2) $replace is in-case-sensitive, so it will replace "Rq" even if it was asked to replace "rq". If you want it to be case-sensitive, you can use $replacecs.

You can test the above examples with

//echo -a $replace(here is the requested text, re, ER) and
//echo -a $replace(here is the requested text, re, ER, rq, QR)

In order to get these replacements to be done automatically when you type something, we need the input event. It is an event that is triggered when you enter text in an editbox and press enter. Text in the editbox can be accessed with $1-.

If you now create an input event and use /msg to send the text to the server, you will see two lines: one that is produced by your script and one that mirc produces automatically. You will want to stop mirc's default output, and for that you can use /haltdef.

So a simple script to replace every occurence of "lol" with "laughing out loud" looks like this:

on 1:input:*:{ msg $target $replace($1-, lol, laughing out loud) | haltdef }

(Explanation: $target returns the target of the event. You need it to be able to send the message to a channel or to a query.)

This event is only an example, it's not a perfect version. I'll list below some faults and how to fix them.

  • 1) currently also occurences of "lol" in words like "lollipop" are also replaced. So every occurence of "lollipop" results in "laughing out loudlipop". This can be fixed by "telling" the script to replace only those occurences that begin and end with a space. This can be accomplished by adding $chr(32) to the beginning and to the end of "lol". ($chr(N) is an identifier that returns the character with ASCII number N - space's ASCII number is 32. So $chr(32) returns a space.)

You will also need to add spaces to the beginning and to the end of "laughing out loud", so that the correct amount of spaces is maintained. So the whole command looks like this:

msg $target $replace($1-, $chr(32) $+ lol $+ $chr(32), $chr(32) laughing out loud $chr(32))

Note that with "lol", you need to "tell" the script that the spaces need to be added directly to the word (by using $+ to combine them), unlike in "laughing out loud".

Now we have another problem at hand: what if the text starts or ends with "lol" ? There won't be a space there before & after it so our $replace can't handle the situation. There are a number of possibilities how to fix this, but all of them go beyong the scope of this tutorial. So for now, it is left 'unfixed'.

  • 2) currently the script prevents all commandline commands (such as /join #channel ) from being executed. The reason is simple: even if it doesn't find "lol" in the text, it still just msg's the text to the channel/query.

To fix this, you need to tell the script not to trigger if you are trying to execute a command. Additionally, you don't want it to be executed if you press ctrl down with the enter key.

Adding the following in front of all commands in the event fixes this problem:

if ($ctrlenter) || ($left($1,1) === $readini(mirc.ini,text,commandchar)) { return }

Explaining this fully would cause this tutorial to go too far from the original subject so I'll just explain it shortly (never mind if you don't understand all this, it's just a 'thing' that needs to be done).

  • ($ctrlenter) checks if ctrl was pressed down while pressing the enter key. if it was, return.
  • the rest of it checks if the first character in the text is the commandcharacter defined in mirc.ini. Usually it is "/" , but some users may have changed it so it needs to be looked up from the ini.
  • if ctrl was pressed down or the first character was the commandcharacter, then halt the script.

Combining all the knowledge we now have, we should be able to make a decent input event. So now our event looks the following:

on 1:input:*:{
  if ($ctrlenter) || ($left($1,1) === $readini(mirc.ini,text,commandchar)) { return }
  msg $target $replace($1-, $chr(32) $+ lol $+ $chr(32), $chr(32) laughing out loud $chr(32))

It looks a lot more complicated now than it did before. Unfortunately that can't really be avoided. And finding the word you want to replace in there is much harder now, so it might be a good idea to use other methods in storing the word and its replacement. For example, variables.

One way would be to have %var1 that contains list of words that should be replaced and %var2 with their replacements. You could then loop those variables and use token identifiers to get each string and its correct replacement. But those are things out of this topic, so they will not be explained any further in this tutorial.

By now you should have some idea how to replace some words in your text with others, automatically. Remember - experimenting is the best way to learn, and if you have problems, you can always join a help channel and ask.

Contributed by Microbe