From e3cf6d4c3ec10ba7ccfc3738467df1e451d8eebc Mon Sep 17 00:00:00 2001 From: Iron-E Date: Sun, 13 Jun 2021 12:48:07 -0400 Subject: [PATCH 1/7] perf(plugin): use lua for plugin initialization SEE #10 --- plugin/libmodal.lua | 20 ++++++++++++++++++++ plugin/libmodal.vim | 17 ----------------- 2 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 plugin/libmodal.lua delete mode 100644 plugin/libmodal.vim diff --git a/plugin/libmodal.lua b/plugin/libmodal.lua new file mode 100644 index 0000000..126c21e --- /dev/null +++ b/plugin/libmodal.lua @@ -0,0 +1,20 @@ +local exe = vim.api.nvim_command +local g = vim.g +local go = vim.go + +if g.loaded_libmodal then return end +g.loaded_libmodal = true + +if g.libmodalTimeouts == nil then + g.libmodalTimeouts = go.timeout +end + +--[[/* User Configuration */]] + +-- The default highlight groups (for colors) are specified below. +-- Change these default colors by defining or linking the corresponding highlight group. +exe +[[ + highlight default link LibmodalPrompt ModeMsg + highlight default link LibmodalStar StatusLine +]] diff --git a/plugin/libmodal.vim b/plugin/libmodal.vim deleted file mode 100644 index c770303..0000000 --- a/plugin/libmodal.vim +++ /dev/null @@ -1,17 +0,0 @@ -if exists('g:loaded_libmodal') - finish -endif -let g:loaded_libmodal = 1 - -if !exists('g:libmodalTimeouts') - let g:libmodalTimeouts = &timeout -endif - -" ************************************************************ -" * User Configuration -" ************************************************************ - -" The default highlight groups (for colors) are specified below. -" Change these default colors by defining or linking the corresponding highlight group. -highlight default link LibmodalPrompt ModeMsg -highlight default link LibmodalStar StatusLine From 3aff4c71089e4f0ee10cbeb3e23868e70e6350d7 Mon Sep 17 00:00:00 2001 From: Iron-E Date: Wed, 7 Jul 2021 12:40:33 -0400 Subject: [PATCH 2/7] chore(autoload): remove unused function --- autoload/libmodal.vim | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/autoload/libmodal.vim b/autoload/libmodal.vim index 457ceca..3ca8f84 100644 --- a/autoload/libmodal.vim +++ b/autoload/libmodal.vim @@ -1,21 +1,3 @@ -" SUMMARY: -" * Get user input with some `completions`. -" PARAMS: -" * `indicator` => the prompt string. -" * `completions` => the list of completions to provide. -" RETURNS: -" * Input from `input()`. -function! libmodal#_inputWith(indicator, completions) - function! LibmodalCompletionsProvider(argLead, cmdLine, cursorPos) abort closure - return luaeval( - \ 'require("libmodal/src/Prompt").createCompletionsProvider(_A[1])(_A[2], _A[3], _A[4])', - \ [a:completions, a:argLead, a:cmdLine, a:cursorPos] - \) - endfunction - - return input(a:indicator, '', 'customlist,LibmodalCompletionsProvider') -endfunction - " SUMMARY: " * Runs the nvim-libmodal command prompt loop. The function takes an optional " argument specifying how many times to run (runs until exiting by default). From 06648c277b7608f3f55eb079da550b0eb626c7ed Mon Sep 17 00:00:00 2001 From: Iron-E Date: Wed, 7 Jul 2021 12:40:52 -0400 Subject: [PATCH 3/7] perf(plugin): simplify lua --- plugin/libmodal.lua | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/plugin/libmodal.lua b/plugin/libmodal.lua index 126c21e..a1a8153 100644 --- a/plugin/libmodal.lua +++ b/plugin/libmodal.lua @@ -1,19 +1,13 @@ -local exe = vim.api.nvim_command local g = vim.g -local go = vim.go if g.loaded_libmodal then return end g.loaded_libmodal = true -if g.libmodalTimeouts == nil then - g.libmodalTimeouts = go.timeout -end - ---[[/* User Configuration */]] +g.libmodalTimeouts = g.libmodalTimeouts or vim.go.timeout -- The default highlight groups (for colors) are specified below. -- Change these default colors by defining or linking the corresponding highlight group. -exe +vim.cmd [[ highlight default link LibmodalPrompt ModeMsg highlight default link LibmodalStar StatusLine From afdfadf365b0cea900a3db354feaefd7f51cb622 Mon Sep 17 00:00:00 2001 From: Iron-E Date: Wed, 7 Jul 2021 14:25:14 -0400 Subject: [PATCH 4/7] ref: use Neovim 0.5 BREAKING CHANGE: now requires Neovim 0.5 --- README.md | 42 ++-- autoload/libmodal.vim | 18 ++ doc/libmodal-lua.txt | 188 +++++++----------- doc/libmodal.txt | 89 ++++----- examples/lua/key-combos-manually.lua | 7 +- examples/lua/key-combos-submode.lua | 2 +- examples/lua/key-combos-supress-exit.lua | 6 +- examples/lua/key-combos.lua | 16 +- examples/lua/layer-simple.lua | 14 +- examples/lua/layer.lua | 18 +- examples/lua/prompt-callback.lua | 12 +- examples/lua/prompt-commands.lua | 10 +- examples/lua/submodes.lua | 6 +- examples/lua/supress-exit.lua | 11 +- lua/libmodal/init.lua | 6 +- .../src/Indicator/HighlightSegment.lua | 4 +- lua/libmodal/src/Indicator/init.lua | 2 +- lua/libmodal/src/Layer.lua | 6 +- lua/libmodal/src/Mode.lua | 32 ++- lua/libmodal/src/Prompt.lua | 31 ++- lua/libmodal/src/Vars.lua | 16 +- lua/libmodal/src/collections/ParseTable.lua | 6 +- lua/libmodal/src/collections/Popup.lua | 26 ++- lua/libmodal/src/collections/Stack.lua | 2 +- lua/libmodal/src/collections/init.lua | 6 +- lua/libmodal/src/globals.lua | 16 +- lua/libmodal/src/init.lua | 16 +- lua/libmodal/src/utils/Help.lua | 135 ++++--------- lua/libmodal/src/utils/WindowState.lua | 25 +-- lua/libmodal/src/utils/api.lua | 82 ++------ lua/libmodal/src/utils/init.lua | 28 +-- 31 files changed, 361 insertions(+), 517 deletions(-) diff --git a/README.md b/README.md index 5289a16..8eec55a 100644 --- a/README.md +++ b/README.md @@ -59,35 +59,35 @@ _COLORS.text = _COLORS.gray_light -- Table which gets hex values from _COLORS. local _HEX_COLORS = setmetatable( - {['bar'] = setmetatable({}, {['__index'] = function(_, key) return _COLORS.bar[key] and _COLORS.bar[key][1] or nil end})}, - {['__index'] = function(_, key) local color = _COLORS[key] return color and color[1] or nil end} + {bar = setmetatable({}, {__index = function(_, key) return _COLORS.bar[key] and _COLORS.bar[key][1] or nil end})}, + {__index = function(_, key) local color = _COLORS[key] return color and color[1] or nil end} ) local _MODES = { - ['c'] = {'COMMAND-LINE', _COLORS.red}, - ['ce'] = {'NORMAL EX', _COLORS.red}, - ['cv'] = {'EX', _COLORS.red}, - ['i'] = {'INSERT', _COLORS.green}, - ['ic'] = {'INS-COMPLETE', _COLORS.green}, - ['n'] = {'NORMAL', _COLORS.purple}, - ['no'] = {'OPERATOR-PENDING', _COLORS.purple}, - ['r'] = {'HIT-ENTER', _COLORS.blue}, + c = {'COMMAND-LINE', _COLORS.red}, + ce = {'NORMAL EX', _COLORS.red}, + cv = {'EX', _COLORS.red}, + i = {'INSERT', _COLORS.green}, + ic = {'INS-COMPLETE', _COLORS.green}, + n = {'NORMAL', _COLORS.purple}, + no = {'OPERATOR-PENDING', _COLORS.purple}, + r = {'HIT-ENTER', _COLORS.blue}, ['r?'] = {':CONFIRM', _COLORS.blue}, - ['rm'] = {'--MORE', _COLORS.blue}, - ['R'] = {'REPLACE', _COLORS.red}, - ['Rv'] = {'VIRTUAL', _COLORS.red}, - ['s'] = {'SELECT', _COLORS.blue}, - ['S'] = {'SELECT', _COLORS.blue}, - ['t'] = {'TERMINAL', _COLORS.orange}, - ['v'] = {'VISUAL', _COLORS.blue}, - ['V'] = {'VISUAL LINE', _COLORS.blue}, + rm = {'--MORE', _COLORS.blue}, + R = {'REPLACE', _COLORS.red}, + Rv = {'VIRTUAL', _COLORS.red}, + s = {'SELECT', _COLORS.blue}, + S = {'SELECT', _COLORS.blue}, + t = {'TERMINAL', _COLORS.orange}, + v = {'VISUAL', _COLORS.blue}, + V = {'VISUAL LINE', _COLORS.blue}, ['!'] = {'SHELL', _COLORS.yellow}, -- libmodal - ['TABS'] = _COLORS.tan, - ['BUFFERS'] = _COLORS.teal, - ['TABLES'] = _COLORS.orange_light, + TABS = _COLORS.tan, + BUFFERS = _COLORS.teal, + TABLES = _COLORS.orange_light, } require('galaxyline').section.left = diff --git a/autoload/libmodal.vim b/autoload/libmodal.vim index 3ca8f84..457ceca 100644 --- a/autoload/libmodal.vim +++ b/autoload/libmodal.vim @@ -1,3 +1,21 @@ +" SUMMARY: +" * Get user input with some `completions`. +" PARAMS: +" * `indicator` => the prompt string. +" * `completions` => the list of completions to provide. +" RETURNS: +" * Input from `input()`. +function! libmodal#_inputWith(indicator, completions) + function! LibmodalCompletionsProvider(argLead, cmdLine, cursorPos) abort closure + return luaeval( + \ 'require("libmodal/src/Prompt").createCompletionsProvider(_A[1])(_A[2], _A[3], _A[4])', + \ [a:completions, a:argLead, a:cmdLine, a:cursorPos] + \) + endfunction + + return input(a:indicator, '', 'customlist,LibmodalCompletionsProvider') +endfunction + " SUMMARY: " * Runs the nvim-libmodal command prompt loop. The function takes an optional " argument specifying how many times to run (runs until exiting by default). diff --git a/doc/libmodal-lua.txt b/doc/libmodal-lua.txt index df035aa..8dd38fb 100644 --- a/doc/libmodal-lua.txt +++ b/doc/libmodal-lua.txt @@ -35,7 +35,7 @@ See: |libmodal-usage|, |lua|, |lua-require-example|. 1. `libmodal` *libmodal-lua-libmodal* This is the base of |libmodal|. It can be imported using: > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' - local libmodal = require('libmodal') + local libmodal = require 'libmodal' -- Create a mock set of user mappings. local userTable = { - ['zf'] = "echo 'Hello!'", - ['zfo'] = "tabnew" + zf = "echo 'Hello!'", + zfo = "tabnew" } -- Create a new `ParseTable` @@ -225,14 +225,14 @@ FUNCTIONS *libmodal-lua-ParseTable-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' -- Simulate user input. local userInput = {122, 102} -- {'z', 'f'} -- Create a dummy `ParseTable`. local parseTable = libmodal.mode.ParseTable.new({ - ['zfo'] = 'echo "Hello!"' + zfo = 'echo "Hello!"' }) -- Inspect it. print(vim.inspect(parseTable)) @@ -257,7 +257,7 @@ FUNCTIONS *libmodal-lua-ParseTable-functions* > -- Create a dummy `ParseTable`. local parseTable = libmodal.mode.ParseTable.new({ - ['zfo'] = 'echo "Hello!"' + zfo = 'echo "Hello!"' }) -- Inspect it. print(vim.inspect(parseTable)) @@ -288,8 +288,8 @@ FUNCTIONS *libmodal-lua-ParseTable-functions* local parseTable = libmodal.mode.ParseTable.new({}) -- Create some dummy keybindings. local unparsedUserKeybinds = { - ['zfo'] = 'echo "Hello!"', - ['zfc'] = 'split' + zfo = 'echo "Hello!"', + zfc = 'split' } -- Add the dummy keybindings. @@ -312,16 +312,14 @@ opening it and keeping updated. Whenever a `Popup` is created, it is immediately opened. Additionally, it is opened with the following options: > local _winOpenOpts = { - ['anchor'] = 'SW', - ['col'] = api.nvim_get_option('columns') - 1, - ['focusable'] = false, - ['height'] = 1, - ['relative'] = 'editor', - ['row'] = api.nvim_get_option('lines') - - api.nvim_get_option('cmdheight') - - 1, - ['style'] = 'minimal', - ['width'] = 25, + anchor = 'SW', + col = vim.go.columns - 1, + focusable = false, + height = 1, + relative = 'editor', + row = vim.go.lines - vim.go.cmdheight - 1, + style = 'minimal', + width = 25, } < @@ -340,16 +338,14 @@ VARIABLES *libmodal-lua-Popup-variables* Value: ~ > { - ['anchor'] = 'SW', - ['col'] = api.nvim_get_option('columns') - 1, - ['focusable'] = false, - ['height'] = 1, - ['relative'] = 'editor', - ['row'] = api.nvim_get_option('lines') - - api.nvim_get_option('cmdheight') - - 1, - ['style'] = 'minimal', - ['width'] = 1 + anchor = 'SW', + col = vim.go.columns - 1, + focusable = false, + height = 1, + relative = 'editor', + row = vim.go.lines - vim.go.cmdheight - 1, + style = 'minimal', + width = 1 } < @@ -615,16 +611,12 @@ FUNCTIONS *libmodal-lua-globals-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' -- Get Lua's truth values. local falseValue = false local trueValue = true - -- Get Vim's `v:` truth values. - local v_falseValue = vim.api.nvim_get_vvar('false') - local v_trueValue = vim.api.nvim_get_vvar('true') - -- Get Vimscript's truth values. local vim_falseValue = libmodal.globals.VIM_FALSE local vim_trueValue = libmodal.globals.VIM_TRUE @@ -655,16 +647,12 @@ FUNCTIONS *libmodal-lua-globals-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' -- Get Lua's truth values. local falseValue = false local trueValue = true - -- Get Vim's `v:` truth values. - local v_falseValue = vim.api.nvim_get_vvar('false') - local v_trueValue = vim.api.nvim_get_vvar('true') - -- Get Vimscript's truth values. local vim_falseValue = libmodal.globals.VIM_FALSE local vim_trueValue = libmodal.globals.VIM_TRUE @@ -697,7 +685,7 @@ FUNCTIONS *libmodal-lua-indicator-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local indicator = libmodal.utils.Indicator.mode('FOO') libmodal.utils.api.nvim_lecho(indicator) < @@ -716,7 +704,7 @@ FUNCTIONS *libmodal-lua-indicator-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local indicator = libmodal.utils.Indicator.prompt('FOO') print(indicator) -- you can't use `nvim_lecho` with this one. < @@ -766,7 +754,7 @@ FUNCTIONS *libmodal-lua-HighlightSegment-Functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local api = libmodal.utils.api local HighlightSegment = libmodal.Indicator.HighlightSegment @@ -801,11 +789,11 @@ FUNCTIONS *libmodal-lua-Layer-function Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local layer = libmodal.Layer.new({ - ['n'] = { - ['gg'] = { - ['rhs'] = G + n = { + gg = { + rhs = G } } }) @@ -835,24 +823,24 @@ FUNCTIONS *libmodal-lua-Layer-function Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local layer = libmodal.Layer.new({ - ['n'] = { - ['gg'] = { - ['rhs'] = G + n = { + gg = { + rhs = G } } }) -- this adds a binding for `G` to `gg` in normal mode. - layer:map('n', 'G', 'gg', {['noremap'] = true}) + layer:map('n', 'G', 'gg', {noremap = true}) -- use `layer:exit()` or close the buffer to exit. layer:enter() -- you can also call `:map()` after entering. -- this adds a binding for `o` to `gg` in visual mode. - layer:map('v', 'o', 'gg', {['noremap'] = true}) + layer:map('v', 'o', 'gg', {noremap = true}) < See also: ~ @@ -871,17 +859,17 @@ FUNCTIONS *libmodal-lua-Layer-function Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local layer = libmodal.Layer.new({ - ['n'] = { - ['gg'] = { - ['rhs'] = G + n = { + gg = { + rhs = G } } }) -- This adds a binding for `G` to `gg` in normal mode. - layer:map('n', 'G', 'gg', {['noremap'] = true}) + layer:map('n', 'G', 'gg', {noremap = true}) -- Unmap the initial `gg`. layer:unmap('n', 'gg') @@ -909,11 +897,11 @@ FUNCTIONS *libmodal-lua-Layer-function Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local layer = libmodal.Layer.new({ - ['n'] = { - ['gg'] = { - ['rhs'] = G + n = { + gg = { + rhs = G } } }) @@ -963,7 +951,7 @@ Additionally, one may alter the actual `function`s that are used by a `Mode` without forking |libmodal|. By using `setmetatable`, and `vim.inspect()`, one can grab code from the |nvim-libmodal| repository and change whatever they want to for their mode specifically. > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local Mode = libmodal.Mode local HighlightSegment = libmodal.Indicator.HighlightSegment @@ -1170,7 +1158,7 @@ FUNCTIONS *libmodal-lua-utils-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' -- Run `pcall` on an anonymous function. local noErrors, pcall_err = pcall(function() @@ -1213,7 +1201,7 @@ FUNCTIONS *libmodal-lua-api-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' -- This function is called every time the user presses a key. local function _instruction() @@ -1235,7 +1223,7 @@ FUNCTIONS *libmodal-lua-api-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' libmodal.utils.api.nvim_bell() < @@ -1245,26 +1233,6 @@ FUNCTIONS *libmodal-lua-api-functions* 'errorbells' For bell settings. 'visualbell' For bell settings. -`api`.nvim_exists({scope}, {var}) *libmodal-lua-api.nvim_exists()* - - Check whether or not some |variable| exists. - - Parameters: ~ - {scope} The scope of the |variable| (i.e. `g:`, `l:`, etc.) - {var} The |variable| to check for. - - Example: ~ -> - local libmodal = require('libmodal') - - -- Set a mock variable to 3. - libmodal.utils.api.nvim_set_var('foo', 3) - print(libmodal.utils.api.nvim_exists('g', 'foo')) -- true - - libmodal.utils.api.nvim_command('unlet g:foo') - print(libmodal.utils.api.nvim_exists('g', 'foo')) -- false -< - `api`.nvim_input() *libmodal-lua-api.nvim_input()* Gets one character of user input, as a number. @@ -1276,7 +1244,7 @@ FUNCTIONS *libmodal-lua-api-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' -- Get one character of user input. local char = string.char(libmodal.utils.api.nvim_input()) @@ -1297,7 +1265,7 @@ FUNCTIONS *libmodal-lua-api-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' -- Create an indicator. local indicator = libmodal.utils.Indicator.mode('FOO') @@ -1315,12 +1283,11 @@ FUNCTIONS *libmodal-lua-api-functions* Example: ~ > - local libmodal = require('libmodal') - local api = vim.api + local libmodal = require 'libmodal' -- Echo hello and prompt for input before continuing. - api.nvim_command("echo 'Hello!'") - api.nvim_call_function('getchar', {}) + vim.api.nvim_command "echo 'Hello!'" + vim.fn.getchar() -- Clear the screen. libmodal.utils.api.nvim_redraw() @@ -1336,7 +1303,7 @@ FUNCTIONS *libmodal-lua-api-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' -- Show an error. libmodal.utils.api.nvim_show_err( @@ -1371,18 +1338,18 @@ FUNCTIONS *libmodal-lua-Help-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' -- Create a table of mock user commands. local commands = { - ['close'] = 'tabclose', - ['new'] = 'tabnew' + close = 'tabclose', + new = 'tabnew' } -- Create a table of mock user maps. local maps = { - ['zf'] = 'split', - ['zfo'] = 'echo "Hello!"' + zf = 'split', + zfo = 'echo "Hello!"' } local commandHelp = libmodal.utils.Help.new(commands, 'COMMANDS') @@ -1395,18 +1362,18 @@ FUNCTIONS *libmodal-lua-Help-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' -- Create a table of mock user commands. local commands = { - ['close'] = 'tabclose', - ['new'] = 'tabnew' + close = 'tabclose', + new = 'tabnew' } -- Create a table of mock user maps. local maps = { - ['zf'] = 'split', - ['zfo'] = 'echo "Hello!"' + zf = 'split', + zfo = 'echo "Hello!"' } local commandHelp = libmodal.utils.Help.new(commands, 'COMMANDS') @@ -1434,7 +1401,7 @@ FUNCTIONS *libmodal-lua-WindowState-functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local windowState = libmodal.utils.WindowState.new() print(vim.inspect(windowState)) < @@ -1449,18 +1416,17 @@ FUNCTIONS *libmodal-lua-WindowState-functions* Example: ~ > - local libmodal = require('libmodal') - local api = vim.api + local libmodal = require 'libmodal' -- Create a new `WindowState`. local windowState = libmodal.utils.WindowState.new() -- Set the 'winheight' to a new value. - api.nvim_set_option('winheight', 100) + vim.go.winheight = 100 -- Define a function to print the 'winheight' value. local print_height = function() - api.nvim_command('echo &winheight') + vim.api.nvim_command 'echo &winheight' end -- Print the 'winheight' prior to `restore()`. @@ -1492,7 +1458,7 @@ FUNCTIONS *libmodal-lua-Vars.functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local input = libmodal.Vars('input', 'FOO') print(input:name()) -- 'fooModeInput' @@ -1510,10 +1476,10 @@ FUNCTIONS *libmodal-lua-Vars.functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local input = libmodal.Vars('input', 'FOO') - vim.api.nvim_set_var(input:name(), 'test') + vim.g[input:name()] = 'test' print(input:nvimGet()) < @@ -1527,7 +1493,7 @@ FUNCTIONS *libmodal-lua-Vars.functions* Example: ~ > - local libmodal = require('libmodal') + local libmodal = require 'libmodal' local input = libmodal.Vars('input', 'FOO') input:nvimSet('test') diff --git a/doc/libmodal.txt b/doc/libmodal.txt index 7301423..cbb7952 100644 --- a/doc/libmodal.txt +++ b/doc/libmodal.txt @@ -144,10 +144,10 @@ FUNCTIONS *libmodal-usage-function map of user key-chord to Vim |command|s. Example: > -- LUA local modeInstruction = { - ['zf'] = 'split', - ['zfo'] = 'vsplit', + zf = 'split', + zfo = 'vsplit', -- You can also use lua functions - ['zfc'] = function() return 'tabnew' end + zfc = function() return 'tabnew' end } " VIMSCRIPT @@ -217,7 +217,7 @@ FUNCTIONS *libmodal-usage-function { [] = { [] = { - ['rhs'] = , + rhs = , }, … @@ -259,9 +259,9 @@ FUNCTIONS *libmodal-usage-function map of user inputs to Vim |command|s. Example: > -- LUA local modeInstruction = { - ['new'] = 'tabnew', - ['close'] = 'tabclose', - ['last'] = 'tablast' + new = 'tabnew', + close = 'tabclose', + last = 'tablast' } " VIMSCRIPT @@ -334,7 +334,6 @@ See: |libmodal-usage|, |libmodal-use-case|, |lua-require-example|. MODES *libmodal-examples-modes* Using a callback `function`: > - local api = vim.api local libmodal = require('libmodal') local fooModeInputHistory = {} @@ -346,14 +345,14 @@ Using a callback `function`: > function fooMode() fooModeInputHistory[#fooModeInputHistory + 1] = string.char( - api.nvim_get_var('fooModeInput') + vim.g.fooModeInput ) local index = 1 if fooModeInputHistory[1] == 'z' then if fooModeInputHistory[2] == 'f' then if fooModeInputHistory[3] == 'o' then - api.nvim_command("echom 'It works!'") + vim.api.nvim_command "echom 'It works!'" else index = 3 end else index = 2 end end @@ -377,9 +376,9 @@ using a |key-mapping| `dict`: > Using a |key-mapping| `table`: > local libmodal = require('libmodal') local fooModeCombos = { - ['zf'] = 'split', - ['zfo'] = 'vsplit', - ['zfc'] = 'tabnew' + zf = 'split', + zfo = 'vsplit', + zfc = 'tabnew' } libmodal.mode.enter('FOO', fooModeCombos) @@ -393,17 +392,17 @@ Using a callback `function`: > function fooMode() local userInput = string.char( - vim.api.nvim_get_var('fooModeInput') + vim.g.fooModeInput ) if userInput == '' then - vim.api.nvim_command("echom 'You cant leave using .'") + vim.api.nvim_command "echom 'You cant leave using .'" elseif userInput == 'q' then - vim.api.nvim_set_var('fooModeExit', true) + vim.g.fooModeExit = true end end - vim.api.nvim_set_var('fooModeExit', 0) + vim.g.fooModeExit = false libmodal.mode.enter('FOO', fooMode, true) < @@ -421,10 +420,10 @@ Using a |key-mapping| `table`: > local libmodal = require('libmodal') local fooModeCombos = { [''] = 'echom "You cant exit using escape."', - ['q'] = 'let g:fooModeExit = 1' + q = 'let g:fooModeExit = 1' } - vim.api.nvim_set_var('fooModeExit', 0) + vim.g.fooModeExit = false libmodal.mode.enter('FOO', fooModeCombos, true) < @@ -436,9 +435,9 @@ Using a callback `function`: > local fooModeRecurse = 0 function fooMode() - local userInput = string.char(vim.api.nvim_get_var( - 'foo' .. tostring(fooModeRecurse) .. 'ModeInput' - )) + local userInput = string.char(vim.g[ + 'foo'..tostring(fooModeRecurse)..'ModeInput' + ]) if userInput == 'z' then fooModeRecurse = fooModeRecurse + 1 @@ -475,7 +474,7 @@ Using a |key-mapping| `table`: > local libmodal = require('libmodal') local fooModeRecurse = 0 local fooModeCombos = { - ['z'] = 'lua fooMode()' + z = 'lua fooMode()' } function fooMode() @@ -494,14 +493,14 @@ LAYERS *libmodal-examples-layer -- save the exit function local exitFunc = libmodal.layer.enter({ - ['n'] = { -- normal mode - ['gg'] = { -- remap `gg` - ['rhs'] = 'G', -- map it to `G` - ['noremap'] = true -- don't remap + n = { -- normal mode + gg = { -- remap `gg` + rhs = 'G', -- map it to `G` + noremap = true -- don't remap }, - ['G'] = { -- remap `G` - ['rhs'] = 'gg', -- map it to `gg` - ['noremap'] = true -- don't remap + G = { -- remap `G` + rhs = 'gg', -- map it to `gg` + noremap = true -- don't remap } } }) @@ -516,17 +515,17 @@ PROMPTS *libmodal-examples-prompt Using a callback `function`: > local libmodal = require('libmodal') - local api = vim.api + local cmd = vim.api.nvim_command local commandList = {'new', 'close', 'last'} function fooMode() - local userInput = vim.api.nvim_get_var('fooModeInput') + local userInput = vim.api.nvim_get_var 'fooModeInput' if userInput == 'new' then - api.nvim_command('tabnew') + cmd 'tabnew' elseif userInput == 'close' then - api.nvim_command('tabclose') + cmd 'tabclose' elseif userInput == 'last' then - api.nvim_command('tablast') + cmd 'tablast' end end @@ -546,9 +545,9 @@ Using a |command| `dict`: > Using a |command| `table`: > local libmodal = require('libmodal') local commands = { - ['new'] = 'tabnew', - ['close'] = 'tabclose', - ['last'] = 'tablast' + new = 'tabnew', + close = 'tabclose', + last = 'tablast' } libmodal.prompt.enter('BAR', commands) @@ -589,16 +588,16 @@ To enable 'timeout's, one may set the following |variables|: Lua: ~ > " Set libmodal modes to turn timeouts on. - vim.api.nvim_set_var('libmodalTimeouts', true) + vim.g.libmodalTimeouts = true " Enable timeouts for specific mode. - vim.api.nvim_set_var('{name}ModeTimeouts', true) + vim.g[name..'ModeTimeouts'] = true < Vimscript: ~ > " Set libmodal modes to turn timeouts on. - let g:libmodalTimeouts = 1 + let g:libmodalTimeouts = v:true " Enable timeouts for specific mode. - let g:{name}ModeTimeouts = 1 + let g:{name}ModeTimeouts = v:true < @@ -648,13 +647,13 @@ then reset it upon exit. Example: function fooMode() -- Get the user's preferred timeout length. - local prevTimeoutLen = api.nvim_get_option('timeoutlen') + local prevTimeoutLen = vim.go.timeoutlen -- Set it to something else, like 1500ms. - api.nvim_set_option('timeoutlen', 1500) + vim.go.timeoutlen = 1500 -- Enter a mode. libmodal.mode.enter(…) -- Restore the `timeoutlen` - api.nvim_set_option('timeoutlen', prevTimeoutLen) + vim.go.timeoutlen = prevTimeoutLen end < diff --git a/examples/lua/key-combos-manually.lua b/examples/lua/key-combos-manually.lua index 6e65cd0..655bcab 100644 --- a/examples/lua/key-combos-manually.lua +++ b/examples/lua/key-combos-manually.lua @@ -1,6 +1,5 @@ -- Imports -local api = vim.api -local libmodal = require('libmodal') +local libmodal = require 'libmodal' -- Keep track of the user's input history manually. local _inputHistory = {} @@ -19,7 +18,7 @@ local function fooMode() -- Append to the input history, the latest button press. _inputHistory[#_inputHistory + 1] = string.char( -- The input is a character number. - api.nvim_get_var('fooModeInput') + vim.g.fooModeInput ) -- Custom logic to test for each character index to see if it matches the 'zfo' mapping. @@ -27,7 +26,7 @@ local function fooMode() if _inputHistory[1] == 'z' then if _inputHistory[2] == 'f' then if _inputHistory[3] == 'o' then - api.nvim_command("echom 'It works!'") + vim.api.nvim_command "echom 'It works!'" else index = 3 end else index = 2 diff --git a/examples/lua/key-combos-submode.lua b/examples/lua/key-combos-submode.lua index 3b1d728..f3b3d8c 100644 --- a/examples/lua/key-combos-submode.lua +++ b/examples/lua/key-combos-submode.lua @@ -5,7 +5,7 @@ local libmodal = require('libmodal') local fooModeRecurse = 0 -- Register 'z' as the map for recursing further (by calling the FooMode function again). local fooModeCombos = { - ['z'] = 'lua FooMode()' + z = 'lua FooMode()' } -- define the FooMode() function which is called whenever the user presses 'z' diff --git a/examples/lua/key-combos-supress-exit.lua b/examples/lua/key-combos-supress-exit.lua index 9f7a018..14457ce 100644 --- a/examples/lua/key-combos-supress-exit.lua +++ b/examples/lua/key-combos-supress-exit.lua @@ -1,14 +1,14 @@ -- Imports -local libmodal = require('libmodal') +local libmodal = require 'libmodal' -- Register key commands and what they do. local fooModeCombos = { [''] = 'echom "You cant exit using escape."', - ['q'] = 'let g:fooModeExit = 1' + q = 'let g:fooModeExit = 1' } -- Tell the mode not to exit automatically. -vim.api.nvim_set_var('fooModeExit', 0) +vim.g.fooModeExit = false -- Enter the mode using the key combos created before. libmodal.mode.enter('FOO', fooModeCombos, true) diff --git a/examples/lua/key-combos.lua b/examples/lua/key-combos.lua index 4ad1f2b..f097a06 100644 --- a/examples/lua/key-combos.lua +++ b/examples/lua/key-combos.lua @@ -1,19 +1,19 @@ -- Imports -local libmodal = require('libmodal') +local cmd = vim.api.nvim_command +local libmodal = require 'libmodal' -- A function which will split the window both horizontally and vertically. local function _split_twice() - local cmd = vim.api.nvim_command - cmd('split') - cmd('vsplit') + cmd 'split' + cmd 'vsplit' end -- Register key combos for splitting windows and then closing windows local fooModeCombos = { - ['zf'] = 'split', - ['zfo'] = 'vsplit', - ['zfc'] = 'q', - ['zff'] = _split_twice + zf = 'split', + zfo = 'vsplit', + zfc = 'q', + zff = _split_twice } -- Enter the mode using the key combos. diff --git a/examples/lua/layer-simple.lua b/examples/lua/layer-simple.lua index c0c72fc..57015d5 100644 --- a/examples/lua/layer-simple.lua +++ b/examples/lua/layer-simple.lua @@ -3,14 +3,14 @@ local libmodal = require('libmodal') -- create a new layer. local exitFunc = libmodal.layer.enter({ - ['n'] = { -- normal mode mappings - ['gg'] = { -- remap `gg` - ['rhs'] = 'G', -- map it to `G` - ['noremap'] = true, -- don't recursively map. + n = { -- normal mode mappings + gg = { -- remap `gg` + rhs = 'G', -- map it to `G` + noremap = true, -- don't recursively map. }, - ['G'] = { -- remap `G` - ['rhs'] = 'gg', -- map it to `gg` - ['noremap'] = true -- don't recursively map. + G = { -- remap `G` + rhs = 'gg', -- map it to `gg` + noremap = true -- don't recursively map. } } }) diff --git a/examples/lua/layer.lua b/examples/lua/layer.lua index 034a956..ee93020 100644 --- a/examples/lua/layer.lua +++ b/examples/lua/layer.lua @@ -3,14 +3,14 @@ local libmodal = require('libmodal') -- create a new layer. local layer = libmodal.Layer.new({ - ['n'] = { -- normal mode mappings - ['gg'] = { -- remap `gg` - ['rhs'] = 'G', -- map it to `G` - ['noremap'] = true, -- don't recursively map. + n = { -- normal mode mappings + gg = { -- remap `gg` + rhs = 'G', -- map it to `G` + noremap = true, -- don't recursively map. }, - ['G'] = { -- remap `G` - ['rhs'] = 'gg', -- map it to `gg` - ['noremap'] = true -- don't recursively map. + G = { -- remap `G` + rhs = 'gg', -- map it to `gg` + noremap = true -- don't recursively map. } } }) @@ -24,12 +24,12 @@ function LibmodalLayerExampleExit() end -- Add an additional mapping for `z`. -layer:map('n', 'z', 'gg', {['noremap'] = true}) +layer:map('n', 'z', 'gg', {noremap = true}) -- add an additional mapping for `q`. layer:map( 'n', 'q', ':lua LibmodalLayerExampleExit()', - {['noremap'] = true, ['silent'] = true} + {noremap = true, silent = true} ) --[[ unmap `gg` and `G`. Notice they both return to their defaults, diff --git a/examples/lua/prompt-callback.lua b/examples/lua/prompt-callback.lua index 9bca1b0..0746440 100644 --- a/examples/lua/prompt-callback.lua +++ b/examples/lua/prompt-callback.lua @@ -1,19 +1,19 @@ -- Imports -local libmodal = require('libmodal') -local api = vim.api +local libmodal = require 'libmodal' +local cmd = vim.api.nvim_command -- The list of commands. Providing this will allow for autocomplete. local commandList = {'new', 'close', 'last'} -- The function which will be called whenever the user enters a command. function FooMode() - local userInput = vim.api.nvim_get_var('fooModeInput') + local userInput = vim.g.fooModeInput if userInput == 'new' then - api.nvim_command('tabnew') + cmd 'tabnew' elseif userInput == 'close' then - api.nvim_command('tabclose') + cmd 'tabclose' elseif userInput == 'last' then - api.nvim_command('tablast') + cmd 'tablast' end end diff --git a/examples/lua/prompt-commands.lua b/examples/lua/prompt-commands.lua index be96481..ca1eb3d 100644 --- a/examples/lua/prompt-commands.lua +++ b/examples/lua/prompt-commands.lua @@ -1,12 +1,12 @@ -- Import -local libmodal = require('libmodal') +local libmodal = require 'libmodal' -- Define commands through a dictionary. local commands = { - ['new'] = 'tabnew', - ['close'] = 'tabclose', - ['last'] = 'tablast', - ['exit'] = libmodal.utils.api.mode_exit + new = 'tabnew', + close = 'tabclose', + last = 'tablast', + exit = libmodal.utils.api.mode_exit } -- Begin the prompt. diff --git a/examples/lua/submodes.lua b/examples/lua/submodes.lua index 4347e66..ed3ace3 100644 --- a/examples/lua/submodes.lua +++ b/examples/lua/submodes.lua @@ -1,5 +1,5 @@ -- Imports -local libmodal = require('libmodal') +local libmodal = require 'libmodal' -- Recurse counter local fooModeRecurse = 1 @@ -7,10 +7,10 @@ local fooModeRecurse = 1 -- Function which is called whenever the user presses a button function FooMode() -- Append to the input history, the latest button press. - local userInput = string.char(vim.api.nvim_get_var( + local userInput = string.char(vim.g[ -- The input is a character number. 'foo' .. tostring(fooModeRecurse) .. 'ModeInput' - )) + ]) -- If the user pressed 'z', then increase the counter and recurse. if userInput == 'z' then diff --git a/examples/lua/supress-exit.lua b/examples/lua/supress-exit.lua index 0df74fc..5af932f 100644 --- a/examples/lua/supress-exit.lua +++ b/examples/lua/supress-exit.lua @@ -1,25 +1,24 @@ -- Imports -local api = vim.api -local libmodal = require('libmodal') +local libmodal = require 'libmodal' -- Function which is called whenever the user presses a button local function fooMode() -- Append to the input history, the latest button press. local userInput = string.char( -- The input is a character number. - api.nvim_get_var('fooModeInput') + vim.g.fooModeInput ) if userInput == '' then - api.nvim_command("echom 'You cant leave using .'") + vim.api.nvim_command "echom 'You cant leave using .'" elseif userInput == 'q' then -- If the user presses 'q', libmodal will exit the mode. - api.nvim_set_var('fooModeExit', true) + vim.g.fooModeExit = true end end -- Tell libmodal not to exit the mode immediately. -api.nvim_set_var('fooModeExit', 0) +vim.g.fooModeExit = false -- Enter the mode. libmodal.mode.enter('FOO', fooMode, true) diff --git a/lua/libmodal/init.lua b/lua/libmodal/init.lua index f489803..c30cbb2 100644 --- a/lua/libmodal/init.lua +++ b/lua/libmodal/init.lua @@ -12,17 +12,17 @@ local libmodal = require('libmodal/src') */ --]] -libmodal.layer = {['enter'] = function(keymap) +libmodal.layer = {enter = function(keymap) local layer = libmodal.Layer.new(keymap) layer:enter() return function() layer:exit() end end} -libmodal.mode = {['enter'] = function(name, instruction, ...) +libmodal.mode = {enter = function(name, instruction, ...) libmodal.Mode.new(name, instruction, ...):enter() end} -libmodal.prompt = {['enter'] = function(name, instruction, ...) +libmodal.prompt = {enter = function(name, instruction, ...) libmodal.Prompt.new(name, instruction, ...):enter() end} diff --git a/lua/libmodal/src/Indicator/HighlightSegment.lua b/lua/libmodal/src/Indicator/HighlightSegment.lua index db5907a..6683a99 100644 --- a/lua/libmodal/src/Indicator/HighlightSegment.lua +++ b/lua/libmodal/src/Indicator/HighlightSegment.lua @@ -23,8 +23,8 @@ local HighlightSegment = {} -------------------------------- function HighlightSegment.new(hlgroup, str) return { - ['hl'] = hlgroup, - ['str'] = str + hl = hlgroup, + str = str } end diff --git a/lua/libmodal/src/Indicator/init.lua b/lua/libmodal/src/Indicator/init.lua index cabde39..4095d9e 100644 --- a/lua/libmodal/src/Indicator/init.lua +++ b/lua/libmodal/src/Indicator/init.lua @@ -5,7 +5,7 @@ --]] local Indicator = { - ['HighlightSegment'] = require('libmodal/src/Indicator/HighlightSegment') + HighlightSegment = require('libmodal/src/Indicator/HighlightSegment') } -- highlight group names diff --git a/lua/libmodal/src/Layer.lua b/lua/libmodal/src/Layer.lua index c904584..754cf41 100644 --- a/lua/libmodal/src/Layer.lua +++ b/lua/libmodal/src/Layer.lua @@ -13,7 +13,7 @@ local libmodal_api = require('libmodal/src/utils/api') */ --]] -local Layer = {['TYPE'] = 'libmodal-layer'} +local Layer = {TYPE = 'libmodal-layer'} local _BUFFER_CURRENT = 0 local _ERR_NO_MAP = 'E5555: API call: E31: No such mapping' @@ -142,7 +142,7 @@ function _metaLayer:map(mode, lhs, rhs, options) -- add the new mapping to the keymap self._keymap[mode][lhs] = vim.tbl_extend('force', - options, {['rhs'] = rhs} + options, {rhs = rhs} ) end @@ -239,7 +239,7 @@ end ----------------------------------------------------- function Layer.new(keymap) return setmetatable( - {['_keymap'] = keymap}, + {_keymap = keymap}, _metaLayer ) end diff --git a/lua/libmodal/src/Mode.lua b/lua/libmodal/src/Mode.lua index b119a87..7ffb3d2 100644 --- a/lua/libmodal/src/Mode.lua +++ b/lua/libmodal/src/Mode.lua @@ -10,8 +10,8 @@ local ParseTable = require('libmodal/src/collections/ParseTable') local utils = require('libmodal/src/utils') local Vars = require('libmodal/src/Vars') -local vim = vim local api = vim.api +local go = vim.go --[[ /* @@ -19,15 +19,13 @@ local api = vim.api */ --]] -local Mode = {['TYPE'] = 'libmodal-mode'} +local Mode = {TYPE = 'libmodal-mode'} local _HELP = '?' local _TIMEOUT = { - ['CHAR'] = 'ø', - ['LEN'] = api.nvim_get_option('timeoutlen'), - ['SEND'] = function(__self) - api.nvim_feedkeys(__self.CHAR, 'nt', false) - end + CHAR = 'ø', + LEN = go.timeoutlen, + SEND = function(self) api.nvim_feedkeys(self.CHAR, 'nt', false) end } _TIMEOUT.NR = string.byte(_TIMEOUT.CHAR) @@ -40,9 +38,9 @@ _TIMEOUT.NR = string.byte(_TIMEOUT.CHAR) local _metaMode = classes.new(Mode.TYPE) local _metaInputBytes = classes.new(nil, { - ['clear'] = function(__self) - for i, _ in ipairs(__self) do - __self[i] = nil + clear = function(self) + for i, _ in ipairs(self) do + self[i] = nil end end }) @@ -180,7 +178,7 @@ function _metaMode:_initMappings() self._timeouts = Vars.new('timeouts', self._name) -- Read the correct timeout variable. - if utils.api.nvim_exists('g', self._timeouts:name()) + if vim.g[self._timeouts:name()] ~= nil then self._timeouts.enabled = self._timeouts:nvimGet() else self._timeouts.enabled = @@ -285,12 +283,12 @@ function Mode.new(name, instruction, ...) -- Inherit the metatable. local self = setmetatable( { - ['exit'] = Vars.new('exit', name), - ['indicator'] = require('libmodal/src/Indicator').mode(name), - ['input'] = Vars.new('input', name), - ['_instruction'] = instruction, - ['_name'] = name, - ['_winState'] = utils.WindowState.new(), + exit = Vars.new('exit', name), + indicator = require('libmodal/src/Indicator').mode(name), + input = Vars.new('input', name), + _instruction = instruction, + _name = name, + _winState = utils.WindowState.new(), }, _metaMode ) diff --git a/lua/libmodal/src/Prompt.lua b/lua/libmodal/src/Prompt.lua index 90277cd..2996523 100644 --- a/lua/libmodal/src/Prompt.lua +++ b/lua/libmodal/src/Prompt.lua @@ -1,15 +1,11 @@ ---[[ - /* - * IMPORTS - */ ---]] +--[[/* IMPORTS */]] local globals = require('libmodal/src/globals') local utils = require('libmodal/src/utils') local Vars = require('libmodal/src/Vars') -local vim = vim local api = vim.api +local fn = vim.fn --[[ /* @@ -17,7 +13,7 @@ local api = vim.api */ --]] -local Prompt = {['TYPE'] = 'libmodal-prompt'} +local Prompt = {TYPE = 'libmodal-prompt'} local _HELP = 'help' local _REPLACEMENTS = { @@ -62,7 +58,7 @@ function _metaPrompt:_executeInstruction(userInput) else -- show an error. utils.api.nvim_show_err(globals.DEFAULT_ERROR_TITLE, 'Unknown command') end - elseif type(instruction) == globals.TYPE_STR and vim.fn then -- The instruction is a function. Works on Neovim 0.5+. + elseif type(instruction) == globals.TYPE_STR then -- The instruction is a function. Works on Neovim 0.5+. vim.fn[instruction]() else -- attempt to call the instruction. instruction() @@ -90,13 +86,10 @@ function _metaPrompt:_inputLoop() api.nvim_command('echohl ' .. self.indicator.hl) -- set the user input variable - if self._completions - then userInput = - api.nvim_call_function('libmodal#_inputWith', { - self.indicator.str, self._completions - }) + if self._completions then userInput = + fn['libmodal#_inputWith'](self.indicator.str, self._completions) else userInput = - api.nvim_call_function('input', {self.indicator}) + fn.input(self.indicator) end -- determine what to do with the input @@ -203,11 +196,11 @@ function Prompt.new(name, instruction, ...) local self = setmetatable( { - ['exit'] = Vars.new('exit', name), - ['indicator'] = require('libmodal/src/Indicator').prompt(name), - ['input'] = require('libmodal/src/Vars').new('input', name), - ['_instruction'] = instruction, - ['_name'] = name + exit = Vars.new('exit', name), + indicator = require('libmodal/src/Indicator').prompt(name), + input = require('libmodal/src/Vars').new('input', name), + _instruction = instruction, + _name = name }, _metaPrompt ) diff --git a/lua/libmodal/src/Vars.lua b/lua/libmodal/src/Vars.lua index 2a4e3f7..15f0f2f 100644 --- a/lua/libmodal/src/Vars.lua +++ b/lua/libmodal/src/Vars.lua @@ -1,8 +1,4 @@ ---[[ - /* - * IMPORTS - */ ---]] +--[[/* IMPORTS */]] local api = vim.api @@ -12,11 +8,9 @@ local api = vim.api */ --]] -local _TIMEOUT_GLOBAL_NAME = 'libmodalTimeouts' - local Vars = { - [_TIMEOUT_GLOBAL_NAME] = api.nvim_get_var(_TIMEOUT_GLOBAL_NAME), - ['TYPE'] = 'libmodal-vars' + libmodalTimeouts = vim.g.libmodalTimeouts, + TYPE = 'libmodal-vars' } --[[ @@ -48,7 +42,7 @@ end ]] ------------------------------------ function _metaVars:nvimGet() - return api.nvim_get_var(self:name()) + return vim.g[self:name()] end ----------------------------------------- @@ -61,7 +55,7 @@ end ]] ----------------------------------------- function _metaVars:nvimSet(val) - api.nvim_set_var(self:name(), val) + vim.g[self:name()] = val end --[[ diff --git a/lua/libmodal/src/collections/ParseTable.lua b/lua/libmodal/src/collections/ParseTable.lua index e689657..0e57121 100644 --- a/lua/libmodal/src/collections/ParseTable.lua +++ b/lua/libmodal/src/collections/ParseTable.lua @@ -15,8 +15,8 @@ local globals = require('libmodal/src/globals') local _REGEX_ALL = '.' local ParseTable = { - ['CR'] = 13, -- The number corresponding to in vim. - ['TYPE'] = 'libmodal-parse-table', + CR = 13, -- The number corresponding to in vim. + TYPE = 'libmodal-parse-table', -------------------------------------- --[[ SUMMARY: @@ -30,7 +30,7 @@ local ParseTable = { * The split `str`. ]] -------------------------------------- - ['stringSplit'] = function(str, regex) + stringSplit = function(str, regex) local split = {} for char in string.gmatch(str, regex) do split[#split + 1] = char diff --git a/lua/libmodal/src/collections/Popup.lua b/lua/libmodal/src/collections/Popup.lua index 65f6963..9490156 100644 --- a/lua/libmodal/src/collections/Popup.lua +++ b/lua/libmodal/src/collections/Popup.lua @@ -4,8 +4,8 @@ */ --]] -local vim = vim local api = vim.api +local go = vim.go --[[ /* @@ -15,17 +15,15 @@ local api = vim.api local Popup = require('libmodal/src/classes').new( 'libmodal-popup', - {['config'] = { - ['anchor'] = 'SW', - ['col'] = api.nvim_get_option('columns') - 1, - ['focusable'] = false, - ['height'] = 1, - ['relative'] = 'editor', - ['row'] = api.nvim_get_option('lines') - - api.nvim_get_option('cmdheight') - - 1, - ['style'] = 'minimal', - ['width'] = 1 + {config = { + anchor = 'SW', + col = go.columns - 1, + focusable = false, + height = 1, + relative = 'editor', + row = go.lines - go.cmdheight - 1, + style = 'minimal', + width = 1 }} ) @@ -146,8 +144,8 @@ function Popup.new(config) local self = setmetatable( { - ['buffer'] = buf, - ['_inputChars'] = {}, + buffer = buf, + _inputChars = {}, }, _metaPopup ) diff --git a/lua/libmodal/src/collections/Stack.lua b/lua/libmodal/src/collections/Stack.lua index 981a3a9..3da3c11 100644 --- a/lua/libmodal/src/collections/Stack.lua +++ b/lua/libmodal/src/collections/Stack.lua @@ -4,7 +4,7 @@ */ --]] -local Stack = {['TYPE'] = 'libmodal-stack'} +local Stack = {TYPE = 'libmodal-stack'} --[[ /* diff --git a/lua/libmodal/src/collections/init.lua b/lua/libmodal/src/collections/init.lua index b446561..5ae5101 100644 --- a/lua/libmodal/src/collections/init.lua +++ b/lua/libmodal/src/collections/init.lua @@ -1,5 +1,5 @@ return { - ['ParseTable'] = require('libmodal/src/collections/ParseTable'), - ['Popup'] = require('libmodal/src/collections/Popup'), - ['Stack'] = require('libmodal/src/collections/Stack') + ParseTable = require('libmodal/src/collections/ParseTable'), + Popup = require('libmodal/src/collections/Popup'), + Stack = require('libmodal/src/collections/Stack') } diff --git a/lua/libmodal/src/globals.lua b/lua/libmodal/src/globals.lua index 4fa2321..ce87a68 100644 --- a/lua/libmodal/src/globals.lua +++ b/lua/libmodal/src/globals.lua @@ -2,17 +2,17 @@ local _VIM_FALSE = 0 local _VIM_TRUE = 1 return { - ['DEFAULT_ERROR_TITLE'] = 'vim-libmodal error', + DEFAULT_ERROR_TITLE = 'vim-libmodal error', - ['ESC_NR'] = 27, + ESC_NR = 27, - ['TYPE_FUNC'] = 'function', - ['TYPE_NUM'] = 'number', - ['TYPE_STR'] = 'string', - ['TYPE_TBL'] = 'table', + TYPE_FUNC = 'function', + TYPE_NUM = 'number', + TYPE_STR = 'string', + TYPE_TBL = 'table', - ['VIM_FALSE'] = _VIM_FALSE, - ['VIM_TRUE'] = _VIM_TRUE, + VIM_FALSE = _VIM_FALSE, + VIM_TRUE = _VIM_TRUE, is_false = function(val) return val == false or val == _VIM_FALSE diff --git a/lua/libmodal/src/init.lua b/lua/libmodal/src/init.lua index ac122a2..0d332be 100644 --- a/lua/libmodal/src/init.lua +++ b/lua/libmodal/src/init.lua @@ -1,10 +1,10 @@ return { - ['classes'] = require('libmodal/src/classes'), - ['collections'] = require('libmodal/src/collections'), - ['globals'] = require('libmodal/src/globals'), - ['Indicator'] = require('libmodal/src/Indicator'), - ['Layer'] = require('libmodal/src/Layer'), - ['Mode'] = require('libmodal/src/Mode'), - ['Prompt'] = require('libmodal/src/Prompt'), - ['utils'] = require('libmodal/src/utils') + classes = require('libmodal/src/classes'), + collections = require('libmodal/src/collections'), + globals = require('libmodal/src/globals'), + Indicator = require('libmodal/src/Indicator'), + Layer = require('libmodal/src/Layer'), + Mode = require('libmodal/src/Mode'), + Prompt = require('libmodal/src/Prompt'), + utils = require('libmodal/src/utils') } diff --git a/lua/libmodal/src/utils/Help.lua b/lua/libmodal/src/utils/Help.lua index 9b6c464..aebc820 100644 --- a/lua/libmodal/src/utils/Help.lua +++ b/lua/libmodal/src/utils/Help.lua @@ -1,57 +1,55 @@ ---[[ - /* - * IMPORTS - */ ---]] -local vim = vim +--[[/* IMPORTS */]] + local globals = require('libmodal/src/globals') ---[[ - /* - * MODULE - */ ---]] +--[[/* Utilities */]] + +--- Align `tbl` according to the `longestKeyLen`. +--- @param tbl table what to align. +--- @param longestKeyLen number how long the longest key is. +--- @return table aligned +local function tabAlign(tbl, longestKeyLen) + local toPrint = {} + for key, value in pairs(tbl) do + toPrint[#toPrint + 1] = key + local len = string.len(key) + local byte = string.byte(key) + -- account for ASCII chars that take up more space. + if byte <= 32 or byte == 127 then len = len + 1 end + + for _ = len, longestKeyLen do + toPrint[#toPrint + 1] = ' ' + end + + toPrint[#toPrint + 1] = table.concat( + {' │ ', '\n'}, + (type(value) == globals.TYPE_STR) and value or '' + ) + end + return toPrint +end + +--[[/* MODULE */]] -local Help = {['TYPE'] = 'libmodal-help'} +local Help = {TYPE = 'libmodal-help'} ---[[ - /* - * META `Help` - */ ---]] +--[[/* META `Help` */]] local _metaHelp = require('libmodal/src/classes').new(Help.TYPE) -------------------------- ---[[ SUMMARY: - * Show the contents of this `Help`. -]] -------------------------- +--- Show the contents of this `Help`. function _metaHelp:show() for _, helpText in ipairs(self) do print(helpText) end - vim.api.nvim_call_function('getchar', {}) + vim.fn.getchar() end ---[[ - /* - * CLASS `Help` - */ ---]] - - ----------------------------------------- ---[[ SUMMARY: - * Create a default help table with `commandsOrMaps` and vim expressions. -]] ---[[ PARAMS: - * `commandsOrMaps` => the table of commands or mappings to vim expressions. -]] ---[[ RETURNS: - * A new `Help`. -]] ----------------------------------------- +--[[/* CLASS `Help` */]] + +--- Create a default help table with `commandsOrMaps` and vim expressions. +--- @param commandsOrMaps table commands or mappings to vim expressions. +--- @return table Help function Help.new(commandsOrMaps, title) -- find the longest key in the table. local longestKeyLen = 0 @@ -67,68 +65,21 @@ function Help.new(commandsOrMaps, title) longestKeyLen = string.len(title) end - -- define the separator for entries in the help table. - local SEPARATOR_TEMPLATE = {' │ ', '\n'} - - ---------------------- - --[[ SUMMARY: - * Align `tbl` according to the `longestKey`. - ]] - --[[ PARAMS: - * `tbl` => the table to align. - ]] - --[[ RETURNS: - * The aligned `tbl`. - ]] - ---------------------- - local function tabAlign(tbl) - local toPrint = {} - for key, value in pairs(tbl) do - toPrint[#toPrint + 1] = key - local len = string.len(key) - local byte = string.byte(key) - -- account for ASCII chars that take up more space. - if byte <= 32 or byte == 127 then len = len + 1 end - - for _ = len, longestKeyLen do - toPrint[#toPrint + 1] = ' ' - end - - toPrint[#toPrint + 1] = table.concat( - SEPARATOR_TEMPLATE, - (type(value) == globals.TYPE_STR) and value or '' - ) - end - return toPrint - end - -- define the separator for the help table. local helpSeparator = {} - for i = 1, string.len(title) do - helpSeparator[i] = '-' - end + for i = 1, string.len(title) do helpSeparator[i] = '-' end helpSeparator = table.concat(helpSeparator) -- Create a new `Help`. return setmetatable( { [1] = ' ', - [2] = table.concat(tabAlign({ - [title] = 'VIM EXPRESSION' - })), - [3] = table.concat(tabAlign({ - [helpSeparator] = '--------------' - })), - [4] = table.concat(tabAlign(commandsOrMaps)), + [2] = table.concat(tabAlign({[title] = 'VIM EXPRESSION'}, longestKeyLen)), + [3] = table.concat(tabAlign({[helpSeparator] = '--------------'}, longestKeyLen)), + [4] = table.concat(tabAlign(commandsOrMaps, longestKeyLen)), }, _metaHelp ) end ---[[ - /* - * PUBLICIZE `Help`. - */ ---]] - return Help diff --git a/lua/libmodal/src/utils/WindowState.lua b/lua/libmodal/src/utils/WindowState.lua index 777340e..e8bccb8 100644 --- a/lua/libmodal/src/utils/WindowState.lua +++ b/lua/libmodal/src/utils/WindowState.lua @@ -1,11 +1,7 @@ ---[[ - /* - * IMPORTS - */ ---]] +--[[/* IMPORTS */]] -local api = vim.api -local libmodal_api = require('libmodal/src/utils/api') +local go = vim.go +local api = require('libmodal/src/utils/api') --[[ /* @@ -13,10 +9,7 @@ local libmodal_api = require('libmodal/src/utils/api') */ --]] -local WindowState = {['TYPE'] = 'libmodal-window-state'} - -local height = 'winheight' -local width = 'winwidth' +local WindowState = {TYPE = 'libmodal-window-state'} --[[ /* @@ -32,9 +25,9 @@ local _metaWindowState = require('libmodal/src/classes').new(WindowState.TYPE) ]] ----------------------------------- function _metaWindowState:restore() - api.nvim_set_option(height, self.height) - api.nvim_set_option(width, self.width) - libmodal_api.nvim_redraw() + go.winheight = self.height + go.winwidth = self.width + api.nvim_redraw() end --[[ @@ -54,8 +47,8 @@ end function WindowState.new() return setmetatable( { - ['height'] = api.nvim_get_option(height), - ['width'] = api.nvim_get_option(width), + height = go.winheight, + width = go.winwidth, }, _metaWindowState ) diff --git a/lua/libmodal/src/utils/api.lua b/lua/libmodal/src/utils/api.lua index b7f0f94..a0c8c52 100644 --- a/lua/libmodal/src/utils/api.lua +++ b/lua/libmodal/src/utils/api.lua @@ -1,21 +1,16 @@ ---[[ - /* - * MODULE - */ ---]] +--[[/* IMPORTS */]] -local api = {} +local fn = vim.fn local globals = require('libmodal/src/globals') +local HighlightSegment = require('libmodal/src/Indicator/HighlightSegment') local vim_api = vim.api ---------------------------------- ---[[ SUMMARY: - * Send a character to exit a mode. -]] ---[[ PARAMS: - * `exit_char` => the character used to exit the mode, or ESCAPE if none was provided. -]] ---------------------------------- +--[[/* MODULE */]] + +local api = {} + +--- Send a character to exit a mode. +--- @param exit_char string the character used to exit the mode, or ESCAPE if none was provided. function api.mode_exit(exit_char) -- If there was no provided `exit_char`, or it is a character code. if not exit_char or type(exit_char) == globals.TYPE_NUM then @@ -27,54 +22,14 @@ function api.mode_exit(exit_char) vim_api.nvim_feedkeys(exit_char, 'nt', false) end ------------------------- ---[[ SUMMARY: - * Make vim ring the visual/audio bell, if it is enabled. -]] ------------------------- +--- Make vim ring the visual/audio bell, if it is enabled. function api.nvim_bell() vim_api.nvim_command('normal '..string.char(27)) -- escape char 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. -]] ------------------------------------- -function api.nvim_exists(scope, var) - return vim_api.nvim_call_function('exists', {scope..':'..var}) == require('libmodal/src/globals').VIM_TRUE -end - -------------------------- ---[[ SUMMARY: - * Gets one character of user input, as a number. -]] ---[[ REMARKS: - * This could also be: - ```lua - local cmd = { - '"while 1"', - '"let c = getchar(0)"', - '"if empty(c)"', - '"sleep 20m"', - '"else"', - '"echo c"', - '"break"', - '"endif"', - '"endwhile"' - } - - return tonumber(vim.api.nvim_call_function("execute",cmd)) - ``` - However, I'm not sure if it would accidentally affect text. -]] -------------------------- +--- Gets one character of user input, as a number. function api.nvim_input() - return vim_api.nvim_call_function('getchar', {}) + return fn.getchar() end -------------------------- @@ -84,7 +39,7 @@ end ]] -------------------------- function api.nvim_redraw() - vim_api.nvim_command('mode') + vim_api.nvim_command 'mode' end --------------------------------- @@ -112,7 +67,7 @@ function api.nvim_lecho(hlTables) vim_api.nvim_command(table.concat(lecho_template)) end - vim_api.nvim_command('echohl None') + vim_api.nvim_command 'echohl None' end -------------------------------------- @@ -125,18 +80,11 @@ end ]] -------------------------------------- function api.nvim_show_err(title, msg) - local HighlightSegment = require('libmodal/src/Indicator/HighlightSegment') api.nvim_lecho({ HighlightSegment.new('Title', tostring(title)..'\n'), HighlightSegment.new('Error', tostring(msg)), }) - vim_api.nvim_call_function('getchar', {}) + fn.getchar() end ---[[ - /* - * PUBLICIZE MODULE - */ ---]] - return api diff --git a/lua/libmodal/src/utils/init.lua b/lua/libmodal/src/utils/init.lua index 841c754..8e39b2e 100644 --- a/lua/libmodal/src/utils/init.lua +++ b/lua/libmodal/src/utils/init.lua @@ -1,8 +1,8 @@ ---[[ - /* - * MODULE - */ ---]] +--[[/* IMPORTS */]] + +local v = vim.v + +--[[/* MODULE */]] local utils = {} utils.api = require('libmodal/src/utils/api') @@ -15,26 +15,14 @@ utils.WindowState = require('libmodal/src/utils/WindowState') */ --]] ----------------------------------- ---[[ SUMMARY: - * Show an error from `pcall()`. -]] ---[[ PARAMS: - `pcall_err` => the error generated by `pcall()`. -]] ----------------------------------- +--- Show an error from `pcall()`. +--- @param pcall_err string the error generated by `pcall()`. function utils.show_error(pcall_err) - local api = vim.api - utils.api.nvim_bell() utils.api.nvim_show_err( require('libmodal/src/globals').DEFAULT_ERROR_TITLE, - api.nvim_get_vvar('throwpoint') - .. '\n' .. - api.nvim_get_vvar('exception') - .. '\n' .. - tostring(pcall_err) + v.throwpoint..'\n'..v.exception..'\n'..pcall_err ) end From b4419002a297a4832dffcb4111dd21cf79ff4d1a Mon Sep 17 00:00:00 2001 From: Iron-E Date: Wed, 8 Sep 2021 15:05:28 -0400 Subject: [PATCH 5/7] perf: don't make local copies of global `vim` module --- doc/libmodal.txt | 10 +++--- examples/lua/key-combos.lua | 5 ++- examples/lua/prompt-callback.lua | 7 ++-- lua/libmodal/src/Mode.lua | 25 ++++--------- lua/libmodal/src/Prompt.lua | 17 +++------ lua/libmodal/src/collections/Popup.lua | 49 +++++++------------------- lua/libmodal/src/utils/WindowState.lua | 9 +++-- lua/libmodal/src/utils/api.lua | 16 ++++----- lua/libmodal/src/utils/init.lua | 18 ++-------- plugin/libmodal.lua | 8 ++--- 10 files changed, 51 insertions(+), 113 deletions(-) diff --git a/doc/libmodal.txt b/doc/libmodal.txt index cbb7952..574bc21 100644 --- a/doc/libmodal.txt +++ b/doc/libmodal.txt @@ -515,17 +515,16 @@ PROMPTS *libmodal-examples-prompt Using a callback `function`: > local libmodal = require('libmodal') - local cmd = vim.api.nvim_command local commandList = {'new', 'close', 'last'} function fooMode() - local userInput = vim.api.nvim_get_var 'fooModeInput' + local userInput = vim.g.fooModeInput if userInput == 'new' then - cmd 'tabnew' + vim.api.nvim_command 'tabnew' elseif userInput == 'close' then - cmd 'tabclose' + vim.api.nvim_command 'tabclose' elseif userInput == 'last' then - cmd 'tablast' + vim.api.nvim_command 'tablast' end end @@ -642,7 +641,6 @@ then reset it upon exit. Example: < Lua: ~ > - local api = vim.api local libmodal = require('libmodal') function fooMode() diff --git a/examples/lua/key-combos.lua b/examples/lua/key-combos.lua index f097a06..d01971d 100644 --- a/examples/lua/key-combos.lua +++ b/examples/lua/key-combos.lua @@ -1,11 +1,10 @@ -- Imports -local cmd = vim.api.nvim_command local libmodal = require 'libmodal' -- A function which will split the window both horizontally and vertically. local function _split_twice() - cmd 'split' - cmd 'vsplit' + vim.api.nvim_command 'split' + vim.api.nvim_command 'vsplit' end -- Register key combos for splitting windows and then closing windows diff --git a/examples/lua/prompt-callback.lua b/examples/lua/prompt-callback.lua index 0746440..c742448 100644 --- a/examples/lua/prompt-callback.lua +++ b/examples/lua/prompt-callback.lua @@ -1,6 +1,5 @@ -- Imports local libmodal = require 'libmodal' -local cmd = vim.api.nvim_command -- The list of commands. Providing this will allow for autocomplete. local commandList = {'new', 'close', 'last'} @@ -9,11 +8,11 @@ local commandList = {'new', 'close', 'last'} function FooMode() local userInput = vim.g.fooModeInput if userInput == 'new' then - cmd 'tabnew' + vim.api.nvim_command 'tabnew' elseif userInput == 'close' then - cmd 'tabclose' + vim.api.nvim_command 'tabclose' elseif userInput == 'last' then - cmd 'tablast' + vim.api.nvim_command 'tablast' end end diff --git a/lua/libmodal/src/Mode.lua b/lua/libmodal/src/Mode.lua index 7ffb3d2..d98026e 100644 --- a/lua/libmodal/src/Mode.lua +++ b/lua/libmodal/src/Mode.lua @@ -1,8 +1,4 @@ ---[[ - /* - * IMPORTS - */ ---]] +--[[/* IMPORTS */]] local classes = require('libmodal/src/classes') local globals = require('libmodal/src/globals') @@ -10,24 +6,17 @@ local ParseTable = require('libmodal/src/collections/ParseTable') local utils = require('libmodal/src/utils') local Vars = require('libmodal/src/Vars') -local api = vim.api -local go = vim.go - ---[[ - /* - * MODULE - */ ---]] +--[[/* MODULE */]] local Mode = {TYPE = 'libmodal-mode'} local _HELP = '?' local _TIMEOUT = { CHAR = 'ø', - LEN = go.timeoutlen, - SEND = function(self) api.nvim_feedkeys(self.CHAR, 'nt', false) end + LEN = vim.go.timeoutlen, + SEND = function(self) vim.api.nvim_feedkeys(self.CHAR, 'nt', false) end } -_TIMEOUT.NR = string.byte(_TIMEOUT.CHAR) +_TIMEOUT.CHAR_NUMBER = string.byte(_TIMEOUT.CHAR) --[[ /* @@ -60,7 +49,7 @@ classes = nil ----------------------------------------------------------- function _metaMode._commandTableExecute(instruction) if type(instruction) == globals.TYPE_FUNC then instruction() - else api.nvim_command(instruction) end + else vim.api.nvim_command(instruction) end end ----------------------------------------------- @@ -209,7 +198,7 @@ function _metaMode:_inputLoop() local userInput = utils.api.nvim_input() -- Return if there was a timeout event. - if userInput == _TIMEOUT.NR then + if userInput == _TIMEOUT.CHAR_NUMBER then return true end diff --git a/lua/libmodal/src/Prompt.lua b/lua/libmodal/src/Prompt.lua index 2996523..00b1195 100644 --- a/lua/libmodal/src/Prompt.lua +++ b/lua/libmodal/src/Prompt.lua @@ -4,14 +4,7 @@ local globals = require('libmodal/src/globals') local utils = require('libmodal/src/utils') local Vars = require('libmodal/src/Vars') -local api = vim.api -local fn = vim.fn - ---[[ - /* - * MODULE - */ ---]] +--[[/* MODULE */]] local Prompt = {TYPE = 'libmodal-prompt'} @@ -51,7 +44,7 @@ function _metaPrompt:_executeInstruction(userInput) if type(to_execute) == globals.TYPE_FUNC then to_execute() else - api.nvim_command(instruction[userInput]) + vim.api.nvim_command(instruction[userInput]) end elseif userInput == _HELP then -- The user did not define a 'help' command, so use the default. self._help:show() @@ -83,13 +76,13 @@ function _metaPrompt:_inputLoop() local userInput = '' -- echo the highlighting - api.nvim_command('echohl ' .. self.indicator.hl) + vim.api.nvim_command('echohl ' .. self.indicator.hl) -- set the user input variable if self._completions then userInput = - fn['libmodal#_inputWith'](self.indicator.str, self._completions) + vim.fn['libmodal#_inputWith'](self.indicator.str, self._completions) else userInput = - fn.input(self.indicator) + vim.fn.input(self.indicator) end -- determine what to do with the input diff --git a/lua/libmodal/src/collections/Popup.lua b/lua/libmodal/src/collections/Popup.lua index 9490156..7533e2d 100644 --- a/lua/libmodal/src/collections/Popup.lua +++ b/lua/libmodal/src/collections/Popup.lua @@ -1,27 +1,14 @@ ---[[ - /* - * IMPORTS - */ ---]] - -local api = vim.api -local go = vim.go - ---[[ - /* - * MODULE - */ ---]] +--[[/* MODULE */]] local Popup = require('libmodal/src/classes').new( 'libmodal-popup', {config = { anchor = 'SW', - col = go.columns - 1, + col = vim.go.columns - 1, focusable = false, height = 1, relative = 'editor', - row = go.lines - go.cmdheight - 1, + row = vim.go.lines - vim.go.cmdheight - 1, style = 'minimal', width = 1 }} @@ -40,7 +27,7 @@ local Popup = require('libmodal/src/classes').new( ]] ---------------------------- local function valid(window) - return window and api.nvim_win_is_valid(window) + return window and vim.api.nvim_win_is_valid(window) end --[[ @@ -62,7 +49,7 @@ local _metaPopup = require('libmodal/src/classes').new(Popup.TYPE) ------------------------------------- function _metaPopup:close(keepBuffer) if valid(self.window) then - api.nvim_win_close(self.window, false) + vim.api.nvim_win_close(self.window, false) end self.window = nil @@ -83,11 +70,11 @@ function _metaPopup:open(config) if not config then config = Popup.config end if valid(self.window) then - config = vim.tbl_extend('keep', config, api.nvim_win_get_config(self.window)) + config = vim.tbl_extend('keep', config, vim.api.nvim_win_get_config(self.window)) self:close(true) end - self.window = api.nvim_open_win(self.buffer, false, config) + self.window = vim.api.nvim_open_win(self.buffer, false, config) end --------------------------------------- @@ -114,22 +101,18 @@ function _metaPopup:refresh(inputBytes) self._inputChars = chars end - api.nvim_buf_set_lines(self.buffer, 0, 1, true, { + vim.api.nvim_buf_set_lines(self.buffer, 0, 1, true, { table.concat(self._inputChars) }) - if not valid(self.window) or api.nvim_win_get_tabpage(self.window) ~= api.nvim_get_current_tabpage() then + if not valid(self.window) or vim.api.nvim_win_get_tabpage(self.window) ~= vim.api.nvim_get_current_tabpage() then self:open() end - api.nvim_win_set_width(self.window, #self._inputChars) + vim.api.nvim_win_set_width(self.window, #self._inputChars) end ---[[ - /* - * CLASS `Popup` - */ ---]] +--[[/* CLASS `Popup` */]] -------------------- --[[ SUMMARY: @@ -140,11 +123,9 @@ end ]] -------------------- function Popup.new(config) - local buf = api.nvim_create_buf(false, true) - local self = setmetatable( { - buffer = buf, + buffer = vim.api.nvim_create_buf(false, true), _inputChars = {}, }, _metaPopup @@ -155,10 +136,6 @@ function Popup.new(config) return self end ---[[ - /* - * PUBLICIZE `Popup` - */ ---]] +--[[/* PUBLICIZE `Popup` */]] return Popup diff --git a/lua/libmodal/src/utils/WindowState.lua b/lua/libmodal/src/utils/WindowState.lua index e8bccb8..27ceb48 100644 --- a/lua/libmodal/src/utils/WindowState.lua +++ b/lua/libmodal/src/utils/WindowState.lua @@ -1,6 +1,5 @@ --[[/* IMPORTS */]] -local go = vim.go local api = require('libmodal/src/utils/api') --[[ @@ -25,8 +24,8 @@ local _metaWindowState = require('libmodal/src/classes').new(WindowState.TYPE) ]] ----------------------------------- function _metaWindowState:restore() - go.winheight = self.height - go.winwidth = self.width + vim.go.winheight = self.height + vim.go.winwidth = self.width api.nvim_redraw() end @@ -47,8 +46,8 @@ end function WindowState.new() return setmetatable( { - height = go.winheight, - width = go.winwidth, + height = vim.go.winheight, + width = vim.go.winwidth, }, _metaWindowState ) diff --git a/lua/libmodal/src/utils/api.lua b/lua/libmodal/src/utils/api.lua index a0c8c52..0c43370 100644 --- a/lua/libmodal/src/utils/api.lua +++ b/lua/libmodal/src/utils/api.lua @@ -1,9 +1,7 @@ --[[/* IMPORTS */]] -local fn = vim.fn local globals = require('libmodal/src/globals') local HighlightSegment = require('libmodal/src/Indicator/HighlightSegment') -local vim_api = vim.api --[[/* MODULE */]] @@ -19,17 +17,17 @@ function api.mode_exit(exit_char) end -- Exit the prompt by sending an escape key. - vim_api.nvim_feedkeys(exit_char, 'nt', false) + vim.api.nvim_feedkeys(exit_char, 'nt', false) end --- Make vim ring the visual/audio bell, if it is enabled. function api.nvim_bell() - vim_api.nvim_command('normal '..string.char(27)) -- escape char + vim.api.nvim_command('normal '..string.char(27)) -- escape char end --- Gets one character of user input, as a number. function api.nvim_input() - return fn.getchar() + return vim.fn.getchar() end -------------------------- @@ -39,7 +37,7 @@ end ]] -------------------------- function api.nvim_redraw() - vim_api.nvim_command 'mode' + vim.api.nvim_command 'mode' end --------------------------------- @@ -65,9 +63,9 @@ function api.nvim_lecho(hlTables) lecho_template[2] = tostring(hlTable.hl) lecho_template[4] = tostring(hlTable.str) - vim_api.nvim_command(table.concat(lecho_template)) + vim.api.nvim_command(table.concat(lecho_template)) end - vim_api.nvim_command 'echohl None' + vim.api.nvim_command 'echohl None' end -------------------------------------- @@ -84,7 +82,7 @@ function api.nvim_show_err(title, msg) HighlightSegment.new('Title', tostring(title)..'\n'), HighlightSegment.new('Error', tostring(msg)), }) - fn.getchar() + vim.fn.getchar() end return api diff --git a/lua/libmodal/src/utils/init.lua b/lua/libmodal/src/utils/init.lua index 8e39b2e..9a6e44f 100644 --- a/lua/libmodal/src/utils/init.lua +++ b/lua/libmodal/src/utils/init.lua @@ -1,7 +1,3 @@ ---[[/* IMPORTS */]] - -local v = vim.v - --[[/* MODULE */]] local utils = {} @@ -9,11 +5,7 @@ utils.api = require('libmodal/src/utils/api') utils.Help = require('libmodal/src/utils/Help') utils.WindowState = require('libmodal/src/utils/WindowState') ---[[ - /* - * FUNCTIONS - */ ---]] +--[[/* FUNCTIONS */]] --- Show an error from `pcall()`. --- @param pcall_err string the error generated by `pcall()`. @@ -22,14 +14,10 @@ function utils.show_error(pcall_err) utils.api.nvim_show_err( require('libmodal/src/globals').DEFAULT_ERROR_TITLE, - v.throwpoint..'\n'..v.exception..'\n'..pcall_err + vim.v.throwpoint..'\n'..vim.v.exception..'\n'..pcall_err ) end ---[[ - /* - * PUBLICIZE MODULE - */ ---]] +--[[/* PUBLICIZE MODULE */]] return utils diff --git a/plugin/libmodal.lua b/plugin/libmodal.lua index a1a8153..1f1b9e3 100644 --- a/plugin/libmodal.lua +++ b/plugin/libmodal.lua @@ -1,9 +1,7 @@ -local g = vim.g +if vim.g.loaded_libmodal then return end +vim.g.loaded_libmodal = true -if g.loaded_libmodal then return end -g.loaded_libmodal = true - -g.libmodalTimeouts = g.libmodalTimeouts or vim.go.timeout +vim.g.libmodalTimeouts = vim.g.libmodalTimeouts or vim.go.timeout -- The default highlight groups (for colors) are specified below. -- Change these default colors by defining or linking the corresponding highlight group. From 9e8005a4febd2a4f9fbbfea7bb8924a1b71c9d8a Mon Sep 17 00:00:00 2001 From: Iron-E Date: Mon, 13 Sep 2021 18:26:00 -0400 Subject: [PATCH 6/7] docs(README): add `feline.nvim` config --- README.md | 83 +------------------------------------------------------ 1 file changed, 1 insertion(+), 82 deletions(-) diff --git a/README.md b/README.md index 8eec55a..0bd3b53 100644 --- a/README.md +++ b/README.md @@ -37,85 +37,4 @@ See [vim-libmodal][libmodal] and the [docs](./doc) for more information. ## Statusline -You can add `libmodal` modes to your status line by using [galaxyline.nvim](https://github.com/glepnir/galaxyline.nvim.git). Here is an example configuration: - -```lua -local _COLORS = -{ - black = {'#202020', 0, 'black'}, - red = {'#ee4a59', 196, 'red'}, - orange = {'#ff8900', 208, 'darkyellow'}, - yellow = {'#f0df33', 220, 'yellow'}, - green = {'#77ff00', 72, 'green'}, - blue = {'#7090ff', 63, 'darkblue'}, - purple = {'#cf55f0', 129, 'magenta'}, -} - --- Statusline color -_COLORS.bar = {middle=_COLORS.gray_dark, side=_COLORS.black} - --- Text color -_COLORS.text = _COLORS.gray_light - --- Table which gets hex values from _COLORS. -local _HEX_COLORS = setmetatable( - {bar = setmetatable({}, {__index = function(_, key) return _COLORS.bar[key] and _COLORS.bar[key][1] or nil end})}, - {__index = function(_, key) local color = _COLORS[key] return color and color[1] or nil end} -) - -local _MODES = -{ - c = {'COMMAND-LINE', _COLORS.red}, - ce = {'NORMAL EX', _COLORS.red}, - cv = {'EX', _COLORS.red}, - i = {'INSERT', _COLORS.green}, - ic = {'INS-COMPLETE', _COLORS.green}, - n = {'NORMAL', _COLORS.purple}, - no = {'OPERATOR-PENDING', _COLORS.purple}, - r = {'HIT-ENTER', _COLORS.blue}, - ['r?'] = {':CONFIRM', _COLORS.blue}, - rm = {'--MORE', _COLORS.blue}, - R = {'REPLACE', _COLORS.red}, - Rv = {'VIRTUAL', _COLORS.red}, - s = {'SELECT', _COLORS.blue}, - S = {'SELECT', _COLORS.blue}, - t = {'TERMINAL', _COLORS.orange}, - v = {'VISUAL', _COLORS.blue}, - V = {'VISUAL LINE', _COLORS.blue}, - ['!'] = {'SHELL', _COLORS.yellow}, - - -- libmodal - TABS = _COLORS.tan, - BUFFERS = _COLORS.teal, - TABLES = _COLORS.orange_light, -} - -require('galaxyline').section.left = -{ - {ViMode = { - provider = function() -- auto change color according the vim mode - local mode_color = nil - local mode_name = nil - - if vim.g.libmodalActiveModeName then - mode_name = vim.g.libmodalActiveModeName - mode_color = _MODES[mode_name] - else - local current_mode = _MODES[vim.fn.mode(true)] - - mode_name = current_mode[1] - mode_color = current_mode[2] - end - - -- If you have Iron-E/nvim-highlite, use this step. - -- If not, just manually highlight it with vim.cmd() - require('highlite').highlight('GalaxyViMode', {fg=mode_color, style='bold'}) - - return '▊ '..mode_name..' ' - end, - highlight = {_HEX_COLORS.bar.side, _HEX_COLORS.bar.side}, - separator = _SEPARATORS.right, - separator_highlight = {_HEX_COLORS.bar.side, get_file_icon_color} - }} -} -``` +You can add `libmodal` modes to your status line by using [`feline.nvim`](https://github.com/famiu/feline.nvim) or [`galaxyline.nvim`](https://github.com/glepnir/galaxyline.nvim) or . You can find my configuration for `feline.nvim` [here](https://gitlab.com/Iron_E/dotfiles/-/blob/master/.config/nvim/lua/plugin/feline.lua#L130-156) and `galaxyline.nvim` [here](https://gitlab.com/Iron_E/dotfiles/-/blob/edf3e1c9779bbc81002832bb03ec875dc86cc16b/.config/nvim/lua/plugin/galaxyline.lua#L140-163)— both of which leverage `nvim-libmodal`'s in the statusbar. From 73d0e234bed56ae1d27d36ab5e21a8bab98bb4d6 Mon Sep 17 00:00:00 2001 From: Iron-E Date: Mon, 13 Sep 2021 22:04:18 -0400 Subject: [PATCH 7/7] docs(README): update link to feline config --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0bd3b53..d7a9b58 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,4 @@ See [vim-libmodal][libmodal] and the [docs](./doc) for more information. ## Statusline -You can add `libmodal` modes to your status line by using [`feline.nvim`](https://github.com/famiu/feline.nvim) or [`galaxyline.nvim`](https://github.com/glepnir/galaxyline.nvim) or . You can find my configuration for `feline.nvim` [here](https://gitlab.com/Iron_E/dotfiles/-/blob/master/.config/nvim/lua/plugin/feline.lua#L130-156) and `galaxyline.nvim` [here](https://gitlab.com/Iron_E/dotfiles/-/blob/edf3e1c9779bbc81002832bb03ec875dc86cc16b/.config/nvim/lua/plugin/galaxyline.lua#L140-163)— both of which leverage `nvim-libmodal`'s in the statusbar. +You can add `libmodal` modes to your status line by using [`feline.nvim`](https://github.com/famiu/feline.nvim) or [`galaxyline.nvim`](https://github.com/glepnir/galaxyline.nvim) or . You can find my configuration for `feline.nvim` [here](https://gitlab.com/Iron_E/dotfiles/-/blob/master/.config/nvim/lua/plugin/feline.lua#L129-155) and `galaxyline.nvim` [here](https://gitlab.com/Iron_E/dotfiles/-/blob/edf3e1c9779bbc81002832bb03ec875dc86cc16b/.config/nvim/lua/plugin/galaxyline.lua#L140-163)— both of which leverage `nvim-libmodal`'s in the statusbar.