From 4d80cd680742e182b6d67798018ca8fcf8a15629 Mon Sep 17 00:00:00 2001
From: Philipp Gesang <megas.kapaneus@gmail.com>
Date: Sun, 28 Aug 2011 12:13:42 +0200
Subject: generalized tab expansion

---
 mod/tex/context/third/rst/rst_parser.lua | 34 +++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)

(limited to 'mod/tex/context')

diff --git a/mod/tex/context/third/rst/rst_parser.lua b/mod/tex/context/third/rst/rst_parser.lua
index 8951542..4289941 100644
--- a/mod/tex/context/third/rst/rst_parser.lua
+++ b/mod/tex/context/third/rst/rst_parser.lua
@@ -76,8 +76,8 @@ state.lastbullets = {}
 state.roman_cache = {} -- storing roman numerals that were already converted
 state.currentindent  = "" -- used in definition lists and elsewhere
 state.previousindent = "" -- for literal blocks included in paragraphs to restore the paragraph indent
-state.currentwidth   = 0 -- table layout
-state.currentlayout  = {} -- table layout
+state.currentwidth   = 0   -- table layout
+state.currentlayout  = {}  -- table layout
 state.previousadorn  = nil -- section underlining and overlining
 
 state.footnotes = {}
@@ -1362,22 +1362,34 @@ function file_helpers.strip_BOM (raw)
     return raw
 end
 
+--- Tab expansion: feature request by Philipp A.
 do
-    local space = " "
-    function file_helpers.expandtab (raw)
-        local spaces   = space:rep(rst.shiftwidth)
+    local shiftwidth = rst.shiftwidth
+    local stringrep  = string.rep
+    local position   = 1
+
+    local reset_position     = function ()  position = 1 return "\n" end
+    local increment_position = function (c) position = position + 1 return c end
+    local expand_tab         = function ()
+        local expand = (shiftwidth - position) % shiftwidth + 1
+        position     = position + expand
+        return stringrep(" ", expand)
+    end
 
-        local tab    = P"\t" / spaces
-        local eol    = P"\n"
-        local indent = eol * tab^1
-        local detabber = Cs(tab^-1 * (indent + 1)^0)
+    local tab      = S"\t\v" / expand_tab
+    local utfchar  = utfchar / increment_position
+    local eol      = P"\n"   / reset_position
+    local p_expand = Cs((tab + eol + utfchar)^1)
 
-        return detabber:match(raw)
+    function file_helpers.expandtab (raw)
+        position = 1
+        p_expand:match(raw)
     end
 end
 
+--- Spotted by Philipp A.
 function file_helpers.insert_blank (raw)
-    if not raw:match"\n%s$" then
+    if not raw:find"\n%s$" then
         return raw .. "\n\n"
     end
     return raw
-- 
cgit v1.2.3