diff --git a/README.md b/README.md index f00fdbe..0cf3d54 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ require("stopinsert").setup() | Items | Type | Default Value | Description | | --------------------- | --------- | ------------------ | -------------- | | `idle_time_ms` | number | `5000` (5 seconds) | Maximum time (in milliseconds) before you are forced out of Insert mode back to Normal mode. | +| `show_popup_msg` | boolean | `true` | Enable/disable popup message" | +| `clear_popup_ms` | number | `5000` | Maximum time (in milliseconds) for which the popup message hangs around | | `disabled_filetypes` | list | `{ "TelescopePrompt", "checkhealth", "help", "lspinfo", "mason", "neo%-tree*", }` | List of filetypes to exclude the effect of this plugin. | **NOTE:** diff --git a/lua/stopinsert/config.lua b/lua/stopinsert/config.lua index 87e44ce..e5ff37e 100644 --- a/lua/stopinsert/config.lua +++ b/lua/stopinsert/config.lua @@ -2,6 +2,8 @@ local M = {} M.config = { idle_time_ms = 5000, + show_popup_msg = true, + clear_popup_ms = 5000, disabled_filetypes = { "TelescopePrompt", "checkhealth", diff --git a/lua/stopinsert/init.lua b/lua/stopinsert/init.lua index 1946c01..2d61fd3 100644 --- a/lua/stopinsert/init.lua +++ b/lua/stopinsert/init.lua @@ -1,7 +1,6 @@ local M = {} -local util = require("stopinsert.util") -M.enable = true +M.enable = true local user_cmds = { enable = function() M.enable = true @@ -21,14 +20,18 @@ local user_cmds = { end, } +local config = require("stopinsert.config") +local util = require("stopinsert.util") + ---@param opts table ---@return nil function M.setup(opts) opts = opts or {} - require("stopinsert.config").set(opts) + config.set(opts) + local plugin_autocmd_group = "StopInsertAutoCmd" vim.api.nvim_create_autocmd("InsertEnter", { - group = vim.api.nvim_create_augroup("InsertEnterListener", { clear = true }), + group = vim.api.nvim_create_augroup(plugin_autocmd_group, { clear = true }), callback = function() if not M.enable then return diff --git a/lua/stopinsert/popup.lua b/lua/stopinsert/popup.lua new file mode 100644 index 0000000..f6923f5 --- /dev/null +++ b/lua/stopinsert/popup.lua @@ -0,0 +1,39 @@ +local M = {} + +--- Create a simple popup message, positioned in the bottom right corner of the buffer +--- Automatically close this popup after 5 seconds +--- Courtersy of encourage.nvim +---@param message string +---@return nil +function M.show(message) + local width = #message + local height = 1 + local buf = vim.api.nvim_create_buf(false, true) + local current_win = vim.api.nvim_get_current_win() + local win_config = vim.api.nvim_win_get_config(current_win) + local win_width = win_config.width + local win_height = win_config.height + + local opts = { + style = "minimal", + relative = "win", + win = current_win, + width = width, + height = height, + row = win_height - height - 2, + col = win_width - width - 2, + border = "rounded", + } + + local win = vim.api.nvim_open_win(buf, false, opts) + vim.api.nvim_buf_set_lines(buf, 0, -1, false, { message }) + vim.api.nvim_win_set_option(win, "winhl", "Normal:NormalFloat,FloatBorder:FloatBorder") + + vim.defer_fn(function() + if vim.api.nvim_win_is_valid(win) then + vim.api.nvim_win_close(win, true) + end + end, 5000) +end + +return M diff --git a/lua/stopinsert/util.lua b/lua/stopinsert/util.lua index 47ab36f..aba3f15 100644 --- a/lua/stopinsert/util.lua +++ b/lua/stopinsert/util.lua @@ -1,11 +1,16 @@ local M = {} local timer = nil local config = require("stopinsert.config").config +local popup = require("stopinsert.popup") ---@return nil function M.force_exit_insert_mode() if vim.fn.mode() == "i" then vim.cmd("stopinsert") + + if config.show_popup_msg then + popup.show("StopInsertPlug: You were idling in Insert mode. Remeber to when you finish editing.") + end end end