kd2bwzgen

editasm w/ highlighting on old CC

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