2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local command = require "core.command"
|
2020-05-02 12:14:07 +02:00
|
|
|
local config = require "core.config"
|
2019-12-28 12:16:32 +01:00
|
|
|
local search = require "core.doc.search"
|
2021-06-21 03:22:37 +02:00
|
|
|
local keymap = require "core.keymap"
|
2019-12-28 12:16:32 +01:00
|
|
|
local DocView = require "core.docview"
|
2021-06-21 03:22:37 +02:00
|
|
|
local CommandView = require "core.commandview"
|
|
|
|
local StatusView = require "core.statusview"
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-06-21 03:22:37 +02:00
|
|
|
local max_last_finds = 50
|
|
|
|
local last_finds, last_view, last_fn, last_text, last_sel
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-06-21 03:22:37 +02:00
|
|
|
local case_insensitive = config.find_case_insensitive or false
|
|
|
|
local plain = config.find_plain or false
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
local function doc()
|
2021-06-21 03:22:37 +02:00
|
|
|
return last_view and last_view.doc or core.active_view.doc
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2021-06-21 03:22:37 +02:00
|
|
|
local function get_find_tooltip()
|
|
|
|
local rf = keymap.get_binding("find-replace:repeat-find")
|
|
|
|
local ti = keymap.get_binding("find-replace:toggle-insensitivity")
|
|
|
|
local tr = keymap.get_binding("find-replace:toggle-plain")
|
|
|
|
return (plain and "[Plain] " or "") ..
|
|
|
|
(case_insensitive and "[Insensitive] " or "") ..
|
|
|
|
(rf and ("Press " .. rf .. " to select the next match.") or "") ..
|
|
|
|
(ti and (" " .. ti .. " toggles case sensitivity.") or "") ..
|
|
|
|
(tr and (" " .. tr .. " toggles plain find.") or "")
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-06-21 03:22:37 +02:00
|
|
|
local function update_preview(sel, search_fn, text)
|
|
|
|
local ok, line1, col1, line2, col2 =
|
|
|
|
pcall(search_fn, last_view.doc, sel[1], sel[2], text, case_insensitive, plain)
|
|
|
|
if ok and line1 and text ~= "" then
|
|
|
|
last_view.doc:set_selection(line2, col2, line1, col1)
|
|
|
|
last_view:scroll_to_line(line2, true)
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
last_view.doc:set_selection(unpack(sel))
|
|
|
|
return false
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function find(label, search_fn)
|
2021-06-21 03:22:37 +02:00
|
|
|
last_view, last_sel, last_finds = core.active_view,
|
|
|
|
{ core.active_view.doc:get_selection() }, {}
|
|
|
|
local text, found = last_view.doc:get_text(unpack(last_sel)), false
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2020-05-09 17:09:07 +02:00
|
|
|
core.command_view:set_text(text, true)
|
2021-06-21 03:22:37 +02:00
|
|
|
core.status_view:show_tooltip(get_find_tooltip())
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
core.command_view:enter(label, function(text)
|
2021-06-21 03:22:37 +02:00
|
|
|
core.status_view:remove_tooltip()
|
2019-12-28 12:16:32 +01:00
|
|
|
if found then
|
|
|
|
last_fn, last_text = search_fn, text
|
|
|
|
else
|
|
|
|
core.error("Couldn't find %q", text)
|
2021-06-21 03:22:37 +02:00
|
|
|
last_view.doc:set_selection(unpack(last_sel))
|
|
|
|
last_view:scroll_to_make_visible(unpack(last_sel))
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end, function(text)
|
2021-06-21 03:22:37 +02:00
|
|
|
found = update_preview(last_sel, search_fn, text)
|
|
|
|
last_fn, last_text = search_fn, text
|
2019-12-28 12:16:32 +01:00
|
|
|
end, function(explicit)
|
2021-06-21 03:22:37 +02:00
|
|
|
core.status_view:remove_tooltip()
|
2019-12-28 12:16:32 +01:00
|
|
|
if explicit then
|
2021-06-21 03:22:37 +02:00
|
|
|
last_view.doc:set_selection(unpack(last_sel))
|
|
|
|
last_view:scroll_to_make_visible(unpack(last_sel))
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-07 11:55:11 +02:00
|
|
|
local function replace(kind, default, fn)
|
2020-05-09 17:09:07 +02:00
|
|
|
core.command_view:set_text(default, true)
|
2020-05-07 11:55:11 +02:00
|
|
|
|
2021-06-21 03:22:37 +02:00
|
|
|
core.status_view:show_tooltip(get_find_tooltip())
|
2020-05-02 15:45:33 +02:00
|
|
|
core.command_view:enter("Find To Replace " .. kind, function(old)
|
2020-05-09 17:09:07 +02:00
|
|
|
core.command_view:set_text(old, true)
|
2020-05-08 14:55:23 +02:00
|
|
|
|
2020-05-02 15:45:33 +02:00
|
|
|
local s = string.format("Replace %s %q With", kind, old)
|
|
|
|
core.command_view:enter(s, function(new)
|
2019-12-28 12:16:32 +01:00
|
|
|
local n = doc():replace(function(text)
|
2020-05-02 12:14:07 +02:00
|
|
|
return fn(text, old, new)
|
2019-12-28 12:16:32 +01:00
|
|
|
end)
|
2020-05-02 15:45:33 +02:00
|
|
|
core.log("Replaced %d instance(s) of %s %q with %q", n, kind, old, new)
|
2021-06-21 03:22:37 +02:00
|
|
|
end, function() end, function()
|
|
|
|
core.status_view:remove_tooltip()
|
2019-12-28 12:16:32 +01:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-05-01 11:21:57 +02:00
|
|
|
local function has_selection()
|
2021-06-21 03:22:37 +02:00
|
|
|
return core.active_view:is(DocView) and core.active_view.doc:has_selection()
|
2020-05-01 11:21:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
command.add(has_selection, {
|
|
|
|
["find-replace:select-next"] = function()
|
|
|
|
local l1, c1, l2, c2 = doc():get_selection(true)
|
|
|
|
local text = doc():get_text(l1, c1, l2, c2)
|
2021-06-21 03:22:37 +02:00
|
|
|
l1, c1, l2, c2 = search.find(doc(), l2, c2, text, { wrap = true })
|
2020-05-12 21:04:50 +02:00
|
|
|
if l2 then doc():set_selection(l2, c2, l1, c1) end
|
2020-05-01 11:21:57 +02:00
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
command.add("core.docview", {
|
|
|
|
["find-replace:find"] = function()
|
2021-06-21 03:22:37 +02:00
|
|
|
find("Find Text", function(doc, line, col, text, case_insensitive, plain)
|
|
|
|
local opt = { wrap = true, no_case = case_insensitive, regex = not plain }
|
2019-12-28 12:16:32 +01:00
|
|
|
return search.find(doc, line, col, text, opt)
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
|
2021-06-21 03:22:37 +02:00
|
|
|
["find-replace:replace"] = function()
|
2021-06-21 04:24:20 +02:00
|
|
|
replace("Text", doc():get_text(doc():get_selection(true)), function(text, old, new)
|
2021-06-21 03:22:37 +02:00
|
|
|
if plain then
|
|
|
|
return text:gsub(old:gsub("%W", "%%%1"), new:gsub("%%", "%%%%"), nil)
|
|
|
|
end
|
2021-06-21 04:24:20 +02:00
|
|
|
local result, matches = regex.gsub(regex.compile(old), text, new)
|
|
|
|
return result, #matches
|
2019-12-28 12:16:32 +01:00
|
|
|
end)
|
|
|
|
end,
|
|
|
|
|
2021-06-21 03:22:37 +02:00
|
|
|
["find-replace:replace-symbol"] = function()
|
|
|
|
local first = ""
|
|
|
|
if doc():has_selection() then
|
|
|
|
local text = doc():get_text(doc():get_selection())
|
|
|
|
first = text:match(config.symbol_pattern) or ""
|
|
|
|
end
|
|
|
|
replace("Symbol", first, function(text, old, new)
|
|
|
|
local n = 0
|
|
|
|
local res = text:gsub(config.symbol_pattern, function(sym)
|
|
|
|
if old == sym then
|
|
|
|
n = n + 1
|
|
|
|
return new
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
return res, n
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
local function valid_for_finding()
|
|
|
|
return core.active_view:is(DocView) or core.active_view:is(CommandView)
|
|
|
|
end
|
|
|
|
|
|
|
|
command.add(valid_for_finding, {
|
2019-12-28 12:16:32 +01:00
|
|
|
["find-replace:repeat-find"] = function()
|
|
|
|
if not last_fn then
|
|
|
|
core.error("No find to continue from")
|
|
|
|
else
|
2021-06-21 03:34:42 +02:00
|
|
|
local sl1, sc1, sl2, sc2 = doc():get_selection(true)
|
|
|
|
local line1, col1, line2, col2 = last_fn(doc(), sl1, sc2, last_text, case_insensitive, plain)
|
2019-12-28 12:16:32 +01:00
|
|
|
if line1 then
|
2021-06-21 03:22:37 +02:00
|
|
|
if last_view.doc ~= doc() then
|
|
|
|
last_finds = {}
|
|
|
|
end
|
|
|
|
if #last_finds >= max_last_finds then
|
|
|
|
table.remove(last_finds, 1)
|
|
|
|
end
|
2021-06-21 03:34:42 +02:00
|
|
|
table.insert(last_finds, { sl1, sc1, sl2, sc2 })
|
2019-12-28 12:16:32 +01:00
|
|
|
doc():set_selection(line2, col2, line1, col1)
|
2021-06-21 03:22:37 +02:00
|
|
|
last_view:scroll_to_line(line2, true)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
["find-replace:previous-find"] = function()
|
2021-06-21 03:22:37 +02:00
|
|
|
local sel = table.remove(last_finds)
|
|
|
|
if not sel or doc() ~= last_view.doc then
|
2019-12-28 12:16:32 +01:00
|
|
|
core.error("No previous finds")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
doc():set_selection(table.unpack(sel))
|
2021-06-21 03:22:37 +02:00
|
|
|
last_view:scroll_to_line(sel[3], true)
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
2021-06-21 03:22:37 +02:00
|
|
|
})
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-06-21 03:22:37 +02:00
|
|
|
command.add("core.commandview", {
|
|
|
|
["find-replace:toggle-insensitivity"] = function()
|
|
|
|
case_insensitive = not case_insensitive
|
|
|
|
core.status_view:show_tooltip(get_find_tooltip())
|
|
|
|
update_preview(last_sel, last_fn, last_text)
|
2020-05-02 12:14:07 +02:00
|
|
|
end,
|
|
|
|
|
2021-06-21 03:22:37 +02:00
|
|
|
["find-replace:toggle-plain"] = function()
|
|
|
|
plain = not plain
|
|
|
|
core.status_view:show_tooltip(get_find_tooltip())
|
|
|
|
update_preview(last_sel, last_fn, last_text)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
})
|