-
Notifications
You must be signed in to change notification settings - Fork 14
/
autocmd.lua
307 lines (290 loc) · 9.98 KB
/
autocmd.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
local utils = require("utils")
local aucmd = vim.api.nvim_create_autocmd
local function augroup(name, fnc)
fnc(vim.api.nvim_create_augroup(name, { clear = true }))
end
augroup("ibhagwan/FzfLuaCtrlC", function(g)
aucmd("FileType",
{
group = g,
pattern = "fzf",
callback = function(e)
vim.keymap.set("t", "<C-c>", "<C-c>", { buffer = e.buf, silent = true })
end,
})
end)
if utils.is_root() then
augroup("ibhagwan/SmartTextYankPost", function(g)
-- highlight yanked text and copy to system clipboard
-- TextYankPost is also called on deletion, limit to
-- yanks via v:operator
-- if we are connected over ssh also copy using OSC52
aucmd("TextYankPost", {
group = g,
pattern = "*",
-- command = "if has('clipboard') && v:operator=='y' && len(@0)>0 | "
-- .. "let @+=@0 | endif | "
-- .. "lua vim.highlight.on_yank{higroup='IncSearch', timeout=2000}"
desc = "Copy to clipboard/tmux/OSC52",
callback = function()
local ok, yank_data = pcall(vim.fn.getreg, "0")
local valid_yank = ok and #yank_data > 0 and vim.v.operator == "y"
if valid_yank and vim.fn.has("clipboard") == 1 then
pcall(vim.fn.setreg, "+", yank_data)
end
-- $SSH_CONNECTION doesn't pass over to
-- root when using `su -`, copy indiscriminately
if valid_yank and (vim.env.SSH_CONNECTION or utils.is_root()) then
utils.osc52printf(yank_data)
end
if valid_yank and vim.env.TMUX then
-- we use `-w` to also copy to client's clipboard
vim.fn.system({ "tmux", "set-buffer", "-w", yank_data })
end
vim.highlight.on_yank({ higroup = "IncSearch", timeout = 1000 })
end
})
end)
end
augroup("ibhagwan/StatusLineColors", function(g)
aucmd("ColorScheme",
{
group = g,
callback = function(_)
-- fix 'listchars' highlight on nightfly
if vim.g.colors_name == "nightfly" then
vim.api.nvim_set_hl(0, "Whitespace", { default = false, link = "NonText" })
vim.api.nvim_set_hl(0, "FzfLuaDirPart", { default = false, link = "NightFlyGreyBlue" })
end
-- update heirline highlights, only do this after
-- statusline is loaded or we lose the :intro screen
if package.loaded.heirline then
local get_colors = require("plugins.heirline")._get_colors
require("heirline.utils").on_colorscheme(get_colors)
end
-- treesitter context
vim.api.nvim_set_hl(0, "TreesitterContext", { default = false, link = "Visual" })
vim.api.nvim_set_hl(0, "TreesitterContextBottom", { default = false, underline = true })
-- render-markdown
vim.api.nvim_set_hl(0, "RenderMarkdownCode", { default = false, link = "DiffChange" })
vim.api.nvim_set_hl(0, "RenderMarkdownH1Bg", { default = false, link = "Visual" })
vim.api.nvim_set_hl(0, "RenderMarkdownH2Bg", { default = false, link = "DiffText" })
vim.api.nvim_set_hl(0, "RenderMarkdownH3Bg", { default = false, link = "DiffAdd" })
vim.api.nvim_set_hl(0, "RenderMarkdownH4Bg", { default = false, link = "DiffAdd" })
vim.api.nvim_set_hl(0, "RenderMarkdownH5Bg", { default = false, link = "DiffAdd" })
vim.api.nvim_set_hl(0, "RenderMarkdownH6Bg", { default = false, link = "DiffAdd" })
end,
})
end)
-- disable mini.indentscope for certain filetype|buftype
augroup("ibhagwan/MiniIndentscopeDisable", function(g)
aucmd("BufEnter", {
group = g,
callback = function(_)
if vim.bo.filetype == "fzf"
or vim.bo.filetype == "help"
or vim.bo.buftype == "nofile"
or vim.bo.buftype == "terminal"
then
vim.b.miniindentscope_disable = true
end
end,
})
end)
augroup("ibhagwan/TermOptions", function(g)
aucmd("TermOpen",
{
group = g,
command = "setlocal listchars= nonumber norelativenumber"
})
end)
augroup("ibhagwan/ResizeWindows", function(g)
aucmd("VimResized",
{
group = g,
command = "tabdo wincmd ="
})
end)
augroup("ibhagwan/ToggleColorcolumn", function(g)
aucmd({ "VimResized", "WinEnter", "BufWinEnter" }, {
group = g,
callback = require("utils").toggle_colorcolumn
})
end)
augroup("ibhagwan/ToggleSearchHL", function(g)
aucmd("InsertEnter", {
group = g,
callback = function()
vim.schedule(function() vim.cmd("nohlsearch") end)
end
})
aucmd("CursorMoved", {
group = g,
callback = function()
--[[ -- No bloat lua adpatation of: https://github.com/romainl/vim-cool
local view, rpos = vim.fn.winsaveview(), vim.fn.getpos(".")
assert(view.lnum == rpos[2])
assert(view.col + 1 == rpos[3])
-- Move the cursor to a position where (whereas in active search) pressing `n`
-- brings us to the original cursor position, in a forward search / that means
-- one column before the match, in a backward search ? we move one col forward
vim.cmd(string.format("silent! keepjumps go%s",
(vim.fn.line2byte(view.lnum) + view.col + 1 - (vim.v.searchforward == 1 and 2 or 0))))
-- Attempt to goto next match, if we're in an active search cursor position
-- should be equal to original cursor position
local ok, _ = pcall(vim.cmd, "silent! keepjumps norm! n")
local insearch = ok and (function()
local npos = vim.fn.getpos(".")
return npos[2] == rpos[2] and npos[3] == rpos[3]
end)()
-- restore original view and position
vim.fn.winrestview(view)
if not insearch then
vim.schedule(function() vim.cmd("nohlsearch") end)
end ]]
if vim.v.hlsearch == 1 and vim.fn.searchcount().exact_match == 0 then
vim.schedule(function() vim.cmd.nohlsearch() end)
end
end
})
end)
augroup("ibhagwan/ActiveWinCursorLine", function(g)
-- Highlight current line only on focused window
aucmd({ "WinEnter", "BufEnter", "InsertLeave" }, {
group = g,
command = "if ! &cursorline && ! &pvw | setlocal cursorline | endif"
})
aucmd({ "WinLeave", "BufLeave", "InsertEnter" }, {
group = g,
command = "if &cursorline && ! &pvw | setlocal nocursorline | endif"
})
end)
-- goto last location when opening a buffer
augroup("ibhagwan/BufLastLocation", function(g)
aucmd("BufReadPost", {
group = g,
callback = function(e)
-- skip fugitive commit message buffers
local bufname = vim.api.nvim_buf_get_name(e.buf)
if bufname:match("COMMIT_EDITMSG$") then return end
local mark = vim.api.nvim_buf_get_mark(e.buf, '"')
local line_count = vim.api.nvim_buf_line_count(e.buf)
if mark[1] > 0 and mark[1] <= line_count then
vim.cmd 'normal! g`"zz'
end
end,
})
end)
-- auto-delete fugitive buffers
augroup("ibhagwan/Fugitive", function(g)
aucmd("BufReadPost", {
group = g,
pattern = "fugitive:*",
command = "set bufhidden=delete"
})
end)
-- Solidity abi JSON
augroup("ibhagwan/SolidityABI", function(g)
aucmd({ "BufRead", "BufNewFile" }, {
group = g,
pattern = "*.abi",
command = "set filetype=jsonc"
})
end)
-- Display help|man in vertical splits and map 'q' to quit
augroup("ibhagwan/Help", function(g)
local function open_vert()
-- do nothing for floating windows or if this is
-- the fzf-lua minimized help window (height=1)
local cfg = vim.api.nvim_win_get_config(0)
if cfg and (cfg.external or cfg.relative and #cfg.relative > 0)
or vim.api.nvim_win_get_height(0) == 1 then
return
end
-- do not run if Diffview is open
if vim.g.diffview_nvim_loaded and
require "diffview.lib".get_current_view() then
return
end
local width = math.floor(vim.o.columns * 0.75)
vim.cmd("wincmd L")
vim.cmd("vertical resize " .. width)
vim.keymap.set("n", "q", "<CMD>q<CR>", { buffer = true })
end
aucmd("FileType", {
group = g,
pattern = "help,man",
callback = open_vert,
})
-- we also need this auto command or help
-- still opens in a split on subsequent opens
aucmd("BufEnter", {
group = g,
pattern = "*.txt",
callback = function()
if vim.bo.buftype == "help" then
open_vert()
end
end
})
aucmd("BufHidden", {
group = g,
pattern = "man://*",
callback = function()
if vim.bo.filetype == "man" then
local bufnr = vim.api.nvim_get_current_buf()
vim.defer_fn(function()
if vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_buf_delete(bufnr, { force = true })
end
end, 0)
end
end
})
end)
-- https://vim.fandom.com/wiki/Avoid_scrolling_when_switch_buffers
augroup("ibhagwan/DoNotAutoScroll", function(g)
aucmd("BufLeave", {
group = g,
desc = "Avoid autoscroll when switching buffers",
callback = function()
-- at this stage, current buffer is the buffer we leave
-- but the current window already changed, verify neither
-- source nor destination are floating windows
local from_buf = vim.api.nvim_get_current_buf()
local from_win = vim.fn.bufwinid(from_buf)
local to_win = vim.api.nvim_get_current_win()
if not utils.win_is_float(to_win) and not utils.win_is_float(from_win) then
vim.b.__VIEWSTATE = vim.fn.winsaveview()
end
end
})
aucmd("BufEnter", {
group = g,
desc = "Avoid autoscroll when switching buffers",
callback = function()
if vim.b.__VIEWSTATE then
local to_win = vim.api.nvim_get_current_win()
if not utils.win_is_float(to_win) then
vim.fn.winrestview(vim.b.__VIEWSTATE)
end
vim.b.__VIEWSTATE = nil
end
end
})
end)
augroup("ibhagwan/GQFormatter", function(g)
aucmd({ "FileType", "LspAttach" },
{
group = g,
callback = function(e)
-- execlude diffview and vim-fugitive
if vim.bo.filetype == "fugitive"
or e.file:match("^fugitive:")
or require("plugins.diffview")._is_open() then
return
end
require("plugins.conform")._set_gq_keymap(e)
end,
})
end)