slides.nvim/lua/slides.lua

77 lines
1.7 KiB
Lua
Raw Permalink Normal View History

2021-09-29 18:36:17 -03:00
local M = {}
local state = {
2023-12-31 13:14:21 +03:00
win = nil,
2021-09-29 18:36:17 -03:00
}
local config = {
2023-12-31 13:14:21 +03:00
bin = "slides",
2023-12-31 12:26:55 +03:00
fullscreen = true,
style = "minimal",
2023-12-31 13:14:21 +03:00
border = "shadow",
2021-09-29 18:36:17 -03:00
}
function M.close()
if state.win ~= nil then
vim.api.nvim_win_close(state.win, true)
end
end
function M.setup(user_config)
2023-12-31 13:14:21 +03:00
config = vim.tbl_deep_extend("force", config, user_config or {})
2021-09-29 18:36:17 -03:00
2023-12-31 13:14:21 +03:00
vim.cmd(
[[command! -nargs=? -complete=file Slides :lua require"slides".show('<f-args>')]]
)
vim.cmd([[command! SlidesClose :lua require"slides".close()]])
2021-09-29 18:36:17 -03:00
end
function M.show(file)
2021-09-29 20:16:44 -03:00
local window = vim.api.nvim_get_current_win()
2021-09-29 18:36:17 -03:00
local opts = {
2023-12-31 12:26:55 +03:00
style = config.style,
2021-09-29 18:36:17 -03:00
relative = "editor",
2023-12-31 13:14:21 +03:00
width = config.fullscreen and vim.api.nvim_get_option("columns")
or vim.api.nvim_win_get_width(window),
height = config.fullscreen and vim.api.nvim_get_option("lines")
or vim.api.nvim_win_get_height(window),
2021-09-29 18:36:17 -03:00
row = 1,
col = 1,
2023-12-31 12:26:55 +03:00
border = config.border,
2021-09-29 18:36:17 -03:00
}
local input = string.len(file) == 0 and vim.api.nvim_get_current_buf() or file
2023-12-31 13:14:21 +03:00
local is_file = type(input) == "string"
local filetype = is_file and vim.fn.fnamemodify(input, ":e"):gsub('"', "")
or vim.api.nvim_buf_get_option(input, "filetype")
2021-09-29 18:36:17 -03:00
2023-12-31 13:14:21 +03:00
if not vim.tbl_contains({ "md", "markdown" }, filetype) then
vim.api.nvim_err_writeln("Invalid filetype")
2021-09-29 18:36:17 -03:00
return
end
if vim.fn.executable(config.bin) ~= 1 then
2023-12-31 13:14:21 +03:00
vim.api.nvim_err_writeln("Executable not found")
2021-09-29 18:36:17 -03:00
return
end
local bufnr = vim.api.nvim_create_buf(false, true)
local win = vim.api.nvim_open_win(bufnr, true, opts)
state.win = win
2023-12-31 13:14:21 +03:00
vim.cmd("startinsert!")
2021-09-29 18:36:17 -03:00
2023-12-31 13:14:21 +03:00
vim.fn.termopen(
config.bin .. " " .. (is_file and input or vim.api.nvim_buf_get_name(input)),
{
on_exit = function()
M.close()
end,
}
)
2021-09-29 18:36:17 -03:00
end
return M