autocmds.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. { "BufWinEnter", "BufRead", "BufNewFile" },
  70. {
  71. group = "_format_options",
  72. pattern = "*",
  73. command = "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
  74. },
  75. },
  76. {
  77. "VimResized",
  78. {
  79. group = "_auto_resize",
  80. pattern = "*",
  81. command = "tabdo wincmd =",
  82. },
  83. },
  84. {
  85. "FileType",
  86. {
  87. group = "_filetype_settings",
  88. pattern = "alpha",
  89. callback = function()
  90. vim.cmd [[
  91. nnoremap <silent> <buffer> q :qa<CR>
  92. nnoremap <silent> <buffer> <esc> :qa<CR>
  93. set nobuflisted
  94. ]]
  95. end,
  96. },
  97. },
  98. {
  99. "FileType",
  100. {
  101. group = "_filetype_settings",
  102. pattern = "lir",
  103. callback = function()
  104. vim.opt_local.number = false
  105. vim.opt_local.relativenumber = false
  106. end,
  107. },
  108. },
  109. }
  110. M.define_autocmds(definitions)
  111. end
  112. local get_format_on_save_opts = function()
  113. local defaults = require("lvim.config.defaults").format_on_save
  114. -- accept a basic boolean `lvim.format_on_save=true`
  115. if type(lvim.format_on_save) ~= "table" then
  116. return defaults
  117. end
  118. return {
  119. pattern = lvim.format_on_save.pattern or defaults.pattern,
  120. timeout = lvim.format_on_save.timeout or defaults.timeout,
  121. }
  122. end
  123. function M.enable_format_on_save()
  124. local opts = get_format_on_save_opts()
  125. vim.api.nvim_create_augroup("lsp_format_on_save", {})
  126. vim.api.nvim_create_autocmd("BufWritePre", {
  127. group = "lsp_format_on_save",
  128. pattern = opts.pattern,
  129. callback = function()
  130. require("lvim.lsp.utils").format { timeout_ms = opts.timeout, filter = opts.filter }
  131. end,
  132. })
  133. Log:debug "enabled format-on-save"
  134. end
  135. function M.disable_format_on_save()
  136. M.clear_augroup "lsp_format_on_save"
  137. Log:debug "disabled format-on-save"
  138. end
  139. function M.configure_format_on_save()
  140. if lvim.format_on_save then
  141. M.enable_format_on_save()
  142. else
  143. M.disable_format_on_save()
  144. end
  145. end
  146. function M.toggle_format_on_save()
  147. local exists, autocmds = pcall(vim.api.nvim_get_autocmds, {
  148. group = "lsp_format_on_save",
  149. event = "BufWritePre",
  150. })
  151. if not exists or #autocmds == 0 then
  152. M.enable_format_on_save()
  153. else
  154. M.disable_format_on_save()
  155. end
  156. end
  157. function M.enable_reload_config_on_save()
  158. local user_config_file = require("lvim.config"):get_user_config_path()
  159. if vim.loop.os_uname().version:match "Windows" then
  160. -- autocmds require forward slashes even on windows
  161. user_config_file = user_config_file:gsub("\\", "/")
  162. end
  163. vim.api.nvim_create_autocmd("BufWritePost", {
  164. group = "_general_settings",
  165. pattern = user_config_file,
  166. desc = "Trigger LvimReload on saving config.lua",
  167. callback = function()
  168. require("lvim.config"):reload()
  169. end,
  170. })
  171. end
  172. function M.enable_transparent_mode()
  173. vim.api.nvim_create_autocmd("ColorScheme", {
  174. pattern = "*",
  175. callback = function()
  176. local hl_groups = {
  177. "Normal",
  178. "SignColumn",
  179. "NormalNC",
  180. "TelescopeBorder",
  181. "NvimTreeNormal",
  182. "EndOfBuffer",
  183. "MsgArea",
  184. }
  185. for _, name in ipairs(hl_groups) do
  186. vim.cmd(string.format("highlight %s ctermbg=none guibg=none", name))
  187. end
  188. end,
  189. })
  190. vim.opt.fillchars = "eob: "
  191. end
  192. --- Clean autocommand in a group if it exists
  193. --- This is safer than trying to delete the augroup itself
  194. ---@param name string the augroup name
  195. function M.clear_augroup(name)
  196. -- defer the function in case the autocommand is still in-use
  197. Log:debug("request to clear autocmds " .. name)
  198. vim.schedule(function()
  199. pcall(function()
  200. vim.api.nvim_clear_autocmds { group = name }
  201. end)
  202. end)
  203. end
  204. --- Create autocommand groups based on the passed definitions
  205. --- Also creates the augroup automatically if it doesn't exist
  206. ---@param definitions table contains a tuple of event, opts, see `:h nvim_create_autocmd`
  207. function M.define_autocmds(definitions)
  208. for _, entry in ipairs(definitions) do
  209. local event = entry[1]
  210. local opts = entry[2]
  211. if type(opts.group) == "string" and opts.group ~= "" then
  212. local exists, _ = pcall(vim.api.nvim_get_autocmds, { group = opts.group })
  213. if not exists then
  214. vim.api.nvim_create_augroup(opts.group, {})
  215. end
  216. end
  217. vim.api.nvim_create_autocmd(event, opts)
  218. end
  219. end
  220. return M