diff options
author | Squibid <me@zacharyscheiman.com> | 2025-04-17 11:41:32 -0500 |
---|---|---|
committer | Squibid <me@zacharyscheiman.com> | 2025-04-17 11:41:32 -0500 |
commit | 3094bf2a3983b375f4adeccd25c3b12bbbded2aa (patch) | |
tree | 860f0a9cfd3d83bf73837e2f3a0ccc1b0c75cf5b /lua/core/misc.lua | |
parent | 8eaa615596be321a3be12378c5e7d65cc7e482b6 (diff) | |
download | nvim-3094bf2a3983b375f4adeccd25c3b12bbbded2aa.tar.gz nvim-3094bf2a3983b375f4adeccd25c3b12bbbded2aa.tar.bz2 nvim-3094bf2a3983b375f4adeccd25c3b12bbbded2aa.zip |
a lot more stuff
Diffstat (limited to '')
-rw-r--r-- | lua/core/misc.lua | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/lua/core/misc.lua b/lua/core/misc.lua index 5200c8b..5d0c0cf 100644 --- a/lua/core/misc.lua +++ b/lua/core/misc.lua @@ -29,10 +29,19 @@ end --- set colorscheme ---@param name string name of colorscheme function M.colorscheme(name) - vim.cmd.colorscheme(name) + -- only set the colorscheme if it exists + for _, v in pairs(vim.fn.getcompletion('', 'color')) do + if v == name then + vim.cmd("colorscheme "..name) + break + end + end + + -- override with any addons for _, v in pairs(vim.fn.getcompletion('', 'color')) do if v == name..'.ext' then - vim.cmd.colorscheme(name..'.ext') + vim.cmd("colorscheme"..name..'.ext') + break end end end @@ -41,12 +50,24 @@ end ---@param mode string|table mode for the keymap ---@param bind string|table keymap ---@param cmd function|string command to run ----@param opts table? keymap options +---@param opts vim.keymap.set.Opts? keymap options function M.map(mode, bind, cmd, opts) opts = opts or {} opts['noremap'] = true opts['silent'] = true + -- attempt to autogenerate a basic description + if not opts['desc'] then + if type(cmd) == "string" then + opts['desc'] = cmd:gsub("<%a+>", "") + elseif type(cmd) == "function" then + -- TODO: find a way to generate a better name + local file_name = vim.fn.fnamemodify(debug.getinfo(cmd, "S").short_src, ":t") + opts['desc'] = "origin@"..file_name + end + end + + -- define the keybinds if type(bind) == 'table' then for i in pairs(bind) do vim.keymap.set(mode, bind[i], cmd, opts) @@ -69,7 +90,7 @@ end --- extend vim.api.nvim_create_autocmd ---@param event string|table event or events ----@param opts table options +---@param opts vim.api.keyset.create_autocmd options function M.auto(event, opts) vim.api.nvim_create_autocmd(event, opts) end @@ -161,24 +182,29 @@ function M.timeout_highlight(opts) -- timer code was happily stolen from neovim/runtime/lua/vim/highlight.lua :) local timer, timer_cancel - - if timer then - timer:close() - assert(timer_cancel) - timer_cancel() + if not vim.fn.has("nvim-0.11") then + if timer then + timer:close() + assert(timer_cancel) + timer_cancel() + end end -- set the highlight - vim.highlight.range(0, namespaceid, opts.hl, opts.range[1], opts.range[2], {}) + vim.highlight.range(0, namespaceid, opts.hl, opts.range[1], opts.range[2], { + opts.timeout, + }) - timer_cancel = function() - timer = nil - timer_cancel = nil - pcall(vim.api.nvim_buf_clear_namespace, 0, namespaceid, 0, -1) - pcall(vim.api.nvim_win_remove_ns, 0, namespaceid) - end + if not vim.fn.has("nvim-0.11") then + timer_cancel = function() + timer = nil + timer_cancel = nil + pcall(vim.api.nvim_buf_clear_namespace, 0, namespaceid, 0, -1) + pcall(vim.api.nvim_win_remove_ns, 0, namespaceid) + end - timer = vim.defer_fn(timer_cancel, opts.timeout) + timer = vim.defer_fn(timer_cancel, opts.timeout) + end end return M |