From 6ae4edea4a4394f87d2d1f1182747b6f93da8617 Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Tue, 18 Jan 2022 10:29:49 +0100 Subject: [PATCH 01/13] Remove unused functions --- lua/nvimux/bindings.lua | 46 ++--------------------------------------- lua/nvimux/fns.lua | 7 ------- 2 files changed, 2 insertions(+), 51 deletions(-) diff --git a/lua/nvimux/bindings.lua b/lua/nvimux/bindings.lua index 978b25c..c40522f 100644 --- a/lua/nvimux/bindings.lua +++ b/lua/nvimux/bindings.lua @@ -7,50 +7,6 @@ local consts = { esc = '', } -fns.nvim_do_bind = function(options) - local escape_prefix = options.escape_prefix or '' - local mode = options.mode - return function(cfg) - local suffix = cfg.suffix - local prefix = vars.local_prefix[mode] or vars.prefix - if suffix == nil then - suffix = string.sub(cfg.mapping, 1, 1) == ':' and '' or '' - end - vim.cmd(mode .. 'noremap ' .. prefix .. cfg.key .. ' ' .. escape_prefix .. cfg.mapping .. suffix) - end -end - -fns.bind = { - t = fns.nvim_do_bind{mode = 't', escape_prefix = consts.terminal_quit}, - i = fns.nvim_do_bind{mode = 'i', escape_prefix = consts.esc}, - n = fns.nvim_do_bind{mode = 'n'}, - v = fns.nvim_do_bind{mode = 'v'} -} - -fns.bind_all_modes = function(options) - for _, mode in ipairs(options.modes) do - fns.bind[mode](options) - end -end - -bindings.bind = function(options) - fns.bind_all_modes(options) -end - -bindings.create_binding = function(modes, command) - local tbl = {} - tbl[table.concat(modes, "")] = { command } - return tbl - -end - -bindings.bind_all = function(options) - for _, bind in ipairs(options) do - local key, cmd, modes = unpack(bind) - bindings.mappings[key] = bindings.create_binding(modes, cmd) - end -end - if vim.keymap ~= nil then bindings.set_keymap = vim.keymap.set else @@ -78,6 +34,8 @@ end bindings.keymap = function(binding, context) local options = {silent = true} + print(vim.inspect(binding)) + if (type(binding[3]) == "function") then bindings.set_keymap(binding[1], context.prefix .. binding[2], binding[3], options) elseif (type(binding[3]) == "string") then diff --git a/lua/nvimux/fns.lua b/lua/nvimux/fns.lua index ed878e7..3aade37 100644 --- a/lua/nvimux/fns.lua +++ b/lua/nvimux/fns.lua @@ -1,12 +1,5 @@ local fns = {} -fns.prompt = function(message) - vim.fn.inputsave() - local ret = vim.fn.input(message) - vim.fn.inputrestore() - return ret -end - fns.split = function(str) local p = {} for i=1, #str do From 8d123c8769d25e19029ef6bd6851920d284e7ad2 Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Tue, 18 Jan 2022 10:33:06 +0100 Subject: [PATCH 02/13] Remove unused functions --- lua/nvimux/init.lua | 89 --------------------------------------------- 1 file changed, 89 deletions(-) diff --git a/lua/nvimux/init.lua b/lua/nvimux/init.lua index e54d44a..adbe542 100644 --- a/lua/nvimux/init.lua +++ b/lua/nvimux/init.lua @@ -5,69 +5,18 @@ -- @module nvimux -- luacheck: globals unpack -local __dep_warn = true -local deprecated = function(msg) - if __dep_warn then - print(msg) - __dep_warn = false - end -end - local nvimux = {} local bindings = require('nvimux.bindings') local vars = require('nvimux.vars') local fns = require('nvimux.fns') -nvimux.debug = {} nvimux.bindings = bindings nvimux.config = {} nvimux.term = {} -nvimux.term.prompt = {} nvimux.commands = {} --- [ Private variables and tables -local nvim_proxy = { - __index = function(_, key) - deprecated("Don't use the proxy to vim vars. It will be removed in the next version") - local key_ = 'nvimux_' .. key - local val = nil - if vim.fn.exists(key_) == 1 then - val = vim.api.nvim_get_var(key_) - end - return val - end -} - --- [[ Table of default bindings --- Deprecated -bindings.mappings = { - [''] = { nvi = {':so $MYVIMRC'}}, - ['!'] = { nvit = {':wincmd T'}}, - ['%'] = { nvit = {function() return vars.vertical_split end}}, - ['\"'] = { nvit = {function() return vars.horizontal_split end}}, - ['-'] = { nvit = {':NvimuxPreviousTab'}}, - ['q'] = { nvit = {':NvimuxToggleTerm'}}, - ['w'] = { nvit = {':tabs'}}, - ['o'] = { nvit = {'w'}}, - ['n'] = { nvit = {'gt'}}, - ['p'] = { nvit = {'gT'}}, - ['x'] = { nvi = {':bd %'}, - t = {function() return vars.close_term end}}, - ['X'] = { nvi = {':enew \\| bd #'}}, - ['h'] = { nvit = {''}}, - ['j'] = { nvit = {''}}, - ['k'] = { nvit = {''}}, - ['l'] = { nvit = {''}}, - [':'] = { t = {':', suffix = ''}}, - ['['] = { t = {''}}, - [']'] = { t = {':NvimuxTermPaste'}}, - [','] = { t = {'', nvimux.term.prompt.rename}}, - ['c'] = { nvit = {':NvimuxNewTab'}}, -} - bindings.map_table = {} --- Deprecated local win_cmd = function(create_window) local select_buffer vim.cmd(create_window) @@ -81,10 +30,8 @@ local win_cmd = function(create_window) end select_buffer() - end --- Deprecated local tab_cmd = function(create_window) local select_buffer vim.cmd(create_window) @@ -106,7 +53,6 @@ nvimux.commands.horizontal_split = function() return win_cmd[[spl|wincmd j]] end nvimux.commands.vertical_split = function() return win_cmd[[vspl|wincmd l]] end nvimux.commands.new_tab = function() return tab_cmd[[tabe]] end --- Deprecated local nvimux_commands = { {name = 'NvimuxPreviousTab', cmd = [[lua require('nvimux').go_to_last_tab()]]}, {name = 'NvimuxSet', cmd = [[lua require('nvimux').config.set_fargs()]], nargs='+'}, @@ -166,8 +112,6 @@ setmetatable(vars, nvim_proxy) -- ] --- ] - nvimux.do_autocmd = function(commands) local au = {"augroup nvimux"} for _, v in ipairs(commands) do @@ -204,39 +148,6 @@ nvimux.term.new_toggle = function() vim.cmd(split_type .. ' | enew | ' .. nvimux.context.quickterm.command) local buf_nr = vim.fn.bufnr('%') vim.wo.wfw = true - vim.b[buf_nr].nvimux_buf_orientation = split_type - vim[nvimux.context.quickterm.scope].nvimux_last_buffer_id = buf_nr -end - --- TODO port -nvimux.term.toggle = function() - -- TODO Allow external commands - local buf_nr = vim.g.nvimux_last_buffer_id - - if not buf_nr then - nvimux.term.new_toggle() - else - local id = math.floor(buf_nr) - local window = vim.fn.bufwinnr(id) - - if window == -1 then - if vim.fn.bufname(id) == '' then - nvimux.term.new_toggle() - else - local split_type = vim.b[buf_nr].nvimux_buf_orientation - vim.cmd(split_type .. ' | b' .. id) - end - else - vim.cmd(window .. ' wincmd w | q | stopinsert') - end - end -end - -nvimux.term.prompt.rename = function() - nvimux.term_only{ - cmd = fns.prompt('nvimux > New term name: '), - action = function(k) vim.api.nvim_command('file term://' .. k) end - } end -- ]] From 0bf2b88e494519c74866c7b185f27d61e07528b5 Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Tue, 18 Jan 2022 10:33:24 +0100 Subject: [PATCH 03/13] Improve mappings quality --- lua/nvimux/init.lua | 185 +++++++++++++++++--------------------------- 1 file changed, 70 insertions(+), 115 deletions(-) diff --git a/lua/nvimux/init.lua b/lua/nvimux/init.lua index adbe542..7c01bd0 100644 --- a/lua/nvimux/init.lua +++ b/lua/nvimux/init.lua @@ -53,9 +53,77 @@ nvimux.commands.horizontal_split = function() return win_cmd[[spl|wincmd j]] end nvimux.commands.vertical_split = function() return win_cmd[[vspl|wincmd l]] end nvimux.commands.new_tab = function() return tab_cmd[[tabe]] end +-- [[ Quickterm +-- TODO port +nvimux.term.new_toggle = function() + local split_type = nvimux.context.quickterm:split_type() + vim.cmd(split_type .. ' | enew | ' .. nvimux.context.quickterm.command) + local buf_nr = vim.fn.bufnr('%') + vim.wo.wfw = true + vim.b[buf_nr].nvimux_buf_orientation = split_type + vim[nvimux.context.quickterm.scope].nvimux_last_buffer_id = buf_nr +end + +-- TODO port +nvimux.term.toggle = function() + -- TODO Allow external commands + local buf_nr = vim.g.nvimux_last_buffer_id + + if not buf_nr then + nvimux.term.new_toggle() + else + local id = math.floor(buf_nr) + local window = vim.fn.bufwinnr(id) + + if window == -1 then + if vim.fn.bufname(id) == '' then + nvimux.term.new_toggle() + else + local split_type = vim.b[buf_nr].nvimux_buf_orientation + vim.cmd(split_type .. ' | b' .. id) + end + else + vim.cmd(window .. ' wincmd w | q | stopinsert') + end + end +end + +nvimux.term.rename = function() + vim.ui.input( + {prompt = 'nvimux > New term name: '}, + function(name) + nvimux.term_only{ + cmd = name, + action = function(k) vim.api.nvim_command('file term://' .. k) end + } + end) +end + +nvimux.term_only = function(options) + local action = options.action or vim.api.nvim_command + if vim.bo.buftype == 'terminal' then + action(options.cmd) + else + print("Not on terminal") + end +end + +nvimux.set_last_tab = function(tabn) + if tabn == nil then + tabn = vim.fn.tabpagenr() + end + + nvimux.context.state.last_tab = tabn +end + +nvimux.go_to_last_tab = function() + vim.cmd((nvimux.context.state.last_tab or 1) .. 'tabn') +end + +-- ]] + local nvimux_commands = { {name = 'NvimuxPreviousTab', cmd = [[lua require('nvimux').go_to_last_tab()]]}, - {name = 'NvimuxSet', cmd = [[lua require('nvimux').config.set_fargs()]], nargs='+'}, } local autocmds = { @@ -90,7 +158,7 @@ local mappings = { {{'t'}, ':', ':', suffix = ''}, {{'t'}, '[', ''}, {{'t'}, ']', function() nvimux.term_only{cmd = 'normal pa'} end}, - {{'t'}, ',', nvimux.term.prompt.rename}, + {{'t', 'n'}, ',', nvimux.term.rename}, -- Tab management {{'n', 'v', 'i', 't'}, 'c', nvimux.commands.new_tab}, @@ -121,119 +189,6 @@ nvimux.do_autocmd = function(commands) vim.fn.execute(au) end --- [ Public API --- [[ Config-handling commands -nvimux.config.set = function(options) - deprecated("nvimux.config.set is deprecated. Use nvimux.setup") - vars[options.key] = options.value -end - -nvimux.config.set_fargs = function(key, value) - deprecated("nvimux.config.set_fargs is deprecated. Use nvimux.setup") - nvimux.config.set{key=key, value=value} -end - -nvimux.config.set_all = function(options) - deprecated("nvimux.config.set_all is deprecated. Use nvimux.setup") - for key, value in pairs(options) do - nvimux.config.set{['key'] = key, ['value'] = value} - end -end --- ]] - --- [[ Quickterm --- TODO port -nvimux.term.new_toggle = function() - local split_type = nvimux.context.quickterm:split_type() - vim.cmd(split_type .. ' | enew | ' .. nvimux.context.quickterm.command) - local buf_nr = vim.fn.bufnr('%') - vim.wo.wfw = true -end --- ]] - --- [[ Bindings --- ]] - --- [[ Top-level commands -nvimux.debug.context = function() - print(vim.inspect(nvimux.context)) -end - -nvimux.debug.bindings = function() - print(vim.inspect(nvimux.context.bindings)) -end - -nvimux.debug.state = function() - print(vim.inspect(nvimux.context.state)) -end - -nvimux.term_only = function(options) - local action = options.action or vim.api.nvim_command - if vim.bo.buftype == 'terminal' then - action(options.cmd) - else - print("Not on terminal") - end -end - --- deprecated -nvimux.mapped = function(options) - local mapping = bindings.map_table[options.key] - local ret = mapping.arg() - if ret ~= '' and ret ~= nil then - vim.cmd(ret) - end -end - -nvimux.set_last_tab = function(tabn) - if tabn == nil then - tabn = vim.fn.tabpagenr() - end - - nvimux.context.state.last_tab = tabn -end - -nvimux.go_to_last_tab = function() - vim.cmd((nvimux.context.state.last_tab or 1) .. 'tabn') -end - - -- ]] --- ] - - -nvimux.bootstrap = function(force) - deprecated("nvimux.bootstrap is deprecated. Use nvimux.setup") - if force or nvimux.loaded == nil then - for i=1, 9 do - bindings.mappings[i] = bindings.create_binding({"n", "v", "i", "t"} , i .. 'gt') - end - - for _, cmd in ipairs(nvimux_commands) do - fns.build_cmd(cmd) - end - - for key, cmd in pairs(bindings.mappings) do - for modes, binds in pairs(cmd) do - modes = fns.split(modes) - local arg = table.remove(binds, 1) - binds.key = key - binds.modes = modes - if type(arg) == 'function' then - bindings.map_table[key] = {['arg'] = arg, ['action'] = nil} - binds.mapping = ":lua require('nvimux').mapped{key = '" .. key .. "'}" - else - binds.mapping = arg - end - bindings.bind(binds) - end - end - fns.build_cmd{name = 'NvimuxReload', cmd = 'lua require("nvimux").bootstrap(true)'} - nvimux.do_autocmd(autocmds) - nvimux.loaded = true - end -end --- ] - --- Configure nvimux to start with the supplied arguments -- It can be configured to use the defaults by only supplying an empty table. -- This function must be called to initalize nvimux. From 0b80cdd9a2a473f02e16f8441e34b1b10f04b9f0 Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Tue, 18 Jan 2022 11:29:45 +0100 Subject: [PATCH 04/13] Special case bindings only on non rhs This allows to make use of --- lua/nvimux/bindings.lua | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lua/nvimux/bindings.lua b/lua/nvimux/bindings.lua index c40522f..456b2bb 100644 --- a/lua/nvimux/bindings.lua +++ b/lua/nvimux/bindings.lua @@ -4,7 +4,6 @@ local fns = {} local consts = { terminal_quit = '', - esc = '', } if vim.keymap ~= nil then @@ -40,26 +39,29 @@ bindings.keymap = function(binding, context) bindings.set_keymap(binding[1], context.prefix .. binding[2], binding[3], options) elseif (type(binding[3]) == "string") then local suffix = '' + local starts_with_colon = string.sub(binding[3], 1, 1) == ':' if binding.suffix == nil then -- TODO revisit - suffix = string.sub(binding[3], 1, 1) == ':' and '' or '' + suffix = starts_with_colon and '' or '' else suffix = binding.suffix end - if vim.tbl_contains(binding[1], 't') then - binding[1] = vim.tbl_filter(function(mode) return mode ~= 't' end, binding[1]) - bindings.set_keymap('t', - context.prefix .. binding[2], - consts.terminal_quit .. binding[3] .. suffix, - options) - elseif vim.tbl_contains(binding[1], 'i') then - binding[1] = vim.tbl_filter(function(mode) return mode ~= 'i' end, binding[1]) - bindings.set_keymap('i', - context.prefix .. binding[2], - consts.esc .. binding[3] .. suffix, - options) + if starts_with_colon then + if vim.tbl_contains(binding[1], 't') then + binding[1] = vim.tbl_filter(function(mode) return mode ~= 't' end, binding[1]) + bindings.set_keymap('t', + context.prefix .. binding[2], + consts.terminal_quit .. binding[3] .. suffix, + options) + elseif vim.tbl_contains(binding[1], 'i') then + binding[1] = vim.tbl_filter(function(mode) return mode ~= 'i' end, binding[1]) + bindings.set_keymap('i', + context.prefix .. binding[2], + consts.esc .. binding[3] .. suffix, + options) + end end bindings.set_keymap(binding[1], context.prefix .. binding[2], binding[3] .. suffix, options) From ec52f9b0e3f2229cc09f0e7c98df3598d04e510d Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Tue, 18 Jan 2022 11:30:24 +0100 Subject: [PATCH 05/13] Migrate commands to This allows to skip autocmds when changing mode + is less intrusive on the user --- lua/nvimux/init.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lua/nvimux/init.lua b/lua/nvimux/init.lua index 7c01bd0..8aeb5b8 100644 --- a/lua/nvimux/init.lua +++ b/lua/nvimux/init.lua @@ -132,21 +132,21 @@ local autocmds = { local mappings = { -- Reload global configs - {{'n', 'v', 'i'}, '', ':so $MYVIMRC'}, + {{'n', 'v', 'i'}, '', 'source $MYVIMRC'}, -- Window management - {{'n', 'v', 'i', 't'}, '!', ':wincmd T'}, + {{'n', 'v', 'i', 't'}, '!', 'wincmd T'}, {{'n', 'v', 'i', 't'}, '%', nvimux.commands.vertical_split}, {{'n', 'v', 'i', 't'}, '\"', nvimux.commands.horizontal_split}, {{'n', 'v', 'i', 't'}, '-', nvimux.go_to_last_tab}, {{'n', 'v', 'i', 't'}, 'q', nvimux.term.toggle }, - {{'n', 'v', 'i', 't'}, 'w', ':tabs'}, + {{'n', 'v', 'i', 't'}, 'w', 'tabs'}, {{'n', 'v', 'i', 't'}, 'o', 'w'}, {{'n', 'v', 'i', 't'}, 'n', 'gt'}, {{'n', 'v', 'i', 't'}, 'p', 'gT'}, - {{'n', 'v', 'i'}, 'x', ':bd %'}, + {{'n', 'v', 'i'}, 'x', 'bdelete %'}, {{'t'}, 'x', function() vim.api.nvim_buf_delete(0, {force = true}) end}, - {{'n', 'v', 'i'}, 'X', ':enew \\| bd #'}, + {{'n', 'v', 'i'}, 'X', 'enew \\| bd #'}, -- Moving around {{'n', 'v', 'i', 't'}, 'h', ''}, @@ -156,8 +156,8 @@ local mappings = { -- Term facilities {{'t'}, ':', ':', suffix = ''}, - {{'t'}, '[', ''}, - {{'t'}, ']', function() nvimux.term_only{cmd = 'normal pa'} end}, + {{'t'}, '[', ''}, + {{'t'}, ']', function() vim.paste(vim.fn.getreg('"', 1, true), -1) end }, {{'t', 'n'}, ',', nvimux.term.rename}, -- Tab management From c9031b96e43d5ebde20be3a97a699ed5b33ad113 Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Tue, 18 Jan 2022 15:36:22 +0100 Subject: [PATCH 06/13] Remove print --- lua/nvimux/bindings.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/lua/nvimux/bindings.lua b/lua/nvimux/bindings.lua index 456b2bb..d35b5c9 100644 --- a/lua/nvimux/bindings.lua +++ b/lua/nvimux/bindings.lua @@ -33,8 +33,6 @@ end bindings.keymap = function(binding, context) local options = {silent = true} - print(vim.inspect(binding)) - if (type(binding[3]) == "function") then bindings.set_keymap(binding[1], context.prefix .. binding[2], binding[3], options) elseif (type(binding[3]) == "string") then From 9747d70aaa46cc1ba9ee41fcecfca0b39a8f0946 Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Tue, 18 Jan 2022 15:38:05 +0100 Subject: [PATCH 07/13] Define commands from lua MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This should be better by avoiding swapping back-and-forth between lua and viml. Those lua commands are usually simple façades to lua functions and defined only as convenience mappings anyways. --- lua/nvimux/fns.lua | 10 ++++++++++ lua/nvimux/init.lua | 24 ++++++++++++++++++------ plugin/nvimux.vim | 8 -------- 3 files changed, 28 insertions(+), 14 deletions(-) delete mode 100644 plugin/nvimux.vim diff --git a/lua/nvimux/fns.lua b/lua/nvimux/fns.lua index 3aade37..9c9c395 100644 --- a/lua/nvimux/fns.lua +++ b/lua/nvimux/fns.lua @@ -15,4 +15,14 @@ fns.build_cmd = function(options) vim.cmd('command! -nargs=' .. nargs .. ' ' .. options.name .. ' ' .. cmd) end + +local function capitalize_first(s) + return string.upper(string.sub(s,1,1))..string.sub(s,2) +end +local function to_pascal(s) + return (string.gsub(capitalize_first(s),"_(%w+)",capitalize_first)) +end + +fns.snake_to_pascal = to_pascal + return fns diff --git a/lua/nvimux/init.lua b/lua/nvimux/init.lua index 8aeb5b8..ffb6e9b 100644 --- a/lua/nvimux/init.lua +++ b/lua/nvimux/init.lua @@ -13,7 +13,16 @@ local fns = require('nvimux.fns') nvimux.bindings = bindings nvimux.config = {} nvimux.term = {} -nvimux.commands = {} +nvimux.commands = setmetatable({},{ + +__newindex = function(tbl, key, value) + vim.api.nvim_add_user_command("Nvimux"..fns.snake_to_pascal(key), + function(opts) + value(opts.args, opts) + end, {}) + rawset(tbl, key, value) + end +}) bindings.map_table = {} @@ -49,9 +58,16 @@ local tab_cmd = function(create_window) select_buffer() end +-- [[ Commands +-- Commands defined in `nvimux.commands` will be automatically converted to nvim's command nvimux.commands.horizontal_split = function() return win_cmd[[spl|wincmd j]] end nvimux.commands.vertical_split = function() return win_cmd[[vspl|wincmd l]] end nvimux.commands.new_tab = function() return tab_cmd[[tabe]] end +nvimux.commands.previous_tab = nvimux.go_to_last_tab +nvimux.commands.term_paste = function(reg) vim.paste(vim.fn.getreg(reg or '"', 1, true), -1) end +nvimux.commands.toggle_term = nvimux.term.toggle +nvimux.commands.term_rename = nvimux.term.rename +-- ]] -- [[ Quickterm -- TODO port @@ -122,10 +138,6 @@ end -- ]] -local nvimux_commands = { - {name = 'NvimuxPreviousTab', cmd = [[lua require('nvimux').go_to_last_tab()]]}, -} - local autocmds = { {event = "TabLeave", target="*", cmd = [[lua require('nvimux').set_last_tab()]]}, } @@ -157,7 +169,7 @@ local mappings = { -- Term facilities {{'t'}, ':', ':', suffix = ''}, {{'t'}, '[', ''}, - {{'t'}, ']', function() vim.paste(vim.fn.getreg('"', 1, true), -1) end }, + {{'t'}, ']', nvimux.commands.term_paste }, {{'t', 'n'}, ',', nvimux.term.rename}, -- Tab management diff --git a/plugin/nvimux.vim b/plugin/nvimux.vim deleted file mode 100644 index 3f4c36a..0000000 --- a/plugin/nvimux.vim +++ /dev/null @@ -1,8 +0,0 @@ -" Commands -command! -nargs=0 NvimuxTermPaste lua require('nvimux').term_only{cmd = 'normal pa'} -command! -nargs=0 NvimuxToggleTerm lua require('nvimux').term.toggle() -command! -nargs=1 NvimuxTermRename lua require('nvimux').term_only{cmd = 'file term://'} - -command! -nargs=0 NvimuxHorizontalSplit lua require('nvimux').commands.horizontal_split() -command! -nargs=0 NvimuxVerticalSplit lua require('nvimux').commands.vertical_split() -command! -nargs=0 NvimuxNewTab lua require('nvimux').commands.new_tab() From 6d079a4475b1a9cb29f7281910e24a51ab327d51 Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Wed, 19 Jan 2022 10:28:06 +0100 Subject: [PATCH 08/13] Remove unused functions Also, simplify the structure and create a new wrapper --- lua/nvimux/fns.lua | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/lua/nvimux/fns.lua b/lua/nvimux/fns.lua index 9c9c395..1997dc0 100644 --- a/lua/nvimux/fns.lua +++ b/lua/nvimux/fns.lua @@ -1,28 +1,23 @@ local fns = {} -fns.split = function(str) - local p = {} - for i=1, #str do - table.insert(p, str:sub(i, i)) - end - return p +local function capitalize_first(s) + return string.upper(string.sub(s,1,1))..string.sub(s,2) end -fns.build_cmd = function(options) - local nargs = options.nargs or 0 - local cmd = options.cmd or options.lazy_cmd() - - vim.cmd('command! -nargs=' .. nargs .. ' ' .. options.name .. ' ' .. cmd) +fns.snake_to_pascal = function(s) + return (string.gsub(capitalize_first(s),"_(%w+)",capitalize_first)) end - -local function capitalize_first(s) - return string.upper(string.sub(s,1,1))..string.sub(s,2) -end -local function to_pascal(s) - return (string.gsub(capitalize_first(s),"_(%w+)",capitalize_first)) +fns.fn_or_command = function(cmd) + local tp = type(cmd) + if tp == "function" then + cmd() + elseif tp == "string" then + vim.cmd(cmd) + else + print("nvimux: Cannot run command of type " .. tp) + end end -fns.snake_to_pascal = to_pascal return fns From 9ab481273baf8da44561b967ab41a57defbf5171 Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Wed, 19 Jan 2022 10:28:47 +0100 Subject: [PATCH 09/13] Remove unused/deprecated configs Also, document a few of the properties --- lua/nvimux/vars.lua | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/lua/nvimux/vars.lua b/lua/nvimux/vars.lua index a9704b1..2f41434 100644 --- a/lua/nvimux/vars.lua +++ b/lua/nvimux/vars.lua @@ -22,13 +22,13 @@ local singleton_buf = function() end -vars.local_prefix = { - n = nil, - v = nil, - i = nil, - t = nil - } - +--- Quickterm configuration +-- @table quickterm +-- @field scope Scope of quickterm. Can be one of: 'b', 'w', 't', 'g' +-- @field direction nvim's window direction +-- @field orientation nvim's window orientation +-- @field size size in columns/rows depending on orientation +-- @field command nvim command or lua function for the quickterm vars.quickterm = { scope = 'g', direction = 'botright', @@ -38,30 +38,34 @@ vars.quickterm = { } vars.prefix = '' --- Deprecated -vars.vertical_split = ':NvimuxVerticalSplit' -vars.horizontal_split = ':NvimuxHorizontalSplit' - -vars.close_term = function() - vim.api.nvim_buf_delete(0, {force = true}) -end +--- Defines what is going to be displayed when a buffer is +-- required for a new window/tab. +-- By default, that content will be created once on a +-- non-listed scratch buffer. vars.scratch_buf_content = { "" } -vars.new_buffer = function() - return vim.api.nvim_create_buf(false, true) -end - +--- Prepares a new window +-- Can be used to display dashboards, TODO lists or as a hook to invoke +-- other functions (like telescope). +-- Defaults to setting a scratch buffer whose contents are defined by +-- @{\\vars.scratch_buf_content}. vars.new_window = function() vim.api.nvim_set_current_buf(singleton_buf()) end +--- Prepares a new tab +-- Can be used to display dashboards, TODO lists or as a hook to invoke +-- other functions (like telescope). +-- Defaults to setting a scratch buffer whose contents are defined by +-- @{\\vars.scratch_buf_content}. vars.new_tab = function() vim.api.nvim_set_current_buf(singleton_buf()) end +--- Prepares the command that will be executed to create a new quickterm vars.quickterm.split_type = function(t) return t.direction .. ' ' .. t.orientation .. ' ' .. t.size .. 'split' end From 3e1c234ea1b0798ca620d2c3a26534d2c247053c Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Wed, 19 Jan 2022 10:29:37 +0100 Subject: [PATCH 10/13] Simplify tab and window creation functions They were very similar and introduced an unnecessary indirection. This should be simpler now --- lua/nvimux/init.lua | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/lua/nvimux/init.lua b/lua/nvimux/init.lua index ffb6e9b..663f8a8 100644 --- a/lua/nvimux/init.lua +++ b/lua/nvimux/init.lua @@ -27,35 +27,13 @@ __newindex = function(tbl, key, value) bindings.map_table = {} local win_cmd = function(create_window) - local select_buffer - vim.cmd(create_window) - - if type(vars.new_window) == "function" then - select_buffer = vars.new_window - else - select_buffer = function() - vim.api.nvim_command(vars.new_window) - end - end - - select_buffer() + vim.cmd(create_window) + fns.fn_or_command(nvimux.context.new_window) end local tab_cmd = function(create_window) - local select_buffer vim.cmd(create_window) - - local selector = nvimux.context.new_tab or nvimux.context.new_window - - if type(selector) == "function" then - select_buffer = selector - else - select_buffer = function() - vim.api.nvim_command(selector) - end - end - - select_buffer() + fns.fn_or_command(nvimux.context.new_tab) end -- [[ Commands From f2f8f4e17e3ddd02e2095ca8eb8b3e5d344de484 Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Wed, 19 Jan 2022 10:30:23 +0100 Subject: [PATCH 11/13] Port quickterm functions to use newer nvim api Also, fixes a little bug in which context was not taken in account when reading the values --- lua/nvimux/init.lua | 69 +++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/lua/nvimux/init.lua b/lua/nvimux/init.lua index 663f8a8..ce885e2 100644 --- a/lua/nvimux/init.lua +++ b/lua/nvimux/init.lua @@ -47,37 +47,61 @@ nvimux.commands.toggle_term = nvimux.term.toggle nvimux.commands.term_rename = nvimux.term.rename -- ]] +-- [[ Top-level helper functions +nvimux.set_last_tab = function(tabn) + if tabn == nil then + tabn = vim.fn.tabpagenr() + end + + nvimux.context.state.last_tab = tabn +end + +nvimux.go_to_last_tab = function() + vim.cmd((nvimux.context.state.last_tab or 1) .. 'tabn') +end + + +nvimux.do_autocmd = function(commands) + local au = {"augroup nvimux"} + for _, v in ipairs(commands) do + table.insert(au, "au! " .. v.event .. " " .. v.target .. " " .. v.cmd) + end + table.insert(au, "augroup END") + vim.fn.execute(au) +end +-- ]] + + -- [[ Quickterm --- TODO port nvimux.term.new_toggle = function() local split_type = nvimux.context.quickterm:split_type() - vim.cmd(split_type .. ' | enew | ' .. nvimux.context.quickterm.command) - local buf_nr = vim.fn.bufnr('%') + vim.cmd(split_type) + fns.fn_or_command(nvimux.context.quickterm.command) + local buf_nr = vim.api.nvim_get_current_buf() vim.wo.wfw = true vim.b[buf_nr].nvimux_buf_orientation = split_type vim[nvimux.context.quickterm.scope].nvimux_last_buffer_id = buf_nr end --- TODO port nvimux.term.toggle = function() -- TODO Allow external commands - local buf_nr = vim.g.nvimux_last_buffer_id + local buf_nr = vim[nvimux.context.quickterm.scope].nvimux_last_buffer_id if not buf_nr then nvimux.term.new_toggle() else - local id = math.floor(buf_nr) - local window = vim.fn.bufwinnr(id) + local window = vim.fn.bufwinid(buf_nr) if window == -1 then - if vim.fn.bufname(id) == '' then - nvimux.term.new_toggle() - else + if vim.api.nvim_buf_is_loaded(buf_nr) then local split_type = vim.b[buf_nr].nvimux_buf_orientation - vim.cmd(split_type .. ' | b' .. id) + vim.cmd(split_type) + vim.api.nvim_win_set_buf(0, buf_nr) + else + nvimux.term.new_toggle() end else - vim.cmd(window .. ' wincmd w | q | stopinsert') + vim.api.nvim_win_hide(window) end end end @@ -102,18 +126,6 @@ nvimux.term_only = function(options) end end -nvimux.set_last_tab = function(tabn) - if tabn == nil then - tabn = vim.fn.tabpagenr() - end - - nvimux.context.state.last_tab = tabn -end - -nvimux.go_to_last_tab = function() - vim.cmd((nvimux.context.state.last_tab or 1) .. 'tabn') -end - -- ]] local autocmds = { @@ -170,15 +182,6 @@ setmetatable(vars, nvim_proxy) -- ] -nvimux.do_autocmd = function(commands) - local au = {"augroup nvimux"} - for _, v in ipairs(commands) do - table.insert(au, "au! " .. v.event .. " " .. v.target .. " " .. v.cmd) - end - table.insert(au, "augroup END") - vim.fn.execute(au) -end - --- Configure nvimux to start with the supplied arguments -- It can be configured to use the defaults by only supplying an empty table. -- This function must be called to initalize nvimux. From 3a53405ee15c90737b1fa3b49ad28f08425267a5 Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Wed, 19 Jan 2022 10:32:47 +0100 Subject: [PATCH 12/13] Clean up vars usage It should only be used when running the setup --- lua/nvimux/init.lua | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lua/nvimux/init.lua b/lua/nvimux/init.lua index ce885e2..084826d 100644 --- a/lua/nvimux/init.lua +++ b/lua/nvimux/init.lua @@ -7,7 +7,6 @@ local nvimux = {} local bindings = require('nvimux.bindings') -local vars = require('nvimux.vars') local fns = require('nvimux.fns') nvimux.bindings = bindings @@ -178,10 +177,6 @@ local mappings = { -- ]] -setmetatable(vars, nvim_proxy) - --- ] - --- Configure nvimux to start with the supplied arguments -- It can be configured to use the defaults by only supplying an empty table. -- This function must be called to initalize nvimux. @@ -192,10 +187,8 @@ setmetatable(vars, nvim_proxy) -- @tparam opts.autocmds table autocmds that belong to the same logical group than nvimux -- @see nvimux.vars for the defaults nvimux.setup = function(opts) - -- TODO Remove global vars, make it local to context only - vars = vim.tbl_deep_extend("force", vars or {}, opts.config or {}) + local context = vim.tbl_deep_extend("force", require('nvimux.vars'), opts.config or {}) - local context = vars context.bindings = mappings for _, b in ipairs(opts.bindings) do table.insert(context.bindings, b) From 8db147188c73cce3be048acd81d557ed0fe9d172 Mon Sep 17 00:00:00 2001 From: Henry Kupty Date: Wed, 19 Jan 2022 10:33:30 +0100 Subject: [PATCH 13/13] Remove `nvimux.bindings` proxy It should not be immediately available. If someone needs it, it can still be accessed by `require("nvimux.bindings")` --- lua/nvimux/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/nvimux/init.lua b/lua/nvimux/init.lua index 084826d..2000d94 100644 --- a/lua/nvimux/init.lua +++ b/lua/nvimux/init.lua @@ -9,7 +9,6 @@ local nvimux = {} local bindings = require('nvimux.bindings') local fns = require('nvimux.fns') -nvimux.bindings = bindings nvimux.config = {} nvimux.term = {} nvimux.commands = setmetatable({},{