From d54f38cf684a0d2805f3c171148a14d939913cb3 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sat, 23 Mar 2024 14:12:42 +0000 Subject: [PATCH] Move more stuff to Lua --- home-manager/home.nix | 5 ++ home-manager/nvim/init.lua | 102 ++++++++++++++++++++++++++++++ home-manager/nvim/init.vim | 106 -------------------------------- home-manager/nvim/which-key.lua | 88 ++++++++++++++++++++++++++ 4 files changed, 195 insertions(+), 106 deletions(-) create mode 100644 home-manager/nvim/which-key.lua diff --git a/home-manager/home.nix b/home-manager/home.nix index 9790ca5..cb9baad 100644 --- a/home-manager/home.nix +++ b/home-manager/home.nix @@ -148,6 +148,11 @@ in { enable = true; plugins = [ + { + plugin = nixpkgs.vimPlugins.which-key-nvim; + type = "lua"; + config = builtins.readFile ./nvim/which-key.lua; + } { plugin = nixpkgs.vimPlugins.tokyonight-nvim; config = builtins.readFile ./nvim/tokyonight.lua; diff --git a/home-manager/nvim/init.lua b/home-manager/nvim/init.lua index 5ca4e79..83c4047 100644 --- a/home-manager/nvim/init.lua +++ b/home-manager/nvim/init.lua @@ -1 +1,103 @@ vim.g.python3_host_prog = "%PYTHONENV%/bin/python" +vim.opt.mouse = "" +vim.opt.history = 500 +vim.opt.background = "dark" + +vim.opt.langmenu = "en" + +-- Always show current position +vim.opt.ruler = true +vim.opt.number = true + +-- A bit of extra margin to the left +vim.opt.foldcolumn = "1" + +vim.opt.autoread = true +vim.opt.backup = false +vim.opt.swapfile = false + +-- Use spaces instead of tabs +vim.opt.expandtab = true +vim.opt.smarttab = true +vim.opt.shiftwidth = 4 +vim.opt.tabstop = 4 + +vim.opt.hlsearch = true + +-- Don't redraw while executing macros +vim.opt.lazyredraw = true + +-- Show matching brackets when text indicator is on one of them +vim.opt.showmatch = true +vim.opt.mat = 2 + +-- Turn off sound +vim.opt.errorbells = false +vim.opt.visualbell = false + +vim.api.nvim_set_keymap('n', ';', '', { noremap = true }) +vim.api.nvim_set_var("maplocalleader", ";") + +function MarkdownPreview() + local temp_file = vim.fn.tempname() .. ".md" + local file_name = vim.fn.substitute(vim.fn.tolower(vim.fn.expand('%:t')), '\\W', '_', 'g') + local temp_html = "/tmp/" .. file_name .. "_tmp.html" + + -- Write the current buffer to the temp file + vim.cmd('write! ' .. temp_file) + + local pandoc_cmd = 'pandoc ' .. temp_file .. ' -o ' .. temp_html + + -- Execute the pandoc command + vim.fn.system(pandoc_cmd) + + -- Use tmux and lynx to preview the HTML file + local lynx_cmd = 'tmux split-window -h lynx ' .. temp_html + vim.fn.jobstart(vim.split(lynx_cmd, ' '), {silent=true}) + + -- Delete the temp markdown file + vim.fn.delete(temp_file, 'rf') +end + +function RemoveCarriageReturn() + vim.cmd("mark m") + vim.cmd("normal! Hmt") + vim.cmd("%s/\r//ge") + vim.cmd("normal! 'tzt'm") +end + +function FormatJson() + local curpos = vim.api.nvim_win_get_cursor(0) + vim.cmd("%!python -m json.tool") + vim.api.nvim_win_set_cursor(0, curpos) +end + +function ChangeToCurrentDirectory() + vim.cmd(":cd %:p:h") + vim.cmd(":pwd") +end + +local status, whichkey = pcall(require, "which-key") +if status then + whichkey.register({ + ["mp"] = { + MarkdownPreview, "Preview Markdown in Lynx" + }, + ["md"] = { + RemoveCarriageReturns, "Delete carriage returns from file" + }, + ["j"] = { + FormatJson, "Auto-format JSON" + }, + ["cd"] = { + ChangeToCurrentDirectory, "Switch CWD to the directory of the open buffer" + } + }, { prefix = vim.api.nvim_get_var("maplocalleader") }) +else + vim.api.nvim_set_keymap('n', 'mp', ':lua MarkdownPreview()', { noremap = true, silent = true }) + -- Remove the Windows ^M - when the encodings gets messed up + vim.api.nvim_set_keymap('n', 'md', ':lua RemoveCarriageReturn()', { noremap=true }) + vim.api.nvim_set_keymap('n', 'j', ':lua FormatJson()', { noremap=true }) + vim.api.nvim_set_keymap('n', 'cd', ':lua ChangeToCurrentDirectory()', { noremap=true }) +end + diff --git a/home-manager/nvim/init.vim b/home-manager/nvim/init.vim index 11101be..c385fe4 100644 --- a/home-manager/nvim/init.vim +++ b/home-manager/nvim/init.vim @@ -1,7 +1,3 @@ -set nu - -set mouse= - """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " Maintainer: " Amir Salihefendic — @amix3k @@ -34,21 +30,13 @@ set mouse= """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " => General """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" Sets how many lines of history VIM has to remember -set history=500 - " Enable filetype plugins filetype plugin on filetype indent on -" Set to auto read when a file is changed from the outside -set autoread - " With a map leader it's possible to do extra key combinations " like w saves the current file let mapleader = "`" -noremap ";" -let maplocalleader = ";" """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " => VIM user interface @@ -73,9 +61,6 @@ else set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.DS_Store endif -"Always show current position -set ruler - " Height of the command bar set cmdheight=2 @@ -88,27 +73,13 @@ set ignorecase " When searching try to be smart about cases set smartcase -" Highlight search results -set hlsearch - " Makes search act like search in modern browsers set incsearch -" Don't redraw while executing macros (good performance config) -set lazyredraw - " For regular expressions turn magic on set magic -" Show matching brackets when text indicator is over them -set showmatch -" How many tenths of a second to blink when matching brackets -set mat=2 - " No annoying sound on errors -set noerrorbells -set novisualbell -set t_vb= set tm=500 " Properly disable sound on errors on MacVim @@ -116,11 +87,6 @@ if has("gui_macvim") autocmd GUIEnter * set vb t_vb= endif - -" Add a bit extra margin to the left -set foldcolumn=1 - - """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " => Colors and Fonts """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" @@ -132,8 +98,6 @@ if $COLORTERM == 'gnome-terminal' set t_Co=256 endif -set background=dark - " Set extra options when running in GUI mode if has("gui_running") set guioptions-=T @@ -152,25 +116,11 @@ set ffs=unix """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " => Files, backups and undo """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" Turn backup off, since most stuff is in SVN, git et.c anyway... -set nobackup set nowb -set noswapfile - """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " => Text, tab and indent related """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" Use spaces instead of tabs -set expandtab - -" Be smart when using tabs ;) -set smarttab - -" 1 tab == 4 spaces -set shiftwidth=4 -set tabstop=4 - " Linebreak on 500 characters set lbr set tw=500 @@ -183,29 +133,6 @@ set wrap "Wrap lines " => Moving around, tabs, windows and buffers """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" Disable highlight when is pressed -map :noh - -" Useful mappings for managing tabs -map tn :tabnew -map to :tabonly -map tc :tabclose -map tm :tabmove -map t :tabnext - -" Let 'tl' toggle between this and the last accessed tab -let g:lasttab = 1 -nmap tl :exe "tabn ".g:lasttab -au TabLeave * let g:lasttab = tabpagenr() - - -" Opens a new tab with the current buffer's path -" Super useful when editing files in the same directory -map te :tabedit =expand("%:p:h")/ - -" Switch CWD to the directory of the open buffer -map cd :cd %:p:h:pwd - " Specify the behavior when switching between buffers try set switchbuf=useopen,usetab,newtab @@ -255,14 +182,6 @@ map sp [s map sa zg map s? z= - -""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" => Misc -""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" Remove the Windows ^M - when the encodings gets messed up -noremap m mmHmt:%s///ge'tzt'm - - """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " => Helper functions """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" @@ -316,33 +235,8 @@ function! VisualSelection(direction, extra_filter) range let @" = l:saved_reg endfunction -nnoremap c :!cargo clippy -nnoremap j :%!python -m json.tool - set statusline+=%#warningmsg# set statusline+=%{SyntasticStatuslineFlag()} set statusline+=%* " Format the status line set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l\ \ Column:\ %c - -set fileformat=unix -set fileformats=unix - -function MarkdownPreview() - let temp_file = tempname() . ".md" - let file_name = substitute(tolower(expand('%:t')), '\W', '_', 'g') - let temp_html = "/tmp/" . file_name . "_tmp.html" - - execute 'write! ' . temp_file - - let pandoc_cmd = 'pandoc ' . temp_file . ' -o ' . temp_html - - call system(pandoc_cmd) - - " Use tmux and lynx to preview the HTML file - let lynx_cmd = 'tmux split-window -h lynx ' . temp_html - execute "silent call jobstart(split('" . lynx_cmd . "', ' '))" - - silent! execute "call delete('" . temp_file . "')" -endfunction -nnoremap mp :call MarkdownPreview() diff --git a/home-manager/nvim/which-key.lua b/home-manager/nvim/which-key.lua new file mode 100644 index 0000000..5de907d --- /dev/null +++ b/home-manager/nvim/which-key.lua @@ -0,0 +1,88 @@ +require('which-key').setup({ +plugins = { + marks = true, -- shows a list of your marks on ' and ` + registers = true, -- shows your registers on " in NORMAL or in INSERT mode + -- the presets plugin, adds help for a bunch of default keybindings in Neovim + -- No actual key bindings are created + spelling = { + enabled = true, -- enabling this will show WhichKey when pressing z= to select spelling suggestions + suggestions = 20, -- how many suggestions should be shown in the list? + }, + presets = { + operators = true, -- adds help for operators like d, y, ... + motions = true, -- adds help for motions + text_objects = true, -- help for text objects triggered after entering an operator + windows = true, -- default bindings on + nav = true, -- misc bindings to work with windows + z = true, -- bindings for folds, spelling and others prefixed with z + g = true, -- bindings for prefixed with g + }, + }, + -- add operators that will trigger motion and text object completion + -- to enable all native operators, set the preset / operators plugin above + operators = { gc = "Comments" }, + key_labels = { + -- override the label used to display some keys. It doesn't effect WK in any other way. + -- For example: + -- [""] = "SPC", + -- [""] = "RET", + -- [""] = "TAB", + }, + motions = { + count = true, + }, + icons = { + breadcrumb = "»", -- symbol used in the command line area that shows your active key combo + separator = "➜", -- symbol used between a key and it's label + group = "+", -- symbol prepended to a group + }, + popup_mappings = { + scroll_down = "", -- binding to scroll down inside the popup + scroll_up = "", -- binding to scroll up inside the popup + }, + window = { + border = "none", -- none, single, double, shadow + position = "bottom", -- bottom, top + margin = { 1, 0, 1, 0 }, -- extra window margin [top, right, bottom, left]. When between 0 and 1, will be treated as a percentage of the screen size. + padding = { 1, 2, 1, 2 }, -- extra window padding [top, right, bottom, left] + winblend = 0, -- value between 0-100 0 for fully opaque and 100 for fully transparent + zindex = 1000, -- positive value to position WhichKey above other floating windows. + }, + layout = { + height = { min = 4, max = 25 }, -- min and max height of the columns + width = { min = 20, max = 50 }, -- min and max width of the columns + spacing = 3, -- spacing between columns + align = "left", -- align columns left, center or right + }, + ignore_missing = false, -- enable this to hide mappings for which you didn't specify a label + hidden = { "", "", "", "", "^:", "^ ", "^call ", "^lua " }, -- hide mapping boilerplate + show_help = true, -- show a help message in the command line for using WhichKey + show_keys = true, -- show the currently pressed key and its label as a message in the command line + triggers = "auto", -- automatically setup triggers + -- triggers = {""} -- or specifiy a list manually + -- list of triggers, where WhichKey should not wait for timeoutlen and show immediately + triggers_nowait = { + -- marks + "`", + "'", + "g`", + "g'", + -- registers + '"', + "", + -- spelling + "z=", + }, + triggers_blacklist = { + -- list of mode / prefixes that should never be hooked by WhichKey + -- this is mostly relevant for keymaps that start with a native binding + i = { "j", "k" }, + v = { "j", "k" }, + }, + -- disable the WhichKey popup for certain buf types and file types. + -- Disabled by default for Telescope + disable = { + buftypes = {}, + filetypes = {}, + }, +})