init.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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("-- O", separator:sub(2), " = function ()\n")
  36. else
  37. io.write("O", 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 `O` related settings to `lv-settings.lua` file
  47. r_inspect_settings(O, 10000, ".")
  48. -- closes the open file
  49. io.close(file)
  50. end
  51. function lv_utils.reload_lv_config()
  52. vim.cmd "source ~/.config/lvim/lv-config.lua"
  53. vim.cmd "source ~/.local/share/lunarvim/lvim/lua/plugins.lua"
  54. local plugins = require "plugins"
  55. local plugin_loader = require("plugin-loader").init()
  56. plugin_loader:load { plugins, O.user_plugins }
  57. vim.cmd "source ~/.local/share/lunarvim/lvim/lua/settings.lua"
  58. vim.cmd "source ~/.local/share/lunarvim/lvim/lua/core/formatter.lua"
  59. vim.cmd ":PackerCompile"
  60. vim.cmd ":PackerInstall"
  61. -- vim.cmd ":PackerClean"
  62. end
  63. function lv_utils.check_lsp_client_active(name)
  64. local clients = vim.lsp.get_active_clients()
  65. for _, client in pairs(clients) do
  66. if client.name == name then
  67. return true
  68. end
  69. end
  70. return false
  71. end
  72. function lv_utils.add_keymap(mode, opts, keymaps)
  73. for _, keymap in ipairs(keymaps) do
  74. vim.api.nvim_set_keymap(mode, keymap[1], keymap[2], opts)
  75. end
  76. end
  77. function lv_utils.add_keymap_normal_mode(opts, keymaps)
  78. lv_utils.add_keymap("n", opts, keymaps)
  79. end
  80. function lv_utils.add_keymap_visual_mode(opts, keymaps)
  81. lv_utils.add_keymap("v", opts, keymaps)
  82. end
  83. function lv_utils.add_keymap_visual_block_mode(opts, keymaps)
  84. lv_utils.add_keymap("x", opts, keymaps)
  85. end
  86. function lv_utils.add_keymap_insert_mode(opts, keymaps)
  87. lv_utils.add_keymap("i", opts, keymaps)
  88. end
  89. function lv_utils.add_keymap_term_mode(opts, keymaps)
  90. lv_utils.add_keymap("t", opts, keymaps)
  91. end
  92. function lv_utils.define_augroups(definitions) -- {{{1
  93. -- Create autocommand groups based on the passed definitions
  94. --
  95. -- The key will be the name of the group, and each definition
  96. -- within the group should have:
  97. -- 1. Trigger
  98. -- 2. Pattern
  99. -- 3. Text
  100. -- just like how they would normally be defined from Vim itself
  101. for group_name, definition in pairs(definitions) do
  102. vim.cmd("augroup " .. group_name)
  103. vim.cmd "autocmd!"
  104. for _, def in pairs(definition) do
  105. local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ")
  106. vim.cmd(command)
  107. end
  108. vim.cmd "augroup END"
  109. end
  110. end
  111. function lv_utils.unrequire(m)
  112. package.loaded[m] = nil
  113. _G[m] = nil
  114. end
  115. lv_utils.define_augroups {
  116. _general_settings = {
  117. {
  118. "TextYankPost",
  119. "*",
  120. "lua require('vim.highlight').on_yank({higroup = 'Search', timeout = 200})",
  121. },
  122. {
  123. "BufWinEnter",
  124. "*",
  125. "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
  126. },
  127. {
  128. "BufWinEnter",
  129. "dashboard",
  130. "setlocal cursorline signcolumn=yes cursorcolumn number",
  131. },
  132. {
  133. "BufRead",
  134. "*",
  135. "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
  136. },
  137. {
  138. "BufNewFile",
  139. "*",
  140. "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
  141. },
  142. { "BufWritePost", "lv-config.lua", "lua require('lv-utils').reload_lv_config()" },
  143. -- { "VimLeavePre", "*", "set title set titleold=" },
  144. },
  145. _solidity = {
  146. { "BufWinEnter", ".tf", "setlocal filetype=hcl" },
  147. { "BufRead", "*.tf", "setlocal filetype=hcl" },
  148. { "BufNewFile", "*.tf", "setlocal filetype=hcl" },
  149. },
  150. -- _solidity = {
  151. -- {'BufWinEnter', '.sol', 'setlocal filetype=solidity'}, {'BufRead', '*.sol', 'setlocal filetype=solidity'},
  152. -- {'BufNewFile', '*.sol', 'setlocal filetype=solidity'}
  153. -- },
  154. -- _gemini = {
  155. -- {'BufWinEnter', '.gmi', 'setlocal filetype=markdown'}, {'BufRead', '*.gmi', 'setlocal filetype=markdown'},
  156. -- {'BufNewFile', '*.gmi', 'setlocal filetype=markdown'}
  157. -- },
  158. _markdown = {
  159. { "FileType", "markdown", "setlocal wrap" },
  160. { "FileType", "markdown", "setlocal spell" },
  161. },
  162. _buffer_bindings = {
  163. { "FileType", "floaterm", "nnoremap <silent> <buffer> q :q<CR>" },
  164. },
  165. _auto_resize = {
  166. -- will cause split windows to be resized evenly if main window is resized
  167. { "VimResized", "*", "wincmd =" },
  168. },
  169. _packer_compile = {
  170. -- will cause split windows to be resized evenly if main window is resized
  171. { "BufWritePost", "plugins.lua", "PackerCompile" },
  172. },
  173. -- _fterm_lazygit = {
  174. -- -- will cause esc key to exit lazy git
  175. -- {"TermEnter", "*", "call LazyGitNativation()"}
  176. -- },
  177. -- _mode_switching = {
  178. -- -- will switch between absolute and relative line numbers depending on mode
  179. -- {'InsertEnter', '*', 'if &relativenumber | let g:ms_relativenumberoff = 1 | setlocal number norelativenumber | endif'},
  180. -- {'InsertLeave', '*', 'if exists("g:ms_relativenumberoff") | setlocal relativenumber | endif'},
  181. -- {'InsertEnter', '*', 'if &cursorline | let g:ms_cursorlineoff = 1 | setlocal nocursorline | endif'},
  182. -- {'InsertLeave', '*', 'if exists("g:ms_cursorlineoff") | setlocal cursorline | endif'},
  183. -- },
  184. _user_autocommands = O.user_autocommands,
  185. }
  186. function lv_utils.gsub_args(args)
  187. if args == nil or type(args) ~= "table" then
  188. return args
  189. end
  190. local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
  191. for i = 1, #args do
  192. args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
  193. end
  194. return args
  195. end
  196. vim.cmd [[
  197. function! QuickFixToggle()
  198. if empty(filter(getwininfo(), 'v:val.quickfix'))
  199. copen
  200. else
  201. cclose
  202. endif
  203. endfunction
  204. ]]
  205. return lv_utils
  206. -- TODO: find a new home for these autocommands