autocmds.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. --- Load the default set of autogroups and autocommands.
  4. function M.load_defaults()
  5. vim.api.nvim_create_autocmd({ "FileType" }, {
  6. pattern = {
  7. "Jaq",
  8. "qf",
  9. "help",
  10. "man",
  11. "lspinfo",
  12. "spectre_panel",
  13. "lir",
  14. "DressingSelect",
  15. "tsplayground",
  16. },
  17. callback = function()
  18. vim.cmd [[
  19. nnoremap <silent> <buffer> q :close<CR>
  20. set nobuflisted
  21. ]]
  22. end,
  23. })
  24. local definitions = {
  25. {
  26. "TextYankPost",
  27. {
  28. group = "_general_settings",
  29. pattern = "*",
  30. desc = "Highlight text on yank",
  31. callback = function()
  32. require("vim.highlight").on_yank { higroup = "Search", timeout = 100 }
  33. end,
  34. },
  35. },
  36. {
  37. "FileType",
  38. {
  39. group = "_hide_dap_repl",
  40. pattern = "dap-repl",
  41. command = "set nobuflisted",
  42. },
  43. },
  44. {
  45. "FileType",
  46. {
  47. group = "_filetype_settings",
  48. pattern = "qf",
  49. command = "set nobuflisted",
  50. },
  51. },
  52. {
  53. "FileType",
  54. {
  55. group = "_filetype_settings",
  56. pattern = { "gitcommit", "markdown" },
  57. command = "setlocal wrap spell",
  58. },
  59. },
  60. {
  61. "FileType",
  62. {
  63. group = "_buffer_mappings",
  64. pattern = { "qf", "help", "man", "floaterm", "lspinfo", "lsp-installer", "null-ls-info" },
  65. command = "nnoremap <silent> <buffer> q :close<CR>",
  66. },
  67. },
  68. {
  69. "VimResized",
  70. {
  71. group = "_auto_resize",
  72. pattern = "*",
  73. command = "tabdo wincmd =",
  74. },
  75. },
  76. {
  77. "FileType",
  78. {
  79. group = "_filetype_settings",
  80. pattern = "alpha",
  81. callback = function()
  82. vim.cmd [[
  83. nnoremap <silent> <buffer> q :qa<CR>
  84. nnoremap <silent> <buffer> <esc> :qa<CR>
  85. set nobuflisted
  86. ]]
  87. end,
  88. },
  89. },
  90. {
  91. "FileType",
  92. {
  93. group = "_filetype_settings",
  94. pattern = "lir",
  95. callback = function()
  96. vim.opt_local.number = false
  97. vim.opt_local.relativenumber = false
  98. end,
  99. },
  100. },
  101. }
  102. M.define_autocmds(definitions)
  103. end
  104. local get_format_on_save_opts = function()
  105. local defaults = require("lvim.config.defaults").format_on_save
  106. -- accept a basic boolean `lvim.format_on_save=true`
  107. if type(lvim.format_on_save) ~= "table" then
  108. return defaults
  109. end
  110. return {
  111. pattern = lvim.format_on_save.pattern or defaults.pattern,
  112. timeout = lvim.format_on_save.timeout or defaults.timeout,
  113. }
  114. end
  115. function M.enable_format_on_save()
  116. local opts = get_format_on_save_opts()
  117. vim.api.nvim_create_augroup("lsp_format_on_save", {})
  118. vim.api.nvim_create_autocmd("BufWritePre", {
  119. group = "lsp_format_on_save",
  120. pattern = opts.pattern,
  121. callback = function()
  122. require("lvim.lsp.utils").format { timeout_ms = opts.timeout, filter = opts.filter }
  123. end,
  124. })
  125. Log:debug "enabled format-on-save"
  126. end
  127. function M.disable_format_on_save()
  128. M.clear_augroup "lsp_format_on_save"
  129. Log:debug "disabled format-on-save"
  130. end
  131. function M.configure_format_on_save()
  132. if lvim.format_on_save then
  133. M.enable_format_on_save()
  134. else
  135. M.disable_format_on_save()
  136. end
  137. end
  138. function M.toggle_format_on_save()
  139. local exists, autocmds = pcall(vim.api.nvim_get_autocmds, {
  140. group = "lsp_format_on_save",
  141. event = "BufWritePre",
  142. })
  143. if not exists or #autocmds == 0 then
  144. M.enable_format_on_save()
  145. else
  146. M.disable_format_on_save()
  147. end
  148. end
  149. function M.enable_reload_config_on_save()
  150. local user_config_file = require("lvim.config"):get_user_config_path()
  151. if vim.loop.os_uname().version:match "Windows" then
  152. -- autocmds require forward slashes even on windows
  153. user_config_file = user_config_file:gsub("\\", "/")
  154. end
  155. vim.api.nvim_create_autocmd("BufWritePost", {
  156. group = "_general_settings",
  157. pattern = user_config_file,
  158. desc = "Trigger LvimReload on saving config.lua",
  159. callback = function()
  160. require("lvim.config"):reload()
  161. end,
  162. })
  163. end
  164. function M.enable_transparent_mode()
  165. vim.api.nvim_create_autocmd("ColorScheme", {
  166. pattern = "*",
  167. callback = function()
  168. local hl_groups = {
  169. "Normal",
  170. "SignColumn",
  171. "NormalNC",
  172. "TelescopeBorder",
  173. "NvimTreeNormal",
  174. "EndOfBuffer",
  175. "MsgArea",
  176. }
  177. for _, name in ipairs(hl_groups) do
  178. vim.cmd(string.format("highlight %s ctermbg=none guibg=none", name))
  179. end
  180. end,
  181. })
  182. vim.opt.fillchars = "eob: "
  183. end
  184. --- Clean autocommand in a group if it exists
  185. --- This is safer than trying to delete the augroup itself
  186. ---@param name string the augroup name
  187. function M.clear_augroup(name)
  188. -- defer the function in case the autocommand is still in-use
  189. Log:debug("request to clear autocmds " .. name)
  190. vim.schedule(function()
  191. pcall(function()
  192. vim.api.nvim_clear_autocmds { group = name }
  193. end)
  194. end)
  195. end
  196. --- Create autocommand groups based on the passed definitions
  197. --- Also creates the augroup automatically if it doesn't exist
  198. ---@param definitions table contains a tuple of event, opts, see `:h nvim_create_autocmd`
  199. function M.define_autocmds(definitions)
  200. for _, entry in ipairs(definitions) do
  201. local event = entry[1]
  202. local opts = entry[2]
  203. if type(opts.group) == "string" and opts.group ~= "" then
  204. local exists, _ = pcall(vim.api.nvim_get_autocmds, { group = opts.group })
  205. if not exists then
  206. vim.api.nvim_create_augroup(opts.group, {})
  207. end
  208. end
  209. vim.api.nvim_create_autocmd(event, opts)
  210. end
  211. end
  212. return M