build.lua (3062B)
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 -- 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 local result = str:find("^/usr/include") ~= nil 62 result = result or str:find("^In file included") 63 return result 64 end) 65 return true 66 end 67 68 local build_c = function (f) return run_sh({name = 'build.sh'}) end 69 70 local lang = {} 71 lang["ansi_c"] = build_c 72 lang["cpp"] = build_c 73 lang["bash"] = run_sh 74 lang["latex"] = build_tex 75 lang["python"] = run_python 76 77 local builder = lang[win.syntax] 78 if builder == nil then 79 builder = function () 80 vis:info(win.syntax .. ': filetype not supported') 81 return false 82 end 83 end 84 85 win:map(vis.modes.NORMAL, " c", function () 86 vis:command('X/.*/w') 87 local s = "built: " .. win.file.name 88 local ret, info = builder(win.file) 89 if info then s = s .. " | info: " .. info end 90 91 -- check for FIXMEs/TODOs 92 local _, out = vis:pipe('ag --depth=0 --count "(FIXME|TODO)"') 93 if out then 94 local file_count_table = gf.generate_line_indices(out) 95 local count = 0 96 for i = 1, #file_count_table do 97 local file, occurences = table.unpack(file_count_table[i]) 98 count = count + tonumber(occurences) 99 end 100 if count ~= 0 then s = s .. " | FIXME/TODOs: " .. tostring(count) end 101 end 102 103 if ret == true then vis:info(s) end 104 return ret 105 end, "build file in current window") 106 end 107 vis.events.subscribe(vis.events.WIN_OPEN, build_files)