ZyX
ZyX

Reputation: 53604

Preventing redefining existing buffer mapping

I want to prevent *noremap command from remapping an existing sequence, but only if this sequence is local to buffer:

noremap a b
" Will fail, must succeed
noremap <buffer> <unique> a c

noremap <buffer> a b
" Will fail, OK
noremap <unique> <buffer> a c

noremap a b
noremap <buffer> a c
" Will fail, OK
noremap <unique> <buffer> a d

Upvotes: 2

Views: 99

Answers (1)

ZyX
ZyX

Reputation: 53604

With newer vim one can use maparg() with fourth argument:

let oldmap=maparg('a', '', 0, 1)
if empty(oldmap) || !oldmap.buffer
    noremap <buffer> a c
endif

or, better (also supports older vim), but requires frawor:

execute frawor#Setup('0.0', {'@/mappings': '0.0'})
let oldmap=s:_r.map.maparg('a', 'n', 0)
if empty(oldmap) || !oldmap.buffer
    noremap <buffer> a c
endif

Upvotes: 1

Related Questions