autocmds.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = vim.fn.resolve(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. { "BufWinEnter", ".tf", "setlocal filetype=terraform" },
  36. { "BufRead", "*.tf", "setlocal filetype=terraform" },
  37. { "BufNewFile", "*.tf", "setlocal filetype=terraform" },
  38. { "BufWinEnter", ".zsh", "setlocal filetype=sh" },
  39. { "BufRead", "*.zsh", "setlocal filetype=sh" },
  40. { "BufNewFile", "*.zsh", "setlocal filetype=sh" },
  41. },
  42. _git = {
  43. { "FileType", "gitcommit", "setlocal wrap" },
  44. { "FileType", "gitcommit", "setlocal spell" },
  45. },
  46. _markdown = {
  47. { "FileType", "markdown", "setlocal wrap" },
  48. { "FileType", "markdown", "setlocal spell" },
  49. },
  50. _buffer_bindings = {
  51. { "FileType", "floaterm", "nnoremap <silent> <buffer> q :q<CR>" },
  52. },
  53. _auto_resize = {
  54. -- will cause split windows to be resized evenly if main window is resized
  55. { "VimResized", "*", "tabdo wincmd =" },
  56. },
  57. _general_lsp = {
  58. { "FileType", "lspinfo,lsp-installer,null-ls-info", "nnoremap <silent> <buffer> q :close<CR>" },
  59. },
  60. custom_groups = {},
  61. }
  62. end
  63. local get_format_on_save_opts = function()
  64. local defaults = require("lvim.config.defaults").format_on_save
  65. -- accept a basic boolean `lvim.format_on_save=true`
  66. if type(lvim.format_on_save) ~= "table" then
  67. return defaults
  68. end
  69. return {
  70. pattern = lvim.format_on_save.pattern or defaults.pattern,
  71. timeout = lvim.format_on_save.timeout or defaults.timeout,
  72. }
  73. end
  74. function M.enable_format_on_save(opts)
  75. local fmd_cmd = string.format(":silent lua vim.lsp.buf.formatting_sync({}, %s)", opts.timeout_ms)
  76. M.define_augroups {
  77. format_on_save = { { "BufWritePre", opts.pattern, fmd_cmd } },
  78. }
  79. Log:debug "enabled format-on-save"
  80. end
  81. function M.disable_format_on_save()
  82. M.remove_augroup "format_on_save"
  83. Log:debug "disabled format-on-save"
  84. end
  85. function M.configure_format_on_save()
  86. if lvim.format_on_save then
  87. if vim.fn.exists "#format_on_save#BufWritePre" == 1 then
  88. M.remove_augroup "format_on_save"
  89. Log:debug "reloading format-on-save configuration"
  90. end
  91. local opts = get_format_on_save_opts()
  92. M.enable_format_on_save(opts)
  93. else
  94. M.disable_format_on_save()
  95. end
  96. end
  97. function M.toggle_format_on_save()
  98. if vim.fn.exists "#format_on_save#BufWritePre" == 0 then
  99. local opts = get_format_on_save_opts()
  100. M.enable_format_on_save(opts)
  101. else
  102. M.disable_format_on_save()
  103. end
  104. end
  105. function M.remove_augroup(name)
  106. if vim.fn.exists("#" .. name) == 1 then
  107. vim.cmd("au! " .. name)
  108. end
  109. end
  110. function M.define_augroups(definitions) -- {{{1
  111. -- Create autocommand groups based on the passed definitions
  112. --
  113. -- The key will be the name of the group, and each definition
  114. -- within the group should have:
  115. -- 1. Trigger
  116. -- 2. Pattern
  117. -- 3. Text
  118. -- just like how they would normally be defined from Vim itself
  119. for group_name, definition in pairs(definitions) do
  120. vim.cmd("augroup " .. group_name)
  121. vim.cmd "autocmd!"
  122. for _, def in pairs(definition) do
  123. local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ")
  124. vim.cmd(command)
  125. end
  126. vim.cmd "augroup END"
  127. end
  128. end
  129. return M