kana / vim-submode

Vim plugin: Create your own submodes
http://www.vim.org/scripts/script.php?script_id=2467
217 stars 24 forks source link

Enter with mapping `<C-w><M-Left>` failed #33

Closed laggardkernel closed 4 years ago

laggardkernel commented 5 years ago
## versions

vim version: NVIM v0.3.1
term: iTerm.app
platform: darwin

Config

call submode#enter_with('tab/window', 'n', '', '<C-w>h', '<C-w>h')
call submode#enter_with('tab/window', 'n', '', '<C-w>j', '<C-w>j')
call submode#enter_with('tab/window', 'n', '', '<C-w>k', '<C-w>k')
call submode#enter_with('tab/window', 'n', '', '<C-w>l', '<C-w>l')
call submode#leave_with('tab/window', 'n', '', '<Esc>')
call submode#map('tab/window', 'n', '', 'h', '<C-w>h')
call submode#map('tab/window', 'n', '', 'j', '<C-w>j')
call submode#map('tab/window', 'n', '', 'k', '<C-w>k')
call submode#map('tab/window', 'n', '', 'l', '<C-w>l')

call submode#enter_with('tab/window', 'n', '', '<C-w><M-Left>', ':call TmuxResize("h",5)<CR>')
call submode#enter_with('tab/window', 'n', '', '<C-w><M-Down>', ':call TmuxResize("j",5)<CR>')
call submode#enter_with('tab/window', 'n', '', '<C-w><M-Up>', ':call TmuxResize("k",5)<CR>')
call submode#enter_with('tab/window', 'n', '', '<C-w><M-Right>', ':call TmuxResize("l",5)<CR>')
" call submode#leave_with('tab/window', 'n', '', '<Esc>')
call submode#map('tab/window', 'n', '', '<M-Left>', ':call TmuxResize("h",5)<CR>')
call submode#map('tab/window', 'n', '', '<M-Down>', ':call TmuxResize("j",5)<CR>')
call submode#map('tab/window', 'n', '', '<M-Up>', ':call TmuxResize("k",5)<CR>')
call submode#map('tab/window', 'n', '', '<M-Right>', ':call TmuxResize("l",5)<CR>')

Errors

Error detected while processing function submode#enter_with[3]..<SNR>18_define_entering_mapping:
line   16:
E474: Invalid argument
E474: Invalid argument
E474: Invalid argument
E474: Invalid argument
Press ENTER or type command to continue

It seems only the submode#enter_with related to <C-w><M-*> failed. If I disable this part, the corresponding mapping still works as a submode.

laggardkernel commented 4 years ago

Closed because of inactivity.

weibeld commented 1 year ago

I had this same issue, which seems to be caused by the maximum length limit of 50 for lhs (left-hand side) in the mapping that is defined by the define_entering_mapping function as described in the documentation:

The maximum length of {lhs} of normal key mappings is 50. Since submode key mappings are built on top of normal key mappings, the maximum length of a submode {lhs} is shorter than 50. Usually 10 or more characters are available, depending on a submode name. And it is enough length because you don't want to type 4 or more keys to execute a command.

The above can also be found with :help submode-bugs.

Now, the lhs mapping that you define (<C-w><M-*>) is way less than 50 characters, but vim-submode actually concatenates this with a lot of stuff before passing it to nnoremap. In particular, the resulting lhs is something like <Plug>(submode-before-entering:SUBMODE-NAME:with:YOUR-LHS), which is already close to 50 characters. Moreover, it includes the name of the submode, thus the longer the submode name, the fewer characters are available for the mapping.

This is also discussed in issue #2.

It seems that in four of your submode#enter_with commands (those with the longest mappings), you just hit this length limit resulting in nnoremap throwing an error. Try changing the name of your submode from tab/window to t or something very short, and it will probably work. Conversely, if you change the submode name to something very long, the other commands will fail too.

This is of course a terribly nasty bug and it's not clearly stated in e.g. the README. In the documentation it just states that "Usually 10 or more characters are available, depending on a submode name", however, this is not guaranteed at all and depends entirely on the length of the submode name.

It could probably also be easily fixed by making the prefix that is concatenated with the user-provided mapping much shorter, or imposing a maximum length on the submode name, or just implementing a check with a clear error message that the concatenated lhs is too long. However, I'm not sure if the maintainer @kana is still actively maintaining this project to accept PRs.