0

I am trying to reproduce this vimscript code

function! NormalRemap()
    :nnoremap <silent> <nowait> I I
    :nnoremap <silent> <nowait> a a
    :nnoremap <silent> <nowait> o o
    :nnoremap <silent> <nowait> O O
    :nnoremap <silent> <nowait> c c
    :nnoremap <silent> <nowait> C C
    :nnoremap <silent> <nowait> s s
    :nnoremap <silent> <nowait> S S
    :nnoremap <silent> <nowait> v v
    :nnoremap <silent> <nowait> V V
    :nnoremap <silent> <nowait> r r
    :nnoremap <silent> <nowait> R R
    :nnoremap <silent> <nowait> q q
endfunction

function! NormalUnmap()
    :nnoremap <silent> <nowait> I <nop>
    :nnoremap <silent> <nowait> a <nop>
    :nnoremap <silent> <nowait> o <nop>
    :nnoremap <silent> <nowait> O <nop>
    :nnoremap <silent> <nowait> c <nop>
    :nnoremap <silent> <nowait> C <nop>
    :nnoremap <silent> <nowait> s <nop>
    :nnoremap <silent> <nowait> S <nop>
    :nnoremap <silent> <nowait> v <nop>
    :nnoremap <silent> <nowait> V <nop>
    :nnoremap <silent> <nowait> r <nop>
    :nnoremap <silent> <nowait> R <nop>
    :nnoremap <silent> <nowait> q <nop>
endfunction

function! DoxyGenComment()
    :call NormalRemap()
    :Dox
    :call NormalUnmap()
endfunction

(which works fine) with the following one:

let mymap = 1

function! DoxyGenComment()
    let g:mymap = 0
    :Dox
    let g:mymap = 1
endfunction

:nnoremap <expr> I (mymap == 1) ? '<nop>' : 'I'
:nnoremap <expr> a (mymap == 1) ? '<nop>' : 'a'
:nnoremap <expr> o (mymap == 1) ? '<nop>' : 'o'
:nnoremap <expr> O (mymap == 1) ? '<nop>' : 'O'
:nnoremap <expr> c (mymap == 1) ? '<nop>' : 'c'
:nnoremap <expr> C (mymap == 1) ? '<nop>' : 'C'
:nnoremap <expr> s (mymap == 1) ? '<nop>' : 's'
:nnoremap <expr> S (mymap == 1) ? '<nop>' : 'S'
:nnoremap <expr> v (mymap == 1) ? '<nop>' : 'v'
:nnoremap <expr> V (mymap == 1) ? '<nop>' : 'V'
:nnoremap <expr> r (mymap == 1) ? '<nop>' : 'r'
:nnoremap <expr> R (mymap == 1) ? '<nop>' : 'R'
:nnoremap <expr> q (mymap == 1) ? '<nop>' : 'q'

This last one doesn't work (DoxygenToolkit.vim doesn't generate the correct comment syntax since it doesn't see the correct mapping).

What is wrong in my second approach with conditional mapping ?

  • 3
    A couple of minor points to add to dedowsdi answer: if you are in a vimscript file you don't need the : in front of your commands. Also I think you may have a scope issue on your variable: you mix g:mymap and mymap, I'm not sure what's the default scope for a variable without prefix but anyway it is a good practice to explicitly specify a variable scope. Finally you may be interested in reading How to debug a mapping. – statox May 2 at 8:43
  • 1
    WOW, thanks for the suggestion and for the post ! – simo-zz May 2 at 9:02
1

AFAIK, <nop> should be used without <expr>. If you want to use <expr>, just return an empty string:

:nnoremap <expr> I (mymap == 1) ? '' : 'I'

By the way, you may consider to do it like this:

let forbiddens = 'IaoO.....'
for oper in split(forbiddens, '\zs')
  exec 'nnoremap ' . oper . ' <nop>'
endfor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.