Allow transformations to be indented.
[matthijs/master-project/report.git] / pret-trans.lua
index 212c835ace52d0c7b026fc606dca6d336cba5ba1..062eceb536230b109585af5d19b8ec5a3f7e31ae 100644 (file)
@@ -26,6 +26,9 @@ commands.rightframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,
 -- A table to keep the lines in this buffer, so we can process them all at
 -- once at the end.
 local lines
 -- 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)
 
 -- Some helper functions
 local function ltrim(s)
@@ -66,11 +69,20 @@ function vis.begin_of_display()
     -- Let all the lambda pretty printing in this buffer use shared subscript
     -- detection
     buffers.visualizers.handlers.lam.begin_of_block()
     -- Let all the lambda pretty printing in this buffer use shared subscript
     -- detection
     buffers.visualizers.handlers.lam.begin_of_block()
+    min_indent = nil
 end
 function vis.begin_of_line(n)
     -- Don't generate output here
 end
 function vis.flush_line(str, nested)
 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
     table.insert(lines, str)
     -- Don't generate output here
 end
@@ -85,6 +97,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()
 -- 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
     -- Find the horizontal rule, and see how long it is.
     len = nil
     for i = 1,#lines do