tigion / dotfiles

My personal dotfiles and Neovim config
MIT License
0 stars 0 forks source link

[Neovim] Find an alternative for nvim-lastplace #3

Open tigion opened 1 year ago

tigion commented 1 year ago

Currently nvim-lastplace works, but may break on future Neovim releases.

tigion commented 1 year ago

Plugins:

Autocmd:

local lastplace = vim.api.nvim_create_augroup("LastPlace", {})
vim.api.nvim_clear_autocmds({ group = lastplace })
vim.api.nvim_create_autocmd("BufReadPost", {
    group = lastplace,
    pattern = { "*" },
    desc = "remember last cursor place",
    callback = function()
        local mark = vim.api.nvim_buf_get_mark(0, '"')
        local lcount = vim.api.nvim_buf_line_count(0)
        if mark[1] > 0 and mark[1] <= lcount then
            pcall(vim.api.nvim_win_set_cursor, 0, mark)
        end
    end,
})
local autocmd = vim.api.nvim_create_autocmd
--function(autocmd, group_id)
  autocmd("BufReadPre", {
    callback = function()
      autocmd("FileType", {
      pattern = "<buffer>",
      once = true,
      callback = function()
        local filetype = vim.opt.filetype:get()
        if (filetype == "gitcommit" or filetype == "gitrebase") then
          vim.cmd([[ exe 'normal! gg' ]])
        elseif vim.fn.line("'") > 1 and vim.fn.line("'") <= vim.fn.line("$") then
          vim.cmd([[ exe 'normal! g`"' ]])
        end
      end,
    })
    end,
    group = group_id,
    pattern = "*",
  })
--end
local cmd = vim.cmd
function save_cursor_position()
    -- Save the last cursor position.
    cmd([[ if @% !~# '\.git[\/\\]COMMIT_EDITMSG$' && line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif ]])
end

-- https://gitlab.com/FOSSilized_Daemon/dotfiles/-/blob/main/src/dotfiles/home/.config/nvim/lua/autocmd.lua
create_autocmd('BufReadPost', {
    callback = function()
        save_cursor_position()
    end,
    group = read_group })