From 4e4388182966791a6202874eb51adfee42208fd1 Mon Sep 17 00:00:00 2001 From: Anton Date: Sat, 5 Jul 2025 07:52:05 +0200 Subject: [PATCH] chore(nvim): clean config --- .config/nvim/init.lua | 42 ++--- .config/nvim/lua/BluePlum/commands.lua | 22 +++ .config/nvim/lua/BluePlum/init.lua | 3 + .config/nvim/lua/BluePlum/input.lua | 248 ------------------------- .config/nvim/lua/BluePlum/keymap.lua | 164 ++++++++-------- .config/nvim/lua/BluePlum/lazy.lua | 32 ++++ .config/nvim/lua/BluePlum/neovide.lua | 24 --- .config/nvim/lua/BluePlum/set.lua | 130 ++++++++----- 8 files changed, 229 insertions(+), 436 deletions(-) create mode 100644 .config/nvim/lua/BluePlum/commands.lua create mode 100644 .config/nvim/lua/BluePlum/init.lua delete mode 100644 .config/nvim/lua/BluePlum/input.lua create mode 100644 .config/nvim/lua/BluePlum/lazy.lua delete mode 100644 .config/nvim/lua/BluePlum/neovide.lua diff --git a/.config/nvim/init.lua b/.config/nvim/init.lua index d3ab722..c707fab 100644 --- a/.config/nvim/init.lua +++ b/.config/nvim/init.lua @@ -1,31 +1,13 @@ --- Lazy bootstrap -local lazypath = vim.fn.stdpath('data') .. '/lazy/lazy.nvim' -if not (vim.uv or vim.loop).fs_stat(lazypath) then - local lazyrepo = 'https://github.com/folke/lazy.nvim.git' - local out = vim.fn.system({ 'git', 'clone', '--filter=blob:none', '--branch=stable', lazyrepo, lazypath }) - if vim.v.shell_error ~= 0 then - vim.api.nvim_echo({ - { 'Failed to clone lazy.nvim:\n', 'ErrorMsg' }, - { out, 'WarningMsg' }, - { '\nPress any key to exit...' }, - }, true, {}) - vim.fn.getchar() - os.exit(1) - end +local lazy = require('BluePlum.lazy').bootstrap() + +require('BluePlum') + +if lazy then + lazy.setup({ + spec = { + { import = 'plugins' }, + }, + checker = { enabled = false }, + change_detection = { notify = false }, + }) end -vim.opt.rtp:prepend(lazypath) - -require('BluePlum.set') -require('BluePlum.keymap') -require('BluePlum.neovide') - -require('lazy').setup({ - -- Plugins - spec = { - { import = 'plugins' }, - }, - checker = { enabled = false }, - change_detection = { notify = false }, -}) - -require('BluePlum.input') diff --git a/.config/nvim/lua/BluePlum/commands.lua b/.config/nvim/lua/BluePlum/commands.lua new file mode 100644 index 0000000..35c9509 --- /dev/null +++ b/.config/nvim/lua/BluePlum/commands.lua @@ -0,0 +1,22 @@ +vim.api.nvim_create_user_command('Dot', 'edit ~/.config/nvim', {}) + +vim.cmd.cnoreabbrev('Git', 'GIt') +vim.api.nvim_create_user_command('GIt', function(ctx) + local subcommand = ctx.fargs[1] + if subcommand == 'log' then + local argv = table.concat({ + 'log', + '--graph', + '--abbrev-commit', + '--decorate', + }, ' ') + + local sliced = vim.list_slice(ctx.fargs, 2) + vim.cmd.Git(argv .. ' ' .. table.concat(sliced, ' ')) + return + end + + vim.cmd.Git(table.concat(ctx.fargs, ' ')) +end, { + nargs = '+', +}) diff --git a/.config/nvim/lua/BluePlum/init.lua b/.config/nvim/lua/BluePlum/init.lua new file mode 100644 index 0000000..d5ed97f --- /dev/null +++ b/.config/nvim/lua/BluePlum/init.lua @@ -0,0 +1,3 @@ +require('BluePlum.set') +require('BluePlum.keymap') +require('BluePlum.commands') diff --git a/.config/nvim/lua/BluePlum/input.lua b/.config/nvim/lua/BluePlum/input.lua deleted file mode 100644 index 055f186..0000000 --- a/.config/nvim/lua/BluePlum/input.lua +++ /dev/null @@ -1,248 +0,0 @@ -local M = { - interupt = false, -} - -M.Keys = { - enter = '', - tab = '', - backspace = '', - escape = '', - - -- if await_key().is_arrow - -- if key == Keys.left - left = { is_arrow = true, this = '' }, - right = { is_arrow = true, this = '' }, - up = { is_arrow = true, this = '' }, - down = { is_arrow = true, this = '' }, -} - -function M.await_key() - -- local char = vim.fn.getchar() - local char = vim.fn.getchar() - local num = vim.fn.nr2char(char) - local key = vim.fn.keytrans(num) - -- vim.print(char, num, key, key == '') - - -- Some special keys work weird.. - local is_key = function(key) - return pcall(function(char) - if vim.fn.keytrans(char) ~= key then - error() - end - end, char) - end - - if is_key('') then - return M.Keys.backspace - elseif is_key('') then - return M.Keys.left - elseif is_key('') then - return M.Keys.right - elseif is_key('') then - return M.Keys.up - elseif is_key('') then - return M.Keys.down - elseif key == '' then - return ' ' - elseif key == '' then - return '<' - elseif key == '' then - return '>' - end - - return key -end - --- vim.print(vim.fn.getcompletion('ech', 'command')) --- vim.fn.getchar() - -local function refresh_completions(pattern, kind) - local completions = vim.fn.getcompletion(pattern, kind) - - if #completions > 25 then - local new = {} - for i = 1, 25 do - new[i] = completions[i] - end - new[#new + 1] = '...' -- Append truncation indicator - return new - end - - return completions -end - -local function format_completions(completions) - local formatted = { { '{', 'Normal' } } - for i = 1, #completions do - table.insert(formatted, { completions[i], i == 1 and 'CursorLineNr' or 'Normal' }) - if i ~= #completions then - table.insert(formatted, { ', ', 'Normal' }) - end - end - table.insert(formatted, { '}', 'Normal' }) - return formatted -end - -local function input(prompt, default, completion) - vim.validate({ - prompt = { prompt, 'table' }, - default = { default, { 'string', 'nil' } }, - completion = { completion, { 'string', 'nil' } }, - }) - local using_default = default ~= nil - local typed = default or '' - - local completions = {} - - local cursor = 0 - - while true do - if completion ~= nil then - completions = refresh_completions(typed, completion) - end - - vim.cmd('echo | redraw') - vim.api.nvim_echo({ - unpack(prompt), - { string.sub(typed, 0, cursor), using_default and 'Visual' or 'Normal' }, - cursor == #typed and { '█', 'CursorLineNr' } or { string.sub(typed, cursor + 1, cursor + 1), 'Search' }, - { string.sub(typed, cursor + 2), 'Normal' }, - unpack(#completions > 0 and format_completions(completions) or {}), - }, false, {}) - local input = M.await_key() - - if input == M.Keys.enter or input == M.Keys.escape then - vim.cmd('echo | redraw') - return input == M.Keys.enter, typed - elseif input == M.Keys.backspace then - if cursor <= 0 then - goto continue - end - typed = string.sub(typed, 0, cursor - 1) .. string.sub(typed, cursor + 1) - cursor = cursor - 1 - goto continue - elseif input.is_arrow then - if input == M.Keys.left then - if cursor > 0 then - cursor = cursor - 1 - end - elseif input == M.Keys.right then - if cursor < #typed then - cursor = cursor + 1 - end - end - -- vim.api.nvim_win_set_cursor(0, { 1, 10 }) - goto continue - elseif input == M.Keys.tab then - if #completions == 0 then - goto continue - end - cursor = #typed - - local insert = completions[1] - local offset = #typed - - -- Find the largest overlapping slice - while offset > 0 and not vim.startswith(insert, string.sub(typed, -offset)) do - offset = offset - 1 - end - - -- Replace `typed` with the completion - typed = string.sub(typed, 1, #typed - offset) .. insert .. ' ' - cursor = #typed - goto continue - end - - typed = string.sub(typed, 0, cursor) .. input .. string.sub(typed, cursor + 1) - cursor = cursor + 1 - - ::continue:: - end -end - --- Safe wrapper for hiding the cursor (also captures ) -function M.input(prompt, default, completion) - local hl = vim.api.nvim_get_hl(0, { name = 'Cursor' }) - - hl.blend = 100 - vim.api.nvim_set_hl(0, 'Cursor', hl) - - -- local result, success, typed = pcall(input, prompt, defaut, completion) - local success, typed = input(prompt, default, completion) - vim.cmd('echo | redraw') - - hl.blend = 0 - vim.api.nvim_set_hl(0, 'Cursor', hl) - - return success, typed, not result -end - ----@param opts? table ----@param on_confirm function -vim.ui.input = function(opts, on_confirm) - vim.validate({ - opts = { opts, 'table' }, - on_confirm = { on_confirm, 'function' }, - }) - - opts = opts or {} - on_confirm = on_confirm or function() end - - vim.validate({ - -- Also supports list of HlGroup tuples - prompt = { opts.prompt, { 'table', 'string', 'nil' } }, - - default = { opts.default, { 'string', 'nil' } }, - completion = { opts.completion, { 'string', 'nil' } }, - - highlight = { - opts.highlight, - function(it) - return it == nil - end, - 'nil (not supported)', - }, - }) - - local prompt = opts.prompt or { { 'Enter input > ', 'Question' } } - if type(prompt) == 'string' then - -- Strip invalid characters - prompt = vim.trim(prompt) - - local is_alpha = function(char) - local num = vim.fn.char2nr(char) - return (num >= 65 and num <= 90) or (num >= 97 and num <= 122) - end - - local last = string.sub(prompt, #prompt) - while not is_alpha(last) do - prompt = string.sub(prompt, 0, #prompt - 1) - prompt = vim.trim(prompt) - last = string.sub(prompt, #prompt) - end - - prompt = { { prompt .. ' > ', 'Question' } } - end - - local finished, typed, interrupted = M.input(prompt, opts.default, opts.completion) - if interrupted then - return - end - on_confirm(finished and typed or nil) -end - --- vim.ui.input({ completion = 'cmdline' }, function(a) --- vim.print(a) --- end) - --- vim.keymap.set('n', ';', function() --- vim.ui.input({ prompt = { { ':', 'Normal' } }, completion = 'cmdline' }, function(command) --- vim.print(command) --- if command then --- vim.cmd(command) --- end --- end) --- end) --- add cmdheight to the input function - -return M diff --git a/.config/nvim/lua/BluePlum/keymap.lua b/.config/nvim/lua/BluePlum/keymap.lua index 1230e63..951b55d 100644 --- a/.config/nvim/lua/BluePlum/keymap.lua +++ b/.config/nvim/lua/BluePlum/keymap.lua @@ -1,93 +1,87 @@ -local set = vim.keymap.set +--- Defaults to `{'n'}` +--- @alias Mode 'n'|'v'|'Q'|'t'|'i'|'c' +--- @alias Mapping string|fun(): nil -set('i', '', ':move-2i') -set('i', '', ':move+i') +--- @type table +local keymap = { + move_lines = { + [''] = ':move-2i', + [''] = ':move+i', + modes = { 'i' }, + }, + half_screen_scroll = { + [''] = 'zz', + [''] = 'zz', + }, + clipboard_yank = { + ['y'] = '"+y', + modes = { 'n', 'v' }, + }, + terminal_escape = { + [''] = '', + modes = { 't' }, + }, + terminal = { + ['t'] = function() + OpenTerminal = OpenTerminal or nil -set('n', '', 'zz') -set('n', '', 'zz') + local open_buf = vim.api.nvim_get_current_buf() --- yank to clipboard with leader -set('n', 'y', '"+y') -set('v', 'y', '"+y') + if PreviousBuffer and open_buf == OpenTerminal then + vim.api.nvim_set_current_buf(PreviousBuffer) + PreviousBuffer = nil + return + end -set('n', 'Q', '') + PreviousBuffer = open_buf --- terminal --- set('n', 't', ':split:term') -OpenTerminal = nil -PreviousBuffer = -1 -set('n', 't', function() - if vim.api.nvim_get_current_buf() == OpenTerminal and PreviousBuffer ~= -1 then - vim.api.nvim_set_current_buf(PreviousBuffer) - PreviousBuffer = -1 - return - end + if OpenTerminal then + vim.api.nvim_set_current_buf(OpenTerminal) + return + end - PreviousBuffer = vim.api.nvim_get_current_buf() - if OpenTerminal ~= nil then - vim.api.nvim_set_current_buf(OpenTerminal) - else - vim.cmd.term() - OpenTerminal = vim.api.nvim_get_current_buf() - vim.api.nvim_create_autocmd({ 'BufDelete' }, { - callback = function(args) - if args.buf == OpenTerminal then + vim.cmd.term() + OpenTerminal = vim.api.nvim_get_current_buf() + vim.api.nvim_create_autocmd({ 'BufDelete' }, { + callback = function() OpenTerminal = nil - end - end, - }) + end, + buffer = OpenTerminal, + }) + end, + }, + goto_last_buffer = { + ['l'] = ':e#', + }, + scratch_pad = { + ['s'] = table.concat({ + ':bo vs', + ':enew', + ':setlocal noswapfile', + ':setlocal bufhidden=hide', + ':set filetype=markdown', + ':set syntax=markdown', + '', + }, ''), + }, + window_navigation = { + [''] = 'q', + [''] = '', + [''] = '', + [''] = '', + [''] = '', + }, +} + +for _, tbl in pairs(keymap) do + for key, value in pairs(tbl) do + if key ~= 'modes' then + local modes = tbl.modes --[[ @as Mode[] ]] + or { 'n' } + for _, mode in ipairs(modes) do + --- @cast value Mapping + vim.keymap.set(mode, key, value) + end + end end -end) -set('t', '', '') - --- nice additions -set('n', 'er', function() - vim.diagnostic.open_float() -end) -set('n', 'l', ':e#') --- set('n', 'ca', ':CodeActionMenu') --- set('n', 'cn', ':IncRename ') - -set('n', 'i', 'cc') --- peek -set('n', 'p', vim.lsp.buf.hover) - --- Scratch pad -set( - 'n', - 's', - ':bo vs:enew:setlocal noswapfile:setlocal bufhidden=hide:setlocal buftype=nofile:set filetype=markdown:set syntax=markdown' -) - --- Help --- vim.cmd.cnoreabbrev('h vert h') --- set('n', 'h', ':h ') - --- Comp mode -set('n', 'c', ':Compile') -set('n', 'cs', ':CompileInterrupt') - -set('n', 'E', ':NextError') - --- Windows -set('n', '', 'q') -set('n', '', '') -set('n', '', '') -set('n', '', '') -set('n', '', '') - -set('n', '', 'tH') - -Fullscreen = false -set('n', '', function() - if Fullscreen then - print('full screen') - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('_|', true, false, true), 'm', false) - else - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('=', true, false, true), 'm', false) - end - Fullscreen = not Fullscreen -end) - --- Generic -set('n', 'q', ':bd!') +end diff --git a/.config/nvim/lua/BluePlum/lazy.lua b/.config/nvim/lua/BluePlum/lazy.lua new file mode 100644 index 0000000..2ab2251 --- /dev/null +++ b/.config/nvim/lua/BluePlum/lazy.lua @@ -0,0 +1,32 @@ +--- @class Lazy +--- @field setup fun(opts: table) + +local M = {} + +local lazypath = vim.fn.stdpath('data') .. '/lazy/lazy.nvim' + +--- @nodiscard +--- @return Lazy? +function M.bootstrap() + --- @type { fs_stat: fun(path: string): boolean } + local fs = vim.uv or vim.loop + if fs.fs_stat(lazypath) then + vim.opt.rtp:prepend(lazypath) + return require('lazy') + end + + local lazyrepo = 'https://github.com/folke/lazy.nvim.git' + local out = vim.fn.system({ 'git', 'clone', '--filter=blob:none', '--branch=stable', lazyrepo, lazypath }) + + if vim.v.shell_error ~= 0 then + vim.api.nvim_echo({ + { 'Failed to clone lazy.nvim:\n', 'ErrorMsg' }, + { out, 'WarningMsg' }, + }, true, {}) + return nil + end + + return require('lazy') +end + +return M diff --git a/.config/nvim/lua/BluePlum/neovide.lua b/.config/nvim/lua/BluePlum/neovide.lua deleted file mode 100644 index f10cf6b..0000000 --- a/.config/nvim/lua/BluePlum/neovide.lua +++ /dev/null @@ -1,24 +0,0 @@ -if not vim.g.neovide then - return -end - -vim.o.guifont = 'Jetbrains Mono:h17' - -vim.g.neovide_underline_stroke_scale = 2.0 -vim.g.neovide_theme = 'dark' -vim.g.neovide_refresh_rate_idle = 10 -vim.g.neovide_transparency = 0.6 - --- Scroll -vim.g.neovide_scroll_animation_length = 0.1 -vim.g.neovide_scroll_animation_far_lines = 50 - --- Cursor -vim.g.neovide_cursor_animation_length = 0.03 -vim.g.neovide_cursor_trail_size = 0.5 -vim.g.neovide_cursor_smooth_blink = true -vim.cmd.set('guicursor=i-v-c:blinkwait400-blinkoff350-blinkon250,i-ci-ve:ver25,r-cr:hor20,o:hor20') - --- Pasting -vim.keymap.set('i', '', '+') -vim.keymap.set('n', '', '"+p') diff --git a/.config/nvim/lua/BluePlum/set.lua b/.config/nvim/lua/BluePlum/set.lua index a242b08..38e4c7e 100644 --- a/.config/nvim/lua/BluePlum/set.lua +++ b/.config/nvim/lua/BluePlum/set.lua @@ -1,64 +1,96 @@ --- Tabs for indenting -vim.opt.autoindent = true -vim.g.noexpandtab = true -vim.opt.tabstop = 4 -vim.opt.shiftwidth = 4 +local opts = { + mapleader = ' ', --- Line numbers -vim.opt.number = true -vim.opt.relativenumber = true -vim.opt.cursorline = true -vim.opt.cursorlineopt = 'number' + linenr = { + number = true, + relativenumber = true, + + cursorline = true, + cursorlineopt = 'number', + }, + + -- Use tabs for indents + indent = { + autoindent = true, + noexpandtab = true, + tabstop = 4, + shiftwidth = 4, + }, + + terminal = { + termguicolors = true, + shell = '/bin/fish', + }, + + split = { + splitright = true, + splitbelow = true, + }, + + search = { + hlsearch = false, + incsearch = true, + }, + + foldlevelstart = 99, +} + +--- @param tbl table +local function apply(tbl) + for key, value in pairs(tbl) do + if type(value) == 'table' then + apply(value) + else + if vim.fn.exists('&' .. key) == 1 then + vim.opt[key] = value + else + vim.g[key] = value + end + end + end +end + +apply(opts) + +-- Tabs for indenting +-- vim.opt.autoindent = true +-- vim.g.noexpandtab = true +-- vim.opt.tabstop = 4 +-- vim.opt.shiftwidth = 4 +-- +-- -- Line numbers +-- vim.opt.number = true +-- vim.opt.relativenumber = true +-- vim.opt.cursorline = true +-- vim.opt.cursorlineopt = 'number' -- Folding -vim.opt.foldlevelstart = 99 +-- vim.opt.foldlevelstart = 99 -vim.g.mapleader = ' ' +-- vim.g.mapleader = ' ' -- Terminal -vim.g.termguicolors = true -vim.opt.shell = '/bin/fish' +-- vim.g.termguicolors = true +-- vim.opt.shell = '/bin/fish' -- LSP -vim.lsp.inlay_hint.enable() +-- vim.lsp.inlay_hint.enable() -- Splitting --- vim.cmd.set('splitbelow') --- vim.cmd.set('splitright') -vim.opt.splitright = true -vim.opt.splitbelow = true +-- vim.opt.splitright = true +-- vim.opt.splitbelow = true -- Make text readable -vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { - pattern = { '*.md', '*.typ' }, - command = 'setlocal linebreak', -}) +-- vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { +-- pattern = { '*.md', '*.typ' }, +-- command = 'setlocal linebreak', +-- }) -vim.api.nvim_create_autocmd('FileType', { - pattern = 'help', - command = 'setlocal linebreak', -}) - -vim.cmd.cnoreabbrev('grep', 'Grepper') +-- vim.api.nvim_create_autocmd('FileType', { +-- pattern = 'help', +-- command = 'setlocal linebreak', +-- }) -- Search -vim.opt.hlsearch = false -vim.opt.incsearch = true - --- Make dotfile navigation bareble -vim.api.nvim_create_user_command('Dot', 'edit ~/.config/nvim', {}) - --- Git -vim.api.nvim_create_user_command('Glog', function() - local args = { - 'log', - '--graph', - '--abbrev-commit', - '--decorate', - } - local str = '' - for _, arg in pairs(args) do - str = str .. ' ' .. arg - end - vim.cmd.Git(str) -end, {}) +-- vim.opt.hlsearch = false +-- vim.opt.incsearch = true