From 68d50081ff9b86f481d5a5d3256f0c4b1dff94a6 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Mon, 4 Mar 2024 21:21:20 +0000 Subject: [PATCH] Simplify netrw code that moves cursor to filename --- lua/netrw.lua | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/lua/netrw.lua b/lua/netrw.lua index b70234e..067227b 100644 --- a/lua/netrw.lua +++ b/lua/netrw.lua @@ -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)