vim: start keymap refactor
This commit is contained in:
parent
48ecd25d7c
commit
0a59d62d33
16 changed files with 353 additions and 330 deletions
|
@ -1,2 +1,13 @@
|
|||
require 'fschauen'
|
||||
vim.g.mapleader = ' '
|
||||
vim.g.maplocalleader = ','
|
||||
|
||||
require 'fschauen.setup.globals'
|
||||
require 'fschauen.setup.options'
|
||||
require('fschauen.keymap').setup()
|
||||
require 'fschauen.setup.autocmd'
|
||||
require 'fschauen.setup.filetype'
|
||||
require 'fschauen.setup.diagnostic'
|
||||
require 'fschauen.setup.lazy'
|
||||
|
||||
require('fschauen').colorscheme('gruvbox')
|
||||
|
||||
|
|
45
config/nvim/lua/fschauen/diagnostic.lua
Normal file
45
config/nvim/lua/fschauen/diagnostic.lua
Normal file
|
@ -0,0 +1,45 @@
|
|||
M = {}
|
||||
|
||||
local diag_opts = {
|
||||
wrap = false, -- don't wrap around the begin/end of file
|
||||
}
|
||||
|
||||
---Move to the next diagnostic.
|
||||
---@param opts table\nil: options passed along to `vim.diagnostic.goto_next`.
|
||||
M.goto_next= function(opts)
|
||||
vim.diagnostic.goto_next(vim.tbl_extend('keep', opts or {}, diag_opts))
|
||||
vim.cmd 'normal zz'
|
||||
end
|
||||
|
||||
---Move to the previous diagnostic.
|
||||
---@param opts table|nil: options passed along to `vim.diagnostic.goto_prev`.
|
||||
M.goto_prev= function(opts)
|
||||
vim.diagnostic.goto_prev(vim.tbl_extend('keep', opts or {}, diag_opts))
|
||||
vim.cmd 'normal zz'
|
||||
end
|
||||
|
||||
---Show diagnostics in a floating window.
|
||||
---@param opts table|nil: options passed along to `vim.diagnostic.open_float`.
|
||||
M.open_float= function(opts)
|
||||
vim.diagnostic.open_float(opts)
|
||||
end
|
||||
|
||||
---Toggle diagnostics in the given buffer.
|
||||
---@param bufnr integer|nil: Buffer number (0 for current buffer, nil for all buffers.
|
||||
M.toggle = function(bufnr)
|
||||
bufnr = bufnr or 0
|
||||
if vim.diagnostic.is_disabled(bufnr) then
|
||||
vim.diagnostic.enable(bufnr)
|
||||
else
|
||||
vim.diagnostic.disable(bufnr)
|
||||
end
|
||||
end
|
||||
|
||||
---Hide currently displayed diagnostics.
|
||||
---@param bufnr integer|nil: Buffer number (0 for current buffer, nil for all buffers.
|
||||
M.hide = function(bufnr)
|
||||
vim.diagnostic.hide(nil, bufnr or 0)
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
vim.g.loaded_gzip = 1
|
||||
vim.g.loaded_zip = 1
|
||||
vim.g.loaded_zipPlugin = 1
|
||||
vim.g.loaded_tar = 1
|
||||
vim.g.loaded_tarPlugin = 1
|
||||
vim.g.loaded_getscript = 1
|
||||
vim.g.loaded_getscriptPlugin = 1
|
||||
vim.g.loaded_vimball = 1
|
||||
vim.g.loaded_vimballPlugin = 1
|
||||
vim.g.loaded_2html_plugin = 1
|
||||
-- vim.g.loaded_matchit = 1
|
||||
vim.g.loaded_matchparen = 1
|
||||
vim.g.loaded_logiPat = 1
|
||||
vim.g.loaded_rrhelper = 1
|
||||
vim.g.loaded_netrw = 1
|
||||
vim.g.loaded_netrwPlugin = 1
|
||||
vim.g.loaded_netrwSettings = 1
|
|
@ -1,14 +1,55 @@
|
|||
vim.g.mapleader = ' '
|
||||
vim.g.maplocalleader = ','
|
||||
local M = {}
|
||||
|
||||
require 'fschauen.disable_builtin'
|
||||
require 'fschauen.globals'
|
||||
require 'fschauen.options'
|
||||
require 'fschauen.keymap'
|
||||
require 'fschauen.autocmds'
|
||||
require 'fschauen.filetypes'
|
||||
require 'fschauen.diagnostics'
|
||||
require 'fschauen.lazy'
|
||||
--- Flip function arguments.
|
||||
---
|
||||
--- flip(f)(a, b) == f(b, a)
|
||||
---
|
||||
---@param f function: function to flip.
|
||||
---@return function: function that takes `f`'s arguments flipped.
|
||||
M.flip = function(f)
|
||||
return function(a, b)
|
||||
return f(b, a)
|
||||
end
|
||||
end
|
||||
|
||||
require('fschauen.util').set_colorscheme('gruvbox')
|
||||
--- Concatenate lists.
|
||||
---
|
||||
--- extend({'a', 'b'}, {'c', 'd'}) == {'a', 'b', 'c', 'd'}
|
||||
--- extend({1, 2}, {3, 4}, {5, 6}) == {1, 2, 3, 4, 5, 6}
|
||||
---
|
||||
---@param ... table: lists to concatenate.
|
||||
---@return table: concatenation of arguments.
|
||||
M.concat = function(...)
|
||||
local result = {}
|
||||
for _, tbl in ipairs {...} do
|
||||
for _, v in pairs(tbl) do
|
||||
result[#result+1] = v
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
--- Partial function application.
|
||||
---
|
||||
--- partial(f, x)(...) == f(x, ...)
|
||||
--- partial(f, x, y)(...) == f(x, y, ...)
|
||||
---
|
||||
---@param f function: function to partially apply.
|
||||
---@param ... any: arguments to bind.
|
||||
---@return function: partially applied function.
|
||||
M.partial = function(f, ...)
|
||||
local argv = {...}
|
||||
return function(...)
|
||||
return f(unpack(M.concat(argv, {...})))
|
||||
end
|
||||
end
|
||||
|
||||
M.colorscheme = function(name)
|
||||
vim.cmd('silent! colorscheme ' .. name)
|
||||
if vim.v.errmsg ~= '' then
|
||||
vim.notify(string.format('Colorscheme %s not found!', name), vim.log.levels.WARN)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
|
|
|
@ -1,84 +1,6 @@
|
|||
local util = require('fschauen.util')
|
||||
local partial = util.partial
|
||||
local nmap = partial(vim.keymap.set, 'n')
|
||||
local imap = partial(vim.keymap.set, 'i')
|
||||
local vmap = partial(vim.keymap.set, 'v')
|
||||
local cmap = partial(vim.keymap.set, 'c')
|
||||
local tmap = partial(vim.keymap.set, 't')
|
||||
local diagnostic = require 'fschauen.diagnostic'
|
||||
local window = require 'fschauen.window'
|
||||
|
||||
-- better navigation for wrapped lines
|
||||
nmap('j', 'gj')
|
||||
nmap('k', 'gk')
|
||||
|
||||
-- maintain cursor position when joining lines
|
||||
nmap('J', 'mzJ`z')
|
||||
|
||||
-- retain selection when making changes in visual mode
|
||||
vmap( '<c-a>', '<c-a>gv')
|
||||
vmap( '<c-x>', '<c-x>gv')
|
||||
vmap('g<c-a>', 'g<c-a>gv')
|
||||
vmap('g<c-x>', 'g<c-x>gv')
|
||||
vmap('>', '><cr>gv')
|
||||
vmap('<', '<<cr>gv')
|
||||
|
||||
-- place destination of important movements in the center of the screen
|
||||
nmap('n', 'nzzzv')
|
||||
nmap('N', 'Nzzzv')
|
||||
nmap('<c-d>', '<c-d>zzzv')
|
||||
nmap('<c-u>', '<c-u>zzzv')
|
||||
|
||||
-- easier window navigation
|
||||
nmap('<c-j>', '<c-w>j')
|
||||
nmap('<c-k>', '<c-w>k')
|
||||
nmap('<c-h>', '<c-w>h')
|
||||
nmap('<c-l>', '<c-w>l')
|
||||
|
||||
-- window resizing
|
||||
nmap('<s-Up>', util.win_resize_up(2), { desc = 'Resize window upward' })
|
||||
nmap('<s-Down>', util.win_resize_down(2), { desc = 'Resize window downward' })
|
||||
nmap('<s-Left>', util.win_resize_left(2), { desc = 'Resize window leftward' })
|
||||
nmap('<s-Right>', util.win_resize_right(2), { desc = 'Resize window rightward' })
|
||||
|
||||
-- easy tab navigation
|
||||
nmap('<Right>', '<cmd>tabnext<cr>', { silent = true })
|
||||
nmap('<Left>', '<cmd>tabprevious<cr>', { silent = true })
|
||||
|
||||
-- move lines up and down
|
||||
nmap('<c-a-j>', [[:move .+1<cr>==]], { silent = true })
|
||||
nmap('<c-a-k>', [[:move .-2<cr>==]], { silent = true })
|
||||
vmap('<c-a-j>', [[:move '>+1<cr>gv=gv]], { silent = true })
|
||||
vmap('<c-a-k>', [[:move '<-2<cr>gv=gv]], { silent = true })
|
||||
imap('<c-a-j>', [[<esc>:move .+1<cr>==gi]], { silent = true })
|
||||
imap('<c-a-k>', [[<esc>:move .-2<cr>==gi]], { silent = true })
|
||||
|
||||
-- move to begin/end of line in insert mode
|
||||
imap('<c-a>', '<c-o>^')
|
||||
imap('<c-e>', '<c-o>$')
|
||||
|
||||
-- move to begin of line in command mode (<c-e> moves to end by default)
|
||||
cmap('<c-a>', '<c-b>')
|
||||
|
||||
-- navigate items in quickfix and location lists
|
||||
nmap('<leader>j', '<cmd>cnext<cr>zz', { silent = true })
|
||||
nmap('<leader>k', '<cmd>cprevious<cr>zz', { silent = true })
|
||||
nmap('<localleader>j', '<cmd>lnext<cr>zz', { silent = true })
|
||||
nmap('<localleader>k', '<cmd>lprevious<cr>zz', { silent = true })
|
||||
|
||||
-- navigate diagnostics
|
||||
nmap('<leader>dj', require('fschauen.util').goto_next_diagnostic)
|
||||
nmap('<leader>dk', require('fschauen.util').goto_prev_diagnostic)
|
||||
nmap('<leader>dd', require('fschauen.util').toggle_diagnostics)
|
||||
nmap('<leader>do', require('fschauen.util').open_float_diagnostic)
|
||||
nmap('<leader>dh', require('fschauen.util').hide_diagnostics)
|
||||
|
||||
-- toggle quickfix and loclist
|
||||
nmap('<leader>ll', util.toggle_quickfix, { desc = 'Toggle quickfix' } )
|
||||
nmap('<localleader>ll', util.toggle_loclist, { desc = 'Toggle loclist' } )
|
||||
|
||||
-- quickly open lazy.nvim plugin manager
|
||||
nmap('<leader>L', '<cmd>Lazy<cr>')
|
||||
|
||||
-- toggle options
|
||||
local toggle_number = function()
|
||||
vim.wo.number = not vim.wo.number
|
||||
vim.wo.relativenumber = false
|
||||
|
@ -89,23 +11,113 @@ local toggle_relativenumber = function()
|
|||
vim.wo.number = vim.wo.relativenumber or vim.wo.number
|
||||
end
|
||||
|
||||
nmap('<leader>sn', toggle_number)
|
||||
nmap('<leader>sr', toggle_relativenumber)
|
||||
nmap('<leader>sl', '<cmd>set list! | set list?<CR>', { silent = true })
|
||||
nmap('<leader>sw', '<cmd>set wrap! | set wrap?<CR>', { silent = true })
|
||||
nmap('<leader>ss', '<cmd>set spell! | set spell?<CR>', { silent = true })
|
||||
M = {}
|
||||
|
||||
local base = {
|
||||
-- better navigation for wrapped lines
|
||||
{ 'j', 'gj' },
|
||||
{ 'k', 'gk' },
|
||||
|
||||
-- maintain cursor position when joining lines
|
||||
{ 'J', 'mzJ`z' },
|
||||
|
||||
-- retain selection when making changes in visual mode
|
||||
{ '<c-a>', '<c-a>gv', 'v' },
|
||||
{ '<c-x>', '<c-x>gv', 'v' },
|
||||
{ 'g<c-a>', 'g<c-a>gv', 'v' },
|
||||
{ 'g<c-x>', 'g<c-x>gv', 'v' },
|
||||
{ '>', '><cr>gv', 'v' },
|
||||
{ '<', '<<cr>gv', 'v' },
|
||||
|
||||
-- place destination of important movements in the center of the screen
|
||||
{ 'n', 'nzzzv' },
|
||||
{ 'N', 'Nzzzv' },
|
||||
{ '<c-d>', '<c-d>zzzv' },
|
||||
{ '<c-u>', '<c-u>zzzv' },
|
||||
|
||||
-- easier window navigation
|
||||
{ '<c-j>', '<c-w>j' },
|
||||
{ '<c-k>', '<c-w>k' },
|
||||
{ '<c-h>', '<c-w>h' },
|
||||
{ '<c-l>', '<c-w>l' },
|
||||
|
||||
-- window resizing
|
||||
{ '<s-Up>', window.resize_up(2), desc = 'Resize window upward' },
|
||||
{ '<s-Down>', window.resize_down(2), desc = 'Resize window downward' },
|
||||
{ '<s-Left>', window.resize_left(2), desc = 'Resize window leftward' },
|
||||
{ '<s-Right>', window.resize_right(2), desc = 'Resize window rightward' },
|
||||
|
||||
-- easy tab navigation
|
||||
{ '<Right>', '<cmd>tabnext<cr>' },
|
||||
{ '<Left>', '<cmd>tabprevious<cr>' },
|
||||
|
||||
-- move lines up and down
|
||||
{ '<c-a-j>', [[:move .+1<cr>==]] },
|
||||
{ '<c-a-k>', [[:move .-2<cr>==]] },
|
||||
{ '<c-a-j>', [[:move '>+1<cr>gv=gv]], 'v' },
|
||||
{ '<c-a-k>', [[:move '<-2<cr>gv=gv]], 'v' },
|
||||
{ '<c-a-j>', [[<esc>:move .+1<cr>==gi]], 'i' },
|
||||
{ '<c-a-k>', [[<esc>:move .-2<cr>==gi]], 'i' },
|
||||
|
||||
-- move to begin/end of line in insert mode
|
||||
{ '<c-a>', '<c-o>^', 'i' },
|
||||
{ '<c-e>', '<c-o>$', 'i' },
|
||||
|
||||
-- move to begin of line in command mode (<c-e> moves to end by default)
|
||||
{ '<c-a>', '<c-b>', 'c' },
|
||||
|
||||
-- navigate items in quickfix and location lists
|
||||
{ '<leader>j', '<cmd>cnext<cr>zz' },
|
||||
{ '<leader>k', '<cmd>cprevious<cr>zz' },
|
||||
{ '<localleader>j', '<cmd>lnext<cr>zz' },
|
||||
{ '<localleader>k', '<cmd>lprevious<cr>zz' },
|
||||
|
||||
-- navigate diagnostics
|
||||
{ '<leader>dj', diagnostic.goto_next },
|
||||
{ '<leader>dk', diagnostic.goto_prev },
|
||||
{ '<leader>dd', diagnostic.toggle },
|
||||
{ '<leader>do', diagnostic.open_float },
|
||||
{ '<leader>dh', diagnostic.hide },
|
||||
|
||||
-- toggle quickfix and loclist
|
||||
{ '<leader>ll', window.toggle_quickfix, desc = 'Toggle quickfix' },
|
||||
{ '<localleader>ll', window.toggle_loclist, desc = 'Toggle loclist' },
|
||||
|
||||
-- quickly open lazy.nvim plugin manager
|
||||
{ '<leader>L', '<cmd>Lazy<cr>' },
|
||||
|
||||
-- toggle options
|
||||
{ '<leader>sn', toggle_number },
|
||||
{ '<leader>sr', toggle_relativenumber },
|
||||
{ '<leader>sl', '<cmd>set list! | set list?<CR>' },
|
||||
{ '<leader>sw', '<cmd>set wrap! | set wrap?<CR>' },
|
||||
{ '<leader>ss', '<cmd>set spell! | set spell?<CR>' },
|
||||
|
||||
-- quickly change background
|
||||
nmap('<leader>bg', [[<cmd>let &background = &background ==? 'light' ? 'dark' : 'light'<cr>]])
|
||||
{ '<leader>bg', [[<cmd>let &background = &background ==? 'light' ? 'dark' : 'light'<cr>]] },
|
||||
|
||||
-- disable highlight until next search
|
||||
nmap('<esc>', '<cmd>nohlsearch<cr><esc>')
|
||||
imap('<esc>', '<cmd>nohlsearch<cr><esc>')
|
||||
{ '<leader>h', '<cmd>nohlsearch<cr><esc>' },
|
||||
|
||||
-- more convenient way of entering normal mode from terminal mode
|
||||
tmap([[<c-\><c-\>]], [[<c-\><c-n>]])
|
||||
{ [[<c-\><c-\>]], [[<c-\><c-n>]], 't' },
|
||||
|
||||
-- recall older/recent command-line from history
|
||||
cmap('<c-j>', '<down>')
|
||||
cmap('<c-k>', '<up>')
|
||||
{ '<c-j>', '<down>', 'c' },
|
||||
{ '<c-k>', '<up>', 'c' },
|
||||
}
|
||||
|
||||
local keymap_set = vim.keymap.set
|
||||
local map = function(opts)
|
||||
local lhs, rhs, mode = opts[1], opts[2], opts[3] or 'n'
|
||||
opts[1], opts[2], opts[3], opts.mode = nil, nil, nil, nil
|
||||
opts.silent = opts.silent ~= false
|
||||
keymap_set(mode, lhs, rhs, opts)
|
||||
end
|
||||
|
||||
M.setup = function()
|
||||
vim.tbl_map(map, base)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
local config = function()
|
||||
local cmp = require('cmp')
|
||||
local cmp = require 'cmp'
|
||||
local map = cmp.mapping
|
||||
|
||||
local flip = require('fschauen.util').flip
|
||||
local partial = require('fschauen.util').partial
|
||||
local fs = require 'fschauen'
|
||||
local flip, partial = fs.flip, fs.partial
|
||||
|
||||
-- assign('i', { key = func, ... }) == { key = { i = func }, ... }
|
||||
-- assign({'i', 'c'}, { key = func, ... }) == { key = { i = func, c = func }, ...}
|
||||
|
|
|
@ -56,8 +56,7 @@ local config = function()
|
|||
local window_is_wide = window_is_at_least(80)
|
||||
local window_is_medium = window_is_at_least(50)
|
||||
|
||||
local concat = require('fschauen.util').concat
|
||||
|
||||
local fs = require 'fschauen'
|
||||
local my = {
|
||||
paste = {
|
||||
function() return '' end,
|
||||
|
@ -93,7 +92,7 @@ local config = function()
|
|||
|
||||
status = {
|
||||
function()
|
||||
local flags = concat(
|
||||
local flags = fs.concat(
|
||||
vim.bo.modified and {'+'} or {},
|
||||
(vim.bo.readonly or not vim.bo.modifiable) and {'RO'} or {})
|
||||
return vim.fn.join(flags, ' ')
|
||||
|
@ -142,8 +141,8 @@ local config = function()
|
|||
|
||||
|
||||
local active_sections = vim.tbl_extend('force', inactive_sections, {
|
||||
lualine_a = concat({ my.paste, my.mode }, inactive_sections.lualine_a),
|
||||
lualine_x = concat({ 'diagnostics' }, inactive_sections.lualine_x),
|
||||
lualine_a = fs.concat({ my.paste, my.mode }, inactive_sections.lualine_a),
|
||||
lualine_x = fs.concat({ 'diagnostics' }, inactive_sections.lualine_x),
|
||||
})
|
||||
|
||||
require('lualine').setup {
|
||||
|
|
|
@ -23,6 +23,19 @@ if lazy then
|
|||
},
|
||||
ui = {
|
||||
border = 'rounded',
|
||||
performance = {
|
||||
rtp = {
|
||||
disabled_plugins = {
|
||||
'gzip',
|
||||
'matchit',
|
||||
'matchparen',
|
||||
'netrwPlugin',
|
||||
'tarPlugin',
|
||||
'tohtml',
|
||||
'tutor',
|
||||
'zipPlugin',
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
else
|
|
@ -55,6 +55,28 @@ local config_builtin = function(picker, opts)
|
|||
end
|
||||
end
|
||||
|
||||
---Preserve register contents over function call.
|
||||
---@param reg string: register to save, must be a valid register name.
|
||||
---@param func function: function that may freely clobber the register.
|
||||
---@return any: return value of calling `func`.
|
||||
local with_saved_register = function(reg, func)
|
||||
local saved = vim.fn.getreg(reg)
|
||||
local result = func()
|
||||
vim.fn.setreg(reg, saved)
|
||||
return result
|
||||
end
|
||||
|
||||
---Get selected text.
|
||||
---@return string: selected text, or work under cursor if not in visual mode.
|
||||
local get_selected_text = function()
|
||||
if vim.fn.mode() ~= 'v' then return vim.fn.expand '<cword>' end
|
||||
|
||||
return with_saved_register('v', function()
|
||||
vim.cmd [[noautocmd sil norm "vy]]
|
||||
return vim.fn.getreg 'v'
|
||||
end)
|
||||
end
|
||||
|
||||
M.pickers = setmetatable({
|
||||
all_files = config_builtin('find_files', {
|
||||
hidden = true,
|
||||
|
@ -73,7 +95,7 @@ M.pickers = setmetatable({
|
|||
}),
|
||||
selection = function(_)
|
||||
return function()
|
||||
local text = require('fschauen.util').get_selected_text()
|
||||
local text = get_selected_text()
|
||||
builtin().grep_string {
|
||||
prompt_title = string.format(' Grep: %s ', text),
|
||||
search = text,
|
||||
|
|
|
@ -1,197 +0,0 @@
|
|||
local M = {}
|
||||
|
||||
--- Flip function arguments.
|
||||
---
|
||||
--- flip(f)(a, b) == f(b, a)
|
||||
---
|
||||
---@param f function: function to flip.
|
||||
---@return function: function that takes `f`'s arguments flipped.
|
||||
M.flip = function(f)
|
||||
return function(a, b)
|
||||
return f(b, a)
|
||||
end
|
||||
end
|
||||
|
||||
--- Concatenate lists.
|
||||
---
|
||||
--- extend({'a', 'b'}, {'c', 'd'}) == {'a', 'b', 'c', 'd'}
|
||||
--- extend({1, 2}, {3, 4}, {5, 6}) == {1, 2, 3, 4, 5, 6}
|
||||
---
|
||||
---@param ... table: lists to concatenate.
|
||||
---@return table: concatenation of arguments.
|
||||
M.concat = function(...)
|
||||
local result = {}
|
||||
for _, tbl in ipairs {...} do
|
||||
for _, v in pairs(tbl) do
|
||||
result[#result+1] = v
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
--- Partial function application.
|
||||
---
|
||||
--- partial(f, x)(...) == f(x, ...)
|
||||
--- partial(f, x, y)(...) == f(x, y, ...)
|
||||
---
|
||||
---@param f function: function to partially apply.
|
||||
---@param ... any: arguments to bind.
|
||||
---@return function: partially applied function.
|
||||
M.partial = function(f, ...)
|
||||
local argv = {...}
|
||||
return function(...)
|
||||
return f(unpack(M.concat(argv, {...})))
|
||||
end
|
||||
end
|
||||
|
||||
--- Delayed function evaluation.
|
||||
---@param f function: function whose evaluation will be delayed.
|
||||
---@param ... any: arguments to `f`.
|
||||
---@return function: a new function that calls f with provided arguments.
|
||||
M.thunk = function(f, ...)
|
||||
local args = {...}
|
||||
return function()
|
||||
return f(unpack(args))
|
||||
end
|
||||
end
|
||||
|
||||
--- Preserve register contents over function call.
|
||||
---@param reg string: register to save, must be a valid register name.
|
||||
---@param func function: function that may freely clobber the register.
|
||||
---@return any: return value of calling `func`.
|
||||
M.with_saved_register = function(reg, func)
|
||||
local saved = vim.fn.getreg(reg)
|
||||
local result = func()
|
||||
vim.fn.setreg(reg, saved)
|
||||
return result
|
||||
end
|
||||
|
||||
--- Get selected text.
|
||||
---@return string: selected text, or work under cursor if not in visual mode.
|
||||
M.get_selected_text = function()
|
||||
if vim.fn.mode() ~= 'v' then return vim.fn.expand '<cword>' end
|
||||
|
||||
return M.with_saved_register('v', function()
|
||||
vim.cmd [[noautocmd sil norm "vy]]
|
||||
return vim.fn.getreg 'v'
|
||||
end)
|
||||
end
|
||||
|
||||
local diag_opts = {
|
||||
wrap = false, -- don't wrap around the begin/end of file
|
||||
-- float = {
|
||||
-- border = 'rounded' -- enable border for the floating window
|
||||
-- },
|
||||
}
|
||||
|
||||
--- Move to the next diagnostic.
|
||||
---@param opts table: options passed along to `vim.diagnostic.goto_next`.
|
||||
M.goto_next_diagnostic = function(opts)
|
||||
vim.diagnostic.goto_next(vim.tbl_extend('keep', opts or {}, diag_opts))
|
||||
vim.cmd 'normal zz'
|
||||
end
|
||||
|
||||
--- Move to the previous diagnostic.
|
||||
---@param opts table: options passed along to `vim.diagnostic.goto_prev`.
|
||||
M.goto_prev_diagnostic = function(opts)
|
||||
vim.diagnostic.goto_prev(vim.tbl_extend('keep', opts or {}, diag_opts))
|
||||
vim.cmd 'normal zz'
|
||||
end
|
||||
|
||||
M.open_float_diagnostic = function(opts)
|
||||
vim.diagnostic.open_float(opts)
|
||||
end
|
||||
|
||||
M.toggle_diagnostics = function(bufnr)
|
||||
bufnr = bufnr or 0
|
||||
if vim.diagnostic.is_disabled(bufnr) then
|
||||
vim.diagnostic.enable(bufnr)
|
||||
else
|
||||
vim.diagnostic.disable(bufnr)
|
||||
end
|
||||
end
|
||||
|
||||
M.hide_diagnostics = function(bufnr)
|
||||
vim.diagnostic.hide(nil, bufnr or 0)
|
||||
end
|
||||
|
||||
--- Whether the current window is the last in a given direction.
|
||||
---@param direction string: one of 'h', 'j', 'k', or 'l'
|
||||
local win_is_last = function(direction)
|
||||
local current = vim.api.nvim_get_current_win()
|
||||
vim.cmd('wincmd ' .. direction)
|
||||
local next = vim.api.nvim_get_current_win()
|
||||
|
||||
local is_last = current == next
|
||||
if not is_last then vim.cmd('wincmd p') end
|
||||
|
||||
return is_last
|
||||
end
|
||||
|
||||
--- Resize current window in a given direction.
|
||||
---@param dir string: one of 'h', 'j', 'k', or 'l'
|
||||
---@param size integer: how much to resize
|
||||
local win_resize = function(dir, size)
|
||||
if dir ~= 'h' and dir ~= 'j' and dir ~= 'k' and dir ~= 'l' then return end
|
||||
|
||||
size = math.abs(size)
|
||||
local is_height = dir == 'j' or dir == 'k'
|
||||
local is_positive = dir == 'j' or dir == 'l'
|
||||
|
||||
if win_is_last(is_height and 'j' or 'l') then
|
||||
is_positive = not is_positive
|
||||
end
|
||||
|
||||
local delta = string.format('%s%d', is_positive and '+' or '-', size)
|
||||
local prefix = is_height and '' or 'vertical '
|
||||
vim.cmd(prefix .. 'resize ' .. delta .. '<cr>')
|
||||
end
|
||||
|
||||
--- Resize current window upwards.
|
||||
---@param size integer: how much to resize
|
||||
M.win_resize_up = function(size) return M.thunk(win_resize, 'k', size) end
|
||||
|
||||
--- Resize current window downwards.
|
||||
---@param size integer: how much to resize
|
||||
M.win_resize_down = function(size) return M.thunk(win_resize, 'j', size) end
|
||||
|
||||
--- Resize current window leftwards.
|
||||
---@param size integer: how much to resize
|
||||
M.win_resize_left = function(size) return M.thunk(win_resize, 'h', size) end
|
||||
|
||||
--- Resize current window rightwards.
|
||||
---@param size integer: how much to resize
|
||||
M.win_resize_right = function(size) return M.thunk(win_resize, 'l', size) end
|
||||
|
||||
--- Toggle quickfix (or location) list.
|
||||
---@param qf string: 'c' for quickfix, 'l' for location list
|
||||
local toggle_qf_list = function(qf)
|
||||
local l = qf == 'l' and 1 or 0
|
||||
local is_qf = function(win) return win.quickfix == 1 and win.loclist == l end
|
||||
local is_open = not vim.tbl_isempty(vim.tbl_filter(is_qf, vim.fn.getwininfo()))
|
||||
if is_open then
|
||||
vim.cmd(qf .. 'close')
|
||||
else
|
||||
local ok = pcall(function(c) vim.cmd(c) end, qf .. 'open')
|
||||
if not ok and qf == 'l' then
|
||||
vim.notify('No location list', vim.log.levels.WARN)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Toggle quickfix list.
|
||||
M.toggle_quickfix = function() toggle_qf_list('c') end
|
||||
|
||||
--- Toggle location list.
|
||||
M.toggle_loclist = function() toggle_qf_list('l') end
|
||||
|
||||
|
||||
M.set_colorscheme = function(name)
|
||||
vim.cmd('silent! colorscheme ' .. name)
|
||||
if vim.v.errmsg ~= '' then
|
||||
vim.notify(string.format('Colorscheme %s not found!', name), vim.log.levels.WARN)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
94
config/nvim/lua/fschauen/window.lua
Normal file
94
config/nvim/lua/fschauen/window.lua
Normal file
|
@ -0,0 +1,94 @@
|
|||
M = {}
|
||||
|
||||
---Whether the current window is the last in a given direction.
|
||||
---@param direction string: one of 'h', 'j', 'k', or 'l'
|
||||
local is_last = function(direction)
|
||||
local current = vim.api.nvim_get_current_win()
|
||||
vim.cmd('wincmd ' .. direction)
|
||||
local next = vim.api.nvim_get_current_win()
|
||||
|
||||
local is_last = current == next
|
||||
if not is_last then vim.cmd('wincmd p') end
|
||||
|
||||
return is_last
|
||||
end
|
||||
|
||||
---Resize current window in a given direction.
|
||||
---@param dir string: one of 'h', 'j', 'k', or 'l'
|
||||
---@param size integer: how much to resize
|
||||
local resize = function(dir, size)
|
||||
if dir ~= 'h' and dir ~= 'j' and dir ~= 'k' and dir ~= 'l' then return end
|
||||
|
||||
size = math.abs(size)
|
||||
local is_height = dir == 'j' or dir == 'k'
|
||||
local is_positive = dir == 'j' or dir == 'l'
|
||||
|
||||
if is_last(is_height and 'j' or 'l') then
|
||||
is_positive = not is_positive
|
||||
end
|
||||
|
||||
local delta = string.format('%s%d', is_positive and '+' or '-', size)
|
||||
local prefix = is_height and '' or 'vertical '
|
||||
vim.cmd(prefix .. 'resize ' .. delta .. '<cr>')
|
||||
end
|
||||
|
||||
---Resize current window upwards.
|
||||
---@param size integer: how much to resize
|
||||
M.resize_up = function(size)
|
||||
return function()
|
||||
resize('k', size)
|
||||
end
|
||||
end
|
||||
|
||||
---Resize current window downwards.
|
||||
---@param size integer: how much to resize
|
||||
M.resize_down = function(size)
|
||||
return function()
|
||||
resize('j', size)
|
||||
end
|
||||
end
|
||||
|
||||
---Resize current window leftwards.
|
||||
---@param size integer: how much to resize
|
||||
M.resize_left = function(size)
|
||||
return function()
|
||||
resize('h', size)
|
||||
end
|
||||
end
|
||||
|
||||
---Resize current window rightwards.
|
||||
---@param size integer: how much to resize
|
||||
M.resize_right = function(size)
|
||||
return function()
|
||||
resize('l', size)
|
||||
end
|
||||
end
|
||||
|
||||
---Toggle quickfix (or location) list.
|
||||
---@param qf string: 'c' for quickfix, 'l' for location list
|
||||
local toggle_list = function(qf)
|
||||
local l = qf == 'l' and 1 or 0
|
||||
local is_qf = function(win) return win.quickfix == 1 and win.loclist == l end
|
||||
local is_open = not vim.tbl_isempty(vim.tbl_filter(is_qf, vim.fn.getwininfo()))
|
||||
if is_open then
|
||||
vim.cmd(qf .. 'close')
|
||||
else
|
||||
local ok = pcall(function(c) vim.cmd(c) end, qf .. 'open')
|
||||
if not ok and qf == 'l' then
|
||||
vim.notify('No location list', vim.log.levels.WARN)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Toggle quickfix list.
|
||||
M.toggle_quickfix = function()
|
||||
toggle_list('c')
|
||||
end
|
||||
|
||||
---Toggle location list.
|
||||
M.toggle_loclist = function()
|
||||
toggle_list('l')
|
||||
end
|
||||
|
||||
return M
|
||||
|
Loading…
Add table
Reference in a new issue