Advertisement
billysback

CCEdit

Nov 12th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.09 KB | None | 0 0
  1. local cfname = "NEW"
  2. local title = "\\___/ File: "..cfname
  3.  
  4. local clipboard = {}
  5.  
  6. local lastname = nil
  7. local lastlines = nil
  8.  
  9. local lines = {" "}
  10. local firstpoint = nil
  11. local lastpoint = nil
  12.  
  13. local cursor = {1, 1}
  14. local blink = true
  15.  
  16. local scroll = {1, 1}
  17.  
  18. local curtab = " "
  19.  
  20. local function drawMenu()
  21.     local menu = {"|    \\__",
  22.                   "| SAVE  |",
  23.                   "| NEW   |",
  24.                   "| LOAD  |",
  25.                   "| PRINT |",
  26.                   "+-------+"}
  27.                  
  28.     for y=1,#menu do
  29.         local line = menu[y]
  30.         term.setCursorPos(1, y+1)
  31.         term.write(line)
  32.     end
  33. end
  34.  
  35. local function drawLines()
  36.     local oldx, oldy = term.getCursorPos()
  37.     for y=scroll[2],#lines do
  38.         local line = lines[y]
  39.         for x=scroll[1],string.len(line) do
  40.             local ch = string.sub(line, x, x)
  41.             local color = colors.white
  42.             if firstpoint ~= nil then
  43.                 if firstpoint[2] < y and lastpoint[2] > y then color = colors.yellow end
  44.                 if firstpoint[2] == y and firstpoint[1] <= x then color = colors.yellow end
  45.                 if lastpoint[2] == y and lastpoint[1] >= x then color = colors.yellow end
  46.             end
  47.             term.setCursorPos((x)-scroll[1]+1, (y+2)-scroll[2])
  48.             term.setTextColor(color)
  49.             term.write(ch)
  50.             term.setTextColor(colors.white)
  51.         end
  52.     end
  53.     term.setCursorPos(cursor[1]-scroll[1]+1, ((cursor[2])+2)-scroll[2])
  54.     if blink then term.write("_") end
  55.     term.setCursorPos(oldx, oldy)
  56. end
  57.  
  58. local function type(ch)
  59.     if firstpoint == nil then
  60.         local line = lines[cursor[2]]
  61.         local bef = string.sub(line, 1, cursor[1]-1)
  62.         local aft = string.sub(line, cursor[1], -1)
  63.         local nline = ""
  64.         if bef ~= nil then nline = bef end
  65.         nline = nline..ch
  66.         if aft ~= nil then nline = nline..aft end
  67.         lines[cursor[2]] = nline
  68.         cursor[1] = cursor[1] + 1
  69.     else
  70.         local lns = {}
  71.         for i=1,#lines do
  72.             local line = lines[i]
  73.             if i == firstpoint[2] then
  74.                 lns[#lns + 1] = string.sub(line, firstpoint[1], -1)..ch
  75.             elseif i == lastpoint[2] then
  76.                 lns[#lns + 1] = string.sub(line, 1, lastpoint[1])
  77.             elseif i > firstpoint[2] and i < lastpoint[2] then
  78.                
  79.             else
  80.                 lns[#lns + 1] = line
  81.             end
  82.         end
  83.         lines = lns
  84.         firstpoint = nil
  85.         lastpoint = nil
  86.         cursor[1] = cursor[1] + 1
  87.     end
  88. end
  89.  
  90. local function backspace()
  91.     if firstpoint == nil then
  92.         local line = lines[cursor[2]]
  93.         if line == "" or line == nil then
  94.             local lns = {}
  95.             for i=1,#lines do
  96.                 local line = lines[i]
  97.                 if i ~= cursor[2] then
  98.                     lns[#lns + 1] = line
  99.                 end
  100.             end
  101.             cursor[2] = cursor[2] - 1
  102.         else
  103.             local bef = string.sub(line, 1, cursor[1]-1)
  104.             local aft = string.sub(line, cursor[1]+1, -1)
  105.             local nline = ""
  106.             if bef ~= nil then nline = bef end
  107.             if aft ~= nil then nline = nline..aft end
  108.             lines[cursor[2]] = nline
  109.             cursor[1] = cursor[1] - 1
  110.         end
  111.     else
  112.         local lns = {}
  113.         for i=1,#lines do
  114.             local line = lines[i]
  115.             if i == firstpoint[2] then
  116.                 lns[#lns + 1] = string.sub(line, firstpoint[1], -1)
  117.             elseif i == lastpoint[2] then
  118.                 lns[#lns + 1] = string.sub(line, 1, lastpoint[1])
  119.             elseif i > firstpoint[2] and i < lastpoint[2] then
  120.                
  121.             else
  122.                 lns[#lns + 1] = line
  123.             end
  124.         end
  125.         lines = lns
  126.         firstpoint = nil
  127.         lastpoint = nil
  128.         cursor[1] = cursor[1] - 1
  129.     end
  130. end
  131.  
  132. local tabamt = "    "
  133. local function tab()
  134.     type(tabamt)
  135.     curtab = curtab..tabamt
  136. end
  137.  
  138. local function untab()
  139.     local line = lines[cursor[2]]
  140.     local sub = string.sub(line, cursor[1]-(string.len(tabat)), cursor[1])
  141.     if sub ~= nil then
  142.         if string.gsub(sub, " ", "") == "" then
  143.             local bef = string.sub(line, 1, cursor[1]-(string.len(tabat)-1))
  144.             local aft = string.sub(line, cursor[1], -1)
  145.             local nline = ""
  146.             if bef ~= nil then nline = bef end
  147.             if aft ~= nil then nline = nline..aft end
  148.             lines[cursor[2]] = nline
  149.             if string.len(curtab) > string.len(tabamt) then
  150.                 curtab = string.sub(curtab, 1, string.len(curtab)-string.len(tabamt))
  151.             end
  152.             if curtab == "" then curtab = " " end
  153.         end
  154.     end
  155. end
  156.  
  157. local function enter()
  158.     local lns = {}
  159.     local done = false
  160.     if firstpoint == nil then
  161.         for i=1,#lines do
  162.             local line = lines[i]
  163.             lns[#lns + 1] = line
  164.             if i == cursor[2] and not done then
  165.                 lns[#lns + 1] = curtab
  166.                 cursor = {string.len(curtab), i+1}
  167.                 done = true
  168.             end
  169.         end
  170.     else
  171.         for i=1,#lines do
  172.             local line = lines[i]
  173.             if i == firstpoint[2] then
  174.                 lns[#lns + 1] = string.sub(line, 1, firstpoint[1])
  175.                 local nstr = " "
  176.                 local sub = string.sub(line, firstpoint[1], -1)
  177.                 if sub ~= nil and sub ~= "" then nstr = sub end
  178.                 lns[#lns + 1] = curtab
  179.                 cursor = {string.len(curtab), i+1}
  180.             elseif i == lastpoint[2] then
  181.                 lns[#lns + 1] = string.sub(line, 1, lastpoint[1])
  182.             elseif i > firstpoint[2] and i < lastpoint[2] then
  183.                
  184.             else
  185.                 lns[#lns + 1] = line
  186.             end
  187.         end
  188.         firstpoint = nil
  189.         lastpoint = nil
  190.     end
  191.     lines = lns
  192. end
  193.  
  194. local function copy()
  195.     local lns = {}
  196.     for y=firstpoint[2],lastpoint[2] do
  197.         local line = lines[y]
  198.         if y == firstpoint[2] then
  199.             line = string.sub(line, firstpoint[1], -1)
  200.         elseif y == lastpoint[2] then
  201.             line = string.sub(line, 1, lastpoint[1])
  202.         else
  203.             lns[#lns + 1] = line
  204.         end
  205.     end
  206.     clipboard = lns
  207. end
  208.  
  209. local function paste()
  210.     local lns = {}
  211.     for y=1,#lines do
  212.         local line = lines[y]
  213.         if y == cursor[2] then
  214.             for i=1,#clipboard do
  215.                 if i == 1 then
  216.                     line = line..clipboard[i]
  217.                 else
  218.                     line = clipboard[i]
  219.                 end
  220.                 lns[#lns + 1] = line
  221.             end
  222.         else
  223.             lns[#lns + 1] = line
  224.         end
  225.     end
  226.     lastlines = lines
  227.     lines = lns
  228. end
  229.  
  230. local function undo()
  231.     if lastlines ~= nil then
  232.         local templines = lines
  233.         lines = lastlines
  234.         lastlines = templines
  235.         if lastname ~= nil then
  236.             cfname = lastname
  237.             title = string.sub(title, 1, 5).." File: "..cfname
  238.             lastname = nil
  239.         end
  240.     end
  241. end
  242.  
  243. local timer = os.startTimer(0)
  244. local interval = 0.1
  245.  
  246. local function save()
  247.     term.setCursorPos(1, 1)
  248.     local line = ""
  249.     local mx, my = term.getSize()
  250.     for i=1,mx do line = line.." " end
  251.     term.write(line)
  252.     term.setCursorPos(1, 1)
  253.     local msg = "Enter Dir: "
  254.     term.write(msg)
  255.     term.setCursorPos(string.len(msg), 1)
  256.     local input = read()
  257.     while input == "" or input == nil do
  258.         term.setCursorPos(1, 1)
  259.         term.write(line)
  260.         term.setCursorPos(1, 1)
  261.         term.write(msg)
  262.         term.setCursorPos(string.len(msg), 1)
  263.         input = read()
  264.     end
  265.     if string.gsub(input, " ", "") == "" then else
  266.         if fs.exists(input) then fs.delete(input) end
  267.         local file = fs.open(input, "a")
  268.         for i=1,#lines do
  269.             local line = lines[i]
  270.             file.writeLine(line)
  271.         end
  272.         file.close()
  273.         cfname = input
  274.         title = string.sub(title, 1, 5).." File: "..cfname
  275.     end
  276.     timer = os.startTimer(interval)
  277.     title = "\\___/ File: "..cfname
  278. end
  279.  
  280. local function load()
  281.     term.setCursorPos(1, 1)
  282.     local line = ""
  283.     local mx, my = term.getSize()
  284.     for i=1,mx do line = line.." " end
  285.     term.write(line)
  286.     term.setCursorPos(1, 1)
  287.     local msg = "Enter Dir: "
  288.     term.write(msg)
  289.     term.setCursorPos(string.len(msg), 1)
  290.     local input = read()
  291.     while input == "" or input == nil do
  292.         term.setCursorPos(1, 1)
  293.         term.write(line)
  294.         term.setCursorPos(1, 1)
  295.         term.write(msg)
  296.         term.setCursorPos(string.len(msg), 1)
  297.         input = read()
  298.     end
  299.     if string.gsub(input, " ", "") == "" then else
  300.         if fs.exists(input) then
  301.             local file = fs.open(input, "r")
  302.             local ln = file.readLine()
  303.             local lns = {}
  304.             while ln ~= nil do
  305.                 lns[#lns + 1] = ln
  306.                 ln = file.readLine()
  307.             end
  308.             file.close()
  309.             lines = lns
  310.             cfname = input
  311.             title = string.sub(title, 1, 5).." File: "..cfname
  312.         end
  313.     end
  314.     timer = os.startTimer(interval)
  315.     title = "\\___/ File: "..cfname
  316. end
  317.  
  318. local function new()
  319.     title = "\\___/ File: NEW"
  320.  
  321.     clipboard = {}
  322.  
  323.     lastname = cfname
  324.     lastlines = lines
  325.  
  326.     cfname = "NEW"
  327.    
  328.     lines = {" "}
  329.     firstpoint = nil
  330.     lastpoint = nil
  331.  
  332.     cursor = {1, 1}
  333.     blink = true
  334. end
  335.  
  336. local function round (x)
  337.   if x >= 0 then
  338.     return math.floor (x + 0.5)
  339.   end  -- if positive
  340.  
  341.   return math.ceil (x - 0.5)
  342. end
  343.  
  344. local function print()
  345.     term.setCursorPos(1, 1)
  346.     local line = ""
  347.     local mx, my = term.getSize()
  348.     for i=1,mx do line = line.." " end
  349.     term.write(line)
  350.     term.setCursorPos(1, 1)
  351.     local msg = "Enter title: "
  352.     term.write(msg)
  353.     term.setCursorPos(string.len(msg), 1)
  354.     while input == "" or input == nil do
  355.         term.setCursorPos(1, 1)
  356.         term.write(line)
  357.         term.setCursorPos(1, 1)
  358.         term.write(msg)
  359.         term.setCursorPos(string.len(msg), 1)
  360.         input = read()
  361.     end
  362.    
  363.     tSides = {"left","right","bottom","top","front","back"}
  364.     local printer = nil
  365.     for i=1,#tSides do
  366.         local side = tSides[i]
  367.         if peripheral.getType(side) == "printer" and printer == nil then
  368.             printer = peripheral.wrap(side)
  369.         end
  370.     end
  371.     if printer ~= nil then
  372.         printer.newPage()
  373.         local px, py = printer.getPageSize()
  374.         local pages = round(#lines / py)
  375.         if printer.getInkLevel() >= pages then
  376.             if printer.getPaperLevel() >=  pages then
  377.                 local cur = 0
  378.                 for i=1,#lines do
  379.                     cur = cur + 1
  380.                     if cur > py then
  381.                         printer.endPage()
  382.                         printer.newPage()
  383.                         printer.setPageTitle(input)
  384.                     end
  385.                     local line = lines[i]
  386.                     while string.len(line) > px do
  387.                         local ln = string.sub(line, 1, px)
  388.                         printer.write(ln)
  389.                         cur = cur + 1
  390.                         if cur > py then
  391.                             printer.endPage()
  392.                             printer.newPage()
  393.                             printer.setPageTitle(input)
  394.                         end
  395.                         line = string.sub(line, px+1, -1)
  396.                         if line == nil then line = "" end
  397.                     end
  398.                     printer.write(line)
  399.                 end
  400.                 printer.endPage()
  401.             end
  402.         end
  403.     end
  404.     timer = os.startTimer(interval)
  405.     title = "\\___/ File: "..input
  406. end
  407.  
  408. local function checkCurs()
  409.  
  410.     local mx, my = term.getSize()
  411.  
  412.     if cursor[1] < scroll[1] then scroll[1] = scroll[1] - 1 end
  413.     if cursor[1] > scroll[1]+mx then scroll[1] = scroll[1] + 1 end
  414.     if cursor[2] < scroll[2] then scroll[2] = scroll[2] - 1 end
  415.     if cursor[2] > scroll[2]+my then scroll[2] = scroll[2] + 1 end
  416.    
  417.     if cursor[2] < 1 then cursor[2] = 1 end
  418.     if cursor[2] > #lines then cursor[2] = #lines end
  419.     if cursor[1] < 1 then cursor[1] = 1 end
  420.     if cursor[1] > string.len(lines[cursor[2]]) then cursor[1] = string.len(lines[cursor[2]]) end
  421.    
  422.     if scroll[1] < 1 then scroll[1] = 1 end
  423.     if scroll[1] > cursor[1] then scroll[1] = cursor[1] end
  424.     if scroll[2] < 1 then scroll[2] = 1 end
  425.     if scroll[2] > #lines then scroll[2] = #lines end
  426.    
  427. end
  428.  
  429. local showtab = false
  430.  
  431. local on = true
  432.  
  433. local dochar = true
  434.  
  435. local ticks = 0
  436. while on do
  437.     local event, p1, p2, p3 = os.pullEvent()
  438.     if event == "timer" and p1 == timer then
  439.         checkCurs()
  440.         term.clear()
  441.         term.setCursorPos(1, 1)
  442.         term.write(title)
  443.         drawLines()
  444.         if showtab then
  445.             drawMenu()
  446.         end
  447.         ticks = ticks + 1
  448.         if ticks >= 3 then
  449.             if blink then blink = false else blink = true end
  450.             ticks = 0
  451.         end
  452.         timer = os.startTimer(interval)
  453.     elseif event == "key" then
  454.         local key = p1
  455.         if key == 29 then
  456.             on = false
  457.         elseif key == 15 then
  458.             tab()
  459.         elseif key == 41 then
  460.             untab()
  461.         elseif key == 54 then
  462.             dochar = false
  463.         elseif key == 28 then
  464.             enter()
  465.         elseif key == 14 then
  466.             backspace()
  467.         elseif key == 157 then
  468.             if showtab then
  469.                 showtab = false
  470.                 title = "\\___/"..string.sub(title, 6, -1)
  471.             else
  472.                 showtab = true
  473.                 title = " ____"..string.sub(title, 6, -1)
  474.             end
  475.         elseif key == 200 then --up
  476.             local x = string.len(lines[cursor[2]])
  477.             cursor[2] = cursor[2] - 1
  478.             checkCurs()
  479.             if x > string.len(lines[cursor[2]]) then x = string.len(lines[cursor[2]]) end
  480.             cursor[1] = x
  481.         elseif key == 203 then --left
  482.             cursor[1] = cursor[1] - 1
  483.         elseif key == 208 then --down
  484.             local x = string.len(lines[cursor[2]])
  485.             cursor[2] = cursor[2] + 1
  486.             checkCurs()
  487.             if x > string.len(lines[cursor[2]]) then x = string.len(lines[cursor[2]]) end
  488.             cursor[1] = x
  489.         elseif key == 205 then --right
  490.             cursor[1] = cursor[1] + 1
  491.         end
  492.     elseif event == "char" then
  493.         local char = p1
  494.         if dochar then
  495.             type(char)
  496.         elseif not showtab then
  497.             char = string.lower(char)
  498.             if char == "c" then copy() end
  499.             if char == "v" then paste() end
  500.             if char == "p" then print() end
  501.             if char == "s" then save() end
  502.             if char == "l" then load() end
  503.             if char == "n" then new() end
  504.             if char == "y" or char == "u" then undo() end
  505.         end
  506.         dochar = true
  507.     elseif event == "mouse_click" then
  508.         local button = p1
  509.         local y = p3
  510.         local x = p2
  511.         if y == 1 and x >= 1 and x <= 5 then
  512.             if showtab then
  513.                 showtab = false
  514.                 title = "\\___/"..string.sub(title, 6, -1)
  515.             else
  516.                 showtab = true
  517.                 title = " ____"..string.sub(title, 6, -1)
  518.             end
  519.         elseif showtab then
  520.             local cy = y - 2
  521.             if cy == 1 then --save
  522.                 save()
  523.                 showtab = false
  524.             elseif cy == 2 then --new
  525.                 new()
  526.                 showtab = false
  527.             elseif cy == 3 then --load
  528.                 load()
  529.                 showtab = false
  530.             elseif cy == 4 then --print
  531.                 print()
  532.                 showtab = false
  533.             end
  534.         else
  535.             if button == 2 then
  536.                 firstpoint = {cursor[1], cursor[2]}
  537.                 lastpoint = {x, y}
  538.             elseif button == 3 then
  539.                 firstpoint = nil
  540.                 lastpoint = nil
  541.             else
  542.                 cursor[1] = x
  543.                 cursor[2] = y
  544.             end
  545.         end
  546.     elseif event == "mouse_drag" then
  547.         local x = p1
  548.         local y = p2
  549.         local firstpoint = {cursor[1], cursor[2]}
  550.         local lastpoint = {x, y}
  551.     end
  552. end
  553.  
  554. term.clear()
  555. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement