-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvim-table-mode.lua
53 lines (48 loc) · 1.26 KB
/
vim-table-mode.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
---@param buf integer?
local function table_mode_toggle(buf)
buf = buf or vim.api.nvim_get_current_buf()
if not vim.api.nvim_buf_is_valid(buf) then
return
end
vim.api.nvim_buf_call(buf, function()
local ft = vim.bo.ft
local table_mode_buf_is_active = vim.fn['tablemode#IsActive']() == 1
if ft == 'markdown' and not table_mode_buf_is_active then
vim.cmd.TableModeEnable({
mods = {
silent = true,
emsg_silent = true,
},
})
return
end
if ft ~= 'markdown' and table_mode_buf_is_active then
vim.cmd.TableModeDisable({
mods = {
silent = true,
emsg_silent = true,
},
})
end
end)
end
table_mode_toggle()
vim.api.nvim_create_autocmd({ 'Filetype', 'BufEnter' }, {
group = vim.api.nvim_create_augroup('TableModeAutoToggle', {}),
callback = function(info)
table_mode_toggle(info.buf)
end,
})
vim.api.nvim_create_autocmd('BufWritePre', {
group = vim.api.nvim_create_augroup('TableModeFormatOnSave', {}),
callback = function(info)
if
vim.bo[info.buf].ft == 'markdown'
and vim.api.nvim_get_current_line():match('^%s*|')
then
vim.cmd.TableModeRealign({
mods = { emsg_silent = true },
})
end
end,
})