autocmds.lua 5.3 KB

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