2019-10-22 14:58:32 +02:00
|
|
|
" COMBO Counter
|
2021-10-18 00:45:46 +02:00
|
|
|
|
2022-07-13 23:53:18 +02:00
|
|
|
if has('nvim')
|
|
|
|
lua require('combo')
|
|
|
|
|
|
|
|
command Cheated lua ComboCheated()
|
|
|
|
command ComboEnable lua ComboEnable()
|
|
|
|
command ComboDisable lua ComboDisable()
|
|
|
|
command ComboToggle lua ComboToggle()
|
|
|
|
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
2021-10-18 00:45:46 +02:00
|
|
|
" Check if already loaded
|
|
|
|
if exists('g:combo_tracker_already_loaded')
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:combo_tracker_already_loaded = 1
|
2021-02-21 21:05:21 +01:00
|
|
|
|
|
|
|
" Check for a .combo folder, make one if missing
|
2021-10-18 00:45:46 +02:00
|
|
|
if !isdirectory($HOME . '/.vim')
|
2021-10-18 01:31:08 +02:00
|
|
|
silent !mkdir $HOME/.vim
|
2021-10-18 00:45:46 +02:00
|
|
|
endif
|
2021-02-21 21:05:21 +01:00
|
|
|
if !isdirectory($HOME . '/.vim/.combo')
|
2021-10-18 01:31:08 +02:00
|
|
|
silent !mkdir $HOME/.vim/.combo
|
2021-02-21 21:05:21 +01:00
|
|
|
endif
|
2021-10-18 00:45:46 +02:00
|
|
|
|
|
|
|
function ComboFmt()
|
2021-10-18 01:31:08 +02:00
|
|
|
return printf("combo|%d [best|%d]", g:combo_counter, g:best_combo)
|
2021-10-18 00:45:46 +02:00
|
|
|
endfunction
|
2022-07-13 23:53:18 +02:00
|
|
|
|
2021-10-18 01:31:08 +02:00
|
|
|
" Declare variables
|
2022-07-13 23:53:18 +02:00
|
|
|
let g:combo_counter = 0 " The actual combo variable
|
|
|
|
let g:best_combo = 0 " Where best score for filetype is tracked
|
|
|
|
let g:best_last_combo = 0 " Used to revert, when you cheat by accident
|
2021-10-18 01:31:08 +02:00
|
|
|
let g:combo_timeout = 1
|
|
|
|
let g:combo = ComboFmt()
|
2022-07-13 23:53:18 +02:00
|
|
|
let g:last_combo = reltime() " Set current time as last combo time
|
2021-10-19 14:30:08 +02:00
|
|
|
let g:combo_file = $HOME . '/.vim/.combo/none.cmb'
|
2021-10-18 00:45:46 +02:00
|
|
|
|
2021-02-21 21:05:21 +01:00
|
|
|
" Get extension, choose combo file
|
2021-10-18 00:45:46 +02:00
|
|
|
function ReloadComboFile()
|
|
|
|
let g:combo_file_type = &filetype
|
|
|
|
if strlen(g:combo_file_type) > 0
|
2021-10-18 01:31:08 +02:00
|
|
|
let g:combo_file = $HOME . '/.vim/.combo/' . g:combo_file_type . ".cmb"
|
2021-10-18 00:45:46 +02:00
|
|
|
else
|
2021-10-18 01:31:08 +02:00
|
|
|
let g:combo_file = $HOME . '/.vim/.combo/none.cmb'
|
2021-10-18 00:45:46 +02:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Find best score for current filetype. If none exists, start tracking
|
|
|
|
if filereadable(g:combo_file)
|
|
|
|
let g:best_combo = readfile(g:combo_file)
|
|
|
|
let g:best_combo = g:best_combo[0]
|
|
|
|
else
|
|
|
|
silent exec '!echo 0 > ' . g:combo_file
|
|
|
|
let g:best_combo = 0
|
|
|
|
endif
|
|
|
|
let g:best_last_combo = g:best_combo " Used to revert
|
|
|
|
let g:combo_counter = 0 " The actual combo variable
|
|
|
|
let g:combo = ComboFmt()
|
|
|
|
endfunction
|
2019-10-23 00:02:56 +02:00
|
|
|
|
2021-10-18 00:45:46 +02:00
|
|
|
autocmd FileType * call ReloadComboFile()
|
2021-02-21 21:08:21 +01:00
|
|
|
|
|
|
|
" Main check function, executed every time the file is changed
|
|
|
|
function! UpdateCombo()
|
2021-10-18 00:45:46 +02:00
|
|
|
if reltimefloat(reltime(g:last_combo)) > g:combo_timeout
|
2021-02-21 21:08:21 +01:00
|
|
|
call SaveCombo()
|
|
|
|
let g:combo_counter = 1
|
2019-11-02 02:33:20 +01:00
|
|
|
else
|
2021-02-21 21:08:21 +01:00
|
|
|
let g:combo_counter +=1
|
2019-11-02 02:33:20 +01:00
|
|
|
endif
|
2021-10-18 00:45:46 +02:00
|
|
|
let g:combo = ComboFmt()
|
2021-02-21 21:08:21 +01:00
|
|
|
let g:last_combo = reltime()
|
|
|
|
endfunction
|
2021-10-18 01:31:08 +02:00
|
|
|
autocmd TextChangedI * call UpdateCombo() " Every time text is changed, call combo function
|
2021-10-23 15:28:33 +02:00
|
|
|
autocmd TextChangedP * call UpdateCombo() " Every time text is changed, call combo function
|
2021-10-18 00:45:46 +02:00
|
|
|
|
2021-02-21 21:08:21 +01:00
|
|
|
" Checks if a new combo has been achieved and saves it to file
|
|
|
|
function! SaveCombo() " Should check inside because it can be called on InsertLeave
|
|
|
|
if g:combo_counter > g:best_combo
|
2019-11-07 06:00:19 +01:00
|
|
|
call writefile([g:combo_counter], g:combo_file)
|
2021-02-21 21:08:21 +01:00
|
|
|
let g:best_last_combo = g:best_combo
|
|
|
|
let g:best_combo = g:combo_counter
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
autocmd InsertLeave * call SaveCombo()
|
2021-10-18 00:45:46 +02:00
|
|
|
|
2021-02-21 21:08:21 +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
|
2021-10-18 00:45:46 +02:00
|
|
|
|
2021-02-21 21:08:21 +01:00
|
|
|
" This is only needed to avoid backspace cheating.
|
|
|
|
function! Decrease()
|
|
|
|
let g:combo_counter -= 1
|
|
|
|
return "\<BS>"
|
|
|
|
endfunction
|
2021-10-18 00:45:46 +02:00
|
|
|
|
2021-02-21 21:08:21 +01:00
|
|
|
inoremap <expr> <BS> Decrease()
|