Skip to content

Commit

Permalink
Merge pull request #66 from monaqa/feat-gnormal
Browse files Browse the repository at this point in the history
feat: g<C-a> in NORMAL mode
  • Loading branch information
monaqa committed Apr 5, 2023
2 parents 0fb00f5 + 84c097b commit 5f2bd37
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -50,6 +50,8 @@ To use this plugin, you need to assign the plugin key-mapping to the key you lik
```vim
nmap <C-a> <Plug>(dial-increment)
nmap <C-x> <Plug>(dial-decrement)
nmap g<C-a> g<Plug>(dial-increment)
nmap g<C-x> g<Plug>(dial-decrement)
vmap <C-a> <Plug>(dial-increment)
vmap <C-x> <Plug>(dial-decrement)
vmap g<C-a> g<Plug>(dial-increment)
Expand All @@ -61,6 +63,8 @@ Or you can configure it with Lua as follows:
```lua
vim.keymap.set("n", "<C-a>", require("dial.map").inc_normal(), {noremap = true})
vim.keymap.set("n", "<C-x>", require("dial.map").dec_normal(), {noremap = true})
vim.keymap.set("n", "g<C-a>", require("dial.map").inc_gnormal(), {noremap = true})
vim.keymap.set("n", "g<C-x>", require("dial.map").dec_gnormal(), {noremap = true})
vim.keymap.set("v", "<C-a>", require("dial.map").inc_visual(), {noremap = true})
vim.keymap.set("v", "<C-x>", require("dial.map").dec_visual(), {noremap = true})
vim.keymap.set("v", "g<C-a>",require("dial.map").inc_gvisual(), {noremap = true})
Expand Down
4 changes: 4 additions & 0 deletions README_ja.md
Expand Up @@ -53,6 +53,8 @@
```vim
nmap <C-a> <Plug>(dial-increment)
nmap <C-x> <Plug>(dial-decrement)
nmap g<C-a> g<Plug>(dial-increment)
nmap g<C-x> g<Plug>(dial-decrement)
vmap <C-a> <Plug>(dial-increment)
vmap <C-x> <Plug>(dial-decrement)
vmap g<C-a> g<Plug>(dial-increment)
Expand All @@ -64,6 +66,8 @@ vmap g<C-x> g<Plug>(dial-decrement)
```lua
vim.keymap.set("n", "<C-a>", require("dial.map").inc_normal(), {noremap = true})
vim.keymap.set("n", "<C-x>", require("dial.map").dec_normal(), {noremap = true})
vim.keymap.set("n", "g<C-a>", require("dial.map").inc_gnormal(), {noremap = true})
vim.keymap.set("n", "g<C-x>", require("dial.map").dec_gnormal(), {noremap = true})
vim.keymap.set("v", "<C-a>", require("dial.map").inc_visual(), {noremap = true})
vim.keymap.set("v", "<C-x>", require("dial.map").dec_visual(), {noremap = true})
vim.keymap.set("v", "g<C-a>", require("dial.map").inc_gvisual(), {noremap = true})
Expand Down
12 changes: 10 additions & 2 deletions autoload/dial/operator.vim
@@ -1,9 +1,17 @@
function dial#operator#increment_normal(type, ...)
lua require("dial.command").operator_normal("increment")
lua require("dial.command").operator_normal("increment", false)
endfunction

function dial#operator#decrement_normal(type, ...)
lua require("dial.command").operator_normal("decrement")
lua require("dial.command").operator_normal("decrement", false)
endfunction

function dial#operator#increment_gnormal(type, ...)
lua require("dial.command").operator_normal("increment", true)
endfunction

function dial#operator#decrement_gnormal(type, ...)
lua require("dial.command").operator_normal("decrement", true)
endfunction

function dial#operator#increment_visual(type, ...)
Expand Down
87 changes: 87 additions & 0 deletions doc/dial.jax
Expand Up @@ -29,6 +29,7 @@ Configurations |dial-config|
User |dial-augends-user|
Advanced Usage |dial-advanced-usage|
Dot Repeating |dial-dot-repeating|
Additive Dot Repeating |dial-additive-dot-repeating|
Interface |dial-interface|
Mapping |dial-mapping|
Command |dial-command|
Expand Down Expand Up @@ -95,6 +96,8 @@ USAGE *dial-usage*
>
nmap <C-a> <Plug>(dial-increment)
nmap <C-x> <Plug>(dial-decrement)
nmap g<C-a> g<Plug>(dial-increment)
nmap g<C-x> g<Plug>(dial-decrement)
vmap <C-a> <Plug>(dial-increment)
vmap <C-x> <Plug>(dial-decrement)
vmap g<C-a> g<Plug>(dial-increment)
Expand All @@ -109,6 +112,12 @@ USAGE *dial-usage*
vim.keymap.set("n", "<C-x>", function()
require("dial.map").manipulate("decrement", "normal")
end)
vim.keymap.set("n", "g<C-a>", function()
require("dial.map").manipulate("increment", "gnormal")
end)
vim.keymap.set("n", "g<C-x>", function()
require("dial.map").manipulate("decrement", "gnormal")
end)
vim.keymap.set("v", "<C-a>", function()
require("dial.map").manipulate("increment", "visual")
end)
Expand Down Expand Up @@ -1264,6 +1273,64 @@ DOT REPEATING *dial-dot-repeating*
したがって上の例では、ひとたび検索を行った後であれば `.n.n.` とするだけですべ
ての期限を1ヶ月延ばすことができるのです。

------------------------------------------------------------------------------
ADDITIVE DOT REPEATING *dial-additive-dot-repeating*

Vim の便利な機能の1つに、VISUAL モードにおける g<C-a> があります
|v_g_CTRL-A| 参照)。これは加数を行ごとに1ずつ増やすことができる機能であり、
番号付きの箇条書きなどを生成する際に重宝します。もちろん |dial.nvim| でも同等
の機能を備えています (|v_g<Plug>(dial-increment)|)。この機能は便利ながら、増加
対象が離れた場所にあると使いづらいという制限がありました。
これを解決するため、|dial.nvim| ではノーマルモードの g<C-a> に相当する機能を提
供しています。これを有効にするには、以下の設定を書いておく必要があります:
>
nmap g<C-a> g<Plug>(dial-increment)
nmap g<C-x> g<Plug>(dial-decrement)
<
もしくは Lua 上で以下のように設定することもできます。
>
vim.keymap.set("n", "g<C-a>", function()
require("dial.map").manipulate("increment", "gnormal")
end)
vim.keymap.set("n", "g<C-x>", function()
require("dial.map").manipulate("decrement", "gnormal")
end)
ノーマルモード上での g<C-a> の動作は通常の <C-a> とほぼ同じですが、ドットリ
ピートを実行したときの挙動のみが異なります。 <C-a> でドットリピートを実行した
とき加数は [count] のままですが、 g<C-a> でドットリピートすると加数が [count]
ずつ増えていきます。

たとえば、以下のようなバッファを考えます。なにか適当な関数を定義し、同じものを
2回コピーした場面です。このままでは関数名が衝突してしまうため、関数の数字を1つ
ずつインクリメントして `test_foo_1`, `test_foo_2`, `test_foo_3` という名前に変
更したいものとします。
>
def test_foo_0():
assert 100 + 100 == 200
def test_foo_0():
assert 100 + 100 == 200
def test_foo_0():
assert 100 + 100 == 200
このような関数の命名が良いのかどうかはさておき、テキスト編集を行う上での直近の
問題は編集対象の行が離れていることです。このままではビジュアルモードでの g<C-a>
が使えません。
しかし、ノーマルモードの g<C-a> であれば簡単に解決できます。

1. `test_foo_0` を検索し、g<C-a> を押す。
2. `n` で次の検索結果に移動し、 `.` を押す(ドットリピート)。
3. `n` で次の検索結果に移動し、 `.` を押す(ドットリピート)。

これだけで `test_foo_1`, `test_foo_2`, `test_foo_3` という関数名が得られたはず
です。

これだけ見ると被加数が延々と増加してしまうのではと感じたかもしれませんが、そん
なことはありません。被加数が増加するのは g<C-a> でドットリピートを実行したとき
だけであり、<C-a> や g<C-a> を改めて押せば数値はリセットされます。

==============================================================================
INTERFACE *dial-interface*

Expand Down Expand Up @@ -1331,6 +1398,14 @@ MAPPING *dial-mapping*
{VISUAL}["x]g<Plug>(dial-decrement) *v_g<Plug>(dial-decrement)*
VISUAL モードにおける |v_g_CTRL-X| に相当する機能を提供します。

["x]g<Plug>(dial-increment) *g<Plug>(dial-increment)*
基本的に |<Plug>(dial-increment)| と同じですが、ドットリピートを実行したと
きの挙動が異なります。 |dial-additive-dot-repeating| 参照。

["x]g<Plug>(dial-decrement) *g<Plug>(dial-decrement)*
基本的に |<Plug>(dial-decrement)| と同じですが、ドットリピートを実行したと
きの挙動が異なります。 |dial-additive-dot-repeating| 参照。

------------------------------------------------------------------------------
COMMAND *dial-command*

Expand Down Expand Up @@ -1397,6 +1472,18 @@ require("dial.map").dec_normal([group_name])
NORMAL モードにおいて与えられたグループ名をもとにデクリメントを行うための
キーシーケンスを出力します。

*dial.map.inc_normal()*
require("dial.map").inc_gnormal([group_name])

加算的なインクリメントを行うためのキーシーケンスを表す文字列を出力します。
|dial-additive-dot-repeating| 参照。

*dial.map.dec_normal()*
require("dial.map").dec_gnormal([group_name])

加算的なデクリメントを行うためのキーシーケンスを表す文字列を出力します。
|dial-additive-dot-repeating| 参照。

*dial.map.inc_visual()*
require("dial.map").inc_visual([group_name])

Expand Down
89 changes: 89 additions & 0 deletions doc/dial.txt
Expand Up @@ -30,6 +30,7 @@ Configurations |dial-config|
User |dial-augends-user|
Advanced Usage |dial-advanced-usage|
Dot Repeating |dial-dot-repeating|
Additive Dot Repeating |dial-additive-dot-repeating|
Interface |dial-interface|
Mapping |dial-mapping|
Command |dial-command|
Expand Down Expand Up @@ -98,6 +99,8 @@ plugin.
>
nmap <C-a> <Plug>(dial-increment)
nmap <C-x> <Plug>(dial-decrement)
nmap g<C-a> g<Plug>(dial-increment)
nmap g<C-x> g<Plug>(dial-decrement)
vmap <C-a> <Plug>(dial-increment)
vmap <C-x> <Plug>(dial-decrement)
vmap g<C-a> g<Plug>(dial-increment)
Expand All @@ -112,6 +115,12 @@ Alternatively, you can configure with Lua as follows:
vim.keymap.set("n", "<C-x>", function()
require("dial.map").manipulate("decrement", "normal")
end)
vim.keymap.set("n", "g<C-a>", function()
require("dial.map").manipulate("increment", "gnormal")
end)
vim.keymap.set("n", "g<C-x>", function()
require("dial.map").manipulate("decrement", "gnormal")
end)
vim.keymap.set("v", "<C-a>", function()
require("dial.map").manipulate("increment", "visual")
end)
Expand Down Expand Up @@ -1320,6 +1329,66 @@ the nearest "month" of the date and then the same increment operation is
performed. Thus, in the above example, once you have done the search, you can
extend all the due dates by one month just by typing `.n.n.`.

------------------------------------------------------------------------------
ADDITIVE DOT REPEATING *dial-additive-dot-repeating*

One convenient feature of Vim is g<C-a> in VISUAL mode (see |v_g_CTRL-A|).
This allows you to increase the addend by one per line, which is useful for
generating numbered list, etc. Of course, |dial.nvim| provides the same
functionality (|v_g<Plug>(dial-increment)|). However, this feature had the
limitation that it was difficult to use when the increase targets were located
far away.
To solve this, |dial.nvim| provides the equivalent of g<C-a> in normal mode.
To enable it, the following configuration must be written: .
>
nmap g<C-a> g<Plug>(dial-increment)
nmap g<C-x> g<Plug>(dial-decrement)
Alternatively, you can configure with Lua as follows:
>
vim.keymap.set("n", "g<C-a>", function()
require("dial.map").manipulate("increment", "gnormal")
end)
vim.keymap.set("n", "g<C-x>", function()
require("dial.map").manipulate("decrement", "gnormal")
end)
The behavior of g<C-a> in normal mode is almost the same as normal <C-a>, but
only the behavior during dot repeating is different. While the addend remains
[count] in dot repeating of <C-a>, the addend is increased by [count] in dot
repeating of g<C-a>.

For example, consider the following buffer, where you have defined some
function and copied it twice. Since the function names conflict, we want to
rename the functions test_foo_1, test_foo_2, and test_foo_3 by incrementing
the function number by one.

>
def test_foo_0():
assert 100 + 100 == 200
def test_foo_0():
assert 100 + 100 == 200
def test_foo_0():
assert 100 + 100 == 200
We do not consider whether such function naming is a good idea. Anyway, the
most immediate problem is that the lines to be edited are far apart. As it is,
you cannot use g<C-a> in VISUAL mode.
However, this can be easily resolved with g<C-a> in NORMAL mode.

1. Search for `test_foo_0`, jump, and press g<C-a>.
2. Press `n` to move to the next result and press `.` (dot repeat).
3. Press `n` to move to the next result and press `.` (dot repeat).

This should be enough to obtain the function names `test_foo_1`, `test_foo_2`,
and `test_foo_3`.

You may think that the number of addend will increase endlessly, but this is
not true. The addend increases only when you execute a dot repeat, and the
number is reset when you press <C-a> or g<C-a> again.

==============================================================================
INTERFACE *dial-interface*

Expand Down Expand Up @@ -1390,6 +1459,14 @@ MAPPING *dial-mapping*
{VISUAL}["x]g<Plug>(dial-decrement) *v_g<Plug>(dial-decrement)*
An alternative to the existing |v_g_CTRL-X| command in VISUAL mode.

["x]g<Plug>(dial-increment) *g<Plug>(dial-increment)*
Almost the same as |<Plug>(dial-increment)|, but but the behavior differs
during dot repeating. See |dial-additive-dot-repeating|.

["x]g<Plug>(dial-decrement) *g<Plug>(dial-decrement)*
Almost the same as |<Plug>(dial-decrement)|, but but the behavior differs
during dot repeating. See |dial-additive-dot-repeating|.

------------------------------------------------------------------------------
COMMAND *dial-command*

Expand Down Expand Up @@ -1449,6 +1526,18 @@ require("dial.map").inc_normal([group_name])
)
<

*dial.map.inc_normal()*
require("dial.map").inc_gnormal([group_name])

Outputs a key sequence for additive incrementing. See
|dial-additive-dot-repeating|.

*dial.map.dec_normal()*
require("dial.map").dec_gnormal([group_name])

Outputs a key sequence for additive decrementing. See
|dial-additive-dot-repeating|.

*dial.map.dec_normal()*
require("dial.map").dec_normal([group_name])

Expand Down
9 changes: 7 additions & 2 deletions lua/dial/command.lua
Expand Up @@ -34,6 +34,10 @@ function M.select_augend_normal(group_name)
handler:select_augend(line, col, augends)
end

function M.select_augend_gnormal(group_name)
return M.select_augend_normal(group_name)
end

---Select the most appropriate augend from given augend group (in VISUAL mode).
---@param group_name? string
function M.select_augend_visual(group_name)
Expand Down Expand Up @@ -102,12 +106,13 @@ end

---The process that runs when operator is called (in NORMAL mode).
---@param direction direction
function M.operator_normal(direction)
---@param additive? boolean
function M.operator_normal(direction, additive)
local col = vim.fn.col "."
local line_num = vim.fn.line "."
local line = vim.fn.getline "."

local result = handler:operate(line, col, direction)
local result = handler:operate(line, col, direction, additive)

if result.line ~= nil then
vim.fn.setline(".", result.line)
Expand Down

0 comments on commit 5f2bd37

Please sign in to comment.