Drobe :: The archives
About Drobe | Contact | RSS | Twitter | Webspace | Tech docs | Downloads | BBC Micro

Using StrongED with Lua

By Gavin Wraith. Published: 29th Apr 2006, 23:13:56 | Permalink | Printable

Work that text editor hard



ex1.gifLine 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:

screen4.gifThe 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 end

Nice, informative article.

Incidentally, Cretin also supports lua scripts (implemented by the brothers Sidwell back before I got hold of it), so this article may also be useful to those who wish to tinker there, too.

 is a RISC OS Userjymbob on 30/4/06 9:40AM
[ Reply | Permalink | Report ]

Indeed an intersting article!

What kind of irritates me is the list of "Related articles:" since the only real relation between the ones listed and this one is that they appeared on Drobe.

 is a RISC OS Userhzn on 30/4/06 10:07AM
[ Reply | Permalink | Report ]

Maybe the programmers do link "Related articles" to this one, considering that they use a Mouse and maybe Central Heating during the winter months of the long hours of their work? ;-)

(Just kidding here).

Steve.

 is a RISC OS UserSawadee on 30/4/06 11:25AM
[ Reply | Permalink | Report ]

Please 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

    Username

    Password

    Create a new account
    Forgot your password?

    Useful links

    News and media:
    IconbarMyRISCOSArcSiteRISCOScodeANSC.S.A.AnnounceArchiveQercusRiscWorldDrag'n'DropGAG-News

    Top developers:
    RISCOS LtdRISC OS OpenMW SoftwareR-CompAdvantage SixVirtualAcorn

    Dealers:
    CJE MicrosAPDLCastlea4X-AmpleLiquid SiliconWebmonster

    Usergroups:
    WROCCRONENKACCIRUGSASAUGROUGOLRONWUGMUGWAUGGAGRISCOS.be

    Useful:
    RISCOS.org.ukRISCOS.orgRISCOS.infoFilebaseChris Why's Acorn/RISC OS collectionNetSurf

    Non-RISC OS:
    The RegisterThe InquirerApple InsiderBBC NewsSky NewsGoogle Newsxkcddiodesign


    © 1999-2009 The Drobe Team. Some rights reserved, click here for more information
    Powered by MiniDrobeCMS, based on J4U | Statistics
    "We incurred the wrath of Chris Williams, the main journalist at Drobe, who told us to 'Take our filthy spam and shove it where the sun doesn't shine'"
    Page generated in 0.0338 seconds.