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