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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
-- 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, 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
local c
c = vim.loop.spawn(cmd, { args = {txt} }, function() c:close() end)
end
end)
end
local function get_visual_selection()
local pos1 = vim.fn.getpos("'<")
local ln1, col1 = pos1[2], pos1[3]
local pos2 = vim.fn.getpos("'>")
local ln2, col2 = pos2[2], pos2[3]
local 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
local pos1, _, 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)
if not dirs then vim.ui.input('Dirs: ', function(result) dirs = result end) end
if not dirs then dirs = '%:p:h' end
require('telescope.builtin').live_grep {
search_dirs=vim.split(dirs, ','),
prompt_title='Live Grep: '.. vim.fn.expand(dirs),
}
end
function M.find_files(dirs)
if not dirs then vim.ui.input('Dirs: ', function(result) dirs = result end) end
if not dirs then dirs = '%:h:p' end
require('telescope.builtin').find_files {
search_dirs=vim.split(dirs, ','),
prompt_title='Find Files: '.. vim.fn.expand(dirs),
}
end
function M.fold_block() -- {{{
local Comment = require('Comment.api')
local m = vim.api.nvim_buf_get_mark
local inital_cusor_pos = vim.api.nvim_win_get_cursor(0)
local spos, epos = m(0, '<'), m(0, '>')
-- do start of selection
vim.api.nvim_win_set_cursor(0, spos)
Comment.insert_linewise_eol()
vim.api.nvim_feedkeys('\\<esc>', 'ni', true)
local sln = vim.api.nvim_get_current_line()
vim.api.nvim_set_current_line(sln .. '{{{')
-- do end of selection
vim.api.nvim_win_set_cursor(0, epos)
Comment.insert_linewise_eol()
vim.api.nvim_feedkeys('\\<esc>', 'ni', true)
local eln = vim.api.nvim_get_current_line()
vim.api.nvim_set_current_line(eln .. '}}}')
-- restore initial cursor postition
vim.api.nvim_win_set_cursor(0, inital_cusor_pos)
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
|