Advertisement
Redxone

[CC - 6502] - Edit with Syntax Highlighting

Mar 30th, 2017
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.58 KB | None | 0 0
  1. -- Edit template by: Dan200
  2. -- ASM Rework by: Lewisk3 (Redxone)
  3.  
  4. -- Get file to edit
  5. local tArgs = { ... }
  6. if #tArgs == 0 then
  7.     print( "Usage: "..shell.resolve(shell.getRunningProgram()).." <path>" )
  8.     return
  9. end
  10. -- If the PLEB has an old version of CC
  11. if(settings == nil)then
  12.     settings = {}
  13.     settings.get = function(stuff) return false end
  14. end
  15.  
  16. -- Error checking
  17. local sPath = shell.resolve( tArgs[1] )
  18. local bReadOnly = fs.isReadOnly( sPath )
  19. if fs.exists( sPath ) and fs.isDir( sPath ) then
  20.     print( "Cannot edit a directory." )
  21.     return
  22. end
  23.  
  24. local x,y = 1,1
  25. local w,h = term.getSize()
  26. local scrollX, scrollY = 0,0
  27. local CompilerLocation = ""
  28.  
  29. local tLines = {}
  30. local bRunning = true
  31.  
  32. -- Colours
  33. local highlightColour, keywordColour, commentColour, textColour, bgColour, stringColour, labelColour, highlightBack
  34. if term.isColour() then
  35.     bgColour = colours.grey
  36.     textColour = colours.white
  37.     highlightColour = colours.lightGrey
  38.     highlightBack = bgColour
  39.     keywordColour = colours.orange
  40.     commentColour = colours.blue
  41.     stringColour = colours.red
  42.     labelColour = colors.lime
  43.     beginColour = colors.purple
  44.     brakColour = colors.lightGray
  45.     accColour   = colors.yellow
  46. else
  47.     bgColour = colours.black
  48.     textColour = colours.white
  49.     highlightColour = colours.white
  50.     keywordColour = colours.white
  51.     commentColour = colours.white
  52.     stringColour = colours.white
  53.     labelColour = colors.white
  54.     beginColour = colors.white
  55.     accColour = colors.white
  56.     brakColour = colors.white
  57. end
  58.  
  59. -- Menus
  60. local bMenu = false
  61. local nMenuItem = 1
  62. local tMenuItems = {}
  63. if not bReadOnly then
  64.     table.insert( tMenuItems, "Save" )
  65. end
  66. if shell.openTab then
  67.     table.insert( tMenuItems, "Run" )
  68. end
  69. if peripheral.find( "printer" ) then
  70.     table.insert( tMenuItems, "Print" )
  71. end
  72. table.insert( tMenuItems, "Exit" )
  73.  
  74. local sStatus = "Press Ctrl to access menu"
  75. if string.len( sStatus ) > w - 5 then
  76.     sStatus = "Press Ctrl for menu"
  77. end
  78.  
  79. local function load( _sPath )
  80.     tLines = {}
  81.     if fs.exists( _sPath ) then
  82.         local file = io.open( _sPath, "r" )
  83.         local sLine = file:read()
  84.         while sLine do
  85.             table.insert( tLines, sLine )
  86.             sLine = file:read()
  87.         end
  88.         file:close()
  89.     end
  90.    
  91.     if #tLines == 0 then
  92.         table.insert( tLines, "" )
  93.     end
  94. end
  95.  
  96. local function save( _sPath )
  97.     -- Create intervening folder
  98.     local sDir = _sPath:sub(1, _sPath:len() - fs.getName(_sPath):len() )
  99.     if not fs.exists( sDir ) then
  100.         fs.makeDir( sDir )
  101.     end
  102.  
  103.     -- Save
  104.     local file = nil
  105.     local function innerSave()
  106.         file = fs.open( _sPath, "w" )
  107.         if file then
  108.             for n, sLine in ipairs( tLines ) do
  109.                 file.write( sLine .. "\n" )
  110.             end
  111.         else
  112.             error( "Failed to open ".._sPath )
  113.         end
  114.     end
  115.    
  116.     local ok, err = pcall( innerSave )
  117.     if file then
  118.         file.close()
  119.     end
  120.     return ok, err
  121. end
  122. local tKeylabel = {
  123.     [":"] = true,
  124. }
  125. local tKeywords = {
  126.     ["lda"] = true,
  127.     ["ldx"] = true,
  128.     ["ldy"] = true,
  129.     ["sta"] = true,
  130.     ["stx"] = true,
  131.     ["sty"] = true,
  132.     ["adc"] = true,
  133.     ["bcc"] = true,
  134.     ["bcs"] = true,
  135.     ["beq"] = true,
  136.     ["bit"] = true,
  137.     ["bmi"] = true,
  138.     ["bne"] = true,
  139.     ["bpl"] = true,
  140.     ["brk"] = true,
  141.     ["bvc"] = true,
  142.     ["clc"] = true,
  143.     ---------------
  144.     ["cld"] = true,
  145.     ["cli"] = true,
  146.     ["clv"] = true,
  147.     ["cmp"] = true,
  148.     ["cpx"] = true,
  149.     ["cpy"] = true,
  150.     ["dec"] = true,
  151.     ["dex"] = true,
  152.     ["dey"] = true,
  153.     ["eor"] = true,
  154.     ["inc"] = true,
  155.     ["inx"] = true,
  156.     ["iny"] = true,
  157.     ["jmp"] = true,
  158.     --------------
  159.     ["jsr"] = true,
  160.     ["lsr"] = true,
  161.     ["nop"] = true,
  162.     ["ora"] = true,
  163.     ["pha"] = true,
  164.     ["php"] = true,
  165.     ["pla"] = true,
  166.     ["plp"] = true,
  167.     ["rol"] = true,
  168.     ["ror"] = true,
  169.     ["rti"] = true,
  170.     --------------
  171.     ["rts"] = true,
  172.     ["sbc"] = true,
  173.     ["sec"] = true,
  174.     ["sed"] = true,
  175.     ["sei"] = true,
  176.     ["tax"] = true,
  177.     ["tay"] = true,
  178.     ["tsx"] = true,
  179.     ["txa"] = true,
  180.     ["txs"] = true,
  181.     ["tya"] = true,
  182.     --------------
  183.     ['dcb'] = true,
  184.     --------------
  185.     ["define"] = true,
  186. }
  187.  
  188. local function tryWrite( sLine, regex, colour )
  189.     local match = string.match( sLine, regex )
  190.     if match then
  191.         if type(colour) == "number" then
  192.             term.setTextColour( colour )
  193.         else
  194.             term.setTextColour( colour(match) )
  195.         end
  196.         term.write( match )
  197.         term.setTextColour( textColour )
  198.         return string.sub( sLine, string.len(match) + 1 )
  199.     end
  200.     return nil
  201. end
  202.  
  203. -- ACC highlighting is near impossible, so screw it.
  204. local function writeHighlighted( sLine )
  205.     while string.len(sLine) > 0 do 
  206.         sLine =
  207.             tryWrite( sLine, "^.begin+", beginColour ) or
  208.             tryWrite( sLine, "^%;%[%[.;%]%]", commentColour ) or
  209.             tryWrite( sLine, "^[%#.*]", stringColour ) or
  210.             tryWrite( sLine, "^[\$]...[0123456789abcefABCDEF]", stringColour ) or
  211.             tryWrite( sLine, "^[\$].[0123456789abcefABCDEF]", stringColour ) or
  212.             tryWrite( sLine, "^%#%$%", stringColour ) or
  213.             tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or
  214.             tryWrite( sLine, "^[\'].*[\']", stringColour ) or
  215.             tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or
  216.             tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or
  217.             tryWrite( sLine, "^%;.*", commentColour ) or
  218.             tryWrite( sLine, "^.*:", labelColour ) or
  219.             tryWrite( sLine, "^[\(]", brakColour ) or
  220.             tryWrite( sLine, "^[\)]", brakColour ) or
  221.             tryWrite( sLine, "^[%w_]+", function( match )
  222.                 if tKeywords[ string.lower(match) ] then
  223.                     return keywordColour
  224.                 end
  225.                 return textColour
  226.             end ) or
  227.             tryWrite( sLine, "^[^%w_]", textColour )
  228.     end
  229. end
  230.  
  231. local tCompletions
  232. local nCompletion
  233.  
  234. local tCompleteEnv = _ENV
  235. local function complete( sLine )
  236.     if settings.get and settings.get( "edit.autocomplete" ) then
  237.         local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.]+$" )
  238.         if nStartPos then
  239.             sLine = string.sub( sLine, nStartPos )
  240.         end
  241.         if #sLine > 0 then
  242.             return textutils.complete( sLine, tCompleteEnv )
  243.         end
  244.     end
  245.     return nil
  246. end
  247.  
  248. local function recomplete()
  249.     local sLine = tLines[y]
  250.     if not bMenu and not bReadOnly and x == string.len(sLine) + 1 then
  251.         tCompletions = complete( sLine )
  252.         if tCompletions and #tCompletions > 0 then
  253.             nCompletion = 1
  254.         else
  255.             nCompletion = nil
  256.         end
  257.     else
  258.         tCompletions = nil
  259.         nCompletion = nil
  260.     end
  261. end
  262.  
  263. local function writeCompletion( sLine )
  264.     if nCompletion then
  265.         local sCompletion = tCompletions[ nCompletion ]
  266.         term.setTextColor( colours.white )
  267.         term.setBackgroundColor( colours.grey )
  268.         term.write( sCompletion )
  269.         term.setTextColor( textColour )
  270.         term.setBackgroundColor( bgColour )
  271.     end
  272. end
  273.  
  274. local function redrawText()
  275.     local cursorX, cursorY = x, y
  276.     for y=1,h-1 do
  277.         term.setCursorPos( 1 - scrollX, y )
  278.         term.clearLine()
  279.  
  280.         local sLine = tLines[ y + scrollY ]
  281.         if sLine ~= nil then
  282.             writeHighlighted( sLine )
  283.             if cursorY == y and cursorX == #sLine + 1 then
  284.                 writeCompletion()
  285.             end
  286.         end
  287.     end
  288.     term.setCursorPos( x - scrollX, y - scrollY )
  289. end
  290.  
  291. local function redrawLine(_nY)
  292.     local sLine = tLines[_nY]
  293.     if sLine then
  294.         term.setCursorPos( 1 - scrollX, _nY - scrollY )
  295.         term.clearLine()
  296.         writeHighlighted( sLine )
  297.         if _nY == y and x == #sLine + 1 then
  298.             writeCompletion()
  299.         end
  300.         term.setCursorPos( x - scrollX, _nY - scrollY )
  301.     end
  302. end
  303.  
  304. local function redrawMenu()
  305.     -- Clear line
  306.     term.setCursorPos( 1, h )
  307.     term.clearLine()
  308.  
  309.     -- Draw line numbers
  310.     term.setCursorPos( w - string.len( "Ln "..y ) + 1, h )
  311.     term.setTextColour( highlightColour )
  312.     term.write( "Ln " )
  313.     term.setTextColour( textColour )
  314.     term.write( y )
  315.  
  316.     term.setCursorPos( 1, h )
  317.     if bMenu then
  318.         -- Draw menu
  319.         term.setTextColour( textColour )
  320.         for nItem,sItem in pairs( tMenuItems ) do
  321.             if nItem == nMenuItem then
  322.                 term.setTextColour( highlightColour )
  323.                 term.write( "[" )
  324.                 term.setTextColour( textColour )
  325.                 term.write( sItem )
  326.                 term.setTextColour( highlightColour )
  327.                 term.write( "]" )
  328.                 term.setTextColour( textColour )
  329.             else
  330.                 term.write( " "..sItem.." " )
  331.             end
  332.         end
  333.     else
  334.         -- Draw status
  335.         term.setTextColour( highlightColour )
  336.         term.setBackgroundColor( highlightBack )
  337.         term.write( sStatus )
  338.          term.setBackgroundColor( bgColour )
  339.         term.setTextColour( textColour )
  340.     end
  341.  
  342.     -- Reset cursor
  343.     term.setCursorPos( x - scrollX, y - scrollY )
  344. end
  345.  
  346. local tMenuFuncs = {
  347.     Save = function()
  348.         if bReadOnly then
  349.             sStatus = "Access denied"
  350.         else
  351.             local ok, err = save( sPath )
  352.             if ok then
  353.                 sStatus="Saved to "..sPath
  354.             else
  355.                 sStatus="Error saving to "..sPath
  356.             end
  357.         end
  358.         redrawMenu()
  359.     end,
  360.     Print = function()
  361.         local printer = peripheral.find( "printer" )
  362.         if not printer then
  363.             sStatus = "No printer attached"
  364.             return
  365.         end
  366.  
  367.         local nPage = 0
  368.         local sName = fs.getName( sPath )
  369.         if printer.getInkLevel() < 1 then
  370.             sStatus = "Printer out of ink"
  371.             return
  372.         elseif printer.getPaperLevel() < 1 then
  373.             sStatus = "Printer out of paper"
  374.             return
  375.         end
  376.  
  377.         local screenTerminal = term.current()
  378.         local printerTerminal = {
  379.             getCursorPos = printer.getCursorPos,
  380.             setCursorPos = printer.setCursorPos,
  381.             getSize = printer.getPageSize,
  382.             write = printer.write,
  383.         }
  384.         printerTerminal.scroll = function()
  385.             if nPage == 1 then
  386.                 printer.setPageTitle( sName.." (page "..nPage..")" )           
  387.             end
  388.            
  389.             while not printer.newPage() do
  390.                 if printer.getInkLevel() < 1 then
  391.                     sStatus = "Printer out of ink, please refill"
  392.                 elseif printer.getPaperLevel() < 1 then
  393.                     sStatus = "Printer out of paper, please refill"
  394.                 else
  395.                     sStatus = "Printer output tray full, please empty"
  396.                 end
  397.    
  398.                 term.redirect( screenTerminal )
  399.                 redrawMenu()
  400.                 term.redirect( printerTerminal )
  401.                
  402.                 local timer = os.startTimer(0.5)
  403.                 sleep(0.5)
  404.             end
  405.  
  406.             nPage = nPage + 1
  407.             if nPage == 1 then
  408.                 printer.setPageTitle( sName )
  409.             else
  410.                 printer.setPageTitle( sName.." (page "..nPage..")" )
  411.             end
  412.         end
  413.        
  414.         bMenu = false
  415.         term.redirect( printerTerminal )
  416.         local ok, error = pcall( function()
  417.             term.scroll()
  418.             for n, sLine in ipairs( tLines ) do
  419.                 print( sLine )
  420.             end
  421.         end )
  422.         term.redirect( screenTerminal )
  423.         if not ok then
  424.             print( error )
  425.         end
  426.        
  427.         while not printer.endPage() do
  428.             sStatus = "Printer output tray full, please empty"
  429.             redrawMenu()
  430.             sleep( 0.5 )
  431.         end
  432.         bMenu = true
  433.            
  434.         if nPage > 1 then
  435.             sStatus = "Printed "..nPage.." Pages"
  436.         else
  437.             sStatus = "Printed 1 Page"
  438.         end
  439.         redrawMenu()
  440.     end,
  441.     Exit = function()
  442.         bRunning = false
  443.     end,
  444.     Run = function()
  445.         term.setCursorPos(1, h)
  446.         term.clearLine()
  447.         sleep(0)
  448.         write("Run program? (Y/n*) ")
  449.         local _,ch = os.pullEvent("char")
  450.         ch = string.lower(ch)
  451.         if(ch ~= "y")then redrawMenu() return end
  452.         if(CompilerLocation == "")then
  453.             local w, h = term.getSize()
  454.             redrawMenu()
  455.             term.setCursorPos(1,h)
  456.             term.clearLine()
  457.             write("Enter location of compiler: ")
  458.             CompilerLocation = read()
  459.             if(not fs.exists(CompilerLocation))then
  460.                 term.setCursorPos(1,h)
  461.                 term.clearLine()
  462.                 write("File not found, press any key. ")
  463.                 os.pullEvent("key")
  464.                 redrawMenu()
  465.                 return
  466.             else
  467.                 os.loadAPI(CompilerLocation)
  468.                 if(not _G.ASM)then
  469.                     term.setCursorPos(1,h)
  470.                     term.clearLine()
  471.                     write("Compiler not valid, press any key.")
  472.                     os.pullEvent("key")
  473.                     redrawMenu()
  474.                     return
  475.                 end
  476.             end
  477.         end
  478.         local sTempPath = "/.temp"
  479.         local sTempASM  = "/.asmtemp"
  480.         local sCompile = CompilerLocation .. " -c " .. sTempPath .. " " .. sTempASM
  481.         local sRun =  CompilerLocation .. " -r " .. sTempASM
  482.         local ok, err = save( sTempPath )
  483.         redrawMenu()
  484.         term.setCursorPos(1, h)
  485.         term.clearLine()
  486.         if ok then
  487.             term.setCursorPos(1, h)
  488.             term.clearLine()
  489.             write("Press any key to compile.")
  490.             os.pullEvent("key")
  491.             redrawMenu()
  492.             term.setCursorPos(1, h)
  493.             term.clearLine()
  494.             sleep(0)
  495.             write("Run in Step mode? (Y/n*): ")
  496.             local _,ch = os.pullEvent("char")
  497.             ch = string.lower(ch)
  498.             local step = false
  499.             if(ch == "y")then sRun = sRun .. " -s" end
  500.             redrawMenu()
  501.             term.setCursorPos(1, h)
  502.             term.clearLine()
  503.             write("Press any key to run.")
  504.             local nTask = shell.openTab( sCompile )
  505.             if nTask then
  506.                     shell.switchTab( nTask )
  507.                     redrawMenu()
  508.                     term.setCursorPos(1, h)
  509.                     os.pullEvent("key")
  510.                     nTask = shell.openTab( sRun )
  511.                 if nTask then
  512.                     shell.switchTab( nTask )
  513.                     redrawMenu()
  514.                 else
  515.                     sStatus="Error running ASM"
  516.                 end
  517.             else
  518.                 sStatus="Error starting Task"
  519.             end
  520.             fs.delete( sTempPath )
  521.             fs.delete( sTempASM )
  522.         else
  523.             sStatus="Error saving to "..sTempPath
  524.         end
  525.         redrawMenu()
  526.     end
  527. }
  528.  
  529. local function doMenuItem( _n )
  530.     tMenuFuncs[tMenuItems[_n]]()
  531.     if bMenu then
  532.         bMenu = false
  533.         term.setCursorBlink( true )
  534.     end
  535.     redrawMenu()
  536. end
  537.  
  538. local function setCursor( newX, newY )
  539.     local oldX, oldY = x, y
  540.     x, y = newX, newY
  541.     local screenX = x - scrollX
  542.     local screenY = y - scrollY
  543.    
  544.     local bRedraw = false
  545.     if screenX < 1 then
  546.         scrollX = x - 1
  547.         screenX = 1
  548.         bRedraw = true
  549.     elseif screenX > w then
  550.         scrollX = x - w
  551.         screenX = w
  552.         bRedraw = true
  553.     end
  554.    
  555.     if screenY < 1 then
  556.         scrollY = y - 1
  557.         screenY = 1
  558.         bRedraw = true
  559.     elseif screenY > h-1 then
  560.         scrollY = y - (h-1)
  561.         screenY = h-1
  562.         bRedraw = true
  563.     end
  564.  
  565.     recomplete()
  566.     if bRedraw then
  567.         redrawText()
  568.     elseif y ~= oldY then
  569.         redrawLine( oldY )
  570.         redrawLine( y )
  571.     else
  572.         redrawLine( y )
  573.     end
  574.     term.setCursorPos( screenX, screenY )
  575.  
  576.     redrawMenu()
  577. end
  578.  
  579. -- Actual program functionality begins
  580. load(sPath)
  581.  
  582. term.setBackgroundColour( bgColour )
  583. term.clear()
  584. term.setCursorPos(x,y)
  585. term.setCursorBlink( true )
  586.  
  587. recomplete()
  588. redrawText()
  589. redrawMenu()
  590.  
  591. local function acceptCompletion()
  592.     if nCompletion then
  593.         -- Append the completion
  594.         local sCompletion = tCompletions[ nCompletion ]
  595.         tLines[y] = tLines[y] .. sCompletion
  596.         setCursor( x + string.len( sCompletion ), y )
  597.     end
  598. end
  599.  
  600. -- Handle input
  601. while bRunning do
  602.     local sEvent, param, param2, param3 = os.pullEvent()
  603.     if sEvent == "key" then
  604.         local oldX, oldY = x, y
  605.         if param == keys.up then
  606.             -- Up
  607.             if not bMenu then
  608.                 if nCompletion then
  609.                     -- Cycle completions
  610.                     nCompletion = nCompletion - 1
  611.                     if nCompletion < 1 then
  612.                         nCompletion = #tCompletions
  613.                     end
  614.                     redrawLine(y)
  615.  
  616.                 elseif y > 1 then
  617.                     -- Move cursor up
  618.                     setCursor(
  619.                         math.min( x, string.len( tLines[y - 1] ) + 1 ),
  620.                         y - 1
  621.                     )
  622.                 end
  623.             end
  624.  
  625.         elseif param == keys.down then
  626.             -- Down
  627.             if not bMenu then
  628.                 -- Move cursor down
  629.                 if nCompletion then
  630.                     -- Cycle completions
  631.                     nCompletion = nCompletion + 1
  632.                     if nCompletion > #tCompletions then
  633.                         nCompletion = 1
  634.                     end
  635.                     redrawLine(y)
  636.  
  637.                 elseif y < #tLines then
  638.                     -- Move cursor down
  639.                     setCursor(
  640.                         math.min( x, string.len( tLines[y + 1] ) + 1 ),
  641.                         y + 1
  642.                     )
  643.                 end
  644.             end
  645.  
  646.         elseif param == keys.tab then
  647.             -- Tab
  648.             if not bMenu and not bReadOnly then
  649.                 if nCompletion and x == string.len(tLines[y]) + 1 then
  650.                     -- Accept autocomplete
  651.                     acceptCompletion()
  652.                 else
  653.                     -- Indent line
  654.                     local sLine = tLines[y]
  655.                     tLines[y] = string.sub(sLine,1,x-1) .. "  " .. string.sub(sLine,x)
  656.                     setCursor( x + 2, y )
  657.                 end
  658.             end
  659.  
  660.         elseif param == keys.pageUp then
  661.             -- Page Up
  662.             if not bMenu then
  663.                 -- Move up a page
  664.                 local newY
  665.                 if y - (h - 1) >= 1 then
  666.                     newY = y - (h - 1)
  667.                 else
  668.                     newY = 1
  669.                 end
  670.                 setCursor(
  671.                     math.min( x, string.len( tLines[newY] ) + 1 ),
  672.                     newY
  673.                 )
  674.             end
  675.  
  676.         elseif param == keys.pageDown then
  677.             -- Page Down
  678.             if not bMenu then
  679.                 -- Move down a page
  680.                 local newY
  681.                 if y + (h - 1) <= #tLines then
  682.                     newY = y + (h - 1)
  683.                 else
  684.                     newY = #tLines
  685.                 end
  686.                 local newX = math.min( x, string.len( tLines[newY] ) + 1 )
  687.                 setCursor( newX, newY )
  688.             end
  689.  
  690.         elseif param == keys.home then
  691.             -- Home
  692.             if not bMenu then
  693.                 -- Move cursor to the beginning
  694.                 if x > 1 then
  695.                     setCursor(1,y)
  696.                 end
  697.             end
  698.  
  699.         elseif param == keys["end"] then
  700.             -- End
  701.             if not bMenu then
  702.                 -- Move cursor to the end
  703.                 local nLimit = string.len( tLines[y] ) + 1
  704.                 if x < nLimit then
  705.                     setCursor( nLimit, y )
  706.                 end
  707.             end
  708.  
  709.         elseif param == keys.left then
  710.             -- Left
  711.             if not bMenu then
  712.                 if x > 1 then
  713.                     -- Move cursor left
  714.                     setCursor( x - 1, y )
  715.                 elseif x==1 and y>1 then
  716.                     setCursor( string.len( tLines[y-1] ) + 1, y - 1 )
  717.                 end
  718.             else
  719.                 -- Move menu left
  720.                 nMenuItem = nMenuItem - 1
  721.                 if nMenuItem < 1 then
  722.                     nMenuItem = #tMenuItems
  723.                 end
  724.                 redrawMenu()
  725.             end
  726.  
  727.         elseif param == keys.right then
  728.             -- Right
  729.             if not bMenu then
  730.                 local nLimit = string.len( tLines[y] ) + 1
  731.                 if x < nLimit then
  732.                     -- Move cursor right
  733.                     setCursor( x + 1, y )
  734.                 elseif nCompletion and x == string.len(tLines[y]) + 1 then
  735.                     -- Accept autocomplete
  736.                     acceptCompletion()
  737.                 elseif x==nLimit and y<#tLines then
  738.                     -- Go to next line
  739.                     setCursor( 1, y + 1 )
  740.                 end
  741.             else
  742.                 -- Move menu right
  743.                 nMenuItem = nMenuItem + 1
  744.                 if nMenuItem > #tMenuItems then
  745.                     nMenuItem = 1
  746.                 end
  747.                 redrawMenu()
  748.             end
  749.  
  750.         elseif param == keys.delete then
  751.             -- Delete
  752.             if not bMenu and not bReadOnly then
  753.                 local nLimit = string.len( tLines[y] ) + 1
  754.                 if x < nLimit then
  755.                     local sLine = tLines[y]
  756.                     tLines[y] = string.sub(sLine,1,x-1) .. string.sub(sLine,x+1)
  757.                     recomplete()
  758.                     redrawLine(y)
  759.                 elseif y<#tLines then
  760.                     tLines[y] = tLines[y] .. tLines[y+1]
  761.                     table.remove( tLines, y+1 )
  762.                     recomplete()
  763.                     redrawText()
  764.                 end
  765.             end
  766.  
  767.         elseif param == keys.backspace then
  768.             -- Backspace
  769.             if not bMenu and not bReadOnly then
  770.                 if x > 1 then
  771.                     -- Remove character
  772.                     local sLine = tLines[y]
  773.                     tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
  774.                     setCursor( x - 1, y )
  775.                 elseif y > 1 then
  776.                     -- Remove newline
  777.                     local sPrevLen = string.len( tLines[y-1] )
  778.                     tLines[y-1] = tLines[y-1] .. tLines[y]
  779.                     table.remove( tLines, y )
  780.                     setCursor( sPrevLen + 1, y - 1 )
  781.                     redrawText()
  782.                 end
  783.             end
  784.  
  785.         elseif param == keys.enter then
  786.             -- Enter
  787.             if not bMenu and not bReadOnly then
  788.                 -- Newline
  789.                 local sLine = tLines[y]
  790.                 local _,spaces=string.find(sLine,"^[ ]+")
  791.                 if not spaces then
  792.                     spaces=0
  793.                 end
  794.                 tLines[y] = string.sub(sLine,1,x-1)
  795.                 table.insert( tLines, y+1, string.rep(' ',spaces)..string.sub(sLine,x) )
  796.                 setCursor( spaces + 1, y + 1 )
  797.                 redrawText()
  798.  
  799.             elseif bMenu then
  800.                 -- Menu selection
  801.                 doMenuItem( nMenuItem )
  802.  
  803.             end
  804.  
  805.         elseif param == keys.leftCtrl or param == keys.rightCtrl or param == keys.rightAlt then
  806.             -- Menu toggle
  807.             bMenu = not bMenu
  808.             if bMenu then
  809.                 term.setCursorBlink( false )
  810.             else
  811.                 term.setCursorBlink( true )
  812.             end
  813.             redrawMenu()
  814.  
  815.         end
  816.        
  817.     elseif sEvent == "char" then
  818.         if not bMenu and not bReadOnly then
  819.             -- Input text
  820.             local sLine = tLines[y]
  821.             tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  822.             setCursor( x + 1, y )
  823.  
  824.         elseif bMenu then
  825.             -- Select menu items
  826.             for n,sMenuItem in ipairs( tMenuItems ) do
  827.                 if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
  828.                     doMenuItem( n )
  829.                     break
  830.                 end
  831.             end
  832.         end
  833.  
  834.     elseif sEvent == "paste" then
  835.         if not bMenu and not bReadOnly then
  836.             -- Input text
  837.             local sLine = tLines[y]
  838.             tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  839.             setCursor( x + string.len( param ), y )
  840.         end
  841.        
  842.     elseif sEvent == "mouse_click" then
  843.         if not bMenu then
  844.             if param == 1 then
  845.                 -- Left click
  846.                 local cx,cy = param2, param3
  847.                 if cy < h then
  848.                     local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
  849.                     local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
  850.                     setCursor( newX, newY )
  851.                 end
  852.             end
  853.         end
  854.        
  855.     elseif sEvent == "mouse_scroll" then
  856.         if not bMenu then
  857.             if param == -1 then
  858.                 -- Scroll up
  859.                 if scrollY > 0 then
  860.                     -- Move cursor up
  861.                     scrollY = scrollY - 1
  862.                     redrawText()
  863.                 end
  864.            
  865.             elseif param == 1 then
  866.                 -- Scroll down
  867.                 local nMaxScroll = #tLines - (h-1)
  868.                 if scrollY < nMaxScroll then
  869.                     -- Move cursor down
  870.                     scrollY = scrollY + 1
  871.                     redrawText()
  872.                 end
  873.                
  874.             end
  875.         end
  876.  
  877.     elseif sEvent == "term_resize" then
  878.         w,h = term.getSize()
  879.         setCursor( x, y )
  880.         redrawMenu()
  881.         redrawText()
  882.  
  883.     end
  884. end
  885.  
  886. -- Cleanup
  887. term.setBackgroundColor(colors.black)
  888. term.clear()
  889. term.setCursorBlink( false )
  890. term.setCursorPos( 1, 1 )
  891. --print("Thanks for using ASM-Edit! (6502)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement