summary refs log tree commit diff
path: root/.vim/lua/internal
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2024-05-15 09:00:31 +0200
committerRobert Günzler <r@gnzler.io>2024-05-15 09:00:31 +0200
commit3dbe74f2eef627e2dc19b79fe06753d5dc09003f (patch)
treeb78d1cdab62cec9ed4bbe35447108547b935ef03 /.vim/lua/internal
parent0ab72c1113139c196115c974eadaded449002c32 (diff)
update nvim
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '')
-rw-r--r--.vim/lua/internal/align.lua124
-rw-r--r--.vim/lua/internal/colors.lua18
-rw-r--r--.vim/lua/internal/commit-preview.lua67
-rw-r--r--.vim/lua/internal/cursor.lua11
-rw-r--r--.vim/lua/internal/dap.lua104
-rw-r--r--.vim/lua/internal/find.lua106
-rw-r--r--.vim/lua/internal/job.lua106
-rw-r--r--.vim/lua/internal/lsp.lua66
-rw-r--r--.vim/lua/internal/lsp_symbols.lua8
-rw-r--r--.vim/lua/internal/misc.lua99
-rw-r--r--.vim/lua/internal/snips.lua29
-rw-r--r--.vim/lua/internal/sortline.lua78
-rw-r--r--.vim/lua/internal/statusline.lua66
-rw-r--r--.vim/lua/internal/syncbg.lua46
-rw-r--r--.vim/lua/internal/zk.lua7
15 files changed, 568 insertions, 367 deletions
diff --git a/.vim/lua/internal/align.lua b/.vim/lua/internal/align.lua
deleted file mode 100644
index 1ada01d..0000000
--- a/.vim/lua/internal/align.lua
+++ /dev/null
@@ -1,124 +0,0 @@
--- based on the ideas from:
--- https://github.com/RRethy/nvim-align
-
-local M = {}
-
-function M.align_lines(pat, line1, line2, preview_ns, preview_bufnr)
-  local re = vim.regex(pat)
-  local bufnr = vim.api.nvim_get_current_buf()
-  local lines = vim.api.nvim_buf_get_lines(bufnr, line1 - 1, line2, false)
-  local newlines = {}
-  local preview_buf_line = 0
-
-  -- find the longest match
-  local max = -1
-  for _, line in pairs(lines) do
-    local s = re:match_str(line)
-    if s and max < s then
-      max = s
-    end
-  end
-
-  -- exit if nothing was found
-  if max == -1 then
-    return error('nothing found')
-  end
-
-  for i, line in pairs(lines) do
-    local s = re:match_str(line)
-    if s then
-      local rep = max - s
-      local changeset = {
-        string.sub(line, 1, s),
-        string.rep(' ', rep),
-        string.sub(line, s + 1),
-      }
-      local newline = table.concat(changeset)
-
-      -- append to changse if not inside the preview callback
-      if not preview_ns then
-        newlines[#newlines + 1] = newline
-      end
-
-      if preview_ns ~= nil then
-        -- set extmarks inside the live buffer
-        vim.api.nvim_buf_set_extmark(bufnr, preview_ns, line1 + i - 2, 0, {
-          hl_mode = 'combine',
-          virt_text_pos = 'overlay',
-          virt_text = {
-            { changeset[1] },
-            { changeset[2], 'Substitute' },
-            { changeset[3] },
-          },
-        })
-
-        -- modify preview buffer
-        if preview_bufnr ~= nil then
-          local prefix = string.format('|%d| ', line1 + i - 1)
-          vim.api.nvim_buf_set_lines(
-            preview_bufnr,
-            preview_buf_line,
-            preview_buf_line,
-            false,
-            { prefix .. newline }
-          )
-          vim.api.nvim_buf_add_highlight(
-            preview_bufnr,
-            preview_ns,
-            'Substitute',
-            preview_buf_line,
-            #prefix + s,
-            #prefix + s + #changeset[2]
-          )
-          preview_buf_line = preview_buf_line + 1
-        end
-      end
-    end
-  end
-
-  -- only change buffer when not previewing
-  if not preview_ns then
-    -- exit if nothing was changed
-    if #newlines == 0 then
-      return error('nothing changed')
-    end
-    vim.api.nvim_buf_set_lines(bufnr, line1 - 1, line2, 0, newlines)
-    return 0
-  end
-
-  if preview_ns ~= nil then
-    -- open preview buffer only if there is more than a single line of change
-    return (#preview_buf_line > 1) and 2 or 1
-  end
-end
-
-function M.align(pat)
-  local top, bot = vim.fn.getpos("'<"), vim.fn.getpos("'>")
-  M.align_lines(pat, top[2] - 1, bot[2])
-  vim.fn.setpos("'<", top)
-  vim.fn.setpos("'>", bot)
-end
-
-local function aligncmd(opts, preview_ns, preview_bufnr)
-  return M.align_lines(opts.fargs[1], opts.line1, opts.line2, preview_ns, preview_bufnr)
-end
-
-local default_opts = {
-  bindings = true,
-}
-
-function M.setup(opts)
-  opts = vim.tbl_extend('keep', opts or {}, default_opts)
-
-  vim.api.nvim_create_user_command(
-    'SimpleAlign',
-    aligncmd,
-    { nargs = 1, range = '%', preview = aligncmd }
-  )
-
-  -- if opts.bindings then
-  --   vim.keymap.set('v', '<enter>', ':SimpleAlign ')
-  -- end
-end
-
-return M
diff --git a/.vim/lua/internal/colors.lua b/.vim/lua/internal/colors.lua
new file mode 100644
index 0000000..a03501f
--- /dev/null
+++ b/.vim/lua/internal/colors.lua
@@ -0,0 +1,18 @@
+return {
+  base00 = '#080808',
+  base01 = '#db4d6d',
+  base02 = '#5dac81',
+  base03 = '#fad689',
+  base04 = '#58b2dc',
+  base05 = '#70649a',
+  base06 = '#69b0ac',
+  base07 = '#bdc0ba',
+  base08 = '#4f4f48',
+  base09 = '#cb1b45',
+  base0A = '#86c166',
+  base0B = '#f7d94c',
+  base0C = '#2ea9df',
+  base0D = '#8a6bbe',
+  base0E = '#81c7d4',
+  base0F = '#fffffb',
+}
\ No newline at end of file
diff --git a/.vim/lua/internal/commit-preview.lua b/.vim/lua/internal/commit-preview.lua
new file mode 100644
index 0000000..512b3fb
--- /dev/null
+++ b/.vim/lua/internal/commit-preview.lua
@@ -0,0 +1,67 @@
+local M = {}
+
+function M.under_cursor()
+  require('internal.cursor').word(require('internal.commit-preview').commit_preview)
+end
+
+function M.commit_preview(commit, on_attach)
+  if not commit then return end
+
+  local result = require('internal.job')
+      .jobstart({
+        command = 'git',
+        args = { 'show', '--stat', '--patch', commit },
+        format_title = function(_, _)
+          return 'git-show'
+        end,
+        populate_quickfix = false,
+      })
+      :wait()
+      :result()
+
+  -- create buffer
+  local bufnr = vim.api.nvim_create_buf(false, true)
+  assert(bufnr, 'Failed to create buffer')
+  vim.api.nvim_buf_set_name(bufnr, commit)
+  vim.api.nvim_set_option_value('filetype', 'commitpreview', { buf = bufnr })
+
+  -- add lines
+  vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr })
+  vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, result)
+  vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
+
+  -- create window
+  local winpos = 'belowright'
+  local winheight = #result <= 25 and #result or 25
+  vim.cmd(string.format('%s %dsplit', winpos, winheight)) -- open split
+  local winid = vim.api.nvim_get_current_win()
+  vim.api.nvim_win_set_buf(winid, bufnr)
+  vim.cmd('wincmd p')
+
+  vim.keymap.set('n', 'gd', M.under_cursor, { buffer = bufnr })
+  vim.keymap.set('n', 'q', function() vim.api.nvim_win_close(winid, true) end, { buffer = bufnr })
+
+  if on_attach then on_attach(bufnr, winid) end
+  -- close on CursorMoved (anywhere but git-show-buf)
+  -- local old_cursor = vim.api.nvim_win_get_cursor(beforewin)
+  -- local group = vim.api.nvim_create_augroup('gitcommit_win_' .. winid, {})
+  -- vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
+  --   group = group,
+  --   callback = function()
+  --     local cursor = vim.api.nvim_win_get_cursor(0)
+  --     if
+  --         (old_cursor[1] ~= cursor[1] or old_cursor[2] ~= cursor[2])
+  --         and vim.api.nvim_get_current_win() ~= winid
+  --     then
+  --       close()
+  --       return
+  --     end
+  --     old_cursor = cursor
+  --   end,
+  -- })
+
+  -- focus
+  vim.api.nvim_set_current_win(winid)
+end
+
+return M
diff --git a/.vim/lua/internal/cursor.lua b/.vim/lua/internal/cursor.lua
new file mode 100644
index 0000000..ad8d8b2
--- /dev/null
+++ b/.vim/lua/internal/cursor.lua
@@ -0,0 +1,11 @@
+local M = {}
+
+function impl(obj, cb) return cb(vim.fn.expand(vim.fn.expand(obj))) end
+
+function M.word(cb) return impl('<cword>', cb) end
+
+function M.expr(cb) return impl('<cexpr>', cb) end
+
+function M.file(cb) return impl('<cfile>', cb) end
+
+return M
diff --git a/.vim/lua/internal/dap.lua b/.vim/lua/internal/dap.lua
index 1790575..4b1d776 100644
--- a/.vim/lua/internal/dap.lua
+++ b/.vim/lua/internal/dap.lua
@@ -2,13 +2,16 @@
 
 local dap = require('dap')
 dap.defaults.fallback.terminal_win_cmd = 'belowright 10new'
-dap.defaults.fallback.terminal_win_cmd = 'belowright 10new'
+dap.defaults.fallback.focus_terminal = false
 
 -- open repl when session starts
 dap.listeners.after['event_initialized']['me'] = function()
-  dap.repl.toggle()
+  -- dap.repl.toggle()
 end
 
+-- try loading dap-launch.json
+-- require('dap.ext.vscode').load_launchjs(vim.fn.getcwd() .. '/dap-launch.json')
+
 -- signs
 vim.fn.sign_define({
   { name = 'DapBreakpoint', text = '🞱', texthl = 'DapBreakpoint', linehl = 'DapBreakpointLn' },
@@ -21,12 +24,12 @@ vim.fn.sign_define({
 vim.keymap.set('n', '<leader>dd', dap.toggle_breakpoint, { desc = 'toggle breakpoint' }) -- convenience
 vim.keymap.set('n', '<leader>db', dap.toggle_breakpoint, { desc = 'toggle breakpoint' })
 vim.keymap.set('n', '<leader>dc', dap.continue, { desc = 'continue' })
-vim.keymap.set('n', '<leader>do', dap.step_over, { desc = 'step over' })
+vim.keymap.set('n', '<leader>ds', dap.step_over, { desc = 'step over' })
 vim.keymap.set('n', '<leader>di', dap.step_into, { desc = 'step into' })
-vim.keymap.set('n', '<leader>dO', dap.step_out, { desc = 'step out' })
+vim.keymap.set('n', '<leader>do', dap.step_out, { desc = 'step out' })
 vim.keymap.set('n', '<leader>dR', dap.repl.toggle, { desc = 'toggle REPL' })
 vim.keymap.set('n', '<leader>dL', dap.run_last, { desc = 'run last' })
-vim.keymap.set('n', '<leader>dt', dap.terminate, { desc = 'terminate' })
+vim.keymap.set('n', '<leader>dq', dap.terminate, { desc = 'terminate' })
 
 local widgets = require('dap.ui.widgets')
 vim.keymap.set('n', '<leader>dh', widgets.hover, { desc = 'hover' })
@@ -39,15 +42,23 @@ vim.keymap.set(
 
 -- Common
 local bin_from_var_or_pick = function()
-  local ok, retval = pcall(vim.api.nvim_buf_get_var, 0, 'dap_bin')
+  local ok, retval = pcall(vim.api.nvim_get_var, 'dap_bin') -- global let g:dap_bin
   if ok then
+    local workspace_folders = vim.lsp.buf.list_workspace_folders()
+    if #workspace_folders >= 1 then
+      retval = retval:gsub(':workspace', workspace_folders[1])
+    end
     return retval
   end
+  -- NOTE: try to resolve binaries from build system
   local bin = nil
-  vim.ui.input({ prompt = 'DAP Binary: ' }, function(choice)
+  vim.ui.input({ prompt = 'DAP: binary to launch: ' }, function(choice)
     bin = choice
   end)
-  return bin
+  if not bin == nil then
+    return bin
+  end
+  return false
 end
 
 -- Go {{{
@@ -106,59 +117,12 @@ dap.configurations.go = {
 -- end of Go }}}
 
 -- C/C++/Rust {{{
--- dap.adapters.cppdbg = {
---   id = 'cppdbg',
---   type = 'executable',
---   command = vim.env.HOME .. '/.local/share/nvim/dap/ms-vscode.cpptools/extension/debugAdapters/bin/OpenDebugAD7',
--- }
 dap.adapters.lldb = {
   type = 'executable',
-  command = vim.trim(vim.fn.system('command -v lldb-vscode')),
+  command = '/usr/bin/lldb-vscode',
   name = 'lldb',
 }
 
--- dap.configurations.c = {
---   {
---     name = 'Launch binary',
---     type = 'cppdbg',
---     request = 'launch',
---     program = function()
---       local file = vim.g.dapbin
---       if file then return file end
---       -- TODO: assuming meson
---       file = vim.trim(vim.fn.system(
---         [[jq -r '..|select(.type?=="executable")|.filename|.[0]' ]] ..
---         vim.lsp.buf.list_workspace_folders()[1] ..
---         [[/build/meson-info/intro-targets.json]]
---       ))
---       local fd = vim.loop.fs_open(file, "r", 438)
---       if file and fd then
---         vim.loop.fs_close(fd)
---         return file
---       end
---       vim.ui.input('binary: ', function(result) file = result end)
---       return file
---     end,
---     cwd = '${workspaceFolder}',
---     stopOnEntry = true,
---   },
---   {
---     name = 'Attach to gdbserver',
---     type = 'cppdbg',
---     request = 'launch',
---     MIMode = 'gdb',
---     miDebuggerServerAddress = 'localhost:1234',
---     miDebuggerPath = '/usr/bin/gdb',
---     cwd = '${workspaceFolder}',
---     program = function()
---       local file = vim.g.dapbin
---       if file then return file end
---       vim.ui.input('binary: ', function(result) file = result end)
---       return file
---     end,
---   },
--- }
-
 dap.configurations.c = {
   {
     name = 'Launch',
@@ -178,6 +142,32 @@ dap.configurations.c = {
   },
 }
 dap.configurations.cpp = dap.configurations.c
-dap.configurations.rust = dap.configurations.c
 
+-- enable rust types
+dap.configurations.rust = vim.tbl_extend('force', dap.configurations.c, {
+  initCommands = function()
+    local rustc_sysroot = vim.fn.trim(vim.fn.system('rustc --print sysroot'))
+
+    local script_import = 'command script import "'
+        .. rustc_sysroot
+        .. '/lib/rustlib/etc/lldb_lookup.py"'
+    local commands_file = rustc_sysroot .. '/lib/rustlib/etc/lldb_commands'
+
+    local commands = {}
+    local file = io.open(commands_file, 'r')
+    if file then
+      for line in file:lines() do
+        table.insert(commands, line)
+      end
+      file:close()
+    end
+    table.insert(commands, 1, script_import)
+
+    return commands
+  end,
+})
 -- end of C/C++/Rust }}}
+
+local dapui = require('dapui')
+dapui.setup {}
+vim.keymap.set('n', '<leader>du', dapui.toggle, { desc = 'toggle ui' })
diff --git a/.vim/lua/internal/find.lua b/.vim/lua/internal/find.lua
new file mode 100644
index 0000000..32bdea5
--- /dev/null
+++ b/.vim/lua/internal/find.lua
@@ -0,0 +1,106 @@
+local M = {}
+
+local get_searchdirs = function(dirs)
+  dirs = dirs or {}
+  if type(dirs) == 'table' and #dirs > 0 then
+    return dirs
+  elseif type(dirs) == 'string' and (dirs == ':workspace') then
+    dirs = vim.lsp.buf.list_workspace_folders()
+  elseif type(dirs) == 'string' and not (dirs == '') then
+    dirs = vim.split(dirs, ',')
+  end
+  -- skip asking if we're looking at known filetypes
+  local ft = vim.api.nvim_buf_get_option(0, 'filetype')
+  if #dirs == 0 and (ft == 'dirbuf' or ft == 'NvimTree' or ft == 'alpha') then
+    dirs = { '%:p:h' }
+  end
+  -- ask user
+  if #dirs == 0 then
+    vim.ui.input({ prompt = 'Search directories: ', default = '%:p:h' }, function(result)
+      dirs = { result }
+    end)
+  end
+  -- default to something reasonable
+  if #dirs == 0 then
+    dirs = { '%:p:h' }
+  end
+  -- finally return
+  return dirs
+end
+
+function onchoice(opts, cb)
+  if not cb then
+    return
+  end
+  opts.attach_mappings = function(prompt_bufnr)
+    require('telescope.actions').select_default:replace(function()
+      local selection = require('telescope.actions.state').get_selected_entry()
+      if selection == nil then
+        return
+      end
+      require('telescope.actions').close(prompt_bufnr)
+      on_choice(selection.path)
+    end)
+    return true
+  end
+end
+
+function search_shim(fn, search_dirs, opts, cb)
+  opts = opts or {}
+  opts.search_dirs = get_searchdirs(search_dirs)
+  opts.prompt_title = vim.fn.expand(table.concat(opts.search_dirs, ','))
+  onchoice(opts, cb)
+  fn(opts)
+end
+
+function M.live_grep(search_dirs, opts, cb)
+  search_shim(require('telescope.builtin').live_grep, search_dirs, opts, cb)
+end
+
+function M.find_files(search_dirs, opts, cb)
+  -- convenience aliases
+  if type(search_dirs) == 'string' and search_dirs == 'data' then
+    search_dirs = vim.fn.stdpath('data')
+  end
+  search_shim(require('telescope.builtin').find_files, search_dirs, opts, cb)
+end
+
+function M.setup(opts)
+  vim.keymap.set('n', ';f', function()
+    vim.notify('use gsf', vim.log.levels.ERROR)
+  end, { desc = 'find files' })
+  vim.keymap.set('n', ';g', function()
+    vim.notify('use gsg', vim.log.levels.ERROR)
+  end, { desc = 'live grep' })
+
+  vim.keymap.set('n', 'gsg', function()
+    M.live_grep(':workspace')
+  end, { desc = 'live grep in workspace' })
+  vim.keymap.set('n', 'gsG', M.live_grep, { desc = 'live grep ask' })
+  vim.keymap.set('n', 'gs-', function()
+    M.live_grep('%:p:h')
+  end, { desc = 'live grep in parent' })
+
+  vim.keymap.set('n', 'gsf', function()
+    M.find_files(':workspace')
+  end, { desc = 'find files in workspace' })
+  vim.keymap.set('n', 'gsF', M.find_files, { desc = 'find files ask' })
+
+  vim.api.nvim_create_user_command('Grep', function(o)
+    M.live_grep(table.concat(o.fargs))
+  end, {
+    desc = 'live grep (rg)',
+    nargs = '*',
+    complete = 'dir',
+  })
+
+  vim.api.nvim_create_user_command('Find', function(o)
+    M.find_files(table.concat(o.fargs))
+  end, {
+    desc = 'find files (fd)',
+    nargs = '*',
+    complete = 'dir',
+  })
+end
+
+return M
diff --git a/.vim/lua/internal/job.lua b/.vim/lua/internal/job.lua
index 42a548b..2e97f16 100644
--- a/.vim/lua/internal/job.lua
+++ b/.vim/lua/internal/job.lua
@@ -28,11 +28,28 @@ end
 function M.jobstart(opts)
   opts = opts or {}
 
-  local spinner_ok, spinner = pcall(require, 'spinner.core')
+  opts = vim.tbl_extend('keep', opts or {}, {
+    enable_spinner = vim.g.job_enable_spinner,
+    populate_quickfix = vim.g.job_populate_quickfix,
+    attach = vim.g.job_attach,
+  })
+
+  -- vim.notify_once(vim.inspect(opts), vim.log.levels.DEBUG)
 
-  -- default options
-  opts.enable_spinner = (opts.enable_spinner or true) and spinner_ok
-  opts.populate_quickfix = opts.populate_quickfix or 'onerror'
+  if opts.attach then
+    return vim.schedule_wrap(function()
+      vim.cmd.terminal({ args = { opts.command, unpack(opts.args) } })
+    end)()
+  end
+
+  local spinner = {}
+  if opts.enable_spinner then
+    local ok = false
+    ok, spinner = pcall(require, 'spinner.core')
+    if not ok then
+      opts.enable_spinner = false
+    end
+  end
 
   if opts.format_title == nil then
     opts.format_title = table.concat({ opts.command, unpack(opts.args) }, ' ')
@@ -102,58 +119,63 @@ function M.jobstart(opts)
   return j
 end
 
-function M.make(extra_args)
-  local makeprg = vim.fn.expandcmd(vim.opt.makeprg:get())
-  -- split makeprg into command + args
-  local args = vim.split(makeprg, ' ')
-  local command = args[1]
-  table.remove(args, 1)
+function M.sh(command, opts)
+  return M.jobstart(vim.tbl_extend('keep', opts or {}, {
+    command = vim.env.SHELL,
+    args = { '-c', command },
+    -- strip 'sh -c' from the title (used by progress reporting etc.)
+    format_title = function(_, args)
+      return table.concat({ unpack(args, 2, #args) }, ' ')
+    end,
+  }))
+end
 
+function M.make(opts)
+  opts = opts or {}
+
+  local makeprg = vim.fn.expandcmd(vim.opt.makeprg:get())
   local cwd = vim.fn.expand('%:p:h')
 
   if not (string.match(makeprg, 'make') == nil) then
     -- detect makefile
-    cwd = require('lspconfig.util').root_pattern('Makefile')(cwd)
-  end
-
-  for _, v in pairs(extra_args or {}) do
-    table.insert(args, v)
+    opts.cwd = require('lspconfig.util').root_pattern('Makefile')(cwd)
   end
-  -- table.foreach(extra_args or {}, function(_, v) end)
 
-  return M.jobstart({
-    command = command,
-    args = args,
-    cwd = cwd,
-  })
-end
-
-function M.sh(command_string)
-  return M.jobstart({
-    command = vim.env.SHELL,
-    args = { '-c', command_string },
-
-    -- strip 'sh -c' from the title (used by progress reporting etc.)
-    format_title = function(_, args)
-      return table.concat({ unpack(args, 2, #args) }, ' ')
-    end,
-  })
+  return M.sh(makeprg, opts)
 end
 
 function M.list()
   require('telescope._extensions').manager['jobs']['jobs']()
 end
 
-function M.setup()
-  require('telescope').load_extension('jobs')
+function M.setup(opts)
+  opts = vim.tbl_extend('keep', opts, { telescope = true, mappings = true })
+
+  -- defaults
+  vim.g.job_enable_spinner = true
+  vim.g.job_populate_quickfix = 'onerror'
+  vim.g.job_attach = false
+
+  if opts.telescope then
+    require('telescope').load_extension('jobs')
+    vim.api.nvim_create_user_command('Jobs', M.list, { desc = 'list jobs managed by internal.job' })
+    if opts.mappings then
+      vim.keymap.set('n', '<leader>jl', M.list, { desc = 'list jobs managed by internal.job' })
+    end
+  end
+
+  vim.api.nvim_create_user_command('Make', function(a)
+    require('internal.job').make(a.fargs)
+  end, { nargs = '*', desc = 'run make target or &makeprg with internal.job' })
 
-  vim.keymap.set(
-    'n',
-    '<leader>J',
-    require('internal.job').list,
-    { desc = 'list jobs managed by internal.job' }
-  )
-  vim.api.nvim_create_user_command('Jobs', M.list, { desc = 'list jobs managed by internal.job' })
+  vim.api.nvim_create_user_command('Sh', function(a)
+    require('internal.job').sh(table.concat(a.fargs, ' '))
+  end, { nargs = '*', desc = 'run shell command with internal.job' })
+
+  if opts.mappings then
+    vim.keymap.set('n', '<leader>jm', M.make, { desc = 'run &makeprg with internal.job' })
+    vim.keymap.set('n', '<leader>js', ':Sh ', { desc = 'run shell commands with internal.job' })
+  end
 end
 
 return M
diff --git a/.vim/lua/internal/lsp.lua b/.vim/lua/internal/lsp.lua
index 1c8c29c..b0aa7dc 100644
--- a/.vim/lua/internal/lsp.lua
+++ b/.vim/lua/internal/lsp.lua
@@ -8,7 +8,7 @@
 local my_attach = function(client, bufnr)
   if vim.opt.diff:get() then
     vim.notify('not running LSP client in diff mode', vim.log.levels.WARN)
-    vim.lsp.stop_client()
+    vim.lsp.stop_client(client)
     return
   end
 
@@ -79,14 +79,16 @@ local my_attach = function(client, bufnr)
     )
   end
   if client.supports_method('textDocument/formatting') then
-    vim.api.nvim_buf_set_option(bufnr, 'formatexpr', 'v:lua.vim.lsp.formatexpr()')
+    -- if client.server_capabilities.documentFormattingProvider then
+    vim.api.nvim_set_option_value('formatexpr', 'v:lua.vim.lsp.formatexpr()', { buf = bufnr })
     vim.keymap.set('n', '<leader>f', vim.lsp.buf.format, { buffer = bufnr, desc = 'format' })
     vim.api.nvim_create_autocmd('BufWritePre', {
       buffer = bufnr,
-      group = group,
       callback = function()
+        vim.cmd.mkview({ bang = true })
         require('internal.lsp').format(vim.fn.expand('<afile>:p'))
       end,
+      group = vim.api.nvim_create_augroup('LspAutoFormat', {}),
     })
   end
   if client.supports_method('textDocument/typeDefinition') then
@@ -114,7 +116,8 @@ local my_attach = function(client, bufnr)
     )
   end
   if client.supports_method('textDocument/codeAction') then
-    vim.keymap.set('n', 'ga', vim.lsp.buf.code_action, { buffer = bufnr, desc = 'code action' })
+    -- vim.keymap.set('n', 'ga', vim.lsp.buf.code_action, { buffer = bufnr, desc = 'code action' })
+    vim.keymap.set('n', 'ga', vim.cmd.CodeActionMenu, { buffer = bufnr, desc = 'code action' })
   end
   if client.supports_method('textDocument/documentHighlight') then
     vim.keymap.set(
@@ -135,22 +138,25 @@ local my_exit = function(_, _, _) end
 
 local capabilities = function()
   local caps = vim.lsp.protocol.make_client_capabilities()
+
   -- enable lsp-based snippets
-  caps.textDocument.completion.completionItem.snippetSupport = true
-  caps.textDocument.completion.completionItem.resolveSupport = {
-    properties = {
-      'documentation',
-      'detail',
-      'additionalTextEdits',
-    },
-  }
+  caps = vim.tbl_extend('keep', caps or {}, require('cmp_nvim_lsp').default_capabilities())
+
   -- add window/workDoneProgress capability
   caps = vim.tbl_extend('keep', caps or {}, require('lsp-status').capabilities)
   return caps
 end
 
 local servers = {
-  clangd = {},
+  clangd = {
+    cmd = {
+      'clangd',
+      '--clang-tidy',
+      '--header-insertion=iwyu',
+      '--import-insertions',
+      '--header-insertion-decorators',
+    }
+  },
   elixirls = {
     cmd = { 'elixir-ls' },
     settings = {
@@ -177,7 +183,7 @@ local servers = {
   },
   lua_ls = {
     -- cmd = { 'lua-language-server' },
-    cmd = { 'luals' },
+    cmd = { 'luals' }, -- custom firejail-wrapped
     settings = {
       Lua = {
         runtime = {
@@ -237,15 +243,9 @@ local servers = {
       },
     },
   },
+  tsserver = {},
   pylsp = {},
-  -- tsserver = { disabled = true,
-  --   cmd = {
-  --     'toolbox', 'run', '--',
-  --     'sh', '-c',
-  --     '. /etc/profile.d/nvm.sh && typescript-language-server --stdio'
-  --   },
-  -- },
-  -- zls = { disabled = true },
+  zls = {},
 }
 
 local setup = function()
@@ -256,7 +256,7 @@ local setup = function()
     border = _G.floating_win_border,
   })
   vim.lsp.handlers['textDocument/signature_help'] =
-    vim.lsp.with(vim.lsp.handlers.signature_help, { border = _G.floating_win_border })
+      vim.lsp.with(vim.lsp.handlers.signature_help, { border = _G.floating_win_border })
 
   local caps = capabilities()
   for server, opts in pairs(servers) do
@@ -283,16 +283,18 @@ local format = function(afile)
     return
   end
 
-  local ok, isMatch = pcall(string.match, afile, '^/home/robert/devel/upstream')
-  if not ok or isMatch then
-    return
-  end
-
   local errhandler = function(err)
-    vim.notify('fmt failed: ' .. err, vim.log.levels.ERROR)
+    vim.notify('internal.lsp.format: ' .. err, vim.log.levels.ERROR)
     return err
   end
+
+  -- save folds
+  pcall(vim.cmd.mkview, { bang = true })
+
+  -- run lsp format
   xpcall(vim.lsp.buf.format, errhandler)
+
+  -- run organize imports for go
   if string.match(afile, '.go$') then
     xpcall(
       vim.lsp.buf.code_action,
@@ -300,8 +302,12 @@ local format = function(afile)
       { context = { only = { 'source.organizeImports' } }, apply = true }
     )
   end
+
+  -- restore folds
+  pcall(vim.cmd.silent, { 'loadview', bank = true })
+
   -- vim.notify('%#MoreMsg#󰃢%#Italic# fmt', vim.log.levels.INFO)
-  vim.notify('󰃢 ', vim.log.levels.INFO)
+  vim.notify('internal.lsp.format: 󰃢', vim.log.levels.INFO)
 end
 
 return {
diff --git a/.vim/lua/internal/lsp_symbols.lua b/.vim/lua/internal/lsp_symbols.lua
index ce8b1a7..97a1b0e 100644
--- a/.vim/lua/internal/lsp_symbols.lua
+++ b/.vim/lua/internal/lsp_symbols.lua
@@ -4,16 +4,16 @@ return {
   Boolean = '󰦍 ',
   Class = '󰙀 ',
   Color = '󰉦 ',
-  Constant = ' ',
+  Constant = '󰐀 ',
   Constructor = '󱇿 ',
-  Enum = ' ',
-  EnumMember = ' ',
+  Enum = '󱃢 ',
+  EnumMember = '󱃡 ',
   Event = '󱐋 ',
   -- Field = '󰽜 ',
   Field = '󰆈 ',
   File = '󰈔 ',
   Folder = '󰝰 ',
-  Function = ' ',
+  Function = '󰊕 ',
   Interface = '󱦜 ',
   Key = '󰌋 ',
   Keyword = '󰓽 ',
diff --git a/.vim/lua/internal/misc.lua b/.vim/lua/internal/misc.lua
index 8664cbc..a92b7aa 100644
--- a/.vim/lua/internal/misc.lua
+++ b/.vim/lua/internal/misc.lua
@@ -2,53 +2,6 @@
 
 local M = {}
 
-function M.do_under_cursor(obj, cb)
-  return cb(vim.fn.expand(vim.fn.expand(obj)))
-end
-
-function M.word_under_cursor(cb)
-  return M.do_under_cursor('<cword>', cb)
-end
-
-function M.expr_under_cursor(cb)
-  return M.do_under_cursor('<cexpr>', cb)
-end
-
-function M.file_under_cursor(cb)
-  return M.do_under_cursor('<cfile>', cb)
-end
-
-function M.open_under_cursor(cmd, detect_cmd)
-  return M.file_under_cursor(function(txt)
-    txt = vim.trim(txt)
-    if not cmd and detect_cmd then
-      if vim.regex([[^http.*$]]):match_str(txt) then
-        cmd = 'url-launcher'
-      end
-      if vim.regex([[^(\.\./|[\w\d_/\-])*(\.[\w\d]+)?$]]):match_str(txt) then
-        cmd = ':edit'
-      end
-      vim.notify('open_under_cursor: detected command ' .. txt .. ' => ' .. (cmd or '<nil>'))
-    end
-    if not cmd then
-      vim.fn.inputsave()
-      vim.ui.input('open with: ', function(result)
-        cmd = result
-      end)
-      vim.fn.inputrestore()
-      if not cmd then
-        return
-      end
-    end
-    -- detect vim command
-    if cmd:sub(1, 1) == ':' then
-      vim.cmd(cmd:sub(2) .. ' ' .. txt)
-    else
-      vim.loop.spawn(cmd, { args = { txt } })
-    end
-  end)
-end
-
 local function get_visual_selection()
   local top = vim.fn.getpos("'<")
   top = { ln = top[2], col = top[3] }
@@ -132,7 +85,7 @@ local get_searchdirs = function(dirs)
   return dirs
 end
 
-function onchoice(opts, cb)
+local function onchoice(opts, cb)
   if not cb then
     return
   end
@@ -143,7 +96,7 @@ function onchoice(opts, cb)
         return
       end
       require('telescope.actions').close(prompt_bufnr)
-      on_choice(selection.path)
+      cb(selection.path)
     end)
     return true
   end
@@ -209,35 +162,37 @@ function M.link_preview()
   local ln = vim.api.nvim_win_get_cursor(0)[1] - 1
 
   require('job')
-    .jobstart({
-      format_title = function()
-        return 'link-preview'
-      end,
-      command = vim.env.SHELL,
-      args = {
-        '-c',
-        [[ curl -sSfL ]]
+      .jobstart({
+        format_title = function()
+          return 'link-preview'
+        end,
+        command = vim.env.SHELL,
+        args = {
+          '-c',
+          [[ curl -sSfL ]]
           .. link
           .. [[ | grep -iPo '(?<=property="og:title" content=")([^\"]+)(?=")' ]],
-      },
-      populate_quickfix = false,
-      enable_recording = true,
-    })
-    :after_success(vim.schedule_wrap(function(j, code, _)
-      local results = j:result()
-      if not code == 0 or #results == 0 then
-        return
-      end
-
-      local ns = vim.api.nvim_create_namespace('')
-      vim.api.nvim_buf_set_extmark(buf, ns, ln, 0, {
-        virt_text = { { results[1], 'Error' } },
-        virt_text_pos = 'eol',
+        },
+        populate_quickfix = false,
+        enable_recording = true,
       })
-    end))
+      :after_success(vim.schedule_wrap(function(j, code, _)
+        local results = j:result()
+        if not code == 0 or #results == 0 then
+          return
+        end
+
+        local ns = vim.api.nvim_create_namespace('')
+        vim.api.nvim_buf_set_extmark(buf, ns, ln, 0, {
+          virt_text = { { results[1], 'Error' } },
+          virt_text_pos = 'eol',
+        })
+      end))
 end
 
 function M.setup(opts)
+  opts = opts or {}
+
   if opts.sortline then
     vim.api.nvim_create_user_command(
       'SortLine',
diff --git a/.vim/lua/internal/snips.lua b/.vim/lua/internal/snips.lua
index 91bce13..bb9fdef 100644
--- a/.vim/lua/internal/snips.lua
+++ b/.vim/lua/internal/snips.lua
@@ -48,7 +48,7 @@ local comment = function(wrapped, blockcomment)
     })
     local tbl = vim.split(cs or '', '%s', { plain = true, trimempty = true })
     return (#tbl == 0) and { '', '' }
-      or ((#tbl == 1) and { tbl[1] .. ' ', '' } or { tbl[1], tbl[2] })
+        or ((#tbl == 1) and { tbl[1] .. ' ', '' } or { tbl[1], tbl[2] })
   end
 
   if type(wrapped) ~= 'table' then
@@ -78,12 +78,12 @@ end
 local exec = function(command)
   return ls.f(function(_, _, ...)
     local results, code = require('plenary.job')
-      :new({
-        command = vim.env.SHELL,
-        args = { '-c', ... },
-        enable_recording = true,
-      })
-      :sync()
+        :new({
+          command = vim.env.SHELL,
+          args = { '-c', ... },
+          enable_recording = true,
+        })
+        :sync()
     if not code == 0 or #results == 0 then
       error(string.format('exec "%s" failed', ...))
     end
@@ -102,6 +102,10 @@ local file_contents = function(path, vargs)
 end
 
 ls.setup({
+  history = true,
+  update_events = 'TextChanged,TextChangedI',
+  delete_check_events = 'TextChanged',
+
   ext_opts = {
     [types.snippet] = {
       active = {
@@ -118,13 +122,18 @@ ls.setup({
       },
     },
   },
-
   snip_env = vim.tbl_extend('keep', ls.session.config.snip_env, {
+    f = ls.f,
+    i = ls.i,
+    s = ls.s,
+    t = ls.t,
+    c = ls.c,
+    sn = ls.sn,
+    fmt = require('luasnip.extras.fmt').fmt,
     autowrap = autowrap,
     exec = exec,
     comment = comment,
     file_contents = file_contents,
-
     user_email = {
       exec([[git config --get user.name]]),
       ls.t(' <'),
@@ -139,7 +148,7 @@ ls.setup({
 })
 
 -- snipmate snippets
-require('luasnip.loaders.from_snipmate').lazy_load({ paths = './after/snippets' })
+-- require('luasnip.loaders.from_snipmate').lazy_load({ paths = './after/snippets' })
 -- lua snippets
 require('luasnip.loaders.from_lua').lazy_load({ paths = './snippets' })
 
diff --git a/.vim/lua/internal/sortline.lua b/.vim/lua/internal/sortline.lua
new file mode 100644
index 0000000..69f7f64
--- /dev/null
+++ b/.vim/lua/internal/sortline.lua
@@ -0,0 +1,78 @@
+local M = {}
+
+local function get_visual_selection(intact)
+  intact = intact or false
+  local top = vim.fn.getpos("'<")
+  top = { ln = top[2], col = top[3] }
+  local bot = vim.fn.getpos("'>")
+  bot = { ln = bot[2], col = bot[3] }
+  local lines = vim.api.nvim_buf_get_lines(0, (top.ln - 1), bot.ln, false)
+  if not intact then
+    if vim.opt.selection:get() == 'inclusive' then
+      lines[#lines] = lines[#lines]:sub(1, bot.col)
+    else
+      lines[#lines] = lines[#lines]:sub(1, (bot.col - 1))
+    end
+    lines[1] = lines[1]:sub(top.col)
+  end
+  return top, bot, lines
+end
+
+local function sortline(line, a, o, separator)
+  separator = separator or ' '
+  local result, _ = string.gsub(line, string.sub(line, a, o),
+    table.concat(vim.fn.sort(vim.fn.split(string.sub(line, a, o), separator)), separator), 1)
+  return result
+end
+
+function M.sort_lines(_, preview_ns, preview_bufnr)
+  local bufnr = vim.api.nvim_get_current_buf()
+
+  local top, bot, lines = get_visual_selection(true)
+  vim.pretty_print(top, bot, lines)
+  for i = 1, #lines do
+    local a = 0; if i == 1 then a = top.col end
+    local o = -1; if i == #lines then o = bot.col end
+    lines[i] = sortline(lines[i], a, o, ' ')
+  end
+
+  if not preview_ns then
+    vim.api.nvim_buf_set_lines(bufnr, top.ln - 1, bot.ln, false, lines)
+    return 0
+  end
+
+  -- inccommand preview
+  if preview_ns ~= nil then
+    for i, line in ipairs(lines) do
+      vim.api.nvim_buf_set_extmark(bufnr, preview_ns, top.ln + i - 2, 0, {
+        hl_mode = 'combine',
+        virt_text_pos = 'overlay',
+        virt_text = { { line, 'Substitute' } },
+      })
+
+      if preview_bufnr ~= nil then
+        local prefix = string.format('|%d| ', top.ln + i - 1)
+        vim.api.nvim_buf_set_lines(preview_bufnr, i - 1, -1, false, { prefix .. line })
+        vim.api.nvim_buf_add_highlight(
+          preview_bufnr,
+          preview_ns,
+          'Substitute',
+          i,
+          #prefix,
+          #prefix + #line
+        )
+      end
+    end
+    return (#lines > 1 and 2 or 1)
+  end
+end
+
+function M.setup(opts)
+  vim.api.nvim_create_user_command(
+    'SortLine',
+    M.sort_lines,
+    { desc = 'sort line', range = '%', preview = M.sort_lines }
+  )
+end
+
+return M
diff --git a/.vim/lua/internal/statusline.lua b/.vim/lua/internal/statusline.lua
index 9d1ddc7..8ed5cae 100644
--- a/.vim/lua/internal/statusline.lua
+++ b/.vim/lua/internal/statusline.lua
@@ -7,6 +7,8 @@ function _G.statusline()
 
   sl = sl .. [[%-h%-w%-q%-f %-m %-r]]
 
+  sl = sl .. statusline_append(statusline_grapple(bufnr), 'StatusLineGrapple', hi)
+
   -- sl = sl .. statusline_append(statusline_ts(), 'StatusLineTreesitter', hi)
 
   sl = sl .. [[ %= ]] -- spacer
@@ -64,18 +66,16 @@ function _G.statusline_append(element, hi, default_hi, opts)
 end
 
 function _G.statusline_combine_hi(outer_name, inner_name)
-  local outer = vim.api.nvim_get_hl_by_id(vim.api.nvim_get_hl_id_by_name(outer_name), true)
-  local inner = vim.api.nvim_get_hl_by_id(vim.api.nvim_get_hl_id_by_name(inner_name), true)
-  local hi = 'auto' .. outer_name .. inner_name
-  if pcall(vim.api.nvim_get_hl_by_name, hi, true) then
-    return hi
-  end
-  local gui
-  gui = inner.bold and 'bold'
-  gui = inner.italic and 'italic'
-  gui = inner.underline and 'underline'
-  vim.api.nvim_set_hl(0, hi, { bg = outer.background, fg = inner.foreground, gui = gui })
-  return hi
+  local outer = vim.api.nvim_get_hl(0, { name = outer_name, create = false })
+  local inner = vim.api.nvim_get_hl(0, { name = inner_name, create = false })
+  local name = ('auto' .. outer_name .. inner_name)
+  vim.api.nvim_get_hl(0,
+    { name = name, create = true })
+  local hi = inner
+  hi.bg = inner.fg
+  hi.fg = outer.bg
+  vim.api.nvim_set_hl(0, name, hi)
+  return name
 end
 
 function _G.statusline_lsp_diagnostics(bufnr, default_hi)
@@ -86,20 +86,20 @@ function _G.statusline_lsp_diagnostics(bufnr, default_hi)
   if #error_num > 0 then
     local errs = vim.fn.sign_getdefined('DiagnosticSignError')[1].text .. #error_num
     s = s
-      .. statusline_append(
-        errs,
-        statusline_combine_hi('StatusLineDiagnostics', 'DiagnosticError'),
-        default_hi
-      )
+        .. statusline_append(
+          errs,
+          statusline_combine_hi('StatusLine', 'DiagnosticError'),
+          default_hi
+        )
   end
   if #warn_num > 0 then
     local warns = vim.fn.sign_getdefined('DiagnosticSignWarn')[1].text .. #warn_num
     s = s
-      .. statusline_append(
-        warns,
-        statusline_combine_hi('StatusLineDiagnostics', 'DiagnosticWarn'),
-        default_hi
-      )
+        .. statusline_append(
+          warns,
+          statusline_combine_hi('StatusLine', 'DiagnosticWarn'),
+          default_hi
+        )
   end
   return vim.trim(s)
 end
@@ -128,6 +128,16 @@ function _G.statusline_ts()
   return table.concat(s, ' > ')
 end
 
+function _G.statusline_grapple(bufnr)
+  local g = package.loaded.grapple
+  if not g then return '' end
+  return g.statusline()
+  -- if not g or not g.exists({ buffer = bufnr }) then
+  --   return ''
+  -- end
+  -- return '➥ ' .. g.key({ buffer = bufnr })
+end
+
 function _G.statusline_lspstatus()
   local lst = package.loaded['lsp-status']
   if not lst then
@@ -175,12 +185,12 @@ function _G.statusline_notify(default_hi)
   local s = ''
   if n.hi then
     s = s
-      .. statusline_append(
-        n.lvl_string .. ' ',
-        statusline_combine_hi('StatusLineNotify', n.hi),
-        default_hi,
-        { append_space = false }
-      )
+        .. statusline_append(
+          n.lvl_string .. ' ',
+          statusline_combine_hi('StatusLineNotify', n.hi),
+          default_hi,
+          { append_space = false }
+        )
   end
   s = s .. statusline_append(vim.inspect(n.message), 'StatusLineNotify', default_hi)
   return vim.trim(s)
diff --git a/.vim/lua/internal/syncbg.lua b/.vim/lua/internal/syncbg.lua
new file mode 100644
index 0000000..418e17e
--- /dev/null
+++ b/.vim/lua/internal/syncbg.lua
@@ -0,0 +1,46 @@
+local M = {}
+
+M.init = function()
+  -- get tty
+  local tty_handle = io.popen('tty')
+  if tty_handle == nil then return end
+  local tty = tty_handle:read('*a')
+  tty_handle:close()
+  if tty:find("not a tty") then error('not a tty') end
+
+  M.tty = tty
+end
+
+M.update = function()
+  if M.tty == nil then return end
+
+  if not M.normal then
+    -- get colorscheme Normal highlight
+    local normal = vim.api.nvim_get_hl(0, { name = 'Normal', create = false })
+    if (normal.bg == nil) then return end
+    -- cache old value for use with VimResume
+    M.normal = normal
+  end
+
+  -- emit CSI to change terminal background to Normal.bg
+  os.execute('printf "\\033]11;' .. string.format('#%06x', M.normal.bg) .. '\\007" > ' .. M.tty)
+
+  -- set colorscheme Normal.bg to NONE, making it transparent
+  vim.cmd [[hi Normal ctermbg=NONE guibg=NONE]]
+end
+
+M.reset = function()
+  if M.tty == nil then return end
+
+  os.execute('printf "\\033]111\\007" > ' .. M.tty)
+end
+
+M.setup = function(opts)
+  opts = opts or {}
+
+  M.init()
+  vim.api.nvim_create_autocmd({ 'ColorScheme', 'UIEnter', 'VimResume' }, { callback = M.update })
+  vim.api.nvim_create_autocmd({ 'UILeave' }, { callback = M.reset })
+end
+
+return M
diff --git a/.vim/lua/internal/zk.lua b/.vim/lua/internal/zk.lua
new file mode 100644
index 0000000..5e17786
--- /dev/null
+++ b/.vim/lua/internal/zk.lua
@@ -0,0 +1,7 @@
+-- vim.keymap.set('n', '<leader>zn', function() require('zk').new() end, {buffer=true})
+-- vim.keymap.set('n', '<leader>zi', function() require('zk').index() end, {buffer=true})
+-- vim.cmd [[ command! -nargs=* ZettelkastenNew lua require('zk').new(nil, <f-args>) ]]
+
+vim.keymap.set('n', 'gt', function()
+  require('telescope._extensions.todo-comments').exports.todo({ cwd = vim.env.ZK_NOTEBOOK_DIR })
+end, { desc = 'show zk todos', buffer = true })