Advertisement
CaptainSpaceCat

Edit++ Backup

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