metakirby5 / codi.vim

:notebook_with_decorative_cover: The interactive scratchpad for hackers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't assign custom auto commands without the insert versions

dylan-chong opened this issue · comments

I want to assign custom auto commands: TextChanged,InsertLeave without the insert versions of them.

Why?

  • Insert leave is not good enough by itself, because when you edit code without going into insert mode, codi does not get updated
  • the plug-in lessspace is stripping the whitespace as I type it seemingly because Codi is running (does not happen when autocmd is set to InsertLeave)
  • one may find it distracting - for example seeing lots of syntax errors because you haven't finished typing yet
  • reduced battery usage???

I am the author of LessSpace from the linked issue. My plugin strips whitespace when leaving insert mode, or after normal mode edits. I am having trouble integrating with Codi's behavior of quickly leaving insert mode during Codi updates. The CodiUpdatePre and CodiUpdatePost hooks normally would allow me to disable the plugin for the duration of the Codi update, but based on a cursory reading of the code, it appears the Post hook may not be called? Correct me if I'm wrong.

codi.vim/autoload/codi.vim

Lines 352 to 355 in 26fdafe

" Only trigger post if sync
if !s:get_opt('async')
call s:user_au('CodiUpdatePost')
endif

Alternatively, Codi could be set to run on CursorHold events, but it needs to be possible to exclude CursorHoldI events for the reasons @dylan-chong gave in the original issue.

This is what I use to assign custom auto commands for codi without the insert version.

" vimrc/init.vim

let g:codi#autocmd = 'None'
augroup codi_update
  autocmd!
  autocmd TextChanged,InsertLeave <buffer> call codi#update()
augroup END

@taylorskalyo

The problem with your solution is that codi#update will get called on every file, even when codi is not even active on that file. I suppose when codi#update is called while codi is inactive, it just exits, but this seems clunky to me.

The below works only when codi is active, using the CodiEnterPre and CodiLeavePost autocommands (see :help codi). Just add these lines to your .vimrc.

let g:codi#autocmd = 'None'
augroup codi
  au!
  au User CodiEnterPre call s:codi_enter()
  au User CodiLeavePost call s:codi_leave()
augroup END
function! s:codi_enter()
  exe 'augroup codi_' . bufnr('%')
    au!
    au InsertLeave,TextChanged <buffer> call codi#update()
  augroup END
endfunction
function! s:codi_leave()
  exe 'augroup codi_' . bufnr('%')
    au!
  augroup END
endfunction