diff --git a/README.md b/README.md index bceff48..28dbd10 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ hardtime.nvim -
Establish good command workflow and quit bad habit.

@@ -49,6 +48,7 @@ Learn more in this [blog post](https://m4xshen.dev/posts/vim-command-workflow/) ``` 2. Setup the plugin in your `init.lua`. This step is not needed with lazy.nvim if `opts` is set as above. + ```lua require("hardtime").setup() ``` @@ -60,7 +60,7 @@ But if you want to see both the hint message and current mode you can setup with - Display the mode on status line and set `'showmode'` to false. You can do this with some statusline plugin such as lualine.nvim. - Set the `'cmdheight'` to 2 so that the hint message won't be replaced by mode message. - Use nvim-notify to display hint messages on the right top corner instead of commandline. - + ## 🚀 Usage hardtime.nvim is enabled by default. You can change its state with the following commands: @@ -80,6 +80,7 @@ You can pass your config table into the `setup()` function or `opts` if you use If the option is a boolean, number, or array, your value will overwrite the default configuration. Example: + ```lua -- Add "oil" to the disabled_filetypes disabled_filetypes = { "qf", "netrw", "NvimTree", "lazy", "mason", "oil" }, @@ -88,6 +89,7 @@ disabled_filetypes = { "qf", "netrw", "NvimTree", "lazy", "mason", "oil" }, If the option is a table with a `key = value` pair, your value will overwrite the default if the key exists, and the pair will be appended to the default configuration if the key doesn't exist. You can set `key = {}` to remove the default key-value pair. Example: + ```lua -- Remove keys and append to the disabled_keys disabled_keys = { @@ -98,24 +100,26 @@ disabled_keys = { ### Options -| Option Name | Type | Default Valuae | Meaning | -| --------------------- | ---------------------------- | ---------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `max_time` | number | `1000` | Maximum time (in milliseconds) to consider key presses as repeated. | -| `max_count` | number | `3` | Maximum count of repeated key presses allowed within the `max_time` period. | -| `disable_mouse` | boolean | `true` | Disable mouse support. | -| `hint` | boolean | `true` | Enable hint messages for better commands. | -| `notification` | boolean | `true` | Enable notification messages for restricted and disabled keys. | -| `allow_different_key` | boolean | `true` | Allow different keys to reset the count. | -| `enabled` | boolean | `true` | Whether the plugin is enabled by default or not. | -| `resetting_keys` | table of strings/table pair | [See Config](https://github.com/m4xshen/hardtime.nvim/blob/main/lua/hardtime/config.lua) | Keys in what modes that reset the count. | -| `restricted_keys` | table of strings/table pair | [See Config](https://github.com/m4xshen/hardtime.nvim/blob/main/lua/hardtime/config.lua) | Keys in what modes triggering the count mechanism. | -| `restriction_mode` | string (`"block" or "hint"`) | `"block"` | The behavior when `restricted_keys` trigger count mechanism. | -| `disabled_keys` | table of strings/table pair | [See Config](https://github.com/m4xshen/hardtime.nvim/blob/main/lua/hardtime/config.lua) | Keys in what modes are disabled. | -| `disabled_filetypes` | table of strings | [See Config](https://github.com/m4xshen/hardtime.nvim/blob/main/lua/hardtime/config.lua) | `hardtime.nvim` is disabled under these filetypes. | -| `hints` | table | [See Config](https://github.com/m4xshen/hardtime.nvim/blob/main/lua/hardtime/config.lua) | `key` is a string pattern you want to match, `value` is a table of hint message and pattern length. Learn more about [Lua string pattern](https://www.lua.org/pil/20.2.html). | -| `callback` | function(text) | `vim.notify` | `callback` function can be used to override the default notification behavior. | - -### `hints` example +| Option Name | Type | Default Valuae | Meaning | +| ------------------------ | ---------------------------- | ---------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `max_time` | number | `1000` | Maximum time (in milliseconds) to consider key presses as repeated. | +| `max_count` | number | `3` | Maximum count of repeated key presses allowed within the `max_time` period. | +| `disable_mouse` | boolean | `true` | Disable mouse support. | +| `hint` | boolean | `true` | Enable hint messages for better commands. | +| `notification` | boolean | `true` | Enable notification messages for restricted and disabled keys. | +| `allow_different_key` | boolean | `true` | Allow different keys to reset the count. | +| `enabled` | boolean | `true` | Whether the plugin is enabled by default or not. | +| `resetting_keys` | table of strings/table pair | [See Config](https://github.com/m4xshen/hardtime.nvim/blob/main/lua/hardtime/config.lua) | Keys in what modes that reset the count. | +| `restricted_keys` | table of strings/table pair | [See Config](https://github.com/m4xshen/hardtime.nvim/blob/main/lua/hardtime/config.lua) | Keys in what modes triggering the count mechanism. | +| `restriction_mode` | string (`"block" or "hint"`) | `"block"` | The behavior when `restricted_keys` trigger count mechanism. | +| `disabled_keys` | table of strings/table pair | [See Config](https://github.com/m4xshen/hardtime.nvim/blob/main/lua/hardtime/config.lua) | Keys in what modes are disabled. | +| `disabled_filetypes` | table of strings | [See Config](https://github.com/m4xshen/hardtime.nvim/blob/main/lua/hardtime/config.lua) | `hardtime.nvim` is disabled under these filetypes. | +| `hints` | table | [See Config](https://github.com/m4xshen/hardtime.nvim/blob/main/lua/hardtime/config.lua) | `key` is a string pattern you want to match, `value` is a table of hint message and pattern length. Learn more about [Lua string pattern](https://www.lua.org/pil/20.2.html). | +| `callback` | function(text) | `vim.notify` | `callback` function can be used to override the default notification behavior. | +| `force_exit_insert_mode` | boolean | `false` | Enable forcing exit Insert mode if user is inactive in Insert mode. | +| `max_insert_idle_ms` | number | `5000` | Maximum amount of idle time, in milliseconds, allowed in Insert mode. | + +### `hints` example These are two default hints: diff --git a/lua/hardtime/config.lua b/lua/hardtime/config.lua index 2fabfae..09cae5f 100644 --- a/lua/hardtime/config.lua +++ b/lua/hardtime/config.lua @@ -8,6 +8,8 @@ M.config = { notification = true, allow_different_key = true, enabled = true, + force_exit_insert_mode = false, + max_insert_idle_ms = 5000, resetting_keys = { ["1"] = { "n", "x" }, ["2"] = { "n", "x" }, diff --git a/lua/hardtime/init.lua b/lua/hardtime/init.lua index 60002f1..ad0545e 100644 --- a/lua/hardtime/init.lua +++ b/lua/hardtime/init.lua @@ -5,6 +5,7 @@ local key_count = 0 local last_keys = "" local last_key = "" local mappings +local timer = nil local config = require("hardtime.config").config @@ -112,6 +113,16 @@ local function handler(key) return "" end +local function reset_timer() + if timer then + timer:stop() + end + + if not should_disable() and config.force_exit_insert_mode then + timer = vim.defer_fn(util.stopinsert, config.max_insert_idle_ms) + end +end + local M = {} M.is_plugin_enabled = false @@ -175,11 +186,23 @@ function M.setup(user_config) "BufEnter", { once = true, callback = M.enable } ) + + vim.api.nvim_create_autocmd("InsertEnter", { + group = vim.api.nvim_create_augroup("HardtimeGroup", {}), + callback = function() + reset_timer() + end, + }) end vim.on_key(function(_, k) local mode = vim.fn.mode() - if k == "" or mode == "i" or mode == "c" or mode == "R" then + if k == "" or mode == "c" or mode == "R" then + return + end + + if mode == "i" then + reset_timer() return end diff --git a/lua/hardtime/util.lua b/lua/hardtime/util.lua index 00f055f..24031e5 100644 --- a/lua/hardtime/util.lua +++ b/lua/hardtime/util.lua @@ -8,6 +8,10 @@ local logger = require("hardtime.log").new({ use_console = false, }) +function M.stopinsert() + vim.cmd("stopinsert") +end + function M.get_time() return vim.fn.reltimefloat(vim.fn.reltime()) * 1000 end