1
0
mirror of https://github.com/jdhao/nvim-config.git synced 2025-06-08 14:14:33 +02:00

Merge pull request #87 from jdhao/autocmd

simplify and add new autocmd
This commit is contained in:
jdhao 2022-09-01 13:24:05 +08:00 committed by GitHub
commit eb87c5b0c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,13 +7,9 @@ local utils = require("utils")
-- Display a message when the current file is not in utf-8 format. -- Display a message when the current file is not in utf-8 format.
-- Note that we need to use `unsilent` command here because of this issue: -- Note that we need to use `unsilent` command here because of this issue:
-- https://github.com/vim/vim/issues/4379 -- https://github.com/vim/vim/issues/4379
api.nvim_create_augroup("non_utf8_file", {
clear = true
})
api.nvim_create_autocmd({ "BufRead" }, { api.nvim_create_autocmd({ "BufRead" }, {
pattern = "*", pattern = "*",
group = "non_utf8_file", group = api.nvim_create_augroup("non_utf8_file", { clear = true }),
callback = function() callback = function()
if vim.bo.fileencoding ~= 'utf-8' then if vim.bo.fileencoding ~= 'utf-8' then
vim.notify("File not in UTF-8 format!", vim.log.levels.WARN, { title = "nvim-config" }) vim.notify("File not in UTF-8 format!", vim.log.levels.WARN, { title = "nvim-config" })
@ -22,26 +18,18 @@ api.nvim_create_autocmd({ "BufRead" }, {
}) })
-- highlight yanked region, see `:h lua-highlight` -- highlight yanked region, see `:h lua-highlight`
api.nvim_create_augroup("highlight_yank", {
clear = true
})
api.nvim_create_autocmd({ "TextYankPost" }, { api.nvim_create_autocmd({ "TextYankPost" }, {
pattern = "*", pattern = "*",
group = "highlight_yank", group = api.nvim_create_augroup("highlight_yank", { clear = true }),
callback = function() callback = function()
vim.highlight.on_yank { higroup = "YankColor", timeout = 300, on_visual = false } vim.highlight.on_yank { higroup = "YankColor", timeout = 300 }
end end
}) })
-- Auto-generate packer_compiled.lua file -- Auto-generate packer_compiled.lua file
api.nvim_create_augroup("packer_auto_compile", {
clear = true
})
api.nvim_create_autocmd({ "BufWritePost" }, { api.nvim_create_autocmd({ "BufWritePost" }, {
pattern = "*/nvim/lua/plugins.lua", pattern = "*/nvim/lua/plugins.lua",
group = "packer_auto_compile", group = api.nvim_create_augroup("packer_auto_compile", { clear = true }),
callback = function(ctx) callback = function(ctx)
local cmd = "source " .. ctx.file local cmd = "source " .. ctx.file
vim.cmd(cmd) vim.cmd(cmd)
@ -50,13 +38,9 @@ api.nvim_create_autocmd({ "BufWritePost" }, {
}) })
-- Auto-create dir when saving a file, in case some intermediate directory does not exist -- Auto-create dir when saving a file, in case some intermediate directory does not exist
api.nvim_create_augroup("auto_create_dir", {
clear = true
})
api.nvim_create_autocmd({ "BufWritePre" }, { api.nvim_create_autocmd({ "BufWritePre" }, {
pattern = "*", pattern = "*",
group = "auto_create_dir", group = api.nvim_create_augroup("auto_create_dir", { clear = true }),
callback = function(ctx) callback = function(ctx)
local dir = fn.fnamemodify(ctx.file, ":p:h") local dir = fn.fnamemodify(ctx.file, ":p:h")
utils.may_create_dir(dir) utils.may_create_dir(dir)
@ -66,9 +50,7 @@ api.nvim_create_autocmd({ "BufWritePre" }, {
-- Automatically reload the file if it is changed outside of Nvim, see https://unix.stackexchange.com/a/383044/221410. -- Automatically reload the file if it is changed outside of Nvim, see https://unix.stackexchange.com/a/383044/221410.
-- It seems that `checktime` does not work in command line. We need to check if we are in command -- It seems that `checktime` does not work in command line. We need to check if we are in command
-- line before executing this command, see also https://vi.stackexchange.com/a/20397/15292 . -- line before executing this command, see also https://vi.stackexchange.com/a/20397/15292 .
api.nvim_create_augroup("auto_read", { api.nvim_create_augroup("auto_read", { clear = true })
clear = true
})
api.nvim_create_autocmd({ "FileChangedShellPost" }, { api.nvim_create_autocmd({ "FileChangedShellPost" }, {
pattern = "*", pattern = "*",
@ -87,3 +69,10 @@ api.nvim_create_autocmd({ "FocusGained", "CursorHold" }, {
end end
end end
}) })
-- Resize all windows when we resize the terminal
api.nvim_create_autocmd("VimResized", {
group = api.nvim_create_augroup("win_autoresize", { clear = true }),
desc = "autoresize windows on resizing operation",
command = "wincmd =",
})