-
Notifications
You must be signed in to change notification settings - Fork 8
/
autocmd.lua
77 lines (65 loc) 路 2.63 KB
/
autocmd.lua
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
local editor = require('distant.editor')
local log = require('distant.log')
local u = require('distant.utils')
local vars = require('distant.vars')
local function _initialize()
log.trace('Initializing autocmds')
-- Assign appropriate handlers for distant:// scheme
u.augroup('distant', function()
-- If we enter a buffer that is not initialized, we trigger a BufReadCmd
u.autocmd('BufEnter', 'distant://*', function()
--- @diagnostic disable-next-line:missing-parameter
local bufnr = tonumber(vim.fn.expand('<abuf>'))
if bufnr > 0 and vars.buf(bufnr).remote_path.is_unset() then
log.fmt_debug('buf %s is not initialized, so triggering BufReadCmd', bufnr)
vim.api.nvim_exec_autocmds('BufReadCmd', {
group = 'distant',
--- @diagnostic disable-next-line:missing-parameter
pattern = vim.fn.expand('<amatch>'),
})
end
end)
-- Primary entrypoint to load remote files
u.autocmd('BufReadCmd,FileReadCmd', 'distant://*', function()
--- @diagnostic disable-next-line:missing-parameter
local bufnr = tonumber(vim.fn.expand('<abuf>'))
--- @diagnostic disable-next-line:missing-parameter
local fname = vim.fn.expand('<amatch>')
local path = u.strip_prefix(fname, 'distant://')
local line, col
path, line, col = u.strip_line_col(path)
-- Ensure our buffer is named without the line/column,
-- but with the appropriate prefix
vim.api.nvim_buf_set_name(bufnr, 'distant://' .. path)
log.fmt_debug('Reading %s into buf %s', path, bufnr)
editor.open({
path = path,
bufnr = bufnr,
reload = true,
line = line,
col = col,
})
end)
-- Primary entrypoint to write remote files
u.autocmd('BufWriteCmd', 'distant://*', function()
--- @diagnostic disable-next-line:missing-parameter
local bufnr = tonumber(vim.fn.expand('<abuf>'))
if type(bufnr) == 'number' and bufnr ~= -1 then
log.fmt_debug('Writing buf %s', bufnr)
editor.write(bufnr)
end
end)
end)
end
local is_initialized = false
return {
--- Configures the autocmds associated with this plugin
---
--- Subsequent calls will do nothing
initialize = function()
if not is_initialized then
_initialize()
is_initialized = true
end
end
}