Blog

Scite/Lua Word Count Tutorial

In this tutorial we will be creating a Lua script that will count the number of characters, words and lines in the current document.

First up let's look at some of the functions and strategies we will be using.

editor.Length This property stores the number of chars in the current document. You should be wary as on Windows a newline is stored as \r\n (carriage return, new line) making up two chars, whereas Linux uses only \n and the Mac uses only \r.

editor.LineCount This property stores the number of lines in the document.

editor.CurrentPos This property stores the position of the caret (curser). We'll be using it during the tutorial for completeness but not in the final version.

editor:LineFromPosition([char position]) This is a function that takes one argument (a char position), and returns the line number of that position. If we give it editor.CurrentPos as an argument then it will return the current line number. Once more we will be using it for completeness, but not in the final version.

Right, first some declarations.

Now we want to count the number of whitespace control characters. We aren't directly interested in this value, but we will be using it to find the number of standard characters. for m in editor:match("\n") do iterates through the entire document. Each time the text is found, the code inside the do .. end block is executed. In this case if we find a new line then we increment whiteSpace by 1.

Now we are going to calculate the number of non-empty lines, that is lines that contain at least one alphanumeric character. We iterate through the document from the first line till we reach the last line, i.e. editor.LineCount using a while loop. while itt < editor.LineCount do We then get the current line with editor:GetLine(itt); and store it in the line variable. The string.find(line,'%w'); function takes the line as input and searches for alpha numeric chars. The '%w' is a search pattern that tells the function to look for any alphanumeric char. For more on patterns consult the Lua Reference Manual. If the line contains alpha numeric characters then we increment nonEmptyLine by one.

Now we want to calculate the number of words in our document. We'll be using another while loop to iterate through the entire document. It should be noted that this loop is not necessary, the logic can be placed in the previous loop, but for the purposes of this tutorial, a second loop adds to the clarity. The Lua source file contains the merged loops. We use for word in string.gfind(line, "%w+") do, where gfind returns each word in the line. The definition of word in this case is one or more alphanumeric chars (%w+) separated by a non alphanumeric character, like a space. Each time gfind finds a word we increment wordCount by 1.

Note that in both the above loops we place the gfind and find in an if line then block. For these functions Scite will complain if you pass an empty string as an argument. The if block ensures that the function never receives an empty string as an argument. The Lua error message you will get is: "bad argument #1 to `gfind' (string expected, got nil)"

Finally all we need to do is print out the values we have calculated. In the Lua script file I only print out the number of chars, words, lines and non-empty lines but this expanded view should give a more informative idea of how Scite and Lua work.

If you have any questions in regard to this tutorial email me at robert@rrreese.com. A nicer version of the above code is available in this Lua file.