restore cursor position

This commit is contained in:
fiplox 2023-01-22 17:46:45 +01:00
parent 6d4aabf5a7
commit 6c7b83a960
1 changed files with 47 additions and 3 deletions

View File

@ -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('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_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', 'BufLeave', 'WinLeave' }, { pattern = { '?*' }, command = 'mkview' })
-- vim.api.nvim_create_autocmd({ 'BufWinLeave' }, { pattern = { '*' }, command = 'mkview' }) -- vim.api.nvim_create_autocmd({ 'BufWinEnter' }, { pattern = { '?*' }, command = 'silent! loadview' })
-- vim.api.nvim_create_autocmd({ 'BufReadPost' }, { pattern = { '*' }, command = 'silent! loadview' })
-- }}} -- }}}
function ToggleTroubleAuto() function ToggleTroubleAuto()
local ok, trouble = pcall(require, 'trouble') local ok, trouble = pcall(require, 'trouble')
@ -79,3 +78,48 @@ function ToggleTroubleAuto()
end, 0) end, 0)
end end
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'"<c-e>]]
end
end
end
vim.api.nvim_create_autocmd({ 'BufWinEnter', 'FileType' }, {
group = vim.api.nvim_create_augroup('nvim-lastplace', {}),
callback = run,
})