218 lines
6.2 KiB
Lua
218 lines
6.2 KiB
Lua
local fn = vim.fn
|
|
local api = vim.api
|
|
|
|
local utils = require("utils")
|
|
|
|
-- Display a message when the current file is not in utf-8 format
|
|
api.nvim_create_autocmd({ "BufRead" }, {
|
|
pattern = "*",
|
|
group = api.nvim_create_augroup("non_utf8_file", { clear = true }),
|
|
callback = function()
|
|
if vim.bo.fileencoding ~= "utf-8" then
|
|
vim.notify("File not in UTF-8 format!", vim.log.levels.WARN, { title = "nvim-config" })
|
|
end
|
|
end,
|
|
})
|
|
|
|
-- Highlight yanked region
|
|
local yank_group = api.nvim_create_augroup("highlight_yank", { clear = true })
|
|
api.nvim_create_autocmd({ "TextYankPost" }, {
|
|
pattern = "*",
|
|
group = yank_group,
|
|
callback = function()
|
|
vim.hl.on_yank { higroup = "YankColor", timeout = 300 }
|
|
end,
|
|
})
|
|
|
|
api.nvim_create_autocmd({ "CursorMoved" }, {
|
|
pattern = "*",
|
|
group = yank_group,
|
|
callback = function()
|
|
vim.g.current_cursor_pos = vim.fn.getcurpos()
|
|
end,
|
|
})
|
|
|
|
api.nvim_create_autocmd("TextYankPost", {
|
|
pattern = "*",
|
|
group = yank_group,
|
|
---@diagnostic disable-next-line: unused-local
|
|
callback = function(ctx)
|
|
if vim.v.event.operator == "y" then
|
|
vim.fn.setpos(".", vim.g.current_cursor_pos)
|
|
end
|
|
end,
|
|
})
|
|
|
|
-- Auto-create dir when saving a file in case some intermediate directory does not exist
|
|
api.nvim_create_autocmd({ "BufWritePre" }, {
|
|
pattern = "*",
|
|
group = api.nvim_create_augroup("auto_create_dir", { clear = true }),
|
|
callback = function(ctx)
|
|
local dir = fn.fnamemodify(ctx.file, ":p:h")
|
|
utils.may_create_dir(dir)
|
|
end,
|
|
})
|
|
|
|
-- Automatically reload the file if it is changed outside of nvim
|
|
-- It seems that `checktime` does not work in command line, so we need to check if we are in
|
|
-- command line before executing this command
|
|
local auto_read_group = api.nvim_create_augroup("auto_read", { clear = true })
|
|
|
|
api.nvim_create_autocmd({ "FileChangedShellPost" }, {
|
|
pattern = "*",
|
|
group = auto_read_group,
|
|
callback = function()
|
|
vim.notify("File changed on disk. Buffer reloaded!", vim.log.levels.WARN, { title = "nvim-config" })
|
|
end,
|
|
})
|
|
|
|
api.nvim_create_autocmd({ "FocusGained", "CursorHold" }, {
|
|
pattern = "*",
|
|
group = auto_read_group,
|
|
callback = function()
|
|
if fn.getcmdwintype() == "" then
|
|
vim.cmd("checktime")
|
|
end
|
|
end,
|
|
})
|
|
|
|
-- Resize all windows when we resize the terminal
|
|
api.nvim_create_autocmd("VimResized", {
|
|
group = api.nvim_create_augroup("win_autoresize", { clear = true }),
|
|
command = "wincmd =",
|
|
})
|
|
|
|
local function open_nvim_tree(data)
|
|
local is_directory = vim.fn.isdirectory(data.file) == 1
|
|
|
|
if not directory then
|
|
return
|
|
end
|
|
|
|
vim.cmd.enew()
|
|
vim.cmd.bw(data.buf)
|
|
require("nvim-tree.api").tree.open()
|
|
end
|
|
|
|
api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree })
|
|
|
|
-- Do not use smart case in command line mode
|
|
api.nvim_create_augroup("dynamic_smartcase", { clear = true })
|
|
api.nvim_create_autocmd("CmdLineEnter", {
|
|
group = "dynamic_smartcase",
|
|
pattern = ":",
|
|
callback = function()
|
|
vim.o.smartcase = false
|
|
end,
|
|
})
|
|
|
|
api.nvim_create_autocmd("CmdLineLeave", {
|
|
group = "dynamic_smartcase",
|
|
pattern = ":",
|
|
callback = function()
|
|
vim.o.smartcase = true
|
|
end,
|
|
})
|
|
|
|
api.nvim_create_autocmd("TermOpen", {
|
|
group = api.nvim_create_augroup("term_start", { clear = true }),
|
|
pattern = "*",
|
|
callback = function()
|
|
vim.wo.relativenumber = false
|
|
vim.wo.number = false
|
|
vim.cmd("startinsert")
|
|
end,
|
|
})
|
|
|
|
local number_toggle_group = api.nvim_create_augroup("numbertoggle", { clear = true })
|
|
api.nvim_create_autocmd({ "BufEnter", "FocusGained", "InsertLeave", "WinEnter" }, {
|
|
pattern = "*",
|
|
group = number_toggle_group,
|
|
callback = function()
|
|
if vim.wo.number then
|
|
vim.wo.relativenumber = true
|
|
end
|
|
end,
|
|
})
|
|
|
|
api.nvim_create_autocmd({ "BufLeave", "FocusLost", "InsertEnter", "WinLeave" }, {
|
|
group = number_toggle_group,
|
|
callback = function()
|
|
if vim.wo.number then
|
|
vim.wo.relativenumber = false
|
|
end
|
|
end,
|
|
})
|
|
|
|
api.nvim_create_autocmd("ColorScheme", {
|
|
group = api.nvim_create_augroup("custom_highlight", { clear = true }),
|
|
pattern = "*",
|
|
desc = "Define or override some highlight groups",
|
|
callback = function()
|
|
vim.api.nvim_set_hl(0, "YankColor", { fg = "#34495E", bg = "#2ECC71", ctermfg = 59, ctermbg = 41 })
|
|
vim.api.nvim_set_hl(0, "Cursor", { fg = "black", bg = "#00C918", bold = true })
|
|
vim.api.nvim_set_hl(0, "Cursor2", { fg = "red", bg = "red" })
|
|
vim.api.nvim_set_hl(0, "FloatBorder", { fg = "LightGreen", bg = "None", bold = true })
|
|
|
|
local hl = vim.api.nvim_get_hl(0, { name = "NormalFloat" })
|
|
vim.api.nvim_set_hl(0, "NormalFloat", { fg = hl.fg, bg = "None" })
|
|
|
|
vim.api.nvim_set_hl(0, "MatchParen", { bold = true, underline = true })
|
|
end,
|
|
})
|
|
|
|
api.nvim_create_autocmd("BufEnter", {
|
|
pattern = "*",
|
|
group = api.nvim_create_augroup("auto_close_win", { clear = true }),
|
|
desc = "Quit nvim if we have only one window and its filetype matches our pattern",
|
|
---@diagnostic disable-next-line: unused-local
|
|
callback = function(ctx)
|
|
local quit_filetypes = { "qf", "vista", "NvimTree" }
|
|
local should_quit = true
|
|
local tabwins = api.nvim_tabpage_list_wins(0)
|
|
|
|
for _, win in pairs(tabwins) do
|
|
local buf = api.nvim_win_get_buf(win)
|
|
local buf_type = vim.api.nvim_get_option_value("filetype", { buf = buf })
|
|
|
|
if not vim.tbl_contains(quit_filetypes, buf_type) then
|
|
should_quit = false
|
|
end
|
|
end
|
|
|
|
if should_quit then
|
|
vim.cmd("qall")
|
|
end
|
|
end,
|
|
})
|
|
|
|
api.nvim_create_autocmd({ "VimEnter", "DirChanged" }, {
|
|
group = api.nvim_create_augroup("git_repo_check", { clear = true }),
|
|
pattern = "*",
|
|
desc = "Check if we are inside a git repo",
|
|
callback = function()
|
|
utils.inside_git_repo()
|
|
end,
|
|
})
|
|
|
|
-- ref: https://vi.stackexchange.com/a/169/15292
|
|
api.nvim_create_autocmd("BufReadPre", {
|
|
group = api.nvim_create_augroup("large_file", { clear = true }),
|
|
pattern = "*",
|
|
desc = "Optimize for large files",
|
|
callback = function(ctx)
|
|
local file_size_limit = 524288 -- 0.5MB
|
|
local f = ctx.file
|
|
|
|
if fn.getfsize(f) > file_size_limit or fn.getfsize(f) == -2 then
|
|
vim.o.eventignore = "all"
|
|
vim.o.ruler = true
|
|
vim.wo.relativenumber = false
|
|
vim.wo.number = false
|
|
vim.bo.swapfile = false
|
|
vim.bo.bufhidden = "unload"
|
|
vim.bo.undolevels = -1
|
|
end
|
|
end,
|
|
})
|