summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/page-mbk.mklx
blob: 3dc1c4e7c95060c8f9c65acdeb89109bb3a81c0e (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
%D \module
%D   [       file=page-mbk,   % was part of page-mis.mkiv / 2008.11.17
%D        version=2011.11.23, % was part of page-flt.tex  / 2000.10.20
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Margin Floats,
%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 Page Macros / Margin Floats}

\unprotect

%D This is an old mechanism that is quite independent of other floats. There is for
%D instance no save/restore used here. When there is need (and demand) we can use
%D the float cache and then flush them in the text when they are left over. On the
%D other hand, margin float are somewhat manual so it does not make too much sense
%D to complicate the code. As an alternative users can fall back on margin texts:
%D they do stack but don't float.
%D
%D \starttyping
%D \dorecurse{9} {
%D   \placefigure[margin]{#1}{}
%D   \input ward
%D }
%D \page \null % will flush them
%D \stoptyping

% status=start: in margin
% status=stop : in text
%
% todo: flush margin floats at end of text
% todo: separate boxes for left and right

\installcorenamespace{marginblock}
\installcorenamespace{marginblocklocation}

\installcommandhandler \??marginblock {marginblock} \??marginblock

\aliased\let\setupmarginblocks\setupmarginblock

\newconditional\c_page_margin_blocks_enabled % not really needed as we can check each time
\newconditional\c_page_margin_blocks_present

\newbox        \b_page_margin_blocks
\newbox        \b_page_margin_blocks_prepared

\appendtoks
   \doifelse{\rootmarginblockparameter\c!state}\v!start
     \settrue\setfalse\c_page_margin_blocks_enabled
\to \everysetupmarginblock

\setupmarginblocks
  [\c!state=\v!start,
   \c!location=\v!inmargin,
   \c!width=\rightmarginwidth,
  %\c!style=,
  %\c!color=,
  %\c!align=,
  %\c!left=,
  %\c!right=,
  %\c!top=,
  %\c!before=,
  %\c!after=,
   \c!inbetween=\blank,
   \c!bottom=\vfill]

\definesystemconstant{marginblock}

\resetboxesincache \s!marginblock \s!marginblock

\setfalse\c_page_margin_blocks_enabled

\let\page_margin_blocks_stop_block\relax

\permanent\tolerant\protected\def\startmarginblock[#tag]%
  {\begingroup
   \edef\currentmarginblock{#tag}%
   \ifcstok{\marginblockparameter\c!state}\v!start
     \expandafter\page_margin_blocks_start_block_yes
   \else
     \expandafter\page_margin_blocks_start_block_nop
   \fi}

\permanent\protected\def\stopmarginblock
  {\page_margin_blocks_stop_block
   \endgroup}

\def\page_margin_blocks_start_block_yes
  {%\showmessage\m!layouts4\empty
   \global\settrue\c_page_margin_blocks_enabled
   \dowithnextboxcs
     \page_margin_blocks_start_block_save
     \vbox\bgroup
       \let\page_margin_blocks_stop_block\page_margin_blocks_stop_block_yes
       \hsize\marginblockparameter\c!width
       \usealignparameter\marginblockparameter
       \usemarginblockstyleandcolor\c!style\c!color
       \begstrut
       \ignorespaces}

\def\page_margin_blocks_stop_block_yes
  {\removeunwantedspaces
   \endstrut
   \egroup}

\def\page_margin_blocks_start_block_save
  {\putboxincache\s!marginblock{+}\nextbox}

\def\page_margin_blocks_start_block_nop
  {%\showmessage\m!layouts5\empty
   \marginblockparameter\c!before
   \bgroup
   \let\page_margin_blocks_stop_block\page_margin_blocks_stop_block_nop
   \usemarginblockstyleandcolor\c!style\c!color}

\def\page_margin_blocks_stop_block_nop
  {\egroup
   \marginblockparameter\c!after}

\permanent\protected\def\checkmarginblocks
  {\ifconditional\c_page_margin_blocks_enabled\ifcase\getboxcountfromcache\s!marginblock\else
     \expandafter\page_margin_blocks_check_indeed
   \fi\fi}

\def\page_margin_blocks_check_indeed
  {\ifcsname\??marginblocklocation\marginblockparameter\c!location\endcsname
     \page_margin_blocks_prepare_box
     \csname\??marginblocklocation\marginblockparameter\c!location\endcsname
   \else
     \global\setbox\b_page_margin_blocks\emptybox
   \fi}

\def\page_margin_blocks_prepare_box
  {\begingroup
   \scratchcounter\zerocount
   \localcontrolledloop \plusone \getboxcountfromcache\s!marginblock \plusone
     {\scratchcounter\currentloopiterator
      \setbox\scratchbox\vbox\bgroup
        \marginblockparameter\c!top
        \localcontrolledloop \plusone \scratchcounter \plusone
          {\ifcase\currentloopiterator\or
             \marginblockparameter\c!before
           \else
             \marginblockparameter\c!inbetween
           \fi
           \setbox\scratchbox\emptyvbox
           \ht\scratchbox\getboxhtfromcache\s!marginblock{\number\currentloopiterator}%
           \dp\scratchbox\getboxdpfromcache\s!marginblock{\number\currentloopiterator}%
           \box\scratchbox
           \marginblockparameter\c!after}%
       \marginblockparameter\c!bottom
      \egroup
      \ifdim\htdp\scratchbox>\textheight
        \advanceby\scratchcounter\minusone
       %\writestatus{quit}{\the\scratchcounter=>\the\htdp\scratchbox}
        \quitloop
      \else
       %\writestatus{progress}{\the\htdp\scratchbox}
      \fi}%
   \ifcase\scratchcounter\else
     \global\setbox\b_page_margin_blocks_prepared\vbox to \textheight\bgroup
       \marginblockparameter\c!top
       \localcontrolledloop \plusone \scratchcounter \plusone
         {\ifcase\currentloopiterator\or
            \marginblockparameter\c!before
          \else
            \marginblockparameter\c!inbetween
          \fi
          \getboxfromcache\s!marginblock{\number\currentloopiterator}\scratchbox
          \box\scratchbox
          \marginblockparameter\c!after}%
       \marginblockparameter\c!bottom
    \egroup
    \pruneboxesincache\s!marginblock
    \ifcase\getboxcountfromcache\s!marginblock\relax
      \global\settrue\c_page_margin_blocks_enabled
    \fi
  \fi
  \endgroup}

% inner outer

\permanent\protected\def\page_margin_blocks_place_r_yes
  {\setbox\b_page_margin_blocks_prepared\hbox to \rightmarginwidth
     {\marginblockparameter\c!left
      \box\b_page_margin_blocks_prepared
      \marginblockparameter\c!right}%
   \vsmashbox\b_page_margin_blocks_prepared
   \box\b_page_margin_blocks_prepared}

\permanent\protected\def\page_margin_blocks_place_l_yes
  {\setbox\b_page_margin_blocks_prepared\hbox to \leftmarginwidth
     {\marginblockparameter\c!right
      \box\b_page_margin_blocks_prepared
      \marginblockparameter\c!left}%
   \vsmashbox\b_page_margin_blocks_prepared
   \box\b_page_margin_blocks_prepared}

\permanent\protected\def\page_margin_blocks_place_r_nop{\hskip\rightmarginwidth}
\permanent\protected\def\page_margin_blocks_place_l_nop{\hskip\leftmarginwidth}

\aliased\let\placerightmarginblock\page_margin_blocks_place_r_nop
\aliased\let\placeleftmarginblock \page_margin_blocks_place_l_nop

\def\page_margin_blocks_set_r_box{\enforced\aliased\let\placerightmarginblock\page_margin_blocks_place_r_yes}
\def\page_margin_blocks_set_l_box{\enforced\aliased\let\placeleftmarginblock \page_margin_blocks_place_l_yes}

\letcsname\??marginblocklocation\v!left \endcsname\page_margin_blocks_set_l_box
\letcsname\??marginblocklocation\v!right\endcsname\page_margin_blocks_set_r_box

\defcsname\??marginblocklocation\v!inmargin\endcsname
  {\doifbothsidesoverruled
     \page_margin_blocks_set_r_box
     \page_margin_blocks_set_r_box
     \page_margin_blocks_set_l_box}

\defcsname\??marginblocklocation\v!middle\endcsname
  {\doifbothsidesoverruled
     \page_margin_blocks_set_r_box
     \page_margin_blocks_set_l_box
     \page_margin_blocks_set_r_box}

% margin floats (keyword 'margin' in option list)

\protected\def\page_margin_blocks_process_float
  {\doifelse{\marginblockparameter\c!state}\v!start
     \page_margin_blocks_process_float_yes
     \page_margin_blocks_process_float_nop}

\def\page_margin_blocks_process_float_yes
  {\global\settrue\c_page_margin_blocks_enabled
   \putboxincache\s!marginblock{+}\floatbox}

\def\page_margin_blocks_process_float_nop
  {\handlefloatmethod\v!here}

\appendtoks
   \ifcase\getboxcountfromcache\s!marginblock\else
     \writestatus\m!layouts{beware: there are left-over margin floats!}%
   \fi
\to \everystoptext

\permanent\protected\def\flushmarginblocks
 {\ifconditional\c_page_margin_blocks_enabled
    \page_otr_command_flush_margin_blocks
  \fi}

\permanent\tolerant\protected\def\flushallmarginblocks[#1]%
 {\ifconditional\c_page_margin_blocks_enabled
    \begingroup
    \scratchcounterone\getboxcountfromcache\s!marginblock\relax
    \ifcase\scratchcounterone\else
      \scratchcountertwo\ifparameter#1\or\numexpr\scratchcounterone-#1+\plusone\relax\else\plusone\fi
      \localcontrolledloop \scratchcountertwo \scratchcounterone \plusone
        {\ifcase\currentloopiterator\or
           \marginblockparameter\c!before
         \else
           \marginblockparameter\c!inbetween
         \fi
         \getboxfromcache\s!marginblock{\number\currentloopiterator}\scratchbox
         \box\scratchbox
         \marginblockparameter\c!after}%
      \pruneboxesincache\s!marginblock
    \fi
    \endgroup
  \fi}

\protect \endinput