|
Using StrongED with Lua By Gavin Wraith. Published: 29th Apr 2006, 23:13:56 | Permalink | PrintableWork that text editor hard
Line Numbering
Here is a no frills example that numbers the lines of a text file, starting from one. The window at the top contains a very short Lua script. The font style and colours are artefacts of the Lua mode in my configuration of StrongED; you may find the syntax colouring useful. The script is saved as the file number1. This file's icon is then CTRL-dragged from the Filer to the apply smarticon of a StrongED window. This produces a new StrongED window with line numbering added, although its titlebar is not accurately presented in the illustration shown here.
We have already mentioned how #! lua should be the first line. This enables StrongED to recognise Lua scripts. We use a variable n for the line count, and give it the initial value '1'. Then comes a for loop, that iterates over the lines of text in the StrongED window - that is to say it runs through the text one line at a time in a sequential order. We have used a variable line as the loop variable. It contains the current line read from the text as a string of characters. It is said to be local to the for loop in that it belongs to the code inside the loop. The words for, in, do, end are all keywords built into Lua.
The function io.lines is a component of Lua's io library. It takes a file name as an argument and it produces an iterator over the lines of the file. This means every time you call the function, it will provide the next line of text in the given file, which is useful for loops. Iteration is much more general in Lua than in older languages, where only arithmetic progressions can be expressed by for loops. In Lua you can design your own iterators to work with the for ... in ... do ... end syntax. The print function is built into Lua to provide simple unformatted output. Incidentally, the initial # symbol indicates that the rest of the first line is a comment. Elsewhere in a script, the -- token indicates that the rest of the line is a comment.
Suppose we only want to number non-blank lines? Here is a slightly more involved script that replaces blank lines by empty lines:
The variable blank holds a pattern string which is used to detect strings consisting only of white space. Patterns are expressions that allow programs to recognise particular sequences in blocks of text; although Lua does not use the standard POSIX regular expression language, it comes close. This pattern matching can be used in search and replace type actions, or just to simply detect the presence of a particular piece of text. The do ... end chunk has the line counter n as a local variable, so that it is hidden from the rest of the code. In Lua variables can be made local to a chunk; function bodies and for loops are examples of chunks. You can make any sequence of statements into a chunk by enclosing it between a pair of do and end keywords. The ability to conceal variables inside chunks is an important feature of Lua.
The function assigned to the variable linecount updates and returns the number of times the function has been called - which will be the number of non-blank lines. It provides the only access to the variable n, which stores the line count. The notation \ is a RiscLua abbreviation for the keyword function - it is supposed to remind you of a lambda. The notation => is a RiscLua abbreviation for the keyword return. The expression x and y or z evaluates to y if x is defined and to z otherwise. This is used in this code to make sure linecount starts off at 1 and is incremented each time the function is called.
The function match is a component of Lua's string library. The expression line:match(blank) is an alternative notation for string.match(line,blank).
Back to the main article
Previous: Dispute over 'intrusive' VRPC copy protection
Next: R-Comp responds to Grapevine feedback
Discussion Viewing threaded comments | View comments unthreaded, listed by date | Skip to the endPlease login before posting a comment. Use the form on the right to do so or create a free account.
|
+++ Message board +++
Read messages or start a new thread
Search the archives
Today's featured article
RiscPC breaks 1.5GHz barrier Calm down dear, this is a case mod
23 comments, latest by flypig on 13/5/04 1:56PM. Published: 12 May 2004
Random article
In the news this week: Friday roundup Aemulor, R-Comp gaining Remote Control, South West show and more
2 comments, latest by diomus on 22/2/03 1:58PM. Published: 21 Feb 2003
Login
Create a new account
Forgot your password?
Useful links
News and media:
Iconbar •
MyRISCOS •
ArcSite •
RISCOScode •
ANS •
C.S.A.Announce •
Archive •
Qercus •
RiscWorld •
Drag'n'Drop •
GAG-News
Top developers:
RISCOS Ltd •
RISC OS Open •
MW Software •
R-Comp •
Advantage Six •
VirtualAcorn
Dealers:
CJE Micros •
APDL •
Castle •
a4 •
X-Ample •
Liquid Silicon •
Webmonster
Usergroups:
WROCC •
RONE •
NKACC •
IRUG •
SASAUG •
ROUGOL •
RONWUG •
MUG •
WAUG •
GAG •
RISCOS.be
Useful:
RISCOS.org.uk •
RISCOS.org •
RISCOS.info •
Filebase •
Chris Why's Acorn/RISC OS collection •
NetSurf
Non-RISC OS:
The Register •
The Inquirer •
Apple Insider •
BBC News •
Sky News •
Google News •
xkcd •
diodesign
|