From 5671f954ddff3fb60ee6fda3fac664375be1a425 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Tue, 26 Nov 2024 14:40:35 +0100 Subject: nvim: autocmds.lua --- .config/nvim/after/plugin/autocmds.lua | 158 +++++++++++++++++++++++++++++++++ .config/nvim/after/plugin/autocmds.vim | 22 ----- 2 files changed, 158 insertions(+), 22 deletions(-) create mode 100644 .config/nvim/after/plugin/autocmds.lua (limited to '.config/nvim') diff --git a/.config/nvim/after/plugin/autocmds.lua b/.config/nvim/after/plugin/autocmds.lua new file mode 100644 index 0000000..3751cd4 --- /dev/null +++ b/.config/nvim/after/plugin/autocmds.lua @@ -0,0 +1,158 @@ +local function augroup(name) + return vim.api.nvim_create_augroup("lazyvim_" .. name, { clear = true }) +end + +-- Set typst filetype +vim.api.nvim_create_autocmd( + { + "BufNewFile", + "BufRead", + }, + { + pattern = "*.typ", + callback = function() + local buf = vim.api.nvim_get_current_buf() + vim.api.nvim_set_option_value("filetype", "typst", { buf = buf }) + vim.api.nvim_set_option_value("shiftwidth", 2, { buf = buf }) + end + } +) + +-- Set PKGBUILD filetype +vim.api.nvim_create_autocmd( + { + "BufNewFile", + "BufRead", + }, + { + pattern = "PKGBUILD", + callback = function() + local buf = vim.api.nvim_get_current_buf() + vim.api.nvim_set_option_value("filetype", "PKGBUILD", { buf = buf }) + end + } +) + +-- Set vimwiki filetype +vim.api.nvim_create_autocmd( + { + "BufNewFile", + "BufRead", + }, + { + pattern = "PKGBUILD", + callback = function() + local buf = vim.api.nvim_get_current_buf() + vim.api.nvim_set_option_value("filetype", "*.wiki", { buf = buf }) + end + } +) + +-- Set buku-edit filetype +vim.api.nvim_create_autocmd( + { + "BufNewFile", + "BufRead", + }, + { + pattern = "buku-edit-*", + callback = function() + local buf = vim.api.nvim_get_current_buf() + vim.api.nvim_set_option_value("filetype", "buku", { buf = buf }) + end + } +) + +-- Set mail filetype +vim.api.nvim_create_autocmd( + { + "BufNewFile", + "BufRead", + }, + { + pattern = "/tmp/neomutt*", + callback = function() + local buf = vim.api.nvim_get_current_buf() + vim.api.nvim_set_option_value("autoindent", false, { buf = buf }) + vim.api.nvim_set_option_value("filetype", "mail", { buf = buf }) + vim.api.nvim_set_option_value("wrapmargin", 0, { buf = buf }) + vim.api.nvim_set_option_value("textwidth", 80, { buf = buf }) + end + } +) + +-- Resize splits if window got resized +vim.api.nvim_create_autocmd({ "VimResized" }, { + group = augroup("resize_splits"), + callback = function() + local current_tab = vim.fn.tabpagenr() + vim.cmd("tabdo wincmd =") + vim.cmd("tabnext " .. current_tab) + end, +}) + +-- go to last loc when opening a buffer +vim.api.nvim_create_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].lazyvim_last_loc then + return + end + vim.b[buf].lazyvim_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, +}) + +-- close some filetypes with +vim.api.nvim_create_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", + "snacks_win", + "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, +}) + +-- Auto create dir when saving a file, in case some intermediate directory does not exist +vim.api.nvim_create_autocmd({ "BufWritePre" }, { + group = augroup("auto_create_dir"), + callback = function(event) + if event.match:match("^%w%w+:[\\/][\\/]") then + return + end + local file = vim.uv.fs_realpath(event.match) or event.match + vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p") + end, +}) diff --git a/.config/nvim/after/plugin/autocmds.vim b/.config/nvim/after/plugin/autocmds.vim index 0e7935b..ae55046 100644 --- a/.config/nvim/after/plugin/autocmds.vim +++ b/.config/nvim/after/plugin/autocmds.vim @@ -1,24 +1,2 @@ " I don't know how to port this yet autocmd BufRead,BufWrite * if ! &bin | silent! %s/\s\+$//ge | endif - -autocmd BufNewFile,BufRead /tmp/neomutt* set noautoindent filetype=mail wm=0 tw=80 - -augroup bukugroup - au! BufRead,BufNewFile,BufEnter buku-edit-* set filetype=buku -augroup END - -" Restore last position -autocmd BufReadPost * - \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit' - \ | exe "normal! g`\"" - \ | endif - -augroup pkgbuild - autocmd! - autocmd BufRead,BufNewFile PKGBUILD set filetype=PKGBUILD -augroup END - -augroup vimwiki - autocmd! - autocmd BufRead,BufNewFile *.wiki set filetype=vimwiki -augroup END -- cgit v1.2.3-70-g09d2