Improve subscript handling in pret-lam.
[matthijs/master-project/report.git] / pret-lam.lua
index eb7d3530395529a0b6ce1b934a4aa6ce4b38c467..ea07811759ae40920b5ef4d6a4f8e174a3e3fa1f 100644 (file)
@@ -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).
 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
 
         return res, newstr or str
 end
 
@@ -90,8 +90,12 @@ local function do_subscripts(word)
         if not bases[base] then
             -- Register that we've added this base
             bases[base] = true
         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
     end
     return word
@@ -100,13 +104,17 @@ end
 function vis.begin_of_display()
     -- Initially allow subscripts using _ or just appending a number (later,
     -- we will add extra patterns here.
 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+)$"}
+    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
 
     -- 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)
     local result, state = { }, 0
     local finish, change = buffers.finish_state, buffers.change_state
 function vis.flush_line(str,nested)
     local result, state = { }, 0
     local finish, change = buffers.finish_state, buffers.change_state