X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fmaster-project%2Freport.git;a=blobdiff_plain;f=pret-lam.lua;h=2e70001aa90ff6971f7ff89c946a2f390a22b0ba;hp=aff6988d8af774ef8f1e1021f954ed3f34e786d9;hb=ffef13cb64e18f66825b861a0ee709637125d9c3;hpb=ba27dfb753703acfda3d7165ab2c0a334515c310 diff --git a/pret-lam.lua b/pret-lam.lua index aff6988..2e70001 100644 --- a/pret-lam.lua +++ b/pret-lam.lua @@ -6,7 +6,7 @@ local utf = unicode.utf8 -vis = buffers.newvisualizer("lam") +local vis = buffers.newvisualizer("lam") local colors = { "prettytwo", @@ -63,7 +63,7 @@ end local function take_word(str) -- A word must always start with a-z (in particular, λ is not a valid -- start of a word). - res, newstr = utf.match(str, "^([a-zA-Z][%a%d_]+)(.*)") + res, newstr = utf.match(str, "^([a-zA-Z][%a%d%+%-%,_]+)(.*)") return res, newstr or str end @@ -90,21 +90,49 @@ local function do_subscripts(word) 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,]+)$" + -- 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 +local in_block = 0 + +-- Mark the begin of a block of lambda formatted buffers or expressions. This +-- means that, until you call end_of_block again, the subscript bases are +-- shared. For example, if you have \lam{y1} some text \lam{yn} within a +-- single block, the yn will properly get subscripted. Be sure to call +-- end_of_block again! +-- +-- Blocks can be partially nested, meaning that the block +-- won't be closed until end_of_block was called exactly as often as +-- begin_of_block. However, subscripts from the inner block can still +-- influence subscripts in the outer block. +function vis.begin_of_block() + vis.begin_of_display() + in_block = in_block + 1 +end + +-- Ends the current block +function vis.end_of_block() + in_block = in_block - 1 +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 = {} + if in_block == 0 then + -- Initially allow subscripts using _ or just appending a number (later, + -- we will add extra patterns here. + submatches = {"^(%a*)_([%a%d,]+)$", "^(%a+)(%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 end -- Make things work for inline typeing (e.g., \type{}) as well.