Improvements to filetype detection, plugin loading

- Create .vim folder too if missing
- Use filetype to categorize combo instead of file extension (this will
  reset some combo counters, such as python: py.cmb -> python.cmb)
- Reload combo stats upon filetype change
- Check if combo tracking is already loaded on script start (to prevent
  multiple instances when sourcing vimrc again)
- More verbose g:combo (access numbers directly at g:combo_counter and
  g:best_combo)

To fix any lost combo score, rename the file accordingly in
~/.vim/.combo
This commit is contained in:
əlemi 2021-10-18 00:45:46 +02:00
parent a7fc88de04
commit 9f12726025
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -1,13 +1,28 @@
" COMBO Counter " COMBO Counter
" Choose combo file depending on extension
" Check if already loaded
if exists('g:combo_tracker_already_loaded')
finish
endif
let g:combo_tracker_already_loaded = 1
" Check for a .combo folder, make one if missing " Check for a .combo folder, make one if missing
if !isdirectory($HOME . '/.vim')
silent !mkdir $HOME/.vim
endif
if !isdirectory($HOME . '/.vim/.combo') if !isdirectory($HOME . '/.vim/.combo')
silent !mkdir $HOME/.vim/.combo silent !mkdir $HOME/.vim/.combo
endif endif
function ComboFmt()
return printf("combo|%d [best|%d]", g:combo, g:best_combo)
endfunction
" Get extension, choose combo file " Get extension, choose combo file
if strlen(expand("%:e")) > 0 function ReloadComboFile()
let g:combo_file = $HOME . '/.vim/.combo/' . expand("%:e") . ".cmb" let g:combo_file_type = &filetype
if strlen(g:combo_file_type) > 0
let g:combo_file = $HOME . '/.vim/.combo/' . g:combo_file_type . ".cmb"
else else
let g:combo_file = $HOME . '/.vim/.combo/none.cmb' let g:combo_file = $HOME . '/.vim/.combo/none.cmb'
endif endif
@ -21,23 +36,32 @@ else
let g:best_combo = 0 let g:best_combo = 0
endif endif
let g:best_last_combo = g:best_combo " Used to revert let g:best_last_combo = g:best_combo " Used to revert
" Configure variables
let g:combo_counter = 0 " The actual combo variable let g:combo_counter = 0 " The actual combo variable
let g:timeout = 1 let g:combo = ComboFmt()
let g:combo = printf("[%d] 0", g:best_combo) endfunction
autocmd FileType * call ReloadComboFile()
" Declare variables
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
let g:combo_timeout = 1
let g:combo = ComboFmt()
let g:last_combo = reltime() " Set current time as last combo time let g:last_combo = reltime() " Set current time as last combo time
" Main check function, executed every time the file is changed " Main check function, executed every time the file is changed
function! UpdateCombo() function! UpdateCombo()
if reltimefloat(reltime(g:last_combo)) > g:timeout if reltimefloat(reltime(g:last_combo)) > g:combo_timeout
call SaveCombo() call SaveCombo()
let g:combo_counter = 1 let g:combo_counter = 1
else else
let g:combo_counter +=1 let g:combo_counter +=1
endif endif
let g:combo = printf("[%d] %d", g:best_combo, g:combo_counter) let g:combo = ComboFmt()
let g:last_combo = reltime() let g:last_combo = reltime()
endfunction endfunction
" Checks if a new combo has been achieved and saves it to file " 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 function! SaveCombo() " Should check inside because it can be called on InsertLeave
if g:combo_counter > g:best_combo if g:combo_counter > g:best_combo
@ -49,15 +73,18 @@ endfunction
autocmd TextChangedI * call UpdateCombo() " Every time text is changed, call combo function autocmd TextChangedI * call UpdateCombo() " Every time text is changed, call combo function
autocmd InsertLeave * call SaveCombo() autocmd InsertLeave * call SaveCombo()
" In case you want to revert your last combo " In case you want to revert your last combo
function! Cheated() function! Cheated()
let g:best_combo = g:best_last_combo let g:best_combo = g:best_last_combo
let g:combo_counter = g:best_last_combo let g:combo_counter = g:best_last_combo
call writefile([g:combo_counter], g:combo_file) call writefile([g:combo_counter], g:combo_file)
endfunction endfunction
" This is only needed to avoid backspace cheating. " This is only needed to avoid backspace cheating.
function! Decrease() function! Decrease()
let g:combo_counter -= 1 let g:combo_counter -= 1
return "\<BS>" return "\<BS>"
endfunction endfunction
inoremap <expr> <BS> Decrease() inoremap <expr> <BS> Decrease()