Added `doc:rename` command; changed command_view:set_text to take `select` argument

This commit is contained in:
rxi 2020-05-09 16:09:07 +01:00
parent 7479c1380d
commit 70f62f3c8a
3 changed files with 25 additions and 9 deletions

View File

@ -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,
}

View File

@ -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)

View File

@ -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