dotfiles

personal dotfiles
git clone anongit@rnpnr.xyz:dotfiles.git
Log | Files | Refs | Feed | Submodules

build.lua (3653B)


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