autocmds.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. custom_groups = {},
  54. }
  55. end
  56. local get_format_on_save_opts = function()
  57. local defaults = require("lvim.config.defaults").format_on_save
  58. -- accept a basic boolean `lvim.format_on_save=true`
  59. if type(lvim.format_on_save) ~= "table" then
  60. return defaults
  61. end
  62. return {
  63. pattern = lvim.format_on_save.pattern or defaults.pattern,
  64. timeout = lvim.format_on_save.timeout or defaults.timeout,
  65. }
  66. end
  67. function M.enable_format_on_save()
  68. local opts = get_format_on_save_opts()
  69. vim.api.nvim_create_augroup("lsp_format_on_save", {})
  70. vim.api.nvim_create_autocmd("BufWritePre", {
  71. group = "lsp_format_on_save",
  72. pattern = opts.pattern,
  73. callback = function()
  74. require("lvim.lsp.utils").format { timeout_ms = opts.timeout, filter = opts.filter }
  75. end,
  76. })
  77. Log:debug "enabled format-on-save"
  78. end
  79. function M.disable_format_on_save()
  80. pcall(vim.api.nvim_del_augroup_by_name, "lsp_format_on_save")
  81. Log:debug "disabled format-on-save"
  82. end
  83. function M.configure_format_on_save()
  84. if lvim.format_on_save then
  85. M.enable_format_on_save()
  86. else
  87. M.disable_format_on_save()
  88. end
  89. end
  90. function M.toggle_format_on_save()
  91. local status, _ = pcall(vim.api.nvim_get_autocmds, {
  92. group = "lsp_format_on_save",
  93. event = "BufWritePre",
  94. })
  95. if not status then
  96. M.enable_format_on_save()
  97. else
  98. M.disable_format_on_save()
  99. end
  100. end
  101. function M.enable_transparent_mode()
  102. vim.cmd "au ColorScheme * hi Normal ctermbg=none guibg=none"
  103. vim.cmd "au ColorScheme * hi SignColumn ctermbg=none guibg=none"
  104. vim.cmd "au ColorScheme * hi NormalNC ctermbg=none guibg=none"
  105. vim.cmd "au ColorScheme * hi MsgArea ctermbg=none guibg=none"
  106. vim.cmd "au ColorScheme * hi TelescopeBorder ctermbg=none guibg=none"
  107. vim.cmd "au ColorScheme * hi NvimTreeNormal ctermbg=none guibg=none"
  108. vim.cmd "au ColorScheme * hi EndOfBuffer ctermbg=none guibg=none"
  109. vim.cmd "let &fcs='eob: '"
  110. end
  111. --- Disable autocommand groups if it exists
  112. --- This is more reliable than trying to delete the augroup itself
  113. ---@param name string the augroup name
  114. function M.disable_augroup(name)
  115. -- defer the function in case the autocommand is still in-use
  116. vim.schedule(function()
  117. if vim.fn.exists("#" .. name) == 1 then
  118. vim.cmd("augroup " .. name)
  119. vim.cmd "autocmd!"
  120. vim.cmd "augroup END"
  121. end
  122. end)
  123. end
  124. --- Create autocommand groups based on the passed definitions
  125. ---@param definitions table contains trigger, pattern and text. The key will be used as a group name
  126. function M.define_augroups(definitions, buffer)
  127. for group_name, definition in pairs(definitions) do
  128. vim.cmd("augroup " .. group_name)
  129. if buffer then
  130. vim.cmd [[autocmd! * <buffer>]]
  131. else
  132. vim.cmd [[autocmd!]]
  133. end
  134. for _, def in pairs(definition) do
  135. local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ")
  136. vim.cmd(command)
  137. end
  138. vim.cmd "augroup END"
  139. end
  140. end
  141. return M