More nvim config

Sun, 19 Feb 2023 03:46:30 -0600

author
Meredith Howard <mhoward@roomag.org>
date
Sun, 19 Feb 2023 03:46:30 -0600
changeset 1022
d509e282ae10
parent 1021
f0756bba5d2f
child 1023
5a252b101778

More nvim config

.config/nvim/autoload/tig.vim file | annotate | diff | comparison | revisions
.config/nvim/autoload/vimrc.vim file | annotate | diff | comparison | revisions
.config/nvim/init.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/config/lsp.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/config/maps.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/config/options.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/plugins/editing.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/plugins/etc.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/plugins/filetype.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/plugins/lsp.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/plugins/ui.lua file | annotate | diff | comparison | revisions
.config/nvim/plugin/vimrc/autocmds.vim file | annotate | diff | comparison | revisions
.config/nvim/plugin/vimrc/commands.vim file | annotate | diff | comparison | revisions
new file mode 100644
--- /dev/null
+++ b/.config/nvim/autoload/tig.vim
@@ -0,0 +1,18 @@
+" Why this wrapper? vim :term sets $TERM to xterm but supports -256color and
+" all the suggestions for fixing that involve changing $TERM for vim itself
+" rather than just the subprocess.  Also since this runs in place we can
+" switch back after.
+func! tig#Tig(...) abort
+  call term_start(
+    \ ['/usr/bin/env', 'TERM=xterm-256color', 'tig'] + a:000,
+    \ {'curwin': 1, 'term_name': join(['!tig'] + a:000, ' '), 'exit_cb': 'tig#TigExit'}
+    \ )
+endfunc
+
+func! tig#TigBlame() abort
+  call tig#Tig('blame', '+' . line('.'), '--', expand('%'))
+endfunc
+
+func! tig#TigExit(...) abort
+  buffer #
+endfunc
new file mode 100644
--- /dev/null
+++ b/.config/nvim/autoload/vimrc.vim
@@ -0,0 +1,119 @@
+func! vimrc#CommandAlias(abbrev, expand) abort
+  execute printf('cnoreabbrev <expr> %s (getcmdtype()==":" && getcmdline()=="%s") ? "%s" : "%s"', a:abbrev, a:abbrev, a:expand, a:abbrev)
+endfunc
+
+func! vimrc#AutoFmtToggle() abort
+  if &formatoptions =~# 'a'
+    setl formatoptions-=a | echo '-a'
+  else
+    setl formatoptions+=a | echo '+a'
+  endif
+endfunc
+
+" Make paths when writing, as necessary
+func! vimrc#MkNonExDir(file, buf) abort
+  if empty(getbufvar(a:buf, '&buftype')) && a:file!~#'\v^\w+\:\/'
+    let dir=fnamemodify(a:file, ':h')
+    if !isdirectory(dir)
+      call mkdir(dir, 'p')
+    endif
+  endif
+endfunc
+
+func! vimrc#AutoSessionCheck() abort
+  if strlen(v:servername) > 0 && match(v:servername, 'VIM') == -1
+    let sessionfile = g:vimcache . "/session/" . tolower(v:servername) . ".vim"
+
+    if filereadable(sessionfile)
+      execute "source " . sessionfile
+    endif
+  endif
+endfunc
+
+func! vimrc#Grep(...) abort
+  let pattern = get(a:000, 0, expand('<cword>'))
+  let cmd = join([&grepprg, shellescape(pattern)] + a:000[1:], ' ')
+
+  echo cmd
+  cgetexpr system(cmd)
+  call setqflist([], 'a', {"title": cmd})
+  let @/ = '\v' . pattern
+  copen
+  cfirst
+endfunc
+
+func! vimrc#Gcd() abort
+  let root = system('git rev-parse --show-toplevel 2>/dev/null')[:-2]
+  if ! v:shell_error
+    exec 'cd ' . root
+  endif
+  pwd
+endfunc
+
+func! vimrc#Hgcd() abort
+  let root = system('hg root 2>/dev/null')[:-2]
+  if ! v:shell_error
+    exec 'cd ' . root
+  endif
+  pwd
+endfunc
+
+func! vimrc#SafeFilterFile(cmd)
+  let errors = tempname()
+  try
+    exec 'silent %!' . a:cmd . ' 2>' . shellescape(errors)
+    if v:shell_error
+      for line in readfile(errors)
+        echomsg line
+      endfor
+    endif
+  finally
+    call delete(errors)
+  endtry
+endfunc
+
+if has('perl')
+  func! vimrc#PruneSession() abort
+    perl <<END_PERL
+      my @bufs =
+        grep { !-e $_->Name || -d _ || (-M _ >= 30) }
+        grep { $_->Name } VIM::Buffers();
+
+      while (my $b = shift @bufs) {
+        VIM::Msg 'pruned: ' . $b->Name, 'Comment';
+        VIM::DoCommand 'bwipeout ' . $b->Number;
+      }
+      VIM::DoCommand 'bprev'
+        unless $curbuf->Name;
+END_PERL
+  endfunc
+endif
+
+func! vimrc#PrepDir(path) abort
+  if !filewritable(a:path)
+    call mkdir(a:path, 'p', 0700)
+  endif
+endfunc
+
+if has('ruby')
+  func! s:PruneFiles(path, days) abort
+    ruby <<END_RUBY
+      require 'pathname'
+
+      (path, days) = VIM.evaluate('[a:path, a:days]')
+      sunset       = Time.now - (days * 86400)
+
+      Pathname(path).realpath.each_child do |file|
+        file.delete if file.mtime < sunset
+      end
+END_RUBY
+  endfunc
+else
+  func! s:PruneFiles(path, days) abort
+  endfunc
+endif
+
+func! vimrc#PruneFiles(path, days) abort
+  call s:PruneFiles(a:path, a:days)
+endfunc
+
--- a/.config/nvim/init.lua
+++ b/.config/nvim/init.lua
@@ -1,14 +1,20 @@
+local g = vim.g
+
 require("config.options")
 require("lazy-bootstrap")
 require("config.maps")
 require("config.lsp")
 
-local g = vim.g
-
 -- >> Builtin
 g.netrw_altfile = 1
 g.netrw_use_errorwindow = 0
 
+-- >> Perl
+g.perl_include_pod = 1
+g.perl_sub_signatures = 1
+g.perl_sync_dist = 300
+g.perl_compiler_force_warnings = 0
+
 -- >> Undotree
 g.undotree_SplitWidth = 45
 g.undotree_SetFocusWhenToggle = 1
@@ -21,3 +27,6 @@ g.tagbar_autofocus = 1
 g.tagbar_compact = 1
 g.tagbar_width = 30
 
+-- >> Viewdoc
+g.viewdoc_open = "topleft new"
+g.viewdoc_winwidth_max = 100
--- a/.config/nvim/lua/config/lsp.lua
+++ b/.config/nvim/lua/config/lsp.lua
@@ -8,34 +8,3 @@ vim.api.nvim_create_autocmd("LspAttach",
     vim.opt.number = true
   end,
 })
-
-local opts = { noremap=true, silent=true }
-vim.keymap.set('n', '<leader>d', vim.diagnostic.open_float, opts)
-vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts)
-vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts)
-vim.keymap.set('n', '<leader>ld', vim.diagnostic.setloclist, opts)
-
-vim.api.nvim_create_autocmd("LspAttach", {
-  callback = function(args)
-    local bufnr = args.buf
-    local bufopts = { noremap=true, silent=true, buffer=bufnr }
-
-    vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts)
-    vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts)
-    vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts)
-    vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts)
-    vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, bufopts)
-    vim.keymap.set('n', '<leader>wa', vim.lsp.buf.add_workspace_folder, bufopts)
-    vim.keymap.set('n', '<leader>wr', vim.lsp.buf.remove_workspace_folder, bufopts)
-    vim.keymap.set('n', '<leader>wl', function()
-      print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
-    end, bufopts)
-    vim.keymap.set('n', '<leader>D', vim.lsp.buf.type_definition, bufopts)
-    vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, bufopts)
-    vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, bufopts)
-    vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts)
-    vim.keymap.set('n', '<leader>f', function() vim.lsp.buf.format { async = true } end, bufopts)
-  end,
-})
-
-
--- a/.config/nvim/lua/config/maps.lua
+++ b/.config/nvim/lua/config/maps.lua
@@ -15,13 +15,72 @@ map.set("i", "<F4>", "<C-O><F4>")
 -- cover for search habit
 map.set("c", "<F3>", "<CR>", opts)
 
+-- change to file's directory
 map.set("n", "<leader>cd", ":cd %:p:h<CR>:pwd<CR>", opts)
 
+-- window switching
 map.set("n", "<C-h>", "<C-w>h", opts)
 map.set("n", "<C-j>", "<C-w>j", opts)
 map.set("n", "<C-k>", "<C-w>k", opts)
 map.set("n", "<C-l>", "<C-w>l", opts)
 map.set("n", "<C-\\>", "<C-w>p", opts)
 
+-- buffer switching
 map.set("n", "gb", "<C-^>", opts)
 map.set("n", "gB", ":ls<CR>:b ", {noremap = true})
+
+-- Select last paste, in the same mode it was pasted in
+map.set("n", "gV", "'`[' . strpart(getregtype(), 0, 1) . '`]'", {noremap=true, expr=true})
+
+-- Use ltag over tselect
+map.set("n", "g<C-]>", ":exe 'ltag ' . expand('<cword>') | lopen<CR>", opts)
+
+-- clear all interestingwords with \\k since \K is ri.vim
+map.set("n", "<leader><leader>k", ":call UncolorAllWords()<CR>", opts)
+
+-- mark line
+map.set("n", "<leader>l", "V<leader>k", opts)
+
+-- use Grep for a recursive *
+map.set("n", "g*", ":Grep<CR>", opts)
+
+-- K: doc, gKK: doc current filename
+map.set("n", "gKK", ":call ViewDoc('doc', expand('%:p'))<CR>", opts)
+
+-- Tabular shortcuts
+map.set("n", "<leader>ta", ":Tabularize first_arrow<CR>", opts)
+map.set("n", "<leader>te", ":Tabularize first_eq<CR>", opts)
+map.set("n", "<leader>tc", ":Tabularize first_colon<CR>", opts)
+map.set("n", "<leader>tm", ":Tabularize methods<CR>", opts)
+
+map.set("n", "<leader>a", ":call vimrc#AutoFmtToggle()<CR>", opts)
+
+-- LSP features
+map.set('n', '<leader>d', vim.diagnostic.open_float, opts)
+map.set('n', '[d', vim.diagnostic.goto_prev, opts)
+map.set('n', ']d', vim.diagnostic.goto_next, opts)
+map.set('n', '<leader>ld', vim.diagnostic.setloclist, opts)
+
+vim.api.nvim_create_autocmd("LspAttach", {
+  callback = function(args)
+    local bufnr = args.buf
+    local bufopts = { noremap=true, silent=true, buffer=bufnr }
+
+    map.set('n', 'gD', vim.lsp.buf.declaration, bufopts)
+    map.set('n', 'gd', vim.lsp.buf.definition, bufopts)
+    map.set('n', 'K', vim.lsp.buf.hover, bufopts)
+    map.set('n', 'gi', vim.lsp.buf.implementation, bufopts)
+    map.set('n', '<C-k>', vim.lsp.buf.signature_help, bufopts)
+    map.set('n', '<leader>wa', vim.lsp.buf.add_workspace_folder, bufopts)
+    map.set('n', '<leader>wr', vim.lsp.buf.remove_workspace_folder, bufopts)
+    map.set('n', '<leader>wl', function()
+      print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
+    end, bufopts)
+    map.set('n', '<leader>D', vim.lsp.buf.type_definition, bufopts)
+    map.set('n', '<leader>rn', vim.lsp.buf.rename, bufopts)
+    map.set('n', '<leader>ca', vim.lsp.buf.code_action, bufopts)
+    map.set('n', 'gr', vim.lsp.buf.references, bufopts)
+    map.set('n', '<leader>f', function() vim.lsp.buf.format { async = true } end, bufopts)
+  end,
+})
+
--- a/.config/nvim/lua/config/options.lua
+++ b/.config/nvim/lua/config/options.lua
@@ -45,7 +45,6 @@ o.wildignore = "*~,*.o,*.pyc,.git/*,hg/*
 
 if vim.fn.executable("ag") then
   o.grepprg = "ag --vimgrep"
-  o.grepformat:prepend({"%f:%l:%c:%m", "%f"})
-  o.errorformat:append("%f")
+  -- o.grepformat:prepend({"%f:%l:%c:%m", "%f"})
+  -- o.errorformat:append("%f")
 end
-
--- a/.config/nvim/lua/plugins/editing.lua
+++ b/.config/nvim/lua/plugins/editing.lua
@@ -1,9 +1,24 @@
+local g = vim.g
+
+-- >> Undotree
+g.undotree_SplitWidth = 45
+g.undotree_SetFocusWhenToggle = 1
+g.undotree_ShortIndicators = 1
+g.undotree_DiffCommand = "diff -dp -U 1"
+
+-- >> Tagbar
+g.tagbar_autoclose = 1
+g.tagbar_autofocus = 1
+g.tagbar_compact = 1
+g.tagbar_width = 30
+
 return {
   "editorconfig/editorconfig-vim",
+
   "tpope/vim-unimpaired",
-  "godlygeek/tabular",
   "tomtom/tcomment_vim",
   "tpope/vim-endwise",
+  {"godlygeek/tabular", cmd = "Tabularize"},
   {"mbbill/undotree", cmd = "UndotreeToggle"},
 
   "tpope/vim-vinegar",
new file mode 100644
--- /dev/null
+++ b/.config/nvim/lua/plugins/etc.lua
@@ -0,0 +1,20 @@
+local g = vim.g
+
+g.vimwiki_auto_chdir  = 1
+g.vimwiki_auto_header = 1
+g.vimwiki_ext2syntax = {[vim.type_idx]=vim.types.dictionary}
+
+g.vimwiki_list = {
+  {
+    path = '~/vimwiki/',
+    auto_tags = 1, auto_toc = 1, automatic_nested_syntaxes = 1
+  },
+  {
+    path = '~/Documents/SpiderOak Hive/vimwiki',
+    auto_tags = 1, auto_toc = 1, automatic_nested_syntaxes = 1
+  }
+}
+
+return {
+  'vimwiki/vimwiki', 
+}
new file mode 100644
--- /dev/null
+++ b/.config/nvim/lua/plugins/filetype.lua
@@ -0,0 +1,18 @@
+local g = vim.g
+
+-- >> Polyglot
+g.polyglot_disabled = {'autoindent', 'sensible', 'vifm', 'perl', 'go'}
+
+-- >> Perl
+g.perl_include_pod = 1
+g.perl_sub_signatures = 1
+g.perl_sync_dist = 300
+g.perl_compiler_force_warnings = 0
+
+return {
+  'Shougo/vinarise.vim',
+  'asciidoc/vim-asciidoc',
+  {'vim-perl/vim-perl', branch = 'dev'},
+  'sheerun/vim-polyglot',
+  'yko/mojo.vim',
+}
--- a/.config/nvim/lua/plugins/lsp.lua
+++ b/.config/nvim/lua/plugins/lsp.lua
@@ -11,6 +11,11 @@ return {
         function(server)
           require("lspconfig")[server].setup({})
         end,
+        gopls = function ()
+          require("lspconfig").gopls.setup({
+            settings = { gopls = { gofumpt = true } }
+          })
+        end
       })
     end,
   },
--- a/.config/nvim/lua/plugins/ui.lua
+++ b/.config/nvim/lua/plugins/ui.lua
@@ -1,4 +1,13 @@
+local g = vim.g
+
+-- >> Viewdoc
+g.no_viewdoc_abbrev = 1
+g.viewdoc_open = "topleft new"
+g.viewdoc_winwidth_max = 100
+
 return {
+  "powerman/vim-plugin-viewdoc",
+
   {
     "nvim-lualine/lualine.nvim",
     opts = {
@@ -24,7 +33,7 @@ return {
     opts = {
       enabled = false,
       char = "│",
-      filetype_exclude = { "help", "alpha", "dashboard", "neo-tree", "Trouble", "lazy" },
+      -- filetype_exclude = { "help", "alpha", "dashboard", "neo-tree", "Trouble", "lazy" },
       show_trailing_blankline_indent = false,
       show_current_context = false,
     },
new file mode 100644
--- /dev/null
+++ b/.config/nvim/plugin/vimrc/autocmds.vim
@@ -0,0 +1,51 @@
+augroup vimrc
+  autocmd!
+
+  autocmd WinLeave * if !pumvisible() | stopinsert | endif
+
+  " complement to autowriteall
+  autocmd FocusLost * silent! wa
+
+  " Make paths when writing, as necessary
+  autocmd BufWritePre * :call vimrc#MkNonExDir(expand('<afile>'), +expand('<abuf>'))
+
+  if ! &diff
+    " set and load a session based on servername
+    autocmd VimEnter * nested call vimrc#AutoSessionCheck()
+
+    " Jump to last known pos
+    autocmd BufReadPost *
+      \ if &filetype !~# 'mail\|^git\|^hg' && line("'\"") >= 1 && line("'\"") <= line("$") |
+      \   exe "normal! g`\"" |
+      \ endif
+
+    " Simple highlight conflict markers
+    autocmd BufReadPost *
+      \ match Error "^\([<>|]\)\{7} \@=\|^=\{7}$"
+  endif
+
+  " Simplify noisy ltag output
+  autocmd BufReadPost quickfix
+    \ if w:quickfix_title =~# '^:ltag' |
+      \ setl modifiable |
+      \ silent exe ':%s/\^\\V\s*\|\\\$|.*//g' |
+      \ setl nomodifiable |
+    \ endif
+
+  " easy close quickfix
+  autocmd BufReadPost quickfix nmap <buffer> q <C-w>c
+
+  " Neomutt changed their tmpfile pattern, ugh
+  autocmd BufNewFile,BufRead neomutt-*-\w\+ setf mail
+augroup END
+
+" https://mjj.io/2015/01/27/encrypting-files-with-gpg-and-vim/
+" hacked to work with vimwiki
+augroup encrypted
+  autocmd!
+  autocmd BufReadPre,FileReadPre *.gpg,*.gpg.* setl noswapfile noundofile nobackup viminfo=
+  autocmd BufReadPost *.gpg,*.gpg.* call vimrc#SafeFilterFile('gpg2 -d')
+  autocmd BufWritePre *.gpg,*.gpg.* call vimrc#SafeFilterFile('gpg2 -se -a --default-recipient-self')
+  autocmd BufWritePost *.gpg,*.gpg.* :sil undo
+augroup END
+
new file mode 100644
--- /dev/null
+++ b/.config/nvim/plugin/vimrc/commands.vim
@@ -0,0 +1,35 @@
+command! -nargs=+ CAlias call vimrc#CommandAlias(<f-args>)
+
+command! Gcd call vimrc#Gcd()
+command! Hgcd call vimrc#Hgcd()
+
+command! SyntaxCompleteOn setl omnifunc=syntaxcomplete#Complete
+
+command! Mksession execute "mksession! " . v:this_session
+command! PruneSession call vimrc#PruneSession()
+
+command! -nargs=* -complete=file Tig      call tig#Tig(<f-args>)
+command!                         TigBlame call tig#TigBlame()
+command!                         TigLog   call tig#Tig('log', '-p', '--', expand('%'))
+
+" Preview markdown mail -- I edit with headers so I box them in a code block.
+command! MailPreview     enew | set bt=nofile | 0r # | exe 'norm! 0O```<Esc>}O```' | silent exe '%!mutt-md2html | mutt-html2txt' | 0
+command! MailPreviewHTML enew | set bt=nofile | setf html | 0r # | exe 'norm! 0O```<Esc>}O```' | silent exe '%!mutt-md2html' | 0
+
+command! -nargs=* -complete=file Grep call vimrc#Grep(<f-args>)
+CAlias Ag Grep
+CAlias grep Grep
+
+CAlias Q q
+CAlias Qa qa
+CAlias W w
+
+CAlias gcd Gcd
+CAlias hgcd Hgcd
+
+" make these default to one window/buffer too
+CAlias  doc      ViewDoc!
+CAlias  help     ViewDocHelp!
+CAlias  man      ViewDocMan!
+CAlias  perldoc  ViewDocPerl!
+

mercurial