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
|
|
|
|
|
|
|
|
|
|
|
|
local function get_indent_string()
|
|
|
|
if config.tab_type == "hard" then
|
|
|
|
return "\t"
|
|
|
|
end
|
|
|
|
return string.rep(" ", config.indent_size)
|
|
|
|
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-06-20 04:32:46 +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-05-10 15:08:56 +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.on_doc_save(saved_filename)
|
|
|
|
core.log("Saved \"%s\"", saved_filename)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2021-06-02 19:09:55 +02:00
|
|
|
-- returns the size of the original indent, and the indent
|
2021-05-22 15:01:19 +02:00
|
|
|
-- in your config format, rounded either up or down
|
|
|
|
local function get_line_indent(line, rnd_up)
|
|
|
|
local _, e = line:find("^[ \t]+")
|
|
|
|
local soft_tab = string.rep(" ", config.indent_size)
|
|
|
|
if config.tab_type == "hard" then
|
|
|
|
local indent = e and line:sub(1, e):gsub(soft_tab, "\t") or ""
|
|
|
|
return e, indent:gsub(" +", rnd_up and "\t" or "")
|
|
|
|
else
|
|
|
|
local indent = e and line:sub(1, e):gsub("\t", soft_tab) or ""
|
|
|
|
local number = #indent / #soft_tab
|
2021-06-02 19:09:55 +02:00
|
|
|
return e, indent:sub(1,
|
2021-05-22 15:01:19 +02:00
|
|
|
(rnd_up and math.ceil(number) or math.floor(number))*#soft_tab)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- un/indents text; behaviour varies based on selection and un/indent.
|
2021-06-02 19:09:55 +02:00
|
|
|
-- * if there's a selection, it will stay static around the
|
2021-05-22 15:01:19 +02:00
|
|
|
-- text for both indenting and unindenting.
|
2021-06-02 19:09:55 +02:00
|
|
|
-- * if you are in the beginning whitespace of a line, and are indenting, the
|
2021-05-22 15:01:19 +02:00
|
|
|
-- cursor will insert the exactly appropriate amount of spaces, and jump the
|
|
|
|
-- cursor to the beginning of first non whitespace characters
|
2021-06-02 19:09:55 +02:00
|
|
|
-- * if you are not in the beginning whitespace of a line, and you indent, it
|
2021-05-22 15:01:19 +02:00
|
|
|
-- inserts the appropriate whitespace, as if you typed them normally.
|
|
|
|
-- * if you are unindenting, the cursor will jump to the start of the line,
|
|
|
|
-- and remove the appropriate amount of spaces (or a tab).
|
|
|
|
local function indent_text(unindent)
|
|
|
|
local text = get_indent_string()
|
2021-05-26 23:52:01 +02:00
|
|
|
local line1, col1, line2, col2, swap = doc_multiline_selection(true)
|
2021-05-22 15:01:19 +02:00
|
|
|
local _, se = doc().lines[line1]:find("^[ \t]+")
|
|
|
|
local in_beginning_whitespace = col1 == 1 or (se and col1 <= se + 1)
|
|
|
|
if unindent or doc():has_selection() or in_beginning_whitespace then
|
|
|
|
local l1d, l2d = #doc().lines[line1], #doc().lines[line2]
|
|
|
|
for line = line1, line2 do
|
|
|
|
local e, rnded = get_line_indent(doc().lines[line], unindent)
|
|
|
|
doc():remove(line, 1, line, (e or 0) + 1)
|
2021-06-02 19:09:55 +02:00
|
|
|
doc():insert(line, 1,
|
2021-05-22 15:01:19 +02:00
|
|
|
unindent and rnded:sub(1, #rnded - #text) or rnded .. text)
|
|
|
|
end
|
|
|
|
l1d, l2d = #doc().lines[line1] - l1d, #doc().lines[line2] - l2d
|
|
|
|
if (unindent or in_beginning_whitespace) and not doc():has_selection() then
|
|
|
|
local start_cursor = (se and se + 1 or 1) + l1d or #(doc().lines[line1])
|
|
|
|
doc():set_selection(line1, start_cursor, line2, start_cursor, swap)
|
|
|
|
else
|
|
|
|
doc():set_selection(line1, col1 + l1d, line2, col2 + l2d, swap)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
doc():text_input(text)
|
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-06-06 03:18:05 +02:00
|
|
|
local function cut_or_copy(delete)
|
|
|
|
local full_text = ""
|
|
|
|
for idx, line1, col1, line2, col2 in doc():get_selections() do
|
|
|
|
if line1 ~= line2 or col1 ~= col2 then
|
|
|
|
local text = doc():get_text(line1, col1, line2, col2)
|
|
|
|
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
|
|
|
|
full_text = full_text == "" and text or (full_text .. "\n" .. text)
|
2021-06-07 02:30:09 +02:00
|
|
|
doc().cursor_clipboard[idx] = text
|
2021-06-06 03:18:05 +02:00
|
|
|
else
|
2021-06-07 02:30:09 +02:00
|
|
|
doc().cursor_clipboard[idx] = ""
|
2021-06-06 03:18:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
system.set_clipboard(full_text)
|
|
|
|
end
|
|
|
|
|
2021-06-09 05:09:09 +02:00
|
|
|
local function split_cursor(direction)
|
|
|
|
local new_cursors = {}
|
|
|
|
for _, line1, col1 in doc():get_selections() do
|
2021-06-12 01:01:43 +02:00
|
|
|
if line1 > 1 and line1 < #doc().lines then
|
2021-06-09 23:14:34 +02:00
|
|
|
table.insert(new_cursors, { line1 + direction, col1 })
|
2021-06-09 05:09:09 +02:00
|
|
|
end
|
|
|
|
end
|
2021-06-10 18:32:33 +02:00
|
|
|
for i,v in ipairs(new_cursors) do doc():add_selection(v[1], v[2]) end
|
2021-06-09 05:09:09 +02:00
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
local commands = {
|
|
|
|
["doc:undo"] = function()
|
|
|
|
doc():undo()
|
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:redo"] = function()
|
|
|
|
doc():redo()
|
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:cut"] = function()
|
2021-06-06 03:18:05 +02:00
|
|
|
cut_or_copy(true)
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:copy"] = function()
|
2021-06-06 03:18:05 +02:00
|
|
|
cut_or_copy(false)
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:paste"] = function()
|
2021-06-05 22:37:45 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc():get_selections() do
|
2021-06-07 02:30:09 +02:00
|
|
|
local value = doc().cursor_clipboard[idx] or system.get_clipboard()
|
2021-06-05 22:37:45 +02:00
|
|
|
doc():text_input(value:gsub("\r", ""), idx)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:newline"] = function()
|
2021-06-10 18:29:55 +02:00
|
|
|
for idx, line, col in doc():get_selections(false, true) do
|
2021-06-05 21:22:56 +02:00
|
|
|
local indent = doc().lines[line]:match("^[\t ]*")
|
|
|
|
if col <= #indent then
|
|
|
|
indent = indent:sub(#indent + 2 - col)
|
|
|
|
end
|
|
|
|
doc():text_input("\n" .. indent, idx)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:newline-below"] = function()
|
2021-06-12 00:14:24 +02:00
|
|
|
for idx, line in doc():get_selections(false, true) do
|
2021-06-05 21:22:56 +02:00
|
|
|
local indent = doc().lines[line]:match("^[\t ]*")
|
|
|
|
doc():insert(line, math.huge, "\n" .. indent)
|
|
|
|
doc():set_selections(idx, line + 1, math.huge)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:newline-above"] = function()
|
2021-06-12 00:14:24 +02:00
|
|
|
for idx, line in doc():get_selections(false, true) do
|
2021-06-05 21:22:56 +02:00
|
|
|
local indent = doc().lines[line]:match("^[\t ]*")
|
|
|
|
doc():insert(line, 1, indent .. "\n")
|
|
|
|
doc():set_selections(idx, line, math.huge)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:delete"] = function()
|
2021-06-07 02:30:09 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc():get_selections() do
|
|
|
|
if line1 == line2 and col1 == col2 and doc().lines[line1]:find("^%s*$", col1) then
|
|
|
|
doc():remove(line1, col1, line1, math.huge)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2021-06-09 05:09:09 +02:00
|
|
|
doc():delete_to_cursor(idx, translate.next_char)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:backspace"] = function()
|
2021-06-07 02:30:09 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc():get_selections() do
|
|
|
|
if line1 == line2 and col1 == col2 then
|
|
|
|
local text = doc():get_text(line1, 1, line1, col1)
|
2021-06-05 21:22:56 +02:00
|
|
|
if #text >= config.indent_size and text:find("^ *$") then
|
2021-06-09 05:09:09 +02:00
|
|
|
doc():delete_to_cursor(idx, 0, -config.indent_size)
|
2021-06-05 21:22:56 +02:00
|
|
|
return
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-06-09 05:09:09 +02:00
|
|
|
doc():delete_to_cursor(idx, translate.previous_char)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:select-all"] = function()
|
|
|
|
doc():set_selection(1, 1, math.huge, math.huge)
|
|
|
|
end,
|
|
|
|
|
2020-05-01 20:25:17 +02:00
|
|
|
["doc:select-none"] = function()
|
|
|
|
local line, col = doc():get_selection()
|
|
|
|
doc():set_selection(line, col)
|
|
|
|
end,
|
|
|
|
|
2021-06-20 04:32:46 +02:00
|
|
|
|
2021-06-09 05:09:09 +02:00
|
|
|
["doc:indent"] = function()
|
|
|
|
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
|
|
|
local l1, c1, l2, c2 = doc():indent_text(false, line1, col1, line2, col2)
|
|
|
|
if l1 then
|
|
|
|
doc():set_selections(idx, l1, c1, l2, c2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2020-05-01 20:25:17 +02:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
["doc:select-lines"] = function()
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, _, line2 in doc():get_selections(true) do
|
|
|
|
append_line_if_last_line(line2)
|
2021-06-19 05:58:55 +02:00
|
|
|
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,
|
|
|
|
|
|
|
|
["doc:select-word"] = function()
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, col1 in doc():get_selections(true) do
|
|
|
|
local line1, col1 = translate.start_of_word(doc(), line1, col1)
|
|
|
|
local line2, col2 = translate.end_of_word(doc(), line1, col1)
|
|
|
|
doc():set_selections(idx, line2, col2, line1, col1)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:join-lines"] = function()
|
2021-06-07 02:30:09 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc():get_selections(true) do
|
2021-06-05 21:22:56 +02:00
|
|
|
if line1 == line2 then line2 = line2 + 1 end
|
|
|
|
local text = doc():get_text(line1, 1, line2, math.huge)
|
|
|
|
text = text:gsub("(.-)\n[\t ]*", function(x)
|
|
|
|
return x:find("^%s*$") and x or x .. " "
|
|
|
|
end)
|
|
|
|
doc():insert(line1, 1, text)
|
|
|
|
doc():remove(line1, #text + 1, line2, math.huge)
|
2021-06-07 02:30:09 +02:00
|
|
|
if line1 ~= line2 or col1 ~= col2 then
|
2021-06-05 21:22:56 +02:00
|
|
|
doc():set_selections(idx, line1, math.huge)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:indent"] = function()
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
|
|
|
local l1, c1, l2, c2 = doc():indent_text(false, line1, col1, line2, col2)
|
2021-06-06 03:18:05 +02:00
|
|
|
if l1 then
|
2021-06-05 21:22:56 +02:00
|
|
|
doc():set_selections(idx, l1, c1, l2, c2)
|
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:unindent"] = function()
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
|
|
|
local l1, c1, l2, c2 = doc():indent_text(true, line1, col1, line2, col2)
|
2021-06-06 03:18:05 +02:00
|
|
|
if l1 then
|
2021-06-05 21:22:56 +02:00
|
|
|
doc():set_selections(idx, l1, c1, l2, c2)
|
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:duplicate-lines"] = function()
|
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)
|
|
|
|
doc():insert(line2 + 1, 1, text)
|
|
|
|
local n = line2 - line1 + 1
|
2021-06-19 05:58:55 +02:00
|
|
|
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,
|
|
|
|
|
|
|
|
["doc:delete-lines"] = function()
|
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)
|
|
|
|
doc():remove(line1, 1, line2 + 1, 1)
|
|
|
|
doc():set_selections(idx, line1, col1)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:move-lines-up"] = function()
|
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]
|
|
|
|
doc():insert(line2 + 1, 1, text)
|
|
|
|
doc():remove(line1 - 1, 1, line1, 1)
|
|
|
|
doc():set_selections(idx, line1 - 1, col1, line2 - 1, col2)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:move-lines-down"] = function()
|
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)
|
|
|
|
if line2 < #doc().lines then
|
|
|
|
local text = doc().lines[line2 + 1]
|
|
|
|
doc():remove(line2 + 1, 1, line2 + 2, 1)
|
|
|
|
doc():insert(line1, 1, text)
|
|
|
|
doc():set_selections(idx, line1 + 1, col1, line2 + 1, col2)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:toggle-line-comments"] = function()
|
2020-05-08 21:29:22 +02:00
|
|
|
local comment = doc().syntax.comment
|
2020-05-08 10:07:13 +02:00
|
|
|
if not comment then return end
|
2021-05-28 08:17:49 +02:00
|
|
|
local indentation = get_indent_string()
|
2020-05-08 10:07:13 +02:00
|
|
|
local comment_text = comment .. " "
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, _, line2 in doc_multiline_selections(true) do
|
|
|
|
local uncomment = true
|
|
|
|
local start_offset = math.huge
|
|
|
|
for line = line1, line2 do
|
|
|
|
local text = doc().lines[line]
|
|
|
|
local s = text:find("%S")
|
2021-05-28 08:17:49 +02:00
|
|
|
local cs, ce = text:find(comment_text, s, true)
|
2021-06-05 21:22:56 +02:00
|
|
|
if s and cs ~= s then
|
|
|
|
uncomment = false
|
|
|
|
start_offset = math.min(start_offset, s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for line = line1, line2 do
|
|
|
|
local text = doc().lines[line]
|
|
|
|
local s = text:find("%S")
|
|
|
|
if uncomment then
|
|
|
|
local cs, ce = text:find(comment_text, s, true)
|
|
|
|
if ce then
|
|
|
|
doc():remove(line, cs, line, ce + 1)
|
|
|
|
end
|
|
|
|
elseif s then
|
|
|
|
doc():insert(line, start_offset, comment_text)
|
2021-05-28 08:17:49 +02:00
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:upper-case"] = function()
|
|
|
|
doc():replace(string.upper)
|
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:lower-case"] = function()
|
|
|
|
doc():replace(string.lower)
|
|
|
|
end,
|
2021-06-20 04:32:46 +02:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
["doc:go-to-line"] = function()
|
|
|
|
local dv = dv()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
core.command_view:enter("Go To Line", 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, function(text)
|
|
|
|
if not text:find("^%d*$") then
|
|
|
|
init_items()
|
|
|
|
return common.fuzzy_match(items, text)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
|
2020-05-09 17:09:07 +02:00
|
|
|
["doc:toggle-line-ending"] = function()
|
|
|
|
doc().crlf = not doc().crlf
|
|
|
|
end,
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
["doc:save-as"] = function()
|
2021-05-27 18:53:31 +02:00
|
|
|
local last_doc = core.last_active_view and core.last_active_view.doc
|
2019-12-28 12:16:32 +01:00
|
|
|
if doc().filename then
|
|
|
|
core.command_view:set_text(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("(.*)[/\\](.+)$")
|
|
|
|
core.command_view:set_text(core.normalize_to_project_dir(dirname) .. PATHSEP)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
core.command_view:enter("Save As", function(filename)
|
2021-02-16 22:52:55 +01:00
|
|
|
save(common.home_expand(filename))
|
|
|
|
end, function (text)
|
|
|
|
return common.home_encode_list(common.path_suggest(common.home_expand(text)))
|
|
|
|
end)
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["doc:save"] = function()
|
|
|
|
if doc().filename then
|
|
|
|
save()
|
|
|
|
else
|
|
|
|
command.perform("doc:save-as")
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2021-06-17 22:26:27 +02:00
|
|
|
["file:rename"] = function()
|
2020-05-09 17:09:07 +02:00
|
|
|
local old_filename = doc().filename
|
|
|
|
if not old_filename then
|
|
|
|
core.error("Cannot rename unsaved doc")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
core.command_view:set_text(old_filename)
|
|
|
|
core.command_view:enter("Rename", function(filename)
|
2021-05-10 15:08:56 +02:00
|
|
|
save(common.home_expand(filename))
|
2020-05-09 17:09:07 +02:00
|
|
|
core.log("Renamed \"%s\" to \"%s\"", old_filename, filename)
|
|
|
|
if filename ~= old_filename then
|
|
|
|
os.remove(old_filename)
|
|
|
|
end
|
2021-05-10 15:08:56 +02:00
|
|
|
end, function (text)
|
|
|
|
return common.home_encode_list(common.path_suggest(common.home_expand(text)))
|
|
|
|
end)
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
2021-06-20 04:32:46 +02:00
|
|
|
|
2021-06-17 22:26:27 +02:00
|
|
|
|
|
|
|
["file:delete"] = function()
|
|
|
|
local filename = doc().abs_filename
|
|
|
|
if not filename then
|
|
|
|
core.error("Cannot remove unsaved doc")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for i,docview in ipairs(core.get_views_referencing_doc(doc())) do
|
|
|
|
local node = core.root_view.root_node:get_node_for_view(docview)
|
|
|
|
node:close_view(core.root_view, docview)
|
|
|
|
end
|
|
|
|
os.remove(filename)
|
|
|
|
core.log("Removed \"%s\"", filename)
|
2021-06-18 01:17:24 +02:00
|
|
|
end,
|
2021-06-09 05:09:09 +02:00
|
|
|
|
|
|
|
["doc:create-cursor-previous-line"] = function()
|
|
|
|
split_cursor(-1)
|
2021-06-10 17:26:39 +02:00
|
|
|
doc():merge_cursors()
|
2021-06-09 05:09:09 +02:00
|
|
|
end,
|
2021-06-20 04:32:46 +02:00
|
|
|
|
2021-06-09 05:09:09 +02:00
|
|
|
["doc:create-cursor-next-line"] = function()
|
|
|
|
split_cursor(1)
|
2021-06-10 17:26:39 +02:00
|
|
|
doc():merge_cursors()
|
2021-06-17 22:26:27 +02:00
|
|
|
end
|
2021-06-09 05:09:09 +02:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
local translations = {
|
|
|
|
["previous-char"] = translate.previous_char,
|
|
|
|
["next-char"] = translate.next_char,
|
2020-05-28 12:50:03 +02:00
|
|
|
["previous-word-start"] = translate.previous_word_start,
|
|
|
|
["next-word-end"] = translate.next_word_end,
|
2020-05-28 14:08:18 +02:00
|
|
|
["previous-block-start"] = translate.previous_block_start,
|
|
|
|
["next-block-end"] = translate.next_block_end,
|
2019-12-28 12:16:32 +01:00
|
|
|
["start-of-doc"] = translate.start_of_doc,
|
|
|
|
["end-of-doc"] = translate.end_of_doc,
|
|
|
|
["start-of-line"] = translate.start_of_line,
|
|
|
|
["end-of-line"] = translate.end_of_line,
|
|
|
|
["start-of-word"] = translate.start_of_word,
|
|
|
|
["end-of-word"] = translate.end_of_word,
|
|
|
|
["previous-line"] = DocView.translate.previous_line,
|
|
|
|
["next-line"] = DocView.translate.next_line,
|
|
|
|
["previous-page"] = DocView.translate.previous_page,
|
|
|
|
["next-page"] = DocView.translate.next_page,
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, fn in pairs(translations) do
|
|
|
|
commands["doc:move-to-" .. name] = function() doc():move_to(fn, dv()) end
|
|
|
|
commands["doc:select-to-" .. name] = function() doc():select_to(fn, dv()) end
|
|
|
|
commands["doc:delete-to-" .. name] = function() doc():delete_to(fn, dv()) end
|
|
|
|
end
|
|
|
|
|
|
|
|
commands["doc:move-to-previous-char"] = function()
|
2021-06-07 02:30:09 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc():get_selections(true) do
|
|
|
|
if line1 ~= line2 or col1 ~= col2 then
|
|
|
|
doc():set_selections(idx, line1, col1)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-06-09 05:09:09 +02:00
|
|
|
doc():move_to(translate.previous_char)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
commands["doc:move-to-next-char"] = function()
|
2021-06-07 02:30:09 +02:00
|
|
|
for idx, line1, col1, line2, col2 in doc():get_selections(true) do
|
|
|
|
if line1 ~= line2 or col1 ~= col2 then
|
|
|
|
doc():set_selections(idx, line2, col2)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-06-09 05:09:09 +02:00
|
|
|
doc():move_to(translate.next_char)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
command.add("core.docview", commands)
|