From a6bbfa1a9b3b5c082ab313d7ff42d82394a43242 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 22 Jun 2019 13:16:30 +1200 Subject: Move fortune.vim function into autoload --- vim/autoload/fortune.vim | 43 +++++++++++++++++++++++++++++++++++++++++++ vim/plugin/fortune.vim | 45 +-------------------------------------------- 2 files changed, 44 insertions(+), 44 deletions(-) create mode 100644 vim/autoload/fortune.vim (limited to 'vim') diff --git a/vim/autoload/fortune.vim b/vim/autoload/fortune.vim new file mode 100644 index 00000000..4fb1d0cd --- /dev/null +++ b/vim/autoload/fortune.vim @@ -0,0 +1,43 @@ +let s:paths = [ + \ $HOME.'/.fortunes', + \ $HOME.'/.local/share/games/fortunes', + \] +highlight Fortune + \ term=NONE + \ cterm=NONE ctermfg=248 ctermbg=NONE + +function! fortune#() abort + if !has('unix') + echoerr 'Only works on *nix' + endif + if !executable('fortune') + echoerr 'Missing "fortune" executable' + endif + if !executable('timeout') + echoerr 'Missing "timeout" executable' + endif + let limit = &columns - 1 + let command = [ + \ 'timeout', + \ '0.3s', + \ 'fortune', + \ '-s', + \ '-n', + \ limit, + \] + for path in s:paths + if isdirectory(path) + call add(command, path) + break + endif + endfor + let fortune = substitute( + \ system(join(command)), + \ '[[:cntrl:]]\+', + \ ' ', + \ 'g', + \) + echohl Fortune + echo fortune + echohl None +endfunction diff --git a/vim/plugin/fortune.vim b/vim/plugin/fortune.vim index e52f50af..825ed9f7 100644 --- a/vim/plugin/fortune.vim +++ b/vim/plugin/fortune.vim @@ -1,48 +1,5 @@ -let s:paths = [ - \ $HOME.'/.fortunes', - \ $HOME.'/.local/share/games/fortunes', - \] -highlight Fortune - \ term=NONE - \ cterm=NONE ctermfg=248 ctermbg=NONE - -function! s:Fortune() abort - if !has('unix') - echoerr 'Only works on *nix' - endif - if !executable('fortune') - echoerr 'Missing "fortune" executable' - endif - if !executable('timeout') - echoerr 'Missing "timeout" executable' - endif - let limit = &columns - 1 - let command = [ - \ 'timeout', - \ '0.3s', - \ 'fortune', - \ '-s', - \ '-n', - \ limit, - \] - for path in s:paths - if isdirectory(path) - call add(command, path) - break - endif - endfor - let fortune = substitute( - \ system(join(command)), - \ '[[:cntrl:]]\+', - \ ' ', - \ 'g', - \) - echohl Fortune - echo fortune - echohl None -endfunction command! -bar Fortune - \ call s:Fortune() + \ call fortune#() augroup fortune autocmd! autocmd VimEnter * -- cgit v1.2.3