From 70f62f3c8ad97e6988c03fb783f1a7bc85ce0275 Mon Sep 17 00:00:00 2001 From: rxi Date: Sat, 9 May 2020 16:09:07 +0100 Subject: [PATCH] Added `doc:rename` command; changed command_view:set_text to take `select` argument --- data/core/commands/doc.lua | 20 ++++++++++++++++++-- data/core/commands/findreplace.lua | 9 +++------ data/core/commandview.lua | 5 ++++- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index a8819b5..b7914ec 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -278,6 +278,10 @@ local commands = { end) end, + ["doc:toggle-line-ending"] = function() + doc().crlf = not doc().crlf + end, + ["doc:save-as"] = function() if doc().filename then core.command_view:set_text(doc().filename) @@ -295,8 +299,20 @@ local commands = { end end, - ["doc:toggle-line-ending"] = function() - doc().crlf = not doc().crlf + ["doc:rename"] = function() + 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) + doc():save(filename) + core.log("Renamed \"%s\" to \"%s\"", old_filename, filename) + if filename ~= old_filename then + os.remove(old_filename) + end + end, common.path_suggest) end, } diff --git a/data/core/commands/findreplace.lua b/data/core/commands/findreplace.lua index 1fc48cf..da0f9a2 100644 --- a/data/core/commands/findreplace.lua +++ b/data/core/commands/findreplace.lua @@ -35,8 +35,7 @@ local function find(label, search_fn) local text = dv.doc:get_text(table.unpack(sel)) local found = false - core.command_view:set_text(text) - core.command_view.doc:set_selection(math.huge, math.huge, 1, 1) + core.command_view:set_text(text, true) core.command_view:enter(label, function(text) if found then @@ -69,12 +68,10 @@ end local function replace(kind, default, fn) - core.command_view:set_text(default) - core.command_view.doc:set_selection(math.huge, math.huge, 1, 1) + core.command_view:set_text(default, true) core.command_view:enter("Find To Replace " .. kind, function(old) - core.command_view:set_text(old) - core.command_view.doc:set_selection(math.huge, math.huge, 1, 1) + core.command_view:set_text(old, true) local s = string.format("Replace %s %q With", kind, old) core.command_view:enter(s, function(new) diff --git a/data/core/commandview.lua b/data/core/commandview.lua index ccd8b4b..1809180 100644 --- a/data/core/commandview.lua +++ b/data/core/commandview.lua @@ -70,9 +70,12 @@ function CommandView:get_text() end -function CommandView:set_text(text) +function CommandView:set_text(text, select) self.doc:remove(1, 1, math.huge, math.huge) self.doc:text_input(text) + if select then + self.doc:set_selection(math.huge, math.huge, 1, 1) + end end