-function buffers.visualizers.lam.flush_line(str,nested)
- local result, state = { }, 0
- local finish, change = buffers.finish_state, buffers.change_state
- local take_symbol = buffers.visualizers.lam.take_symbol
- local take_word = buffers.visualizers.lam.take_word
- -- Set the colorscheme, which is used by finish_state and change_state
- buffers.currentcolors = buffers.visualizers.lam.colors
+-- Tries to match each of the patterns and returns the captures of the first
+-- matching pattern (up to 5 captures are supported). Returns nil when nothing
+-- matches.
+local function match_mul(str, patterns)
+ for i, pat in ipairs(patterns) do
+ a, b, c, d, e = utf.match(str, pat)
+ if a then
+ return a, b, c, d, e
+ end
+ end
+ return nil
+end
+
+-- Find any subscripts in the given word and typeset them
+local function do_subscripts(word)
+ base, sub = match_mul(res, submatches)
+ if sub then
+ word = base .. "\\low{" .. sub .. "}"
+ -- After a word has been used as a base, allow subscripts
+ -- without _, even for non-numbers.
+ if not bases[base] then
+ -- Register that we've added this base
+ bases[base] = true
+ -- Add a patterns for this base. First, the base with a single
+ -- letter or number subscript.
+ submatches[#submatches+1] = "^(" .. base .. ")([%a%d])$"
+ -- Seconde, the base with a longer prefix that includes at least
+ -- one of +-, (to catch things like ri+1, but not return).
+ submatches[#submatches+1] = "^(" .. base .. ")([%a%d]*[%-%+,]+[%a%d%-%+,]*)$"
+ end
+ end
+ return word
+end
+
+-- Do proper aligning for subsequent lines. For example, in
+-- foo = bar
+-- | baz
+-- We replace the spaces in the second line with a skip with the same with as
+-- "foo ", to align the | with the =.
+-- For this, we keep a table "indentlines", which contains all previous lines
+-- with smaller indent levels that are still "in scope" (e.g., have not yet
+-- been followed by a line with a smaller indent level). For example:
+-- line1
+-- line2
+-- line3
+-- line4
+-- line5
+-- After the last line, the table will contain:
+-- { 0 = "line1", 2 = " line4", 4 = " line5"}
+-- In other words, line3 is no longer in scope since it is "hidden" by
+-- line4, and line is no longer in scope since it is replaced by line4.
+local function do_indent(line)
+ newind, rest = utf.match(line, '^(%s*)(.*)')
+ prev = -1
+ -- Loop all the previous lines
+ for indent, unused in pairs(indentlines) do
+ if indent > #newind then
+ -- Remove any lines with a larger indent
+ indentlines[indent] = nil
+ elseif indent < #newind and indent > prev then
+ -- Find the last line (e.g, with the highest indent) with an
+ -- indent smaller than the new indent. This is the line from which
+ -- we need to copy the indent.
+ prev = indent
+ end
+ end
+
+ -- Always store this line, possibly overwriting a previous line with the
+ -- same indent
+ indentlines[#newind] = line