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
pull/293/head
Akianonymus 3 years ago committed by siduck76
parent e3f0429a72
commit 02f0122ab4

@ -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()

@ -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,108 +11,100 @@ 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
map("n", "dd", [=[ "_dd ]=], opt)
map("v", "dd", [=[ "_dd ]=], opt)
map("v", "x", [=[ "_x ]=], opt)
this line too ]]
--
-- Don't copy the replaced text after pasting in visual mode
map("v", "p", '"_dP', opt)
-- Allow moving the cursor through wrapped lines with j, k, <Up> and <Down>
-- 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("", "<Down>", 'v:count ? "j" : "gj"', { expr = true })
map("", "<Up>", 'v:count ? "k" : "gk"', { expr = true })
-- copy whole file content
map("n", miscMap.copywhole_file, ":%y+<CR>", opt)
-- toggle numbers
map("n", miscMap.toggle_linenr, ":set nu!<CR>", opt)
-- terminals
local function terms()
local m = user_map.terms
-- get out of terminal mode
map("t", m.esc_termmode, "<C-\\><C-n>", opt)
-- hide a term from within terminal mode
map("t", m.esc_hide_termmode, "<C-\\><C-n> :lua require('utils').close_buffer() <CR>", opt)
-- pick a hidden term
map("n", m.pick_term, ":Telescope terms <CR>", 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 <CR>", opt)
map("n", m.new_vert, ":execute 'vnew +terminal' | let b:term_type = 'vert' | startinsert <CR>", opt)
map("n", m.new_hori, ":execute 15 .. 'new +terminal' | let b:term_type = 'hori' | startinsert <CR>", opt)
end
-- 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
-- Don't copy the replaced text after pasting in visual mode
map("v", "p", '"_dP', opt)
-- Allow moving the cursor through wrapped lines with j, k, <Up> and <Down>
-- 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("", "<Down>", 'v:count ? "j" : "gj"', { expr = true })
map("", "<Up>", 'v:count ? "k" : "gk"', { expr = true })
-- copy whole file content
map("n", miscMap.copywhole_file, ":%y+<CR>", opt)
-- toggle numbers
map("n", miscMap.toggle_linenr, ":set nu!<CR>", opt)
-- terminals
local function terms()
local m = user_map.terms
-- get out of terminal mode
map("t", m.esc_termmode, "<C-\\><C-n>", opt)
-- hide a term from within terminal mode
map("t", m.esc_hide_termmode, "<C-\\><C-n> :lua require('utils').close_buffer() <CR>", opt)
-- pick a hidden term
map("n", m.pick_term, ":Telescope terms <CR>", 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 <CR>", opt)
map("n", m.new_vert, ":execute 'vnew +terminal' | let b:term_type = 'vert' | startinsert <CR>", opt)
map("n", m.new_hori, ":execute 15 .. 'new +terminal' | let b:term_type = 'hori' | startinsert <CR>", opt)
end
terms()
terms()
-- ctrl + s to save file
map("n", "<C-s>", ":w <CR>", opt)
M.truezen = function()
local m = user_map.truezen
-- use ESC to turn off search highlighting
map("n", "<Esc>", ":noh<CR>", opt)
map("n", m.ataraxisMode, ":TZAtaraxis<CR>", opt)
map("n", m.minimalisticmode, ":TZMinimalist<CR>", opt)
map("n", m.focusmode, ":TZFocus<CR>", opt)
end
-- navigation within insert mode
local check_insertNav = require("chadrc").options.enable_insertNav
map("n", "<C-s>", ":w <CR>", opt)
if check_insertNav == true then
local m = user_map.insert_nav
M.comment_nvim = function()
local m = user_map.comment_nvim.comment_toggle
map("n", m, ":CommentToggle<CR>", opt)
map("v", m, ":CommentToggle<CR>", opt)
end
map("i", m.forward, "<Right>", opt)
map("i", m.backward, "<Left>", opt)
map("i", m.top_of_line, "<ESC>^i", opt)
map("i", m.end_of_line, "<End>", opt)
map("i", m.next_line, "<Up>", opt)
map("i", m.prev_line, "<Down>", opt)
end
M.nvimtree = function()
local m = user_map.nvimtree.treetoggle
-- 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, ":NvimTreeToggle<CR>", opt)
end
map("n", m, ":lua require('utils').toggle_theme(require('chadrc').ui.fav_themes)<CR>", opt)
end
M.neoformat = function()
local m = user_map.neoformat.format
map("n", m, ":Neoformat<CR>", 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()"
end
M.dashboard = function()
local m = user_map.dashboard
map("n", m.open, ":Dashboard<CR>", opt)
map("n", m.newfile, ":DashboardNewFile<CR>", opt)
map("n", m.bookmarks, ":DashboardJumpMarks<CR>", opt)
map("n", m.sessionload, ":SessionLoad<CR>", opt)
map("n", m.sessionsave, ":SessionSave<CR>", opt)
end
M.bufferline = function()
local m = user_map.bufferline
M.telescope = function()
local m = user_map.telescope
map("n", m.new_buffer, ":enew<CR>", opt) -- new buffer
map("n", m.newtab, ":tabnew<CR>", opt) -- new tab
map("n", m.close, ":lua require('utils').close_buffer() <CR>", opt) -- close buffer
map("n", m.live_grep, ":Telescope live_grep<CR>", opt)
map("n", m.git_status, ":Telescope git_status <CR>", opt)
map("n", m.git_commits, ":Telescope git_commits <CR>", opt)
map("n", m.find_files, ":Telescope find_files <CR>", opt)
map("n", m.buffers, ":Telescope buffers<CR>", opt)
map("n", m.help_tags, ":Telescope help_tags<CR>", opt)
map("n", m.oldfiles, ":Telescope oldfiles<CR>", opt)
map("n", m.themes, ":Telescope themes<CR>", opt)
end
-- move between tabs
M.telescope_media = function()
local m = user_map.telescope_media
map("n", m.media_files, ":Telescope media_files <CR>", opt)
map("n", m.cycleNext, ":BufferLineCycleNext<CR>", opt)
map("n", m.cyclePrev, ":BufferLineCyclePrev<CR>", opt)
end
M.chadsheet = function()
@ -128,28 +119,21 @@ M.chadsheet = function()
)
end
M.bufferline = function()
local m = user_map.bufferline
map("n", m.new_buffer, ":enew<CR>", opt) -- new buffer
map("n", m.newtab, ":tabnew<CR>", opt) -- new tab
map("n", m.close, ":lua require('utils').close_buffer() <CR>", opt) -- close buffer
-- move between tabs
map("n", m.cycleNext, ":BufferLineCycleNext<CR>", opt)
map("n", m.cyclePrev, ":BufferLineCyclePrev<CR>", opt)
M.comment_nvim = function()
local m = user_map.comment_nvim.comment_toggle
map("n", m, ":CommentToggle<CR>", opt)
map("v", m, ":CommentToggle<CR>", opt)
end
-- use ESC to turn off search highlighting
map("n", "<Esc>", ":noh<CR>", opt)
M.dashboard = function()
local m = user_map.dashboard
-- 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()"
map("n", m.open, ":Dashboard<CR>", opt)
map("n", m.newfile, ":DashboardNewFile<CR>", opt)
map("n", m.bookmarks, ":DashboardJumpMarks<CR>", opt)
map("n", m.sessionload, ":SessionLoad<CR>", opt)
map("n", m.sessionsave, ":SessionSave<CR>", opt)
end
M.fugitive = function()
local m = user_map.fugitive
@ -160,25 +144,41 @@ M.fugitive = function()
map("n", m.git_blame, ":Git blame<CR>", opt)
end
-- navigation within insert mode
local check_insertNav = require("chadrc").options.enable_insertNav
M.nvimtree = function()
local m = user_map.nvimtree.treetoggle
map("n", m, ":NvimTreeToggle<CR>", opt)
end
M.neoformat = function()
local m = user_map.neoformat.format
map("n", m, ":Neoformat<CR>", opt)
end
if check_insertNav == true then
local m = user_map.insert_nav
M.truezen = function()
local m = user_map.truezen
map("i", m.forward, "<Right>", opt)
map("i", m.backward, "<Left>", opt)
map("i", m.top_of_line, "<ESC>^i", opt)
map("i", m.end_of_line, "<End>", opt)
map("i", m.next_line, "<Up>", opt)
map("i", m.prev_line, "<Down>", opt)
map("n", m.ataraxisMode, ":TZAtaraxis<CR>", opt)
map("n", m.minimalisticmode, ":TZMinimalist<CR>", opt)
map("n", m.focusmode, ":TZFocus<CR>", opt)
end
local theme_toggler = require("chadrc").ui.theme_toggler
M.telescope = function()
local m = user_map.telescope
if theme_toggler == true then
local m = user_map.misc.theme_toggle
map("n", m.live_grep, ":Telescope live_grep<CR>", opt)
map("n", m.git_status, ":Telescope git_status <CR>", opt)
map("n", m.git_commits, ":Telescope git_commits <CR>", opt)
map("n", m.find_files, ":Telescope find_files <CR>", opt)
map("n", m.buffers, ":Telescope buffers<CR>", opt)
map("n", m.help_tags, ":Telescope help_tags<CR>", opt)
map("n", m.oldfiles, ":Telescope oldfiles<CR>", opt)
map("n", m.themes, ":Telescope themes<CR>", opt)
end
map("n", m, ":lua require('utils').toggle_theme(require('chadrc').ui.fav_themes)<CR>", opt)
M.telescope_media = function()
local m = user_map.telescope_media
map("n", m.media_files, ":Telescope media_files <CR>", opt)
end
return M

Loading…
Cancel
Save