diff options
author | Yigit Sever | 2024-11-26 14:40:35 +0100 |
---|---|---|
committer | Yigit Sever | 2024-11-26 14:40:35 +0100 |
commit | 5671f954ddff3fb60ee6fda3fac664375be1a425 (patch) | |
tree | aac60061f92e47b7c83002669355776d083e293f /.config/nvim/after | |
parent | b7925fa3f4c7ddd8ddf89b6c345fcdc585a91208 (diff) | |
download | dotfiles-5671f954ddff3fb60ee6fda3fac664375be1a425.tar.gz dotfiles-5671f954ddff3fb60ee6fda3fac664375be1a425.tar.bz2 dotfiles-5671f954ddff3fb60ee6fda3fac664375be1a425.zip |
nvim: autocmds.lua
Diffstat (limited to '.config/nvim/after')
-rw-r--r-- | .config/nvim/after/plugin/autocmds.lua | 158 | ||||
-rw-r--r-- | .config/nvim/after/plugin/autocmds.vim | 22 |
2 files changed, 158 insertions, 22 deletions
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 @@ | |||
1 | local function augroup(name) | ||
2 | return vim.api.nvim_create_augroup("lazyvim_" .. name, { clear = true }) | ||
3 | end | ||
4 | |||
5 | -- Set typst filetype | ||
6 | vim.api.nvim_create_autocmd( | ||
7 | { | ||
8 | "BufNewFile", | ||
9 | "BufRead", | ||
10 | }, | ||
11 | { | ||
12 | pattern = "*.typ", | ||
13 | callback = function() | ||
14 | local buf = vim.api.nvim_get_current_buf() | ||
15 | vim.api.nvim_set_option_value("filetype", "typst", { buf = buf }) | ||
16 | vim.api.nvim_set_option_value("shiftwidth", 2, { buf = buf }) | ||
17 | end | ||
18 | } | ||
19 | ) | ||
20 | |||
21 | -- Set PKGBUILD filetype | ||
22 | vim.api.nvim_create_autocmd( | ||
23 | { | ||
24 | "BufNewFile", | ||
25 | "BufRead", | ||
26 | }, | ||
27 | { | ||
28 | pattern = "PKGBUILD", | ||
29 | callback = function() | ||
30 | local buf = vim.api.nvim_get_current_buf() | ||
31 | vim.api.nvim_set_option_value("filetype", "PKGBUILD", { buf = buf }) | ||
32 | end | ||
33 | } | ||
34 | ) | ||
35 | |||
36 | -- Set vimwiki filetype | ||
37 | vim.api.nvim_create_autocmd( | ||
38 | { | ||
39 | "BufNewFile", | ||
40 | "BufRead", | ||
41 | }, | ||
42 | { | ||
43 | pattern = "PKGBUILD", | ||
44 | callback = function() | ||
45 | local buf = vim.api.nvim_get_current_buf() | ||
46 | vim.api.nvim_set_option_value("filetype", "*.wiki", { buf = buf }) | ||
47 | end | ||
48 | } | ||
49 | ) | ||
50 | |||
51 | -- Set buku-edit filetype | ||
52 | vim.api.nvim_create_autocmd( | ||
53 | { | ||
54 | "BufNewFile", | ||
55 | "BufRead", | ||
56 | }, | ||
57 | { | ||
58 | pattern = "buku-edit-*", | ||
59 | callback = function() | ||
60 | local buf = vim.api.nvim_get_current_buf() | ||
61 | vim.api.nvim_set_option_value("filetype", "buku", { buf = buf }) | ||
62 | end | ||
63 | } | ||
64 | ) | ||
65 | |||
66 | -- Set mail filetype | ||
67 | vim.api.nvim_create_autocmd( | ||
68 | { | ||
69 | "BufNewFile", | ||
70 | "BufRead", | ||
71 | }, | ||
72 | { | ||
73 | pattern = "/tmp/neomutt*", | ||
74 | callback = function() | ||
75 | local buf = vim.api.nvim_get_current_buf() | ||
76 | vim.api.nvim_set_option_value("autoindent", false, { buf = buf }) | ||
77 | vim.api.nvim_set_option_value("filetype", "mail", { buf = buf }) | ||
78 | vim.api.nvim_set_option_value("wrapmargin", 0, { buf = buf }) | ||
79 | vim.api.nvim_set_option_value("textwidth", 80, { buf = buf }) | ||
80 | end | ||
81 | } | ||
82 | ) | ||
83 | |||
84 | -- Resize splits if window got resized | ||
85 | vim.api.nvim_create_autocmd({ "VimResized" }, { | ||
86 | group = augroup("resize_splits"), | ||
87 | callback = function() | ||
88 | local current_tab = vim.fn.tabpagenr() | ||
89 | vim.cmd("tabdo wincmd =") | ||
90 | vim.cmd("tabnext " .. current_tab) | ||
91 | end, | ||
92 | }) | ||
93 | |||
94 | -- go to last loc when opening a buffer | ||
95 | vim.api.nvim_create_autocmd("BufReadPost", { | ||
96 | group = augroup("last_loc"), | ||
97 | callback = function(event) | ||
98 | local exclude = { "gitcommit" } | ||
99 | local buf = event.buf | ||
100 | if vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].lazyvim_last_loc then | ||
101 | return | ||
102 | end | ||
103 | vim.b[buf].lazyvim_last_loc = true | ||
104 | local mark = vim.api.nvim_buf_get_mark(buf, '"') | ||
105 | local lcount = vim.api.nvim_buf_line_count(buf) | ||
106 | if mark[1] > 0 and mark[1] <= lcount then | ||
107 | pcall(vim.api.nvim_win_set_cursor, 0, mark) | ||
108 | end | ||
109 | end, | ||
110 | }) | ||
111 | |||
112 | -- close some filetypes with <q> | ||
113 | vim.api.nvim_create_autocmd("FileType", { | ||
114 | group = augroup("close_with_q"), | ||
115 | pattern = { | ||
116 | "PlenaryTestPopup", | ||
117 | "checkhealth", | ||
118 | "dbout", | ||
119 | "gitsigns-blame", | ||
120 | "grug-far", | ||
121 | "help", | ||
122 | "lspinfo", | ||
123 | "neotest-output", | ||
124 | "neotest-output-panel", | ||
125 | "neotest-summary", | ||
126 | "notify", | ||
127 | "qf", | ||
128 | "snacks_win", | ||
129 | "spectre_panel", | ||
130 | "startuptime", | ||
131 | "tsplayground", | ||
132 | }, | ||
133 | callback = function(event) | ||
134 | vim.bo[event.buf].buflisted = false | ||
135 | vim.schedule(function() | ||
136 | vim.keymap.set("n", "q", function() | ||
137 | vim.cmd("close") | ||
138 | pcall(vim.api.nvim_buf_delete, event.buf, { force = true }) | ||
139 | end, { | ||
140 | buffer = event.buf, | ||
141 | silent = true, | ||
142 | desc = "Quit buffer", | ||
143 | }) | ||
144 | end) | ||
145 | end, | ||
146 | }) | ||
147 | |||
148 | -- Auto create dir when saving a file, in case some intermediate directory does not exist | ||
149 | vim.api.nvim_create_autocmd({ "BufWritePre" }, { | ||
150 | group = augroup("auto_create_dir"), | ||
151 | callback = function(event) | ||
152 | if event.match:match("^%w%w+:[\\/][\\/]") then | ||
153 | return | ||
154 | end | ||
155 | local file = vim.uv.fs_realpath(event.match) or event.match | ||
156 | vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p") | ||
157 | end, | ||
158 | }) | ||
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 @@ | |||
1 | " I don't know how to port this yet | 1 | " I don't know how to port this yet |
2 | autocmd BufRead,BufWrite * if ! &bin | silent! %s/\s\+$//ge | endif | 2 | autocmd BufRead,BufWrite * if ! &bin | silent! %s/\s\+$//ge | endif |
3 | |||
4 | autocmd BufNewFile,BufRead /tmp/neomutt* set noautoindent filetype=mail wm=0 tw=80 | ||
5 | |||
6 | augroup bukugroup | ||
7 | au! BufRead,BufNewFile,BufEnter buku-edit-* set filetype=buku | ||
8 | augroup END | ||
9 | |||
10 | " Restore last position | ||
11 | autocmd BufReadPost * | ||
12 | \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit' | ||
13 | \ | exe "normal! g`\"" | ||
14 | \ | endif | ||
15 | |||
16 | augroup pkgbuild | ||
17 | autocmd! | ||
18 | autocmd BufRead,BufNewFile PKGBUILD set filetype=PKGBUILD | ||
19 | augroup END | ||
20 | |||
21 | augroup vimwiki | ||
22 | autocmd! | ||
23 | autocmd BufRead,BufNewFile *.wiki set filetype=vimwiki | ||
24 | augroup END | ||