From 768764a899b7a2f96b5bf943eb0a0691ff0ceed1 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Mon, 6 Dec 2021 22:49:03 +0100 Subject: [PATCH] Use utility module for keymap/abbrev functions --- home/.config/nvim/init.lua | 23 ----- home/.config/nvim/lua/abbreviations.lua | 31 ++----- home/.config/nvim/lua/mappings.lua | 93 ++++++++++--------- .../nvim/lua/pluginconf/commentary.lua | 5 +- home/.config/nvim/lua/pluginconf/fzf.lua | 15 +-- home/.config/nvim/lua/pluginconf/nerdtree.lua | 5 +- home/.config/nvim/lua/pluginconf/nvim-dap.lua | 25 ++--- home/.config/nvim/lua/utility/mappings.lua | 29 ++++++ 8 files changed, 110 insertions(+), 116 deletions(-) create mode 100644 home/.config/nvim/lua/utility/mappings.lua diff --git a/home/.config/nvim/init.lua b/home/.config/nvim/init.lua index 2af840f..e254098 100644 --- a/home/.config/nvim/init.lua +++ b/home/.config/nvim/init.lua @@ -1,26 +1,3 @@ -local vim = require("vim") -local fn = vim.fn - --- Define some global functions which can then be called --- in the other required scripts - --- Load an arbitrary .vim or .lua file -function LoadFile(file_path) - local extension = file_path:match("^.+(%..+)$") - local run_cmd - if (extension == ".vim") then run_cmd = "source" else run_cmd = "luafile" end - fn.execute(run_cmd .. " " .. file_path) -end - --- Define a key mapping -function Keymap(mode, shortcut, command, options) - -- Assume silent, noremap unless specified otherwise - local opts = {noremap=true, silent=true} - - if options then opts = vim.tbl_extend("force", opts, options) end - vim.api.nvim_set_keymap(mode, shortcut, command, opts) -end - -- Require additional scripts which contain individual configurations require "options" diff --git a/home/.config/nvim/lua/abbreviations.lua b/home/.config/nvim/lua/abbreviations.lua index 2438046..95cb0e1 100644 --- a/home/.config/nvim/lua/abbreviations.lua +++ b/home/.config/nvim/lua/abbreviations.lua @@ -1,27 +1,9 @@ local vim = require("vim") -local cmd = vim.cmd +local fn = vim.fn +local m = require("utility.mappings") --- I'm not aware of abbreviations having direct lua support like mappings, --- though I'm not certain on that and may be completely wrong, if there is --- a better way, this is opened to pull requests. --- TODO: Check direct abbrev lua support - --- Make these function definitions global as they could be reused --- in plugin specific scripts or in other places. -function abbrev(mode, input, result, reabbrev) - -- Assume noreabbrev unless specified otherwise - reabbrev = reabbrev or false - local command - if reabbrev then - command = mode .. "abbrev" - else - command = mode .. "noreabbrev" - end - cmd(command .. " " .. input .. " " .. result) -end - -function cabbrev(input, result, reabbrev) - abbrev("c", input, result, reabbrev) +local function cabbrev(input, result, reabbrev) + m.abbrev("c", input, result, reabbrev) end -- Invalid case abbreviations @@ -40,6 +22,5 @@ cabbrev("Qall!", "qall!") cabbrev("w!!", "w !sudo tee > /dev/null %") -- Reload lua configuration --- TODO: Get the path dynamically -cabbrev("reload", "luafile ~/.config/nvim/init.lua") - +local initlua = fn.stdpath("config") .. "init.lua" +cabbrev("reload", "luafile " .. initlua) diff --git a/home/.config/nvim/lua/mappings.lua b/home/.config/nvim/lua/mappings.lua index cba2b62..a35c434 100644 --- a/home/.config/nvim/lua/mappings.lua +++ b/home/.config/nvim/lua/mappings.lua @@ -1,3 +1,4 @@ +local m = require("utility.mappings") local vim = require("vim") local g = vim.g @@ -6,86 +7,86 @@ local g = vim.g g.mapleader = "\\" -- Unmap arrow keys in normal mode to remove bad habits -Keymap("n", "", "") -Keymap("n", "", "") -Keymap("n", "", "") -Keymap("n", "", "") +m.keymap("n", "", "") +m.keymap("n", "", "") +m.keymap("n", "", "") +m.keymap("n", "", "") -- Tab navigation -Keymap("n", "", "gt") -Keymap("n", "", "gT") -Keymap("n", "", ":tabnew") -Keymap("n", "", ":tabmove +") -Keymap("n", "", ":tabmove -") -Keymap("n", "", ":tabp") -Keymap("n", "", ":tabn") -Keymap("n", "", ":tabc") +m.keymap("n", "", "gt") +m.keymap("n", "", "gT") +m.keymap("n", "", ":tabnew") +m.keymap("n", "", ":tabmove +") +m.keymap("n", "", ":tabmove -") +m.keymap("n", "", ":tabp") +m.keymap("n", "", ":tabn") +m.keymap("n", "", ":tabc") -- Buffer navigation -Keymap("n", "", ":bn") -Keymap("n", "", ":bp") -Keymap("n", "", ":bd") +m.keymap("n", "", ":bn") +m.keymap("n", "", ":bp") +m.keymap("n", "", ":bd") -- Set splits navigation to just ALT + hjkl -Keymap("n", "", "h") -Keymap("n", "", "j") -Keymap("n", "", "k") -Keymap("n", "", "l") +m.keymap("n", "", "h") +m.keymap("n", "", "j") +m.keymap("n", "", "k") +m.keymap("n", "", "l") -- Split size adjusting -Keymap("n", "", ":vertical resize +3") -Keymap("n", "", ":vertical resize -3") -Keymap("n", "", ":resize +3") -Keymap("n", "", ":resize -3") +m.keymap("n", "", ":vertical resize +3") +m.keymap("n", "", ":vertical resize -3") +m.keymap("n", "", ":resize +3") +m.keymap("n", "", ":resize -3") -- Define some common shortcuts -Keymap("n", "", ":w") -Keymap("i", "", ":wi") -Keymap("n", "", ":undo") -Keymap("n", "", ":redo") +m.keymap("n", "", ":w") +m.keymap("i", "", ":wi") +m.keymap("n", "", ":undo") +m.keymap("n", "", ":redo") -- Terminal -Keymap("n", "", ":split term://zsh:resize -7i") -Keymap("n", "", ":vnew term://zshi") -Keymap("n", "", ":tabnew term://zshi") -Keymap("t", "", "") +m.keymap("n", "", ":split term://zsh:resize -7i") +m.keymap("n", "", ":vnew term://zshi") +m.keymap("n", "", ":tabnew term://zshi") +m.keymap("t", "", "") -- Use space for folding/unfolding sections -Keymap("n", "", "za") -Keymap("v", "", "zf") +m.keymap("n", "", "za") +m.keymap("v", "", "zf") -- Use shift to quickly move 10 lines up/down -Keymap("n", "K", "10k") -Keymap("n", "J", "10j") +m.keymap("n", "K", "10k") +m.keymap("n", "J", "10j") -- Enable/Disable auto commenting -Keymap("n", "c", ":setlocal formatoptions-=cro") -Keymap("n", "C", ":setlocal formatoptions+=cro") +m.keymap("n", "c", ":setlocal formatoptions-=cro") +m.keymap("n", "C", ":setlocal formatoptions+=cro") -- Don't leave visual mode after indenting -Keymap("v", "<", "", ">gv") +m.keymap("v", "<", "", ">gv") -- System clipboard copying -Keymap("v", "", '"+y') +m.keymap("v", "", '"+y') -- Alias replace all -Keymap("n", "", ":%s//gI", {silent=false}) +m.keymap("n", "", ":%s//gI", {silent=false}) -- Stop search highlight with Esc -Keymap("n", "", ":noh") +m.keymap("n", "", ":noh") -- Start spell-check -Keymap("n", "s", ":setlocal spell! spelllang=en_us") +m.keymap("n", "s", ":setlocal spell! spelllang=en_us") -- Run shell check -Keymap("n", "p", ":!shellckeck %") +m.keymap("n", "p", ":!shellckeck %") -- Compile opened file (using custom script) -Keymap("n", "", ":w | !comp %") +m.keymap("n", "", ":w | !comp %") -- Close all opened buffers -Keymap("n", "Q", ":bufdo bdelete") +m.keymap("n", "Q", ":bufdo bdelete") -- Don't set the incredibely annoying mappings that trigger dynamic SQL -- completion with dbext and keeps on freezing vim whenever pressed with diff --git a/home/.config/nvim/lua/pluginconf/commentary.lua b/home/.config/nvim/lua/pluginconf/commentary.lua index 69e80f8..6a1f92e 100644 --- a/home/.config/nvim/lua/pluginconf/commentary.lua +++ b/home/.config/nvim/lua/pluginconf/commentary.lua @@ -1,9 +1,10 @@ +local m = require("utility.mappings") local vim = require("vim") local cmd = vim.cmd -- Set up shortcuts to quickly comment some code -Keymap("n", "", ":Commentary") -Keymap("v", "", ":Commentary") +m.keymap("n", "", ":Commentary") +m.keymap("v", "", ":Commentary") -- Set up comments for unhandled file types cmd[[autocmd FileType apache setlocal commentstring=#\ %s]] diff --git a/home/.config/nvim/lua/pluginconf/fzf.lua b/home/.config/nvim/lua/pluginconf/fzf.lua index e520c6e..5cd78d0 100644 --- a/home/.config/nvim/lua/pluginconf/fzf.lua +++ b/home/.config/nvim/lua/pluginconf/fzf.lua @@ -1,3 +1,4 @@ +local m = require("utility.mappings") local vim = require("vim") local cmd = vim.cmd local g = vim.g @@ -26,10 +27,10 @@ command! -bang -nargs=? -complete=dir AllFiles \ call fzf#run(fzf#wrap('allfiles', fzf#vim#with_preview({ 'dir': , 'sink': 'e', 'source': 'rg --files --hidden --no-ignore' }), 0)) ]] -Keymap("n", "f", ":Files") -Keymap("n", "F", ":AllFiles") -Keymap("n", "b", ":Buffers") -Keymap("n", "h", ":History") -Keymap("n", "r", ":Rg") -Keymap("n", "R", ":Rg", { silent = false }) -Keymap("n", "gb", ":GBranches") +m.keymap("n", "f", ":Files") +m.keymap("n", "F", ":AllFiles") +m.keymap("n", "b", ":Buffers") +m.keymap("n", "h", ":History") +m.keymap("n", "r", ":Rg") +m.keymap("n", "R", ":Rg", { silent = false }) +m.keymap("n", "gb", ":GBranches") diff --git a/home/.config/nvim/lua/pluginconf/nerdtree.lua b/home/.config/nvim/lua/pluginconf/nerdtree.lua index 47e7183..5ea4d21 100644 --- a/home/.config/nvim/lua/pluginconf/nerdtree.lua +++ b/home/.config/nvim/lua/pluginconf/nerdtree.lua @@ -1,3 +1,4 @@ +local m = require("utility.mappings") local vim = require("vim") local g = vim.g local fn = vim.fn @@ -5,7 +6,7 @@ local cmd = vim.cmd -- Implement manual NERDTreeToggle, but use NERDTreeFind for openning. -- This makes NERDTree open with the current file pre-selected -Keymap("n", "", "g:NERDTree.IsOpen() ? ':NERDTreeClose' : @% == '' ? ':NERDTree' : ':NERDTreeFind'", {expr=true}) +m.keymap("n", "", "g:NERDTree.IsOpen() ? ':NERDTreeClose' : @% == '' ? ':NERDTree' : ':NERDTreeFind'", {expr=true}) g.NERDTreeShowHidden = 1 g.NERDTreeMinimalUI = 1 @@ -31,6 +32,8 @@ autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in ]] -- Exit Vim if NERDTree is the only window left. +-- WARNING: This causes issues when closing buffers +--[[ cmd[[ autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | \ quit | endif diff --git a/home/.config/nvim/lua/pluginconf/nvim-dap.lua b/home/.config/nvim/lua/pluginconf/nvim-dap.lua index 84f8167..ef597ff 100644 --- a/home/.config/nvim/lua/pluginconf/nvim-dap.lua +++ b/home/.config/nvim/lua/pluginconf/nvim-dap.lua @@ -1,17 +1,18 @@ +local m = require("utility.mappings") local vim = require("vim") local cmd = vim.cmd -- Define dap mappings (:help dap-mapping) local prefix = ":lua require('dap')." -Keymap("n", "", prefix .. "continue()") -Keymap("n", "", prefix .. "step_over()") -Keymap("n", "", prefix .. "step_into()") -Keymap("n", "", prefix .. "step_out()") -Keymap("n", "", prefix .. "toggle_breakpoint()") -Keymap("n", "", prefix .. "set_breakpoint(vim.fn.input('Breakpoint condition: '))") -Keymap("n", "", prefix .. "set_breakpoint(nil, nil, vim.fn.input('Log point message: '))") -Keymap("n", "di", prefix .. "repl.open()") -Keymap("n", "dl", prefix .. "run_last()") +m.keymap("n", "", prefix .. "continue()") +m.keymap("n", "", prefix .. "step_over()") +m.keymap("n", "", prefix .. "step_into()") +m.keymap("n", "", prefix .. "step_out()") +m.keymap("n", "", prefix .. "toggle_breakpoint()") +m.keymap("n", "", prefix .. "set_breakpoint(vim.fn.input('Breakpoint condition: '))") +m.keymap("n", "", prefix .. "set_breakpoint(nil, nil, vim.fn.input('Log point message: '))") +m.keymap("n", "di", prefix .. "repl.open()") +m.keymap("n", "dl", prefix .. "run_last()") -- Setup dap for python (requires nvim-dap-python plugin) require('dap-python').setup('/usr/bin/python') -- Path to python with `debugpy` library installed @@ -19,6 +20,6 @@ require('dap-python').test_runner = 'pytest' -- Use pytest to run unit tests -- Python mappings local pyprefix = ":lua require('dap-python')." -Keymap("n", "dptm", pyprefix .. "test_method()") -Keymap("n", "dptc", pyprefix .. "test_class()") -Keymap("v", "ds", "" .. pyprefix .. "debug_selection()") +m.keymap("n", "dptm", pyprefix .. "test_method()") +m.keymap("n", "dptc", pyprefix .. "test_class()") +m.keymap("v", "ds", "" .. pyprefix .. "debug_selection()") diff --git a/home/.config/nvim/lua/utility/mappings.lua b/home/.config/nvim/lua/utility/mappings.lua new file mode 100644 index 0000000..6d18a7e --- /dev/null +++ b/home/.config/nvim/lua/utility/mappings.lua @@ -0,0 +1,29 @@ +local vim = require("vim") +local api = vim.api +local cmd = vim.cmd + +local M = {} + +-- Define a keymap +function M.keymap(mode, shortcut, command, options) + -- Assume silent, noremap unless specified otherwise + local opts = {noremap=true, silent=true} + + if options then opts = vim.tbl_extend("force", opts, options) end + api.nvim_set_keymap(mode, shortcut, command, opts) +end + +-- Define an abbreviation +function M.abbrev(mode, input, result, reabbrev) + -- Assume noreabbrev unless specified otherwise + reabbrev = reabbrev or false + local command + if reabbrev then + command = mode .. "abbrev" + else + command = mode .. "noreabbrev" + end + cmd(command .. " " .. input .. " " .. result) +end + +return M