diff --git a/lua/libmodal/src/base/globals.lua b/lua/libmodal/src/base/globals.lua index 8fef1e6..ecc65e5 100644 --- a/lua/libmodal/src/base/globals.lua +++ b/lua/libmodal/src/base/globals.lua @@ -3,8 +3,15 @@ * MODULE */ --]] + local globals = {} +--[[ + /* + * TABLE `globals` + */ +--]] + globals.ESC_NR = 27 globals.TYPE_FUNC = 'function' globals.TYPE_NUM = 'number' @@ -19,5 +26,5 @@ globals.VIM_TRUE = 1 * PUBLICIZE MODULE */ --]] -return globals +return globals diff --git a/lua/libmodal/src/base/init.lua b/lua/libmodal/src/base/init.lua index 2620fba..65d4922 100644 --- a/lua/libmodal/src/base/init.lua +++ b/lua/libmodal/src/base/init.lua @@ -3,15 +3,15 @@ * MODULE */ --]] + local base = {} base.globals = require('libmodal/src/base/globals') --- TODO - --[[ /* * PUBLICIZE MODULE */ --]] + return base diff --git a/lua/libmodal/src/init.lua b/lua/libmodal/src/init.lua index 7cef3c0..56ee198 100644 --- a/lua/libmodal/src/init.lua +++ b/lua/libmodal/src/init.lua @@ -3,13 +3,12 @@ * MODULE */ --]] + local libmodal = require('libmodal/src/base') libmodal.mode = require('libmodal/src/mode') libmodal.prompt = require('libmodal/src/prompt') libmodal.utils = require('libmodal/src/utils') --- TODO - --[[ /* * PUBLICIZE MODULE diff --git a/lua/libmodal/src/mode/ParseTable/init.lua b/lua/libmodal/src/mode/ParseTable/init.lua index 341fcee..0689459 100644 --- a/lua/libmodal/src/mode/ParseTable/init.lua +++ b/lua/libmodal/src/mode/ParseTable/init.lua @@ -18,18 +18,19 @@ local strings = {} -- not to be returned. Used for split() function. --[[ /* - * CONSTANTS + * CLASS `ParseTable` */ --]] --- The number corresponding to in vim. -ParseTable.CR = 13 - +------------------------------------ +--[[ SUMMARY: + * Split some `str` using a regex `pattern`. +]] --[[ - /* - * f(x) - */ ---]] + * `str` => the string to split. + * `pattern` => the regex pattern to split `str` with. +]] +------------------------------------ function strings.split(str, pattern) local split = {} for char in string.gmatch(str, pattern) do @@ -38,18 +39,77 @@ function strings.split(str, pattern) return split end -------------------------- +--[[ + /* + * CLASS `ParseTable` + */ +--]] + +-- The number corresponding to in vim. +ParseTable.CR = 13 + +---------------------------------- --[[ SUMMARY: * Create a new parse table from a user-defined table. ]] --[[ PARAMS: * `userTable` => the table of combos defined by the user. ]] -------------------------- -function ParseTable:new(userTable) +---------------------------------- +function ParseTable.new(userTable) local parseTable = {} - ------------------------------- + ---------------------------- + --[[ SUMMARY: + * Get a value from this `ParseTable`. + ]] + --[[ PARAMS: + * `key` => the PARSED key to get. + ]] + --[[ + * `function` => when `key` is a full match. + * `table` => when the `key` partially mathes. + * `false` => when `key` is not ANYWHERE. + ]] + ---------------------------- + function parseTable:get(key) + local function parseGet(dict, splitKey) + --[[ Get the next character in the combo string. ]] + + local k = '' + if #splitKey > 0 then -- There is more input to parse + k = api.nvim_eval("char2nr('" .. table.remove(splitKey) .. "')") + else -- The user input has run out, but there is more in the dictionary. + return dict + end + + --[[ Parse the `k`. ]] + + -- Make sure the dicitonary has a key for that value. + if dict[k] then + val = dict[k] + local valType = type(val) + + if valType == globals.TYPE_TBL then + if val[ParseTable.CR] and #splitKey < 1 then + return val + else + return parseGet(val, splitKey) + end + elseif valType == globals.TYPE_STR and #splitKey < 1 then + return val + end + end + return false + end + + -- run the inner recursive function in order to return the desired result + return parseGet(self, strings.split( + string.reverse(key), '.' + )) + end + + ---------------------------------------- --[[ SUMMARY: * Put `value` into the parse tree as `key`. ]] @@ -57,7 +117,7 @@ function ParseTable:new(userTable) * `key` => the key that `value` is reffered to by. * `value` => the value to store as `key`. ]] - ------------------------------- + ---------------------------------------- function parseTable:parsePut(key, value) -- Internal recursion function. local function update(dict, splitKey) -- † @@ -91,14 +151,14 @@ function ParseTable:new(userTable) )) end - ------------------------------- + --------------------------------------------- --[[ SUMMARY: * Create the union of `self` and `tableToUnite` ]] --[[ PARAMS: * `tableToUnite` => the table to unite with `self.` ]] - ------------------------------- + --------------------------------------------- function parseTable:parsePutAll(tableToUnite) for k, v in pairs(tableToUnite) do self:parsePut(k, v) @@ -116,4 +176,5 @@ end * PUBLICIZE MODULE */ --]] + return ParseTable diff --git a/lua/libmodal/src/mode/init.lua b/lua/libmodal/src/mode/init.lua index b9aa6e4..dc973b4 100644 --- a/lua/libmodal/src/mode/init.lua +++ b/lua/libmodal/src/mode/init.lua @@ -4,8 +4,8 @@ */ --]] -local globals = require('libmodal/src/base/globals') -local utils = require('libmodal/src/utils') +local globals = require('libmodal/src/base/globals') +local utils = require('libmodal/src/utils') local api = utils.api local vars = utils.vars @@ -19,6 +19,12 @@ local vars = utils.vars local mode = {} mode.ParseTable = require('libmodal/src/mode/ParseTable') +--[[ + /* + * LIBRARY `mode` + */ +--]] + ------------------------------------ --[[ SUMMARY: * Parse the `comboDict` and see if there is any command to execute. @@ -28,8 +34,20 @@ mode.ParseTable = require('libmodal/src/mode/ParseTable') ]] ------------------------------------ function mode._comboSelect(modeName) - local comboDict = vars.combos.instances[modeName] - -- TODO translate `LibmodalEnterWithCombos` + -- Stop any running timers + if vars.timers.instances[modeName] then + vars.timers.instances[modeName]:stop() + vars.timers.instances[modeName] = nil + end + + -- Append the latest input to the locally stored input history. + table.insert( + vars.input.instances[modeName], + vars.nvim_get(vars.input, modeName) + ) + + -- Get the combo dict. + local comboTable = vars.combos.instances[modeName] end ------------------------ @@ -64,17 +82,19 @@ function mode.enter(...) -- Determine whether a callback was specified, or a combo table. if type(args[2]) == globals.TYPE_TBL then - mode._initTimeouts(modeName) + mode._initTimeouts(modeName, args[2]) end --[[ MODE LOOP. ]] - while true do + local continueMode = true + while continueMode do -- Try (using pcall) to use the mode. local noErrors = pcall(function() -- If the mode is not handling exit events automatically and the global exit var is true. if not handleExitEvents and var.nvim_get(vars.exit, modeName) then - break + continueMode = false + return end -- Echo the indicator. @@ -86,7 +106,8 @@ function mode.enter(...) -- Make sure that the user doesn't want to exit. if handleExitEvents and uinput == globals.ESC_NR then - break + continueMode = false + return -- If the second argument was a dict, parse it. elseif type(args[2]) == globals.TYPE_TBL then mode._comboSelect(modeName) @@ -95,12 +116,12 @@ function mode.enter(...) args[2]() end - end)() + end) -- If there were errors, handle them. if not noErrors then mode._showError() - break + continueMode = false end end @@ -111,7 +132,7 @@ function mode.enter(...) winState:restore() end -function mode._initTimeouts(modeName) +function mode._initTimeouts(modeName, comboTable) -- Placeholder for timeout value. local doTimeout = nil @@ -124,7 +145,7 @@ function mode._initTimeouts(modeName) vars.timeout.instances[modeName] = doTimeout -- Build the parse tree. - vars.combos.instances[modeName] = mode.ParseTable:new(args[2]) + vars.combos.instances[modeName] = mode.ParseTable.new(comboTable) -- Initialize the input history variable. vars.input.instances[modeName] = {} @@ -144,6 +165,7 @@ end * PUBLICIZE MODULE */ --]] + mode.enter('test', {}) return mode diff --git a/lua/libmodal/src/prompt/init.lua b/lua/libmodal/src/prompt/init.lua index 9011659..cc0c933 100644 --- a/lua/libmodal/src/prompt/init.lua +++ b/lua/libmodal/src/prompt/init.lua @@ -3,18 +3,24 @@ * MODULE */ --]] + local prompt = {} ----------------------------------- +--[[ + /* + * LIB `prompt` + */ +--]] + +-------------------------- --[[ SUMMARY: * Enter a prompt. ]] - ---[[ PARAMETERS: +--[[ PARAMS: * `args[1]` => the prompt name. * `args[2]` => the prompt callback, or mode command table. ]] ----------------------------------- +-------------------------- function prompt.enter(...) local args = {...} end diff --git a/lua/libmodal/src/utils/DateTime/init.lua b/lua/libmodal/src/utils/DateTime/init.lua new file mode 100644 index 0000000..cb0199d --- /dev/null +++ b/lua/libmodal/src/utils/DateTime/init.lua @@ -0,0 +1,54 @@ +--[[ + /* + * MODULE + */ +--]] + +local DateTime = {} + +--[[ + /* + * CLASS `DateTime` + */ +--]] + +----------------------- +--[[ SUMMARY: + * Get the current date. +]] +----------------------- +function DateTime.now() + dateTime = os.date("*t") + + ------------------------------- + --[[ SUMMARY: + * Add some `duration` to `self`. + ]] + --[[ PARAMS: + * `duration` => the duration of time to add. + ]] + --[[ RETURNS: + * A new `DateTime` with the added seconds. + ]] + ------------------------------- + function dateTime:add(seconds) + local time + -- Copy `self` to `time` + for k,v in pairs(self) do + time[k] = v + end + -- Add `seconds` to `time` + time.sec = time.sec + seconds + return time + end + + return dateTime +end + +--[[ + /* + * PUBLICIZE MODULE + */ +--]] + +return DateTime diff --git a/lua/libmodal/src/utils/Indicator/Entry.lua b/lua/libmodal/src/utils/Indicator/Entry.lua index 0c8a916..e30e7a9 100644 --- a/lua/libmodal/src/utils/Indicator/Entry.lua +++ b/lua/libmodal/src/utils/Indicator/Entry.lua @@ -3,6 +3,7 @@ * MODULE */ --]] + local Entry = {} --[[ @@ -11,16 +12,15 @@ local Entry = {} */ --]] --------------------------------------------------------- +-------------------------------- --[[ SUMMARY: * Create a new `Indicator.Entry`. ]] - --[[ PARAMS: * `hlgroup` => The `highlight-group` to be used for this `Indicator.Entry`. * `str` => The text for this `Indicator.Entry`. ]] --------------------------------------------------------- +-------------------------------- function Entry.new(hlgroup, str) return { ['hl'] = hlgroup, @@ -33,4 +33,5 @@ end * PUBLICIZE MODULE */ --]] + return Entry diff --git a/lua/libmodal/src/utils/Indicator/init.lua b/lua/libmodal/src/utils/Indicator/init.lua index a473e73..80159fe 100644 --- a/lua/libmodal/src/utils/Indicator/init.lua +++ b/lua/libmodal/src/utils/Indicator/init.lua @@ -3,6 +3,7 @@ * IMPORTS */ --]] + local Entry = require('libmodal/src/utils/Indicator/Entry') --[[ @@ -10,6 +11,7 @@ local Entry = require('libmodal/src/utils/Indicator/Entry') * MODULE */ --]] + local Indicator = {} --[[ @@ -18,15 +20,14 @@ local Indicator = {} */ --]] ------------------------------------------------ +-------------------------------- --[[ SUMMARY: * Create a new `Indicator`. ]] - --[[ PARAMS: * `modeName` => the name of the mode that this `Indicator` is for. ]] ------------------------------------------------ +-------------------------------- function Indicator:new(modeName) return { Entry.new('LibmodalStar', '*'), @@ -41,4 +42,5 @@ end * PUBLICIZE MODULE */ --]] + return Indicator diff --git a/lua/libmodal/src/utils/WindowState/init.lua b/lua/libmodal/src/utils/WindowState/init.lua index 8de82e9..7ef6538 100644 --- a/lua/libmodal/src/utils/WindowState/init.lua +++ b/lua/libmodal/src/utils/WindowState/init.lua @@ -3,6 +3,7 @@ * IMPORTS */ --]] + local api = vim.api --[[ @@ -10,6 +11,7 @@ local api = vim.api * MODULE */ --]] + local WindowState = {} local height = 'winheight' @@ -43,4 +45,5 @@ end * PUBLICIZE MODULE */ --]] + return WindowState diff --git a/lua/libmodal/src/utils/api.lua b/lua/libmodal/src/utils/api.lua index b1f1978..c892490 100644 --- a/lua/libmodal/src/utils/api.lua +++ b/lua/libmodal/src/utils/api.lua @@ -5,7 +5,7 @@ --]] local globals = require('libmodal/src/base/globals') -local Entry = require('libmodal/src/utils/Indicator/Entry.lua') +local Entry = require('libmodal/src/utils/Indicator/Entry') --[[ /* @@ -41,7 +41,7 @@ end --[[ SUMMARY: * Check whether or not some variable exists. ]] ---[[ +--[[ PARAMS: * `scope` => The scope of the variable (i.e. `g`, `l`, etc.) * `var` => the variable to check for. ]] @@ -59,7 +59,7 @@ function api.nvim_input() return api.nvim_eval('getchar()') end ------------------------- +--------------------------------- --[[ SUMMARY: * Echo a table of {`hlgroup`, `str`} tables. * Meant to be read as "nvim list echo". @@ -67,13 +67,13 @@ end --[[ PARAMS: * `hlTables` => the tables to echo with highlights. ]] ------------------------- +--------------------------------- function api.nvim_lecho(hlTables) api.nvim_redraw() for _, hlTable in ipairs(hlTables) do api.nvim_command( -- `:echohl` the hlgroup and then `:echon` the string. - "execute(['echohl " .. hlTable.hl .. "', 'echon " .. hlTable.str .. "'])" + "echon execute(['echohl \"" .. hlTable.hl .. "\"', 'echon \"" .. hlTable.str .. "\"'])" ) end api.nvim_command('echohl None') @@ -89,7 +89,7 @@ function api.nvim_redraw() api.nvim_command('mode') end -------------------------------- +-------------------------------------- --[[ SUMMARY: * Show a `title` error. ]] @@ -97,7 +97,7 @@ end * `title` => the title of the error. * `msg` => the message of the error. ]] -------------------------------- +-------------------------------------- function api.nvim_show_err(title, msg) api.nvim_lecho({ Entry.new('Title', title .. '\n'), diff --git a/lua/libmodal/src/utils/init.lua b/lua/libmodal/src/utils/init.lua index 2ee3a45..e286dff 100644 --- a/lua/libmodal/src/utils/init.lua +++ b/lua/libmodal/src/utils/init.lua @@ -6,6 +6,7 @@ local utils = {} utils.api = require('libmodal/src/utils/api') +utils.DateTime = require('libmodal/src/utils/DateTime') utils.Indicator = require('libmodal/src/utils/Indicator') utils.WindowState = require('libmodal/src/utils/WindowState') utils.vars = require('libmodal/src/utils/vars') diff --git a/lua/libmodal/src/utils/vars.lua b/lua/libmodal/src/utils/vars.lua index 1fac054..2fd6519 100644 --- a/lua/libmodal/src/utils/vars.lua +++ b/lua/libmodal/src/utils/vars.lua @@ -76,10 +76,12 @@ end * VARS */ --]] -new('combos' , 'ModeCombos') -new('exit' , 'ModeExit') -new('input' , 'ModeInput') + +new('combos' , 'ModeCombos' ) +new('exit' , 'ModeExit' ) +new('input' , 'ModeInput' ) new('timeout' , 'ModeTimeout') +new('timer' , 'ModeTimer' ) --[[ /*