0

I'm trying to make g++ run as a function :call in vim> For some reason that I don't understand vim throws me an E81 error when I try to execute the s:compile_run_cpp() function.

function! s:compile_run_cpp() abort
  let src_path = expand('%:p:~')
  let src_noext = expand('%:p:~:r')
  " The building flags
  let _flag = '-Wall -Wextra -std=c++11 -O2'

  if executable('clang++')
    let prog = 'clang++'
  elseif executable('g++')
    let prog = 'g++'
  else
    echoerr 'No compiler found!'
  endif
  call s:create_term_buf('v', 80)
  execute printf('term %s %s %s -o %s && %s', prog, _flag, src_path, src_noext, src_noext)
  startinsert
endfunction

function s:create_term_buf(_type, size) abort
  set splitbelow
  set splitright
  if a:_type ==# 'v'
    vnew
  else
    new
  endif
  execute 'resize ' . a:size
endfunction
romainl
  • 186,200
  • 21
  • 280
  • 313
boomcreeper11
  • 177
  • 2
  • 14

1 Answers1

2

If you are using autoload instead of .vimrc then change your function name to filename#function_name for eg: if you file name is compile.vim and the function name is compile_run_cpp then the function name would be compile#compile_run_cpp then you can easily call it with :call compile#compile_run_cpp()

function! compile#compile_run_cpp() abort
  let src_path = expand('%:p:~')
  let src_noext = expand('%:p:~:r')
  " The building flags
  let _flag = '-Wall -Wextra -std=c++11 -O2'

  if executable('clang++')
    let prog = 'clang++'
  elseif executable('g++')
    let prog = 'g++'
  else
    echoerr 'No compiler found!'
  endif
  call s:create_term_buf('v', 80)
  execute printf('term %s %s %s -o %s && %s', prog, _flag, src_path, src_noext, src_noext)
  startinsert
endfunction

function s:create_term_buf(_type, size) abort
  set splitbelow
  set splitright
  if a:_type ==# 'v'
    vnew
  else
    new
  endif
  execute 'resize ' . a:size
endfunction

In case you are using .vimrc

Try changing this:

call s:create_term_buf('v', 80)

to this:

call <SID>create_term_buf('v', 80)

Note:

  • Conversion for function naming used by other programmers. for eg: create_term_buf to Create_term_buf
  • Don't scope a function if you want to call it from other script like compile_run_cpp.
Chandan
  • 11,465
  • 1
  • 6
  • 25