From 91da744d15e4335fcdfc451c811acbaa9d3e5f43 Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Wed, 26 Apr 2023 22:05:58 +0200 Subject: [PATCH] add lastplace hacke --- after/plugin/lastplace.lua | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 after/plugin/lastplace.lua diff --git a/after/plugin/lastplace.lua b/after/plugin/lastplace.lua new file mode 100644 index 0000000..5726111 --- /dev/null +++ b/after/plugin/lastplace.lua @@ -0,0 +1,45 @@ +-- adapted from https://github.com/ethanholz/nvim-lastplace/blob/main/lua/nvim-lastplace/init.lua +local ignore_buftype = { "quickfix", "nofile", "help" } +local ignore_filetype = { "gitcommit", "gitrebase" } + +local function run() + if vim.tbl_contains(ignore_buftype, vim.bo.buftype) then + return + end + + if vim.tbl_contains(ignore_filetype, vim.bo.filetype) then + -- reset cursor to first line + vim.cmd[[normal! gg]] + return + end + + -- If a line has already been specified on the command line, we are done + -- nvim file +num + if vim.fn.line(".") > 1 then + return + end + + local last_line = vim.fn.line([['"]]) + local buff_last_line = vim.fn.line("$") + + -- If the last line is set and the less than the last line in the buffer + if last_line > 0 and last_line <= buff_last_line then + local win_last_line = vim.fn.line("w$") + local win_first_line = vim.fn.line("w0") + -- Check if the last line of the buffer is the same as the win + if win_last_line == buff_last_line then + -- Set line to last line edited + vim.cmd[[normal! g`"]] + -- Try to center + elseif buff_last_line - last_line > ((win_last_line - win_first_line) / 2) - 1 then + vim.cmd[[normal! g`"zz]] + else + vim.cmd[[normal! G'"]] + end + end +end + +vim.api.nvim_create_autocmd({'BufWinEnter', 'FileType'}, { + group = vim.api.nvim_create_augroup('nvim-lastplace', {}), + callback = run +})