summaryrefslogtreecommitdiff
path: root/tex/context/base/m-level.mkii
blob: d758288d7ef924d67af5f4d00ee5a5826daa01e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
%D \module
%D   [       file=level,
%D        version=2002.10.20,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=Catching Nesting Errors,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for 
%C details. 

\unprotect 

\newcount\currentnesting

\def\startnesting#1%
  {\global\advance\currentnesting\plusone
   \setxvalue{level::\number\currentnesting}{#1}}

\def\stopnesting#1%
  {\edef\nestingstring{#1}%
   \relax\ifnum\currentnesting>\zerocount
     \doifelsevalue{level::\number\currentnesting}\nestingstring
       {\global\advance\currentnesting\minusone}
       {\@EA\reportnestingerror\@EA\stoptext}
   \else
     \@EA\reportnestingerror\@EA\stoptext
   \fi}

\def\checknesting
  {\relax\ifnum\currentnesting>\zerocount
     \def\nestingstring{end of document}%
     \@EA\reportnestingerror
   \fi}

\def\reportnestingerror
  {\endgraf
   \global\let\checknesting\relax
   \bgroup \definedfont[Mono at 18pt]\incolortrue 
   \setupinterlinespace 
   \raggedright
   \bgroup \red 
   \ifnum\currentnesting>\plusone
     wrong end level 
   \else  
     too many end levels 
   \fi
   at \nestingstring\space in line \number\inputlineno
   \ifnum\currentnesting>\zerocount, stack:\fi\endgraf
   \egroup
   \dostepwiserecurse\currentnesting\plusone\minusone
     {\space\getvalue{level::\recurselevel}}
   \endgraf
   \egroup
   \writestatus\m!systems{quitting due to level error}\wait
   \batchmode}

\prependtoks 
  \checknesting
\to \everystoptext 

\protect \doifnotmode{demo}{\endinput}

\starttext 

% \startnesting{eerste}
%   \startnesting{tweede}
%     \startnesting{derde}
%       \startnesting{vierde}
%         test 
%       \stopnesting{vierde}
% \stopnesting{eerste}

% \startnesting{eerste}
%   \startnesting{tweede}
%         test 
%       \stopnesting{vierde}
%     \stopnesting{derde}
%   \stopnesting{tweede}
% \stopnesting{eerste}

\startnesting{eerste}
  \startnesting{tweede}
    \startnesting{derde}
      \startnesting{vierde}
        test 
      \stopnesting{vierde}
    \stopnesting{derde}
  \stopnesting{tweede}
\stopnesting{eerste}

\stoptext