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 -- Pretty printer to use for the stuff before and after the line
18 commands.before_pret = "lam"
19 commands.after_pret = "lam"
20 -- Frame commands to use for the left (before + line + after) and right
21 -- (conditions) parts. Should include an opening {, which will be closed
23 commands.leftframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.48\\textwidth]{\\sans"
24 commands.rightframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.48\\textwidth]{"
26 -- A table to keep the lines in this buffer, so we can process them all at
29 -- A counter to keep track of the mininum amount of indentation found in each
33 -- Some helper functions
34 local function ltrim(s)
35 return (string.gsub(s, "^%s*", ""))
38 local function rtrim(s)
39 return (string.gsub(s, "%s*$", ""))
42 -- Insert n blank lines
43 local function blanks(n)
45 buffers.visualizers.handlers.default.empty_line()
49 -- Prettyprint the given lines using the given pretty printer
50 local function prettyprint(ppr, lines)
51 -- Change the current visualizer
52 buffers.setvisualizer('lam')
55 buffers.hooks.begin_of_display()
58 _, line = buffers.typeline(lines[i], i, #lines, line)
60 buffers.hooks.end_of_display()
62 -- Change the visualizer back
63 buffers.setvisualizer('trans')
66 -- Capture all lines, without generating any output
67 function vis.begin_of_display()
69 -- Let all the lambda pretty printing in this buffer use shared subscript
71 buffers.visualizers.handlers.lam.begin_of_block()
74 function vis.begin_of_line(n)
75 -- Don't generate output here
77 function vis.flush_line(str, nested)
78 -- Keep track of the minimum indent level of all lines. Note that we don't
79 -- look at empty lines, of course.
80 indent = utf.len(utf.match(str, "^%s*"))
81 -- Find the lowest indent (but don't count empty lines)
82 if (not min_indent or indent < min_indent) then
86 table.insert(lines, str)
87 -- Don't generate output here
89 function vis.end_of_line(n)
90 -- Don't generate output here
92 function vis.empty_line()
93 table.insert(lines, '')
94 -- Don't generate output here
97 -- We do the actual work here. Process all the lines in the buffer and
98 -- generate output for them.
99 function vis.end_of_display()
100 -- Strip indent that is present on every line
101 min_indent = min_indent or 0
103 lines[i] = utf.sub(lines[i], min_indent + 1)
106 -- Find the horizontal rule, and see how long it is.
109 match = utf.match(lines[i], "^%-%-%-*")
117 error("No horizontal separator found in:\n" .. table.concat(lines, "\n"))
120 -- Split the input in three parts. Stuff before the line, stuff
121 -- after the line, stuff to the right of the line.
122 before, after, rights = {}, {}, {}
126 -- Split the line into a left and right part
127 left = rtrim(utf.sub(line, 1, len))
128 right = ltrim(utf.sub(line, len + 1))
129 if utf.match(left, "^%-%-%-*") then
132 if utf.len(left) > 0 then
133 if not found_line then
134 table.insert(before, left)
136 table.insert(after, left)
140 if utf.len(right) > 0 then
141 table.insert(rights, right)
146 -- Real output starts here
149 -- Ensure the left and right frames end up next to each other.
150 tex.sprint("\\dontleavehmode")
152 -- Open and fill the left frame
153 tex.sprint(commands.leftframe)
155 prettyprint('lam', before)
157 tex.sprint(commands.rule)
159 prettyprint('lam', after)
161 -- Close the left frame
164 -- Open and fill the right frame
165 tex.sprint(commands.rightframe)
167 -- Insert spacer blank lines to align the middle condition with the
169 n_blanks = #before - math.ceil((#rights - 1) / 2)
170 n_blanks = math.max(0, n_blanks)
173 -- Print the conditions
175 tex.sprint(rights[i])
176 buffers.visualizers.handlers.default.end_of_line()
179 -- Fill up the remaining space with blanks
180 n_blanks = (#before + 1 + #after) - (n_blanks + #rights)
181 n_blanks = math.max(0, n_blanks)
184 -- Close the right frame
189 buffers.visualizers.handlers.lam.end_of_block()
192 -- vim: set sw=4 sts=4 expandtab ai: