doc.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. local core = require "core"
  2. local command = require "core.command"
  3. local common = require "core.common"
  4. local config = require "core.config"
  5. local translate = require "core.doc.translate"
  6. local search = require "core.doc.search"
  7. local DocView = require "core.docview"
  8. local function dv()
  9. return core.active_view
  10. end
  11. local function doc()
  12. return core.active_view.doc
  13. end
  14. local function get_indent_string()
  15. if config.tab_type == "hard" then
  16. return "\t"
  17. end
  18. return string.rep(" ", config.indent_size)
  19. end
  20. local function insert_at_start_of_selected_lines(text)
  21. local line1, col1, line2, col2, swap = doc():get_selection(true)
  22. for line = line1, line2 do
  23. doc():insert(line, 1, text)
  24. end
  25. doc():set_selection(line1, col1 + #text, line2, col2 + #text, swap)
  26. end
  27. local function remove_from_start_of_selected_lines(text)
  28. local line1, col1, line2, col2, swap = doc():get_selection(true)
  29. for line = line1, line2 do
  30. if doc().lines[line]:sub(1, #text) == text then
  31. doc():remove(line, 1, line, #text + 1)
  32. if line == line1 then col1 = col1 - #text end
  33. if line == line2 then col2 = col2 - #text end
  34. end
  35. end
  36. doc():set_selection(line1, col1, line2, col2, swap)
  37. end
  38. local function append_line_if_last_line(line)
  39. if line >= #doc().lines then
  40. doc():insert(line, math.huge, "\n")
  41. end
  42. end
  43. local function save(filename)
  44. doc():save(filename)
  45. core.log("Saved %q", doc().filename)
  46. end
  47. local commands = {
  48. ["doc:undo"] = function()
  49. doc():undo()
  50. end,
  51. ["doc:redo"] = function()
  52. doc():redo()
  53. end,
  54. ["doc:cut"] = function()
  55. local text = doc():get_text(doc():get_selection())
  56. system.set_clipboard(text)
  57. doc():delete_to(0)
  58. end,
  59. ["doc:copy"] = function()
  60. local text = doc():get_text(doc():get_selection())
  61. system.set_clipboard(text)
  62. end,
  63. ["doc:paste"] = function()
  64. doc():text_input(system.get_clipboard())
  65. end,
  66. ["doc:newline"] = function()
  67. local line, col = doc():get_selection()
  68. local indent = doc().lines[line]:match("^[\t ]*")
  69. if col <= #indent then
  70. indent = indent:sub(#indent + 2 - col)
  71. end
  72. doc():text_input("\n" .. indent)
  73. end,
  74. ["doc:newline-below"] = function()
  75. local line = doc():get_selection()
  76. local indent = doc().lines[line]:match("^[\t ]*")
  77. doc():insert(line, math.huge, "\n" .. indent)
  78. doc():set_selection(line + 1, math.huge)
  79. end,
  80. ["doc:newline-above"] = function()
  81. local line = doc():get_selection()
  82. local indent = doc().lines[line]:match("^[\t ]*")
  83. doc():insert(line, 1, indent .. "\n")
  84. doc():set_selection(line, math.huge)
  85. end,
  86. ["doc:delete"] = function()
  87. local line, col = doc():get_selection()
  88. if not doc():has_selection() and doc().lines[line]:find("^%s*$", col) then
  89. doc():remove(line, col, line, math.huge)
  90. end
  91. doc():delete_to(translate.next_char)
  92. end,
  93. ["doc:backspace"] = function()
  94. local line, col = doc():get_selection()
  95. if not doc():has_selection() then
  96. local text = doc():get_text(line, 1, line, col)
  97. if #text >= config.indent_size and text:find("^ *$") then
  98. doc():delete_to(0, -config.indent_size)
  99. return
  100. end
  101. end
  102. doc():delete_to(translate.previous_char)
  103. end,
  104. ["doc:select-all"] = function()
  105. doc():set_selection(1, 1, math.huge, math.huge)
  106. end,
  107. ["doc:select-lines"] = function()
  108. local line1, _, line2, _, swap = doc():get_selection(true)
  109. append_line_if_last_line(line2)
  110. doc():set_selection(line1, 1, line2 + 1, 1, swap)
  111. end,
  112. ["doc:select-word"] = function()
  113. local line1, col1 = doc():get_selection(true)
  114. local line1, col1 = translate.start_of_word(doc(), line1, col1)
  115. local line2, col2 = translate.end_of_word(doc(), line1, col1)
  116. doc():set_selection(line2, col2, line1, col1)
  117. end,
  118. ["doc:join-lines"] = function()
  119. local line1, _, line2 = doc():get_selection(true)
  120. if line1 == line2 then line2 = line2 + 1 end
  121. local text = doc():get_text(line1, 1, line2, math.huge)
  122. text = text:gsub("\n[\t ]*", " ")
  123. doc():insert(line1, 1, text)
  124. doc():remove(line1, #text + 1, line2, math.huge)
  125. if doc():has_selection() then
  126. doc():set_selection(line1, math.huge)
  127. end
  128. end,
  129. ["doc:indent"] = function()
  130. local text = get_indent_string()
  131. if doc():has_selection() then
  132. insert_at_start_of_selected_lines(text)
  133. else
  134. doc():text_input(text)
  135. end
  136. end,
  137. ["doc:unindent"] = function()
  138. local text = get_indent_string()
  139. remove_from_start_of_selected_lines(text)
  140. end,
  141. ["doc:duplicate-lines"] = function()
  142. local line1, col1, line2, col2, swap = doc():get_selection(true)
  143. append_line_if_last_line(line2)
  144. local text = doc():get_text(line1, 1, line2 + 1, 1)
  145. doc():insert(line2 + 1, 1, text)
  146. local n = line2 - line1 + 1
  147. doc():set_selection(line1 + n, col1, line2 + n, col2, swap)
  148. end,
  149. ["doc:delete-lines"] = function()
  150. local line1, col1, line2 = doc():get_selection(true)
  151. append_line_if_last_line(line2)
  152. doc():remove(line1, 1, line2 + 1, 1)
  153. doc():set_selection(line1, col1)
  154. end,
  155. ["doc:move-lines-up"] = function()
  156. local line1, col1, line2, col2, swap = doc():get_selection(true)
  157. append_line_if_last_line(line2)
  158. if line1 > 1 then
  159. local text = doc().lines[line1 - 1]
  160. doc():insert(line2 + 1, 1, text)
  161. doc():remove(line1 - 1, 1, line1, 1)
  162. doc():set_selection(line1 - 1, col1, line2 - 1, col2, swap)
  163. end
  164. end,
  165. ["doc:move-lines-down"] = function()
  166. local line1, col1, line2, col2, swap = doc():get_selection(true)
  167. append_line_if_last_line(line2 + 1)
  168. if line2 < #doc().lines then
  169. local text = doc().lines[line2 + 1]
  170. doc():remove(line2 + 1, 1, line2 + 2, 1)
  171. doc():insert(line1, 1, text)
  172. doc():set_selection(line1 + 1, col1, line2 + 1, col2, swap)
  173. end
  174. end,
  175. ["doc:toggle-line-comments"] = function()
  176. if not dv().syntax.comment then return end
  177. local text = dv().syntax.comment .. " "
  178. local line1, _, line2 = doc():get_selection(true)
  179. local uncomment = true
  180. for line = line1, line2 do
  181. local str = doc().lines[line]:match("^[ \t]*(.*)$")
  182. if str and str:sub(1, #text) ~= text then
  183. uncomment = false
  184. break
  185. end
  186. end
  187. if uncomment then
  188. remove_from_start_of_selected_lines(text)
  189. else
  190. insert_at_start_of_selected_lines(text)
  191. end
  192. end,
  193. ["doc:upper-case"] = function()
  194. doc():replace(string.upper)
  195. end,
  196. ["doc:lower-case"] = function()
  197. doc():replace(string.lower)
  198. end,
  199. ["doc:go-to-line"] = function()
  200. local dv = dv()
  201. local items
  202. local function init_items()
  203. if items then return end
  204. items = {}
  205. local mt = { __tostring = function(x) return x.text end }
  206. for i, line in ipairs(dv.doc.lines) do
  207. local item = { text = line:sub(1, -2), line = i, info = "line: " .. i }
  208. table.insert(items, setmetatable(item, mt))
  209. end
  210. end
  211. core.command_view:enter("Go To Line", function(text, item)
  212. local line = item and item.line or tonumber(text)
  213. if not line then
  214. core.error("Invalid line number or unmatched string")
  215. return
  216. end
  217. dv.doc:set_selection(line, 1 )
  218. dv:scroll_to_line(line, true)
  219. end, function(text)
  220. if not text:find("^%d*$") then
  221. init_items()
  222. return common.fuzzy_match(items, text)
  223. end
  224. end)
  225. end,
  226. ["doc:save-as"] = function()
  227. if doc().filename then
  228. core.command_view:set_text(doc().filename)
  229. end
  230. core.command_view:enter("Save As", function(filename)
  231. save(filename)
  232. end, common.path_suggest)
  233. end,
  234. ["doc:save"] = function()
  235. if doc().filename then
  236. save()
  237. else
  238. command.perform("doc:save-as")
  239. end
  240. end,
  241. ["doc:toggle-line-ending"] = function()
  242. doc().crlf = not doc().crlf
  243. end,
  244. }
  245. local translations = {
  246. ["previous-char"] = translate.previous_char,
  247. ["next-char"] = translate.next_char,
  248. ["previous-word-boundary"] = translate.previous_word_boundary,
  249. ["next-word-boundary"] = translate.next_word_boundary,
  250. ["previous-start-of-block"] = translate.previous_start_of_block,
  251. ["next-start-of-block"] = translate.next_start_of_block,
  252. ["start-of-doc"] = translate.start_of_doc,
  253. ["end-of-doc"] = translate.end_of_doc,
  254. ["start-of-line"] = translate.start_of_line,
  255. ["end-of-line"] = translate.end_of_line,
  256. ["start-of-word"] = translate.start_of_word,
  257. ["end-of-word"] = translate.end_of_word,
  258. ["previous-line"] = DocView.translate.previous_line,
  259. ["next-line"] = DocView.translate.next_line,
  260. ["previous-page"] = DocView.translate.previous_page,
  261. ["next-page"] = DocView.translate.next_page,
  262. }
  263. for name, fn in pairs(translations) do
  264. commands["doc:move-to-" .. name] = function() doc():move_to(fn, dv()) end
  265. commands["doc:select-to-" .. name] = function() doc():select_to(fn, dv()) end
  266. commands["doc:delete-to-" .. name] = function() doc():delete_to(fn, dv()) end
  267. end
  268. commands["doc:move-to-previous-char"] = function()
  269. if doc():has_selection() then
  270. local line, col = doc():get_selection(true)
  271. doc():set_selection(line, col)
  272. else
  273. doc():move_to(translate.previous_char)
  274. end
  275. end
  276. commands["doc:move-to-next-char"] = function()
  277. if doc():has_selection() then
  278. local _, _, line, col = doc():get_selection(true)
  279. doc():set_selection(line, col)
  280. else
  281. doc():move_to(translate.next_char)
  282. end
  283. end
  284. command.add("core.docview", commands)