-
Notifications
You must be signed in to change notification settings - Fork 13
/
autocmds.lua
111 lines (97 loc) · 3 KB
/
autocmds.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
local autocmd = vim.api.nvim_create_autocmd
-- Ibus typing
local ibus_cur = "xkb:us::eng"
autocmd("InsertEnter", {
pattern = { "*.tex", "*.md", "*.txt" },
callback = function()
os.execute("ibus engine " .. ibus_cur)
end,
})
autocmd("InsertLeave", {
pattern = { "*.tex", "*.md", "*.txt" },
callback = function()
local f = io.popen("ibus engine", "r")
if f then
ibus_cur = f:read "*a"
f:close()
end
os.execute "ibus engine xkb:us::eng"
end,
})
-- Dynamic terminal padding with/without nvim - credit: siduck
-- replace stuff from file
local function sed(from, to, fname)
vim.cmd(string.format("silent !sed -i 's/%s/%s/g' %s", from, to, fname))
end
-- reloads kitty for current focused window only
local function liveReloadKitty()
vim.cmd(
string.format "silent !kill -s SIGUSR1 $(xprop -id $(xdotool getwindowfocus) | grep '_NET_WM_PID' | grep -oE '[[:digit:]]*$')"
)
end
autocmd({ "BufNewFile", "BufRead" }, {
callback = function(ctx)
-- remove terminal padding
sed("window_margin_width 20", "window_margin_width 0", "~/.config/kitty/kitty.conf")
liveReloadKitty()
-- revert kitty margin width change but dont reload it
sed("window_margin_width 0", "window_margin_width 20", "~/.config/kitty/kitty.conf")
vim.api.nvim_del_autocmd(ctx.id)
end,
})
-- add terminal padding
autocmd("VimLeavePre", {
callback = function()
sed("window_margin_width 0", "window_margin_width 20", "~/.config/kitty/kitty.conf")
liveReloadKitty()
end,
})
-- auto reload buffer when changed
autocmd({ "FocusGained", "BufEnter", "CursorHold", "CursorHoldI" }, {
callback = function()
if vim.api.nvim_get_mode() ~= "c" then
vim.cmd "checktime"
end
end,
})
autocmd("FileChangedShellPost", {
callback = function()
vim.cmd(([[echohl WarningMsg | echomsg "%s" | echohl None]]):format "File changed on disk. Buffer reloaded.")
end,
})
autocmd("BufRead", {
pattern = { "*.inp", "*.out" },
callback = function()
vim.cmd(string.format "setlocal nornu")
end,
})
autocmd("FileType", {
pattern = "lua",
callback = function()
vim.opt.tabstop = 2
vim.opt.shiftwidth = 2
end,
})
-- setting `;` (forward) and `,` (backward) to repeat the last Lightspeed motion (s/x or f/t):
vim.g.lightspeed_last_motion = ""
local last_motion = vim.api.nvim_create_augroup("LightSpeedLastMotion", { clear = true })
vim.api.nvim_create_autocmd("User", {
pattern = "LightspeedSxEnter",
group = last_motion,
callback = function()
vim.g.lightspeed_last_motion = "sx"
end,
})
vim.api.nvim_create_autocmd("User", {
pattern = "LightspeedFtEnter",
group = last_motion,
callback = function()
vim.g.lightspeed_last_motion = "ft"
end,
})
vim.keymap.set("n", ";", function()
return (vim.g.lightspeed_last_motion == "sx" and "<Plug>Lightspeed_;_sx" or "<Plug>Lightspeed_;_ft")
end, { expr = true })
vim.keymap.set("n", ",", function()
return (vim.g.lightspeed_last_motion == "sx" and "<Plug>Lightspeed_,_sx" or "<Plug>Lightspeed_,_ft")
end, { expr = true })