smart_n.lua
Branches > main > 4d3a32c > files / nvim / lua / smart_n.lua
--- License: 0BSD
--- In case you use something else :p
FT_MOTIONS = {"f", "F", "t", "T"}
SEARCH = "search"
FT = "f/t"
--- Holds the last type of search performed
--- Either SEARCH, FT, or nil
vim.b.last_n_type = nil
vim.api.nvim_create_autocmd("CmdlineLeave", {
callback = function()
local t = vim.fn.getcmdtype()
if t == "/" or t == "?" then
vim.b.last_n_type = SEARCH
end
end})
for _, key in ipairs(FT_MOTIONS) do
vim.keymap.set({"n", "x", "o"}, key, function()
vim.b.last_n_type = FT
return key
end, {expr = true, noremap = true})
end
local function smart_n(reverse, not_smart)
if vim.b.last_n_type == nil then
return ""
elseif vim.b.last_n_type == FT then
local cs = vim.fn.getcharsearch()
if reverse then
return (not_smart and ",") or cs.forward == 1 and "," or ";"
else
return (not_smart and ";") or cs.forward == 1 and ";" or ","
end
elseif vim.b.last_n_type == SEARCH then
if reverse then
return (not_smart and "N") or vim.v.searchforward == 1 and "N" or "n"
else
return (not_smart and "n") or vim.v.searchforward == 1 and "n" or "N"
end
end
end
--- Goes based on search direction which is dumb but suit yourself :p
local function not_smart_n(reverse)
return smart_n(reverse, true)
end
--- Keybindings
-- vim.keymap.set({"n", "v", "o"}, "n", function() return smart_n(false) end, {expr = true, noremap = true})
-- vim.keymap.set({"n", "v", "o"}, "N", function() return smart_n(true) end, {expr = true, noremap = true})
--- Or lazily from another file
-- vim.keymap.set({"n", "v", "o"}, "n", function() return require("smart_n").smart_n(false) end, {expr = true, noremap = true})
-- vim.keymap.set({"n", "v", "o"}, "N", function() return require("smart_n").smart_n(true) end, {expr = true, noremap = true})
return {smart_n = smart_n,
not_smart_n = not_smart_n}