Well, first, I'd like to mention that an editor in Computer Craft doesn't make a lot of sense, the screens just aren't big enough.
Plus, they can be difficult to code in the first place.
But if you succeed, you can take away a lot of good Lua knowledge. So consider this project wisely.
So anyway, here are the functions that color text in the default edit program:
local function tryWrite( sLine, regex, colour )
local match = string.match( sLine, regex )
if match then
if type(colour) == "number" then
term.setTextColour( colour )
else
term.setTextColour( colour(match) )
end
term.write( match )
term.setTextColour( textColour )
return string.sub( sLine, string.len(match) + 1 )
end
return nil
end
local function writeHighlighted( sLine )
while string.len(sLine) > 0 do
sLine =
tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or
tryWrite( sLine, "^%-%-.*", commentColour ) or
tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or
tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or
tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or
tryWrite( sLine, "^[%w_]+", function( match )
if tKeywords[ match ] then
return keywordColour
end
return textColour
end ) or
tryWrite( sLine, "^[^%w_]", textColour )
end
end
It isn't simple by any stretch of the imagination.
The thing that makes it work is patterns ( These things: "^%-%-%[%[.-%]%]" )
Read about them here:
http://www.lua.org/pil/20.2.htmlThey are very powerful, but can be confusing at first.
Basically, the 'trywrite' function looks through some text for a specific pattern.
For example, the first call in 'writeHighlighted' is this:
tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour )
This specific pattern looks for comments ( You know, the two dashes ), and prints them to the screen in the 'commentColour'
'writeHighlighted' continues to try to find things that should be highlighting, like "strings", or a keyword
(keywords are defined above, I haven't included that table)
Each time 'tryWrite' finds something to color, it removes it from the line buffer (sLine)
I hope you took the time to read this, and that it helped :)/>
Note that I am not an expert with patterns, most of these are gibberish to me too