Advertisement
CaptainSpaceCat

Edit++ Minimalization Project

Jun 24th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.40 KB | None | 0 0
  1. local tArgs = {...}
  2. local w, h = term.getSize()
  3. local lChanged = false
  4. local pChanged = true
  5. local mChanged = true
  6. local numlength = 1
  7. local type = 1
  8. local tabNum = 0
  9. local mode = "edit"
  10. local clickStart, clickEnd, prevline, currentLine, currentPos
  11. local cTables, cComTab, program, colTab = {}, {}, {}, {}
  12. cParTab = {}
  13. if mode == "edit" then
  14.     colTab[1] = colors.lightGray
  15.     colTab[2] = colors.green
  16.     colTab[3] = colors.red
  17.     colTab[4] = colors.orange
  18.     colTab[5] = colors.lightBlue
  19.     colTab[6] = colors.white
  20. elseif mode == "read" then
  21.     colTab[1] = colors.white
  22.     colTab[2] = colors.white
  23.     colTab[3] = colors.white
  24.     colTab[4] = colors.white
  25.     colTab[5] = colors.white
  26.     colTab[6] = colors.white
  27. end
  28. local location = {
  29.     ["x"] = 1,
  30.     ["y"] = 1
  31. }
  32. local position = {
  33.     ["x"] = 1,
  34.     ["y"] = 1
  35. }
  36. local conditionals = {
  37.     ["if"] = {"end", colors.red, colors.pink},
  38.     ["while"] = {"end", colors.red, colors.pink},
  39.     ["for"] = {"end", colors.red, colors.pink},
  40.     ["function"] = {"end", colors.red, colors.pink}
  41. }
  42. local keywords = {
  43.     ["if"] = colors.yellow,
  44.     ["then"] = colors.yellow,
  45.     ["else"] = colors.yellow,
  46.     ["elseif"] = colors.yellow,
  47.     ["end"] = colors.yellow,
  48.     ["function"] = colors.yellow,
  49.     ["while"] = colors.yellow,
  50.     ["for"] = colors.yellow,
  51.     ["in"] = colors.yellow,
  52.     ["do"] = colors.yellow,
  53.     ["local"] = colors.yellow,
  54.     ["repeat"] = colors.yellow,
  55.     ["until"] = colors.yellow,
  56.     ["next"] = colors.yellow,
  57.     ["and"] = colors.orange,
  58.     ["or"] = colors.orange,
  59.     ["not"] = colors.orange,
  60.     ["true"] = colors.orange,
  61.     ["false"] = colors.orange,
  62.     ["nil"] = colors.gray,
  63.     ["break"] = colors.purple,
  64.     ["return"] = colors.purple,
  65.     ["print"] = colors.pink,
  66.     ["write"] = colors.pink,
  67.     ["pairs"] = colors.pink,
  68.     ["ipairs"] = colors.pink,
  69.     ["type"] = colors.blue,
  70.     ["tostring"] = colors.blue,
  71.     ["tonumber"] = colors.blue,
  72.     ["assert"] = colors.blue,
  73.     ["setmetatable"] = colors.blue
  74. }
  75.  
  76. if tArgs[1] and fs.exists(tArgs[1]) then
  77.     for line in io.lines(tArgs[1]) do
  78.         program[#program + 1] = tostring(line)
  79.         program[#program] = program[#program]:gsub("    ", "$___")
  80.     end
  81.     if #program == 0 then
  82.         program[1] = ""
  83.     end
  84. elseif tArgs[1] then
  85.     program[1] = ""
  86. end
  87.  
  88. local function draw(string, xPos, yPos, txtcol, bakcol)
  89.     local string = string or ""
  90.     local txtcol = txtcol or colors.white
  91.     local bakcol = bakcol or colors.black
  92.     term.setCursorPos(xPos, yPos)
  93.     term.setBackgroundColor(bakcol)
  94.     term.setTextColor(txtcol)
  95.     term.write(string)
  96. end
  97.  
  98. local function codeDraw(string, xPos, yPos)
  99.     local string = string or ""
  100.     local xPos = xPos - location["x"] + 1
  101.     local sStart, sEnd = 1, 1 + w - numlength
  102.     local tabDraw = true
  103.     local multiComment = false
  104.     while #string > 0 and xPos <= sEnd do
  105.         --[[for i in pairs(cComTab) do
  106.             if mode == "edit" then
  107.                 if not multiComment and (yPos-1 == cComTab[i][1][2]-location["y"] + 1 and xPos-3 >= cComTab[i][1][1]-location["x"] + 1) or (yPos-1 == cComTab[i][2][2]-location["y"]+1 and xPos-2 <= cComTab[i][2][1]-location["x"]+1) or (yPos-1 > cComTab[i][1][2]-location["y"]+1 and yPos-1 < cComTab[i][2][2]-location["y"]+1) then
  108.                     colTab[1] = colors.green
  109.                     colTab[2] = colors.green
  110.                     colTab[3] = colors.green
  111.                     colTab[4] = colors.green
  112.                     colTab[5] = colors.green
  113.                     colTab[6] = colors.green
  114.                     multiComment = true
  115.                     break
  116.                 elseif multiComment then
  117.                     colTab[1] = colors.lightGray
  118.                     colTab[2] = colors.green
  119.                     colTab[3] = colors.red
  120.                     colTab[4] = colors.orange
  121.                     colTab[5] = colors.lightBlue
  122.                     colTab[6] = colors.white
  123.                     multiComment = false
  124.                 end
  125.             end
  126.         end]]--
  127.         if string:find("^%$___") then
  128.             local _, en = string:find("^%$___")
  129.             if en >= sStart and xPos <= sEnd then
  130.                 if tabDraw and not multiComment then
  131.                     draw("   |", xPos, yPos, colTab[1], colors.black)
  132.                 else
  133.                     draw("    ", xPos, yPos, colTab[1], colors.black)
  134.                 end
  135.             end
  136.             string = string:sub(en + 1)
  137.             xPos = xPos + en
  138.         elseif string:find("^%-%-.*") then
  139.             tabDraw = false
  140.             local _, en = string:find("^%-%-.*")
  141.             if en >= sStart and xPos <= sEnd then
  142.                 draw(string:sub(1, en), xPos, yPos, colTab[2], colors.black)
  143.             end
  144.             string = string:sub(en + 1)
  145.             xPos = xPos + en
  146.         elseif string:find("^\".-[^\\]\"") then
  147.             tabDraw = false
  148.             local _, en = string:find("^\".-[^\\]\"")
  149.             if en >= sStart and xPos <= sEnd then
  150.                 draw(string:sub(1, en), xPos, yPos, colTab[3], colors.black)
  151.             end
  152.             string = string:sub(en + 1)
  153.             xPos = xPos + en
  154.         elseif string:find("^\'.-[^\\]\'") then
  155.             tabDraw = false
  156.             local _, en = string:find("^\'.-[^\\]\'")
  157.             if en >= sStart and xPos <= sEnd then
  158.                 draw(string:sub(1, en), xPos, yPos, colTab[3], colors.black)
  159.             end
  160.             string = string:sub(en + 1)
  161.             xPos = xPos + en
  162.         elseif string:find("^\"\"") then
  163.             tabDraw = false
  164.             local _, en = string:find("^\"\"")
  165.             if en >= sStart and xPos <= sEnd then
  166.                 draw(string:sub(1, en), xPos, yPos, colTab[3], colors.black)
  167.             end
  168.             string = string:sub(en + 1)
  169.             xPos = xPos + en
  170.         elseif string:find("^\'\'") then
  171.             tabDraw = false
  172.             local _, en = string:find("^\'\'")
  173.             if en >= sStart and xPos <= sEnd then
  174.                 draw(string:sub(1, en), xPos, yPos, colTab[3], colors.black)
  175.             end
  176.             string = string:sub(en + 1)
  177.             xPos = xPos + en
  178.         elseif string:find("^%[%[.-%]%]") then
  179.             tabDraw = false
  180.             local _, en = string:find("^%[%[.-%]%]")
  181.             if en >= sStart and xPos <= sEnd then
  182.                 draw(string:sub(1, en), xPos, yPos, colTab[3], colors.black)
  183.             end
  184.             string = string:sub(en + 1)
  185.             xPos = xPos + en
  186.         elseif string:find("^%d+") then
  187.             tabDraw = false
  188.             local _, en = string:find("^%d+")
  189.             if en >= sStart and xPos <= sEnd then
  190.                 draw(string:sub(1, en), xPos, yPos, colTab[4], colors.black)
  191.             end
  192.             string = string:sub(en + 1)
  193.             xPos = xPos + en
  194.         elseif string:find("^%p") then
  195.             tabDraw = false
  196.             local _, en = string:find("^%p")
  197.             if en >= sStart and xPos <= sEnd then
  198.                 draw(string:sub(1, en), xPos, yPos, colTab[5], colors.black)
  199.             end
  200.             string = string:sub(en + 1)
  201.             xPos = xPos + en
  202.         elseif string:find("^[%a_]+") then
  203.             tabDraw = false
  204.             local _, en = string:find("^[%a_]+")
  205.             local test = string:sub(1, en)
  206.             local color = colors.white
  207.             if keywords[test] and mode == "edit" then
  208.                 color = keywords[test]
  209.             end
  210.             if multiComment then
  211.                 color = colors.green
  212.             end
  213.             if en >= sStart and xPos <= sEnd then
  214.                 draw(string:sub(1, en), xPos, yPos, color, colors.black)
  215.             end
  216.             string = string:sub(en + 1)
  217.             xPos = xPos + en
  218.         elseif string:find("^[^%w_]") then
  219.             tabDraw = false
  220.             local _, en = string:find("^[^%w_]")
  221.             if en >= sStart and xPos <= sEnd then
  222.                 draw(string:sub(1, en), xPos, yPos, colTab[6], colors.black)
  223.             end
  224.             string = string:sub(en + 1)
  225.             xPos = xPos + en
  226.         end
  227.     end
  228. end
  229.  
  230. local function printMenu()
  231.     term.setCursorPos(1, 1)
  232.     term.setBackgroundColor(colors.lightGray)
  233.     term.clearLine()
  234.     draw("Press CTRL for menu...", 1, 1, colors.gray, colors.lightGray)
  235. end
  236.  
  237. local function saveFile(codeTable, filename)
  238.     local tempFile = {}
  239.     for i, v in ipairs(codeTable) do
  240.         tempFile[i] = v:gsub("$___", "    ")
  241.     end
  242.     local sFile = fs.open(filename, "w")
  243.     for _, v in ipairs(tempFile) do
  244.         sFile.writeLine(v)
  245.     end
  246.     sFile.close()
  247. end
  248.  
  249. local function showProgram()
  250.     multiComment = false
  251.     numlength = #tostring(#program - location["y"] + 1 >= h and location["y"] + h - 1  or #program)
  252.     for y = location["y"], #program - location["y"] + 1 >= h - 1 and location["y"] + h - 2  or #program do
  253.         codeDraw(program[y], 1 + numlength, y - location["y"] + 2)
  254.     end
  255. end
  256.  
  257. local function showProgramLine(line)
  258.     numlength = #tostring(#program - location["y"] + 1 >= h and location["y"] + h - 1  or #program)
  259.     codeDraw(program[line], 1 + numlength, line - location["y"] + 2)
  260. end
  261.  
  262. local function showNumbers()
  263.     local activeLoops = {}
  264.     local up, down
  265.     for _, v in pairs(cTables) do
  266.         if currentLine >= v[1] and currentLine <= v[2] then
  267.             activeLoops[#activeLoops + 1] = {v[1], v[2]}
  268.         end
  269.     end
  270.     if #activeLoops > 0 then
  271.         local key, mini = 0, #program
  272.         for i, v in pairs(activeLoops) do
  273.             if v[2] - v[1] < mini then
  274.                 key, mini = i, v[2] - v[1]
  275.             end
  276.         end
  277.         if key > 0 then
  278.             up, down = activeLoops[key][1], activeLoops[key][2]
  279.         end
  280.     end
  281.     for y = location["y"], #program - location["y"] + 1 >= h - 1 and location["y"] + h - 2  or #program do
  282.         local text, back = colors.gray, colors.lightGray
  283.         if up and down and y > up and y < down then
  284.             text, back = colors.red, colors.pink
  285.         elseif up and down and (y == up or y == down) then
  286.             text, back = colors.pink, colors.red
  287.         end
  288.         draw(tostring(y), 1, y - location["y"] + 2, text, back)
  289.         if #tostring(y) < numlength then
  290.             write(" ")
  291.         end
  292.     end
  293. end
  294.  
  295. local function checkParenthesis()
  296.     local cancelNum = 0
  297.     local pString = ""
  298.     pBreaks = {}
  299.     local cBrackets = {}
  300.     pBreaks[1] = 0
  301.     for y = 1, #program do
  302.         pString = pString .. program[y]
  303.         pBreaks[y+1] = #program[y] + pBreaks[y]
  304.     end
  305.     while pString:find("[%[%{%(]") do
  306.         local oppChar
  307.         local st, en = pString:find("[%[%{%(]")
  308.         term.setBackgroundColor(colors.magenta)
  309.         local char = pString:sub(st, st)
  310.         if char == "[" then oppChar = "]"
  311.         elseif char == "{" then oppChar = "}"
  312.         elseif char == "(" then oppChar = ")" end
  313.         st, en = pString:find("%b" .. char .. oppChar)
  314.         if st and en then
  315.             local x1, y1, x2, y2
  316.             local active = true
  317.             for i, v in pairs(pBreaks) do
  318.                 if st <= v and active then
  319.                     x1 = st - pBreaks[i - 1]
  320.                     y1 = i - 1
  321.                     active = false
  322.                 end
  323.                 if en <= v then
  324.                     x2 = en - pBreaks[i - 1]
  325.                     y2 = i - 1
  326.                 end
  327.                 if x1 and y1 and x2 and y2 then
  328.                     cBrackets[#cBrackets + 1] = {x1, y1, x2, y2, char, oppChar}
  329.                     break
  330.                 end
  331.             end
  332.             pString = pString:sub(1, st - 1) .. " " .. pString:sub(st + 1)
  333.             pString = pString:sub(1, en - 1) .. " " .. pString:sub(en + 1)
  334.         else
  335.             st, en = pString:find("[%[%{%(]")
  336.             pString = pString:sub(1, st - 1) .. " " .. pString:sub(st + 1)
  337.         end
  338.     end
  339.     return cBrackets
  340. end
  341.  
  342. local function checkConditionals()
  343.     local cWords = {}
  344.     local active = false
  345.     local cancelNum = 0
  346.     local cConditional = {}
  347.     for y = 1, #program do
  348.         for i in pairs(conditionals) do
  349.             local test = program[y]:gsub(" ", "")
  350.             local st = test:find(i)
  351.             local _, en = program[y]:find(i)
  352.             if st and en then
  353.                 if st == 1 and (en == #program[y] or program[y]:sub(en + 1, en + 1) == " ") then
  354.                     cWords[#cWords + 1] = {}
  355.                     cWords[#cWords][1] = y
  356.                     cancelNum = cancelNum + 1
  357.                     cConditional[cancelNum] = #cWords
  358.                     active = true
  359.                 end
  360.             end
  361.         end
  362.         if active and program[y]:find("end") then
  363.             cWords[cConditional[cancelNum]][2] = y
  364.             cancelNum = cancelNum - 1
  365.             if cancelNum == 0 then
  366.                 active = false
  367.                 if y > location["y"] + h - 2 then
  368.                     break
  369.                 end
  370.             end
  371.         end
  372.     end
  373.     local rem = {}
  374.     for i = 1, #cWords do
  375.         if not (cWords[i][1] and cWords[i][2]) then
  376.             rem[#rem + 1] = i
  377.         end
  378.     end
  379.     for i, v in pairs(rem) do
  380.         table.remove(cWords, v - i + 1)
  381.     end
  382.     return cWords
  383. end
  384.  
  385. function checkMultiComments()
  386.     local cComments = {}
  387.     local active = false
  388.     local cancelNum = 0
  389.     local cConditional = {}
  390.     for y = 1, #program do
  391.         if program[y]:find("%-%-%[%[") and not active then
  392.             active = true
  393.             local st = program[y]:find("%-%-%[%[")
  394.             cComments[#cComments + 1] = {}
  395.             cComments[#cComments][1]= {st, y}
  396.         elseif program[y]:find("%]%]") and active then
  397.             active = false
  398.             local _, en = program[y]:find("%]%]")
  399.             cComments[#cComments][2] = {en, y}
  400.         end
  401.     end
  402.     local rem = {}
  403.     for i = 1, #cComments do
  404.         if #cComments[i] < 2  then
  405.             rem[#rem + 1] = i
  406.         end
  407.     end
  408.     for i, v in pairs(rem) do
  409.         table.remove(cComments, v - i + 1)
  410.     end
  411.     return cComments
  412. end
  413.  
  414. local function addLetter(char, str, pos)
  415.     return str:sub(0, pos - 1) .. tostring(char) .. str:sub(pos)
  416. end
  417.  
  418. local function removeLetter(str, pos)
  419.     return str:sub(0, pos) .. str:sub(pos + 2)
  420. end
  421.  
  422. local function fixCursorPos()
  423.     if position["y"] < 1 then
  424.         pChanged = true
  425.         location["y"] = location["y"] + position["y"] - 1
  426.         position["y"] = 1
  427.     elseif position["y"] > h - 1 then
  428.         pChanged = true
  429.         location["y"] = location["y"] + position["y"] - (h-1)
  430.         position["y"] = h - 1
  431.     end
  432.     if location["y"] < 1 then
  433.         pChanged = false
  434.         location["y"] = 1
  435.     elseif location["y"] + h - 1 > #program then
  436.         pChanged = false
  437.         location["y"] = #program - h + 1
  438.     end
  439.     local dollarNum
  440.     if program[location["y"] + position["y"] - 1]:sub(location["x"] + position["x"] - 1, location["x"] + position["x"] - 1) == "_" then
  441.         for i = 1, 3 do
  442.         if program[location["y"] + position["y"] - 1]:sub(location["x"] + position["x"] - 1 - i, location["x"] + position["x"] - 1 - i) ~= "_" then
  443.                 if program[location["y"] + position["y"] - 1]:sub(location["x"] + position["x"] - 1 - i, location["x"] + position["x"] - 1 - i) == "$" then
  444.                     dollarNum = i
  445.                     break
  446.                 end
  447.                 break
  448.             end
  449.         end
  450.         if dollarNum and dollarNum < 3 then
  451.             for i = 1, 3 - dollarNum do
  452.                 if program[location["y"] + position["y"] - 1]:sub(location["x"] + position["x"] - 1 + i, location["x"] + position["x"] - 1 + i) ~= "_" then
  453.                     dollarNum = nil
  454.                     break
  455.                 end
  456.             end
  457.         end
  458.         if dollarNum then
  459.             position["x"] = position["x"] - numlength - dollarNum + 2
  460.         end
  461.     end
  462.     if position["x"] + location["x"] - 1 > #program[position["y"] + location["y"] - 1] then
  463.         position["x"] = #program[position["y"] + location["y"] - 1] - location["x"] + 2
  464.     end
  465.     if position["x"] < 1 then
  466.         pChanged = true
  467.         location["x"] = location["x"] + position["x"] - 1
  468.         position["x"] = 1
  469.     elseif position["x"] > w - numlength then
  470.         pChanged = true
  471.         location["x"] = location["x"] + position["x"] - (w-numlength)
  472.         position["x"] = w - numlength
  473.     end
  474.     if location["x"] < 1 then
  475.         pChanged = false
  476.         location["x"] = 1
  477.     elseif location["x"] + w - numlength > #program then
  478.         pChanged = false
  479.         location["x"] = #program - w + numlength
  480.     end
  481.     if mode == "read" and location["x"] < 3 then
  482.         location["x"] = 3
  483.     elseif mode == "edit" and location["x"] < 1 then
  484.         location["x"] = 1
  485.     end
  486.     currentLine = location["y"] + position["y"] - 1
  487.     currentPos = position["x"] + location["x"] - 1
  488. end
  489.  
  490. term.setCursorBlink(true)
  491. while true do
  492.     if mode == "edit" then
  493.         numlength = #tostring(#program - location["y"] + 1 >= h and location["y"] + h - 1  or #program)
  494.     elseif mode == "read" then
  495.         numlength = 0
  496.     end
  497.     fixCursorPos()
  498.     --cComTab = checkMultiComments()
  499.     if pChanged or program[currentLine]:sub(currentPos - 2, currentPos - 1) == "]]" then
  500.         --cTables = checkConditionals()
  501.         term.setBackgroundColor(colors.black)
  502.         term.clear()
  503.         showProgram()
  504.         pChanged = false
  505.         lChanged = false
  506.     elseif lChanged then
  507.         --cTables = checkConditionals()
  508.         term.setBackgroundColor(colors.black)
  509.         term.clearLine()
  510.         showProgramLine(currentLine)
  511.         lChanged = false
  512.     end
  513.     if mChanged then
  514.         printMenu()
  515.         mChanged = false
  516.     end
  517.     --[[if prevline then
  518.         showProgramLine(prevline[1])
  519.         showProgramLine(prevline[2])
  520.         prevline = nil
  521.     end
  522.     if mode == "edit" then
  523.         cParTab = checkParenthesis()
  524.     end
  525.     for i, v in pairs(cParTab) do
  526.         if (currentPos == cParTab[i][1] and currentLine == cParTab[i][2]) or (currentPos == cParTab[i][3] and currentLine == cParTab[i][4]) then
  527.             draw(cParTab[i][5], cParTab[i][1] + numlength - location["x"] + 1, cParTab[i][2] - location["y"] + 2, colors.blue, colors.lightBlue)
  528.             draw(cParTab[i][6], cParTab[i][3] + numlength - location["x"] + 1, cParTab[i][4] - location["y"] + 2, colors.blue, colors.lightBlue)
  529.             prevline = {cParTab[i][2], cParTab[i][4]}
  530.         end
  531.     end]]
  532.     if mode == "edit" then
  533.         showNumbers()
  534.     end
  535.     term.setTextColor(colors.white)
  536.     term.setCursorPos(position["x"] + numlength, position["y"] + 1)
  537.     local events = {os.pullEvent()}
  538.     if events[1] == "char" and mode == "edit" then
  539.         lChanged = true
  540.         program[currentLine] = addLetter(events[2], program[currentLine], position["x"] + location["x"] - 1)
  541.         position["x"] = position["x"] + 1
  542.     elseif events[1] == "key" then
  543.         if events[2] == keys.up then   --^
  544.                 position["y"] = position["y"] - 1
  545.         elseif events[2] == keys.down then   --v
  546.                 position["y"] = position["y"] + 1
  547.         elseif events[2] == keys.right then   -->
  548.             if (position["x"] + location["x"] - 1) % 4 == 1 and position["x"] + location["x"] - 1 < #program[currentLine] and program[currentLine]:sub(position["x"] + location["x"] - 1, position["x"] + location["x"] + 2) == "$___" then
  549.                 position["x"] = position["x"] + 4
  550.             elseif currentLine < #program and currentPos > #program[currentLine] then
  551.                 position["x"] = 1 - location["x"] + 1
  552.                 position["y"] = position["y"] + 1
  553.             else
  554.                 position["x"] = position["x"] + 1
  555.             end
  556.         elseif events[2] == keys.left then   --<
  557.             if (position["x"] + location["x"] - 1) % 4 == 1 and position["x"] + location["x"] - 1 > 4 and program[currentLine]:sub(position["x"] + location["x"] - 5, position["x"] + location["x"] - 2) == "$___" then
  558.                 position["x"] = position["x"] - 4
  559.             elseif currentLine > 1 and currentPos == 1 then
  560.                 position["x"] = #program[currentLine - 1] + 1
  561.                 position["y"] = position["y"] - 1
  562.             else
  563.                 position["x"] = position["x"] - 1
  564.             end
  565.         end
  566.         if events[2] == keys.backspace and mode == "edit" then
  567.             lChanged = true
  568.             if (position["x"] + location["x"] - 1) % 4 == 1 and position["x"] + location["x"] - 1 > 4 and program[currentLine]:sub(position["x"] + location["x"] - 5, position["x"] + location["x"] - 2) == "$___"  then
  569.                 for i = 1, 4 do
  570.                     program[currentLine] = removeLetter(program[currentLine], location["x"] + position["x"] - 6)
  571.                     position["x"] = position["x"] - 4
  572.                 end
  573.             elseif currentLine > 1 and currentPos == 1 then
  574.                 position["y"] = position["y"] - 1
  575.                 location["x"] = (location["x"] > #program[currentLine - 1] or location["x"] + w - #tostring(currentLine - 1) < #program[currentLine - 1]) and #program[currentLine - 1] + 1 or location["x"]
  576.                 position["x"] = #program[currentLine - 1] - location["x"] + 2
  577.                 program[currentLine - 1] = program[currentLine - 1] .. (table.remove(program, currentLine))
  578.                 pChanged = true
  579.                         else
  580.                 program[currentLine] = removeLetter(program[currentLine], currentPos - 2)
  581.                 position["x"] = position["x"] - 1
  582.             end
  583.         end
  584.         if events[2] == keys.enter and mode == "edit" then
  585.             pChanged = true
  586.             table.insert(program, currentLine + 1, program[currentLine]:sub(position["x"] + location["x"] - 1))
  587.             program[currentLine] = program[currentLine]:sub(1, position["x"] + location["x"] - 2)
  588.             program[currentLine + 1] = string.rep("$___", tabNum) .. program[currentLine + 1]
  589.             position["y"] = position["y"] + 1
  590.             position["x"] = 1 + tabNum * 4
  591.         end
  592.         if events[2] == keys.tab and mode == "edit" then
  593.             lChanged = true
  594.             if (position["x"] + location["x"] - 1) % 4 == 1 then
  595.                 program[currentLine] = addLetter("$___", program[currentLine], position["x"] + location["x"] - 1)
  596.             else
  597.                 local num = (position["x"] + location["x"] - 1) % 4 + 1
  598.                 if num == 4 then num = 2 end
  599.                 program[currentLine] = addLetter(string.rep(" ", num), program[currentLine], position["x"] + location["x"] - 1)
  600.             end
  601.             position["x"] = position["x"] + 4
  602.         end
  603.         if events[2] == keys.leftCtrl then
  604.             mChanged = true
  605.             term.setCursorBlink(false)
  606.             term.setCursorPos(1, 1)
  607.             term.setBackgroundColor(colors.lightGray)
  608.             term.clearLine()
  609.             local broken = false
  610.             while true do
  611.                 if type == 1 then
  612.                     draw("Save", 1, 1, colors.gray, colors.white)
  613.                     draw("Exit", 8, 1, colors.gray, colors.lightGray)
  614.                     draw("Funcs", 15, 1, colors.gray, colors.lightGray)
  615.                 elseif type == 2 then
  616.                     draw("Save", 1, 1, colors.gray, colors.lightGray)
  617.                     draw("Exit", 8, 1, colors.gray, colors.white)
  618.                     draw("Funcs", 15, 1, colors.gray, colors.lightGray)
  619.                 elseif type == 3 then
  620.                     draw("Save", 1, 1, colors.gray, colors.lightGray)
  621.                     draw("Exit", 8, 1, colors.gray, colors.lightGray)
  622.                     draw("Funcs", 15, 1, colors.gray, colors.white)
  623.                 end
  624.                 local events = {os.pullEvent()}
  625.                 if events[1] == "key" then
  626.                     if events[2] == keys.enter then
  627.                         if type == 1 then
  628.                             draw("Save", 1, 1, colors.gray, colors.yellow)
  629.                             saveFile(program, tArgs[1])
  630.                             sleep(.2)
  631.                         elseif type == 2 then
  632.                             draw("Exit", 8, 1, colors.gray, colors.yellow)
  633.                             broken = true
  634.                             sleep(.2)
  635.                             break
  636.                         elseif type == 3 then
  637.                             draw("Funcs", 15, 1, colors.gray, colors.yellow)
  638.                             sleep(.1)
  639.                             term.setCursorPos(1, 1)
  640.                             term.setBackgroundColor(colors.lightGray)
  641.                             term.clearLine()
  642.                             local choice = 1
  643.                             local brokenIn = false
  644.                             while true do
  645.                                 if choice == 1 then
  646.                                     draw("Move", 1, 1, colors.gray, colors.white)
  647.                                     draw("Mode", 8, 1, colors.gray, colors.lightGray)
  648.                                 elseif choice == 2 then
  649.                                     draw("Move", 1, 1, colors.gray, colors.lightGray)
  650.                                     draw("Mode", 8, 1, colors.gray, colors.white)  
  651.                                 end
  652.                                 local events = {os.pullEvent()}
  653.                                 if events[1] == "key" then
  654.                                     if events[2] == keys.leftCtrl then
  655.                                         break
  656.                                     end
  657.                                     if events[2] == keys.left then
  658.                                         choice = choice - 1
  659.                                     elseif events[2] == keys.right then
  660.                                         choice = choice + 1
  661.                                     end
  662.                                     if choice == 3 then choice = 2 end
  663.                                     if choice == 0 then choice = 1 end
  664.                                     if events[2] == keys.enter then
  665.                                         if choice == 1 then
  666.                                             draw("Move", 1, 1, colors.gray, colors.yellow)
  667.                                             draw("    ", 1, 2, colors.gray, colors.white)
  668.                                             term.setCursorPos(1, 2)
  669.                                             local line = read()
  670.                                             if tonumber(line) and tonumber(line) > 0 then
  671.                                                 if tonumber(line) < #program - h + 2 then
  672.                                                     location["y"] = tonumber(line)
  673.                                                 else
  674.                                                     location["y"] = #program - h + 2
  675.                                                 end
  676.                                                 pChanged = true
  677.                                             end
  678.                                             brokenIn = true
  679.                                             break
  680.                                         elseif choice == 2 then
  681.                                             pChanged = true
  682.                                             draw("Mode", 8, 1, colors.gray, colors.yellow)
  683.                                             local tempmode = mode
  684.                                             while true do
  685.                                                 if mode == "edit" then
  686.                                                     draw("Edit", 8, 2, colors.white, colors.lime)
  687.                                                     draw("Read", 8, 3, colors.gray, colors.white)
  688.                                                 elseif mode == "read" then
  689.                                                     draw("Edit", 8, 2, colors.gray, colors.white)
  690.                                                     draw("Read", 8, 3, colors.white, colors.lime)
  691.                                                 end
  692.                                                 local events = {os.pullEvent()}
  693.                                                 if events[1] == "key" then
  694.                                                     if events[2] == keys.leftCtrl then
  695.                                                         break
  696.                                                     end
  697.                                                     if events[2] == keys.up then
  698.                                                         mode = "edit"
  699.                                                     elseif events[2] == keys.down then
  700.                                                         mode = "read"
  701.                                                     end
  702.                                                     if events[2] == keys.enter then
  703.                                                         if mode == "edit" and tempmode == "read" then
  704.                                                             location["x"] = 1
  705.                                                         end
  706.                                                         if mode == "edit" then
  707.                                                             colTab[1] = colors.lightGray
  708.                                                             colTab[2] = colors.green
  709.                                                             colTab[3] = colors.red
  710.                                                             colTab[4] = colors.orange
  711.                                                             colTab[5] = colors.lightBlue
  712.                                                             colTab[6] = colors.white
  713.                                                         elseif mode == "read" then
  714.                                                             colTab[1] = colors.white
  715.                                                             colTab[2] = colors.white
  716.                                                             colTab[3] = colors.white
  717.                                                             colTab[4] = colors.white
  718.                                                             colTab[5] = colors.white
  719.                                                             colTab[6] = colors.white
  720.                                                         end
  721.                                                         break
  722.                                                     end
  723.                                                 end
  724.                                             end
  725.                                             brokenIn = true
  726.                                             break
  727.                                         end
  728.                                     end
  729.                                 end
  730.                             end
  731.                             if brokenIn then break end
  732.                         end
  733.                     end
  734.                     if events[2] == keys.leftCtrl then
  735.                         break
  736.                     end
  737.                     if events[2] == keys.left then
  738.                         type = type - 1
  739.                     elseif events[2] == keys.right then
  740.                         type = type + 1
  741.                     end
  742.                 end
  743.                 if type == 4 then type = 3 end
  744.                 if type == 0 then type = 1 end
  745.             end
  746.             term.setCursorBlink(true)
  747.             if broken then
  748.                 term.setBackgroundColor(colors.black)
  749.                 term.clear()
  750.                 term.setCursorPos(1, 1)
  751.                 break
  752.             end
  753.         end
  754.     end
  755.     if events[1] == "mouse_click" and events[2] == 1 and #program >= events[4] + location["y"] - 2 and events[4] > 1 then
  756.         position["y"] = events[4] - 1
  757.         position["x"] = events[3] - numlength
  758.     elseif events[1] == "mouse_drag" and events[2] == 1 then
  759.         clickEnd = {events[3] + location["x"] - 1, events[4] + location["y"] - 1}
  760.     end
  761.     if events[1] == "mouse_scroll" then
  762.         pChanged = true
  763.         location["y"] = location["y"] + events[2]
  764.     end
  765. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement