autocmds.lua 5.8 KB

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