65 lines
1.8 KiB
Lua
65 lines
1.8 KiB
Lua
local M = {}
|
|
|
|
local output_viewer_pid = 0
|
|
|
|
local compile = function(file)
|
|
return vim.fn.jobstart({ "compiledoc", file }, {
|
|
on_exit = function(_, exit_code) if exit_code == 0 then print("Finished compiling.") end end,
|
|
on_stderr = function(_, err, _) print(table.concat(err, "\n")) end,
|
|
stderr_buffered = true
|
|
})
|
|
end
|
|
|
|
local file_exists = function(name)
|
|
local f=io.open(name,"r")
|
|
if f~=nil then io.close(f) return true else return false end
|
|
end
|
|
|
|
|
|
M.open_document_preview = function()
|
|
local filename = vim.api.nvim_buf_get_name(0)
|
|
local compile_job = compile(filename)
|
|
|
|
vim.api.nvim_create_augroup("compileDoc", {})
|
|
|
|
vim.api.nvim_create_autocmd("BufWritePost", {
|
|
group = "compileDoc",
|
|
pattern = { "*.tex", "*.md", "*.puml" },
|
|
callback = function() compile(filename) end
|
|
})
|
|
|
|
local output_file_extension
|
|
local output_file_viewer
|
|
|
|
if string.match(filename, "%.tex$") or string.match(filename, "%.md") then
|
|
output_file_extension = ".pdf"
|
|
output_file_viewer = "zathura"
|
|
elseif string.match(filename, "%.puml$") then
|
|
output_file_extension = ".png"
|
|
output_file_viewer = "feh"
|
|
end
|
|
|
|
local output_filename = string.gsub(filename, "%..+$", output_file_extension)
|
|
|
|
vim.fn.jobwait({compile_job})
|
|
|
|
if not file_exists(output_filename) then
|
|
local scan = require'plenary.scandir'
|
|
local build_dir = scan.scan_dir({ '.', '..' }, { depth = 1, add_dirs = true, search_pattern = 'build' })
|
|
local result = scan.scan_dir(build_dir[1], { depth = 3, search_pattern = ".*%.pdf" })
|
|
|
|
output_filename = result[1]
|
|
end
|
|
|
|
output_viewer_pid = vim.fn.jobstart({ output_file_viewer, output_filename })
|
|
end
|
|
|
|
M.close_document_preview = function()
|
|
vim.api.nvim_del_augroup_by_name("compileDoc")
|
|
if (output_viewer_pid ~= 0) then
|
|
vim.fn.jobstop(output_viewer_pid)
|
|
end
|
|
end
|
|
|
|
return M
|