From b35e436abc72609043a74b38df8308dead8d2542 Mon Sep 17 00:00:00 2001 From: Iron-E Date: Sat, 2 May 2020 00:57:42 -0400 Subject: [PATCH] Structural improvements; begin to implement mode.enter --- lua/libmodal/init.lua | 6 +- lua/libmodal/src/base/globals.lua | 17 +++++ lua/libmodal/src/base/init.lua | 17 +++++ lua/libmodal/src/init.lua | 6 +- lua/libmodal/src/mode.lua | 35 --------- lua/libmodal/src/mode/init.lua | 64 ++++++++++++++++ .../src/{prompt.lua => prompt/init.lua} | 14 +--- lua/libmodal/src/utils.lua | 60 --------------- lua/libmodal/src/utils/Indicator/Entry.lua | 33 +++++++++ lua/libmodal/src/utils/Indicator/init.lua | 44 +++++++++++ lua/libmodal/src/utils/WindowState/init.lua | 33 +++++++++ lua/libmodal/src/utils/api.lua | 37 ++++++++++ lua/libmodal/src/utils/init.lua | 19 +++++ lua/libmodal/src/utils/vars.lua | 74 +++++++++++++++++++ 14 files changed, 351 insertions(+), 108 deletions(-) create mode 100644 lua/libmodal/src/base/globals.lua create mode 100644 lua/libmodal/src/base/init.lua delete mode 100644 lua/libmodal/src/mode.lua create mode 100644 lua/libmodal/src/mode/init.lua rename lua/libmodal/src/{prompt.lua => prompt/init.lua} (67%) delete mode 100644 lua/libmodal/src/utils.lua create mode 100644 lua/libmodal/src/utils/Indicator/Entry.lua create mode 100644 lua/libmodal/src/utils/Indicator/init.lua create mode 100644 lua/libmodal/src/utils/WindowState/init.lua create mode 100644 lua/libmodal/src/utils/api.lua create mode 100644 lua/libmodal/src/utils/init.lua create mode 100644 lua/libmodal/src/utils/vars.lua diff --git a/lua/libmodal/init.lua b/lua/libmodal/init.lua index 2812cff..00326e9 100644 --- a/lua/libmodal/init.lua +++ b/lua/libmodal/init.lua @@ -8,7 +8,9 @@ libmodal.mode = require('libmodal/src/mode') libmodal.prompt = require('libmodal/src/prompt') libmodal.utils = require('libmodal/src/utils') ---[[/* +--[[ + /* * PUBLICIZE MODULE - */]] + */ +--]] return libmodal diff --git a/lua/libmodal/src/base/globals.lua b/lua/libmodal/src/base/globals.lua new file mode 100644 index 0000000..9147f85 --- /dev/null +++ b/lua/libmodal/src/base/globals.lua @@ -0,0 +1,17 @@ +--[[ + /* + * MODULE + */ +--]] +local globals = {} + +globals.VIM_FALSE = 0 +globals.VIM_TRUE = 1 + +--[[ + /* + * PUBLICIZE MODULE + */ +--]] +return globals + diff --git a/lua/libmodal/src/base/init.lua b/lua/libmodal/src/base/init.lua new file mode 100644 index 0000000..2620fba --- /dev/null +++ b/lua/libmodal/src/base/init.lua @@ -0,0 +1,17 @@ +--[[ + /* + * 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 799e469..7cef3c0 100644 --- a/lua/libmodal/src/init.lua +++ b/lua/libmodal/src/init.lua @@ -3,7 +3,10 @@ * MODULE */ --]] -local libmodal = {} +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 @@ -13,3 +16,4 @@ local libmodal = {} */ --]] return libmodal + diff --git a/lua/libmodal/src/mode.lua b/lua/libmodal/src/mode.lua deleted file mode 100644 index 1a12988..0000000 --- a/lua/libmodal/src/mode.lua +++ /dev/null @@ -1,35 +0,0 @@ ---[[ - /* - * IMPORTS - */ ---]] -local libmodal = require('libmodal/src') - ---[[ - /* - * MODULE - */ ---]] -libmodal.mode = {} - --------------------------------- ---[[ SUMMARY: - * Enter a mode. -]] - ---[[ PARAMETERS: - * `args[1]` => the mode name. - * `args[2]` => the mode callback, or mode combo table. - * `args[3]` => optional exit supresion flag. -]] --------------------------------- -function libmodal.mode.enter(...) - local args = {...} -end - ---[[ - /* - * PUBLICIZE MODULE - */ ---]] -return libmodal.mode diff --git a/lua/libmodal/src/mode/init.lua b/lua/libmodal/src/mode/init.lua new file mode 100644 index 0000000..46c4a86 --- /dev/null +++ b/lua/libmodal/src/mode/init.lua @@ -0,0 +1,64 @@ +--[[ + /* + * IMPORTS + */ +--]] + +local globals = require('libmodal/src/base/globals') +local utils = require('libmodal/src/utils') +local api = utils.api + +--[[ + /* + * MODULE + */ +--]] + +local mode = {} + +-------------------------------- +--[[ SUMMARY: + * Enter a mode. +]] + +--[[ PARAMETERS: + * `args[1]` => the mode name. + * `args[2]` => the mode callback, or mode combo table. + * `args[3]` => optional exit supresion flag. +]] +-------------------------------- +function mode.enter(...) + local args = {...} + + --[[ VAR INIT ]] + + -- Create the indicator for the mode. + local indicator = utils.Indicator:new(args[1]) + -- Grab the state of the window. + local winState = utils.WindowState.new() + -- Convert the name into one that can be used for variables. + local modeName = string.lower(args[1]) + local handleExitEvents = false + if #args > 2 and args[3] then + handleExitEvents = true + end + -- Determine whether a callback was specified, or a combo table. + local doTimeout = nil + if type(args[2]) == 'table' then + if api.nvim_exists('g', utils.vars.timeout.name(modeName)) then + doTimeout = utils.vars.get(vars.timeout, modeName) + else + doTimeout = utils.vars.libmodalTimeout + end + print(doTimeout) + end +end + +--[[ + /* + * PUBLICIZE MODULE + */ +--]] +mode.enter('test', {}) +return mode + diff --git a/lua/libmodal/src/prompt.lua b/lua/libmodal/src/prompt/init.lua similarity index 67% rename from lua/libmodal/src/prompt.lua rename to lua/libmodal/src/prompt/init.lua index d08b045..9011659 100644 --- a/lua/libmodal/src/prompt.lua +++ b/lua/libmodal/src/prompt/init.lua @@ -1,16 +1,9 @@ ---[[ - /* - * IMPORTS - */ ---]] -local libmodal = require('libmodal/src') - --[[ /* * MODULE */ --]] -libmodal.prompt = {} +local prompt = {} ---------------------------------- --[[ SUMMARY: @@ -22,7 +15,7 @@ libmodal.prompt = {} * `args[2]` => the prompt callback, or mode command table. ]] ---------------------------------- -function libmodal.prompt.enter(...) +function prompt.enter(...) local args = {...} end @@ -31,4 +24,5 @@ end * PUBLICIZE MODULE */ --]] -return libmodal.prompt +return prompt + diff --git a/lua/libmodal/src/utils.lua b/lua/libmodal/src/utils.lua deleted file mode 100644 index 999d50e..0000000 --- a/lua/libmodal/src/utils.lua +++ /dev/null @@ -1,60 +0,0 @@ ---[[ - /* - * IMPORTS - */ ---]] -local libmodal = require('libmodal/src') - ---[[ - /* - * MODULE - */ ---]] -libmodal.utils = {} -libmodal.utils.Indicator = {} -libmodal.utils.Indicator.Entry = {} - ---[[ - /* - * INDICATOR - */ ---]] - ------------------------------------------------ ---[[ SUMMARY: - * Create a new `Indicator`. -]] - ---[[ PARAMS: - * `modeName` => the name of the mode that this `Indicator` is for. -]] ------------------------------------------------ -function libmodal.utils.Indicator:new(modeName) - return { - self.Entry.new('LibmodalStar', '*')(), - self.Entry.new( 'None', '*' )(), - self.Entry.new( 'LibmodalPrompt', tostring(modeName) )(), - self.Entry.new('None', ' > ')(), - } -end - --------------------------------------------------------- ---[[ 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 libmodal.utils.Indicator.Entry.new(hlgroup, str) - return {hlgroup, str} -end - ---[[ - /* - * PUBLICIZE MODULE - */ ---]] -return libmodal.utils diff --git a/lua/libmodal/src/utils/Indicator/Entry.lua b/lua/libmodal/src/utils/Indicator/Entry.lua new file mode 100644 index 0000000..cf11d9c --- /dev/null +++ b/lua/libmodal/src/utils/Indicator/Entry.lua @@ -0,0 +1,33 @@ +--[[ + /* + * MODULE + */ +--]] +local Entry = {} + +--[[ + /* + * STRUCT `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 {hlgroup, str} +end + +--[[ + /* + * PUBLICIZE MODULE + */ +--]] +return Entry diff --git a/lua/libmodal/src/utils/Indicator/init.lua b/lua/libmodal/src/utils/Indicator/init.lua new file mode 100644 index 0000000..8484c7c --- /dev/null +++ b/lua/libmodal/src/utils/Indicator/init.lua @@ -0,0 +1,44 @@ +--[[ + /* + * IMPORTS + */ +--]] +local Entry = require('libmodal/src/utils/Indicator/Entry') + +--[[ + /* + * MODULE + */ +--]] +local Indicator = {} + +--[[ + /* + * STRUCT `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', '*'), + Entry.new( 'None', '*' ), + Entry.new( 'LibmodalPrompt', tostring(modeName) ), + Entry.new('None', ' > '), + } +end + +--[[ + /* + * PUBLICIZE MODULE + */ +--]] +return Indicator diff --git a/lua/libmodal/src/utils/WindowState/init.lua b/lua/libmodal/src/utils/WindowState/init.lua new file mode 100644 index 0000000..671c527 --- /dev/null +++ b/lua/libmodal/src/utils/WindowState/init.lua @@ -0,0 +1,33 @@ +--[[ + /* + * IMPORTS + */ +--]] +local api = vim.api + +--[[ + /* + * MODULE + */ +--]] +local WindowState = {} + +--[[ + /* + * STRUCT `WindowState` + */ +--]] + +function WindowState.new() + return { + ['height'] = api.nvim_get_option('winheight'), + ['width'] = api.nvim_get_option('winwidth') + } +end + +--[[ + /* + * PUBLICIZE MODULE + */ +--]] +return WindowState diff --git a/lua/libmodal/src/utils/api.lua b/lua/libmodal/src/utils/api.lua new file mode 100644 index 0000000..becb15f --- /dev/null +++ b/lua/libmodal/src/utils/api.lua @@ -0,0 +1,37 @@ +--[[ + /* + * IMPORTS + */ +--]] + +local globals = require('libmodal/src/base/globals') + +--[[ + /* + * MODULE + */ +--]] + +local api = vim.api + +----------------------------------- +--[[ SUMMARY: + * Check whether or not some variable exists. +]] + +--[[ + * `scope` => The scope of the variable (i.e. `g`, `l`, etc.) + * `var` => the variable to check for. +]] +----------------------------------- +function api.nvim_exists(scope, var) + return api.nvim_eval("exists('" .. scope .. ":" .. var .. "')") ~= globals.VIM_FALSE +end + +--[[ + /* + * PUBLICIZE MODULE + */ +--]] + +return api diff --git a/lua/libmodal/src/utils/init.lua b/lua/libmodal/src/utils/init.lua new file mode 100644 index 0000000..2ee3a45 --- /dev/null +++ b/lua/libmodal/src/utils/init.lua @@ -0,0 +1,19 @@ +--[[ + /* + * MODULE + */ +--]] + +local utils = {} +utils.api = require('libmodal/src/utils/api') +utils.Indicator = require('libmodal/src/utils/Indicator') +utils.WindowState = require('libmodal/src/utils/WindowState') +utils.vars = require('libmodal/src/utils/vars') + +--[[ + /* + * PUBLICIZE MODULE + */ +--]] + +return utils diff --git a/lua/libmodal/src/utils/vars.lua b/lua/libmodal/src/utils/vars.lua new file mode 100644 index 0000000..0e7680c --- /dev/null +++ b/lua/libmodal/src/utils/vars.lua @@ -0,0 +1,74 @@ +--[[ + /* + * IMPORTS + */ +--]] + +local api = vim.api + +--[[ + /* + * MODULE + */ +--]] + +local vars = { + combos = {}, + input = {}, + libmodalTimeout = api.nvim_get_var('libmodalTimeouts'), + timeout = {} +} + +--[[ + /* + * HELPERS + */ +--]] + +------------------------------- +--[[ SUMMARY: + * Create a new entry in `vars` +]] + +--[[ PARAMS: + * `keyName` => the name of the key used to refer to this variable in `vars`. + * `varName` => the name of the variable as it is stored in vim. +]] +------------------------------- +local function new(keyName, varName) + vars[keyName] = { + name = function(modeName) + return modeName .. varName + end + } +end + +------------------------------- +--[[ SUMMARY: + * Retrieve a variable value. +]] + +--[[ PARAMS: + * `var` => the `vars.*` table to retrieve the value of. + * `modeName` => the mode name this value is being retrieved for. +]] +------------------------------- +function vars.get(var, modeName) + return api.nvim_get_vars(var.name(modeName)) +end + +--[[ + /* + * VARS + */ +--]] +new('combos' , 'ModeCombos') +new('input' , 'ModeInput') +new('timeout' , 'ModeTimeout') + +--[[ + /* + * PUBLICIZE MODULE + */ +--]] +return vars