build.lua (2996B)
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["bibtex"] = { "bibtidy" } 12 M.fixers["c"] = { "clang-format -fallback-style=none" } 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 -- reload pdf (zathura does this automatically) 45 -- vis:command('!pkill -HUP mupdf') 46 47 return true 48 end 49 50 local run_python = function (f) 51 local _, ostr, estr = vis:pipe('python ' .. f.name) 52 logger(true, ostr, estr) 53 if estr then return false end 54 return true 55 end 56 57 local run_sh = function (f) 58 local _, ostr, estr = vis:pipe("$PWD/" .. f.name) 59 logger(true, ostr, estr) 60 gf.setup_iterators_from_text(estr, function(str) 61 return not str:find(": error:") and not str:find(": warning:") 62 end) 63 return true 64 end 65 66 local build_c = function (f) return run_sh({name = 'build.sh'}) end 67 68 local lang = {} 69 lang["bash"] = run_sh 70 lang["c"] = build_c 71 lang["cpp"] = build_c 72 lang["latex"] = build_tex 73 lang["python"] = run_python 74 75 local builder = lang[win.syntax] 76 if builder == nil then 77 builder = function () 78 vis:info(win.syntax .. ': filetype not supported') 79 return false 80 end 81 end 82 83 win:map(vis.modes.NORMAL, " c", function () 84 vis:command('X/.*/w') 85 local s = "built: " .. win.file.name 86 local ret, info = builder(win.file) 87 if info then s = s .. " | info: " .. info end 88 89 -- check for FIXMEs/TODOs 90 local _, out = vis:pipe('ag --depth=0 --count "(FIXME|TODO)"') 91 if out then 92 local file_count_table = gf.generate_line_indices(out) 93 local count = 0 94 for i = 1, #file_count_table do 95 local file, occurences = table.unpack(file_count_table[i]) 96 count = count + tonumber(occurences) 97 end 98 if count ~= 0 then s = s .. " | FIXME/TODOs: " .. tostring(count) end 99 end 100 101 if ret == true then vis:info(s) end 102 return ret 103 end, "build file in current window") 104 end 105 vis.events.subscribe(vis.events.WIN_OPEN, build_files)