From 9f7bd12ed4560e888d9eaa5398c5a0bdc5779759 Mon Sep 17 00:00:00 2001
From: Karl Berry <karl@freefriends.org>
Date: Tue, 16 Mar 2021 22:11:47 +0000
Subject: lua-typo (16mar21)

git-svn-id: svn://tug.org/texlive/trunk@58441 c570f23f-e606-0410-a88d-b1316a301751
---
 .../texmf-dist/tex/lualatex/lua-typo/lua-typo.sty  | 185 ++++++++++++---------
 1 file changed, 103 insertions(+), 82 deletions(-)

(limited to 'Master/texmf-dist/tex/lualatex/lua-typo/lua-typo.sty')

diff --git a/Master/texmf-dist/tex/lualatex/lua-typo/lua-typo.sty b/Master/texmf-dist/tex/lualatex/lua-typo/lua-typo.sty
index 0408dc24a18..ed88c065c35 100644
--- a/Master/texmf-dist/tex/lualatex/lua-typo/lua-typo.sty
+++ b/Master/texmf-dist/tex/lualatex/lua-typo/lua-typo.sty
@@ -8,7 +8,7 @@
 %% 
 \NeedsTeXFormat{LaTeX2e}[2020/01/01]
 \ProvidesPackage{lua-typo}
-                [2021/03/03 v.0.30 Daniel Flipo]
+                [2021/03/14 v.0.32 Daniel Flipo]
 \ifdefined\directlua
   \RequirePackage{luatexbase,luacode,luacolor}
   \RequirePackage{kvoptions,atveryend}
@@ -202,8 +202,8 @@ luatypo = { }
        texio.write_nl('*** lua-typo: No Typo Flaws found.')
     else
        texio.write_nl('*** lua-typo: WARNING *************')
-       texio.write_nl('The following pages need attention: '
-                      .. luatypo.pagelist)
+       texio.write_nl('The following pages need attention: ')
+       texio.write(luatypo.pagelist)
     end
     texio.write_nl('***********************************')
     texio.write_nl(' ')
@@ -259,6 +259,7 @@ local hyphcount = 0
 local parlines = 0
 local pagelines = 0
 local pageno = 0
+local prevno = 0
 local pageflag = false
 
 local char_to_discard = { }
@@ -276,6 +277,8 @@ split_lig[0xFB01] = "fi"
 split_lig[0xFB02] = "fl"
 split_lig[0xFB03] = "ffi"
 split_lig[0xFB04] = "ffl"
+split_lig[0xFB05] = "st"
+split_lig[0xFB06] = "st"
 
 local DISC  = node.id("disc")
 local GLYPH = node.id("glyph")
@@ -295,6 +298,7 @@ local LINE    = 1
 local BOX     = 2
 local USER = 0
 local HYPH = 0x2D
+local LIGA = 0x102
 
 local effective_glue = node.effective_glue
 local set_attribute = node.set_attribute
@@ -307,20 +311,20 @@ local is_glyph  = node.is_glyph
 
 local color_node = function (node, color)
   local attr = oberdiek.luacolor.getattribute()
-  if node.id == DISC then
+  if node and node.id == DISC then
      local pre = node.pre
      local post = node.post
-     local replace = node.replace
+     local repl = node.replace
      if pre then
         set_attribute(pre,attr,color)
      end
      if post then
         set_attribute(post,attr,color)
      end
-     if replace then
-        set_attribute(replace,attr,color)
+     if repl then
+        set_attribute(repl,attr,color)
      end
-  else
+  elseif node then
      set_attribute(node,attr,color)
   end
 end
@@ -333,7 +337,7 @@ end
 local color_line = function (head, color)
   local first = head.head
   for n in traverse(first) do
-      if n.id == HLIST and n.subtype == BOX then
+      if n and n.id == HLIST and n.subtype == BOX then
          color_hbox(n, color)
       else
          color_node(n, color)
@@ -343,7 +347,7 @@ end
 local signature = function (node, string, swap)
   local n = node
   local str = string
-  if n.id == GLYPH then
+  if n and n.id == GLYPH then
     local b, id = is_glyph(n)
     if b and not char_to_discard[b] then
        if b == 0x2019 then b = 0x27 end
@@ -355,9 +359,15 @@ local signature = function (node, string, swap)
              c = string.reverse(c)
           end
           str = str .. c
+       elseif n.subtype == LIGA and b > 0xE000 then
+          local c = string.sub(b,-2)
+          if swap then
+             c = string.reverse(c)
+          end
+          str = str .. c
        end
     end
-  elseif n.id == DISC then
+  elseif n and n.id == DISC then
     local pre = n.pre
     local post = n.post
     local c1 = ""
@@ -376,63 +386,65 @@ local signature = function (node, string, swap)
        end
     end
     if swap then
-       str = str .. c2  .. c1
+       str = str .. c2 .. c1
     else
        str = str .. c1 .. c2
     end
   end
   local len = string.len(str)
+  if string.find(str, "_") then
+     len = len - 1
+  end
   return len, str
 end
 local check_last_word = function (old, node, color, flag)
   local match = false
   local new = ""
-  local len = 0
-  if flag then
+  local maxlen = 0
+  if flag and node then
      local swap = true
      local lastn = node
-     while lastn and lastn.id ~= GLYPH and lastn.prev do
+     while lastn and lastn.id ~= GLYPH and lastn.id ~= DISC do
        lastn = lastn.prev
      end
      local n = lastn
-     repeat
-       len, new = signature (n, new, swap)
+     while n and n.id ~= GLUE do
+       maxlen, new = signature (n, new, swap)
        n = n.prev
-     until not n or n.id == GLUE
+     end
      if n and n.id == GLUE then
         new = new .. "_"
-        len = len + 1
         repeat
           n = n.prev
-          len, new = signature (n, new, swap)
-        until n.id == GLUE or not n.prev
+          maxlen, new = signature (n, new, swap)
+        until not n or n.id == GLUE
      end
      new = string.reverse(new)
      local MinFull = luatypo.MinFull
      local MinPart = luatypo.MinPart
      MinFull = math.min(MinPart,MinFull)
      local k = MinPart
-     local oldlast = string.gsub (old, '%w+_', '')
-     local newlast = string.gsub (new, '%w+_', '')
-     len = string.len(newlast)
-     if len < MinPart then
+     local oldlast = string.gsub (old, '.*_', '')
+     local newlast = string.gsub (new, '.*_', '')
+     local i = string.find(new, "_")
+     if i and i > maxlen - MinPart + 1 then
         k = MinPart + 1
      end
      local oldsub = string.sub(old,-k)
      local newsub = string.sub(new,-k)
-     local maxlen = string.len(new)
-     if oldsub == newsub then
+     local l = string.len(new)
+     if oldsub == newsub and l >= k then
         match = true
-     elseif oldlast == newlast and len >= MinFull then
+     elseif oldlast == newlast and string.len(newlast) >= MinFull then
         match = true
         oldsub = oldlast
         newsub = newlast
-        k = len
+        k = string.len(newlast)
      end
      if match then
         local osub = oldsub
         local nsub = newsub
-        while osub == nsub and k < maxlen do
+        while osub == nsub and k <= maxlen do
           k = k +1
           osub = string.sub(old,-k)
           nsub = string.sub(new,-k)
@@ -440,20 +452,19 @@ local check_last_word = function (old, node, color, flag)
              newsub = nsub
           end
         end
+        newsub = string.gsub(newsub, '^_', '')
         oldsub = string.reverse(newsub)
         local newsub = ""
-        local k = string.len(oldsub)
         local n = lastn
         repeat
           if n and n.id ~= GLUE then
              color_node(n, color)
-             len, newsub = signature(n, newsub, swap)
+             l, newsub = signature(n, newsub, swap)
           elseif n then
              newsub = newsub .. "_"
-             len = len + 1
           end
           n = n.prev
-        until not n or newsub == oldsub or len >= k
+        until not n or newsub == oldsub or l >= k
      end
   end
   return new, match
@@ -462,49 +473,51 @@ local check_first_word = function (old, node, color, flag)
   local match = false
   local swap = false
   local new = ""
-  local len = 0
+  local maxlen = 0
   local start = node
   local n = start
-  while n and n.id ~= GLUE do
-    len, new = signature (n, new, swap)
-    n = n.next
+  while n and n.id ~= GLYPH and n.id ~= DISC do
+     n = n.next
   end
-  n = n.next
-  new = new .. "_"
   while n and n.id ~= GLUE do
-    len, new = signature (n, new, swap)
+    maxlen, new = signature (n, new, swap)
     n = n.next
   end
+  if n and n.id == GLUE then
+     new = new .. "_"
+     repeat
+       n = n.next
+       maxlen, new = signature (n, new, swap)
+     until not n or n.id == GLUE
+  end
   if flag then
      local MinFull = luatypo.MinFull
      local MinPart = luatypo.MinPart
      MinFull = math.min(MinPart,MinFull)
      local k = MinPart
-     local L = MinPart -1
      local oldsub = ""
      local newsub = ""
-     local oldfirst = string.gsub (old, '_%w+', '')
-     local newfirst = string.gsub (new, '_%w+', '')
-     len = string.len(newfirst)
-     if len < MinPart then
+     local oldfirst = string.gsub (old, '_.*', '')
+     local newfirst = string.gsub (new, '_.*', '')
+     local i = string.find(new, "_")
+     if i and i <= MinPart then
         k = MinPart + 1
      end
-     local maxlen = string.len(new)
-     local oldsub = string.sub(old,1,L)
-     local newsub = string.sub(new,1,L)
-     if oldsub == newsub then
+     local oldsub = string.sub(old,1,k)
+     local newsub = string.sub(new,1,k)
+     local l = string.len(newsub)
+     if oldsub == newsub and l >= k then
         match = true
-        k = L
-     elseif oldfirst == newfirst  and len >= MinFull then
+     elseif oldfirst == newfirst  and string.len(newfirst) >= MinFull then
         match = true
         oldsub = oldfirst
         newsub = newfirst
-        k = len
+        k = string.len(newfirst)
      end
      if match then
         local osub = oldsub
         local nsub = newsub
-        while osub == nsub and k < maxlen do
+        while osub == nsub and k <= maxlen do
           k = k + 1
           osub = string.sub(old,1,k)
           nsub = string.sub(new,1,k)
@@ -512,11 +525,7 @@ local check_first_word = function (old, node, color, flag)
              newsub = nsub
           end
         end
-        if k < MinPart then
-           match =false
-        end
-     end
-     if match then
+        newsub = string.gsub(newsub, '_$', '')   --$
         oldsub = newsub
         local newsub = ""
         local k = string.len(oldsub)
@@ -524,13 +533,12 @@ local check_first_word = function (old, node, color, flag)
         repeat
           if n and n.id ~= GLUE then
              color_node(n, color)
-             len, newsub = signature(n, newsub, swap)
+             l, newsub = signature(n, newsub, swap)
           elseif n then
              newsub = newsub .. "_"
-             len = len + 1
           end
           n = n.next
-        until not n or newsub == oldsub or len >= k
+        until not n or newsub == oldsub or l >= k
      end
   end
   return new, match
@@ -581,7 +589,7 @@ local check_regexpr = function (glyph)
 end
 local show_pre_disc = function (disc, color)
   local n = disc
-  while n.id ~= GLUE do
+  while n and n.id ~= GLUE do
     color_node(n, color)
     n = n.prev
   end
@@ -623,13 +631,16 @@ luatypo.check_page = function (head)
        pprevnode = prevnode.prev
     end
     if head.id == GLUE and head.subtype == TOPSKIP then
-       pageflag = false
-       match1 = false
        pageno = tex.getcount("c@page")
        hyphcount = 0
-       pagelines = 0
-       firstwd = ""
-       lastwd = ""
+       if pageno > prevno then
+          pageflag = false
+          pagelines = 0
+          match1 = false
+          firstwd = ""
+          lastwd = ""
+          prevno = pageno
+       end
     elseif head.id == HLIST and head.subtype == LINE then
        local first = head.head
        if first.id == MKERN or
@@ -638,14 +649,18 @@ luatypo.check_page = function (head)
        end
        pagelines = pagelines + 1
        local ListItem = false
-       local textline = true
-       local n = first
-       while (n and n.id ~= GLYPH) and
-             not (n.id == GLUE and n.subtype == RGTSKIP) do
-         n = n.next
-       end
-       if n.id == GLUE then
-          textline = false
+       local textline = false
+       if first.id == GLYPH then
+          textline = true
+       else
+          local n = first
+          repeat
+            n = n.next
+            if n and n.id == GLYPH then
+               textline = true
+               break
+            end
+          until not n or (n.id == GLUE and n.subtype == RGTSKIP)
        end
        if head.glue_set == 1 and head.glue_sign == 2 and
           head.glue_order == 0 and OverfullLines then
@@ -786,26 +801,32 @@ luatypo.check_page = function (head)
           color_line (head, COLOR)
        end
        if lwhyphflag and EOPHyphens then
+          pageflag = true
           local COLOR = luatypo.colortbl[1]
           local pg = show_pre_disc (pn,COLOR)
-          pageflag = true
        end
     elseif not nextnode and head.id == GLUE and
                             head.subtype == USRSKIP then
        if pagelines > 1 and pagelines < PAGEmin and ShortPages then
           pageflag = true
           local COLOR = luatypo.colortbl[9]
-          local node = head
-          while node.id ~= HLIST or node.subtype ~= LINE do
-             node = node.prev
+          local n = head
+          while n and (n.id ~= HLIST or n.subtype ~= LINE) do
+             n = n.prev
+          end
+          if n then
+             color_line(n, COLOR)
           end
-          color_line(node, COLOR)
        end
     end
   head = nextnode
   end
   if pageflag then
-     luatypo.pagelist = luatypo.pagelist .. tostring(pageno) .. ", "
+     local pl = luatypo.pagelist
+     local p = tonumber(string.match(pl, "%s(%d+),%s$"))
+     if not p or pageno > p then
+        luatypo.pagelist = luatypo.pagelist .. tostring(pageno) .. ", "
+     end
   end
   return true
 end
-- 
cgit v1.2.3

