Advertisement
CaptainSpaceCat

Edit++ Pastebin Edition

Jun 25th, 2015
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  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. local function draw(string, xPos, yPos, txtcol, bakcol)
  77.     local string = string or ""
  78.     local txtcol = txtcol or colors.white
  79.     local bakcol = bakcol or colors.black
  80.     term.setCursorPos(xPos, yPos)
  81.     term.setBackgroundColor(bakcol)
  82.     term.setTextColor(txtcol)
  83.     term.write(string)
  84. end
  85.  
  86. local function codeDraw(string, xPos, yPos)
  87.     local string = string or ""
  88.     local xPos = xPos - location["x"] + 1
  89.     local sStart, sEnd = 1, 1 + w - numlength
  90.     local tabDraw = true
  91.     local multiComment = false
  92.     while #string > 0 and xPos <= sEnd do
  93.         --[[for i in pairs(cComTab) do
  94.             if mode == "edit" then
  95.                 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
  96.                     colTab[1] = colors.green
  97.                     colTab[2] = colors.green
  98.                     colTab[3] = colors.green
  99.                     colTab[4] = colors.green
  100.                     colTab[5] = colors.green
  101.                     colTab[6] = colors.green
  102.                     multiComment = true
  103.                     break
  104.                 elseif multiComment then
  105.                     colTab[1] = colors.lightGray
  106.                     colTab[2] = colors.green
  107.                     colTab[3] = colors.red
  108.                     colTab[4] = colors.orange
  109.                     colTab[5] = colors.lightBlue
  110.                     colTab[6] = colors.white
  111.                     multiComment = false
  112.                 end
  113.             end
  114.         end]]--
  115.         if string:find("^%$___") then
  116.             local _, en = string:find("^%$___")
  117.             if en >= sStart and xPos <= sEnd then
  118.                 if tabDraw and not multiComment then
  119.                     draw("   |", xPos, yPos, colTab[1], colors.black)
  120.                 else
  121.                     draw("    ", xPos, yPos, colTab[1], colors.black)
  122.                 end
  123.             end
  124.             string = string:sub(en + 1)
  125.             xPos = xPos + en
  126.         elseif string:find("^%-%-.*") then
  127.             tabDraw = false
  128.             local _, en = string:find("^%-%-.*")
  129.             if en >= sStart and xPos <= sEnd then
  130.                 draw(string:sub(1, en), xPos, yPos, colTab[2], colors.black)
  131.             end
  132.             string = string:sub(en + 1)
  133.             xPos = xPos + en
  134.         elseif string:find("^\".-[^\\]\"") then
  135.             tabDraw = false
  136.             local _, en = string:find("^\".-[^\\]\"")
  137.             if en >= sStart and xPos <= sEnd then
  138.                 draw(string:sub(1, en), xPos, yPos, colTab[3], colors.black)
  139.             end
  140.             string = string:sub(en + 1)
  141.             xPos = xPos + en
  142.         elseif string:find("^\'.-[^\\]\'") then
  143.             tabDraw = false
  144.             local _, en = string:find("^\'.-[^\\]\'")
  145.             if en >= sStart and xPos <= sEnd then
  146.                 draw(string:sub(1, en), xPos, yPos, colTab[3], colors.black)
  147.             end
  148.             string = string:sub(en + 1)
  149.             xPos = xPos + en
  150.         elseif string:find("^\"\"") then
  151.             tabDraw = false
  152.             local _, en = string:find("^\"\"")
  153.             if en >= sStart and xPos <= sEnd then
  154.                 draw(string:sub(1, en), xPos, yPos, colTab[3], colors.black)
  155.             end
  156.             string = string:sub(en + 1)
  157.             xPos = xPos + en
  158.         elseif string:find("^\'\'") then
  159.             tabDraw = false
  160.             local _, en = string:find("^\'\'")
  161.             if en >= sStart and xPos <= sEnd then
  162.                 draw(string:sub(1, en), xPos, yPos, colTab[3], colors.black)
  163.             end
  164.             string = string:sub(en + 1)
  165.             xPos = xPos + en
  166.         elseif string:find("^%[%[.-%]%]") then
  167.             tabDraw = false
  168.             local _, en = string:find("^%[%[.-%]%]")
  169.             if en >= sStart and xPos <= sEnd then
  170.                 draw(string:sub(1, en), xPos, yPos, colTab[3], colors.black)
  171.             end
  172.             string = string:sub(en + 1)
  173.             xPos = xPos + en
  174.         elseif string:find("^%d+") then
  175.             tabDraw = false
  176.             local _, en = string:find("^%d+")
  177.             if en >= sStart and xPos <= sEnd then
  178.                 draw(string:sub(1, en), xPos, yPos, colTab[4], colors.black)
  179.             end
  180.             string = string:sub(en + 1)
  181.             xPos = xPos + en
  182.         elseif string:find("^%p") then
  183.             tabDraw = false
  184.             local _, en = string:find("^%p")
  185.             if en >= sStart and xPos <= sEnd then
  186.                 draw(string:sub(1, en), xPos, yPos, colTab[5], colors.black)
  187.             end
  188.             string = string:sub(en + 1)
  189.             xPos = xPos + en
  190.         elseif string:find("^[%a_]+") then
  191.             tabDraw = false
  192.             local _, en = string:find("^[%a_]+")
  193.             local test = string:sub(1, en)
  194.             local color = colors.white
  195.             if keywords[test] and mode == "edit" then
  196.                 color = keywords[test]
  197.             end
  198.             if multiComment then
  199.                 color = colors.green
  200.             end
  201.             if en >= sStart and xPos <= sEnd then
  202.                 draw(string:sub(1, en), xPos, yPos, color, colors.black)
  203.             end
  204.             string = string:sub(en + 1)
  205.             xPos = xPos + en
  206.         elseif string:find("^[^%w_]") then
  207.             tabDraw = false
  208.             local _, en = string:find("^[^%w_]")
  209.             if en >= sStart and xPos <= sEnd then
  210.                 draw(string:sub(1, en), xPos, yPos, colTab[6], colors.black)
  211.             end
  212.             string = string:sub(en + 1)
  213.             xPos = xPos + en
  214.         end
  215.     end
  216. end
  217.  
  218. local function printMenu()
  219.     term.setCursorPos(1, 1)
  220.     term.setBackgroundColor(colors.lightGray)
  221.     term.clearLine()
  222.     draw("Press CTRL for menu...", 1, 1, colors.gray, colors.lightGray)
  223. end
  224.  
  225. local function saveFile(codeTable, filename)
  226.     local tempFile = {}
  227.     for i, v in ipairs(codeTable) do
  228.         tempFile[i] = v:gsub("$___", "    ")
  229.     end
  230.     local sFile = fs.open(filename, "w")
  231.     for _, v in ipairs(tempFile) do
  232.         sFile.writeLine(v)
  233.     end
  234.     sFile.close()
  235. end
  236.  
  237. local function showProgram()
  238.     multiComment = false
  239.     numlength = #tostring(#program - location["y"] + 1 >= h and location["y"] + h - 1  or #program)
  240.     for y = location["y"], #program - location["y"] + 1 >= h - 1 and location["y"] + h - 2  or #program do
  241.         codeDraw(program[y], 1 + numlength, y - location["y"] + 2)
  242.     end
  243. end
  244.  
  245. local function showProgramLine(line)
  246.     numlength = #tostring(#program - location["y"] + 1 >= h and location["y"] + h - 1  or #program)
  247.     codeDraw(program[line], 1 + numlength, line - location["y"] + 2)
  248. end
  249.  
  250. local function showNumbers()
  251.     local activeLoops = {}
  252.     local up, down
  253.     for _, v in pairs(cTables) do
  254.         if currentLine >= v[1] and currentLine <= v[2] then
  255.             activeLoops[#activeLoops + 1] = {v[1], v[2]}
  256.         end
  257.     end
  258.     if #activeLoops > 0 then
  259.         local key, mini = 0, #program
  260.         for i, v in pairs(activeLoops) do
  261.             if v[2] - v[1] < mini then
  262.                 key, mini = i, v[2] - v[1]
  263.             end
  264.         end
  265.         if key > 0 then
  266.             up, down = activeLoops[key][1], activeLoops[key][2]
  267.         end
  268.     end
  269.     for y = location["y"], #program - location["y"] + 1 >= h - 1 and location["y"] + h - 2  or #program do
  270.         local text, back = colors.gray, colors.lightGray
  271.         if up and down and y > up and y < down then
  272.             text, back = colors.red, colors.pink
  273.         elseif up and down and (y == up or y == down) then
  274.             text, back = colors.pink, colors.red
  275.         end
  276.         draw(tostring(y), 1, y - location["y"] + 2, text, back)
  277.         if #tostring(y) < numlength then
  278.             write(" ")
  279.         end
  280.     end
  281. end
  282.  
  283. local function addLetter(char, str, pos)
  284.     return str:sub(0, pos - 1) .. tostring(char) .. str:sub(pos)
  285. end
  286.  
  287. local function removeLetter(str, pos)
  288.     return str:sub(0, pos) .. str:sub(pos + 2)
  289. end
  290.  
  291. local function fixCursorPos()
  292.     if position["y"] < 1 then
  293.         pChanged = true
  294.         location["y"] = location["y"] + position["y"] - 1
  295.         position["y"] = 1
  296.     elseif position["y"] > h - 1 then
  297.         pChanged = true
  298.         location["y"] = location["y"] + position["y"] - (h-1)
  299.         position["y"] = h - 1
  300.     end
  301.     if location["y"] < 1 then
  302.         pChanged = false
  303.         location["y"] = 1
  304.     elseif location["y"] + h - 1 > #program then
  305.         pChanged = false
  306.         location["y"] = #program - h + 1
  307.     end
  308.     local dollarNum
  309.     if program[location["y"] + position["y"] - 1]:sub(location["x"] + position["x"] - 1, location["x"] + position["x"] - 1) == "_" then
  310.         for i = 1, 3 do
  311.         if program[location["y"] + position["y"] - 1]:sub(location["x"] + position["x"] - 1 - i, location["x"] + position["x"] - 1 - i) ~= "_" then
  312.                 if program[location["y"] + position["y"] - 1]:sub(location["x"] + position["x"] - 1 - i, location["x"] + position["x"] - 1 - i) == "$" then
  313.                     dollarNum = i
  314.                     break
  315.                 end
  316.                 break
  317.             end
  318.         end
  319.         if dollarNum and dollarNum < 3 then
  320.             for i = 1, 3 - dollarNum do
  321.                 if program[location["y"] + position["y"] - 1]:sub(location["x"] + position["x"] - 1 + i, location["x"] + position["x"] - 1 + i) ~= "_" then
  322.                     dollarNum = nil
  323.                     break
  324.                 end
  325.             end
  326.         end
  327.         if dollarNum then
  328.             position["x"] = position["x"] - numlength - dollarNum + 2
  329.         end
  330.     end
  331.     if position["x"] + location["x"] - 1 > #program[position["y"] + location["y"] - 1] then
  332.         position["x"] = #program[position["y"] + location["y"] - 1] - location["x"] + 2
  333.     end
  334.     if position["x"] < 1 then
  335.         pChanged = true
  336.         location["x"] = location["x"] + position["x"] - 1
  337.         position["x"] = 1
  338.     elseif position["x"] > w - numlength then
  339.         pChanged = true
  340.         location["x"] = location["x"] + position["x"] - (w-numlength)
  341.         position["x"] = w - numlength
  342.     end
  343.     if location["x"] < 1 then
  344.         pChanged = false
  345.         location["x"] = 1
  346.     elseif location["x"] + w - numlength > #program then
  347.         pChanged = false
  348.         location["x"] = #program - w + numlength
  349.     end
  350.     if mode == "read" and location["x"] < 3 then
  351.         location["x"] = 3
  352.     elseif mode == "edit" and location["x"] < 1 then
  353.         location["x"] = 1
  354.     end
  355.     currentLine = location["y"] + position["y"] - 1
  356.     currentPos = position["x"] + location["x"] - 1
  357. end
  358.  
  359. term.setCursorBlink(true)
  360. while true do
  361.     if mode == "edit" then
  362.         numlength = #tostring(#program - location["y"] + 1 >= h and location["y"] + h - 1  or #program)
  363.     elseif mode == "read" then
  364.         numlength = 0
  365.     end
  366.     fixCursorPos()
  367.     --cComTab = checkMultiComments()
  368.     if pChanged or program[currentLine]:sub(currentPos - 2, currentPos - 1) == "]]" then
  369.         --cTables = checkConditionals()
  370.         term.setBackgroundColor(colors.black)
  371.         term.clear()
  372.         showProgram()
  373.         pChanged = false
  374.         lChanged = false
  375.     elseif lChanged then
  376.         --cTables = checkConditionals()
  377.         term.setBackgroundColor(colors.black)
  378.         term.clearLine()
  379.         showProgramLine(currentLine)
  380.         lChanged = false
  381.     end
  382.     if mChanged then
  383.         printMenu()
  384.         mChanged = false
  385.     end
  386.     --[[if prevline then
  387.         showProgramLine(prevline[1])
  388.         showProgramLine(prevline[2])
  389.         prevline = nil
  390.     end
  391.     if mode == "edit" then
  392.         cParTab = checkParenthesis()
  393.     end
  394.     for i, v in pairs(cParTab) do
  395.         if (currentPos == cParTab[i][1] and currentLine == cParTab[i][2]) or (currentPos == cParTab[i][3] and currentLine == cParTab[i][4]) then
  396.             draw(cParTab[i][5], cParTab[i][1] + numlength - location["x"] + 1, cParTab[i][2] - location["y"] + 2, colors.blue, colors.lightBlue)
  397.             draw(cParTab[i][6], cParTab[i][3] + numlength - location["x"] + 1, cParTab[i][4] - location["y"] + 2, colors.blue, colors.lightBlue)
  398.             prevline = {cParTab[i][2], cParTab[i][4]}
  399.         end
  400.     end]]
  401.     if mode == "edit" then
  402.         showNumbers()
  403.     end
  404.     term.setTextColor(colors.white)
  405.     term.setCursorPos(position["x"] + numlength, position["y"] + 1)
  406.     local events = {os.pullEvent()}
  407.     if events[1] == "char" and mode == "edit" then
  408.         lChanged = true
  409.         program[currentLine] = addLetter(events[2], program[currentLine], position["x"] + location["x"] - 1)
  410.         position["x"] = position["x"] + 1
  411.     elseif events[1] == "key" then
  412.         if events[2] == keys.up then   --^
  413.                 position["y"] = position["y"] - 1
  414.         elseif events[2] == keys.down then   --v
  415.                 position["y"] = position["y"] + 1
  416.         elseif events[2] == keys.right then   -->
  417.             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
  418.                 position["x"] = position["x"] + 4
  419.             elseif currentLine < #program and currentPos > #program[currentLine] then
  420.                 position["x"] = 1 - location["x"] + 1
  421.                 position["y"] = position["y"] + 1
  422.             else
  423.                 position["x"] = position["x"] + 1
  424.             end
  425.         elseif events[2] == keys.left then   --<
  426.             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
  427.                 position["x"] = position["x"] - 4
  428.             elseif currentLine > 1 and currentPos == 1 then
  429.                 position["x"] = #program[currentLine - 1] + 1
  430.                 position["y"] = position["y"] - 1
  431.             else
  432.                 position["x"] = position["x"] - 1
  433.             end
  434.         end
  435.         if events[2] == keys.backspace and mode == "edit" then
  436.             lChanged = true
  437.             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
  438.                 for i = 1, 4 do
  439.                     program[currentLine] = removeLetter(program[currentLine], location["x"] + position["x"] - 6)
  440.                     position["x"] = position["x"] - 4
  441.                 end
  442.             elseif currentLine > 1 and currentPos == 1 then
  443.                 position["y"] = position["y"] - 1
  444.                 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"]
  445.                 position["x"] = #program[currentLine - 1] - location["x"] + 2
  446.                 program[currentLine - 1] = program[currentLine - 1] .. (table.remove(program, currentLine))
  447.                 pChanged = true
  448.                         else
  449.                 program[currentLine] = removeLetter(program[currentLine], currentPos - 2)
  450.                 position["x"] = position["x"] - 1
  451.             end
  452.         end
  453.         if events[2] == keys.enter and mode == "edit" then
  454.             pChanged = true
  455.             table.insert(program, currentLine + 1, program[currentLine]:sub(position["x"] + location["x"] - 1))
  456.             program[currentLine] = program[currentLine]:sub(1, position["x"] + location["x"] - 2)
  457.             program[currentLine + 1] = string.rep("$___", tabNum) .. program[currentLine + 1]
  458.             position["y"] = position["y"] + 1
  459.             position["x"] = 1 + tabNum * 4
  460.         end
  461.         if events[2] == keys.tab and mode == "edit" then
  462.             lChanged = true
  463.             if (position["x"] + location["x"] - 1) % 4 == 1 then
  464.                 program[currentLine] = addLetter("$___", program[currentLine], position["x"] + location["x"] - 1)
  465.             else
  466.                 local num = (position["x"] + location["x"] - 1) % 4 + 1
  467.                 if num == 4 then num = 2 end
  468.                 program[currentLine] = addLetter(string.rep(" ", num), program[currentLine], position["x"] + location["x"] - 1)
  469.             end
  470.             position["x"] = position["x"] + 4
  471.         end
  472.     end
  473.     if events[1] == "mouse_click" and events[2] == 1 and #program >= events[4] + location["y"] - 2 and events[4] > 1 then
  474.         position["y"] = events[4] - 1
  475.         position["x"] = events[3] - numlength
  476.     elseif events[1] == "mouse_drag" and events[2] == 1 then
  477.         clickEnd = {events[3] + location["x"] - 1, events[4] + location["y"] - 1}
  478.     end
  479.     if events[1] == "mouse_scroll" then
  480.         pChanged = true
  481.         location["y"] = location["y"] + events[2]
  482.     end
  483. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement