summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/strc-blk.mkiv
blob: e521987212d017f09ef8fcd080b37b175da6260c (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
%D \module
%D   [       file=strc-blk,
%D        version=2008.10.20,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Blockmoves,
%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.

\writestatus{loading}{ConTeXt Structure Macros / Blockmoves}

\registerctxluafile{strc-blk}{}

\unprotect

% we run on top of buffers and sections
%
% todo: prefix numbers (needs further integration elsewhere)
%       check functionality
%       alternative files (needs further integration elsewhere)
%
% order matters: \c!before (think of: \c!before=\startitemize)
%
% no \endgroups

\installcorenamespace {block}
\installcorenamespace {blocktemp}

\installcommandhandler \??block {block} \??block

\appendtoks
    \clf_definestructureblock{\currentblock}%
    \setuevalue{\e!begin\currentblock}{\strc_blocks_begin{\currentblock}}%
    \letvalue  {\e!end  \currentblock}\donothing
\to \everydefineblock

% The naive way:
%
% \unexpanded\def\strc_blocks_begin#1%
%   {\dotripleempty\strc_blocks_begin_indeed[#1]}
%
% \unexpanded\def\strc_blocks_begin_indeed[#1][#2][#3]%
%   {\normalexpanded{\buff_pickup{\??block}{\e!begin#1}{\e!end#1}}
%      {}%
%      {\clf_savestructureblock{#1}{#2}{#3}{\??block}}%
%      \plusone}%
%
% We need to prevent too much lookahead which will gobble newlines
% that are needed for buffers. See blocks-002.tex as example.

% maybe: systemmode "block:<name>"

\let\m_block   \empty
\let\m_subblock\empty

\unexpanded\def\strc_blocks_begin#1%
  {\edef\m_block   {#1}%
   \let \m_subblock\empty
   \doifelsenextoptionalcs\strc_blocks_begin_yes\strc_blocks_begin_nop}

\unexpanded\def\strc_blocks_begin_yes[#1]%
  {\doifelseassignmentcs{#1}%
     \strc_blocks_begin_indeed
     \strc_blocks_begin_tagged
     {#1}}

\unexpanded\def\strc_blocks_begin_tagged#1%
  {\edef\m_subblock{#1}%
   \doifelsenextoptionalcs\strc_blocks_begin_yes_yes\strc_blocks_begin_nop}

\unexpanded\def\strc_blocks_begin_yes_yes[#1]%
  {\strc_blocks_begin_indeed{#1}}

\unexpanded\def\strc_blocks_begin_nop
  {\strc_blocks_begin_indeed{}}

\unexpanded\def\strc_blocks_begin_indeed#1%
  {\normalexpanded{\buff_pickup{\??block}{\e!begin\m_block}{\e!end\m_block}}%
     {}%
     {\clf_savestructureblock{\m_block}{\m_subblock}{#1}{\??block}}%
     \plusone}

\let\strc_blocks_setup\relax

\newconstant   \c_strc_blocks_index
\newconditional\c_strc_blocks_display

\unexpanded\def\dostarthiddenblock % called at lua end
  {\begingroup
   \visiblefalse % blocks float
   \startnointerference
   \strc_start_block}

\unexpanded\def\dostophiddenblock % called at lua end
  {\strc_stop_block
   \stopnointerference
   \endgroup}

\unexpanded\def\dostartnormalblock % called at lua end
  {\begingroup
   \visibletrue
   \strc_start_block}

\unexpanded\def\dostopnormalblock % called at lua end
  {\strc_stop_block
   \endgroup}

\def\strc_start_block#1#2%
  {\edef\currentblock{#2}%
   \c_strc_blocks_index#1\relax
   \strc_blocks_setup
   \let\strc_blocks_setup\relax
   \edef\p_alternative{\blockparameter\c!alternative}%
   \ifx\p_alternative\v!text
      \setfalse\c_strc_blocks_display
   \else
      \settrue\c_strc_blocks_display
   \fi
   \ifconditional\c_strc_blocks_display
      \blockparameter\c!before
   \fi
   \begingroup
   \usesetupsparameter\blockparameter\relax
   \dostarttagged\t!block\currentblock
   \useblockstyleandcolor\c!style\c!color
   \blockparameter\c!inner % old
   \ifconditional\c_strc_blocks_display
     \usealignparameter\blockparameter
   \else
     \blockparameter\c!left
   \fi
   \ignorespaces}

\def\strc_stop_block
  {\removeunwantedspaces
   \ifconditional\c_strc_blocks_display
     \par
   \else
     \blockparameter\c!right
   \fi
   \dostoptagged
   \endgroup
   \ifconditional\c_strc_blocks_display
     \blockparameter\c!after
   \fi}

\def\strc_blocks_set_state[#1][#2][#3]% state name tag
  {\clf_setstructureblockstate{#1}{#2}{#3}}

\def\strc_blocks_select[#1][#2][#3][#4]% state name tag setups
  {\begingroup
   \doifelseassignment{#3}
     {\getparameters[\??blocktemp][\c!criterium=\v!text,#3]%
      \def\strc_blocks_setup{\setupcurrentblock[#3]}%
      \clf_selectstructureblock{#1}{#2}{}{\csname\??blocktemp\c!criterium\endcsname}}
     {\getparameters[\??blocktemp][\c!criterium=\v!text,#4]%
      \def\strc_blocks_setup{\setupcurrentblock[#4]}%
      \clf_selectstructureblock{#1}{#2}{#3}{\csname\??blocktemp\c!criterium\endcsname}}%
   \endgroup}

\def\blockuservariable#1%
  {\clf_structureblockuservariable\c_strc_blocks_index{#1}}

% hide   : save, if [+] also hidden execute
% keep   : save and normal execute
% use    : normal execute unless [-]
% process: hidden execute unless [-]
% select : idem use

\unexpanded\def\hideblocks   {\dotripleempty   \strc_blocks_set_state[hide]}
\unexpanded\def\keepblocks   {\dotripleempty   \strc_blocks_set_state[keep]}
\unexpanded\def\useblocks    {\doquadrupleempty\strc_blocks_select   [use]}
\unexpanded\def\processblocks{\doquadrupleempty\strc_blocks_select   [process]}
\unexpanded\def\selectblocks {\doquadrupleempty\strc_blocks_select   [use]}

\protect \endinput