summary refs log tree commit diff
path: root/.vim
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2022-02-08 23:57:02 +0100
committerRobert Günzler <r@gnzler.io>2022-02-08 23:57:02 +0100
commit2e3ebd3b8e2fbf88e1681ef52248ad3de1c4a251 (patch)
tree26cd71a54ddc00cca0b723b156588a4ed68cd6a9 /.vim
parentd85ee2757c412e2df4d45d23d7fef88d0c66cfe0 (diff)
vim: markdown: add link preview helper
Diffstat (limited to '.vim')
-rw-r--r--.vim/after/ftplugin/markdown.lua4
-rw-r--r--.vim/lua/job.lua27
-rw-r--r--.vim/lua/misc.lua43
3 files changed, 62 insertions, 12 deletions
diff --git a/.vim/after/ftplugin/markdown.lua b/.vim/after/ftplugin/markdown.lua
index 45219ee..373b79e 100644
--- a/.vim/after/ftplugin/markdown.lua
+++ b/.vim/after/ftplugin/markdown.lua
@@ -11,6 +11,10 @@ vim.keymap.set('n',
   function() require('misc').open_under_cursor('url-launcher') end
 )
 
+vim.keymap.set('n',
+  '<leader>l',
+  function() require('misc').link_preview() end
+)
 
 -- vim.keymap.set('n', '<leader>zn', function() require('zk').new() end)
 -- vim.keymap.set('n', '<leader>zi', function() require('zk').index() end)
diff --git a/.vim/lua/job.lua b/.vim/lua/job.lua
index 9a235c4..c14e978 100644
--- a/.vim/lua/job.lua
+++ b/.vim/lua/job.lua
@@ -7,7 +7,7 @@ local M = {
 }
 
 --create a new job and register
---@return /usr/local/share/nvim/site/pack/packer/start/plenary.nvim/lua/plenary/job.lua
+--@return ~/.local/share/nvim/site/pack/packer/start/plenary.nvim/lua/plenary/job.lua
 function M.jobstart(opts)
   opts = opts or {}
 
@@ -19,14 +19,20 @@ function M.jobstart(opts)
     title = opts.command
   end
 
+  if opts.populate_quickfix == nil then
+    opts.populate_quickfix = true
+  end
+
   opts.enable_recording = true -- enable job:result()
 
-  opts.on_exit = vim.schedule_wrap(function(j, _)
+  opts.on_exit = vim.schedule_wrap(function(j, code)
     spinner.on_progress('end', j.pid, j.pid)
     spinner.on_exit(nil, nil, j.pid)
 
-    local lines = j:result()
-    if not lines or #lines < 1 then
+    local lines
+    if code == 0 then
+      lines = j:result()
+    else
       lines = j:stderr_result()
     end
 
@@ -35,12 +41,13 @@ function M.jobstart(opts)
       lines[i] = (lines[i]):gsub(string.char(27) .. '[[0-9;]*m', '')
     end
 
-    vim.fn.setqflist({}, ' ', {
-      title = title,
-      lines = lines,
-    })
-
-    if #lines > 0 then
+    -- only show quickfix if there is output
+    if opts.populate_quickfix and #lines > 0 then
+      vim.bo.errorformat = '%m'
+      vim.fn.setqflist({}, 'r', {
+        title = title,
+        items = lines,
+      })
       vim.api.nvim_command('doautocmd QuickFixCmdPost')
     end
 
diff --git a/.vim/lua/misc.lua b/.vim/lua/misc.lua
index 7cb2ad9..0c4d191 100644
--- a/.vim/lua/misc.lua
+++ b/.vim/lua/misc.lua
@@ -18,14 +18,29 @@ function M.file_under_cursor(cb)
   return M.do_under_cursor('<cfile>', cb)
 end
 
-function M.open_under_cursor(cmd)
+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
-    if cmd then
+    -- detect vim command
+    if cmd:sub(1, 1) == ":" then
+      vim.cmd(cmd:sub(2) .. ' ' .. txt)
+    else
+      local c
       c = vim.loop.spawn(cmd, { args = {txt} }, function() c:close() end)
     end
   end)
@@ -67,4 +82,28 @@ function M.find_files(dirs)
   require('telescope.builtin').find_files {search_dirs=vim.split(dirs, ',')}
 end
 
+local function render_link_preview(buf, ln, link)
+  local j = require('plenary.job'):new({
+    command = vim.env.SHELL,
+    args = {'-c', [[ curl -sSfL ]] .. link .. [[ | grep -iPo '(?<=property="og:title" content=")([^\"]+)(?=")' ]]},
+    enable_recording = true,
+  })
+  local results, code = j:sync()
+  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
+function M.link_preview()
+  local link = vim.fn.expand('<cfile>')
+  if not link then return end
+
+  local ln = (vim.api.nvim_win_get_cursor(0)[1]) - 1
+
+  render_link_preview(0, ln, link)
+end
+
 return M