From e8e1563aad6d3f22f8c87f0d3c19b001eb14b714 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Tue, 3 May 2022 09:10:59 +0200 Subject: vim: convert autocmd to Lua, better restore position on open --- dotfiles/vim.lua | 71 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/dotfiles/vim.lua b/dotfiles/vim.lua index 619cb9b..95ba8a1 100644 --- a/dotfiles/vim.lua +++ b/dotfiles/vim.lua @@ -3,9 +3,9 @@ vim.o.undofile = true vim.o.backup = true -vim.opt.backupdir:remove(".") +vim.opt.backupdir:remove "." -vim.opt.shortmess:append("c") +vim.opt.shortmess:append "c" vim.o.mouse = "a" @@ -59,15 +59,56 @@ vim.g.mapleader = ";" -- From neovim#14420 -- Restores the position of previously opened files -vim.cmd [[ - augroup LastCursorPos - autocmd! - autocmd BufReadPost * if @% !~# "\.git[\/\\]COMMIT_EDITMSG$" && line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif - augroup end -]] --- Highlight yanked text -vim.cmd "autocmd TextYankPost * silent! lua vim.highlight.on_yank()" +-- Inspired by: https://github.com/ethanholz/nvim-lastplace + +local last_cursor_pos_augroup = vim.api.nvim_create_augroup("LastCursorPos", {}) +vim.api.nvim_create_autocmd("BufReadPost", { + group = last_cursor_pos_augroup, + desc = "Restore the position of previously opened files", + callback = function(opts) + if vim.tbl_contains({ "quickfix", "nofile", "help" }, vim.bo.buftype) then + return + end + + if vim.tbl_contains({ "gitcommit", "gitrebase", "svn", "hgcommit" }, vim.bo.filetype) then + 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 + + -- If the last line is set and the less than the last line in the buffer + if vim.fn.line [['"]] > 0 and vim.fn.line [['"]] <= vim.fn.line "$" then + if vim.fn.line "w$" == vim.fn.line "$" then + -- if the last line in the current buffer is also the last line visible + -- in this window + vim.cmd [[normal! g`"]] + elseif vim.fn.line "$" - vim.fn.line [['"]] > ((vim.fn.line "w$" - vim.fn.line "w0") / 2) - 1 then + -- if we're not at the bottom of the file, center the cursor on the + -- screen after we make the jump + vim.cmd [[normal! g`"zz]] + else + -- otherwise, show as much context as we can by jumping to the end of + -- the file and then to the mark. If we pressed zz here, there would be + -- blank lines at the bottom of the screen. We intentionally leave the + -- last line blank by pressing so the user has a clue that they + -- are near the end of the file. + vim.cmd [[normal! G'"]] + end + end + end, +}) + +vim.api.nvim_create_autocmd("TextYankPost", { + desc = "Highlight yanked text", + callback = function(opts) + vim.highlight.on_yank() + end, +}) vim.g.tex_flavor = "latex" @@ -211,6 +252,7 @@ require("nvim-treesitter.configs").setup { vim.o.foldmethod = "expr" vim.o.foldexpr = "nvim_treesitter#foldexpr()" +vim.opt.foldopen = { "all" } vim.api.nvim_set_hl(0, "TSCurrentScope", { bg = vim.g.current_gruvbox_colors.dark0_soft[1], @@ -337,7 +379,14 @@ vim.keymap.set( -- Text yanked into the "t register gets copied using OSC52 escape sequences -- (e.g. goes through SSH) -vim.cmd "autocmd TextYankPost * if v:event.regname is 't' | OSCYankReg t | endif" +vim.api.nvim_create_autocmd("TextYankPost", { + desc = "Setup for OSCYank", + callback = function(opts) + if vim.v.event.regname == "t" then + vim.cmd [[OSCYankReg t]] + end + end, +}) -- Diffview -- cgit v1.2.3