diff --git a/.combo/none.cmb b/.combo/none.cmb new file mode 100644 index 0000000..573541a --- /dev/null +++ b/.combo/none.cmb @@ -0,0 +1 @@ +0 diff --git a/doc/vim-combo.txt b/doc/vim-combo.txt index 4e74baa..80b98ae 100644 --- a/doc/vim-combo.txt +++ b/doc/vim-combo.txt @@ -13,7 +13,9 @@ Each keystroke in insert mode increases your combo, but not typing for 1 second resets it. The script is triggered whenever a text edit is detected in insert mode. -Best score is displayed next to the current combo in square brackets. +Progress is tracked per filetype (and stored in ~/.vim/.combo/). +Best score is displayed next to the current combo, and in square brackets +the best score across all filetypes is shown. When a new high score is achieved, it is saved inside .vim as .combo. If you get errors at launch, check that .combo exists and has just a number inside (should be 0, or last best combo). @@ -22,8 +24,6 @@ By default, combo is not tracked in .txt, .tex, .vim files and in files with no extension. This can be removed by deleting (or commenting) the first lines in the script. -The plugin is not in its final version. Right now, you will need to edit -the path to the .combo file and choose where to display the combo information. By default, the section B of airline-vim is overwritten to display current combo. A random Runic character has been chosen to indicate the combo field. diff --git a/plugin/vim-combo.vim b/plugin/vim-combo.vim index 002620f..7c273ed 100644 --- a/plugin/vim-combo.vim +++ b/plugin/vim-combo.vim @@ -1,32 +1,48 @@ " COMBO Counter -let g:disable_combo = 0 -" Combo counting is disabled in plain text, LaTeX, vim config and files -" without extension (usually configuration files) -if expand("%:e") == "txt" || expand("%:e") == "tex" || expand("%:e") == "vim" || expand("%:e") == "" - let g:disable_combo = 1 +" Choose combo file depending on extension +if strlen(expand("%:e")) > 0 + let g:combo_file = $HOME . '/.vim/.combo/' . expand("%:e") . ".cmb" +else + let g:combo_file = $HOME . '/.vim/.combo/none.cmb' endif -if g:disable_combo == 1 - let g:best_combo = readfile('$HOME/.vim/.combo') +" Find best score across all filetypes +let scores = [] +for f in split(globpath($HOME . '/.vim/.combo/', '*'), '\n') + let buf = readfile(f) + call insert(scores, buf[0]) +endfor +let g:best_combo_all = max(scores) + +" Find best score for current filetype +if filereadable(g:combo_file) + let g:best_combo = readfile(g:combo_file) let g:best_combo = g:best_combo[0] - let g:airline_section_b = 'ᛥ [%{g:best_combo}]' else - let g:combo_counter = 0 " The actual combo variable - let g:best_combo = readfile('$HOME/.vim/.combo') " ~ is not expanded ??? - let g:best_combo = g:best_combo[0] " Reading from file returns a list, but an int is needed - let g:last_combo = reltime() " Set current time as last combo time - let g:airline_section_b = 'ᛥ %{g:combo_counter} [%{g:best_combo}]' " I use airline vim and inserted the combo meter in it - function! UpdateCombo() - if reltimefloat(reltime(g:last_combo)) > 1 " Timeout is 1 second - if g:combo_counter > g:best_combo " Before resetting combo counter, check if new high score - call writefile([g:combo_counter], "$HOME/.vim/.combo") - let g:best_combo = g:combo_counter - endif - let g:combo_counter = 1 - else - let g:combo_counter +=1 - endif - let g:last_combo = reltime() - endfunction - autocmd TextChangedI * call UpdateCombo() " Every time text in the file changes, call combo function + silent !echo 0 > $HOME/.vim/.combo/%:e.cmb + let g:best_combo = 0 endif + +" Configure variables +let g:combo_counter = 0 " The actual combo variable +let g:timeout = 1 +let g:last_combo = reltime() " Set current time as last combo time +let g:airline_section_b = 'ᛥ %{g:combo_counter}|%{g:best_combo} [%{g:best_combo_all}]' " I use airline vim and inserted the combo meter in it +function! UpdateCombo() + if reltimefloat(reltime(g:last_combo)) > g:timeout " Timeout is 1 second + call SaveCombo() + let g:combo_counter = 1 + else + let g:combo_counter +=1 + endif + let g:last_combo = reltime() +endfunction +function! SaveCombo() + if g:combo_counter > g:best_combo + call writefile([g:combo_counter], g:combo_file) + let g:best_combo = g:combo_counter + endif +endfunction + +autocmd TextChangedI * call UpdateCombo() " Every time the cursor moves, call combo function +autocmd InsertLeave * call SaveCombo()