Files
HyperionOS/prod/micro/bin/micro

2 lines
6.7 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
local a={...}local function b()local c=syscall.devctl(1,"size")return tonumber(c:match("^(%d+)"))or 80,tonumber(c:match(";(%d+)$"))or 24 end;local function d(e,f)syscall.devctl(1,"spos",e,f)end;local function g(h)syscall.devctl(1,"sfgc",h)end;local function i(h)syscall.devctl(1,"sbgc",h)end;local function j(c)if c and c~=""then syscall.write(1,c)end end;local function k()syscall.devctl(1,"clear")end;local l,m=b()local n=m-2;local o={""}local p=1;local q=1;local r=0;local s=true;local t=nil;local u=""local v=false;local w=nil;local x=""local y=0;local z=false;local function A(B)if B:sub(1,1)=="/"then return B end;local C=syscall.getcwd()C=C:gsub("/+$","")return C.."/"..B end;local function D(E)if not syscall.exists(E)then o={""}u="[new file]"return end;local F=syscall.open(E,"r")local G=""while true do local h=syscall.read(F,4096)if not h or h==""then break end;G=G..h end;syscall.close(F)o={}for H in(G.."\n"):gmatch("([^\n]*)\n")do table.insert(o,H)end;if#o>1 and o[#o]==""and G:sub(-1)=="\n"then table.remove(o)end;if#o==0 then o={""}end end;local function I(E)local J,K=pcall(function()local F=syscall.open(E,"w")for L,H in ipairs(o)do syscall.write(F,H)if L<#o then syscall.write(F,"\n")end end;syscall.write(F,"\n")syscall.close(F)end)if J then u="Saved: "..E;v=false else u="Save failed: "..tostring(K)v=true end end;local function M(N)return math.max(1,math.ceil(#N/l))end;local function O(P,Q)return math.floor((Q-1)/l)end;local function R()local S={}local T=0;for P=1,#o do local U=#o[P]local V=M(o[P])for W=0,V-1 do T=T+1;S[T]={P,W*l+1}end end;return S,T end;local function X(S)local Y=O(q,p)for T,Z in ipairs(S)do if Z[1]==q and math.floor((Z[2]-1)/l)==Y then return T end end;return 1 end;local function _()local a0=#o[q]+1;if p>a0 then p=a0 end;if p<1 then p=1 end end;local function a1(S)local a2=X(S)if a2-1<r then r=a2-1 end;if a2-1>=r+n then r=a2-n end;if r<0 then r=0 end end;local function a3(c,a4)if#c>=a4 then return c:sub(1,a4)end;return c..string.rep(" ",a4-#c)end;local function a5()d(1,1)i(0x0000FF)g(0x000000)local a6=" edit"..(t and" - "..t or"")if s then a6=a6 .." [+]"end;local a7=tostring(q)..","..tostring(p).." "j(a3(a6 ..string.rep(" ",math.max(1,l-#a6-#a7))..a7,l))i(0x000000)g(0xFFFFFF)end;local function a8()d(1,m)i(0x0000FF)g(0x000000)if u~=""then if v then i(2)end;j(a3(" "..u,l))u=""v=false else j(a3(" ^W Save ^X Quit+Save ^P Quit ^K Cut ^U Paste ^F Find ^G Go",l))end;i(0x000000)g(0xFFFFFF)end;local function a9(S)local aa=X(S)local ab=O(q,p)local ac=p-ab*l;for ad=1,n do local T=r+ad;d(1,ad+1)local Z=S[T]if Z then local P=Z[1]local ae=Z[2]local af=o[P]:sub(ae,ae+l-1)local ag=T==aa;if ag then local ah=ac;ah=math.min(ah,#af+1)local ai=af:sub(1,ah-1)local aj=ah>#af and" "or af:sub(ah,ah)local ak=af:sub(ah+1)g(0xFFFFFF)i(0x000000)j(ai)if z then g(0x000000)i(0xFFFFFF)else g(0xFFFFFF)i(0x000000)end;j(aj)g(0xFFFFFF)i(0x000000)j(ak)local al=#ai+1+#ak;if al<l then j(string.rep(" ",l-al))end else if P==y and x~=""and Z[2]==1 then local c,am=af:find(x)if c then g(0xFFFFFF)i(0x000000)j(af:sub(1,c-1))g(0x000000)i(0x00FF00)j(af:sub(c,am))g(0xFFFFFF)i(0x000000)j(af:sub(am+1))j(string.rep(" ",l-#af))else g(0xFFFFFF)i(0x000000)j(a3(af,l))end else g(0xFFFFFF)i(0x000000)j(a3(af,l))end end else g(0xDBDBDB)i(0x000000)j(a3("~",l))g(0xFFFFFF)end end end;local function an()l,m=b()n=m-2;local S=R()a1(S)a5()a9(S)a8()d(1,m)i(0x000000)g(0xFFFFFF)end;local function ao(ap,aq)local ar=aq or""while true do d(1,m)i(0x00FF00)g(0x000000)j(a3(" "..ap..ar.." ",l))i(0x000000)g(0xFFFFFF)local as=syscall.read(0)if not as or as==""then sleep(0.02)elseif as==""then return nil elseif as=="\n"then return ar elseif as=="\b"then if#ar>0 then ar=ar:sub(1,-2)end else local at=as:byte(1)if at>=32 and at<127 then ar=ar..as:sub(1,1)end end end end;local function au(h)local H=o[q]o[q]=H:sub(1,p-1)..h..H:sub(p)p=p+1;s=true end;local function av()if p>1 then local H=o[q]o[q]=H:sub(1,p-2)..H:sub(p)p=p-1;s=true elseif q>1 then local aw=o[q-1]p=#aw+1;o[q-1]=aw..o[q]table.remove(o,q)q=q-1;s=true end end;local function ax()local H=o[q]if p<=#H then o[q]=H:sub(1,p-1)..H:sub(p+1)s=true elseif q<#o then o[q]=H..o[q+1]table.remove(o,q+1)s=true end end;local function ay()local H=o[q]local az=H:sub(1,p-1)local aA=H:sub(p)local aB=az:match("^(%s*)")or""o[q]=az;table.insert(o,q+1,aB..aA)q=q+1;p=#aB+1;s=true end;local function aC()w=o[q]table.remove(o,q)if#o==0 then o={""}end;if q>#o then q=#o end;p=1;s=true;u="Cut"end;local function aD()if not w then u="Nothing to paste"return end;table.insert(o,q,w)q=q+1;p=1;s=true;u="Pasted"end;local function aE()if x==""then local B=ao("Find: ","")if not B or B==""then s=true;return end;x=B;y=0 end;local aF=y>0 and y or q;for L=1,#o do local aG=(aF-1+L)%#o+1;if o[aG]:find(x)then q=aG;y=aG;p=o[aG]:find(x)or 1;u="Found: line "..aG;s=true;return end end;u="Not found: "..x;v=true;s=true end;local function aH()local B=ao("Go to line: ","")if not B then s=true;return end;local aI=tonumber(B)if not aI then u="Not a number"v=true;s=true;return end;q=math.max(1,math.min(#o,math.floor(aI)))p=1;u="Line "..q;s=true end;local function aJ()if not t then local B=ao("Save as: ","")s=true;if not B or B==""then return false end;t=A(B)end;I(t)s=true;return not v end;local function aK(S)local a2=X(S)if a2<=1 then return end;local aL=S[a2-1]if not aL then return end;local aM=aL[1]local aN=aL[2]+(p-1)%l;p=math.min(aN,#o[aM]+1)q=aM end;local function aO(S)local a2=X(S)local aP=S[a2+1]if not aP then return end;local aM=aP[1]local aN=aP[2]+(p-1)%l;p=math.min(aN,#o[aM]+1)q=aM end;if a[1]then t=A(a[1])D(t)end;k()local aQ=true;while aQ do local S=R()local as=syscall.read(0)if as and as~=""then local at=as:byte(1)if as==""then aK(S)s=true elseif as==""then aO(S)s=true elseif as==""then if p<=#o[q]then p=p+1 elseif q<#o then q=q+1;p=1 end;s=true elseif as==""then if p>1 then p=p-1 elseif q>1 then q=q-1;p=#o[q]+1 end;s=true elseif as==""then p=1;s=true elseif as==""then p=#o[q]+1;s=true elseif as=="[5~"then for aR=1,n do aK(S)end;s=true elseif as=="[6~"then for aR=1,n do aO(S)end;s=true elseif as=="[3~"then ax()elseif as=="\n"then ay()elseif as=="\b"then av()elseif as=="\t"then for aR=1,4 do au(" ")end elseif at==6 then local B=ao("Find: ",x)s=true;if B then x=B;y=0;aE()end elseif at==7 then aH()elseif at==11 then aC()elseif at==14 then if x==""then local B=ao("Find: ","")s=true;if B then x=B;y=0 end end;aE()elseif at==16 then if s then local B=ao("Unsaved changes. Quit? [y/N] ","")s=true;if B and B:lower()=="y"then aQ=false end else aQ=false end elseif at==21 then aD()elseif at==23 then aJ()elseif at==24 then aJ()aQ=false else if at>=32 and at<127 then au(as:sub(1,1))end end end;local aS=math.floor(syscall.getUptime()/500)%2==0;if aS~=z then z=aS;s=true end;if s then _()an()s=false end end;k()g(0xFFFFFF)i(0x000000)d(1,1)print("edit: exited"..(t and" - "..t or""))