diff --git a/lua/combo.lua b/lua/combo.lua new file mode 100644 index 0000000..fa0648b --- /dev/null +++ b/lua/combo.lua @@ -0,0 +1,127 @@ +--[[ + +COMBO Counter + +]]-- + +-- Check if already loaded +if vim.g.combo_tracker_already_loaded then + return +end + +vim.g.combo_tracker_already_loaded = true +vim.g.combo_timeout = 1.0 -- seconds + +-- Check for a 'combo' folder, make one if missing +local separator = '/' +if vim.fn.has('win32') ~= 0 then + separator = '\\' +end +local combo_path = vim.fn.stdpath("data") .. separator .. "combo" +local ok, err, code = os.rename(combo_path, combo_path) +if not ok then + if code == 13 then + print("[!] could not find or create combo folder") + print(err) + return + else + vim.fn.mkdir(combo_path, "p") + end +end + +-- Set globals +vim.g.combo_enabled = true +vim.g.combo_counter = 0 +vim.g.best_combo = 0 +vim.g.best_last_combo = 0 +vim.g.combo = "" + +vim.g.last_combo = vim.fn.reltimefloat(vim.fn.reltime()) + +vim.g.combo_file = combo_path .. separator .. 'none' + +-- Define necessary functions + +local function combo_fmt() + if not vim.g.combo_enabled then return "" end + return string.format("%d|%d", vim.g.best_combo, vim.g.combo_counter) +end + +local function save_combo() + if not vim.g.combo_enabled then return end + if vim.g.combo_counter > vim.g.best_combo then + vim.fn.writefile({vim.g.combo_counter}, vim.g.combo_file) + vim.g.best_last_combo = vim.g.best_combo + vim.g.best_combo = vim.g.combo_counter + end +end + +local function reload_combo_file() + if not vim.g.combo_enabled then return end + vim.g.combo_file_type = vim.opt.filetype._value + if vim.g.combo_file_type then + vim.g.combo_file = combo_path .. separator .. vim.g.combo_file_type + end + vim.g.best_combo = 0 + if vim.fn.filereadable(vim.g.combo_file) ~= 0 then + local contents = vim.fn.readfile(vim.g.combo_file) + if contents ~= nil then + vim.g.best_combo = tonumber(contents[1]) + end + end + vim.g.best_last_combo = vim.g.best_combo + vim.g.combo = combo_fmt() +end + +local function update_combo() + if not vim.g.combo_enabled then return end + if vim.fn.reltimefloat(vim.fn.reltime(vim.g.last_combo)) > vim.g.combo_timeout then + save_combo() + vim.g.combo_counter = 1 + else + vim.g.combo_counter = vim.g.combo_counter + 1 + end + vim.g.combo = combo_fmt() + vim.g.last_combo = vim.fn.reltime() +end + +-- Define user-facing functions + +function ComboToggle() + vim.g.combo_enabled = not vim.g.combo_enabled + vim.g.combo = combo_fmt() +end + +function ComboEnable() + vim.g.combo_enabled = true + vim.g.combo = combo_fmt() +end + +function ComboDisable() + vim.g.combo_enabled = false + vim.g.combo = combo_fmt() +end + +function ComboCheated() + vim.g.best_combo = vim.g.best_last_combo + vim.g.combo_counter = vim.g.best_last_combo + vim.fn.writefile({vim.g.combo_counter}, vim.g.combo_file) +end + +local vim_combo_group = vim.api.nvim_create_augroup("VimComboGroup", {clear=true}) +vim.api.nvim_create_autocmd({ "FileType" }, { pattern={"*"}, callback=reload_combo_file, group=vim_combo_group }) +vim.api.nvim_create_autocmd({ "TextChangedI", "TextChangedP" }, { callback=update_combo, group=vim_combo_group }) +vim.api.nvim_create_autocmd({ "InsertLeave"}, { callback=save_combo, group=vim_combo_group }) + +local function decrease_bs() + vim.g.combo_counter = vim.g.combo_counter - 1 + return "" +end +vim.keymap.set('i', '', decrease_bs, {expr=true}) + +local function decrease_del() + vim.g.combo_counter = vim.g.combo_counter - 1 + return "" +end +vim.keymap.set('i', '', decrease_del, {expr=true}) + diff --git a/plugin/vim-combo.vim b/plugin/vim-combo.vim index 6685921..cd18772 100644 --- a/plugin/vim-combo.vim +++ b/plugin/vim-combo.vim @@ -1,5 +1,16 @@ " COMBO Counter +if has('nvim') + lua require('combo') + + command Cheated lua ComboCheated() + command ComboEnable lua ComboEnable() + command ComboDisable lua ComboDisable() + command ComboToggle lua ComboToggle() + + finish +endif + " Check if already loaded if exists('g:combo_tracker_already_loaded') finish @@ -17,14 +28,14 @@ endif function ComboFmt() return printf("combo|%d [best|%d]", g:combo_counter, g:best_combo) endfunction -" + " 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_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 let g:combo_file = $HOME . '/.vim/.combo/none.cmb' " Get extension, choose combo file