Advertisement
Guest User

Untitled

a guest
Jul 24th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 49.45 KB | None | 0 0
  1. --WARNING: THIS PROGRAM IS MADE BY JENS KOLBINGER!!!
  2. --         SEND ALL YOU WANT TO TELL ME ABOUT THIS PROGRAM TO jens.software@gmail.com
  3.  
  4. __mG={}    --my globals! Dont overwrite them!
  5. __mG.version="0.4.2"
  6. __mG.on={}
  7. __mG.WW=318    --WindowWidth
  8. __mG.WH=212    --WindowHeight
  9. __mG.maxChars=36
  10. __mG.maxLines=12
  11. __mG.chrS={25,2,8,15}    --1: posX of first character / 2: posY of first character / 3: width of each character / 4: height of each character
  12. __mG.nl=string.char(10)
  13. __mG.dialogue=false
  14. __mG.dialogueVar={}
  15. __mG.dialogueField=1
  16. __mG.skriptName=""
  17. --__mG.code={'chr=""',"function on.timer()","    chr=string.uchar(math.random(32,1000))","    platform.window:invalidate()","end","function on.paint(gc)","    gc:drawString('Funzt'..chr,50,50)","end","timer.start(0.1)"}
  18. __mG.code={""}
  19. __mG.scroll={1,1}
  20. __mG.coursor={1,1}
  21. __mG.selection={}
  22. __mG.mouseDown=false
  23. __mG.ERRline=nil
  24. __mG.ERR=nil
  25. __mG.font="serif"
  26. __mG.moveOK=true
  27. __mG.chrOffset={}
  28. __mG.colDsp=platform.isColorDisplay()
  29. __mG.calcLayout="touch"
  30. __mG.markSyntax=true
  31. __mG.message = class()
  32. __mG.state={}    --state[__mG.stateNo].before/after = {lineStart,lineEnd,{"line at lineStart",...,"line at lineEnd},{cursor position}}
  33. __mG.stateNo=0    --current position in state history (min 0, max 10)
  34.  
  35. function __mG.message:reset()
  36.     function __mG.message:action() end
  37.     __mG.message.show=false
  38.     __mG.message.selButton=1
  39.     __mG.text={}
  40. end
  41. __mG.message:reset()
  42.  
  43. function __mG.copy_table(t)
  44.     local t2 = {}
  45.     for k,v in pairs(t) do
  46.         if type(v)=="table" then v=__mG.copy_table(v) end
  47.         t2[k] = v
  48.     end
  49.     return t2
  50. end
  51.  
  52. __mG.fileList={}    --Table that indicates if a file with name n already exists: __mG.fileList["test"]=true if the file "test" exists
  53. __mG.repChr={}    --replace character(s)
  54.  
  55. __mG.repChr["touch"]={}
  56.  
  57. --basic universial functions
  58. __mG.repChr["touch"]["−"]=":"    --key [(-)]
  59. __mG.repChr["touch"]["≥"]=">="
  60. __mG.repChr["touch"]["≤"]="<="
  61. __mG.repChr["touch"]["≠"]="~="
  62. __mG.repChr["touch"]["√"]="math.sqrt("
  63. for i, name in pairs({"sin(","cos(","tan("}) do __mG.repChr["touch"][name]="math."..name end
  64. __mG.repChr["touch"]["sin("]="math.asin("
  65. __mG.repChr["touch"]["cos("]="math.acos("
  66. __mG.repChr["touch"]["tan("]="math.atan("
  67. __mG.repChr["touch"]["π"]="math.pi"
  68.  
  69. --special functions for clickpad
  70. __mG.repChr["click"]=__mG.copy_table(__mG.repChr["touch"])
  71. __mG.repChr["click"]["cos("]="["
  72. __mG.repChr["click"]["tan("]="]"
  73. __mG.repChr["click"]["cos("]="{"
  74. __mG.repChr["click"]["tan("]="}"
  75. __mG.repChr["click"]["exp("]="math.exp("
  76. __mG.repChr["click"]["ln("]="math.log10("
  77. __mG.repChr["click"]["log("]="math.log("
  78. __mG.repChr["click"][""]="~"    --that i button below the > button
  79.  
  80. --special functions for touchpad
  81. __mG.repChr["touch"]["exp("]="["
  82. __mG.repChr["touch"]["10^("]="]"
  83. __mG.repChr["touch"]["ln("]="{"
  84. __mG.repChr["touch"]["log("]="}"
  85.  
  86. __mG.wordRGB={}
  87. __mG.wordRGB["--"]={128,128,128}
  88. for i, word in pairs({"'",'"',"[[","]]","nil","true","false"}) do
  89.     if __mG.colDsp then
  90.         __mG.wordRGB[word]={128,0,128}
  91.     else
  92.         __mG.wordRGB[word]={110,110,110}
  93.     end
  94. end
  95. for i, word in pairs({"function","return","end","for","in","do","if","then","else","elseif","while","and","or","not","local"}) do
  96.     if __mG.colDsp then
  97.         __mG.wordRGB[word]={0,0,210}
  98.     else
  99.         __mG.wordRGB[word]={80,80,80}
  100.     end
  101. end
  102. for i, word in pairs({"assert","collectgarbage","error","_G","getfenv","getmetatable","ipairs","load","loadstring","next","pairs","pcall","print","rawequal","rawget","rawset","select","setfenv","setmetatable","tonumber","tostring","type","unpack","_VERSION","xpcall","require"}) do
  103.     if __mG.colDsp then
  104.         __mG.wordRGB[word]={255,0,255}
  105.     else
  106.         __mG.wordRGB[word]={170,170,170}
  107.     end
  108. end
  109.  
  110. function __mG.createMenu()
  111.     __mG.fileList={}    
  112.     local loadMenu={"Load"}
  113.     local files=var.recall("JSEFiles")    --List of all saved files as string
  114.     if not files or files=="" then    
  115.         __mG.fileList={}
  116.     else
  117.         files=files:split("§")    --list as string to table
  118.         for i, fileName in pairs(files) do
  119.             if fileName and fileName~="" then
  120.                 __mG.fileList[fileName]=true
  121.                 table.insert(loadMenu,{fileName,__mG_translateMenu})
  122.             end
  123.         end
  124.     end
  125.     __mG.menu = {
  126.         {"Code",
  127.             {"Run", __mG_translateMenu},
  128.             {"Undo   (ctrl+del)", __mG_translateMenu},
  129.             {"Redo   (ctrl+menu)", __mG_translateMenu},
  130.             {"Copy all", __mG_translateMenu},
  131.             {"Import from clipboard", __mG_translateMenu},
  132.             {"Import from library...", __mG_translateMenu}
  133.         },
  134.         {"Insert",
  135.             {"platform.window:invalidate()", __mG_translateMenu},
  136.             {"function on.paint(gc)", __mG_translateMenu},
  137.             {"gc:setColorRGB(", __mG_translateMenu},
  138.             {"gc:drawString(", __mG_translateMenu},
  139.             {"gc:drawLine(", __mG_translateMenu},
  140.             {"gc:fillRect(", __mG_translateMenu},
  141.             {"gc:drawRect(", __mG_translateMenu},
  142.             {"function on.arrowKey(dir)", __mG_translateMenu},
  143.             {"function on.charIn(chr)", __mG_translateMenu},
  144.             {"function on.enterKey()", __mG_translateMenu},
  145.             {"function on.escapeKey()", __mG_translateMenu},
  146.             {"function on.mouseDown(x,y)", __mG_translateMenu},
  147.             {"function on.mouseMove(x,y)", __mG_translateMenu},
  148.             {"function on.timer()", __mG_translateMenu}
  149.         },  
  150.         {"File",
  151.             {"New", __mG_translateMenu},
  152.             {"Save", __mG_translateMenu},
  153.             {"Save as...", __mG_translateMenu},
  154.             {"Delete", __mG_translateMenu}
  155.         },
  156.         __mG.copy_table(loadMenu),
  157.         {"View",
  158.             {"Go to top", __mG_translateMenu},
  159.             {"Go to end", __mG_translateMenu},
  160.             {"Go to line...", __mG_translateMenu},
  161.             {"Toggle serifs", __mG_translateMenu},
  162.             {"Toggle syntax marking", __mG_translateMenu},
  163.         },
  164.         {"Controls",
  165.             {"nspire with touchpad", __mG_translateMenu},
  166.             {"nspire with clickpad", __mG_translateMenu}
  167.         },
  168.         {"Help",
  169.             {"Help", __mG_translateMenu},
  170.             {"About", __mG_translateMenu}
  171.         }
  172.     }
  173.     toolpalette.register(__mG.menu)
  174. end
  175.  
  176. function __mG.resetEditor()
  177.     __mG.message:reset()
  178.     __mG.dialogue=false
  179.     __mG.skriptName=""
  180.     __mG.code={""}
  181.     __mG.coursor={1,1}
  182.     __mG.scroll={1,1}
  183. end
  184.  
  185. function __mG_translateMenu(box,item)
  186.     if box=="Load" then
  187.         local code=var.recall("JSEF"..item)
  188.         if code then
  189.             __mG.message.show=true
  190.             __mG.message.text={"Warning!","When you load a file, the current","code will be lost (if not saved).","","Do you want to continue anyways?","","","Continue with [enter]        Cancel with [esc]"}
  191.             function __mG.message:action()
  192.                 __mG.resetEditor()
  193.                 __mG.skriptName=item
  194.                 __mG.code=code:split(__mG.nl)
  195.             end
  196.         end
  197.     elseif box=="Insert" then
  198.         __mG.on.charIn(item)
  199.     elseif item=="New" then
  200.         __mG.message.show=true
  201.         __mG.message.text={"Warning!","When you create a new file, the current","code will be lost (if not saved).","","Do you want to continue anyways?","","","Continue with [enter]        Cancel with [esc]"}
  202.         function __mG.message:action()
  203.             __mG.resetEditor()
  204.         end
  205.     elseif item=="Save" then
  206.         if not __mG.skriptName or __mG.skriptName=="" then
  207.             __mG.dialogue="inputName"
  208.             __mG.dialogueVar={__mG.skriptName}
  209.         else
  210.             __mG.saveAs(__mG.skriptName)
  211.         end
  212.     elseif item=="Save as..." then
  213.         __mG.dialogue="inputName"
  214.         __mG.dialogueVar={__mG.skriptName}
  215.     elseif item=="nspire with touchpad" then
  216.         __mG.calcLayout="touch"
  217.         math.eval("DelVar JSElayout")
  218.         var.store("JSElayout", __mG.calcLayout)
  219.         document.markChanged()
  220.     elseif item=="nspire with clickpad" then
  221.         __mG.calcLayout="click"
  222.         math.eval("DelVar JSElayout")
  223.         var.store("JSElayout", __mG.calcLayout)
  224.         document.markChanged()
  225.     elseif item=="Delete" then
  226.         __mG.message.show=true
  227.         __mG.message.text={"Delete?","Do you really want to delete the current file:","'"..__mG.skriptName.."' ?","","","","","Continue with [enter]        Cancel with [esc]"}
  228.         function __mG.message:action()
  229.             math.eval("DelVar JSEF"..__mG.skriptName)
  230.             __mG.fileList[__mG.skriptName]=false
  231.             __mG.saveFileList()
  232.             __mG.createMenu()
  233.         end
  234.     elseif item=="Run" then
  235.         if var.recall("JSEshowInfo")~="NO" then
  236.             __mG.message.show=true
  237.             __mG.message.text={"Info!","Stop the running code with the [EE] key on","the left!","Better save your code before running it!","","Press [tab] to continue and never show","this info again!","Continue with [enter]        Cancel with [esc]"}
  238.             function __mG.message:action()
  239.                 __mG.runCode()
  240.             end
  241.             platform.window:invalidate()
  242.         else
  243.             __mG.runCode()
  244.         end
  245.     elseif item=="Undo   (ctrl+del)" then
  246.         __mG.undo()
  247.     elseif item=="Redo   (ctrl+menu)" then
  248.         __mG.redo()
  249.     elseif item=="Go to top" then
  250.         __mG.coursor={1,1}
  251.         __mG.adjustScroll()
  252.     elseif item=="Go to end" then
  253.         __mG.coursor[2]=#__mG.code
  254.         __mG.coursor[1]=__mG.code[__mG.coursor[2]]:len()
  255.         __mG.adjustScroll()
  256.     elseif item=="Go to line..." then
  257.         __mG.dialogue="gotoLine"
  258.         __mG.dialogueVar={""}
  259.     elseif item=="Toggle serifs" then
  260.         if __mG.font=="sansserif" then __mG.font="serif"
  261.         else __mG.font="sansserif" end
  262.         math.eval("DelVar JSEfont")
  263.         var.store("JSEfont",__mG.font)
  264.         document.markChanged()
  265.         __mG.initialChrOff()
  266.     elseif item=="Toggle syntax marking" then
  267.         __mG.markSyntax=not __mG.markSyntax
  268.         math.eval("DelVar JSEmarkSyntax")
  269.         var.store("JSEmarkSyntax", tostring(__mG.markSyntax))
  270.         document.markChanged()
  271.     elseif item=="Copy all" then
  272.         local codeStr=__mG.arrToStr(__mG.code)
  273.         clipboard.addText(codeStr)
  274.     elseif item=="Import from clipboard" then
  275.         local newCode=clipboard.getText()
  276.         if newCode and newCode~="" then
  277.             __mG.message.show=true
  278.             __mG.message.text={"Warning!","When you import code, your current code","code will be lost (if not saved)","","Do you want to import anyways?","","","Continue with [enter]        Cancel with [esc]"}
  279.             function __mG.message:action()
  280.                 local newCode=clipboard.getText()
  281.                 __mG.resetEditor()
  282.                 __mG.code=newCode:split(__mG.nl)
  283.             end
  284.         end
  285.     elseif item=="Import from library..." then
  286.         __mG.dialogue="importFromLibrary"
  287.         __mG.dialogueVar={"",""}
  288.         __mG.dialogueField=1
  289.     elseif item=="Help" then
  290.         __mG.message.show=true
  291.         if __mG.calcLayout=="touch" then
  292.             __mG.message.text={"Help","Run your program with 'Code'==>'Run'. Stop","running program with the [EE] key on the","left! Ctrl+click and move around to select","code. There are several rebound buttons:","e^x==>[    10^x==>]    ln==>{    log==>}","(-)==>:      Switch calc-layout in 'Controls'!","Close with [esc]"}
  293.         else
  294.             __mG.message.text={"Help","Run your program with 'Code'==>'Run'. Stop","running program with the [EE] key on the","left! Ctrl+click and move around to select","code. There are several rebound buttons:","cos==>[    tan==>]    cos==>{    tan==>}","==>~ Switch calculator layout in 'Controls'!","Close with [esc]"}
  295.         end
  296.     elseif item=="About" then
  297.         __mG.message.show=true
  298.         __mG.message.text={"About","This is Jens' Script Editor "..__mG.version.." by","Jens Kolbinger","Inspired by 'oclua' by Olivier Armand.","","Any feedback to jens.software@gmail.com","","Close with [esc]"}
  299.     end
  300.     platform.window:invalidate()
  301. end
  302.  
  303. function __mG.on.create()
  304.     __mG.createMenu()
  305.     local font=var.recall("JSEfont")
  306.     if font=="serif" then
  307.         __mG.font="serif"
  308.     elseif font=="sansserif" then
  309.         __mG.font="sansserif"
  310.     end
  311.     __mG.initialChrOff()
  312.     local layout=var.recall("JSElayout")
  313.     if layout=="touch" then
  314.         __mG.calcLayout="touch"
  315.     elseif layout=="click" then
  316.         __mG.calcLayout="click"
  317.     end
  318.     local markSyntax=var.recall("JSEmarkSyntax")
  319.     if markSyntax=="true" then
  320.         __mG.markSyntax=true
  321.     elseif markSyntax=="false" then
  322.         __mG.markSyntax=false
  323.     end
  324. end
  325.  
  326. function __mG.on.construction() __mG.on.create() end
  327.  
  328. function __mG.initialChrOff()
  329.     --rendering is much faster if you calculate every characters position before rendering them!
  330.     __mG.chrOffset={}
  331.     local function init(gc)
  332.         gc:setFont(__mG.font,"r",10)
  333.         for i=33,126 do    --all displayable characters
  334.             __mG.chrOffset[string.uchar(i)]=math.floor(4-gc:getStringWidth(string.uchar(i))/2+0.5)
  335.         end
  336.     end
  337.     if platform.withGC then
  338.         platform.withGC(init)
  339.     else
  340.         local gc
  341.         gc=platform.gc()
  342.         gc:begin()
  343.         init(gc)
  344.         gc:finish()
  345.     end
  346.     if __mG.font=="serif" then
  347.         for i, chr in pairs({"i","j","l","m","W","E","T","Z","I","L","M","(",")","{","}","-","?","&","+","^"}) do
  348.             __mG.chrOffset[chr]=__mG.chrOffset[chr]+1
  349.         end
  350.     else
  351.         for i, chr in pairs({"T","W","^","<","=",">","}","+",",",";"}) do
  352.             __mG.chrOffset[chr]=__mG.chrOffset[chr]+1
  353.         end
  354.     end
  355. end
  356.  
  357. function __mG.saveFileList()
  358.     local fileListStr=""
  359.     for name, bool in pairs(__mG.fileList) do
  360.         if bool then
  361.             fileListStr=fileListStr..name.."§"
  362.         end
  363.     end
  364.     math.eval("DelVar JSEFiles")
  365.     var.store("JSEFiles", fileListStr)
  366.     document.markChanged()
  367. end
  368.  
  369. function __mG.saveAs(skriptName)
  370.     __mG.fileList[skriptName]=true
  371.     __mG.saveFileList()
  372.     math.eval("DelVar "..skriptName)
  373.     var.store("JSEF"..skriptName, __mG.arrToStr(__mG.code))
  374.     __mG.createMenu()
  375.     document.markChanged()
  376. end
  377.  
  378. function __mG.on.help()
  379.     __mG_translateMenu("Help","Help")
  380. end
  381.  
  382. function __mG.arrToStr(arr)
  383.     local str=""
  384.     for i=1, #arr do
  385.         if i<#arr then
  386.             str=str..arr[i]..__mG.nl
  387.         else
  388.             str=str..arr[i]
  389.         end
  390.     end
  391.     return str
  392. end
  393.  
  394. function __mG.insert(str,i,insertion)
  395.     return str:sub(0,i-1)..insertion..str:sub(i,str:len())
  396. end
  397.  
  398. function __mG.onError()
  399.     print(__mG.ERR)
  400.     local errArr=__mG.ERR:split(":")
  401.     __mG.ERRline=tonumber(errArr[#errArr-1])
  402.     __mG.ERR=errArr[#errArr]
  403.     __mG.reloadSEHandlers()
  404. end
  405.  
  406. function __mG.runCode()
  407.     timer.stop()
  408.     __mG.ERR = nil
  409.     __mG.ERRline=nil
  410.     local codeStr=__mG.arrToStr(__mG.code)
  411.     local chunk, errload = loadstring(codeStr)
  412.     if not chunk then
  413.         __mG.ERR = errload
  414.         __mG.onError()
  415.     else
  416.         on = {}
  417.         toolpalette.register({})
  418.         cursor.set("default")
  419.         local status, errcall = pcall(chunk)
  420.         if not status then
  421.             __mG.ERR = errcall
  422.             __mG.onError()
  423.             return
  424.         end
  425.         for name, funct in pairs(on) do    --make every function save!
  426.             if funct then
  427.                 on[name]=function(v1,v2,v3,v4,v5)
  428.                     local status, errcall = pcall(function() funct(v1,v2,v3,v4,v5) end)
  429.                     if not status then
  430.                         __mG.ERR = errcall
  431.                         __mG.onError()
  432.                         return
  433.                     end
  434.                 end
  435.             end
  436.         end
  437.         if not __mG.ERR then
  438.             local userCharIn=on.charIn
  439.             function on.charIn(chr,v2,v3,v4,v5)
  440.                 if chr=="" then    --that EE button thing
  441.                     __mG.reloadSEHandlers()
  442.                 else
  443.                     if userCharIn then
  444.                         userCharIn(chr,v2,v3,v4,v5)
  445.                     end
  446.                 end
  447.             end
  448.             if on.construction then on.construction() end
  449.             if on.resize then on.resize(platform.window:width(),platform.window:height()) end
  450.             if on.activate then on.activate() end
  451.             if on.getFocus then on.getFocus() end
  452.             if on.create then on.create() end
  453.         end
  454.         platform.window:invalidate()
  455.     end
  456. end
  457.  
  458. function __mG.validSel(oldSel)  
  459.     if not oldSel then return false end
  460.     if not (oldSel[1] and oldSel[2]) then
  461.         return false
  462.     end
  463.     local newSel=__mG.copy_table(oldSel)
  464.     if oldSel[1][2]==oldSel[2][2] then
  465.         if oldSel[1][1]==oldSel[2][1] then
  466.             return false
  467.         elseif oldSel[1][1]>oldSel[2][1] then
  468.             newSel[1][1]=oldSel[2][1]
  469.             newSel[2][1]=oldSel[1][1]
  470.         end
  471.     elseif oldSel[1][2]>oldSel[2][2] then
  472.         newSel[1]=__mG.copy_table(oldSel[2])
  473.         newSel[2]=__mG.copy_table(oldSel[1])
  474.     end
  475.     return newSel
  476. end
  477.  
  478. function __mG.on.grabDown()
  479.     if __mG.selection[1] then
  480.         if __mG.selection[2] then
  481.             __mG.selection={}
  482.         else
  483.             __mG.selection[2]=__mG.copy_table(__mG.coursor)
  484.         end
  485.     else
  486.         __mG.selection[1]=__mG.copy_table(__mG.coursor)
  487.     end
  488.     platform.window:invalidate()
  489. end
  490.  
  491. function __mG.getSelAsStr()
  492.     local newSel=__mG.validSel(__mG.selection)
  493.     if newSel then
  494.         local text=""
  495.         if newSel[1][2]==newSel[2][2] then
  496.             text = __mG.code[newSel[1][2]]:sub(newSel[1][1],newSel[2][1]-1)
  497.         else
  498.             local firstLine = __mG.code[newSel[1][2]]
  499.             text = firstLine:sub(newSel[1][1],firstLine:len())
  500.             if newSel[2][2]-newSel[1][2]>1 then
  501.                 for l=newSel[1][2]+1, newSel[2][2]-1 do
  502.                     text=text..__mG.nl..__mG.code[l]
  503.                 end
  504.             end
  505.             local lastLine=__mG.code[newSel[2][2]]
  506.             text=text..__mG.nl..lastLine:sub(1,newSel[2][1]-1)
  507.         end
  508.         return text
  509.     end
  510. end
  511.  
  512. function __mG.on.copy()
  513.     local markLine=false
  514.     if __mG.selection[1] then
  515.         if __mG.selection[2] then
  516.             if __mG.selection[2][2]==__mG.selection[1][2] and __mG.selection[2][1]==__mG.selection[1][1] then
  517.                 markLine=true
  518.             end
  519.         else    
  520.             markLine=true
  521.         end
  522.     else
  523.         markLine=true
  524.     end
  525.     if markLine then
  526.         local line=__mG.code[__mG.coursor[2]]
  527.         __mG.selection={{1,__mG.coursor[2]},{line:len()+1,__mG.coursor[2]}}
  528.         platform.window:invalidate()
  529.     end
  530.     clipboard.addText(__mG.getSelAsStr())
  531. end
  532.  
  533. function __mG.on.cut()
  534.     __mG.on.copy()
  535.     __mG.on.backspaceKey()
  536. end
  537.  
  538. function __mG.insertText(text)
  539.     if text and text~=""then
  540.         local state={}
  541.         state.before=__mG.createState("coursor")
  542.         text=text:split(__mG.nl)
  543.         if #text==1 or (#text==2 and text[2]=="") then
  544.             __mG.code[__mG.coursor[2]]=__mG.insert(__mG.code[__mG.coursor[2]],__mG.coursor[1],text[1])
  545.             __mG.coursor[1]=__mG.coursor[1]+text[1]:len()
  546.             state.after = __mG.createState("coursor")
  547.         else
  548.             state.after = {__mG.coursor[2],1,{}}
  549.             local oldLine = __mG.code[__mG.coursor[2]]
  550.             local leftOfC = oldLine:sub(1,__mG.coursor[1]-1)
  551.             local rightOfC = oldLine:sub(__mG.coursor[1],oldLine:len())
  552.             __mG.code[__mG.coursor[2]]=leftOfC..text[1]
  553.             table.insert(state.after[3], __mG.code[__mG.coursor[2]])
  554.             table.remove(text,1)
  555.             for i, line in pairs(text) do
  556.                 __mG.coursor={1,__mG.coursor[2]+1}
  557.                 table.insert(__mG.code, __mG.coursor[2], line)
  558.                 table.insert(state.after[3], line)
  559.                 state.after[2]=state.after[2]+1
  560.             end
  561.             __mG.coursor[1]=__mG.code[__mG.coursor[2]]:len()+1
  562.             __mG.code[__mG.coursor[2]]=__mG.code[__mG.coursor[2]]..rightOfC
  563.             state.after[3][#state.after+1]=__mG.code[__mG.coursor[2]]
  564.             state.after[4]=__mG.copy_table(__mG.coursor)
  565.             __mG.adjustScroll()
  566.             platform.window:invalidate()
  567.         end
  568.         __mG.registerState(state)
  569.     end
  570. end
  571.  
  572. function __mG.on.paste()
  573.     local text=clipboard.getText()
  574.     if __mG.selection[1] and __mG.selection[2] then
  575.         __mG.on.backspaceKey()
  576.     end
  577.     __mG.insertText(text)
  578. end
  579.    
  580. function __mG.on.escapeKey()
  581.     if __mG.message.show then
  582.         __mG.message:reset()
  583.     elseif __mG.dialogue then
  584.         __mG.dialogue=false
  585.         __mG.dialogueVar={}
  586.     elseif __mG.selection[1] then
  587.         __mG.selection={}
  588.     end
  589.     platform.window:invalidate()
  590. end
  591.  
  592. function __mG.on.enterKey()
  593.     if __mG.message.show then
  594.         __mG.message:action()
  595.         __mG.message:reset()
  596.         platform.window:invalidate()
  597.         return
  598.     elseif __mG.dialogue=="inputName" then
  599.         if __mG.dialogueVar[1]:len()>0 then
  600.             if __mG.fileList[__mG.dialogueVar[1]] then
  601.                 __mG.message.show=true
  602.                 __mG.message.text={"File already exists!","There is already a file with the name:","'"..__mG.dialogueVar[1].."'","","Do you want to overwrite it?","","","Continue with [enter]        Cancel with [esc]"}
  603.                 function __mG.message:action()
  604.                     __mG.skriptName=__mG.dialogueVar[1]
  605.                     __mG.saveAs(__mG.skriptName)
  606.                     __mG.dialogue=false
  607.                     platform.window:invalidate()
  608.                 end
  609.             else
  610.                 __mG.skriptName=__mG.dialogueVar[1]
  611.                 __mG.saveAs(__mG.skriptName)
  612.                 __mG.dialogue=false
  613.             end
  614.             platform.window:invalidate()
  615.         end
  616.         return
  617.     elseif __mG.dialogue=="gotoLine" then
  618.         if __mG.dialogueVar[1]:len()>0 then
  619.             local line=math.max(math.min(tonumber(__mG.dialogueVar[1]),#__mG.code),1)
  620.             __mG.coursor={1,line}
  621.             __mG.adjustScroll()
  622.             __mG.dialogue=false
  623.             __mG.dialogueVar={}
  624.             platform.window:invalidate()
  625.         end
  626.         return
  627.     elseif __mG.dialogue=="importFromLibrary" then
  628.         if __mG.dialogueVar[1]:len()>0 and __mG.dialogueVar[2]:len()>0 then
  629.             local text, err = var.recall(__mG.dialogueVar[1].."\\"..__mG.dialogueVar[2])
  630.             if err then
  631.                 print(text, err)
  632.                 __mG.message.show=true
  633.                 __mG.message.text={"Error importing from library!","There was an error while loading the","requested variable!",'Check if the file "'..__mG.dialogueVar[1]..'.tns"','is in the folder "MyLib" and contains the','variable "'..__mG.dialogueVar[2]..'"!',"","Close with [esc]"}
  634.             elseif text and text~="" then
  635.                 __mG.insertText(text)
  636.                 __mG.dialogue=false
  637.                 __mG.dialogueVar={}
  638.             end
  639.             platform.window:invalidate()
  640.         end
  641.         return
  642.     end
  643.    
  644.     if __mG.selection[1] then
  645.         __mG.on.backspaceKey()
  646.     end
  647.    
  648.     local state={}
  649.     state.before = __mG.createState("coursor")
  650.     local oldLine = __mG.code[__mG.coursor[2]]
  651.     local leftOfC = oldLine:sub(1,__mG.coursor[1]-1)
  652.     local rightOfC = oldLine:sub(__mG.coursor[1],oldLine:len())
  653.     table.insert(__mG.code, __mG.coursor[2], "")
  654.     __mG.code[__mG.coursor[2]]=leftOfC
  655.     __mG.code[__mG.coursor[2]+1]=rightOfC
  656.     __mG.coursor={1,__mG.coursor[2]+1}
  657.     local c=1
  658.     while c<=__mG.code[__mG.coursor[2]-1]:len() and __mG.code[__mG.coursor[2]-1]:sub(c,c)==" " do
  659.         __mG.on.charIn(" ")
  660.         c=c+1
  661.     end
  662.     state.after={__mG.coursor[2]-1,__mG.coursor[2],{__mG.code[__mG.coursor[2]-1],__mG.code[__mG.coursor[2]]},__mG.copy_table(__mG.coursor)}
  663.     __mG.registerState(state)
  664.     __mG.adjustScroll()
  665.     platform.window:invalidate()
  666. end
  667.  
  668. function __mG.on.returnKey()
  669.     __mG.on.enterKey()
  670. end
  671.  
  672. function __mG.on.tabKey()
  673.     if __mG.message.show then
  674.         if __mG.message.text[1]=="Info!" then
  675.             var.store("JSEshowInfo","NO")
  676.             __mG.message:reset()
  677.             platform.window:invalidate()
  678.             __mG.runCode()
  679.         end
  680.         return
  681.     elseif __mG.dialogue then
  682.         if __mG.dialogue=="importFromLibrary" then
  683.             if __mG.dialogueField==1 then
  684.                 __mG.dialogueField=2
  685.             else
  686.                 __mG.dialogueField=1
  687.             end
  688.             platform.window:invalidate()
  689.         end
  690.         return
  691.     end
  692.     local state={}
  693.     local newSel=__mG.validSel(__mG.selection)
  694.     if newSel then
  695.         state.before={newSel[1][2],newSel[2][2],{},__mG.copy_table(newSel[2])}
  696.         state.after={newSel[1][2],newSel[2][2],{}}
  697.         for l=newSel[1][2], newSel[2][2] do
  698.             table.insert(state.before[3],__mG.code[l])
  699.             __mG.code[l]="    "..__mG.code[l]
  700.             table.insert(state.after[3],__mG.code[l])
  701.         end
  702.         if newSel[1][1]==1 then
  703.             __mG.selection[1][1]=1
  704.         else
  705.             __mG.selection[1][1]=newSel[1][1]+4
  706.         end
  707.         __mG.selection[2][1]=newSel[2][1]+4
  708.         __mG.coursor=__mG.copy_table(__mG.selection[2])
  709.         state.after[4]=__mG.copy_table(__mG.selection[2])
  710.         __mG.adjustScroll()
  711.         platform.window:invalidate()
  712.     else
  713.         state.before=__mG.createState("coursor")
  714.         __mG.code[__mG.coursor[2]]=__mG.insert(__mG.code[__mG.coursor[2]],__mG.coursor[1],"    ")
  715.         __mG.coursor[1]=__mG.coursor[1]+4
  716.         state.after=__mG.createState("coursor")
  717.     end
  718.     __mG.registerState(state)
  719. end
  720.  
  721. function __mG.on.backtabKey()
  722.     if __mG.dialogue or __mG.message.show then return end
  723.     local function backTabInLine(lineNo)
  724.         __mG.coursor[2]=lineNo
  725.         local found=false
  726.         local i=1
  727.         local line=__mG.code[lineNo]
  728.         while i<=line:len()+1 and not found do    --search for first character
  729.             if line:sub(i,i)~=" " then found=true end    
  730.             i=i+1
  731.         end
  732.         i=i-1
  733.         if i>1 then    --if first character is not at left border
  734.             __mG.coursor[1]=i    --go to the left of first character
  735.             __mG.code[lineNo]=line:sub(math.min(i-1,4)+1,line:len()+1)
  736.             __mG.coursor[1]=__mG.coursor[1]-math.min(i-1,4)
  737.         end
  738.     end
  739.     local state={}
  740.     local newSel=__mG.validSel(__mG.selection)
  741.     if newSel then
  742.         state.before={newSel[1][2],newSel[2][2],{},__mG.copy_table(newSel[2])}
  743.         state.after={newSel[1][2],newSel[2][2],{}}
  744.         for l=newSel[1][2], newSel[2][2] do
  745.             table.insert(state.before[3],__mG.code[l])
  746.             backTabInLine(l)
  747.             table.insert(state.after[3],__mG.code[l])
  748.         end
  749.         __mG.selection[1]={1,newSel[1][2]}
  750.         __mG.selection[2]=__mG.copy_table(__mG.coursor)
  751.         state.after[4]=__mG.copy_table(__mG.coursor)
  752.     else
  753.         state.before=__mG.createState("coursor")
  754.         backTabInLine(__mG.coursor[2])
  755.         state.after=__mG.createState("coursor")
  756.     end
  757.     __mG.registerState(state)
  758.     platform.window:invalidate()
  759. end
  760.  
  761. function __mG.adjustScroll()
  762.     if __mG.coursor[2]-__mG.scroll[2]<1 then
  763.        __mG.scroll[2]=math.max(__mG.coursor[2]-1,1)
  764.     end
  765.     if __mG.coursor[2]-__mG.scroll[2]>__mG.maxLines-2 then
  766.         __mG.scroll[2]=math.min(__mG.coursor[2]-__mG.maxLines+2,#__mG.code)
  767.     end
  768.     if __mG.scroll[2]+__mG.maxLines>#__mG.code then
  769.         __mG.scroll[2]=math.max(#__mG.code-__mG.maxLines+1,1)
  770.     end
  771.     if __mG.coursor[1]-__mG.scroll[1]<1 then
  772.        __mG.scroll[1]=math.max(__mG.coursor[1]-1,1)
  773.     end
  774.     if __mG.coursor[1]-__mG.scroll[1]>__mG.maxChars-1 then
  775.         __mG.scroll[1]=math.max(__mG.coursor[1]-__mG.maxChars+1,1)
  776.     end
  777. end
  778.  
  779. function __mG.on.arrowKey(dir)
  780.     cursor.hide()
  781.     if  __mG.message.show then return
  782.     elseif __mG.dialogue then
  783.         if __mG.dialogue=="importFromLibrary" then
  784.             if __mG.dialogueField==1 then
  785.                 __mG.dialogueField=2
  786.             else
  787.                 __mG.dialogueField=1
  788.             end
  789.             platform.window:invalidate()
  790.         end
  791.         return
  792.     end
  793.     if dir=="up" then
  794.         if __mG.code[__mG.coursor[2]-1] then
  795.             __mG.coursor[2]=__mG.coursor[2]-1
  796.         end
  797.        if __mG.scroll[2]>1 and __mG.coursor[2]-__mG.scroll[2]<1 then
  798.            __mG.scroll[2]=__mG.scroll[2]-1
  799.        end
  800.     elseif dir=="down" then
  801.         if __mG.code[__mG.coursor[2]+1] then
  802.             __mG.coursor[2]=__mG.coursor[2]+1
  803.         end
  804.     elseif dir=="right" then
  805.         __mG.coursor[1]=__mG.coursor[1]+1
  806.         if __mG.coursor[1]>__mG.code[__mG.coursor[2]]:len()+1 then
  807.             if __mG.code[__mG.coursor[2]+1] then
  808.                 __mG.coursor[2]=__mG.coursor[2]+1
  809.                 __mG.coursor[1]=1
  810.             end
  811.         end
  812.     elseif dir=="left" then
  813.         __mG.coursor[1]=__mG.coursor[1]-1
  814.         if __mG.coursor[1]<1 then
  815.             if __mG.code[__mG.coursor[2]-1] then
  816.                 __mG.coursor[2]=__mG.coursor[2]-1
  817.                 __mG.coursor[1]=__mG.code[__mG.coursor[2]]:len()+1
  818.             end
  819.         end
  820.     end
  821.     __mG.moveOK=false
  822.     __mG.coursor[1]=math.max(math.min(__mG.coursor[1],__mG.code[__mG.coursor[2]]:len()+1),1)
  823.     if __mG.selection[1] then
  824.         __mG.selection[2]=__mG.copy_table(__mG.coursor)
  825.     end
  826.     __mG.adjustScroll()
  827.     platform.window:invalidate()
  828. end
  829.  
  830. function __mG.on.backspaceKey()
  831.     if __mG.message.show then return end
  832.     if __mG.dialogue then
  833.         local i = 1
  834.         if __mG.dialogue=="importFromLibrary" then
  835.             i=__mG.dialogueField
  836.         end
  837.         if __mG.dialogueVar[i]:len()>0 then
  838.             __mG.dialogueVar[i]=__mG.dialogueVar[i]:sub(1,__mG.dialogueVar[i]:len()-1)
  839.             platform.window:invalidate()
  840.         end
  841.         return
  842.     end
  843.    
  844.     local state={}
  845.     local line=__mG.code[__mG.coursor[2]]
  846.     if __mG.selection[1] then
  847.         local newSel=__mG.validSel(__mG.selection)
  848.         if newSel then
  849.             state.before=__mG.createState("selection",newSel)
  850.             if newSel[1][2]==newSel[2][2] then
  851.                 __mG.code[newSel[1][2]]=line:sub(1,newSel[1][1]-1)..line:sub(newSel[2][1],line:len())
  852.                 __mG.coursor[1]=newSel[1][1]
  853.             else
  854.                 local firstLine=__mG.code[newSel[1][2]]
  855.                 local lastLine=__mG.code[newSel[2][2]]
  856.                 __mG.code[newSel[1][2]]=firstLine:sub(1,newSel[1][1]-1)..lastLine:sub(newSel[2][1],lastLine:len())
  857.                 for i=newSel[1][2]+1, newSel[2][2] do
  858.                     table.remove(__mG.code,newSel[1][2]+1)
  859.                 end
  860.                 __mG.coursor=__mG.copy_table(newSel[1])
  861.             end
  862.             __mG.selection={}
  863.         end
  864.     elseif __mG.coursor[1]==1 then    --if coursor is at left border
  865.         if __mG.coursor[2]>1 then    --not at first line
  866.             state.before={__mG.coursor[2]-1,__mG.coursor[2],{__mG.code[__mG.coursor[2]-1],__mG.code[__mG.coursor[2]]},__mG.copy_table(__mG.coursor)}
  867.             local oldLen=__mG.code[__mG.coursor[2]-1]:len()+1    
  868.             __mG.code[__mG.coursor[2]-1]=__mG.code[__mG.coursor[2]-1]..__mG.code[__mG.coursor[2]]    --add old line to line above
  869.             table.remove(__mG.code,__mG.coursor[2])
  870.             __mG.coursor={oldLen,__mG.coursor[2]-1}
  871.         end
  872.     else    --remove character
  873.         state.before=__mG.createState("coursor")
  874.         __mG.code[__mG.coursor[2]]=line:sub(1,__mG.coursor[1]-2)..line:sub(__mG.coursor[1],line:len())
  875.         __mG.coursor[1]=__mG.coursor[1]-1
  876.     end
  877.    
  878.     state.after=__mG.createState("coursor")
  879.     __mG.registerState(state)
  880.     __mG.adjustScroll()
  881.     platform.window:invalidate()
  882. end
  883.    
  884. function __mG.on.mouseMove(mx,my)
  885.     if __mG.mouseDown then
  886.         __mG.selection[1]=__mG.copy_table(__mG.mouseDown)
  887.         __mG.selection[2]=__mG.getChrPos(mx,my)
  888.         __mG.coursor=__mG.copy_table(__mG.selection[2])
  889.         __mG.adjustScroll()
  890.         platform.window:invalidate()
  891.     end
  892.     if mx>=(__mG.WW-3) then
  893.         cursor.set("hand pointer")
  894.     else
  895.         cursor.set("text")
  896.     end
  897. end
  898.  
  899. function __mG.on.mouseUp()
  900.     __mG.mouseDown=nil
  901.     if platform.isTabletModeRendering then
  902.         if touch then -- maybe even : if touch and touch.enabled() then
  903.              if not touch.isKeyboardVisible() then
  904.                   touch.showKeyboard()
  905.              end
  906.         end
  907.     end
  908. end
  909.  
  910. function __mG.getChrPos(x,y)
  911.     y=y+5    --middle of the "text" cursor
  912.     local newY = math.max(math.min(math.floor((y-__mG.chrS[2])/__mG.chrS[4])+__mG.scroll[2],#__mG.code),1)
  913.     local newX = math.max(math.min(math.floor((x-__mG.chrS[1])/__mG.chrS[3])+__mG.scroll[1],__mG.code[newY]:len()+1),1)
  914.     return {newX,newY}
  915. end
  916.  
  917. function __mG.on.mouseDown(mx,my)
  918.     if __mG.dialogue or __mG.message.show then return end
  919.     if mx>=(__mG.WW-3) then
  920.         __mG.scroll[2]=math.max(math.min(math.floor((my/(__mG.WH-20))*(#__mG.code-math.floor(__mG.maxLines/2-0.5)-1)),#__mG.code-(__mG.maxLines-1)),1)
  921.     elseif mx==0 and my==0 then    --cursor is hidden
  922.         cursor.show()
  923.     else
  924.         __mG.coursor=__mG.getChrPos(mx,my)
  925.         __mG.mouseDown=__mG.copy_table(__mG.coursor)
  926.         if __mG.selection[1] then
  927.             __mG.on.grabDown()
  928.         end
  929.     end
  930.     platform.window:invalidate()
  931. end
  932.  
  933. function __mG.on.charIn(chr)
  934.     if __mG.message.show then return end
  935.     if __mG.dialogue=="inputName" then
  936.         if chr~=" " and not var.store("tEsT"..__mG.dialogueVar[1]..chr,"") then
  937.             __mG.dialogueVar[1]=__mG.dialogueVar[1]..chr
  938.             math.eval("DelVar ".."tEsT"..__mG.dialogueVar[1])
  939.         end
  940.     elseif __mG.dialogue=="gotoLine" then
  941.         if tostring(tonumber(chr))==chr and __mG.dialogueVar[1]:len()<5 then    --chr is a number
  942.             __mG.dialogueVar[1]=__mG.dialogueVar[1]..chr
  943.         end
  944.     elseif __mG.dialogue=="importFromLibrary" then
  945.         if __mG.dialogueVar[__mG.dialogueField]:len()<15 then
  946.             __mG.dialogueVar[__mG.dialogueField]=__mG.dialogueVar[__mG.dialogueField]..chr
  947.         end
  948.     else
  949.         if not __mG.code[__mG.coursor[2]] then __mG.code[__mG.coursor[2]]="" end
  950.    
  951.         local state={}
  952.         state.before=__mG.createState("coursor")
  953.        
  954.         if __mG.selection[1] then
  955.             if __mG.selection[2] then
  956.                 __mG.on.backspaceKey()
  957.             else
  958.                 __mG.selection={}
  959.             end
  960.         end      
  961.         if __mG.repChr[__mG.calcLayout][chr] then
  962.             chr=__mG.repChr[__mG.calcLayout][chr]
  963.         end
  964.        
  965.         __mG.code[__mG.coursor[2]]=__mG.insert(__mG.code[__mG.coursor[2]],__mG.coursor[1],chr)
  966.         __mG.coursor[1]=__mG.coursor[1]+chr:len()
  967.         __mG.adjustScroll()
  968.        
  969.         state.after=__mG.createState("coursor")
  970.         __mG.registerState(state)
  971.     end
  972.    
  973.     platform.window:invalidate()
  974. end
  975.  
  976. function __mG.on.clearKey()
  977.     __mG.undo()
  978. end
  979.  
  980. function __mG.on.contextMenu()
  981.     __mG.redo()
  982. end
  983.  
  984. function __mG.createState(param,sel)
  985.     local state={}
  986.     if param=="coursor" then
  987.         state={__mG.coursor[2],__mG.coursor[2],{__mG.code[__mG.coursor[2]]},__mG.copy_table(__mG.coursor)}
  988.     elseif param=="selection" then
  989.         state={sel[1][2],sel[2][2],{},__mG.copy_table(sel[2])}
  990.         for l=sel[1][2],sel[2][2] do
  991.             table.insert(state[3], __mG.code[l])
  992.         end
  993.     end
  994.     return state
  995. end
  996.  
  997. function __mG.registerState(state)
  998.     if __mG.stateNo>=10 then
  999.         table.remove(__mG.state,1)
  1000.     else
  1001.         __mG.stateNo=__mG.stateNo+1
  1002.         for i=__mG.stateNo, 10 do
  1003.             __mG.state[i]=nil
  1004.         end
  1005.     end
  1006.     __mG.state[__mG.stateNo]=state
  1007. end
  1008.  
  1009. function __mG.undo()
  1010.     if __mG.stateNo>=1 then
  1011.         __mG.selection={}
  1012.         local state=__mG.state[__mG.stateNo]
  1013.         for l=state.after[1], state.after[2] do
  1014.             table.remove(__mG.code,state.after[1])
  1015.         end
  1016.         l2=1
  1017.         for l=state.before[1], state.before[2] do
  1018.             table.insert(__mG.code,l,state.before[3][l2])
  1019.             l2=l2+1
  1020.         end
  1021.         __mG.coursor=state.before[4]
  1022.         __mG.stateNo=__mG.stateNo-1
  1023.         platform.window:invalidate()
  1024.     end
  1025. end
  1026. function __mG.redo()
  1027.     if __mG.stateNo<10 and __mG.state[__mG.stateNo+1] then
  1028.         __mG.selection={}
  1029.         __mG.stateNo=__mG.stateNo+1
  1030.         local state=__mG.state[__mG.stateNo]
  1031.         for l=state.before[1], state.before[2] do
  1032.             table.remove(__mG.code,state.before[1])
  1033.         end
  1034.         l2=1
  1035.         for l=state.after[1], state.after[2] do
  1036.             table.insert(__mG.code,l,state.after[3][l2])
  1037.             l2=l2+1
  1038.         end
  1039.         __mG.coursor=state.after[4]
  1040.         platform.window:invalidate()
  1041.     end
  1042. end
  1043.  
  1044. function __mG.on.timer()
  1045.     timer.stop()
  1046.     __mG.on.create()
  1047.     platform.window:invalidate()
  1048. end
  1049.  
  1050. function __mG.on.resize(w,h)
  1051.     __mG.WW=w
  1052.     __mG.WH=h
  1053.     if platform.isDeviceModeRendering() then
  1054.         __mG.chrS[3]=8
  1055.         __mG.chrS[4]=15
  1056.     else
  1057.         __mG.chrS[3]=10
  1058.         __mG.chrS[4]=18
  1059.     end
  1060.     __mG.maxLines=math.floor((h-__mG.chrS[2]-30)/__mG.chrS[4])
  1061.     __mG.maxChars=math.floor((w-__mG.chrS[1]-5)/__mG.chrS[3])
  1062.     platform.window:invalidate()
  1063. end
  1064.  
  1065. function __mG.on.paint(gc)
  1066.     local function getPosX(pos)    --pixel koordinate of a character on position pos
  1067.         return __mG.chrS[1]+(pos-__mG.scroll[1])*__mG.chrS[3]
  1068.     end
  1069.    
  1070.     --draw Rim
  1071.     if __mG.colDsp then
  1072.         gc:setColorRGB(240,240,240)
  1073.     else
  1074.         gc:setColorRGB(220,220,220)
  1075.     end
  1076.     gc:fillRect(0,0,__mG.chrS[1],__mG.WH-20)
  1077.    
  1078.     --draw Line markers
  1079.     for l=0,__mG.maxLines-1 do
  1080.         local lineNo=__mG.scroll[2]+l
  1081.         local line=__mG.code[lineNo]
  1082.         local posY=__mG.chrS[2]+(l)*__mG.chrS[4]  
  1083.         local newSel=__mG.validSel(__mG.selection)
  1084.        
  1085.         --mark active line
  1086.         if lineNo==__mG.coursor[2] then    
  1087.             if __mG.colDsp then
  1088.                 gc:setColorRGB(255,255,215)
  1089.             else
  1090.                 gc:setColorRGB(240,240,240)
  1091.             end
  1092.             gc:fillRect(__mG.chrS[1],posY+2,__mG.WW-__mG.chrS[1],__mG.chrS[4])
  1093.             gc:setColorRGB(0,0,0)
  1094.         end
  1095.        
  1096.          --mark error line
  1097.         if lineNo==__mG.ERRline then  
  1098.             if __mG.colDsp then
  1099.                 gc:setColorRGB(230,150,150)
  1100.             else
  1101.                 gc:setColorRGB(150,150,150)
  1102.             end
  1103.             gc:fillRect(0,posY+2,__mG.chrS[1],__mG.chrS[4])
  1104.         end
  1105.        
  1106.         if line then
  1107.              --draw line number
  1108.             if newSel and lineNo>=newSel[1][2] and lineNo<=newSel[2][2] then
  1109.                 if __mG.colDsp then    
  1110.                     gc:setColorRGB(0,0,255)
  1111.                 else
  1112.                     gc:setColorRGB(255,255,255)
  1113.                 end
  1114.             else
  1115.                 if __mG.colDsp then    
  1116.                     gc:setColorRGB(128,0,0)
  1117.                 else
  1118.                     gc:setColorRGB(20,20,20)
  1119.                 end
  1120.             end
  1121.             if lineNo>9999 then    --who would write that much on a calulator?!                                                                                                                                                                                             I
  1122.                 gc:setFont("serif","r",7)
  1123.                 gc:drawString(tostring(lineNo),1,posY+3,"top")
  1124.             elseif lineNo>999 then
  1125.                 if platform.isDeviceModeRendering() then
  1126.                     gc:setFont("serif","r",9)
  1127.                     gc:drawString(tostring(lineNo),1,posY+1,"top")
  1128.                 else
  1129.                     gc:setFont("serif","r",6)
  1130.                     gc:drawString(tostring(lineNo),1,posY+3,"top")
  1131.                 end
  1132.                
  1133.             else
  1134.                 gc:setFont("serif","r",10)
  1135.                 gc:drawString(tostring(lineNo),1,posY,"top")
  1136.             end
  1137.         end
  1138.        
  1139.         --mark selection
  1140.         if __mG.selection[1] then
  1141.             if __mG.colDsp then
  1142.                 gc:setColorRGB(200,200,255)
  1143.             else
  1144.                 gc:setColorRGB(200,200,200)
  1145.             end
  1146.             if newSel then
  1147.                 if newSel[1][2]==newSel[2][2] and lineNo==newSel[1][2] then
  1148.                     local startX=math.max(getPosX(newSel[1][1]),__mG.chrS[1])
  1149.                     local endX=math.min(getPosX(newSel[2][1]),__mG.WW)
  1150.                     if startX<__mG.WW and endX>__mG.chrS[1] then
  1151.                         gc:fillRect(startX,posY+2,endX-startX,__mG.chrS[4])
  1152.                     end
  1153.                 else
  1154.                     if lineNo==newSel[1][2] then
  1155.                         local posX1=math.max(getPosX(newSel[1][1]),__mG.chrS[1])
  1156.                         if posX1<__mG.WW then
  1157.                             gc:fillRect(posX1,posY+2,__mG.WW-posX1,__mG.chrS[4])
  1158.                         end
  1159.                     end
  1160.                     if lineNo>newSel[1][2] and lineNo<newSel[2][2] then
  1161.                         gc:fillRect(__mG.chrS[1],posY+2,__mG.WW-__mG.chrS[1],__mG.chrS[4])
  1162.                     end
  1163.                     if lineNo==newSel[2][2] then
  1164.                         local posX2=math.min(getPosX(newSel[2][1]),__mG.WW)
  1165.                         if posX2>__mG.chrS[1] then
  1166.                             gc:fillRect(__mG.chrS[1],posY+2,posX2-__mG.chrS[1],__mG.chrS[4])
  1167.                         end
  1168.                     end
  1169.                 end
  1170.             end
  1171.         end
  1172.                
  1173.        
  1174.         --draw line
  1175.         if line and line~="" then
  1176.  
  1177.             gc:setFont(__mG.font,"r",10)
  1178.            
  1179.             local lineRGB={}
  1180.             local comment=false
  1181.             if __mG.markSyntax then
  1182.                 --compute syntax marking
  1183.                 local function markSyntax()
  1184.                     local line=__mG.code[lineNo].." "
  1185.                     local isString=false
  1186.                     local word=""
  1187.                     for c=1, line:len() do
  1188.                         local chr = line:usub(c,c)
  1189.                         local no=string.byte(chr)
  1190.                         if not no then no=0 end
  1191.                         if comment then
  1192.                             lineRGB[c]=__mG.wordRGB["--"]
  1193.                         elseif isString then
  1194.                             lineRGB[c]=__mG.wordRGB['"']
  1195.                             if chr==isString then
  1196.                                 isString=false
  1197.                             elseif isString=="[[" and chr=="]" and line:sub(c-1,c-1)=="]" then
  1198.                                 isString=false
  1199.                             end
  1200.                         else
  1201.                             if not((no>=65 and no<=90) or (no>=97 and no<=122) or no==95) then --other character than letter or _
  1202.                                 if __mG.wordRGB[word] then
  1203.                                     for lc=c-word:len(), c-1 do
  1204.                                         lineRGB[lc]=__mG.wordRGB[word]
  1205.                                     end
  1206.                                 end
  1207.                                 word=""
  1208.                                 if c>__mG.scroll[1]+__mG.maxChars then
  1209.                                     return
  1210.                                 end
  1211.                             elseif string.byte(chr)>=33 then
  1212.                                 word=word..chr
  1213.                             end
  1214.                             if chr=='"' or chr=="'" then
  1215.                                 isString=chr
  1216.                                 lineRGB[c]=__mG.wordRGB['"']
  1217.                             elseif chr=="-" then
  1218.                                 if line:sub(c+1,c+1)=="-" then
  1219.                                     comment=c
  1220.                                     lineRGB[c]=__mG.wordRGB["--"]
  1221.                                 end
  1222.                             elseif chr=="[" then
  1223.                                 if line:sub(c+1,c+1)=="[" then
  1224.                                     isString="[["
  1225.                                     lineRGB[c]=__mG.wordRGB['"']
  1226.                                 end
  1227.                             end
  1228.                         end
  1229.                     end
  1230.                 end
  1231.                 markSyntax()
  1232.             end
  1233.            
  1234.             --draw characters
  1235.             for c=0, __mG.maxChars-1 do    --for all visible characters
  1236.                 i=__mG.scroll[1]+c
  1237.                
  1238.                 if i<=line:len() then
  1239.                     char=line:usub(i,i)
  1240.                     if not char then print("FAIL!") end
  1241.                     local no=string.byte(char)
  1242.                     if not no then
  1243.                         no=0
  1244.                     end
  1245.                    
  1246.                     local posX=__mG.chrS[1]+c*__mG.chrS[3]
  1247.  
  1248.                     gc:setColorRGB(0,0,0)
  1249.                     gc:setFont(__mG.font,"r",10)
  1250.                     if __mG.markSyntax then
  1251.                         if lineRGB[i] then
  1252.                             gc:setColorRGB(unpack(lineRGB[i]))
  1253.                         end
  1254.                         if comment then
  1255.                             if i>=comment then
  1256.                                 gc:setFont(__mG.font,"i",10)
  1257.                             end
  1258.                         end
  1259.                     end
  1260.                    
  1261.                     if not __mG.chrOffset[char] then
  1262.                         __mG.chrOffset[char]=5-gc:getStringWidth(char)/2
  1263.                     end
  1264.                    
  1265.                     if no>=33 then
  1266.                         gc:drawString(char,posX+__mG.chrOffset[char],posY,"top")
  1267.                     end
  1268.                 end
  1269.             end --for c=0, 35 do
  1270.         end --if not(line=="" or line==nil) then
  1271.     end --for l=0,11 do
  1272.    
  1273.     --draw cursor
  1274.     if __mG.selection[1] then
  1275.         gc:setColorRGB(0,0,255)
  1276.     else
  1277.         gc:setColorRGB(0,0,0)
  1278.     end
  1279.     local posX=getPosX(__mG.coursor[1])    local posY=__mG.chrS[2]+(__mG.coursor[2]-__mG.scroll[2])*__mG.chrS[4]+2
  1280.     gc:drawLine(posX,posY,posX,posY+__mG.chrS[4]-1)
  1281.    
  1282.    
  1283.     --draw error "console"
  1284.     gc:setColorRGB(0,0,0)
  1285.     gc:drawLine(0,__mG.WH-20,__mG.WW,__mG.WH-20)
  1286.     if __mG.ERR then
  1287.         if __mG.colDsp then
  1288.             gc:setColorRGB(100,0,50)
  1289.         else
  1290.             gc:setColorRGB(20,20,20)
  1291.         end
  1292.         gc:setFont("serif","r",7)
  1293.         gc:drawString(__mG.ERRline..": "..__mG.ERR,5,__mG.WH-15,"top")
  1294.     end
  1295.    
  1296.     --draw scroll bar
  1297.     gc:setColorRGB(130,130,130)
  1298.     local posY1=math.floor((__mG.scroll[2]-1)/#__mG.code*(__mG.WH-20))
  1299.     local posY2=math.min(math.floor(((__mG.scroll[2]+(__mG.maxLines-1))/#__mG.code)*(__mG.WH-20)),(__mG.WH-20))
  1300.     local height=posY2-posY1
  1301.     gc:fillRect(__mG.WW-3,posY1,3,height)
  1302.    
  1303.     __mG.moveOK=true
  1304.     if __mG.dialogue=="inputName" or __mG.dialogue=="gotoLine" then
  1305.         gc:setColorRGB(0,0,0)
  1306.         gc:drawRect(30,70,258,72)
  1307.         gc:setColorRGB(200,200,200)
  1308.         gc:fillRect(31,71,257,71)
  1309.         gc:setColorRGB(230,230,230)
  1310.         gc:fillRect(40,100,238,20)
  1311.         gc:setFont("sansserif","b",12)
  1312.         gc:setColorRGB(20,20,20)
  1313.         if __mG.dialogue=="inputName" then
  1314.             gc:drawString("Save as...",40,75,"top")
  1315.             gc:setFont("sansserif","r",10)
  1316.             gc:drawString("Press enter to save  (esc to cancel)",41,140,"bottom")
  1317.         elseif __mG.dialogue=="gotoLine" then
  1318.             gc:drawString("Go to line...",40,75,"top")
  1319.             gc:setFont("sansserif","r",10)
  1320.             gc:drawString("Press enter to go  (esc to cancel)",41,140,"bottom")
  1321.         end
  1322.         gc:drawString(__mG.dialogueVar[1],45,100,"top")
  1323.     elseif __mG.dialogue=="importFromLibrary" then
  1324.         gc:setColorRGB(0,0,0)
  1325.         gc:drawRect(30,50,258,112)
  1326.         gc:setColorRGB(200,200,200)
  1327.         gc:fillRect(31,51,257,111)
  1328.         gc:setColorRGB(230,230,230)
  1329.         gc:fillRect(40,80,238,20)
  1330.         gc:fillRect(40,120,238,20)
  1331.         gc:setColorRGB(20,20,20)
  1332.         gc:setPen("medium")
  1333.         gc:drawRect(40,80+(__mG.dialogueField-1)*40,238,20)
  1334.         gc:setPen("thin")
  1335.         gc:setFont("sansserif","b",12)
  1336.         gc:drawString("Import from file...",40,55,"top")
  1337.         gc:setFont("sansserif","b",10)
  1338.         gc:drawString("Variable name...",40,100,"top")
  1339.         gc:setFont("sansserif","r",10)
  1340.         gc:drawString("Press enter to save  (esc to cancel)",41,160,"bottom")
  1341.         gc:drawString(__mG.dialogueVar[1],45,80,"top")
  1342.         gc:drawString(__mG.dialogueVar[2],45,120,"top")
  1343.     end
  1344.     if __mG.message.show then
  1345.         gc:setColorRGB(0,0,0)
  1346.         gc:drawRect(24,24,318-49,212-49)
  1347.         gc:setColorRGB(255,255,200)
  1348.         gc:fillRect(25,25,318-50,212-50)
  1349.         gc:setColorRGB(0,0,0)
  1350.         gc:drawLine(25,60,318-25,60)
  1351.         gc:setFont("sansserif","b",11)
  1352.         gc:drawString(__mG.message.text[1],40,32,"top")
  1353.         gc:setFont("sansserif","r",10)
  1354.         for l=0,6 do
  1355.             if not __mG.message.text[l+2] then __mG.message.text[l+2]="" end
  1356.             gc:drawString(__mG.message.text[l+2], 33, 63+l*17, "top")
  1357.         end
  1358.     end
  1359. end
  1360.  
  1361. function __mG.reloadSEHandlers()
  1362.     timer.stop()
  1363.     __mG.createMenu()
  1364.     cursor.set("text")
  1365.     toolpalette.enableCut(true)    toolpalette.enableCopy(true)    toolpalette.enablePaste(true)
  1366.     on={}
  1367.     on=__mG.copy_table(__mG.on)
  1368.     platform.window:invalidate()
  1369.     timer.start(0.1)    --after a crash in the on.paint funcion it won't update directly
  1370. end
  1371. __mG.reloadSEHandlers()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement