diff options
author | 2025-09-11 16:38:11 +0100 | |
---|---|---|
committer | 2025-09-11 16:38:11 +0100 | |
commit | aa62e1f27b0cb3d712d6f2b13071cca0f09379be (patch) | |
tree | a6d2a4ba09d66490bc7c1a23f35be707fa0f5c8b /home/.config/nvim/after/plugin | |
parent | c1f310bcc39f6cf4684d938d7be45bb25b427335 (diff) | |
download | dotfiles-aa62e1f27b0cb3d712d6f2b13071cca0f09379be.tar.gz dotfiles-aa62e1f27b0cb3d712d6f2b13071cca0f09379be.tar.bz2 dotfiles-aa62e1f27b0cb3d712d6f2b13071cca0f09379be.zip |
Diffstat (limited to 'home/.config/nvim/after/plugin')
-rw-r--r-- | home/.config/nvim/after/plugin/autocmds.lua | 284 | ||||
-rw-r--r-- | home/.config/nvim/after/plugin/mappings.lua | 170 |
2 files changed, 322 insertions, 132 deletions
diff --git a/home/.config/nvim/after/plugin/autocmds.lua b/home/.config/nvim/after/plugin/autocmds.lua index 7ab937f..22cf732 100644 --- a/home/.config/nvim/after/plugin/autocmds.lua +++ b/home/.config/nvim/after/plugin/autocmds.lua @@ -1,137 +1,229 @@ local function augroup(name) - return vim.api.nvim_create_augroup(name, {}) + return vim.api.nvim_create_augroup(name, { clear = true }) end local autocmd = vim.api.nvim_create_autocmd --- adapted from https://github.com/ethanholz/nvim-lastplace/blob/main/lua/nvim-lastplace/init.lua -local ignore_buftype = { "quickfix", "nofile", "help" } -local ignore_filetype = { "gitcommit", "gitrebase", "svn", "hgcommit" } +-- Check if we need to reload the file when it changed +autocmd({ "FocusGained", "TermClose", "TermLeave" }, { + group = augroup("checktime"), + callback = function() + if vim.o.buftype ~= "nofile" then + vim.cmd("checktime") + end + end, +}) -local function run() - if vim.tbl_contains(ignore_buftype, vim.bo.buftype) then - return - end +-- Highlight on yank +autocmd("TextYankPost", { + group = augroup("highlight_yank"), + callback = vim.hl.on_yank, +}) - if vim.tbl_contains(ignore_filetype, vim.bo.filetype) then - -- reset cursor to first line - vim.cmd [[normal! gg]] - return - end +-- go to last loc when opening a buffer +autocmd("BufReadPost", { + group = augroup("last_loc"), + callback = function(event) + local exclude = { "gitcommit" } + local buf = event.buf + if + vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].last_loc + then + return + end + vim.b[buf].last_loc = true + local mark = vim.api.nvim_buf_get_mark(buf, '"') + local lcount = vim.api.nvim_buf_line_count(buf) + if mark[1] > 0 and mark[1] <= lcount then + pcall(vim.api.nvim_win_set_cursor, 0, mark) + end + 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 +-- close some filetypes with <q> +autocmd("FileType", { + group = augroup("close_with_q"), + pattern = { + "PlenaryTestPopup", + "checkhealth", + "dbout", + "gitsigns-blame", + "grug-far", + "help", + "lspinfo", + "neotest-output", + "neotest-output-panel", + "neotest-summary", + "notify", + "qf", + "spectre_panel", + "startuptime", + "tsplayground", + }, + callback = function(event) + vim.bo[event.buf].buflisted = false + vim.schedule(function() + vim.keymap.set("n", "q", function() + vim.cmd("close") + pcall(vim.api.nvim_buf_delete, event.buf, { force = true }) + end, { + buffer = event.buf, + silent = true, + desc = "Quit buffer", + }) + end) + end, +}) - local last_line = vim.fn.line([['"]]) - local buff_last_line = vim.fn.line("$") +-- make it easier to close man-files when opened inline +autocmd("FileType", { + group = augroup("man_unlisted"), + pattern = { "man" }, + callback = function(event) + vim.bo[event.buf].buflisted = false + end, +}) - -- 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>]] +-- Auto create dir when saving a file, in case some intermediate directory does not exist +autocmd({ "BufWritePre" }, { + group = augroup("auto_create_dir"), + callback = function(event) + if event.match:match("^%w%w+:[\\/][\\/]") then + return end - end -end - -augroup("restore position") -autocmd("BufReadPost", { - once = true, - group = "restore position", - callback = run + local file = vim.uv.fs_realpath(event.match) or event.match + vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p") + end, }) -augroup("postwrite") -autocmd("BufWritePost", { - group = "postwrite", - pattern = ".Xkeymap", - command = "!xkbcomp % $DISPLAY", -}) autocmd("BufWritePost", { - group = "postwrite", + group = augroup("bspwm"), pattern = "*bspwmrc", command = "!bspc wm --restart", }) autocmd("BufWritePost", { - group = "postwrite", + group = augroup("polybar"), pattern = "*/polybar/config", command = "!polybar-msg cmd restart", }) autocmd("BufWritePost", { - group = "postwrite", + group = augroup("xdg-user-dirs"), pattern = "user-dirs.dirs,user-dirs.locale", command = "!xdg-user-dirs-update", }) autocmd("BufWritePost", { - group = "postwrite", - pattern = "plugins.lua", - command = "source % | PackerSync", -}) -autocmd("BufWritePost", { - group = "postwrite", + group = augroup("dunst"), pattern = "dunstrc", command = "!killall -SIGUSR2 dunst", }) -autocmd( - "BufWritePost", - { group = "postwrite", pattern = "fonts.conf", command = "!fc-cache" } -) - -augroup("autocomplete") -autocmd("CompleteDone", { - group = "autocomplete", - command = "if pumvisible() == 0 | silent! pclose | endif", +autocmd("BufWritePost", { + group = augroup("fc-cache"), + pattern = "fonts.conf", + command = "!fc-cache", }) -augroup("reload") -autocmd("CompleteDone", { - group = "reload", - command = "if getcmdwintype() == '' | checktime | endif", -}) +autocmd("LspAttach", { + group = augroup("lsp-attach"), + callback = function(event) + local bufnr = event.buf -augroup("highlightyank") -autocmd( - "TextYankPost", - { group = "highlightyank", callback = vim.highlight.on_yank } -) + local function map(mode, l, r, desc) + vim.keymap.set(mode, l, r, { buffer = bufnr, desc = desc }) + end + local function nmap(l, r, desc) + map("n", l, r, desc) + end + local function nvmap(l, r, desc) + map({ "n", "v" }, l, r, desc) + end + nmap("<c-]>", vim.lsp.buf.definition, "Goto definition") + nmap("gD", vim.lsp.buf.declaration, "[G]oto [D]eclaration") + nvmap("gra", vim.lsp.buf.code_action, "[R]un code [A]ction") -augroup("quitro") -autocmd("BufReadPost", { - group = "quitro", - callback = function() - if vim.opt.readonly:get() then - vim.keymap.set("n", "q", "<cmd>q<cr>") + -- The following two autocommands are used to highlight references of the + -- word under your cursor when your cursor rests there for a little while. + -- See `:help CursorHold` for information about when this is executed + -- + -- When you move your cursor, the highlights will be cleared (the second autocommand). + local client = vim.lsp.get_client_by_id(event.data.client_id) + if + client + and client:supports_method( + vim.lsp.protocol.Methods.textDocument_documentHighlight, + event.buf + ) + then + local highlight_augroup = + vim.api.nvim_create_augroup("lsp-highlight", { clear = false }) + vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, { + buffer = event.buf, + group = highlight_augroup, + callback = vim.lsp.buf.document_highlight, + }) + + vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, { + buffer = event.buf, + group = highlight_augroup, + callback = vim.lsp.buf.clear_references, + }) + + vim.api.nvim_create_autocmd("LspDetach", { + group = vim.api.nvim_create_augroup("lsp-detach", { clear = true }), + callback = function(event2) + vim.lsp.buf.clear_references() + vim.api.nvim_clear_autocmds({ + group = "lsp-highlight", + buffer = event2.buf, + }) + end, + }) + end + + if + client + and client:supports_method( + vim.lsp.protocol.Methods.textDocument_codeLens, + event.buf + ) + then + vim.api.nvim_create_autocmd( + { "CursorHold", "CursorHoldI", "InsertLeave" }, + { + buffer = bufnr, + group = vim.api.nvim_create_augroup("codelens", { clear = true }), + callback = vim.lsp.codelens.refresh, + } + ) + nmap("<leader>cl", vim.lsp.codelens.run, "Run [C]ode [L]ens") + end + + -- The following code creates a keymap to toggle inlay hints in your + -- code, if the language server you are using supports them + -- + -- This may be unwanted, since they displace some of your code + if + client + and client:supports_method( + vim.lsp.protocol.Methods.textDocument_inlayHint, + event.buf + ) + then + nmap("<leader>th", function() + vim.lsp.inlay_hint.enable( + not vim.lsp.inlay_hint.is_enabled({ bufnr = event.buf }) + ) + end, "[T]oggle Inlay [H]ints") end end, }) -augroup("localinit") -autocmd("VimEnter", { - group = "localinit", +autocmd("FileType", { + group = augroup("treesitter_start"), + pattern = { "*" }, callback = function() - local settings = vim.fn.findfile(".doit.lua", ".;") - if settings ~= "" then - print("sourcing local config") - dofile(settings) + if pcall(vim.treesitter.start) then + -- vim.wo.foldexpr = "v:lua.vim.treesitter.foldexpr()" + vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" end end, }) - -augroup("restore guicursor") -autocmd("VimLeave", { - once = true, - group = "restore guicursor", - command = 'set guicursor= | call chansend(v:stderr, "\x1b[ q")' -}) diff --git a/home/.config/nvim/after/plugin/mappings.lua b/home/.config/nvim/after/plugin/mappings.lua index a2fb6cd..9eeaf19 100644 --- a/home/.config/nvim/after/plugin/mappings.lua +++ b/home/.config/nvim/after/plugin/mappings.lua @@ -1,22 +1,48 @@ -local map = require("mapper") +local function map(mode, l, r, desc) + vim.keymap.set(mode, l, r, { desc = desc }) +end +local function cmd(mode, l, r, desc) + map(mode, l, "<cmd>" .. r .. "<cr>", desc) +end +local function cmdi(mode, l, r, desc) + map(mode, l, ":" .. r, desc) +end +local function nmap(l, r, desc) + map("n", l, r, desc) +end +local function vmap(l, r, desc) + map("v", l, r, desc) +end +local function nvmap(l, r, desc) + map({ "n", "v" }, l, r, desc) +end +local function ncmd(l, r, desc) + cmd("n", l, r, desc) +end +local function ncmdi(l, r, desc) + cmdi("n", l, r, desc) +end +local function vcmdi(l, r, desc) + cmdi("v", l, r, desc) +end -map.n("<Space>", "<Nop>") +ncmd("<esc>", "nohlsearch") + +nmap("<Space>", "<Nop>") -- make an accidental ; press also enter command mode -- temporarily disabled to to vim-sneak plugin -map.n(";", ":") +nmap(";", ":") -- highlight last inserted text -map.n("gV", "`[v`]") +nmap("gV", "`[v`]") -map.n("<down>", "<c-e>") -map.n("<up>", "<c-y>") +nmap("<down>", "<c-e>") +nmap("<up>", "<c-y>") -- go to first non-blank character of current line -map.n("<c-a>", "^") -map.v("<c-a>", "^") -map.n("<c-e>", "$") -map.v("<c-e>", "$") +nvmap("<c-a>", "^") +nvmap("<c-e>", "$") -- This extends p in visual mode (note the noremap), so that if you paste from -- the unnamed (ie. default) register, that register content is not replaced by @@ -24,36 +50,108 @@ map.v("<c-e>", "$") -- This enables the user to yank some text and paste it over several places in -- a row, without using a named register -- map.v('p', "p:if v:register == '"'<Bar>let @@=@0<Bar>endif<cr>") -map.v("p", 'p:let @+=@0<CR>:let @"=@0<CR>') - -map.v("<leader>p", '"_dP') -map.n("<leader>d", '"_d') -map.n("<leader>D", '"_D') -map.map("", "<leader>c", '"_c') -map.map("", "<leader>C", '"_C') +vmap("p", 'p:let @+=@0<CR>:let @"=@0<CR>') -- Find and Replace binds -map.ncmdi("<leader>s", "%s/") -map.vcmdi("<leader>s", "s/") -map.ncmdi("<leader>gs", '%s/<c-r>"/') -map.vcmdi("<leader>gs", 's/<c-r>"/') -map.ncmdi("<Leader>S", "%s/<C-r><C-w>/") - -map.ncmd("<leader>x", "wall") -map.ncmd("<leader>z", "wqall") -map.ncmd("<leader>q", "quitall") -map.ncmd("<localleader>x", "update") +ncmdi("<localleader>s", "%s/") +vcmdi("<localleader>s", "s/") -map.n("<c-h>", "<c-w>h") -map.n("<c-j>", "<c-w>j") -map.n("<c-k>", "<c-w>k") -map.n("<c-l>", "<c-w>l") +ncmd("<leader>x", "wall") +ncmd("<leader>z", "wqall") +ncmd("<leader>q", "quitall") -map.t("<Esc>", "<c-\\><c-n>", { silent = true, noremap = true, expr = true }) +vim.keymap.set( + "t", + "<esc><esc>", + "<c-\\><c-n>", + { silent = true, noremap = true, expr = true, desc = "Exit terminal mode" } +) -map.n("[w", function() - vim.diagnostic.goto_prev({ severity = vim.diagnostic.severity.ERROR }) +nmap("[w", function() + vim.diagnostic.jump({ + count = -vim.v.count1, + severity = { min = vim.diagnostic.severity.WARN }, + }) +end) +nmap("]w", function() + vim.diagnostic.jump({ + count = vim.v.count1, + severity = { min = vim.diagnostic.severity.WARN }, + }) +end) +nmap("[e", function() + vim.diagnostic.jump({ + count = -vim.v.count1, + severity = vim.diagnostic.severity.ERROR, + }) end) -map.n("]w", function() - vim.diagnostic.goto_next({ severity = vim.diagnostic.severity.ERROR }) +nmap("]e", function() + vim.diagnostic.jump({ + count = vim.v.count1, + severity = vim.diagnostic.severity.ERROR, + }) end) + +nmap( + "<leader>oq", + vim.diagnostic.setloclist, + "[O]pen diagnostic [Q]uickfix list" +) + +nmap("yp", function() + vim.fn.setreg("+", vim.fn.expand("%")) +end, "[Y]ank [P]ath") + +local sudo_exec = function(_cmd) + vim.fn.inputsave() + local password = vim.fn.inputsecret("Password: ") + vim.fn.inputrestore() + if not password or #password == 0 then + vim.notify("Invalid password, sudo aborted", vim.log.levels.WARN) + return false + end + local out = vim.fn.system(string.format("sudo -p '' -S %s", _cmd), password) + if vim.v.shell_error ~= 0 then + print("\r\n") + vim.notify(out, vim.log.levels.ERROR) + return false + end + return true +end + +vim.api.nvim_create_user_command("SudoWrite", function(opts) + local tmpfile = vim.fn.tempname() + local filepath + if #opts.fargs == 1 then + filepath = opts.fargs[1] + else + filepath = vim.fn.expand("%") + end + if not filepath or #filepath == 0 then + vim.notify("E32: No file name", vim.log.levels.ERROR) + return + end + -- `bs=1048576` is equivalent to `bs=1M` for GNU dd or `bs=1m` for BSD dd + -- Both `bs=1M` and `bs=1m` are non-POSIX + local _cmd = string.format( + "dd if=%s of=%s bs=1048576", + vim.fn.shellescape(tmpfile), + vim.fn.shellescape(filepath) + ) + -- no need to check error as this fails the entire function + vim.api.nvim_exec2(string.format("write! %s", tmpfile), { output = true }) + if sudo_exec(_cmd) then + -- refreshes the buffer and prints the "written" message + vim.cmd.checktime() + -- exit command mode + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes("<Esc>", true, false, true), + "n", + true + ) + end + vim.fn.delete(tmpfile) +end, { + nargs = "?", + desc = "Write using sudo permissions", +}) |