init.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. local lv_utils = {}
  2. -- recursive Print (structure, limit, separator)
  3. local function r_inspect_settings(structure, limit, separator)
  4. limit = limit or 100 -- default item limit
  5. separator = separator or "." -- indent string
  6. if limit < 1 then
  7. print "ERROR: Item limit reached."
  8. return limit - 1
  9. end
  10. if structure == nil then
  11. io.write("-- O", separator:sub(2), " = nil\n")
  12. return limit - 1
  13. end
  14. local ts = type(structure)
  15. if ts == "table" then
  16. for k, v in pairs(structure) do
  17. -- replace non alpha keys wih ["key"]
  18. if tostring(k):match "[^%a_]" then
  19. k = '["' .. tostring(k) .. '"]'
  20. end
  21. limit = r_inspect_settings(v, limit, separator .. "." .. tostring(k))
  22. if limit < 0 then
  23. break
  24. end
  25. end
  26. return limit
  27. end
  28. if ts == "string" then
  29. -- escape sequences
  30. structure = string.format("%q", structure)
  31. end
  32. separator = separator:gsub("%.%[", "%[")
  33. if type(structure) == "function" then
  34. -- don't print functions
  35. io.write("-- lvim", separator:sub(2), " = function ()\n")
  36. else
  37. io.write("lvim", separator:sub(2), " = ", tostring(structure), "\n")
  38. end
  39. return limit - 1
  40. end
  41. function lv_utils.generate_settings()
  42. -- Opens a file in append mode
  43. local file = io.open("lv-settings.lua", "w")
  44. -- sets the default output file as test.lua
  45. io.output(file)
  46. -- write all `lvim` related settings to `lv-settings.lua` file
  47. r_inspect_settings(lvim, 10000, ".")
  48. -- closes the open file
  49. io.close(file)
  50. end
  51. -- autoformat
  52. local toggle_autoformat = function()
  53. if lvim.format_on_save then
  54. require("lv-utils").define_augroups {
  55. autoformat = {
  56. {
  57. "BufWritePost",
  58. "*",
  59. ":silent lua vim.lsp.buf.formatting()",
  60. },
  61. },
  62. }
  63. end
  64. if not lvim.format_on_save then
  65. vim.cmd [[if exists('#autoformat#BufWritePost')
  66. :autocmd! autoformat
  67. endif]]
  68. end
  69. end
  70. function lv_utils.toggle_autoformat()
  71. toggle_autoformat()
  72. end
  73. function lv_utils.reload_lv_config()
  74. vim.cmd "source ~/.config/lvim/lv-config.lua"
  75. vim.cmd "source ~/.local/share/lunarvim/lvim/lua/plugins.lua"
  76. local plugins = require "plugins"
  77. local plugin_loader = require("plugin-loader").init()
  78. toggle_autoformat()
  79. plugin_loader:load { plugins, lvim.plugins }
  80. vim.cmd "source ~/.local/share/lunarvim/lvim/lua/settings.lua"
  81. vim.cmd ":PackerCompile"
  82. vim.cmd ":PackerInstall"
  83. -- vim.cmd ":PackerClean"
  84. end
  85. function lv_utils.check_lsp_client_active(name)
  86. local clients = vim.lsp.get_active_clients()
  87. for _, client in pairs(clients) do
  88. if client.name == name then
  89. return true
  90. end
  91. end
  92. return false
  93. end
  94. function lv_utils.add_keymap(mode, opts, keymaps)
  95. for _, keymap in ipairs(keymaps) do
  96. vim.api.nvim_set_keymap(mode, keymap[1], keymap[2], opts)
  97. end
  98. end
  99. function lv_utils.add_keymap_normal_mode(opts, keymaps)
  100. lv_utils.add_keymap("n", opts, keymaps)
  101. end
  102. function lv_utils.add_keymap_visual_mode(opts, keymaps)
  103. lv_utils.add_keymap("v", opts, keymaps)
  104. end
  105. function lv_utils.add_keymap_visual_block_mode(opts, keymaps)
  106. lv_utils.add_keymap("x", opts, keymaps)
  107. end
  108. function lv_utils.add_keymap_insert_mode(opts, keymaps)
  109. lv_utils.add_keymap("i", opts, keymaps)
  110. end
  111. function lv_utils.add_keymap_term_mode(opts, keymaps)
  112. lv_utils.add_keymap("t", opts, keymaps)
  113. end
  114. function lv_utils.define_augroups(definitions) -- {{{1
  115. -- Create autocommand groups based on the passed definitions
  116. --
  117. -- The key will be the name of the group, and each definition
  118. -- within the group should have:
  119. -- 1. Trigger
  120. -- 2. Pattern
  121. -- 3. Text
  122. -- just like how they would normally be defined from Vim itself
  123. for group_name, definition in pairs(definitions) do
  124. vim.cmd("augroup " .. group_name)
  125. vim.cmd "autocmd!"
  126. for _, def in pairs(definition) do
  127. local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ")
  128. vim.cmd(command)
  129. end
  130. vim.cmd "augroup END"
  131. end
  132. end
  133. function lv_utils.unrequire(m)
  134. package.loaded[m] = nil
  135. _G[m] = nil
  136. end
  137. lv_utils.define_augroups {
  138. _general_settings = {
  139. {
  140. "TextYankPost",
  141. "*",
  142. "lua require('vim.highlight').on_yank({higroup = 'Search', timeout = 200})",
  143. },
  144. {
  145. "BufWinEnter",
  146. "*",
  147. "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
  148. },
  149. {
  150. "BufWinEnter",
  151. "dashboard",
  152. "setlocal cursorline signcolumn=yes cursorcolumn number",
  153. },
  154. {
  155. "BufRead",
  156. "*",
  157. "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
  158. },
  159. {
  160. "BufNewFile",
  161. "*",
  162. "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
  163. },
  164. { "BufWritePost", "lv-config.lua", "lua require('lv-utils').reload_lv_config()" },
  165. -- { "VimLeavePre", "*", "set title set titleold=" },
  166. },
  167. _filetypechanges = {
  168. { "BufWinEnter", ".tf", "setlocal filetype=hcl" },
  169. { "BufRead", "*.tf", "setlocal filetype=hcl" },
  170. { "BufNewFile", "*.tf", "setlocal filetype=hcl" },
  171. { "BufWinEnter", ".zsh", "setlocal filetype=sh" },
  172. { "BufRead", "*.zsh", "setlocal filetype=sh" },
  173. { "BufNewFile", "*.zsh", "setlocal filetype=sh" },
  174. },
  175. -- _solidity = {
  176. -- {'BufWinEnter', '.sol', 'setlocal filetype=solidity'}, {'BufRead', '*.sol', 'setlocal filetype=solidity'},
  177. -- {'BufNewFile', '*.sol', 'setlocal filetype=solidity'}
  178. -- },
  179. -- _gemini = {
  180. -- {'BufWinEnter', '.gmi', 'setlocal filetype=markdown'}, {'BufRead', '*.gmi', 'setlocal filetype=markdown'},
  181. -- {'BufNewFile', '*.gmi', 'setlocal filetype=markdown'}
  182. -- },
  183. _markdown = {
  184. { "FileType", "markdown", "setlocal wrap" },
  185. { "FileType", "markdown", "setlocal spell" },
  186. },
  187. _buffer_bindings = {
  188. { "FileType", "floaterm", "nnoremap <silent> <buffer> q :q<CR>" },
  189. },
  190. _auto_resize = {
  191. -- will cause split windows to be resized evenly if main window is resized
  192. { "VimResized", "*", "wincmd =" },
  193. },
  194. _packer_compile = {
  195. -- will cause split windows to be resized evenly if main window is resized
  196. { "BufWritePost", "plugins.lua", "PackerCompile" },
  197. },
  198. -- _fterm_lazygit = {
  199. -- -- will cause esc key to exit lazy git
  200. -- {"TermEnter", "*", "call LazyGitNativation()"}
  201. -- },
  202. -- _mode_switching = {
  203. -- -- will switch between absolute and relative line numbers depending on mode
  204. -- {'InsertEnter', '*', 'if &relativenumber | let g:ms_relativenumberoff = 1 | setlocal number norelativenumber | endif'},
  205. -- {'InsertLeave', '*', 'if exists("g:ms_relativenumberoff") | setlocal relativenumber | endif'},
  206. -- {'InsertEnter', '*', 'if &cursorline | let g:ms_cursorlineoff = 1 | setlocal nocursorline | endif'},
  207. -- {'InsertLeave', '*', 'if exists("g:ms_cursorlineoff") | setlocal cursorline | endif'},
  208. -- },
  209. _user_autocommands = lvim.autocommands,
  210. }
  211. function lv_utils.gsub_args(args)
  212. if args == nil or type(args) ~= "table" then
  213. return args
  214. end
  215. local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
  216. for i = 1, #args do
  217. args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
  218. end
  219. return args
  220. end
  221. vim.cmd [[
  222. function! QuickFixToggle()
  223. if empty(filter(getwininfo(), 'v:val.quickfix'))
  224. copen
  225. else
  226. cclose
  227. endif
  228. endfunction
  229. ]]
  230. return lv_utils
  231. -- TODO: find a new home for these autocommands