2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local command = require "core.command"
|
|
|
|
local common = require "core.common"
|
|
|
|
local config = require "core.config"
|
|
|
|
local translate = require "core.doc.translate"
|
|
|
|
local DocView = require "core.docview"
|
|
|
|
|
|
|
|
|
|
|
|
local function dv()
|
|
|
|
return core.active_view
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function doc()
|
|
|
|
return core.active_view.doc
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-06-05 21:22:56 +02:00
|
|
|
local function doc_multiline_selections(sort)
|
2021-06-07 02:30:09 +02:00
|
|
|
local iter, state, idx, line1, col1, line2, col2 = doc():get_selections(sort)
|
2021-07-01 14:50:28 +02:00
|
|
|
return function()
|
2021-06-06 03:18:05 +02:00
|
|
|
idx, line1, col1, line2, col2 = iter(state, idx)
|
2021-06-07 02:30:09 +02:00
|
|
|
if idx and line2 > line1 and col2 == 1 then
|
2021-06-05 21:22:56 +02:00
|
|
|
line2 = line2 - 1
|
|
|
|
col2 = #doc().lines[line2]
|
|
|
|
end
|
|
|
|
return idx, line1, col1, line2, col2
|
2020-11-16 11:52:41 +01:00
|
|
|
end
|
2021-02-24 18:06:47 +01:00
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
local function append_line_if_last_line(line)
|
|
|
|
if line >= #doc().lines then
|
|
|
|
doc():insert(line, math.huge, "\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function save(filename)
|
2021-07-20 20:39:50 +02:00
|
|
|
local abs_filename
|
|
|
|
if filename then
|
|
|
|
filename = core.normalize_to_project_dir(filename)
|
|
|
|
abs_filename = core.project_absolute_path(filename)
|
|
|
|
end
|
|
|
|
doc():save(filename, abs_filename)
|
2021-05-01 19:18:39 +02:00
|
|
|
local saved_filename = doc().filename
|
|
|
|
core.log("Saved \"%s\"", saved_filename)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2021-06-06 03:18:05 +02:00
|
|
|
local function cut_or_copy(delete)
|
|
|
|
local full_text = ""
|
2021-12-05 16:30:03 +01:00
|
|
|
local text = ""
|
2021-12-08 13:34:10 +01:00
|
|
|
core.cursor_clipboard = {}
|
|
|
|
core.cursor_clipboard_whole_line = {}
|
2021-06-06 03:18:05 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc():get_selections() do
|
|
|
|
if line1 ~= line2 or col1 ~= col2 then
|
2021-12-05 16:30:03 +01:00
|
|
|
text = doc():get_text(line1, col1, line2, col2)
|
2021-12-08 13:34:10 +01:00
|
|
|
full_text = full_text == "" and text or (full_text .. " " .. text)
|
|
|
|
core.cursor_clipboard_whole_line[idx] = false
|
2021-06-06 03:18:05 +02:00
|
|
|
if delete then
|
2021-06-10 00:08:22 +02:00
|
|
|
doc():delete_to_cursor(idx, 0)
|
2021-06-06 03:18:05 +02:00
|
|
|
end
|
2021-12-06 13:48:30 +01:00
|
|
|
else -- Cut/copy whole line
|
2022-11-01 23:16:39 +01:00
|
|
|
-- Remove newline from the text. It will be added as needed on paste.
|
|
|
|
text = string.sub(doc().lines[line1], 1, -2)
|
|
|
|
full_text = full_text == "" and text or (full_text .. text .. "\n")
|
2021-12-08 13:34:10 +01:00
|
|
|
core.cursor_clipboard_whole_line[idx] = true
|
2021-12-05 16:30:03 +01:00
|
|
|
if delete then
|
|
|
|
if line1 < #doc().lines then
|
|
|
|
doc():remove(line1, 1, line1 + 1, 1)
|
2022-06-24 05:49:20 +02:00
|
|
|
elseif #doc().lines == 1 then
|
|
|
|
doc():remove(line1, 1, line1, math.huge)
|
2021-12-05 16:30:03 +01:00
|
|
|
else
|
|
|
|
doc():remove(line1 - 1, math.huge, line1, math.huge)
|
|
|
|
end
|
|
|
|
end
|
2021-06-06 03:18:05 +02:00
|
|
|
end
|
2021-12-08 13:34:10 +01:00
|
|
|
core.cursor_clipboard[idx] = text
|
2021-06-06 03:18:05 +02:00
|
|
|
end
|
2021-12-08 13:34:10 +01:00
|
|
|
core.cursor_clipboard["full"] = full_text
|
2021-08-11 03:25:40 +02:00
|
|
|
system.set_clipboard(full_text)
|
2021-06-09 05:09:09 +02:00
|
|
|
end
|
|
|
|
|
2021-08-12 00:14:46 +02:00
|
|
|
local function split_cursor(direction)
|
|
|
|
local new_cursors = {}
|
|
|
|
for _, line1, col1 in doc():get_selections() do
|
2021-08-14 14:59:37 +02:00
|
|
|
if line1 + direction >= 1 and line1 + direction <= #doc().lines then
|
2021-08-12 00:14:46 +02:00
|
|
|
table.insert(new_cursors, { line1 + direction, col1 })
|
|
|
|
end
|
|
|
|
end
|
2022-11-01 23:16:39 +01:00
|
|
|
-- add selections in the order that will leave the "last" added one as doc.last_selection
|
|
|
|
local start, stop = 1, #new_cursors
|
|
|
|
if direction < 0 then
|
|
|
|
start, stop = #new_cursors, 1
|
|
|
|
end
|
|
|
|
for i = start, stop, direction do
|
|
|
|
local v = new_cursors[i]
|
|
|
|
doc():add_selection(v[1], v[2])
|
|
|
|
end
|
2021-08-12 00:14:46 +02:00
|
|
|
core.blink_reset()
|
|
|
|
end
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
local function set_cursor(dv, x, y, snap_type)
|
|
|
|
local line, col = dv:resolve_screen_position(x, y)
|
|
|
|
dv.doc:set_selection(line, col, line, col)
|
2021-11-19 10:05:26 +01:00
|
|
|
if snap_type == "word" or snap_type == "lines" then
|
|
|
|
command.perform("doc:select-" .. snap_type)
|
2021-10-06 04:13:16 +02:00
|
|
|
end
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.mouse_selecting = { line, col, snap_type }
|
2021-10-06 04:13:16 +02:00
|
|
|
core.blink_reset()
|
|
|
|
end
|
|
|
|
|
2022-01-19 03:37:26 +01:00
|
|
|
local function line_comment(comment, line1, col1, line2, col2)
|
2022-01-21 04:17:21 +01:00
|
|
|
local start_comment = (type(comment) == 'table' and comment[1] or comment) .. " "
|
|
|
|
local end_comment = (type(comment) == 'table' and " " .. comment[2])
|
2022-01-08 11:45:38 +01:00
|
|
|
local uncomment = true
|
|
|
|
local start_offset = math.huge
|
|
|
|
for line = line1, line2 do
|
|
|
|
local text = doc().lines[line]
|
|
|
|
local s = text:find("%S")
|
2022-02-07 19:22:43 +01:00
|
|
|
if s then
|
|
|
|
local cs, ce = text:find(start_comment, s, true)
|
|
|
|
if cs ~= s then
|
|
|
|
uncomment = false
|
|
|
|
end
|
|
|
|
start_offset = math.min(start_offset, s)
|
2022-01-08 11:45:38 +01:00
|
|
|
end
|
|
|
|
end
|
2022-02-07 19:22:43 +01:00
|
|
|
|
2022-01-21 04:17:21 +01:00
|
|
|
local end_line = col2 == #doc().lines[line2]
|
2022-01-08 11:45:38 +01:00
|
|
|
for line = line1, line2 do
|
|
|
|
local text = doc().lines[line]
|
|
|
|
local s = text:find("%S")
|
2022-02-07 19:22:43 +01:00
|
|
|
if s and uncomment then
|
2022-01-21 04:17:21 +01:00
|
|
|
if end_comment and text:sub(#text - #end_comment, #text - 1) == end_comment then
|
|
|
|
doc():remove(line, #text - #end_comment, line, #text)
|
|
|
|
end
|
|
|
|
local cs, ce = text:find(start_comment, s, true)
|
2022-01-08 11:45:38 +01:00
|
|
|
if ce then
|
|
|
|
doc():remove(line, cs, line, ce + 1)
|
|
|
|
end
|
|
|
|
elseif s then
|
2022-01-21 04:17:21 +01:00
|
|
|
doc():insert(line, start_offset, start_comment)
|
|
|
|
if end_comment then
|
2022-01-19 03:37:26 +01:00
|
|
|
doc():insert(line, #doc().lines[line], " " .. comment[2])
|
|
|
|
end
|
2022-01-08 11:45:38 +01:00
|
|
|
end
|
|
|
|
end
|
2022-01-21 04:17:21 +01:00
|
|
|
col1 = col1 + (col1 > start_offset and #start_comment or 0) * (uncomment and -1 or 1)
|
|
|
|
col2 = col2 + (col2 > start_offset and #start_comment or 0) * (uncomment and -1 or 1)
|
|
|
|
if end_comment and end_line then
|
|
|
|
col2 = col2 + #end_comment * (uncomment and -1 or 1)
|
|
|
|
end
|
2022-01-19 03:37:26 +01:00
|
|
|
return line1, col1, line2, col2
|
2022-01-08 11:45:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function block_comment(comment, line1, col1, line2, col2)
|
|
|
|
-- automatically skip spaces
|
|
|
|
local word_start = doc():get_text(line1, col1, line1, math.huge):find("%S")
|
|
|
|
local word_end = doc():get_text(line2, 1, line2, col2):find("%s*$")
|
|
|
|
col1 = col1 + (word_start and (word_start - 1) or 0)
|
|
|
|
col2 = word_end and word_end or col2
|
|
|
|
|
|
|
|
local block_start = doc():get_text(line1, col1, line1, col1 + #comment[1])
|
|
|
|
local block_end = doc():get_text(line2, col2 - #comment[2], line2, col2)
|
|
|
|
|
|
|
|
if block_start == comment[1] and block_end == comment[2] then
|
|
|
|
-- remove up to 1 whitespace after the comment
|
|
|
|
local start_len, stop_len = #comment[1], #comment[2]
|
|
|
|
if doc():get_text(line1, col1 + #comment[1], line1, col1 + #comment[1] + 1):find("%s$") then
|
|
|
|
start_len = start_len + 1
|
|
|
|
end
|
|
|
|
if doc():get_text(line2, col2 - #comment[2] - 1, line2, col2):find("^%s") then
|
|
|
|
stop_len = stop_len + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
doc():remove(line1, col1, line1, col1 + start_len)
|
|
|
|
col2 = col2 - (line1 == line2 and start_len or 0)
|
|
|
|
doc():remove(line2, col2 - stop_len, line2, col2)
|
|
|
|
|
|
|
|
return line1, col1, line2, col2 - stop_len
|
|
|
|
else
|
|
|
|
doc():insert(line1, col1, comment[1] .. " ")
|
|
|
|
col2 = col2 + (line1 == line2 and (#comment[1] + 1) or 0)
|
|
|
|
doc():insert(line2, col2, " " .. comment[2])
|
|
|
|
|
|
|
|
return line1, col1, line2, col2 + #comment[2] + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-01 23:16:39 +01:00
|
|
|
local function insert_paste(doc, value, whole_line, idx)
|
|
|
|
if whole_line then
|
|
|
|
local line1, col1 = doc:get_selection_idx(idx)
|
|
|
|
doc:insert(line1, 1, value:gsub("\r", "").."\n")
|
|
|
|
-- Because we're inserting at the start of the line,
|
|
|
|
-- if the cursor is in the middle of the line
|
|
|
|
-- it gets carried to the next line along with the old text.
|
|
|
|
-- If it's at the start of the line it doesn't get carried,
|
|
|
|
-- so we move it of as many characters as we're adding.
|
|
|
|
if col1 == 1 then
|
|
|
|
doc:move_to_cursor(idx, #value+1)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
doc:text_input(value:gsub("\r", ""), idx)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-17 21:55:52 +01:00
|
|
|
local commands = {
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:select-none"] = function(dv)
|
2022-11-01 23:16:39 +01:00
|
|
|
local l1, c1 = dv.doc:get_selection_idx(dv.doc.last_selection)
|
|
|
|
if not l1 then
|
|
|
|
l1, c1 = dv.doc:get_selection_idx(1)
|
|
|
|
end
|
|
|
|
dv.doc:set_selection(l1, c1)
|
2022-03-17 21:55:52 +01:00
|
|
|
end,
|
2022-05-13 00:33:56 +02:00
|
|
|
|
2021-11-24 03:03:38 +01:00
|
|
|
["doc:cut"] = function()
|
|
|
|
cut_or_copy(true)
|
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:copy"] = function()
|
|
|
|
cut_or_copy(false)
|
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:undo"] = function(dv)
|
|
|
|
dv.doc:undo()
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:redo"] = function(dv)
|
|
|
|
dv.doc:redo()
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:paste"] = function(dv)
|
2021-08-11 03:25:40 +02:00
|
|
|
local clipboard = system.get_clipboard()
|
|
|
|
-- If the clipboard has changed since our last look, use that instead
|
2022-11-01 23:16:39 +01:00
|
|
|
if core.cursor_clipboard["full"] ~= clipboard then
|
2021-12-08 13:34:10 +01:00
|
|
|
core.cursor_clipboard = {}
|
|
|
|
core.cursor_clipboard_whole_line = {}
|
2022-11-01 23:16:39 +01:00
|
|
|
for idx in dv.doc:get_selections() do
|
|
|
|
insert_paste(dv.doc, clipboard, false, idx)
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Use internal clipboard(s)
|
|
|
|
-- If there are mixed whole lines and normal lines, consider them all as normal
|
|
|
|
local only_whole_lines = true
|
|
|
|
for _,whole_line in pairs(core.cursor_clipboard_whole_line) do
|
|
|
|
if not whole_line then
|
|
|
|
only_whole_lines = false
|
|
|
|
break
|
|
|
|
end
|
2021-08-11 03:25:40 +02:00
|
|
|
end
|
2022-11-01 23:16:39 +01:00
|
|
|
if #core.cursor_clipboard_whole_line == (#dv.doc.selections/4) then
|
|
|
|
-- If we have the same number of clipboards and selections,
|
|
|
|
-- paste each clipboard into its corresponding selection
|
|
|
|
for idx in dv.doc:get_selections() do
|
|
|
|
insert_paste(dv.doc, core.cursor_clipboard[idx], only_whole_lines, idx)
|
2021-12-08 13:34:10 +01:00
|
|
|
end
|
2022-11-01 23:16:39 +01:00
|
|
|
else
|
|
|
|
-- Paste every clipboard and add a selection at the end of each one
|
|
|
|
local new_selections = {}
|
|
|
|
for idx in dv.doc:get_selections() do
|
|
|
|
for cb_idx in ipairs(core.cursor_clipboard_whole_line) do
|
|
|
|
insert_paste(dv.doc, core.cursor_clipboard[cb_idx], only_whole_lines, idx)
|
|
|
|
if not only_whole_lines then
|
|
|
|
table.insert(new_selections, {dv.doc:get_selection_idx(idx)})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if only_whole_lines then
|
|
|
|
table.insert(new_selections, {dv.doc:get_selection_idx(idx)})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local first = true
|
|
|
|
for _,selection in pairs(new_selections) do
|
|
|
|
if first then
|
|
|
|
dv.doc:set_selection(table.unpack(selection))
|
|
|
|
first = false
|
|
|
|
else
|
|
|
|
dv.doc:add_selection(table.unpack(selection))
|
2021-12-08 13:34:10 +01:00
|
|
|
end
|
2021-12-06 13:48:30 +01:00
|
|
|
end
|
2021-06-05 22:37:45 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:newline"] = function(dv)
|
|
|
|
for idx, line, col in dv.doc:get_selections(false, true) do
|
|
|
|
local indent = dv.doc.lines[line]:match("^[\t ]*")
|
2021-06-05 21:22:56 +02:00
|
|
|
if col <= #indent then
|
|
|
|
indent = indent:sub(#indent + 2 - col)
|
|
|
|
end
|
2022-06-25 03:57:47 +02:00
|
|
|
-- Remove current line if it contains only whitespace
|
2022-08-14 01:56:58 +02:00
|
|
|
if dv.doc.lines[line]:match("^%s+$") then
|
|
|
|
dv.doc:remove(line, 1, line, math.huge)
|
2022-06-25 03:57:47 +02:00
|
|
|
end
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:text_input("\n" .. indent, idx)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:newline-below"] = function(dv)
|
|
|
|
for idx, line in dv.doc:get_selections(false, true) do
|
|
|
|
local indent = dv.doc.lines[line]:match("^[\t ]*")
|
|
|
|
dv.doc:insert(line, math.huge, "\n" .. indent)
|
|
|
|
dv.doc:set_selections(idx, line + 1, math.huge)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:newline-above"] = function(dv)
|
|
|
|
for idx, line in dv.doc:get_selections(false, true) do
|
|
|
|
local indent = dv.doc.lines[line]:match("^[\t ]*")
|
|
|
|
dv.doc:insert(line, 1, indent .. "\n")
|
|
|
|
dv.doc:set_selections(idx, line, math.huge)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:delete"] = function(dv)
|
|
|
|
for idx, line1, col1, line2, col2 in dv.doc:get_selections() do
|
|
|
|
if line1 == line2 and col1 == col2 and dv.doc.lines[line1]:find("^%s*$", col1) then
|
|
|
|
dv.doc:remove(line1, col1, line1, math.huge)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:delete_to_cursor(idx, translate.next_char)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:backspace"] = function(dv)
|
|
|
|
local _, indent_size = dv.doc:get_indent_info()
|
|
|
|
for idx, line1, col1, line2, col2 in dv.doc:get_selections() do
|
2021-06-07 02:30:09 +02:00
|
|
|
if line1 == line2 and col1 == col2 then
|
2022-08-14 01:56:58 +02:00
|
|
|
local text = dv.doc:get_text(line1, 1, line1, col1)
|
2021-11-20 03:20:49 +01:00
|
|
|
if #text >= indent_size and text:find("^ *$") then
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:delete_to_cursor(idx, 0, -indent_size)
|
2021-06-05 21:22:56 +02:00
|
|
|
return
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:delete_to_cursor(idx, translate.previous_char)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:select-all"] = function(dv)
|
|
|
|
dv.doc:set_selection(1, 1, math.huge, math.huge)
|
2022-02-11 06:00:38 +01:00
|
|
|
-- avoid triggering DocView:scroll_to_make_visible
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.last_line1 = 1
|
|
|
|
dv.last_col1 = 1
|
|
|
|
dv.last_line2 = #dv.doc.lines
|
|
|
|
dv.last_col2 = #dv.doc.lines[#dv.doc.lines]
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:select-lines"] = function(dv)
|
|
|
|
for idx, line1, _, line2 in dv.doc:get_selections(true) do
|
2021-06-05 21:22:56 +02:00
|
|
|
append_line_if_last_line(line2)
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:set_selections(idx, line1, 1, line2 + 1, 1)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:select-word"] = function(dv)
|
|
|
|
for idx, line1, col1 in dv.doc:get_selections(true) do
|
|
|
|
local line1, col1 = translate.start_of_word(dv.doc, line1, col1)
|
|
|
|
local line2, col2 = translate.end_of_word(dv.doc, line1, col1)
|
|
|
|
dv.doc:set_selections(idx, line2, col2, line1, col1)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:join-lines"] = function(dv)
|
|
|
|
for idx, line1, col1, line2, col2 in dv.doc:get_selections(true) do
|
2021-06-05 21:22:56 +02:00
|
|
|
if line1 == line2 then line2 = line2 + 1 end
|
2022-08-14 01:56:58 +02:00
|
|
|
local text = dv.doc:get_text(line1, 1, line2, math.huge)
|
2021-06-05 21:22:56 +02:00
|
|
|
text = text:gsub("(.-)\n[\t ]*", function(x)
|
|
|
|
return x:find("^%s*$") and x or x .. " "
|
|
|
|
end)
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:insert(line1, 1, text)
|
|
|
|
dv.doc:remove(line1, #text + 1, line2, math.huge)
|
2021-06-07 02:30:09 +02:00
|
|
|
if line1 ~= line2 or col1 ~= col2 then
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:set_selections(idx, line1, math.huge)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:indent"] = function(dv)
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
2022-08-14 01:56:58 +02:00
|
|
|
local l1, c1, l2, c2 = dv.doc:indent_text(false, line1, col1, line2, col2)
|
2021-06-06 03:18:05 +02:00
|
|
|
if l1 then
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:set_selections(idx, l1, c1, l2, c2)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:unindent"] = function(dv)
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
2022-08-14 01:56:58 +02:00
|
|
|
local l1, c1, l2, c2 = dv.doc:indent_text(true, line1, col1, line2, col2)
|
2021-06-06 03:18:05 +02:00
|
|
|
if l1 then
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:set_selections(idx, l1, c1, l2, c2)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:duplicate-lines"] = function(dv)
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
|
|
|
append_line_if_last_line(line2)
|
|
|
|
local text = doc():get_text(line1, 1, line2 + 1, 1)
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:insert(line2 + 1, 1, text)
|
2021-06-05 21:22:56 +02:00
|
|
|
local n = line2 - line1 + 1
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:set_selections(idx, line1 + n, col1, line2 + n, col2)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:delete-lines"] = function(dv)
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
|
|
|
append_line_if_last_line(line2)
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:remove(line1, 1, line2 + 1, 1)
|
|
|
|
dv.doc:set_selections(idx, line1, col1)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:move-lines-up"] = function(dv)
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
|
|
|
append_line_if_last_line(line2)
|
|
|
|
if line1 > 1 then
|
|
|
|
local text = doc().lines[line1 - 1]
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:insert(line2 + 1, 1, text)
|
|
|
|
dv.doc:remove(line1 - 1, 1, line1, 1)
|
|
|
|
dv.doc:set_selections(idx, line1 - 1, col1, line2 - 1, col2)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:move-lines-down"] = function(dv)
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
|
|
|
append_line_if_last_line(line2 + 1)
|
2022-08-14 01:56:58 +02:00
|
|
|
if line2 < #dv.doc.lines then
|
|
|
|
local text = dv.doc.lines[line2 + 1]
|
|
|
|
dv.doc:remove(line2 + 1, 1, line2 + 2, 1)
|
|
|
|
dv.doc:insert(line1, 1, text)
|
|
|
|
dv.doc:set_selections(idx, line1 + 1, col1, line2 + 1, col2)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:toggle-block-comments"] = function(dv)
|
|
|
|
local comment = dv.doc.syntax.block_comment
|
2021-12-26 08:05:27 +01:00
|
|
|
if not comment then
|
2022-08-14 01:56:58 +02:00
|
|
|
if dv.doc.syntax.comment then
|
2021-12-26 08:05:27 +01:00
|
|
|
command.perform "doc:toggle-line-comments"
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2021-12-25 05:57:00 +01:00
|
|
|
|
|
|
|
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
2021-12-26 08:12:28 +01:00
|
|
|
-- if nothing is selected, toggle the whole line
|
|
|
|
if line1 == line2 and col1 == col2 then
|
|
|
|
col1 = 1
|
2022-08-14 01:56:58 +02:00
|
|
|
col2 = #dv.doc.lines[line2]
|
2021-12-26 08:12:28 +01:00
|
|
|
end
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:set_selections(idx, block_comment(comment, line1, col1, line2, col2))
|
2021-12-25 05:57:00 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:toggle-line-comments"] = function(dv)
|
|
|
|
local comment = dv.doc.syntax.comment or dv.doc.syntax.block_comment
|
2022-01-19 03:37:26 +01:00
|
|
|
if comment then
|
|
|
|
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:set_selections(idx, line_comment(comment, line1, col1, line2, col2))
|
2021-05-28 08:17:49 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:upper-case"] = function(dv)
|
|
|
|
dv.doc:replace(string.uupper)
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:lower-case"] = function(dv)
|
|
|
|
dv.doc:replace(string.ulower)
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
2021-07-01 14:50:28 +02:00
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:go-to-line"] = function(dv)
|
2019-12-28 12:16:32 +01:00
|
|
|
local items
|
|
|
|
local function init_items()
|
|
|
|
if items then return end
|
|
|
|
items = {}
|
|
|
|
local mt = { __tostring = function(x) return x.text end }
|
|
|
|
for i, line in ipairs(dv.doc.lines) do
|
|
|
|
local item = { text = line:sub(1, -2), line = i, info = "line: " .. i }
|
|
|
|
table.insert(items, setmetatable(item, mt))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Go To Line", {
|
|
|
|
submit = function(text, item)
|
|
|
|
local line = item and item.line or tonumber(text)
|
|
|
|
if not line then
|
|
|
|
core.error("Invalid line number or unmatched string")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
dv.doc:set_selection(line, 1 )
|
|
|
|
dv:scroll_to_line(line, true)
|
|
|
|
end,
|
|
|
|
suggest = function(text)
|
|
|
|
if not text:find("^%d*$") then
|
|
|
|
init_items()
|
|
|
|
return common.fuzzy_match(items, text)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
})
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:toggle-line-ending"] = function(dv)
|
|
|
|
dv.doc.crlf = not dv.doc.crlf
|
2020-05-09 17:09:07 +02:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:save-as"] = function(dv)
|
2021-05-27 18:53:31 +02:00
|
|
|
local last_doc = core.last_active_view and core.last_active_view.doc
|
2022-06-01 19:34:46 +02:00
|
|
|
local text
|
2022-08-14 01:56:58 +02:00
|
|
|
if dv.doc.filename then
|
|
|
|
text = dv.doc.filename
|
2021-05-27 18:53:31 +02:00
|
|
|
elseif last_doc and last_doc.filename then
|
2021-05-17 09:29:51 +02:00
|
|
|
local dirname, filename = core.last_active_view.doc.abs_filename:match("(.*)[/\\](.+)$")
|
2022-06-01 19:34:46 +02:00
|
|
|
text = core.normalize_to_project_dir(dirname) .. PATHSEP
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Save As", {
|
2022-06-01 19:34:46 +02:00
|
|
|
text = text,
|
2022-05-30 22:06:47 +02:00
|
|
|
submit = function(filename)
|
|
|
|
save(common.home_expand(filename))
|
|
|
|
end,
|
|
|
|
suggest = function (text)
|
|
|
|
return common.home_encode_list(common.path_suggest(common.home_expand(text)))
|
|
|
|
end
|
|
|
|
})
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:save"] = function(dv)
|
|
|
|
if dv.doc.filename then
|
2019-12-28 12:16:32 +01:00
|
|
|
save()
|
|
|
|
else
|
|
|
|
command.perform("doc:save-as")
|
|
|
|
end
|
|
|
|
end,
|
2022-06-25 03:57:47 +02:00
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:reload"] = function(dv)
|
|
|
|
dv.doc:reload()
|
2022-05-15 21:21:26 +02:00
|
|
|
end,
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["file:rename"] = function(dv)
|
|
|
|
local old_filename = dv.doc.filename
|
2020-05-09 17:09:07 +02:00
|
|
|
if not old_filename then
|
|
|
|
core.error("Cannot rename unsaved doc")
|
|
|
|
return
|
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Rename", {
|
2022-06-01 19:34:46 +02:00
|
|
|
text = old_filename,
|
2022-05-30 22:06:47 +02:00
|
|
|
submit = function(filename)
|
|
|
|
save(common.home_expand(filename))
|
|
|
|
core.log("Renamed \"%s\" to \"%s\"", old_filename, filename)
|
|
|
|
if filename ~= old_filename then
|
|
|
|
os.remove(old_filename)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
suggest = function (text)
|
|
|
|
return common.home_encode_list(common.path_suggest(common.home_expand(text)))
|
2020-05-09 17:09:07 +02:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
})
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
2021-07-01 14:50:28 +02:00
|
|
|
|
2021-06-17 22:26:27 +02:00
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["file:delete"] = function(dv)
|
|
|
|
local filename = dv.doc.abs_filename
|
2021-06-17 22:26:27 +02:00
|
|
|
if not filename then
|
|
|
|
core.error("Cannot remove unsaved doc")
|
|
|
|
return
|
|
|
|
end
|
2022-08-14 01:56:58 +02:00
|
|
|
for i,docview in ipairs(core.get_views_referencing_doc(dv.doc)) do
|
2021-06-17 22:26:27 +02:00
|
|
|
local node = core.root_view.root_node:get_node_for_view(docview)
|
2022-01-13 16:38:20 +01:00
|
|
|
node:close_view(core.root_view.root_node, docview)
|
2021-06-17 22:26:27 +02:00
|
|
|
end
|
|
|
|
os.remove(filename)
|
|
|
|
core.log("Removed \"%s\"", filename)
|
2021-06-18 01:17:24 +02:00
|
|
|
end,
|
2021-12-05 16:30:03 +01:00
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:select-to-cursor"] = function(dv, x, y, clicks)
|
2021-10-06 01:42:04 +02:00
|
|
|
local line1, col1 = select(3, doc():get_selection())
|
2022-08-14 01:56:58 +02:00
|
|
|
local line2, col2 = dv:resolve_screen_position(x, y)
|
|
|
|
dv.mouse_selecting = { line1, col1, nil }
|
|
|
|
dv.doc:set_selection(line2, col2, line1, col1)
|
2021-10-06 01:42:04 +02:00
|
|
|
end,
|
2021-12-05 16:30:03 +01:00
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:create-cursor-previous-line"] = function(dv)
|
2022-08-09 20:53:30 +02:00
|
|
|
split_cursor(-1)
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:merge_cursors()
|
2022-08-09 20:53:30 +02:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:create-cursor-next-line"] = function(dv)
|
2022-08-09 20:53:30 +02:00
|
|
|
split_cursor(1)
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:merge_cursors()
|
2022-08-09 20:53:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
command.add(function(x, y)
|
2022-10-24 04:47:26 +02:00
|
|
|
if x == nil or y == nil or not core.active_view:extends(DocView) then return false end
|
2022-08-14 01:56:58 +02:00
|
|
|
local dv = core.active_view
|
|
|
|
local x1,y1,x2,y2 = dv.position.x, dv.position.y, dv.position.x + dv.size.x, dv.position.y + dv.size.y
|
|
|
|
return x >= x1 + dv:get_gutter_width() and x < x2 and y >= y1 and y < y2, dv, x, y
|
2022-08-09 20:53:30 +02:00
|
|
|
end, {
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:set-cursor"] = function(dv, x, y)
|
|
|
|
set_cursor(dv, x, y, "set")
|
2021-12-05 16:30:03 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:set-cursor-word"] = function(dv, x, y)
|
|
|
|
set_cursor(dv, x, y, "word")
|
2021-12-05 16:30:03 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:set-cursor-line"] = function(dv, x, y, clicks)
|
|
|
|
set_cursor(dv, x, y, "lines")
|
2021-12-05 16:30:03 +01:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["doc:split-cursor"] = function(dv, x, y, clicks)
|
|
|
|
local line, col = dv:resolve_screen_position(x, y)
|
2022-03-17 21:55:52 +01:00
|
|
|
local removal_target = nil
|
2022-08-14 01:56:58 +02:00
|
|
|
for idx, line1, col1 in dv.doc:get_selections(true) do
|
2022-03-17 21:57:18 +01:00
|
|
|
if line1 == line and col1 == col and #doc().selections > 4 then
|
|
|
|
removal_target = idx
|
2022-03-17 21:55:52 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if removal_target then
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:remove_selection(removal_target)
|
2022-03-17 21:55:52 +01:00
|
|
|
else
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:add_selection(line, col, line, col)
|
2022-03-17 21:55:52 +01:00
|
|
|
end
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.mouse_selecting = { line, col, "set" }
|
2021-06-18 01:17:24 +02:00
|
|
|
end
|
2022-08-09 20:53:30 +02:00
|
|
|
})
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
local translations = {
|
2022-03-24 17:01:46 +01:00
|
|
|
["previous-char"] = translate,
|
|
|
|
["next-char"] = translate,
|
|
|
|
["previous-word-start"] = translate,
|
|
|
|
["next-word-end"] = translate,
|
|
|
|
["previous-block-start"] = translate,
|
|
|
|
["next-block-end"] = translate,
|
|
|
|
["start-of-doc"] = translate,
|
|
|
|
["end-of-doc"] = translate,
|
|
|
|
["start-of-line"] = translate,
|
|
|
|
["end-of-line"] = translate,
|
|
|
|
["start-of-word"] = translate,
|
|
|
|
["start-of-indentation"] = translate,
|
|
|
|
["end-of-word"] = translate,
|
|
|
|
["previous-line"] = DocView.translate,
|
|
|
|
["next-line"] = DocView.translate,
|
|
|
|
["previous-page"] = DocView.translate,
|
|
|
|
["next-page"] = DocView.translate,
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:01:46 +01:00
|
|
|
for name, obj in pairs(translations) do
|
2022-08-14 01:56:58 +02:00
|
|
|
commands["doc:move-to-" .. name] = function(dv) dv.doc:move_to(obj[name:gsub("-", "_")], dv) end
|
|
|
|
commands["doc:select-to-" .. name] = function(dv) dv.doc:select_to(obj[name:gsub("-", "_")], dv) end
|
|
|
|
commands["doc:delete-to-" .. name] = function(dv) dv.doc:delete_to(obj[name:gsub("-", "_")], dv) end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
commands["doc:move-to-previous-char"] = function(dv)
|
|
|
|
for idx, line1, col1, line2, col2 in dv.doc:get_selections(true) do
|
2021-06-07 02:30:09 +02:00
|
|
|
if line1 ~= line2 or col1 ~= col2 then
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:set_selections(idx, line1, col1)
|
2022-03-20 04:28:26 +01:00
|
|
|
else
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:move_to_cursor(idx, translate.previous_char)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
commands["doc:move-to-next-char"] = function(dv)
|
|
|
|
for idx, line1, col1, line2, col2 in dv.doc:get_selections(true) do
|
2021-06-07 02:30:09 +02:00
|
|
|
if line1 ~= line2 or col1 ~= col2 then
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:set_selections(idx, line2, col2)
|
2022-03-20 04:28:26 +01:00
|
|
|
else
|
2022-08-14 01:56:58 +02:00
|
|
|
dv.doc:move_to_cursor(idx, translate.next_char)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
command.add("core.docview", commands)
|