2019-10-22 14:58:32 +02:00
|
|
|
" COMBO Counter
|
2019-11-02 02:33:20 +01:00
|
|
|
" Choose combo file depending on extension
|
2021-02-21 21:05:21 +01:00
|
|
|
|
|
|
|
" Check for a .combo folder, make one if missing
|
|
|
|
if !isdirectory($HOME . '/.vim/.combo')
|
|
|
|
silent !mkdir $HOME/.vim/.combo
|
|
|
|
endif
|
|
|
|
" Get extension, choose combo file
|
|
|
|
if strlen(expand("%:e")) > 0
|
|
|
|
let g:combo_file = $HOME . '/.vim/.combo/' . expand("%:e") . ".cmb"
|
2019-11-02 02:33:20 +01:00
|
|
|
else
|
2021-02-21 21:05:21 +01:00
|
|
|
let g:combo_file = $HOME . '/.vim/.combo/none.cmb'
|
2019-10-26 16:33:36 +02:00
|
|
|
endif
|
2019-10-23 00:02:56 +02:00
|
|
|
|
2019-11-02 05:22:03 +01:00
|
|
|
" If file should be ignored, just set text, else continue with script
|
|
|
|
if g:disable_combo
|
2021-01-30 22:48:08 +01:00
|
|
|
let g:combo = "N/A"
|
2019-11-02 05:22:03 +01:00
|
|
|
else
|
2019-11-07 06:00:19 +01:00
|
|
|
" Find best score for current filetype. If none exists, start tracking
|
2019-11-02 05:22:03 +01:00
|
|
|
if filereadable(g:combo_file)
|
2019-11-06 22:27:35 +01:00
|
|
|
let g:best_combo = readfile(g:combo_file)
|
2019-11-02 05:22:03 +01:00
|
|
|
let g:best_combo = g:best_combo[0]
|
2019-11-02 02:33:20 +01:00
|
|
|
else
|
2021-01-31 16:43:56 +01:00
|
|
|
silent exec '!echo 0 > ' . g:combo_file
|
2019-11-02 05:22:03 +01:00
|
|
|
let g:best_combo = 0
|
2019-11-02 02:33:20 +01:00
|
|
|
endif
|
2019-11-06 22:27:35 +01:00
|
|
|
let g:best_last_combo = g:best_combo " Used to revert
|
|
|
|
|
2019-11-02 05:22:03 +01:00
|
|
|
" Configure variables
|
|
|
|
let g:combo_counter = 0 " The actual combo variable
|
|
|
|
let g:timeout = 1
|
2021-01-30 22:48:08 +01:00
|
|
|
let g:combo = printf("[%d] 0", g:best_combo)
|
2019-11-07 06:00:19 +01:00
|
|
|
let g:last_combo = reltime() " Set current time as last combo time
|
|
|
|
" Main check function, executed every time the file is changed
|
2019-11-02 05:22:03 +01:00
|
|
|
function! UpdateCombo()
|
|
|
|
if reltimefloat(reltime(g:last_combo)) > g:timeout
|
|
|
|
call SaveCombo()
|
|
|
|
let g:combo_counter = 1
|
|
|
|
else
|
2019-11-06 22:27:35 +01:00
|
|
|
let g:combo_counter +=1
|
2019-11-02 05:22:03 +01:00
|
|
|
endif
|
2021-01-30 22:48:08 +01:00
|
|
|
let g:combo = printf("[%d] %d", g:best_combo, g:combo_counter)
|
2019-11-02 05:22:03 +01:00
|
|
|
let g:last_combo = reltime()
|
|
|
|
endfunction
|
2019-11-07 06:00:19 +01:00
|
|
|
" Checks if a new combo has been achieved and saves it to file
|
2019-11-06 22:27:35 +01:00
|
|
|
function! SaveCombo() " Should check inside because it can be called on InsertLeave
|
2019-11-02 05:22:03 +01:00
|
|
|
if g:combo_counter > g:best_combo
|
|
|
|
call writefile([g:combo_counter], g:combo_file)
|
2019-11-06 22:27:35 +01:00
|
|
|
let g:best_last_combo = g:best_combo
|
2019-11-02 05:22:03 +01:00
|
|
|
let g:best_combo = g:combo_counter
|
|
|
|
endif
|
|
|
|
endfunction
|
2019-11-06 22:27:35 +01:00
|
|
|
|
|
|
|
autocmd TextChangedI * call UpdateCombo() " Every time text is changed, call combo function
|
2019-11-02 05:22:03 +01:00
|
|
|
autocmd InsertLeave * call SaveCombo()
|
2019-11-07 06:00:19 +01:00
|
|
|
" In case you want to revert your last combo
|
|
|
|
function! Cheated()
|
|
|
|
let g:best_combo = g:best_last_combo
|
|
|
|
let g:combo_counter = g:best_last_combo
|
|
|
|
call writefile([g:combo_counter], g:combo_file)
|
|
|
|
endfunction
|
|
|
|
" This is only needed to avoid backspace cheating.
|
|
|
|
function! Decrease()
|
|
|
|
let g:combo_counter -= 1
|
|
|
|
return "\<BS>"
|
|
|
|
endfunction
|
|
|
|
inoremap <expr> <BS> Decrease()
|
2019-11-02 05:22:03 +01:00
|
|
|
endif
|