VIM script NERDTree-<CR> remapping on condition

68 views Asked by At

I am looking for a way to map NERDTree-<CR> on some condition.
If current window is empty, NERDTree-<CR> is mapped to NERDTree-o. If it is not, mapped to NERDTree-t.

I am a beginner of vimscript, so it is hard to find how to do it.
I tried to make a function like below and the function to be called on 'NERDTreeCustomArgsOpen', but couldn't make it.

" Couldn't make 'IsBufferModifiable' function
" and mapping 'MyNERDTree_CR' to 'NERDTreeCustomOpenArgs'

function! IsBufferModifiable(numBuffer)

function! IsBufferEmpty(numBuffer)
  return bufname(a:numBuffer) == ''
endfunction

function! IsModifiableBufferEmpty()
  let emptyModifiableBuffers = filter(range(1, bufnr('$')), 'IsBufferModifiable(v:val) && IsBufferEmpty(v:val)')

  if len(emptyModifiableBuffers) == 0
    return 1
  else
    return 0
endfunction

function! MyNERDTree_CR()
  if IsModifiableBufferEmpty()
    call feedkeys('o')
  else
    call feedkeys('t')
endfunction

" Not sure it works or not
let NERDTreeCustomOpenArgs = {'file':{'where':'MyNERDTree_CR', 'reuse':'all', 'keepopen':1, 'stay':1}}

Is there any way to make them work?

1

There are 1 answers

0
romainl On

Checking if a given buffer is modifiable is usually done by checking the value of the buffer-local :help 'modifiable' option:

function! IsBufferModifiable(numBuffer)
    return getbufvar(a:numBuffer, '&modifiable')
endfunction

See :help getbufvar().