Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://angg.twu.net/sheavesforchildren/rect.lua -- http://angg.twu.net/sheavesforchildren/rect.lua.html -- (find-angg "sheavesforchildren/rect.lua") -- -- (find-lua51manualw3m "") -- (find-lua51manualw3m "#pdf-table.insert") -- (find-books "__comp/__comp.el" "ierusalimschy") -- (find-pil2page 8 "Contents") -- (find-pil2text 8 "Contents") -- (find-dn5file "" "gab") -- (find-dn5file "" "rect") -- (find-dn5 "rect.lua") -- (find-dn5 "rect.lua" "complexdef-tests") -- (find-dn5 "gab.lua") -- (find-dn5 "gab.lua" "modal") -- (find-gabgrep "grep -niH -e modal *") -- (find-angg "LUA/canvas3.lua") -- (find-angg "LUA/canvas3.lua" "ZDag-functions") -- (find-LATEX "2011ebl-abs.tex") -- a = "abcdefg" -- a:replace(2, "CDE") --> "abCDEfg" -- a:replace(2, "CDE", 4) --> "abCDE g" -- -- w is the width of the left part string.leftof = function (s, w) if #s > w then return s:sub(1, w) elseif #s == w then return s else return s..string.rep(" ", w - #s) end end string.rightof = function (s, w) if #s <= w then return "" else return s:sub(w+1) end end string.replace = function (s, x, r, w) w = w or #r r = r:leftof(w) return s:leftof(x)..r..s:rightof(x + #r) end -- Examples: -- if: r = Rect.new("aa\nbb") -- then: r = Rect {[1]="aa", [2]="bb", raise=0} -- r:get(0) = "aa" -- r:get(1) = "bb" -- "--"..r = Rect {[1]="--aa", [2]=" bb", raise=0} -- -- Sanity conditions for a rectangle r: -- r[1] = r:get(0) when r.raise = 0 -- r[1] = r:get(-1) when r.raise = 1 -- r[1] = r:get(-2) when r.raise = 2 -- r[2] = r:get(-1) when r.raise = 2 -- r[3] = r:get(0) when r.raise = 2 -- r:get(y) := r[y + r.raise + 1] -- r.raise >= 0 -- #r >= r.raise + 1 -- The i-set of a rectangle r is always {1,...,#r} -- The y-set of a rectangle is always {1-(r.raise+1),...,#r-(r.raise+1)} --- and we always have 0 \in y-set Rect = Class { type = "Rect", new = function (str, raise) local rect = Rect(splitlines(str or "")) rect.raise = raise or 0 while #rect < rect.raise + 1 do table.insert(rect, "") end return rect end, __index = { width = function (rect) local w = 0 for i=1,#rect do w = max(w, #rect[i]) end return w end, toi = function (rect, y) return y + (rect.raise + 1) end, toy = function (rect, i) return i - (rect.raise + 1) end, -- (find-lua51manualw3m "#pdf-table.insert") makeyvalid = function (rect, y) local newraise = max(-y+1, rect.raise or 0) if newraise > raise then for i=0,newraise-raise do table.insert(rect, 1, "") end rect.raise = newraise end local newn = max(#rect, y + (rect.raise or 0)) if newn > #rect then for i=0,newn-#rect do table.insert(rect, "") end end end, get = function (rect, y) return rect[y + (rect.raise or 0)] end, put1 = function (rect, y, s) end, replace1 = function (rect, x, y, r, w) while #rect < y do table.insert(rect, "") end rect[y] = rect[y]:replace(x, r, w) end, replace = function (rect1, x, y, rect2, w) w = w or rect2:width() for i=1,#rect2 do rect1:replace1(x, y, rect2[i], w) y = y + 1 end return rect1 end, }, __concat = function (r1, r2) return Rect.new():replace(0, 1, r1):replace(r1:width(), 1, r2) end, __tostring = function (rect) return table.concat(rect, "\n") end, } = Rect {"a", "bb"} .. Rect {"a", "bb", "ccc"} --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "rect.lua" --]] -- Local Variables: -- coding: raw-text-unix -- End: