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