build.lua (2891B)
1 local gf = require('goto-ref') 2 local util = require('util') 3 4 vis.events.subscribe(vis.events.FILE_SAVE_PRE, function(file) 5 local M = require('plugins/vis-lint') 6 M.logger = function(str, level) 7 if level == M.log.ERROR then 8 vis:message(str) 9 end 10 end 11 M.fixers["ansi_c"] = { "clang-format -fallback-style=none" } 12 M.fixers["bibtex"] = { "bibtidy" } 13 M.fixers["cpp"] = { "clang-format -fallback-style=none" } 14 M.fixers["json"] = { "jq --tab" } 15 return M.fix(file) 16 end) 17 18 local logger = function(clear, ostr, estr) 19 if ostr == nil and estr == nil then return end 20 if clear then util.message_clear(vis) end 21 if ostr then vis:message(ostr) end 22 if estr then vis:message(estr) end 23 vis:message(string.rep("=", vis.win.viewport.width / 2)) 24 end 25 26 local function build_files(win) 27 local build_tex = function (f) 28 local cmd = "xelatex -halt-on-error -shell-escape " 29 30 -- build in draft mode to update references 31 local err, ostr = vis:pipe(cmd .. "-draftmode " .. f.name) 32 if err ~= 0 then logger(true, ostr) return false end 33 34 local fp = util.splitext(f.name) 35 -- update refrences 36 vis:command("!biber " .. fp .. " >/dev/null") 37 -- update glossary 38 -- vis:command("!makeglossaries " .. fp .. " >/dev/null") 39 40 -- build actual pdf 41 err = vis:pipe(cmd .. f.name) 42 if err ~= 0 then return false end 43 44 -- check for FIXMEs 45 local pos = win.selection.pos 46 local info 47 vis:command("x/FIXME/") 48 -- pathological case: if cursor is on the end of a FIXME 49 if #win.selections and pos ~= win.selection.pos then 50 info = "FIXMEs: " .. tostring(#win.selections) 51 end 52 vis:feedkeys("<Escape><Escape>") 53 win.selection.pos = pos 54 55 -- reload pdf (zathura does this automatically) 56 -- vis:command('!pkill -HUP mupdf') 57 58 return true, info 59 end 60 61 local build_python = function (f) 62 local _, ostr, estr = vis:pipe('python ' .. f.name) 63 logger(true, ostr, estr) 64 if estr then return false end 65 return true 66 end 67 68 local build_c = function (f) 69 local _, ostr, estr = vis:pipe('./build.sh') 70 logger(true, ostr, estr) 71 gf.setup_iterators_from_text(estr, function(str) 72 local result = str:find("^/usr/include") ~= nil 73 result = result or str:find("^In file included") 74 return result 75 end) 76 return true 77 end 78 79 local lang = {} 80 lang["ansi_c"] = build_c 81 lang["cpp"] = build_c 82 lang["latex"] = build_tex 83 lang["python"] = build_python 84 85 local builder = lang[win.syntax] 86 if builder == nil then 87 builder = function () 88 vis:info(win.syntax .. ': filetype not supported') 89 return false 90 end 91 end 92 93 win:map(vis.modes.NORMAL, " c", function () 94 vis:command('X/.*/w') 95 local s = "built: " .. win.file.name 96 local ret, info = builder(win.file) 97 if info then s = s .. " | info: " .. info end 98 if ret == true then vis:info(s) end 99 return ret 100 end, "build file in current window") 101 end 102 vis.events.subscribe(vis.events.WIN_OPEN, build_files)