summaryrefslogtreecommitdiffstats
path: root/.config
diff options
context:
space:
mode:
authorYigit Sever2024-12-16 09:55:01 +0100
committerYigit Sever2024-12-16 09:55:01 +0100
commit9cbd404440b978e435e5fc6c44cb9817ea324a4d (patch)
treed8aa5edcf3bfbecc767b27105daf2adba9c272fb /.config
parent29f04d842592a16428839bca388129bfe5fa47d2 (diff)
downloaddotfiles-9cbd404440b978e435e5fc6c44cb9817ea324a4d.tar.gz
dotfiles-9cbd404440b978e435e5fc6c44cb9817ea324a4d.tar.bz2
dotfiles-9cbd404440b978e435e5fc6c44cb9817ea324a4d.zip
nvim: move autocmds to correct file
Diffstat (limited to '.config')
-rw-r--r--.config/nvim/after/plugin/autocmds.lua145
-rw-r--r--.config/nvim/lua/helpers/autocmds.lua145
2 files changed, 145 insertions, 145 deletions
diff --git a/.config/nvim/after/plugin/autocmds.lua b/.config/nvim/after/plugin/autocmds.lua
deleted file mode 100644
index 3356a49..0000000
--- a/.config/nvim/after/plugin/autocmds.lua
+++ /dev/null
@@ -1,145 +0,0 @@
1-- https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua
2
3local function augroup(name)
4 return vim.api.nvim_create_augroup("lazyvim_" .. name, { clear = true })
5end
6
7-- Set typst filetype
8vim.api.nvim_create_autocmd(
9 {
10 "BufNewFile",
11 "BufRead",
12 },
13 {
14 pattern = "*.typ",
15 callback = function()
16 local buf = vim.api.nvim_get_current_buf()
17 vim.api.nvim_set_option_value("filetype", "typst", { buf = buf })
18 vim.api.nvim_set_option_value("shiftwidth", 2, { buf = buf })
19 end
20 }
21)
22
23-- Set PKGBUILD filetype
24vim.api.nvim_create_autocmd(
25 {
26 "BufNewFile",
27 "BufRead",
28 },
29 {
30 pattern = "PKGBUILD",
31 callback = function()
32 local buf = vim.api.nvim_get_current_buf()
33 vim.api.nvim_set_option_value("filetype", "PKGBUILD", { buf = buf })
34 end
35 }
36)
37
38-- Set buku-edit filetype
39vim.api.nvim_create_autocmd(
40 {
41 "BufNewFile",
42 "BufRead",
43 },
44 {
45 pattern = "buku-edit-*",
46 callback = function()
47 local buf = vim.api.nvim_get_current_buf()
48 vim.api.nvim_set_option_value("filetype", "buku", { buf = buf })
49 end
50 }
51)
52
53-- Set mail filetype
54vim.api.nvim_create_autocmd(
55 {
56 "BufNewFile",
57 "BufRead",
58 },
59 {
60 pattern = "/tmp/neomutt*",
61 callback = function()
62 local buf = vim.api.nvim_get_current_buf()
63 vim.api.nvim_set_option_value("autoindent", false, { buf = buf })
64 vim.api.nvim_set_option_value("filetype", "mail", { buf = buf })
65 vim.api.nvim_set_option_value("wrapmargin", 0, { buf = buf })
66 vim.api.nvim_set_option_value("textwidth", 80, { buf = buf })
67 end
68 }
69)
70
71-- Resize splits if window got resized
72vim.api.nvim_create_autocmd({ "VimResized" }, {
73 group = augroup("resize_splits"),
74 callback = function()
75 local current_tab = vim.fn.tabpagenr()
76 vim.cmd("tabdo wincmd =")
77 vim.cmd("tabnext " .. current_tab)
78 end,
79})
80
81-- go to last loc when opening a buffer
82vim.api.nvim_create_autocmd("BufReadPost", {
83 group = augroup("last_loc"),
84 callback = function(event)
85 local exclude = { "gitcommit" }
86 local buf = event.buf
87 if vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].lazyvim_last_loc then
88 return
89 end
90 vim.b[buf].lazyvim_last_loc = true
91 local mark = vim.api.nvim_buf_get_mark(buf, '"')
92 local lcount = vim.api.nvim_buf_line_count(buf)
93 if mark[1] > 0 and mark[1] <= lcount then
94 pcall(vim.api.nvim_win_set_cursor, 0, mark)
95 end
96 end,
97})
98
99-- close some filetypes with <q>
100vim.api.nvim_create_autocmd("FileType", {
101 group = augroup("close_with_q"),
102 pattern = {
103 "PlenaryTestPopup",
104 "checkhealth",
105 "dbout",
106 "gitsigns-blame",
107 "grug-far",
108 "help",
109 "lspinfo",
110 "neotest-output",
111 "neotest-output-panel",
112 "neotest-summary",
113 "notify",
114 "qf",
115 "snacks_win",
116 "spectre_panel",
117 "startuptime",
118 "tsplayground",
119 },
120 callback = function(event)
121 vim.bo[event.buf].buflisted = false
122 vim.schedule(function()
123 vim.keymap.set("n", "q", function()
124 vim.cmd("close")
125 pcall(vim.api.nvim_buf_delete, event.buf, { force = true })
126 end, {
127 buffer = event.buf,
128 silent = true,
129 desc = "Quit buffer",
130 })
131 end)
132 end,
133})
134
135-- Auto create dir when saving a file, in case some intermediate directory does not exist
136vim.api.nvim_create_autocmd({ "BufWritePre" }, {
137 group = augroup("auto_create_dir"),
138 callback = function(event)
139 if event.match:match("^%w%w+:[\\/][\\/]") then
140 return
141 end
142 local file = vim.uv.fs_realpath(event.match) or event.match
143 vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p")
144 end,
145})
diff --git a/.config/nvim/lua/helpers/autocmds.lua b/.config/nvim/lua/helpers/autocmds.lua
index 11f4480..bf2c325 100644
--- a/.config/nvim/lua/helpers/autocmds.lua
+++ b/.config/nvim/lua/helpers/autocmds.lua
@@ -29,3 +29,148 @@ vim.api.nvim_create_autocmd("VimResized", {
29 }, 29 },
30 command = "wincmd =", 30 command = "wincmd =",
31}) 31})
32
33-- https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua
34-- Set typst filetype
35vim.api.nvim_create_autocmd(
36 {
37 "BufNewFile",
38 "BufRead",
39 },
40 {
41 group = augroup("typst"),
42 pattern = "*.typ",
43 callback = function()
44 local buf = vim.api.nvim_get_current_buf()
45 vim.api.nvim_set_option_value("filetype", "typst", { buf = buf })
46 vim.api.nvim_set_option_value("shiftwidth", 2, { buf = buf })
47 end
48 }
49)
50
51-- Set PKGBUILD filetype
52vim.api.nvim_create_autocmd(
53 {
54 "BufNewFile",
55 "BufRead",
56 },
57 {
58 group = augroup("pkgbuild"),
59 pattern = "PKGBUILD",
60 callback = function()
61 local buf = vim.api.nvim_get_current_buf()
62 vim.api.nvim_set_option_value("filetype", "PKGBUILD", { buf = buf })
63 end
64 }
65)
66
67-- Set buku-edit filetype
68vim.api.nvim_create_autocmd(
69 {
70 "BufNewFile",
71 "BufRead",
72 },
73 {
74 group = augroup("buku-edit"),
75 pattern = "buku-edit-*",
76 callback = function()
77 local buf = vim.api.nvim_get_current_buf()
78 vim.api.nvim_set_option_value("filetype", "buku", { buf = buf })
79 end
80 }
81)
82
83-- Set mail filetype
84vim.api.nvim_create_autocmd(
85 {
86 "BufNewFile",
87 "BufRead",
88 },
89 {
90 group = augroup("mail"),
91 pattern = "/tmp/neomutt*",
92 callback = function()
93 local buf = vim.api.nvim_get_current_buf()
94 vim.api.nvim_set_option_value("autoindent", false, { buf = buf })
95 vim.api.nvim_set_option_value("filetype", "mail", { buf = buf })
96 vim.api.nvim_set_option_value("wrapmargin", 0, { buf = buf })
97 vim.api.nvim_set_option_value("textwidth", 80, { buf = buf })
98 end
99 }
100)
101
102-- Resize splits if window got resized
103vim.api.nvim_create_autocmd({ "VimResized" }, {
104 group = augroup("resize_splits"),
105 callback = function()
106 local current_tab = vim.fn.tabpagenr()
107 vim.cmd("tabdo wincmd =")
108 vim.cmd("tabnext " .. current_tab)
109 end,
110})
111
112-- go to last loc when opening a buffer
113vim.api.nvim_create_autocmd("BufReadPost", {
114 group = augroup("last_loc"),
115 callback = function(event)
116 local exclude = { "gitcommit" }
117 local buf = event.buf
118 if vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].lazyvim_last_loc then
119 return
120 end
121 vim.b[buf].lazyvim_last_loc = true
122 local mark = vim.api.nvim_buf_get_mark(buf, '"')
123 local lcount = vim.api.nvim_buf_line_count(buf)
124 if mark[1] > 0 and mark[1] <= lcount then
125 pcall(vim.api.nvim_win_set_cursor, 0, mark)
126 end
127 end,
128})
129
130-- close some filetypes with <q>
131vim.api.nvim_create_autocmd("FileType", {
132 group = augroup("close_with_q"),
133 pattern = {
134 "PlenaryTestPopup",
135 "checkhealth",
136 "dbout",
137 "gitsigns-blame",
138 "grug-far",
139 "help",
140 "lspinfo",
141 "neotest-output",
142 "neotest-output-panel",
143 "neotest-summary",
144 "notify",
145 "qf",
146 "snacks_win",
147 "spectre_panel",
148 "startuptime",
149 "tsplayground",
150 },
151 callback = function(event)
152 vim.bo[event.buf].buflisted = false
153 vim.schedule(function()
154 vim.keymap.set("n", "q", function()
155 vim.cmd("close")
156 pcall(vim.api.nvim_buf_delete, event.buf, { force = true })
157 end, {
158 buffer = event.buf,
159 silent = true,
160 desc = "Quit buffer",
161 })
162 end)
163 end,
164})
165
166-- Auto create dir when saving a file, in case some intermediate directory does not exist
167vim.api.nvim_create_autocmd({ "BufWritePre" }, {
168 group = augroup("auto_create_dir"),
169 callback = function(event)
170 if event.match:match("^%w%w+:[\\/][\\/]") then
171 return
172 end
173 local file = vim.uv.fs_realpath(event.match) or event.match
174 vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p")
175 end,
176})