Simplify netrw code that moves cursor to filename

This commit is contained in:
Kenneth Benzie 2024-03-04 21:21:20 +00:00
parent 2d22ea836f
commit 68d50081ff

View File

@ -14,22 +14,14 @@ vim.keymap.set('n', '-', function()
-- Invoke netrw on the directory containing the current file
vim.fn.execute(string.format("edit %s", vim.fn.expand('%:hp')))
-- Move the cursor to the line matching file
local netrw_liststyle = vim.api.nvim_buf_get_var(
vim.api.nvim_get_current_buf(), 'netrw_liststyle')
local pattern = nil
-- TODO: Replace this with something simpler, perhaps a loop over the netrw
-- buffer lines to get the line number containing the filename string.
if netrw_liststyle and netrw_liststyle == 2 then
-- tpope's magic pattern for list style with multiple files in columns
pattern = '\\%(^\\|\\s\\+\\)\zs'
.. vim.fn.escape(filename .. '.*[]~\\')
.. '[/*|@=]\\=\\%($\\|\\s\\+\\)'
else
-- tpope's magic pattern for all other list styles
pattern = '^\\%(| \\)*'
.. vim.fn.escape(filename, '.*[]~\\')
.. '[/*|@=]\\=\\%($\\|\\t\\)'
-- In the netrw buffer, move the cursor to the first character of filename
local buffer = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, true)
for row, content in ipairs(lines) do
local column = string.find(content, filename)
if column then
vim.api.nvim_win_set_cursor(0, { row, column - 1 })
break
end
end
vim.fn.search(pattern, 'wc')
end)