+-- 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 pattern for this base
+ submatches[#submatches+1] = "^(" .. base .. ")([%a%d,]+)$"
+ end
+ end
+ return word
+end
+
+function vis.begin_of_display()
+ -- Initially allow subscripts using _ or just appending a number (later,
+ -- we will add extra patterns here.
+ submatches = {"^(.*)_([%a%d,]+)$", "^(.*[^%d])(%d+)$"}
+ -- This stores all the bases we've encountered so far (to prevent
+ -- duplicates). For each of them there will be a pattern in submatches
+ -- above.
+ bases = {}
+end
+
+-- Make things work for inline typeing (e.g., \type{}) as well.
+vis.begin_of_inline = vis.begin_of_display
+vis.end_of_inline = vis.end_of_display
+
+function vis.flush_line(str,nested)