From 6c7b83a9607cd9e929d79bef1ae947fc2ddcbe10 Mon Sep 17 00:00:00 2001 From: fiplox Date: Sun, 22 Jan 2023 17:46:45 +0100 Subject: [PATCH] restore cursor position --- lua/config/settings.lua | 50 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/lua/config/settings.lua b/lua/config/settings.lua index 354edf0..459a977 100644 --- a/lua/config/settings.lua +++ b/lua/config/settings.lua @@ -66,9 +66,8 @@ vim.api.nvim_create_user_command('Pdf', '!pandoc % -o %:r.pdf', { nargs = 0 }) vim.api.nvim_create_user_command('Slide', '!pandoc -t beamer % -o %:r.pdf', { nargs = 0 }) vim.api.nvim_create_user_command('Cd', 'lcd %:p:h', { nargs = 0 }) --- vim.api.nvim_create_autocmd({ 'BufReadPost' }, { pattern = { '*' }, command = 'silent! g`"zz' }) --- vim.api.nvim_create_autocmd({ 'BufWinLeave' }, { pattern = { '*' }, command = 'mkview' }) --- vim.api.nvim_create_autocmd({ 'BufReadPost' }, { pattern = { '*' }, command = 'silent! loadview' }) +-- vim.api.nvim_create_autocmd({ 'BufWinLeave', 'BufLeave', 'WinLeave' }, { pattern = { '?*' }, command = 'mkview' }) +-- vim.api.nvim_create_autocmd({ 'BufWinEnter' }, { pattern = { '?*' }, command = 'silent! loadview' }) -- }}} function ToggleTroubleAuto() local ok, trouble = pcall(require, 'trouble') @@ -79,3 +78,48 @@ function ToggleTroubleAuto() end, 0) end end + +local ignore_buftype = { 'quickfix', 'nofile', 'help' } +local ignore_filetype = { 'gitcommit', 'gitrebase', 'svn', 'hgcommit' } + +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, +})