autocmds.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. --- Load the default set of autogroups and autocommands.
  4. function M.load_augroups()
  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. return {
  11. _general_settings = {
  12. { "FileType", "qf,help,man", "nnoremap <silent> <buffer> q :close<CR>" },
  13. {
  14. "TextYankPost",
  15. "*",
  16. "lua require('vim.highlight').on_yank({higroup = 'Search', timeout = 200})",
  17. },
  18. {
  19. "BufWinEnter",
  20. "dashboard",
  21. "setlocal cursorline signcolumn=yes cursorcolumn number",
  22. },
  23. { "BufWritePost", user_config_file, "lua require('lvim.config'):reload()" },
  24. { "FileType", "qf", "set nobuflisted" },
  25. -- { "VimLeavePre", "*", "set title set titleold=" },
  26. },
  27. _formatoptions = {
  28. {
  29. "BufWinEnter,BufRead,BufNewFile",
  30. "*",
  31. "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
  32. },
  33. },
  34. _filetypechanges = {},
  35. _git = {
  36. { "FileType", "gitcommit", "setlocal wrap" },
  37. { "FileType", "gitcommit", "setlocal spell" },
  38. },
  39. _markdown = {
  40. { "FileType", "markdown", "setlocal wrap" },
  41. { "FileType", "markdown", "setlocal spell" },
  42. },
  43. _buffer_bindings = {
  44. { "FileType", "floaterm", "nnoremap <silent> <buffer> q :q<CR>" },
  45. },
  46. _auto_resize = {
  47. -- will cause split windows to be resized evenly if main window is resized
  48. { "VimResized", "*", "tabdo wincmd =" },
  49. },
  50. _general_lsp = {
  51. { "FileType", "lspinfo,lsp-installer,null-ls-info", "nnoremap <silent> <buffer> q :close<CR>" },
  52. },
  53. _alpha = {
  54. {
  55. "FileType",
  56. "alpha",
  57. "set showtabline=0 | autocmd BufLeave <buffer> set showtabline=" .. vim.opt.showtabline._value,
  58. },
  59. -- https://github.com/goolord/alpha-nvim/issues/42
  60. {
  61. "FileType",
  62. "alpha",
  63. "set laststatus=0 | autocmd BufUnload <buffer> set laststatus=" .. vim.opt.laststatus._value,
  64. },
  65. },
  66. custom_groups = {},
  67. }
  68. end
  69. local get_format_on_save_opts = function()
  70. local defaults = require("lvim.config.defaults").format_on_save
  71. -- accept a basic boolean `lvim.format_on_save=true`
  72. if type(lvim.format_on_save) ~= "table" then
  73. return defaults
  74. end
  75. return {
  76. pattern = lvim.format_on_save.pattern or defaults.pattern,
  77. timeout = lvim.format_on_save.timeout or defaults.timeout,
  78. }
  79. end
  80. function M.enable_format_on_save(opts)
  81. local fmd_cmd = string.format(":silent lua vim.lsp.buf.formatting_sync({}, %s)", opts.timeout)
  82. M.define_augroups {
  83. format_on_save = { { "BufWritePre", opts.pattern, fmd_cmd } },
  84. }
  85. Log:debug "enabled format-on-save"
  86. end
  87. function M.disable_format_on_save()
  88. M.disable_augroup "format_on_save"
  89. Log:debug "disabled format-on-save"
  90. end
  91. function M.configure_format_on_save()
  92. if lvim.format_on_save then
  93. local opts = get_format_on_save_opts()
  94. M.enable_format_on_save(opts)
  95. else
  96. M.disable_format_on_save()
  97. end
  98. end
  99. function M.toggle_format_on_save()
  100. if vim.fn.exists "#format_on_save#BufWritePre" == 0 then
  101. local opts = get_format_on_save_opts()
  102. M.enable_format_on_save(opts)
  103. else
  104. M.disable_format_on_save()
  105. end
  106. end
  107. function M.enable_lsp_document_highlight(client_id)
  108. M.define_augroups({
  109. lsp_document_highlight = {
  110. {
  111. "CursorHold",
  112. "<buffer>",
  113. string.format("lua require('lvim.lsp.utils').conditional_document_highlight(%d)", client_id),
  114. },
  115. {
  116. "CursorMoved",
  117. "<buffer>",
  118. "lua vim.lsp.buf.clear_references()",
  119. },
  120. },
  121. }, true)
  122. end
  123. function M.disable_lsp_document_highlight()
  124. M.disable_augroup "lsp_document_highlight"
  125. end
  126. function M.enable_code_lens_refresh()
  127. M.define_augroups({
  128. lsp_code_lens_refresh = {
  129. {
  130. "InsertLeave ",
  131. "<buffer>",
  132. "lua vim.lsp.codelens.refresh()",
  133. },
  134. {
  135. "InsertLeave ",
  136. "<buffer>",
  137. "lua vim.lsp.codelens.display()",
  138. },
  139. },
  140. }, true)
  141. end
  142. function M.disable_code_lens_refresh()
  143. M.disable_augroup "lsp_code_lens_refresh"
  144. end
  145. function M.enable_transparent_mode()
  146. vim.cmd "au ColorScheme * hi Normal ctermbg=none guibg=none"
  147. vim.cmd "au ColorScheme * hi SignColumn ctermbg=none guibg=none"
  148. vim.cmd "au ColorScheme * hi NormalNC ctermbg=none guibg=none"
  149. vim.cmd "au ColorScheme * hi MsgArea ctermbg=none guibg=none"
  150. vim.cmd "au ColorScheme * hi TelescopeBorder ctermbg=none guibg=none"
  151. vim.cmd "au ColorScheme * hi NvimTreeNormal ctermbg=none guibg=none"
  152. vim.cmd "au ColorScheme * hi EndOfBuffer ctermbg=none guibg=none"
  153. vim.cmd "let &fcs='eob: '"
  154. end
  155. --- Disable autocommand groups if it exists
  156. --- This is more reliable than trying to delete the augroup itself
  157. ---@param name string the augroup name
  158. function M.disable_augroup(name)
  159. -- defer the function in case the autocommand is still in-use
  160. vim.schedule(function()
  161. if vim.fn.exists("#" .. name) == 1 then
  162. vim.cmd("augroup " .. name)
  163. vim.cmd "autocmd!"
  164. vim.cmd "augroup END"
  165. end
  166. end)
  167. end
  168. --- Create autocommand groups based on the passed definitions
  169. ---@param definitions table contains trigger, pattern and text. The key will be used as a group name
  170. ---@param buffer boolean indicate if the augroup should be local to the buffer
  171. function M.define_augroups(definitions, buffer)
  172. for group_name, definition in pairs(definitions) do
  173. vim.cmd("augroup " .. group_name)
  174. if buffer then
  175. vim.cmd [[autocmd! * <buffer>]]
  176. else
  177. vim.cmd [[autocmd!]]
  178. end
  179. for _, def in pairs(definition) do
  180. local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ")
  181. vim.cmd(command)
  182. end
  183. vim.cmd "augroup END"
  184. end
  185. end
  186. return M