Skip to content

Commit

Permalink
Add smartpunc#map_trigger_keys
Browse files Browse the repository at this point in the history
Close gh-28.
  • Loading branch information
kana committed Mar 5, 2012
1 parent 9687d4d commit bf1b80d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 24 deletions.
27 changes: 27 additions & 0 deletions autoload/smartpunc.vim
Expand Up @@ -238,6 +238,33 @@ endfunction



function! smartpunc#map_trigger_keys(...) "{{{2
let overridep = 1 <= a:0 ? a:1 : 0

let all_chars = map(copy(s:available_nrules), 'v:val.char')
let d = {}
for char in all_chars
let d[char] = char
endfor
let unique_chars = keys(d)

let M = function('smartpunc#map_to_trigger')
let map_modifier = overridep ? '' : '<unique>'
for char in unique_chars
" Do not override existing key mappings.
silent! call M(map_modifier . ' ' . char, char, char)
endfor
silent! call M(map_modifier . ' ' . '<C-h>', '<BS>', '<C-h>')
silent! call M(map_modifier . ' ' . '<Return>', '<Enter>', '<Return>')
silent! call M(map_modifier . ' ' . '<C-m>', '<Enter>', '<C-m>')
silent! call M(map_modifier . ' ' . '<CR>', '<Enter>', '<CR>')
silent! call M(map_modifier . ' ' . '<C-j>', '<Enter>', '<C-j>')
silent! call M(map_modifier . ' ' . '<NL>', '<Enter>', '<NL>')
endfunction




"{{{2


Expand Down
9 changes: 9 additions & 0 deletions doc/smartpunc.txt
Expand Up @@ -131,6 +131,7 @@ smartpunc#define_default_rules() *smartpunc#define_default_rules()*
smartpunc#define_rule({rule}) *smartpunc#define_rule()*
Define a rule for the smart input assistant.
See |smartpunc-rules| for the details of {rule}.

*smartpunc#map_to_trigger()*
smartpunc#map_to_trigger({lhs}, {rhs-char}, {rhs-fallback})
Map {lhs} to trigger the smart input assistant for
Expand All @@ -140,6 +141,14 @@ smartpunc#map_to_trigger({lhs}, {rhs-char}, {rhs-fallback})
{rhs-fallback} are strings to denote key sequences.
See also |smartpunc-exampels| for the details.

smartpunc#map_trigger_keys([{overridep}]) *smartpunc#map_trigger_keys()*
Map keys to trigger smart input assistant according to
rules which are currently defined.
Existing key mappings are overridden if {overridep} is
true, otherwise existing key mappings are kept.
If {overridep} is omitted, it is treated as a false
value.


------------------------------------------------------------------------------
KEY MAPPINGS *smartpunc-key-mappings*
Expand Down
28 changes: 4 additions & 24 deletions plugin/smartpunc.vim
Expand Up @@ -29,31 +29,11 @@ endif



function! s:set_up_the_default_configuration()
call smartpunc#define_default_rules()
call smartpunc#define_default_rules()

if !exists('g:smartpunc_no_default_key_mappings')
let all_chars = map(copy(smartpunc#scope().available_nrules), 'v:val.char')
let d = {}
for char in all_chars
let d[char] = char
endfor
let unique_chars = keys(d)

let M = function('smartpunc#map_to_trigger')
for char in unique_chars
" Do not override existing key mappings.
silent! call M('<unique> ' . char, char, char)
endfor
silent! call M('<unique> <C-h>', '<BS>', '<C-h>')
silent! call M('<unique> <Return>', '<Enter>', '<Return>')
silent! call M('<unique> <C-m>', '<Enter>', '<C-m>')
silent! call M('<unique> <CR>', '<Enter>', '<CR>')
silent! call M('<unique> <C-j>', '<Enter>', '<C-j>')
silent! call M('<unique> <NL>', '<Enter>', '<NL>')
endif
endfunction
call s:set_up_the_default_configuration()
if !exists('g:smartpunc_no_default_key_mappings')
call smartpunc#map_trigger_keys()
endif



Expand Down
80 changes: 80 additions & 0 deletions t/map_trigger_keys.vim
@@ -0,0 +1,80 @@
let g:smartpunc_no_default_key_mappings = !0

runtime! plugin/smartpunc.vim

call vspec#hint({'scope': 'smartpunc#scope()', 'sid': 'smartpunc#sid()'})

describe 'smartpunc#map_trigger_keys'
before
new

function! b:get_lhss()
redir => s
0 verbose imap
redir END
let lhss = split(s, '\n')
call map(lhss, 'substitute(v:val, ''\v\S+\s+(\S+)\s+.*'', ''\1'',''g'')')
call sort(lhss)
return lhss
endfunction

imapclear
call smartpunc#clear_rules()
end

after
close!
end

it 'should not map anything but "alias" ones if there is no rule'
call smartpunc#map_trigger_keys()

Expect b:get_lhss() ==# [
\ '<C-H>',
\ '<CR>',
\ '<NL>',
\ ]
end

it 'should not override existing key mappings if overridep is omitted'
inoremap x FOO
call smartpunc#define_rule({'at': '', 'char': 'x', 'input': 'BAR'})
call smartpunc#map_trigger_keys()

Expect b:get_lhss() ==# [
\ '<C-H>',
\ '<CR>',
\ '<NL>',
\ 'x',
\ ]
Expect maparg('x', 'i') ==# 'FOO'
end

it 'should not override existing key mappings if overridep is false'
inoremap x FOO
call smartpunc#define_rule({'at': '', 'char': 'x', 'input': 'BAR'})
call smartpunc#map_trigger_keys(!!0)

Expect b:get_lhss() ==# [
\ '<C-H>',
\ '<CR>',
\ '<NL>',
\ 'x',
\ ]
Expect maparg('x', 'i') ==# 'FOO'
end

it 'should override existing key mappings if overridep is true'
inoremap x FOO
call smartpunc#define_rule({'at': '', 'char': 'x', 'input': 'BAR'})
call smartpunc#map_trigger_keys(!0)

Expect b:get_lhss() ==# [
\ '<C-H>',
\ '<CR>',
\ '<NL>',
\ 'x',
\ ]
Expect maparg('x', 'i') !=# 'FOO'
end
end

0 comments on commit bf1b80d

Please sign in to comment.