From cadc945a1cd5eca1e23fd07366afa14efe6003f6 Mon Sep 17 00:00:00 2001 From: cy Date: Sat, 4 Jan 2025 14:22:10 -0500 Subject: [PATCH] lastplace without the plugin --- home/nvim/init.lua | 47 +++++++++++++++++++++++++++++++++- home/nvim/lazy-lock.json | 7 +++-- home/nvim/lua/plugin_specs.lua | 2 -- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/home/nvim/init.lua b/home/nvim/init.lua index cb404fd..332d61d 100644 --- a/home/nvim/init.lua +++ b/home/nvim/init.lua @@ -2,6 +2,7 @@ require("plugin_specs") local keymap = vim.keymap local opt = vim.opt +local api = vim.api vim.g.loaded_netrw = 1 vim.g.loaded_netrwPlugin = 1 @@ -38,7 +39,7 @@ opt.clipboard:append("unnamedplus") vim.cmd.colorscheme("iceberg") -- restore terminal cursor on exit -vim.api.nvim_create_autocmd("VimLeave", { +api.nvim_create_autocmd("VimLeave", { callback = function() opt.guicursor = "a:ver25-blinkon500-blinkon500" end, @@ -47,6 +48,50 @@ vim.api.nvim_create_autocmd("VimLeave", { -- blinking cursor in insert mode opt.guicursor = "i-ci-ve:ver25-blinkon500-blinkon500" +-- copied from https://github.com/jdhao/nvim-config/blob/c7fb090e4ce94e72414169a247ac62f049d6b03b/lua/custom-autocmd.lua#L138 +-- Return to last cursor position when opening a file, note that here we cannot use BufReadPost +-- as event. It seems that when BufReadPost is triggered, FileType event is still not run. +-- So the filetype for this buffer is empty string. +api.nvim_create_autocmd("FileType", { + group = api.nvim_create_augroup("resume_cursor_position", { clear = true }), + pattern = "*", + callback = function(ev) + local mark_pos = api.nvim_buf_get_mark(ev.buf, '"') + local last_cursor_line = mark_pos[1] + + local max_line = vim.fn.line("$") + local buf_filetype = api.nvim_get_option_value("filetype", { buf = ev.buf }) + local buftype = api.nvim_get_option_value("buftype", { buf = ev.buf }) + + -- only handle normal files + if buf_filetype == "" or buftype ~= "" then + return + end + + -- Only resume last cursor position when there is no go-to-line command (something like '+23'). + if vim.fn.match(vim.v.argv, [[\v^\+(\d){1,}$]]) ~= -1 then + return + end + + if last_cursor_line > 1 and last_cursor_line <= max_line then + -- vim.print(string.format("mark_pos: %s", vim.inspect(mark_pos))) + -- it seems that without vim.schedule, the cursor position can not be set correctly + vim.schedule(function() + local status, result = pcall(api.nvim_win_set_cursor, 0, mark_pos) + if not status then + api.nvim_err_writeln( + string.format("Failed to resume cursor position. Context %s, error: %s", vim.inspect(ev), result) + ) + end + end) + -- the following two ways also seem to work, + -- ref: https://www.reddit.com/r/neovim/comments/104lc26/how_can_i_press_escape_key_using_lua/ + -- vim.api.nvim_feedkeys("g`\"", "n", true) + -- vim.fn.execute("normal! g`\"") + end + end, +}) + keymap.set("n", "s", require("nvim-tree.api").tree.toggle, { desc = "toggle nvim-tree", silent = true, diff --git a/home/nvim/lazy-lock.json b/home/nvim/lazy-lock.json index 76a9030..c31eaba 100644 --- a/home/nvim/lazy-lock.json +++ b/home/nvim/lazy-lock.json @@ -7,7 +7,7 @@ "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, "cmp_luasnip": { "branch": "master", "commit": "98d9cb5c2c38532bd9bdb481067b20fea8f32e90" }, "dressing.nvim": { "branch": "master", "commit": "3a45525bb182730fe462325c99395529308f431e" }, - "fzf-lua": { "branch": "main", "commit": "b27cb583c3185cc9a91f0bcb173b55ae461101c3" }, + "fzf-lua": { "branch": "main", "commit": "c773fabeec3f58b2da0fbad558f6420795a1685e" }, "gitsigns.nvim": { "branch": "main", "commit": "5f808b5e4fef30bd8aca1b803b4e555da07fc412" }, "hop.nvim": { "branch": "master", "commit": "08ddca799089ab96a6d1763db0b8adc5320bf050" }, "iceberg.vim": { "branch": "master", "commit": "23835d5ed696436f716cbfdb56a93a7850fe3b18" }, @@ -17,9 +17,8 @@ "nvim-cmp": { "branch": "main", "commit": "b555203ce4bd7ff6192e759af3362f9d217e8c89" }, "nvim-lspconfig": { "branch": "master", "commit": "a8ef5e6e497b3ebeaaf35b939c07c211563b2e05" }, "nvim-tree.lua": { "branch": "master", "commit": "68fc4c20f5803444277022c681785c5edd11916d" }, - "nvim-treesitter": { "branch": "master", "commit": "6935286b4ee3d938954e446d657eebee71b4d07a" }, + "nvim-treesitter": { "branch": "master", "commit": "cfbc1c0e0ff63e5b5e37b465b915b95fc2e98cef" }, "nvim-web-devicons": { "branch": "master", "commit": "5740b7382429d20b6ed0bbdb0694185af9507d44" }, "tokyonight.nvim": { "branch": "main", "commit": "45d22cf0e1b93476d3b6d362d720412b3d34465c" }, - "vim-commentary": { "branch": "master", "commit": "64a654ef4a20db1727938338310209b6a63f60c9" }, - "vim-lastplace": { "branch": "master", "commit": "e58cb0df716d3c88605ae49db5c4741db8b48aa9" } + "vim-commentary": { "branch": "master", "commit": "64a654ef4a20db1727938338310209b6a63f60c9" } } diff --git a/home/nvim/lua/plugin_specs.lua b/home/nvim/lua/plugin_specs.lua index aa44925..a51272e 100644 --- a/home/nvim/lua/plugin_specs.lua +++ b/home/nvim/lua/plugin_specs.lua @@ -91,8 +91,6 @@ local plugin_specs = { }, { "cocopon/iceberg.vim" }, - - { "farmergreg/vim-lastplace" }, } require("lazy").setup({