buffer.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. local m = {} -- platform independant text buffer
  2. local lexer = require'lua-lexer'
  3. local clipboard = '' -- shared between buffer instances
  4. --helper functions
  5. local function sanitize(text)
  6. text = text:gsub('[\192-\255][\128-\191]*', '?') -- remove non-ASCII chars which would cause crash
  7. text = text:gsub('\t', '') -- tabs are handled with buffer:insertTab()
  8. return text
  9. end
  10. local insertCharAt = function(text, c, pos)
  11. c = sanitize(c)
  12. local first = text:sub(1, pos)
  13. local last = text:sub(pos + 1)
  14. return first .. c .. last, #c
  15. end
  16. local repeatN = function(n, f, ...)
  17. for i=1,n do f(...) end
  18. end
  19. function m.new(cols, rows, drawToken, drawRectangle, initialText)
  20. local buffer = {
  21. -- all coordinates in text space (integers)
  22. cols = cols,
  23. rows = rows,
  24. name = '',
  25. cursor = {x=0, y=1}, -- x is 0-indexed, y is 1-indexed
  26. selection = {x=0, y=1}, -- x is 0-indexed, y is 1-indexed
  27. -- selected text spans between cursor and selection marker
  28. scroll = {x=5, y=0}, -- both 0-indexed
  29. lines = {}, -- text broken in lines
  30. lexed = {}, -- text lines broken into tokens
  31. drawToken = drawToken,
  32. drawRectangle = drawRectangle,
  33. -- 'public' api
  34. getText = function(self)
  35. return table.concat(self.lines, '\n')
  36. end,
  37. getCursorLine = function(self)
  38. return self.lines[self.cursor.y]
  39. end,
  40. setName = function(self, name)
  41. self.name = name:gsub('%c', '')
  42. end,
  43. setText = function(self, text)
  44. text = sanitize(text)
  45. self.lines = {}
  46. self.lexed = lexer(text)
  47. for i, line in ipairs(self.lexed) do
  48. lineStrings = {}
  49. for l, token in ipairs(line) do
  50. table.insert(lineStrings, token.data)
  51. end
  52. table.insert(self.lines, table.concat(lineStrings, ''))
  53. end
  54. self:updateView()
  55. self:deselect()
  56. end,
  57. drawCode = function(self)
  58. local linesToDraw = math.min(self.rows, #(self.lexed)-self.scroll.y)
  59. local selectionFrom, selectionTo = self:selectionSpan()
  60. local selectionWidth = (selectionTo.x + selectionTo.y * self.cols) - (selectionFrom.x + selectionFrom.y * self.cols)
  61. -- highlight cursor line
  62. if selectionWidth == 0 then
  63. self.drawRectangle(-1, self.cursor.y - self.scroll.y, self.cols + 2, 'cursorline')
  64. end
  65. -- selection
  66. local x, y = selectionFrom.x, selectionFrom.y
  67. self.drawRectangle(x - self.scroll.x, y - self.scroll.y, selectionWidth, 'selection')
  68. selectionWidth = selectionWidth - (self.cols - selectionFrom.x)
  69. while selectionWidth > 0 do
  70. y = y + 1
  71. self.drawRectangle(0 - self.scroll.x, y - self.scroll.y, selectionWidth, 'selection')
  72. --selectionWidth = selectionWidth - self.lines[y]:len() --self.cols
  73. selectionWidth = selectionWidth - self.cols
  74. end
  75. -- file content
  76. for y = 1, linesToDraw do
  77. local x = -self.scroll.x
  78. local currentLine = y + self.scroll.y
  79. -- draw cursor line and caret
  80. if currentLine == self.cursor.y then
  81. self.drawToken('|', self.cursor.x - self.scroll.x - 0.5, y, 'caret')
  82. end
  83. -- draw single line of text
  84. local lineTokens = self.lexed[currentLine]
  85. for j, token in ipairs(lineTokens) do
  86. self.drawToken(token.data, x, y, token.type)
  87. --print('token',x,y)
  88. x = x + #token.data
  89. end
  90. end
  91. -- status line
  92. self.drawToken(self.statusLine, self.cols - #self.statusLine, 0, 'comment')
  93. end,
  94. -- cursor movement
  95. cursorUp = function(self)
  96. self.cursor.y = self.cursor.y - 1
  97. self:updateView()
  98. end,
  99. cursorDown = function(self)
  100. self.cursor.y = self.cursor.y + 1
  101. self:updateView()
  102. end,
  103. cursorJumpUp = function(self)
  104. repeatN(10, self.cursorUp, self)
  105. end,
  106. cursorJumpDown = function(self)
  107. repeatN(10, self.cursorDown, self)
  108. end,
  109. cursorLeft = function(self)
  110. if self.cursor.x == 0 then
  111. if self.cursor.y > 1 then
  112. self:cursorUp()
  113. self:cursorEnd()
  114. else
  115. return false;
  116. end
  117. else
  118. self.cursor.x = self.cursor.x - 1
  119. self:updateView()
  120. end
  121. return true
  122. end,
  123. cursorRight = function(self)
  124. local length = string.len(self.lines[self.cursor.y])
  125. if self.cursor.x == length then
  126. if self.cursor.y < #(self.lines) then
  127. self:cursorDown()
  128. self:cursorHome()
  129. else
  130. return false
  131. end
  132. else
  133. self.cursor.x = self.cursor.x + 1
  134. self:updateView()
  135. end
  136. return true
  137. end,
  138. cursorJumpLeft = function(self)
  139. self:cursorLeft()
  140. local pattern = self:charAtCursor():find('[%d%l%u]') and '[%d%l%u]' or '%s'
  141. self:repeatOverPattern(pattern, self.cursorLeft, self)
  142. end,
  143. cursorJumpRight = function(self)
  144. self:cursorRight()
  145. local pattern = self:charAtCursor():find('[%d%l%u]') and '[%d%l%u]' or '%s'
  146. self:repeatOverPattern(pattern, self.cursorRight, self)
  147. end,
  148. cursorHome = function(self)
  149. self.cursor.x = 0
  150. self:updateView()
  151. end,
  152. cursorEnd = function(self)
  153. self.cursor.x = string.len(self.lines[self.cursor.y])
  154. self:updateView()
  155. end,
  156. cursorPageUp = function(self)
  157. self.cursor.y = self.cursor.y - self.rows
  158. self:updateView()
  159. end,
  160. cursorPageDown = function(self)
  161. self.cursor.y = self.cursor.y + self.rows
  162. self:updateView()
  163. end,
  164. cursorJumpHome = function(self)
  165. self.cursor.x, self.cursor.y = 0, 1
  166. self:updateView()
  167. end,
  168. cursorJumpEnd = function(self)
  169. self.cursor.y = #self.lines
  170. self.cursor.x = #self.lines[self.cursor.y]
  171. self:updateView()
  172. end,
  173. -- inserting and removing characters
  174. insertCharacter = function(self, c)
  175. self:deleteSelection()
  176. local length
  177. self.lines[self.cursor.y], length = insertCharAt(self.lines[self.cursor.y], c, self.cursor.x)
  178. self:lexLine(self.cursor.y)
  179. self.cursor.x = self.cursor.x + length
  180. self:updateView()
  181. self:deselect()
  182. end,
  183. insertTab = function(self)
  184. self:deleteSelection()
  185. self:insertString(' ') -- tab width is adjustable here
  186. self:deselect()
  187. end,
  188. breakLine = function(self, withoutIndent)
  189. self:deleteSelection()
  190. local nl = self.lines[self.cursor.y]
  191. local bef = nl:sub(1,self.cursor.x)
  192. local aft = nl:sub(self.cursor.x + 1, #nl)
  193. local indent = #(bef:match('^%s+') or '')
  194. self.lines[self.cursor.y] = bef
  195. self:lexLine(self.cursor.y)
  196. table.insert(self.lines, self.cursor.y + 1, aft)
  197. table.insert(self.lexed, self.cursor.y + 1, {})
  198. self:lexLine(self.cursor.y + 1)
  199. self:cursorHome()
  200. self:cursorDown()
  201. self:deselect()
  202. if not withoutIndent then
  203. repeatN(indent, self.insertCharacter, self, ' ')
  204. end
  205. end,
  206. deleteRight = function(self)
  207. if self:isSelected() then
  208. self:deleteSelection()
  209. else
  210. local length = string.len(self.lines[self.cursor.y])
  211. if length == self.cursor.x then -- end of line
  212. if self.cursor.y < #self.lines then
  213. -- if we have another line, remove newline by joining lines
  214. local nl = self.lines[self.cursor.y] .. self.lines[self.cursor.y + 1]
  215. self.lines[self.cursor.y] = nl
  216. self:lexLine(self.cursor.y)
  217. table.remove(self.lines, self.cursor.y + 1)
  218. table.remove(self.lexed, self.cursor.y + 1)
  219. end
  220. else -- middle of line, remove char
  221. local nl = self.lines[self.cursor.y]
  222. local bef = nl:sub(1, self.cursor.x)
  223. local aft = nl:sub(self.cursor.x + 2, string.len(nl))
  224. self.lines[self.cursor.y] = bef..aft
  225. self:lexLine(self.cursor.y)
  226. end
  227. end
  228. end,
  229. deleteLeft = function(self)
  230. if self:isSelected() then
  231. self:deleteSelection()
  232. elseif self:cursorLeft() then
  233. self:deleteRight()
  234. end
  235. end,
  236. deleteWord = function(self)
  237. self:deleteLeft()
  238. local pattern = self:charAtCursor():find('[%d%l%u]') and '[%d%l%u]' or '%s'
  239. self:repeatOverPattern(pattern, self.deleteLeft, self)
  240. end,
  241. -- clipboard
  242. cutText = function(self)
  243. if self:isSelected() then
  244. self:copyText()
  245. self:deleteSelection()
  246. else
  247. clipboard = self.lines[self.cursor.y] .. '\n'
  248. table.remove(self.lines, self.cursor.y)
  249. table.remove(self.lexed, self.cursor.y)
  250. end
  251. self:updateView()
  252. end,
  253. copyText = function(self)
  254. if self:isSelected() then
  255. local selectionFrom, selectionTo = self:selectionSpan()
  256. local lines = {}
  257. for y = selectionFrom.y, selectionTo.y do
  258. local fromX = y == selectionFrom.y and selectionFrom.x or 0
  259. local toX = y == selectionTo.y and selectionTo.x or self.lines[y]:len()
  260. table.insert(lines, self.lines[y]:sub(fromX + 1, toX))
  261. end
  262. clipboard = table.concat(lines, '\n')
  263. else -- copy cursor line
  264. clipboard = self.lines[self.cursor.y] .. '\n'
  265. end
  266. end,
  267. pasteText = function(self)
  268. self:deleteSelection()
  269. self:insertString(clipboard)
  270. end,
  271. -- helper functions
  272. isSelected = function(self)
  273. return self.selection.x ~= self.cursor.x or self.selection.y ~= self.cursor.y
  274. end,
  275. selectionSpan = function(self)
  276. if self.selection.y * self.cols + self.selection.x < self.cursor.y * self.cols + self.cursor.x then
  277. return self.selection, self.cursor
  278. else
  279. return self.cursor, self.selection
  280. end
  281. end,
  282. deleteSelection = function(self)
  283. if not self:isSelected() then return end
  284. local selectionFrom, selectionTo = self:selectionSpan()
  285. local singleLineChange = selectionFrom.y == selectionTo.y
  286. local lines = {}
  287. for y = selectionTo.y, selectionFrom.y, -1 do
  288. local fromX = y == selectionFrom.y and selectionFrom.x or 0
  289. local toX = y == selectionTo.y and selectionTo.x or self.lines[y]:len()
  290. if y > selectionFrom.y and y < selectionTo.y then
  291. table.remove(self.lines, y)
  292. table.remove(self.lexed, y)
  293. else
  294. local fromX = y == selectionFrom.y and selectionFrom.x or 0
  295. local toX = y == selectionTo.y and selectionTo.x or self.lines[y]:len()
  296. local bef = self.lines[y]:sub(0, fromX)
  297. local aft = self.lines[y]:sub(toX + 1, self.lines[y]:len())
  298. self.lines[y] = bef .. aft
  299. end
  300. end
  301. self.cursor.x, self.cursor.y = selectionFrom.x, selectionFrom.y
  302. self:deselect()
  303. if singleLineChange then
  304. self:lexLine(self.cursor.y)
  305. else
  306. self:deleteRight()
  307. self:lexAll()
  308. end
  309. self:updateView()
  310. end,
  311. deselect = function(self)
  312. self.selection.x, self.selection.y = self.cursor.x, self.cursor.y
  313. end,
  314. jumpToLine = function(self, lineNumber, columnNumber)
  315. lineNumber = math.min(lineNumber or 1, #self.lines)
  316. columnNumber = math.min(columnNumber or 0, #self.lines[lineNumber] - 1)
  317. self.cursor.x = columnNumber
  318. self.cursor.y = lineNumber
  319. self.scroll.y = math.max(lineNumber - math.floor(self.rows / 2), 0)
  320. self.scroll.x = math.max(columnNumber - math.floor(7 * self.cols / 8), 0)
  321. self:deselect()
  322. end,
  323. insertString = function(self, str)
  324. local singleLineChange = true
  325. for c in str:gmatch('.') do
  326. if c == '\n' then
  327. singleLineChange = false
  328. self:deselect()
  329. self:breakLine(true)
  330. elseif c:match('%C') then
  331. local length
  332. self.lines[self.cursor.y], length = insertCharAt(self.lines[self.cursor.y], c, self.cursor.x)
  333. self.cursor.x = self.cursor.x + length
  334. end
  335. end
  336. if singleLineChange then
  337. self:lexLine(self.cursor.y)
  338. else
  339. self:lexAll()
  340. end
  341. self:deselect()
  342. end,
  343. charAtCursor = function(self)
  344. return self.lines[self.cursor.y]:sub(self.cursor.x, self.cursor.x)
  345. end,
  346. lexLine = function(self, lineNum)
  347. self.lexed[lineNum] = lexer(self.lines[lineNum])[1]
  348. end,
  349. lexAll = function(self) -- lexing single line cannot handle multiline comments and strings
  350. self.lexed = lexer(self:getText())
  351. end,
  352. updateView = function(self)
  353. self.cursor.y = math.max(self.cursor.y, 1)
  354. self.cursor.y = math.min(self.cursor.y, #(self.lines))
  355. local lineLength = string.len(self.lines[self.cursor.y] or '')
  356. self.cursor.x = math.max(self.cursor.x, 0)
  357. self.cursor.x = math.min(self.cursor.x, lineLength)
  358. if self.cursor.y <= self.scroll.y then
  359. self.scroll.y = self.cursor.y - 1
  360. elseif self.cursor.y > self.scroll.y + self.rows then
  361. self.scroll.y = self.cursor.y - self.rows
  362. end
  363. if self.cursor.x < self.scroll.x then
  364. self.scroll.x = math.max(self.cursor.x - 10, 0)
  365. elseif self.cursor.x > self.scroll.x + self.cols then
  366. self.scroll.x = self.cursor.x + 10 - self.cols
  367. end
  368. self.statusLine = string.format('L%d C%d %s', self.cursor.y, self.cursor.x, self.name)
  369. end,
  370. repeatOverPattern = function(self, pattern, moveF, ...)
  371. -- execute moveF() over text as long as character matches pattern and cursor moves
  372. while self:charAtCursor():match(pattern) do
  373. local oldX, oldY = self.cursor.x, self.cursor.y
  374. moveF(...)
  375. if (oldX == self.cursor.x and oldY == self.cursor.y) then break end
  376. end
  377. end,
  378. }
  379. -- generate all select_ and move_ actions
  380. for _, functionName in ipairs({'Up', 'Down', 'Left', 'Right', 'JumpUp', 'JumpDown', 'JumpLeft', 'JumpRight',
  381. 'Home', 'End', 'PageUp', 'PageDown', 'JumpHome', 'JumpEnd'}) do
  382. buffer['select' .. functionName] = function(self)
  383. --self.selection = self.selection or {x= self.cursor.x, y= self.cursor.y}
  384. self['cursor' .. functionName](self)
  385. end
  386. buffer['move' .. functionName] = function(self)
  387. self['cursor' .. functionName](self)
  388. self:deselect()
  389. end
  390. end
  391. buffer:setText(initialText or '')
  392. return buffer
  393. end
  394. return m
  395. --[[ Lua pattern matching
  396. str:find(pattern) finds the first instance of pattern in string and returns its position
  397. str:gmatch(pattern) when called repeatedly, returns each successive instance of pattern in string
  398. str:gsub(pattern, repl) returns a string where all instances of pattern in string have been replaced with repl
  399. str:match(pattern) returns the first instance of pattern in string
  400. X represents the character X itself as long as it is not a magic character
  401. . represents any single character
  402. %a represents all letters A-Z and a-z
  403. %c represents all control characters such as Null, Tab, Carr.Return, Linefeed, Delete, etc
  404. %d represents all digits 0-9
  405. %l represents all lowercase letters a-z
  406. %p represents all punctuation characters or symbols such as . , ? ! : ; @ [ ] _ { } ~
  407. %s represents all white space characters such as Tab, Carr.Return, Linefeed, Space, etc
  408. %u represents all uppercase letters A-Z
  409. %w represents all alphanumeric characters A-Z and a-z and 0-9
  410. %x represents all hexadecimal digits 0-9 and A-F and a-f
  411. %z represents the character with code \000 because embedded zeroes in a pattern do not work
  412. The upper case letter versions of the above reverses their meaning
  413. i.e. %A represents all non-letters and %D represents all non-digits
  414. + one or more repetitions
  415. * or - zero or more repetitions
  416. ? optional (zero or one occurrence)
  417. %Y represents the character Y if it is any non-alphanumeric character
  418. This is the standard way to get a magic character to match itself
  419. Any punctuation character (even a non magic one) preceded by a % represents itself
  420. e.g. %% represents % percent and %+ represents + plus
  421. [set] represents the class which is the union of all characters in the set
  422. A range of characters is specified by separating first and last character of range with a - hyphen e.g. 1-5
  423. All classes described above may also be used as components in the set
  424. e.g. [%w~] (or [~%w]) represents all alphanumeric characters plus the ~ tilde
  425. [^set] represents the complement of set, where set is interpreted as above
  426. e.g. [^A-Z] represents any character except upper case letters --]]