summary refs log tree commit diff
path: root/.vim/lua/misc.lua
blob: d1686c204f11ad8e0a73be2ac4b30f42fc85aee3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-- misc stuff implemented in lua

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)
  return M.file_under_cursor(function(txt)
    if not cmd then
      vim.fn.inputsave()
      cmd = vim.fn.input('open with: ')
      vim.fn.inputrestore()
    end
    if cmd then
      c = vim.loop.spawn(cmd, { args = {txt} }, function() c:close() end)
    end
  end)
end

function get_visual_selection()
  pos1 = vim.fn.getpos("'<")
  ln1, col1 = pos1[2], pos1[3]
  pos2 = vim.fn.getpos("'>")
  ln2, col2 = pos2[2], pos2[3]
  lines = vim.fn.getline(ln1, ln2)
  if vim.opt.selection:get() == 'inclusive' then
    lines[#lines] = lines[#lines]:sub(1,col2)
  else
    lines[#lines] = lines[#lines]:sub(1,col2-1)
  end
  lines[1] = lines[1]:sub(col1)
  return pos1, pos2, lines
end

function M.sort_line()
  local fn = vim.fn
  pos1, pos2, lines = get_visual_selection()
  for i=1, #lines do
    local ln = pos1[2] + (i-1)
    local line = fn.join(fn.sort(fn.split(lines[i], " ")))
    line = fn.substitute(fn.getline(ln), lines[i], line, "")
    vim.fn.setline(ln, line)
  end
end

function M.live_grep(dirs)
  dirs = dirs or vim.fn.input('Dirs: ')
  require('telescope.builtin').live_grep {search_dirs=vim.split(dirs, ',')}
end

function M.find_files(dirs)
  dirs = dirs or vim.fn.input('Dirs: ')
  require('telescope.builtin').find_files {search_dirs=vim.split(dirs, ',')}
end

return M