From 7a7c6dbcdfd2ebd6437740e829d05eaa770523cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charlotte=20=F0=9F=A6=9D=20Delenk?= Date: Sat, 15 Apr 2023 08:55:47 +0100 Subject: [PATCH] add autocmds --- config/programs/vim/configuration.nix | 1 + .../vim/modules/lua/custom_autocmd.lua | 66 +++++++++++++++++++ config/programs/vim/modules/lua/utils.lua | 59 +++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 config/programs/vim/modules/lua/custom_autocmd.lua create mode 100644 config/programs/vim/modules/lua/utils.lua diff --git a/config/programs/vim/configuration.nix b/config/programs/vim/configuration.nix index 83b1b847..1f706049 100644 --- a/config/programs/vim/configuration.nix +++ b/config/programs/vim/configuration.nix @@ -89,6 +89,7 @@ in { extraLuaModules = [ "config.undodir" + "custom_autocmd" ]; vim.g.isDesktop = config.isDesktop; diff --git a/config/programs/vim/modules/lua/custom_autocmd.lua b/config/programs/vim/modules/lua/custom_autocmd.lua new file mode 100644 index 00000000..09819922 --- /dev/null +++ b/config/programs/vim/modules/lua/custom_autocmd.lua @@ -0,0 +1,66 @@ +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. +-- Note that we need to use `unsilent` command here because of this issue: +-- https://github.com/vim/vim/issues/4379 +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, see `:h lua-highlight` +api.nvim_create_autocmd({ "TextYankPost" }, { + pattern = "*", + group = api.nvim_create_augroup("highlight_yank", { clear = true }), + callback = function() + vim.highlight.on_yank { higroup = "YankColor", timeout = 300 } + 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, 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 +-- line before executing this command, see also https://vi.stackexchange.com/a/20397/15292 . +api.nvim_create_augroup("auto_read", { clear = true }) + +api.nvim_create_autocmd({ "FileChangedShellPost" }, { + pattern = "*", + group = "auto_read", + 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", + 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 }), + desc = "autoresize windows on resizing operation", + command = "wincmd =", +}) diff --git a/config/programs/vim/modules/lua/utils.lua b/config/programs/vim/modules/lua/utils.lua new file mode 100644 index 00000000..9f01687b --- /dev/null +++ b/config/programs/vim/modules/lua/utils.lua @@ -0,0 +1,59 @@ +local fn = vim.fn + +local M = {} + +function M.executable(name) + if fn.executable(name) > 0 then + return true + end + + return false +end + +--- check whether a feature exists in Nvim +--- @feat: string +--- the feature name, like `nvim-0.7` or `unix`. +--- return: bool +M.has = function(feat) + if fn.has(feat) == 1 then + return true + end + + return false +end + +--- Create a dir if it does not exist +function M.may_create_dir(dir) + local res = fn.isdirectory(dir) + + if res == 0 then + fn.mkdir(dir, "p") + end +end + +--- Generate random integers in the range [Low, High], inclusive, +--- adapted from https://stackoverflow.com/a/12739441/6064933 +--- @low: the lower value for this range +--- @high: the upper value for this range +function M.rand_int(low, high) + -- Use lua to generate random int, see also: https://stackoverflow.com/a/20157671/6064933 + math.randomseed(os.time()) + + return math.random(low, high) +end + +--- Select a random element from a sequence/list. +--- @seq: the sequence to choose an element +function M.rand_element(seq) + local idx = M.rand_int(1, #seq) + + return seq[idx] +end + +function M.add_pack(name) + local status, error = pcall(vim.cmd, "packadd " .. name) + + return status +end + +return M