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 vis = buffers.newvisualizer("trans")
15 -- A command to create a horizontal rule.
16 commands.rule = "\\blackrule[height=0.5pt,depth=0pt,width=.45\\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=.5\\textwidth]{\\sans"
24 commands.rightframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.5\\textwidth]{"
26 -- A table to keep the lines in this buffer, so we can process them all at
30 -- Some helper functions
31 local function ltrim(s)
32 return (string.gsub(s, "^%s*", ""))
35 local function rtrim(s)
36 return (string.gsub(s, "%s*$", ""))
39 -- Insert n blank lines
40 local function blanks(n)
42 buffers.visualizers.handlers.default.empty_line()
46 -- Prettyprint the given lines using the given pretty printer
47 local function prettyprint(ppr, lines)
48 -- Change the current visualizer
49 buffers.setvisualizer('lam')
52 buffers.hooks.begin_of_display()
55 _, line = buffers.typeline(lines[i], i, #lines, line)
57 buffers.hooks.end_of_display()
59 -- Change the visualizer back
60 buffers.setvisualizer('trans')
63 -- Capture all lines, without generating any output
64 function vis.begin_of_display()
67 function vis.begin_of_line(n)
68 -- Don't generate output here
70 function vis.flush_line(str, nested)
71 table.insert(lines, str)
72 -- Don't generate output here
74 function vis.end_of_line(n)
75 -- Don't generate output here
77 function vis.empty_line()
78 table.insert(lines, '')
79 -- Don't generate output here
82 -- We do the actual work here. Process all the lines in the buffer and
83 -- generate output for them.
84 function vis.end_of_display()
85 -- Find the horizontal rule, and see how long it is.
88 match = utf.match(lines[i], "^%-%-%-*")
95 -- Split the input in three parts. Stuff before the line, stuff
96 -- after the line, stuff to the right of the line.
97 before, after, rights = {}, {}, {}
101 -- Split the line into a left and right part
102 left = rtrim(utf.sub(line, 1, len))
103 right = ltrim(utf.sub(line, len + 1))
104 if utf.match(left, "^%-%-%-*") then
107 if utf.len(left) > 0 then
108 if not found_line then
109 table.insert(before, left)
111 table.insert(after, left)
115 if utf.len(right) > 0 then
116 table.insert(rights, right)
121 -- Real output starts here
124 -- Ensure the left and right frames end up next to each other.
125 tex.sprint("\\dontleavehmode")
127 -- Open and fill the left frame
128 tex.sprint(commands.leftframe)
130 prettyprint('lam', before)
132 tex.sprint(commands.rule)
134 prettyprint('lam', after)
136 -- Close the left frame
139 -- Open and fill the right frame
140 tex.sprint(commands.rightframe)
142 -- Insert spacer blank lines to align the middle condition with the
144 n_blanks = #before - math.ceil((#rights - 1) / 2)
145 n_blanks = math.max(0, n_blanks)
148 -- Print the conditions
150 tex.sprint(rights[i])
151 buffers.visualizers.handlers.default.end_of_line()
154 -- Fill up the remaining space with blanks
155 n_blanks = (#before + 1 + #after) - (n_blanks + #rights)
156 n_blanks = math.max(0, n_blanks)
159 -- Close the right frame
166 -- vim: set sw=4 sts=4 expandtab ai: