diff --git a/README.md b/README.md index 59074e65..f60bf6f6 100644 --- a/README.md +++ b/README.md @@ -536,6 +536,20 @@ require("telescope").load_extension("noice") - `:Noice stats` shows debugging stats - `:Noice telescope` opens message history in Telescope +Alternatively, all commands also exist as a full name like `:NoiceLast`, `:NoiceDisable`. + +You can also use `Lua` equivalents. + +```lua +vim.keymap.set("n", "nl", function() + require("noice").cmd("last") +end) + +vim.keymap.set("n", "nh", function() + require("noice").cmd("history") +end) +``` + > You can add custom commands with `config.commands` ## 🌈 Highlight Groups diff --git a/lua/noice/commands.lua b/lua/noice/commands.lua index 9349bed3..664c039f 100644 --- a/lua/noice/commands.lua +++ b/lua/noice/commands.lua @@ -11,6 +11,9 @@ local Message = require("noice.message") local M = {} +---@type table +M.commands = {} + ---@param command NoiceCommand function M.command(command) return function() @@ -26,8 +29,16 @@ function M.command(command) end end +function M.cmd(cmd) + if M.commands[cmd] then + M.commands[cmd]() + else + M.commands.history() + end +end + function M.setup() - local commands = { + M.commands = { debug = function() Config.options.debug = not Config.options.debug end, @@ -59,16 +70,12 @@ function M.setup() } for name, command in pairs(Config.options.commands) do - commands[name] = M.command(command) + M.commands[name] = M.command(command) end vim.api.nvim_create_user_command("Noice", function(args) local cmd = vim.trim(args.args or "") - if commands[cmd] then - commands[cmd]() - else - commands.history() - end + M.cmd(cmd) end, { nargs = "?", desc = "Noice", @@ -79,9 +86,16 @@ function M.setup() local prefix = line:match("^%s*Noice (%w*)") return vim.tbl_filter(function(key) return key:find(prefix) == 1 - end, vim.tbl_keys(commands)) + end, vim.tbl_keys(M.commands)) end, }) + + for name in pairs(M.commands) do + local cmd = "Noice" .. name:sub(1, 1):upper() .. name:sub(2) + vim.api.nvim_create_user_command(cmd, function() + M.cmd(name) + end, { desc = "Noice " .. name }) + end end return M diff --git a/lua/noice/init.lua b/lua/noice/init.lua index 5b27a357..0eae6687 100644 --- a/lua/noice/init.lua +++ b/lua/noice/init.lua @@ -46,6 +46,10 @@ function M.disable() require("noice.util.hacks").disable() end +function M.cmd(name) + require("noice.commands").cmd(name) +end + function M.enable() Config._running = true if Config.options.notify.enabled then