From: Matthijs Kooijman Date: Thu, 11 Jun 2009 15:40:25 +0000 (+0200) Subject: Make pret-lam remember subscripted words. X-Git-Tag: final-thesis~322 X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=9fb83d4c3f0511c8e6e0b584d18370eba508a29b;p=matthijs%2Fmaster-project%2Freport.git Make pret-lam remember subscripted words. This allows pret-lam to make "n" a subscript in "En", if it has seen "E1" before, for example. This requires a change in context, which I will try to get included next. --- diff --git a/pret-lam.lua b/pret-lam.lua index 77fcc5d..1011b19 100644 --- a/pret-lam.lua +++ b/pret-lam.lua @@ -78,15 +78,48 @@ function buffers.visualizers.lam.match_mul(str, patterns) return nil end +function buffers.visualizers.lam.begin_of_buffer() + -- 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 + +function buffers.visualizers.lam.end_of_buffer() + -- Reset submatches and bases, since flush_line can be called without + -- begin / end_of_buffer for \type. + buffers.visualizers.lam.begin_of_buffer() + bases = nil +end + +function buffers.visualizers.lam.do_subscripts(word) + local match_mul = buffers.visualizers.lam.match_mul + 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 bases and 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 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 - local match_mul = buffers.visualizers.lam.match_mul + local do_subscripts = buffers.visualizers.lam.do_subscripts -- Set the colorscheme, which is used by finish_state and change_state buffers.currentcolors = buffers.visualizers.lam.colors - while str ~= "" do local found = false local word, symbol @@ -97,11 +130,8 @@ function buffers.visualizers.lam.flush_line(str,nested) -- Make all keywords bold word = "{\\bold " .. word .. "}" else - -- Allow subscripts using _ or just appending a number. - base, sub = match_mul(res, {"^(.*)_([%a%d,]+)$", "^(.*[^%d])(%d*)$"}) - if sub then - word = base .. "\\low{" .. sub .. "}" - end + -- Process any subscripts in the word + word = do_subscripts(word) end else -- The next token is not a word, it must be a symbol @@ -116,4 +146,8 @@ function buffers.visualizers.lam.flush_line(str,nested) buffers.flush_result(result,nested) end +-- Call end_of_buffer once to set up submatches (since \type doesn't call +-- begin_of_buffer / end_of_buffer). +buffers.visualizers.lam.end_of_buffer() + -- vim: set sw=4 sts=4 expandtab ai: