From 854b18e30b45d8bdfb36ee90db29c7433b3b2014 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= <mpg@elzevir.fr>
Date: Sat, 21 May 2011 14:58:26 +0200
Subject: implement catcode table (number) stacks

---
 luatexbase-cctb.dtx | 94 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 87 insertions(+), 7 deletions(-)

(limited to 'luatexbase-cctb.dtx')

diff --git a/luatexbase-cctb.dtx b/luatexbase-cctb.dtx
index e127e5a..0baf01e 100644
--- a/luatexbase-cctb.dtx
+++ b/luatexbase-cctb.dtx
@@ -253,8 +253,8 @@ See source file '\inFileName' for details.
 % \begin{quote}
 % |\PushCatcodeTableNumStack|\\
 % |\IncCatcodeTableStack|\\
-% |\setluatexcatcodetable\CatcodeTableStack{\catcodetable#1}%|\\
-% |\catcodetable\CatcodeTableStack|
+% |\setluatexcatcodetable\CatcodeTableStack{\luatexcatcodetable#1}%|\\
+% |\luatexcatcodetable\CatcodeTableStack|
 % \end{quote}
 % and \cs{EndCatcodeRegime} is defined as
 % \begin{quote}
@@ -387,15 +387,21 @@ See source file '\inFileName' for details.
 \luatexbase@ensure@primitive{savecatcodetable}
 %    \end{macrocode}
 %
-%    \subsubsection{User macros}
+%    \subsubsection{Allocation and temporary tables}
 %
-%    The allocation macro. Allocate tables starting with 1, since table 0 is
-%    the default table. Allocate only odd numbers to allow using even numbers
-%    as a stack.
+%    Counters for the allocated and temporary tables.
 %
 %    \begin{macrocode}
 \newcount\lltxb@catcodetable@alloc
 \lltxb@catcodetable@alloc\m@ne
+\newcount\CatcodeTableStack
+\CatcodeTableStack\z@
+%    \end{macrocode}
+%
+%    The allocation macro. Allocate only odd numbers to allow using even
+%    numbers for temporary tables.
+%
+%    \begin{macrocode}
 \def\newluatexcatcodetable#1{%
   \ifnum\lltxb@catcodetable@alloc<65535\relax
     \global\advance\lltxb@catcodetable@alloc\tw@
@@ -412,6 +418,30 @@ See source file '\inFileName' for details.
   \fi}
 %    \end{macrocode}
 %
+%    Allocate a new temporary table, using even numbers.
+%
+%    \begin{macrocode}
+\def\IncCatcodeTableStack{%
+  \ifnum\CatcodeTableStack<65534\relax
+    \global\advance\CatcodeTableStack\tw@
+  \else
+    \errmessage{CatcodeTableStack overflow}%
+  \fi}
+%    \end{macrocode}
+%
+%    Free a temporary table.
+%
+%    \begin{macrocode}
+\def\DecCatcodeTableStack{%
+  \ifnum\CatcodeTableStack>\z@
+    \global\advance\CatcodeTableStack-2\relax
+  \else
+    \errmessage{CatcodeTableStack underflow}%
+  \fi}
+%    \end{macrocode}
+%
+%    \subsubsection{Setting catcode tables}
+%
 %    A small patch to get two new counters in Plain too.
 %
 %    \begin{macrocode}
@@ -524,12 +554,62 @@ See source file '\inFileName' for details.
   }
 %    \end{macrocode}
 %
-%    Finally do the shortcuts.
+%    Now do the shortcuts.
 %
 %    \begin{macrocode}
 \luatexbase@directlua{luatexbase.catcodetable_do_shortcuts()}
 %    \end{macrocode}
 %
+%    \subsubsection{Catcode table number stack and regimes}
+%
+%    The stack is implemented as a list of (brace-enclosed) \tex arguments.
+%    Initially, in contains only $0$, the number of the active table when
+%    \luatex starts.
+%
+%    \begin{macrocode}
+\def\lltxb@cctb@numstack{0}
+%    \end{macrocode}
+%
+%    Pushing a number on the stack actually means adding it to the beginning
+%    of the list (ie, unshifting it).
+%
+%    \begin{macrocode}
+\def\PushCatcodeTableNumStack{%
+  \xdef\lltxb@cctb@numstack{{\the\catcodetable}\lltxb@cctb@numstack}}
+%    \end{macrocode}
+%
+%    Popping a number off the stack means shifting it.
+%
+%    \begin{macrocode}
+\def\PopCatcodeTableNumStack{%
+  \expandafter\lltxb@cctb@numstack@pop\lltxb@cctb@numstack\@nil}
+\def\lltxb@cctb@numstack@pop#1#2\@nil{%
+  \ifx\empty#2\empty
+    \errormessage{Popping from and empty CatcoteTableNumStack}%
+    \luatexcatcodetable\z@
+  \else
+    \def\lltxb@numstack@pop{#2}%
+    \luatexcatcodetable#1\relax
+  \fi}
+%    \end{macrocode}
+%
+%    Now the catcode regime pseudo-environment as presented in the
+%    documentation.
+%
+%    \begin{macrocode}
+\def\BeginCatcodeRegime#1{%
+  \PushCatcodeTableNumStack
+  \IncCatcodeTableStack
+  \setluatexcatcodetable\CatcodeTableStack{\luatexcatcodetable#1}%
+  \luatexcatcodetable\CatcodeTableStack}
+%    \end{macrocode}
+%
+%    \begin{macrocode}
+\def\EndCatcodeRegime{%
+  \DecCatcodeTableStack
+  \PopCatcodeTableNumStack}
+%    \end{macrocode}
+%
 %    That's all, folks!
 %
 %    \begin{macrocode}
-- 
cgit v1.2.3

