X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fmaster-project%2Freport.git;a=blobdiff_plain;f=pret-trans.lua;h=148d3ca958c69eb8a789f15b01a95ea2534e3c3d;hp=7f32afded167f7f07fe3f1680c7b5b313a3d78c9;hb=42ec9c78da464de1ed58509a6edaa454c6a225bc;hpb=3ff8ced65f6af69db754b35b263c7d6d9f19e61f diff --git a/pret-trans.lua b/pret-trans.lua index 7f32afd..148d3ca 100644 --- a/pret-trans.lua +++ b/pret-trans.lua @@ -9,23 +9,27 @@ local utf = unicode.utf8 -vis = buffers.newvisualizer("trans") +local vis = buffers.newvisualizer("trans") local commands = {} -- A command to create a horizontal rule. -commands.rule = "\\blackrule[height=0.5pt,depth=0pt,width=.45\\textwidth]" --- Pretty printer to use for the stuff before and after the line -commands.before_pret = "lam" -commands.after_pret = "lam" +commands.rule = "\\HLine[width=.40 * \\the\\textwidth]" +-- Typing environment to use for the stuff before and after the line. Will be +-- prefixed with \start and \stop automatically. +commands.beforeenv = "unboxedlambda" +commands.afterenv = "unboxedlambda" -- Frame commands to use for the left (before + line + after) and right -- (conditions) parts. Should include an opening {, which will be closed -- automatically. -commands.leftframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.5\\textwidth]{\\sans" -commands.rightframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.5\\textwidth]{" +commands.leftframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.48\\textwidth]{" +commands.rightframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.48\\textwidth]{" -- A table to keep the lines in this buffer, so we can process them all at -- once at the end. local lines +-- A counter to keep track of the mininum amount of indentation found in each +-- display. +local min_indent -- Some helper functions local function ltrim(s) @@ -38,7 +42,6 @@ end -- Insert n blank lines local function blanks(n) - print("Inserting " .. n .. " blanks") for i = 1,n do buffers.visualizers.handlers.default.empty_line() end @@ -64,11 +67,20 @@ end -- Capture all lines, without generating any output function vis.begin_of_display() lines = {} + min_indent = nil end function vis.begin_of_line(n) -- Don't generate output here end function vis.flush_line(str, nested) + -- Keep track of the minimum indent level of all lines. Note that we don't + -- look at empty lines, of course. + indent = utf.len(utf.match(str, "^%s*")) + -- Find the lowest indent (but don't count empty lines) + if (not min_indent or indent < min_indent) then + min_indent = indent + end + table.insert(lines, str) -- Don't generate output here end @@ -83,6 +95,12 @@ end -- We do the actual work here. Process all the lines in the buffer and -- generate output for them. function vis.end_of_display() + -- Strip indent that is present on every line + min_indent = min_indent or 0 + for i = 1,#lines do + lines[i] = utf.sub(lines[i], min_indent + 1) + end + -- Find the horizontal rule, and see how long it is. len = nil for i = 1,#lines do @@ -92,8 +110,11 @@ function vis.end_of_display() break end end - print("Rule length: " .. len) - + + if not len then + error("No horizontal separator found in:\n" .. table.concat(lines, "\n")) + end + -- Split the input in three parts. Stuff before the line, stuff -- after the line, stuff to the right of the line. before, after, rights = {}, {}, {} @@ -106,7 +127,6 @@ function vis.end_of_display() if utf.match(left, "^%-%-%-*") then found_line = true else - print("Looking at " .. line .. "('" .. left .. "', '" .. right .. "')") if utf.len(left) > 0 then if not found_line then table.insert(before, left) @@ -124,17 +144,29 @@ function vis.end_of_display() -- Real output starts here -- + -- Let all the lambda pretty printing in this buffer use shared subscript + -- detection + tex.sprint("\\directlua{buffers.visualizers.handlers.lam.begin_of_block()}") + -- Ensure the left and right frames end up next to each other. tex.sprint("\\dontleavehmode") -- Open and fill the left frame tex.sprint(commands.leftframe) - prettyprint('lam', before) + tex.sprint("\\start" .. commands.beforeenv) + + tex.sprint(table.concat(before, "\n")) + + tex.sprint("\\stop" .. commands.beforeenv) tex.sprint(commands.rule) - prettyprint('lam', after) + tex.sprint("\\start" .. commands.afterenv) + + tex.sprint(table.concat(after, "\n")) + + tex.sprint("\\stop" .. commands.afterenv) -- Close the left frame tex.sprint("}") @@ -161,6 +193,8 @@ function vis.end_of_display() -- Close the right frame tex.sprint("}") + + tex.sprint("\\directlua{buffers.visualizers.handlers.lam.end_of_block()}") -- Clean up lines = {}