From 439832a588ec437c2837e90b174e3d006d71db58 Mon Sep 17 00:00:00 2001 From: Akianonymus Date: Thu, 19 Aug 2021 08:45:59 +0530 Subject: [PATCH] mappings|init: Move init mappings to a function, only call when required | Show err message for init because mappings.lua is called from multiple places, so there should't be any code that executes without calling a specific function show error message when something fails in init.lua makes no sense to not rearrange plugin functions alphabetically, but keep misc at top --- init.lua | 6 +- lua/mappings.lua | 228 +++++++++++++++++++++++------------------------ 2 files changed, 119 insertions(+), 115 deletions(-) diff --git a/init.lua b/init.lua index b65c997..6353271 100644 --- a/init.lua +++ b/init.lua @@ -4,5 +4,9 @@ local chad_modules = { } for i = 1, #chad_modules, 1 do - pcall(require, chad_modules[i]) + if not pcall(require, chad_modules[i]) then + error("Error loading " .. chad_modules[i] .. "\n") + end end + +require("mappings").misc() diff --git a/lua/mappings.lua b/lua/mappings.lua index 01ae785..14740aa 100644 --- a/lua/mappings.lua +++ b/lua/mappings.lua @@ -1,7 +1,6 @@ local user_map = require("chadrc").mappings local miscMap = user_map.misc -local M = {} local cmd = vim.cmd local function map(mode, lhs, rhs, opts) @@ -12,64 +11,113 @@ local function map(mode, lhs, rhs, opts) vim.api.nvim_set_keymap(mode, lhs, rhs, options) end +local M = {} local opt = {} --- dont copy any deleted text , this is disabled by default so uncomment the below mappings if you want them ---[[ remove this line +-- these mappings will only be called during initialization +M.misc = function() + -- dont copy any deleted text , this is disabled by default so uncomment the below mappings if you want them + -- map("n", "dd", [=[ "_dd ]=], opt) + -- map("v", "dd", [=[ "_dd ]=], opt) + -- map("v", "x", [=[ "_x ]=], opt) + -- todo: this should be configurable via chadrc -map("n", "dd", [=[ "_dd ]=], opt) -map("v", "dd", [=[ "_dd ]=], opt) -map("v", "x", [=[ "_x ]=], opt) + -- Don't copy the replaced text after pasting in visual mode + map("v", "p", '"_dP', opt) - this line too ]] --- + -- Allow moving the cursor through wrapped lines with j, k, and + -- http://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/ + -- empty mode is same as using :map + map("", "j", 'v:count ? "j" : "gj"', { expr = true }) + map("", "k", 'v:count ? "k" : "gk"', { expr = true }) + map("", "", 'v:count ? "j" : "gj"', { expr = true }) + map("", "", 'v:count ? "k" : "gk"', { expr = true }) --- Don't copy the replaced text after pasting in visual mode -map("v", "p", '"_dP', opt) + -- copy whole file content + map("n", miscMap.copywhole_file, ":%y+", opt) --- Allow moving the cursor through wrapped lines with j, k, and --- http://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/ --- empty mode is same as using :map -map("", "j", 'v:count ? "j" : "gj"', { expr = true }) -map("", "k", 'v:count ? "k" : "gk"', { expr = true }) -map("", "", 'v:count ? "j" : "gj"', { expr = true }) -map("", "", 'v:count ? "k" : "gk"', { expr = true }) + -- toggle numbers + map("n", miscMap.toggle_linenr, ":set nu!", opt) --- copy whole file content -map("n", miscMap.copywhole_file, ":%y+", opt) + -- terminals + local function terms() + local m = user_map.terms --- toggle numbers -map("n", miscMap.toggle_linenr, ":set nu!", opt) + -- get out of terminal mode + map("t", m.esc_termmode, "", opt) + -- hide a term from within terminal mode + map("t", m.esc_hide_termmode, " :lua require('utils').close_buffer() ", opt) + -- pick a hidden term + map("n", m.pick_term, ":Telescope terms ", opt) --- terminals -local function terms() - local m = user_map.terms + -- Open terminals + -- TODO this opens on top of an existing vert/hori term, fixme + map("n", m.new_wind, ":execute 'terminal' | let b:term_type = 'wind' | startinsert ", opt) + map("n", m.new_vert, ":execute 'vnew +terminal' | let b:term_type = 'vert' | startinsert ", opt) + map("n", m.new_hori, ":execute 15 .. 'new +terminal' | let b:term_type = 'hori' | startinsert ", opt) + end + terms() - -- get out of terminal mode - map("t", m.esc_termmode, "", opt) - -- hide a term from within terminal mode - map("t", m.esc_hide_termmode, " :lua require('utils').close_buffer() ", opt) - -- pick a hidden term - map("n", m.pick_term, ":Telescope terms ", opt) + -- ctrl + s to save file + map("n", "", ":w ", opt) - -- Open terminals - -- TODO this opens on top of an existing vert/hori term, fixme - map("n", m.new_wind, ":execute 'terminal' | let b:term_type = 'wind' | startinsert ", opt) - map("n", m.new_vert, ":execute 'vnew +terminal' | let b:term_type = 'vert' | startinsert ", opt) - map("n", m.new_hori, ":execute 15 .. 'new +terminal' | let b:term_type = 'hori' | startinsert ", opt) + -- use ESC to turn off search highlighting + map("n", "", ":noh", opt) + + -- navigation within insert mode + local check_insertNav = require("chadrc").options.enable_insertNav + + if check_insertNav == true then + local m = user_map.insert_nav + + map("i", m.forward, "", opt) + map("i", m.backward, "", opt) + map("i", m.top_of_line, "^i", opt) + map("i", m.end_of_line, "", opt) + map("i", m.next_line, "", opt) + map("i", m.prev_line, "", opt) + end + + -- check the theme toggler + local theme_toggler = require("chadrc").ui.theme_toggler + if theme_toggler == true then + local m = user_map.misc.theme_toggle + + map("n", m, ":lua require('utils').toggle_theme(require('chadrc').ui.fav_themes)", opt) + end + + -- Packer commands till because we are not loading it at startup + cmd "silent! command PackerCompile lua require 'pluginList' require('packer').compile()" + cmd "silent! command PackerInstall lua require 'pluginList' require('packer').install()" + cmd "silent! command PackerStatus lua require 'pluginList' require('packer').status()" + cmd "silent! command PackerSync lua require 'pluginList' require('packer').sync()" + cmd "silent! command PackerUpdate lua require 'pluginList' require('packer').update()" end -terms() +M.bufferline = function() + local m = user_map.bufferline -M.truezen = function() - local m = user_map.truezen + map("n", m.new_buffer, ":enew", opt) -- new buffer + map("n", m.newtab, ":tabnew", opt) -- new tab + map("n", m.close, ":lua require('utils').close_buffer() ", opt) -- close buffer - map("n", m.ataraxisMode, ":TZAtaraxis", opt) - map("n", m.minimalisticmode, ":TZMinimalist", opt) - map("n", m.focusmode, ":TZFocus", opt) + -- move between tabs + + map("n", m.cycleNext, ":BufferLineCycleNext", opt) + map("n", m.cyclePrev, ":BufferLineCyclePrev", opt) end -map("n", "", ":w ", opt) +M.chadsheet = function() + local m = user_map.chadsheet + + map("n", m.default_keys, ":lua require('cheatsheet').show_cheatsheet_telescope()", opt) + map( + "n", + m.user_keys, + ":lua require('cheatsheet').show_cheatsheet_telescope{bundled_cheatsheets = false, bundled_plugin_cheatsheets = false }", + opt + ) +end M.comment_nvim = function() local m = user_map.comment_nvim.comment_toggle @@ -77,6 +125,25 @@ M.comment_nvim = function() map("v", m, ":CommentToggle", opt) end +M.dashboard = function() + local m = user_map.dashboard + + map("n", m.open, ":Dashboard", opt) + map("n", m.newfile, ":DashboardNewFile", opt) + map("n", m.bookmarks, ":DashboardJumpMarks", opt) + map("n", m.sessionload, ":SessionLoad", opt) + map("n", m.sessionsave, ":SessionSave", opt) +end + +M.fugitive = function() + local m = user_map.fugitive + + map("n", m.Git, ":Git", opt) + map("n", m.diffget_2, ":diffget //2", opt) + map("n", m.diffget_3, ":diffget //3", opt) + map("n", m.git_blame, ":Git blame", opt) +end + M.nvimtree = function() local m = user_map.nvimtree.treetoggle @@ -88,14 +155,12 @@ M.neoformat = function() map("n", m, ":Neoformat", opt) end -M.dashboard = function() - local m = user_map.dashboard +M.truezen = function() + local m = user_map.truezen - map("n", m.open, ":Dashboard", opt) - map("n", m.newfile, ":DashboardNewFile", opt) - map("n", m.bookmarks, ":DashboardJumpMarks", opt) - map("n", m.sessionload, ":SessionLoad", opt) - map("n", m.sessionsave, ":SessionSave", opt) + map("n", m.ataraxisMode, ":TZAtaraxis", opt) + map("n", m.minimalisticmode, ":TZMinimalist", opt) + map("n", m.focusmode, ":TZFocus", opt) end M.telescope = function() @@ -116,69 +181,4 @@ M.telescope_media = function() map("n", m.media_files, ":Telescope media_files ", opt) end -M.chadsheet = function() - local m = user_map.chadsheet - - map("n", m.default_keys, ":lua require('cheatsheet').show_cheatsheet_telescope()", opt) - map( - "n", - m.user_keys, - ":lua require('cheatsheet').show_cheatsheet_telescope{bundled_cheatsheets = false, bundled_plugin_cheatsheets = false }", - opt - ) -end - -M.bufferline = function() - local m = user_map.bufferline - - map("n", m.new_buffer, ":enew", opt) -- new buffer - map("n", m.newtab, ":tabnew", opt) -- new tab - map("n", m.close, ":lua require('utils').close_buffer() ", opt) -- close buffer - - -- move between tabs - - map("n", m.cycleNext, ":BufferLineCycleNext", opt) - map("n", m.cyclePrev, ":BufferLineCyclePrev", opt) -end - --- use ESC to turn off search highlighting -map("n", "", ":noh", opt) - --- Packer commands till because we are not loading it at startup -cmd "silent! command PackerCompile lua require 'pluginList' require('packer').compile()" -cmd "silent! command PackerInstall lua require 'pluginList' require('packer').install()" -cmd "silent! command PackerStatus lua require 'pluginList' require('packer').status()" -cmd "silent! command PackerSync lua require 'pluginList' require('packer').sync()" -cmd "silent! command PackerUpdate lua require 'pluginList' require('packer').update()" - -M.fugitive = function() - local m = user_map.fugitive - - map("n", m.Git, ":Git", opt) - map("n", m.diffget_2, ":diffget //2", opt) - map("n", m.diffget_3, ":diffget //3", opt) - map("n", m.git_blame, ":Git blame", opt) -end - --- navigation within insert mode -local check_insertNav = require("chadrc").options.enable_insertNav - -if check_insertNav == true then - local m = user_map.insert_nav - - map("i", m.forward, "", opt) - map("i", m.backward, "", opt) - map("i", m.top_of_line, "^i", opt) - map("i", m.end_of_line, "", opt) - map("i", m.next_line, "", opt) - map("i", m.prev_line, "", opt) -end - -local theme_toggler = require("chadrc").ui.theme_toggler - -if theme_toggler == true then - local m = user_map.misc.theme_toggle - - map("n", m, ":lua require('utils').toggle_theme(require('chadrc').ui.fav_themes)", opt) -end return M