autocmds.lua 5.9 KB

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