Skip to content

Commit

Permalink
feat: force exit insert mode if user is inactive (#114)
Browse files Browse the repository at this point in the history
* feat: force exit insert mode if user is inactive

* feat: updated doc

* bug: fix timer not reseting each time we leave Insert mode

* style: format readme table

---------

Co-authored-by: Max Shen <[email protected]>
  • Loading branch information
csessh and m4xshen authored Oct 12, 2024
1 parent 6513bf4 commit afa7085
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
44 changes: 24 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
hardtime.nvim
</h1>


<div align="center">
<div>Establish good command workflow and quit bad habit.</div><br />
<img src="https://github.com/m4xshen/hardtime.nvim/assets/74842863/117a8d30-64ba-4ca9-8414-5c493cbe8a70" width="700" />
Expand Down Expand Up @@ -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()
```
Expand All @@ -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:
Expand All @@ -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" },
Expand All @@ -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 <Up> keys and append <Space> to the disabled_keys
disabled_keys = {
Expand All @@ -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:

Expand Down
2 changes: 2 additions & 0 deletions lua/hardtime/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
25 changes: 24 additions & 1 deletion lua/hardtime/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lua/hardtime/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit afa7085

Please sign in to comment.