Skip to content

Commit ff454c7

Browse files
committed
Test and doc fixes for keymapping helpers from code review
1 parent 904530d commit ff454c7

File tree

4 files changed

+70
-56
lines changed

4 files changed

+70
-56
lines changed

autoload/maktaba/keymapping.vim

+20-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""
22
" @dict KeyMapping
3-
" A maktaba representation of a vim key mapping, which is used to configure and
4-
" unmap it from vim.
3+
" A maktaba representation of a Vim key mapping, which is used to configure and
4+
" unmap it from Vim.
55

66

77
if !exists('s:next_keymap_id')
@@ -35,6 +35,7 @@ function! s:GetFuncCallKeystrokes(funcstr, mode) abort
3535
elseif a:mode ==# 'i'
3636
return printf('<C-\><C-o>:call %s<CR>', a:funcstr)
3737
elseif a:mode ==# 'v'
38+
" Needs C-u to remove "'<,'>" range inserted for commands from visual mode.
3839
" Uses "gv" at the end to re-enter visual mode.
3940
return printf(':<C-u>call %s<CR>gv', a:funcstr)
4041
elseif a:mode ==# 's'
@@ -48,7 +49,16 @@ endfunction
4849

4950
""
5051
" @dict KeyMapping
51-
" Unmaps the mapping in vim.
52+
" Returns 1 if the mapping is still defined, 0 otherwise
53+
function! maktaba#keymapping#IsMapped() dict abort
54+
let l:foundmap = maparg(self._lhs, self._mode, 0, 1)
55+
return !empty(l:foundmap) && l:foundmap == self._maparg
56+
endfunction
57+
58+
59+
""
60+
" @dict KeyMapping
61+
" Unmaps the mapping in Vim.
5262
" Returns 1 if mapping was found and unmapped, 0 if mapping was gone already.
5363
function! maktaba#keymapping#Unmap() dict abort
5464
if self.IsMapped()
@@ -68,18 +78,6 @@ function! maktaba#keymapping#Unmap() dict abort
6878
endfunction
6979

7080

71-
""
72-
" @dict KeyMapping
73-
" Returns 1 if the mapping is still defined, 0 otherwise
74-
"
75-
" Caveat: This detection can currently false positive if the original mapping
76-
" was unmapped but then another similar one mapped afterwards.
77-
function! maktaba#keymapping#IsMapped() dict abort
78-
let l:foundmap = maparg(self._lhs, self._mode, 0, 1)
79-
return !empty(l:foundmap) && l:foundmap == self._maparg
80-
endfunction
81-
82-
8381
""
8482
" @dict KeyMapping
8583
" Return a copy of the spec used to issue this mapping.
@@ -94,16 +92,16 @@ let s:GetSpec = function('maktaba#keymapping#GetSpec')
9492

9593

9694
""
97-
" Set up a key mapping in vim, mapping key sequence {lhs} to replacement
95+
" Set up a key mapping in Vim, mapping key sequence {lhs} to replacement
9896
" sequence {rhs} in the given [mode]. This is a convenience wrapper for
99-
" @function(#Spec) and its |KeyMappingSpec.Map| that supports the basic mapping
100-
" options. It is equivalent to calling: >
97+
" @function(#Spec) and its |KeyMappingSpec.Map()| that supports the basic
98+
" mapping options. It is equivalent to calling: >
10199
" :call maktaba#keymapping#Spec({lhs}, {rhs}, [mode]).Map()
102100
" <
103101
"
104102
" See those functions for usage and behavior details.
105103
"
106-
" @default mode=all of 'n', 'v', and 'o' (vim's default)
104+
" @default mode=all of 'n', 'v', and 'o' (Vim's default)
107105
function! maktaba#keymapping#Map(lhs, rhs, ...) abort
108106
if a:0 >= 1
109107
let l:spec = maktaba#keymappingspec#Spec(a:lhs, a:rhs, a:1)
@@ -179,7 +177,7 @@ endfunction
179177
""
180178
" @private
181179
" @dict KeyMapping
182-
" Defines the key mapping in vim via the |:map| commands for the keymap in self.
180+
" Defines the key mapping in Vim via the |:map| commands for the keymap in self.
183181
" Core internal implementation of @function(KeyMappingSpec.Map).
184182
function! maktaba#keymapping#MapSelf() dict abort
185183
" TODO(dbarnett): Perform a sweep for expired mapping timeouts before trying
@@ -199,7 +197,7 @@ endfunction
199197
""
200198
" @private
201199
" @dict KeyMapping
202-
" Define a buffer-local one-shot vim mapping from spec that will only trigger
200+
" Define a buffer-local one-shot Vim mapping from spec that will only trigger
203201
" once and then unmap itself.
204202
"
205203
" @throws NotImplemented if used with `WithRemap(1)`
@@ -226,7 +224,7 @@ endfunction
226224
""
227225
" @private
228226
" @dict KeyMapping
229-
" Define a short-lived vim mapping from spec that will only trigger once and
227+
" Define a short-lived Vim mapping from spec that will only trigger once and
230228
" will also expire if 'timeoutlen' duration expires with 'timeout' setting
231229
" active. See |KeyMappingSpec.MapOnceWithTimeout()| for details.
232230
"

autoload/maktaba/keymappingspec.vim

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ let s:plugin = maktaba#Maktaba()
33

44
""
55
" @dict KeyMappingSpec
6-
" A spec for a key mapping that can be mapped in vim with
6+
" A spec for a key mapping that can be mapped in Vim with
77
" @function(KeyMappingSpec.Map).
88

99

1010
""
1111
" Create a @dict(KeyMappingSpec) that can be configured with options and then
12-
" mapped in vim with @function(KeyMappingSpec.Map). The eventual mapping will
12+
" mapped in Vim with @function(KeyMappingSpec.Map). The eventual mapping will
1313
" map {lhs} to {rhs} in the given [mode].
1414
"
1515
" Initialized with recursive mappings disallowed by default (see |:noremap|).
@@ -18,7 +18,7 @@ let s:plugin = maktaba#Maktaba()
1818
" Arguments will be configured with |map-<unique>| by default. Use the
1919
" "overwrite" option of `.WithArgs(…, 1)` if you want to map without <unique>.
2020
"
21-
" @default mode=all of 'n', 'v', and 'o' (vim's default)
21+
" @default mode=all of 'n', 'v', and 'o' (Vim's default)
2222
function! maktaba#keymappingspec#Spec(lhs, rhs, ...) abort
2323
let l:mode = get(a:, 1, '')
2424

@@ -76,6 +76,7 @@ endfunction
7676

7777
""
7878
" @dict KeyMappingSpec.Map
79+
" Define a Vim mapping from spec via the |:map| commands.
7980
function! maktaba#keymappingspec#MapSelf() dict abort
8081
let l:keymap = maktaba#keymapping#PopulateFromSpec(self)
8182
call l:keymap._DoMap()
@@ -85,7 +86,7 @@ endfunction
8586

8687
""
8788
" @dict KeyMappingSpec.MapOnce
88-
" Define a buffer-local one-shot vim mapping from spec that will only trigger
89+
" Define a buffer-local one-shot Vim mapping from spec that will only trigger
8990
" once and then unmap itself.
9091
"
9192
" Not supported for recursive mappings.
@@ -99,14 +100,14 @@ endfunction
99100

100101
""
101102
" @dict KeyMappingSpec.MapOnceWithTimeout
102-
" Define a short-lived vim mapping from spec that will only trigger once and
103+
" Define a short-lived Vim mapping from spec that will only trigger once and
103104
" will also expire if 'timeoutlen' duration expires with 'timeout' setting
104105
" active.
105106
"
106107
" This is useful to detect if the user presses a key immediately after something
107108
" else happens, and respond with a particular behavior.
108109
"
109-
" It can also be used for an improved version of vim's |map-ambiguous| behavior
110+
" It can also be used for an improved version of Vim's |map-ambiguous| behavior
110111
" when one mapping is a prefix of another. You can create a prefix mapping that
111112
" does one thing immediately and then a different follow-up behavior on another
112113
" keystroke.
@@ -117,7 +118,7 @@ endfunction
117118
" \.WithArgs(['<buffer>']).MapOnceWithTimeout()<CR>
118119
" <
119120
"
120-
" Caveat: Unlike vim's |map-ambiguous| behavior, this currently doesn't stop
121+
" Caveat: Unlike Vim's |map-ambiguous| behavior, this currently doesn't stop
121122
" waiting for keypresses if another unrelated key is pressed while it's waiting.
122123
" Caveat: For long mappings, you might notice that the timeout is currently for
123124
" the entire mapping and not for each keystroke. If you need to work around that

doc/maktaba.txt

+16-18
Original file line numberDiff line numberDiff line change
@@ -244,24 +244,21 @@ Flag.Translate({value}) *Flag.Translate()*
244244
running {value} through all registered translators).
245245

246246
*maktaba.KeyMapping*
247-
A maktaba representation of a vim key mapping, which is used to configure and
248-
unmap it from vim.
249-
250-
KeyMapping.Unmap() *KeyMapping.Unmap()*
251-
Unmaps the mapping in vim. Returns 1 if mapping was found and unmapped, 0 if
252-
mapping was gone already.
247+
A maktaba representation of a Vim key mapping, which is used to configure and
248+
unmap it from Vim.
253249

254250
KeyMapping.IsMapped() *KeyMapping.IsMapped()*
255251
Returns 1 if the mapping is still defined, 0 otherwise
256252

257-
Caveat: This detection can currently false positive if the original mapping
258-
was unmapped but then another similar one mapped afterwards.
253+
KeyMapping.Unmap() *KeyMapping.Unmap()*
254+
Unmaps the mapping in Vim. Returns 1 if mapping was found and unmapped, 0 if
255+
mapping was gone already.
259256

260257
KeyMapping.GetSpec() *KeyMapping.GetSpec()*
261258
Return a copy of the spec used to issue this mapping.
262259

263260
*maktaba.KeyMappingSpec*
264-
A spec for a key mapping that can be mapped in vim with
261+
A spec for a key mapping that can be mapped in Vim with
265262
|KeyMappingSpec.Map()|.
266263

267264
KeyMappingSpec.WithArgs({args}, [overwrite]) *KeyMappingSpec.WithArgs()*
@@ -276,23 +273,24 @@ KeyMappingSpec.WithRemap({enabled}) *KeyMappingSpec.WithRemap()*
276273
Throws ERROR(WrongType)
277274

278275
KeyMappingSpec.Map() *KeyMappingSpec.Map()*
276+
Define a Vim mapping from spec via the |:map| commands.
279277

280278
KeyMappingSpec.MapOnce() *KeyMappingSpec.MapOnce()*
281-
Define a buffer-local one-shot vim mapping from spec that will only trigger
279+
Define a buffer-local one-shot Vim mapping from spec that will only trigger
282280
once and then unmap itself.
283281

284282
Not supported for recursive mappings.
285283
Throws ERROR(NotImplemented) if used with `WithRemap(1)`
286284

287285
KeyMappingSpec.MapOnceWithTimeout() *KeyMappingSpec.MapOnceWithTimeout()*
288-
Define a short-lived vim mapping from spec that will only trigger once and
286+
Define a short-lived Vim mapping from spec that will only trigger once and
289287
will also expire if 'timeoutlen' duration expires with 'timeout' setting
290288
active.
291289

292290
This is useful to detect if the user presses a key immediately after
293291
something else happens, and respond with a particular behavior.
294292

295-
It can also be used for an improved version of vim's |map-ambiguous|
293+
It can also be used for an improved version of Vim's |map-ambiguous|
296294
behavior when one mapping is a prefix of another. You can create a prefix
297295
mapping that does one thing immediately and then a different follow-up
298296
behavior on another keystroke.
@@ -304,7 +302,7 @@ KeyMappingSpec.MapOnceWithTimeout() *KeyMappingSpec.MapOnceWithTimeout()*
304302
\.WithArgs(['<buffer>']).MapOnceWithTimeout()<CR>
305303
<
306304

307-
Caveat: Unlike vim's |map-ambiguous| behavior, this currently doesn't stop
305+
Caveat: Unlike Vim's |map-ambiguous| behavior, this currently doesn't stop
308306
waiting for keypresses if another unrelated key is pressed while it's
309307
waiting. Caveat: For long mappings, you might notice that the timeout is
310308
currently for the entire mapping and not for each keystroke. If you need to
@@ -1246,22 +1244,22 @@ maktaba#json#python#IsDisabled() *maktaba#json#python#IsDisabled()*
12461244
disabled, and prevents further changes.
12471245

12481246
maktaba#keymapping#Map({lhs}, {rhs}, [mode]) *maktaba#keymapping#Map()*
1249-
Set up a key mapping in vim, mapping key sequence {lhs} to replacement
1247+
Set up a key mapping in Vim, mapping key sequence {lhs} to replacement
12501248
sequence {rhs} in the given [mode]. This is a convenience wrapper for
1251-
|maktaba#keymapping#Spec()| and its |KeyMappingSpec.Map| that supports the
1249+
|maktaba#keymapping#Spec()| and its |KeyMappingSpec.Map()| that supports the
12521250
basic mapping options. It is equivalent to calling:
12531251
>
12541252
:call maktaba#keymapping#Spec({lhs}, {rhs}, [mode]).Map()
12551253
<
12561254

12571255
See those functions for usage and behavior details.
12581256

1259-
[mode] is all of 'n', 'v', and 'o' (vim's default) if omitted.
1257+
[mode] is all of 'n', 'v', and 'o' (Vim's default) if omitted.
12601258

12611259
maktaba#keymappingspec#Spec({lhs}, {rhs}, [mode])
12621260
*maktaba#keymappingspec#Spec()*
12631261
Create a |maktaba.KeyMappingSpec| that can be configured with options and
1264-
then mapped in vim with |KeyMappingSpec.Map()|. The eventual mapping will
1262+
then mapped in Vim with |KeyMappingSpec.Map()|. The eventual mapping will
12651263
map {lhs} to {rhs} in the given [mode].
12661264

12671265
Initialized with recursive mappings disallowed by default (see |:noremap|).
@@ -1270,7 +1268,7 @@ maktaba#keymappingspec#Spec({lhs}, {rhs}, [mode])
12701268
Arguments will be configured with |map-<unique>| by default. Use the
12711269
"overwrite" option of `.WithArgs(…, 1)` if you want to map without <unique>.
12721270

1273-
[mode] is all of 'n', 'v', and 'o' (vim's default) if omitted.
1271+
[mode] is all of 'n', 'v', and 'o' (Vim's default) if omitted.
12741272

12751273
maktaba#library#Import({library}) *maktaba#library#Import()*
12761274
Imports {library}.

vroom/keymapping.vroom

+26-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Now let's try it out!
2929
:normal i
3030
~ Nope!
3131

32-
Well, that was fun, but let's unlet our mapping and restore the original
32+
Well, that was fun, but let's unmap our mapping and restore the original
3333
behavior.
3434

3535
:call evil_mapping.Unmap()
@@ -62,19 +62,36 @@ open parentheses.
6262

6363

6464

65-
Vim also supports special arguments to mappings, such as the "<unique>" arg to
66-
avoid overriding any existing mapping. Let's define a unique mapping and verify
67-
that it's behaving politely.
65+
Vim also supports special arguments to mappings to affect mapping behavior (see
66+
:help :map-arguments). Let's use the <buffer> argument to define a buffer-local
67+
mapping.
6868

6969
Maktaba provides a builder interface for mappings configuration more complex
7070
than LHS, RHS, and mode. That will let us pass arguments in to the mapping we're
7171
defining.
7272

73-
:let first_mapping = maktaba#keymapping#Map('!', '?')
74-
:let polite_spec = maktaba#keymappingspec#Spec('!', '??')
75-
|.WithArgs(['<unique>'])
76-
:call maktaba#error#Try(maktaba#function#Method(polite_spec, 'Map'))
77-
~ Vim(noremap):E227: * (glob)
73+
:let buffer_mapping = maktaba#keymappingspec#Spec('!', '?')
74+
|.WithArgs(['<buffer>'])
75+
|.Map()
76+
:let mapping_info = maparg('!', '', 0, 1)
77+
:echomsg mapping_info.buffer
78+
~ 1
79+
:call buffer_mapping.Unmap()
80+
81+
82+
By default Maktaba uses <unique> and defines non-recursive mappings to avoid
83+
unpleasant surprises, since programmatically-defined mappings usually wouldn't
84+
intend to silently override existing mappings or expand recursively-nested
85+
mappings. The spec interface allows you to override these default behaviors.
86+
87+
:let recursive_nonunique_mapping = maktaba#keymappingspec#Spec('!', '?')
88+
|.WithArgs([], 1)
89+
|.WithRemap(1)
90+
|.Map()
91+
:let mapping_info = maparg('!', '', 0, 1)
92+
:echomsg mapping_info.noremap
93+
~ 0
94+
:call recursive_nonunique_mapping.Unmap()
7895

7996

8097

0 commit comments

Comments
 (0)