In this tutorial we are going to quickly cover using text selection in our Lua scripts. Our first example will be an italicise-selection command for HTML. Basicly if we select a region of text, we want to be able to hit Ctrl-i and insert <i> at the beginning of the selection and </i> at the end of the selection. This is really easy to do in Scite so let's get to it.
Well there you are, 4 lines of code. editor:GetSelText() returns a string containing the text currently selected. We then pass this line into editor:ReplaceSel([string]), a function that replaces the selected text with a string. In this case the string we pass is the original selection concatenated with the <i> and </i> tags. In Lua the .. operator concatenates strings.
We can of course replace the <i> with <b>, or any arbitrary string we choose to. So all you need to do now is bind a suitable keyboard shortcut to this function (see binding functions for more details.). I personally use Ctrl+Shift+i for italics and Ctrl+Shift+b for bold.
Using editor:ReplaceSel([string]) we can insert text at the start and end of the selection and replace all or part of the selected text. We still however have more tools at our disposal. editor.SelectionStart and editor.SelectionEnd give us the absolute position of the start and the end of the selection, while editor.Anchor gives the position that the selection was started (which is always eaual to either the start or end, depending on which way the selection occured.)
The above trivial example prints the positions to the output frame. A non-trivial use might envolve sending these positions to another function, or using them to calculate an ofset with witch to insert the selected text.
Lets look at another example, we will now build a ROT13 encoder. ROT13 is a Caesar ciphers that works by shifting letters by 13 positions meaning "ROT13 converter" becomes "EBG13 pbairegre" and vice versa.
As previously we use editor:GetSelText() to retrieve the selected text. Looping through the resulting string for every char if it is between a-m we add 13 to its machine value. To do so we use string.byte(tempChar) to get the current chars machine code. We then use string.char(x+13) to get the char that has the machine code plus 13. For n-z we subtract 13. If you're using most modern operating systems the machine code in question will be ASCII in which case this arithmetic works nicely. Then we simply concatenate the new char onto a temporary string and use editor:ReplaceSel(tempString) to write it back into the document.
The final example we will look at is a function that converts special characters into their XML/XHTML name code. I wrote this primarily on account of being sick of going through the example code in this document by hand and changing < to < > to > etc. A list of special characters can be found at webmonkey. Note the use of [[ ]] to quote the ". Lua supports three types of quotes, [] "" and ''. Note [] can contain " and ' as well as further nested [] brackets.