build.lua (4095B)
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 file_exists = function(file) 27 local f = io.open(file, "r") 28 if f ~= nil then io.close(f) return true else return false end 29 end 30 31 local default_error_search = function(error_string) 32 gf.setup_iterators_from_text(error_string, function(str) 33 return not str:find(": error:") and not str:find(": warning:") 34 end) 35 end 36 37 local function build_files(win) 38 local build_tex = function (f) 39 local cmd = "xelatex -halt-on-error -shell-escape " 40 41 -- build in draft mode to update references 42 local err, ostr = vis:pipe(cmd .. "-draftmode " .. f.name) 43 if err ~= 0 then logger(true, ostr) return false end 44 45 local fp = util.splitext(f.name) 46 -- update refrences 47 vis:command("!biber " .. fp .. " >/dev/null") 48 -- update glossary 49 -- vis:command("!makeglossaries " .. fp .. " >/dev/null") 50 51 -- build actual pdf 52 err = vis:pipe(cmd .. f.name) 53 if err ~= 0 then return false end 54 55 -- reload pdf (zathura does this automatically) 56 -- vis:command('!pkill -HUP mupdf') 57 58 return true 59 end 60 61 local run_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 run_sh = function (f) 69 local _, ostr, estr = vis:pipe("$PWD/" .. f.name) 70 logger(true, ostr, estr) 71 default_error_search(estr) 72 return true 73 end 74 75 local build_c = function (f) 76 local cmd 77 if cmd == nil and file_exists('./build') then cmd = '$PWD/build --debug' end 78 if cmd == nil and file_exists('./build.sh') then cmd = '$PWD/build.sh' end 79 if cmd == nil and file_exists('Makefile') then cmd = 'make' end 80 if not cmd then return false, 'failed to determine method to build' end 81 82 local _, _, estr = vis:pipe(cmd) 83 logger(true, nil, estr) 84 default_error_search(estr) 85 return true 86 end 87 88 local lang = { 89 bash = run_sh, 90 c = build_c, 91 cpp = build_c, 92 latex = build_tex, 93 python = run_python, 94 rc = run_sh, 95 } 96 97 local builder = lang[win.syntax] 98 if builder == nil then 99 builder = function () 100 vis:info(win.syntax .. ': filetype not supported') 101 return false 102 end 103 end 104 105 win:map(vis.modes.NORMAL, " c", function () 106 vis:command('X/.*/w') 107 local s = "built: " .. win.file.name 108 local ret, info = builder(win.file) 109 if info then s = s .. " | info: " .. info end 110 111 -- check for FIXMEs/TODOs 112 local _, out = vis:pipe('ag --depth=0 --count "(FIXME|TODO)"') 113 if out then 114 local file_count_table = gf.generate_line_indices(out) 115 local count = 0 116 for i = 1, #file_count_table do 117 local file, occurences = table.unpack(file_count_table[i]) 118 count = count + tonumber(occurences) 119 end 120 if count ~= 0 then s = s .. " | FIXME/TODOs: " .. tostring(count) end 121 end 122 123 if ret == true then vis:info(s) end 124 return ret 125 end, "build file in current window") 126 end 127 128 local cached_command 129 vis:command_register("build", function(argv) 130 if #argv == 0 and cached_command == nil then vis:info("build cmd [arg ...]") return false end 131 if #argv ~= 0 then cached_command = table.concat(argv, " ") end 132 133 vis:command('X/.*/w') 134 vis:info("running: " .. cached_command) 135 vis:redraw() 136 local code, ostr, estr = vis:pipe(cached_command) 137 if code ~= 0 then 138 logger(true, ostr, estr) 139 default_error_search(estr) 140 end 141 end, "run command and try to collect errors") 142 143 vis.events.subscribe(vis.events.WIN_OPEN, build_files)