From fced85576c511630c2b4069d02e34c40a1922f55 Mon Sep 17 00:00:00 2001 From: Kevin Cotugno Date: Thu, 13 Dec 2018 07:22:59 -0800 Subject: [PATCH] Refactor BufWrite/BufLeave autocmds --- .config/nvim/init.vim | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/.config/nvim/init.vim b/.config/nvim/init.vim index 9d0c0b3..ffebe5c 100644 --- a/.config/nvim/init.vim +++ b/.config/nvim/init.vim @@ -85,19 +85,36 @@ function TrimTrailingLines() call winrestview(view) endfunction +function OnWrite() + if Writeable() + call CleanFile() + endif +endfunction + +function OnLeave() + if Writeable() + call CleanFile() + write + GitGutter + endif +endfunction + +function CleanFile() + call TrimTrailingInvisibles() | call TrimTrailingLines() +endfunction + +function Writeable() + return &buftype == '' + \ && !&readonly + \ && &modifiable + \ && &modified + \ && expand("%:t") != "" +endfunction + augroup maximus -autocmd BufWrite * call TrimTrailingInvisibles() | call TrimTrailingLines() -autocmd BufLeave * if &buftype == '' - \ && !&readonly - \ && &modifiable - \ && &modified - \ && expand("%:t") != "" - \ | call TrimTrailingInvisibles() - \ | call TrimTrailingLines() - \ | w - \ | GitGutter - \ | endif +autocmd BufWrite * call OnWrite() +autocmd BufLeave * call OnLeave() augroup END