init.lua 6.5 KB

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