From ed85e857ff2bff846a6cedabf95a210cc7df7555 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Mon, 12 Jun 2017 11:56:22 +0000 Subject: [PATCH] skex2 updates and documentation --- doc/skex2.md | 57 ++++++++++++++++++++++++++++++++++ modules/applications/skex2.lua | 47 +++++++++++++++------------- 2 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 doc/skex2.md diff --git a/doc/skex2.md b/doc/skex2.md new file mode 100644 index 0000000..cdfc376 --- /dev/null +++ b/doc/skex2.md @@ -0,0 +1,57 @@ +# skex2 + +## interactive line editor + +skex2 is a simple but functional line editor for MultICE. + +### invocation + +To start skex2, simply run skex(filename) from the Lua prompt. + +### usage + +Generally, commands and arguments are entered in the form of `command argument1 argument2 argument3` with the exception of inline execution, which is in the form of `!lua code here`. + +If skex does not understand what you mean, it will output a line with a question mark. + +### commands + +#### q - quit skex2 + +Exits the program. + +#### l \[start\] \[end\] - list contents + +Output the lines from start to end, inclusive. + +#### f \[filename\] - set the filename + +This sets the filename to be used for I/O, or prints the current one. + +#### r \[filename\] - load file + +Reads from *filename* or the current set via **f** + +#### w \[filename\] - write to file + +Writes to *filename* or the current set via **f** + +#### p \[line\] - print or set current line + +As skex2 is a line editor, your pointer is a line, and this either prints the current one or sets a new one. + +#### a \[line\] - append to buffer + +Appends lines after the current line or *line* until a line with only . is entered. + +#### i \[line\] - insert into buffer + +Inserts lines before the current line or *line* until a line with only . is entered. + +#### s \[line\] - replace line + +Removes the current line or *line* and enters insert mode + +#### e - executes the contents of the buffer + +Basically converts it into one big string and loads it. diff --git a/modules/applications/skex2.lua b/modules/applications/skex2.lua index 1f3e2ac..95f5240 100644 --- a/modules/applications/skex2.lua +++ b/modules/applications/skex2.lua @@ -1,16 +1,20 @@ -function skex(s,f) +function skex(s) local c,cs,cT,lT,lP="","",{},{},1 - if s then - if f then - c=s - else - local f=fopen(s,"rb") - local nc=fread(f,2048) - while nc ~= nil and nc ~= "" do - c=c..nc - nc=fread(f,2048) - end + local function lf(s) + local f=fopen(s,"rb") + local nc=fread(f,2048) + while nc ~= nil and nc ~= "" do + c=c..nc + nc=fread(f,2048) end + end + local function wf(s) + local f,c=fopen(s,"wb"),"" + for k,v in ipairs(lT) do fwrite(f,v.."\n") end + fclose(f) + end + if s then + lf(s) for l in c:gmatch("(.-)\n") do lT[#lT+1]=l end end while true do @@ -40,22 +44,23 @@ function skex(s,f) c="" for k,v in ipairs(lT) do c=c..v.."\n" end print(pcall(load(c))) + elseif cT[1] == "r" then + s=cT[2] or s + wf(s) elseif cT[1] == "w" then - f=fopen(s,"wb") - c="" - for k,v in ipairs(lT) do fwrite(f,v.."\n") end - fclose(f) + s=cT[2] or s + wf(s) elseif cT[1] == "d" then for i = 1, TN(cT[3])-TN(cT[2]) do T.remove(lT,cT[2]) end elseif cT[1] == "p" then - nPT=TN(cT[2]) - if nPT then - lP=nPT - else - print(lP) - end + lP=TN(cT[2]) or lP + print(lP) + elseif cs:sub(1,1) == "!" then + c="" + for k,v in ipairs(lT) do c=c..v.."\n" end + print(pcall(load(cs:sub(2)))) else print("?") end