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
|
local orig = {}
local function parse(line)
local prefix, name = line:match('^([| ]*)(.-)%s*$')
local _, depth = prefix:gsub('|', '')
return depth, name
end
local function open_entry()
local buf = vim.api.nvim_get_current_buf()
local lnum = vim.fn.line('.')
local depth, name = parse(vim.fn.getline(lnum))
local function fallback()
local rhs = orig[buf] and orig[buf].rhs
if rhs and rhs ~= '' then
vim.api.nvim_feedkeys(vim.keycode(rhs), 'n', false)
end
end
if name == '' or name:sub(-1) == '/' or depth == 0 then
return fallback()
end
local parts, cur, top = {}, depth, nil
for l = lnum - 1, 1, -1 do
local d, n = parse(vim.fn.getline(l))
if n ~= '' and d < cur and n:sub(-1) == '/' then
if d == 0 then
top = n
break
end
table.insert(parts, 1, n)
cur = d
end
end
if not top or not top:match('^%a+://') then
return fallback()
end
vim.cmd('edit ' .. vim.fn.fnameescape(top .. table.concat(parts) .. name))
end
local function install(buf)
local m = vim.fn.maparg('<CR>', 'n', false, true)
if m.callback then return end
orig[buf] = m
vim.keymap.set('n', '<CR>', open_entry, { buffer = buf, silent = true })
end
vim.api.nvim_create_autocmd({ 'BufEnter', 'CursorMoved' }, {
callback = function(a)
if vim.bo[a.buf].filetype == 'netrw' and vim.g.netrw_liststyle == 3 then
install(a.buf)
end
end,
})
|