Advertisement
CaptainSpaceCat

Edit++

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