1 -- filename : type-trans.lua
2 -- comment : Pretty printing of core transformations. Parses a specific
3 -- syntax, consisting of a before / after with a dashed lined in
4 -- between, and some extra conditions to the right of the line.
5 -- Recreates this layout using frames and line commands.
6 -- author : Matthijs Kooijman, Universiteit Twente, NL
7 -- copyright: Matthijs Kooijman
10 local utf = unicode.utf8
12 local vis = buffers.newvisualizer("trans")
15 -- A command to create a horizontal rule.
16 commands.rule = "\\HLine[width=.40 * \\the\\textwidth]"
17 -- Typing environment to use for the stuff before and after the line. Will be
18 -- prefixed with \start and \stop automatically.
19 commands.beforeenv = "unboxedlambda"
20 commands.afterenv = "unboxedlambda"
21 -- Frame commands to use for the left (before + line + after) and right
22 -- (conditions) parts. Should include an opening {, which will be closed
24 commands.leftframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.48\\textwidth]{"
25 commands.rightframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.48\\textwidth]{"
27 -- A table to keep the lines in this buffer, so we can process them all at
31 -- Some helper functions
32 local function ltrim(s)
33 return (string.gsub(s, "^%s*", ""))
36 local function rtrim(s)
37 return (string.gsub(s, "%s*$", ""))
40 -- Insert n blank lines
41 local function blanks(n)
43 buffers.visualizers.handlers.default.empty_line()
47 -- Prettyprint the given lines using the given pretty printer
48 local function prettyprint(ppr, lines)
49 -- Change the current visualizer
50 buffers.setvisualizer('lam')
53 buffers.hooks.begin_of_display()
56 _, line = buffers.typeline(lines[i], i, #lines, line)
58 buffers.hooks.end_of_display()
60 -- Change the visualizer back
61 buffers.setvisualizer('trans')
64 -- Capture all lines, without generating any output
65 function vis.begin_of_display()
68 function vis.begin_of_line(n)
69 -- Don't generate output here
71 function vis.flush_line(str, nested)
72 table.insert(lines, str)
73 -- Don't generate output here
75 function vis.end_of_line(n)
76 -- Don't generate output here
78 function vis.empty_line()
79 table.insert(lines, '')
80 -- Don't generate output here
83 -- We do the actual work here. Process all the lines in the buffer and
84 -- generate output for them.
85 function vis.end_of_display()
86 -- Find the horizontal rule, and see how long it is.
89 match = utf.match(lines[i], "^%-%-%-*")
97 error("No horizontal separator found in:\n" .. table.concat(lines, "\n"))
100 -- Split the input in three parts. Stuff before the line, stuff
101 -- after the line, stuff to the right of the line.
102 before, after, rights = {}, {}, {}
106 -- Split the line into a left and right part
107 left = rtrim(utf.sub(line, 1, len))
108 right = ltrim(utf.sub(line, len + 1))
109 if utf.match(left, "^%-%-%-*") then
112 if utf.len(left) > 0 then
113 if not found_line then
114 table.insert(before, left)
116 table.insert(after, left)
120 if utf.len(right) > 0 then
121 table.insert(rights, right)
126 -- Real output starts here
129 -- Let all the lambda pretty printing in this buffer use shared subscript
131 tex.sprint("\\directlua{buffers.visualizers.handlers.lam.begin_of_block()}")
133 -- Ensure the left and right frames end up next to each other.
134 tex.sprint("\\dontleavehmode")
136 -- Open and fill the left frame
137 tex.sprint(commands.leftframe)
139 tex.sprint("\\start" .. commands.beforeenv)
141 tex.sprint(table.concat(before, "\n"))
143 tex.sprint("\\stop" .. commands.beforeenv)
145 tex.sprint(commands.rule)
147 tex.sprint("\\start" .. commands.afterenv)
149 tex.sprint(table.concat(after, "\n"))
151 tex.sprint("\\stop" .. commands.afterenv)
153 -- Close the left frame
156 -- Open and fill the right frame
157 tex.sprint(commands.rightframe)
159 -- Insert spacer blank lines to align the middle condition with the
161 n_blanks = #before - math.ceil((#rights - 1) / 2)
162 n_blanks = math.max(0, n_blanks)
165 -- Print the conditions
167 tex.sprint(rights[i])
168 buffers.visualizers.handlers.default.end_of_line()
171 -- Fill up the remaining space with blanks
172 n_blanks = (#before + 1 + #after) - (n_blanks + #rights)
173 n_blanks = math.max(0, n_blanks)
176 -- Close the right frame
179 tex.sprint("\\directlua{buffers.visualizers.handlers.lam.end_of_block()}")
185 -- vim: set sw=4 sts=4 expandtab ai: