summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/spac-par.mkxl
blob: 574cfec12d46448ca44850b14385dc24cd80661b (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
%D \module
%D   [       file=spac-par,
%D        version=2009.10.16, % 1997.03.31, was core-spa.tex
%D          title=\CONTEXT\ Spacing Macros,
%D       subtitle=Paragraphs,
%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 Spacing Macros / Paragraphs}

\registerctxluafile{spac-par}{autosuffix}

\unprotect

%D New experimental stuff:

% this might move to syst-ini.mkxl

\setnewconstant\frozenhsizecode                "0000001
\setnewconstant\frozenleftskipcode             "0000002
\setnewconstant\frozenrightskipcode            "0000004
\setnewconstant\frozenhangindentcode           "0000008
\setnewconstant\frozenhangaftercode            "0000010
\setnewconstant\frozenparindentcode            "0000020
\setnewconstant\frozenparfillskipcode          "0000040
\setnewconstant\frozenadjustspacingcode        "0000080
\setnewconstant\frozenprotrudecharscode        "0000100
\setnewconstant\frozenpretolerancecode         "0000200
\setnewconstant\frozentolerancecode            "0000400
\setnewconstant\frozenemergencystretchcode     "0000800
\setnewconstant\frozenloosenesscode            "0001000
\setnewconstant\frozenlastlinefitcode          "0002000
\setnewconstant\frozenlinepenaltycode          "0004000
\setnewconstant\frozeninterlinepenaltycode     "0008000
\setnewconstant\frozenwidowpenaltycode         "0010000
\setnewconstant\frozenclubpenaltycode          "0020000
\setnewconstant\frozenbrokenpenaltycode        "0040000
\setnewconstant\frozenadjdemeritscode          "0080000
\setnewconstant\frozendoublehyphendemeritscode "0100000
\setnewconstant\frozenfinalhyphendemeritscode  "0200000
\setnewconstant\frozenparshapecode             "0400000
\setnewconstant\frozeninterlinepenaltiescode   "0800000
\setnewconstant\frozenwidowpenaltiescode       "1000000
\setnewconstant\frozenclubpenaltiescode        "2000000

\setnewconstant\paragraphupdatecodes\numexpr
    \frozentolerancecode
  + \frozenloosenesscode
  + \frozenlinepenaltycode
  + \frozeninterlinepenaltycode
  + \frozenwidowpenaltycode
  + \frozenclubpenaltycode
  + \frozenbrokenpenaltycode
  + \frozenadjdemeritscode
  + \frozendoublehyphendemeritscode
  + \frozenfinalhyphendemeritscode
  + \frozeninterlinepenaltiescode
  + \frozenwidowpenaltiescode
  + \frozenclubpenaltiescode
\relax

\setnewconstant\paragraphpenaltycodes\numexpr
    \frozeninterlinepenaltycode
  + \frozenwidowpenaltycode
  + \frozenclubpenaltycode
  + \frozenbrokenpenaltycode
  + \frozeninterlinepenaltiescode
  + \frozenwidowpenaltiescode
  + \frozenclubpenaltiescode
\relax

\setnewconstant\paragraphdemeritcodes\numexpr
    \frozenadjdemeritscode
  + \frozendoublehyphendemeritscode
  + \frozenfinalhyphendemeritscode
\relax

% + \frozenparskipcode

\setnewconstant\paragraphshapecodes\numexpr
   \frozenhangindentcode
 + \frozenhangaftercode
 + \frozenleftskipcode
 + \frozenrightskipcode
 + \frozenparfillskipcode
 + \frozenparshapecode
\relax

\protected\def\freezeparagraphproperties {\snapshotpar\maxcount}
\protected\def\defrostparagraphproperties{\snapshotpar\zerocount}

\protected\def\updateparagraphproperties {\ifhmode\snapshotpar\paragraphupdatecodes \fi}
\protected\def\updateparagraphpenalties  {\ifhmode\snapshotpar\paragraphpenaltycodes\fi}
\protected\def\updateparagraphdemerits   {\ifhmode\snapshotpar\paragraphdemeritcodes\fi}
\protected\def\updateparagraphshapes     {\ifhmode\snapshotpar\paragraphshapecodes  \fi}

% so far

\let\spac_paragraph_freeze\relax

\protected\def\setparagraphfreezing      {\let\spac_paragraph_freeze\freezeparagraphproperties}
\protected\def\forgetparagraphfreezing   {\let\spac_paragraph_freeze\relax}

\installcorenamespace {bparwrap}
\installcorenamespace {eparwrap}
\installcorenamespace {parwrapbefore}
\installcorenamespace {parwrapafter}
\installcorenamespace {parwrapcount}

\let\spac_paragraph_wrap\relax

\newcount\c_spac_paragraph_group_level

\protected\def\spac_paragraph_update
  {\c_spac_paragraph_group_level\currentgrouplevel\relax
   \ifcsname\??bparwrap\the\c_spac_paragraph_group_level\endcsname
     \the\lastnamedcs
     \relax
     \dontleavehmode % just in case
     \wrapuppar{\the\csname\??eparwrap\the\c_spac_paragraph_group_level\endcsname\relax}%
   \fi}

\protected\def\registerparwrapper       {\spac_register_par_wrapper\toksapp\tokspre}
\protected\def\registerparwrapperreverse{\spac_register_par_wrapper\tokspre\toksapp}

\protected\def\spac_paragraph_install
  {\expandafter\newtoks\csname\??bparwrap\the\currentgrouplevel\endcsname
   \expandafter\newtoks\csname\??eparwrap\the\currentgrouplevel\endcsname}

\def\spac_paragraph_install_count#1%
  {\expandafter\newcount\csname\??parwrapcount#1\endcsname}

\def\spac_paragraph_install_pair#1#2#3%
  {\expandafter\newtoks\csname\??parwrapbefore#3\endcsname
   \expandafter\newtoks\csname\??parwrapafter #3\endcsname
   #1\csname\??bparwrap\the\currentgrouplevel\endcsname
     {\the\csname\??parwrapbefore#3\endcsname}%
   #2\csname\??eparwrap\the\currentgrouplevel\endcsname
     {\the\csname\??parwrapafter #3\endcsname}}

\def\spac_register_par_wrapper_yes#1#2#3#4#5%
  {\ifcsname\??bparwrap\the\currentgrouplevel\endcsname \else
     \spac_paragraph_install
   \fi
   \ifcsname\??parwrapcount#3\endcsname \else
     \spac_paragraph_install_count{#3}%
   \fi
   \ifcsname\??parwrapbefore#3\endcsname \else
     \spac_paragraph_install_pair#1#2{#3}%
   \fi
   #1\csname\??parwrapbefore#3\endcsname{\advance\csname\??parwrapcount#3\endcsname\plusone\relax
                                         \clf_setparwrapper{#3}#4}%
   #2\csname\??parwrapafter #3\endcsname{#5}%
   \clf_newparwrapper{#3}%
   \let\spac_paragraph_wrap\spac_paragraph_update}

\def\spac_register_par_wrapper_nop#1#2#3#4#5%
  {\ifcsname\??parwrapcount#3\endcsname \else
     \spac_paragraph_install_count{#3}%
   \fi
   \csname\??parwrapcount #3\endcsname\plusone
   #4\wrapuppar{#5}}

\protected\def\spac_register_par_wrapper
  {\ifhmode
     \expandafter\spac_register_par_wrapper_nop
   \else
     \expandafter\spac_register_par_wrapper_yes
   \fi}

\protected\def\forgetparwrapper
  {\csname\??bparwrap\the\currentgrouplevel\endcsname\emptytoks
   \csname\??eparwrap\the\currentgrouplevel\endcsname\emptytoks}

\protected\def\unregisterparwrapper#1%
  {\csname\??parwrapcount #1\endcsname\zerocount
   \csname\??parwrapbefore#1\endcsname\emptytoks
   \csname\??parwrapafter #1\endcsname\emptytoks}

\def\directparwrapper#1#2%
  {#1\wrapuppar{#2}}

% \protected\def\doifelseparwrapper#1%
%   {\ifcsname\??parwrapbefore#1\endcsname
%      \unless\iftok\lastnamedcs\emptytoks
%        \doubleexpandafter\firstoftwoarguments
%      \orelse\iftok\csname\??parwrapafter#1\endcsname\emptytoks
%        \doubleexpandafter\secondoftwoarguments
%      \else
%        \doubleexpandafter\firstoftwoarguments
%      \fi
%    \else
%      \expandafter\secondoftwoarguments
%    \fi}

\protected\def\doifelseparwrapper#1%
  {\unless\ifcsname\??parwrapcount#1\endcsname
     \expandafter\secondoftwoarguments
   \orelse\ifcase\lastnamedcs
     \expandafter\secondoftwoarguments
   \else
     \expandafter\firstoftwoarguments
   \fi}

% \getparwrapper  % defined in lua
% \lastparwrapper % defined in lua

\protected\def\showparwrapperstate#1%
  {\begingroup
   \infofont ¶#1\hilo
     {\smallinfofont\getparwrapper {#1}}%
     {\smallinfofont\lastparwrapper{#1}}%
   \endgroup}

%appendtoks\updateparwrapperindeed\to\everypar
%appendtoks\spac_paragraph_wrap   \to\everypar
%appendtoks\spac_paragraph_freeze \to\everypar

\setparagraphfreezing

\appendtoks\let\spac_paragraph_wrap\relax\to\everyforgetall

%D In due time, the code below will be upgraded using the above mechanisms.

%D The dreadful sequence \type {\bgroup} \unknown\ \type {\carryoverpar} \unknown\
%D \type {\egroup} is needed when for instance sidefloats are used in combination
%D with something that starts with a group. This is because otherwise the
%D indentation as set (by the output routine) inside the group are forgotten
%D afterwards. (I must not forget its existence).

\def\carryoverpar#1% #1 can be \endgroup or \egroup or ... expandable !
  {\normalexpanded
     {\noexpand#1%
      \hangindent\the\hangindent
      \hangafter \the\hangafter
      \parskip   \the\parskip
      \leftskip  \the\leftskip
      \rightskip \the\rightskip}}

\unexpanded\def\pushparagraphproperties
  {\edef\currentparagraphproperties{\carryoverpar\relax}%
   \pushmacro\currentparagraphproperties}

\unexpanded\def\popparagraphproperties
  {\popmacro\currentparagraphproperties
   \currentparagraphproperties}

\unexpanded\def\flushparagraphproperties
  {\popmacro\currentparagraphproperties}

%D Beware, changing this will break some code (like pos/backgrounds) but it has been
%D changed anyway so let's see where things go wrong.

\installcorenamespace{paragraphintro}

\let\insertparagraphintro\relax % hook into everypar

\newtoks\t_spac_paragraphs_intro_first
\newtoks\t_spac_paragraphs_intro_next
\newtoks\t_spac_paragraphs_intro_each

\newconditional\c_spac_paragraphs_intro_first
\newconditional\c_spac_paragraphs_intro_next
\newconditional\c_spac_paragraphs_intro_each

\unexpanded\def\setupparagraphintro
  {\dodoubleempty\spac_paragraphs_intro}

\unexpanded\def\spac_paragraphs_intro[#1][#2]%
  {\def\spac_paragraphs_intro_step##1%
     {\csname\??paragraphintro\ifcsname\??paragraphintro##1\endcsname##1\fi\endcsname{#2}}%
   \processcommacommand[#1]\spac_paragraphs_intro_step}

\letvalue{\??paragraphintro\empty}\gobbleoneargument

\setvalue{\??paragraphintro\v!reset}#1%
  {\global\setfalse\c_spac_paragraphs_intro_first
   \global\setfalse\c_spac_paragraphs_intro_next
   \global\setfalse\c_spac_paragraphs_intro_each
   \global\t_spac_paragraphs_intro_first\emptytoks
   \global\t_spac_paragraphs_intro_next \emptytoks
   \global\t_spac_paragraphs_intro_each \emptytoks
   \glet\insertparagraphintro\relax}

\setvalue{\??paragraphintro\v!first}#1%
  {\global\settrue\c_spac_paragraphs_intro_first
   \gtoksapp\t_spac_paragraphs_intro_first{#1}%
   \glet\insertparagraphintro\spac_paragraphs_flush_intro}

\setvalue{\??paragraphintro\v!next}#1%
  {\global\settrue\c_spac_paragraphs_intro_next
   \gtoksapp\t_spac_paragraphs_intro_next{#1}%
   \glet\insertparagraphintro\spac_paragraphs_flush_intro}

\setvalue{\??paragraphintro\v!each}#1%
  {\global\settrue\c_spac_paragraphs_intro_each
   \gtoksapp\t_spac_paragraphs_intro_each{#1}%
   \glet\insertparagraphintro\spac_paragraphs_flush_intro}

%D We can say:
%D
%D \starttyping
%D \setupparagraphintro[first][\index{Knuth}]
%D \stoptyping
%D
%D Maybe more convenient is:
%D
%D \starttyping
%D \flushatparagraph{\index{Zapf}}
%D \stoptyping
%D
%D \starttyping
%D \setupparagraphintro[first][\hbox to 3.5em{\tt FIRST \hss}]
%D \setupparagraphintro[first][\hbox to 3.5em{\tt TSRIF \hss}]
%D \setupparagraphintro[next] [\hbox to 3.5em{\tt NEXT  \hss}]
%D \setupparagraphintro[next] [\hbox to 3.5em{\tt TXEN  \hss}]
%D \setupparagraphintro[each] [\hbox to 3.0em{\tt EACH  \hss}]
%D \setupparagraphintro[each] [\hbox to 3.0em{\tt HCEA  \hss}]
%D
%D some paragraph \par
%D some paragraph \par
%D some paragraph \par
%D some paragraph \par
%D
%D \setupparagraphintro[first][\hbox to 3.5em{\tt FIRST \hss}]
%D \setupparagraphintro[first][\hbox to 3.5em{\tt TSRIF \hss}]
%D
%D some paragraph \par
%D some paragraph \par
%D
%D \setupparagraphintro[reset]
%D
%D some paragraph \par
%D \stoptyping

\unexpanded\def\flushatparagraph#1%
  {\global\c_spac_paragraphs_intro_first\plusone
   \gtoksapp\t_spac_paragraphs_intro_first{#1}%
   \glet\insertparagraphintro\spac_paragraphs_flush_intro}

%D Here comes the flusher (we misuse the one level expansion of token registers to
%D feed a nice stream into the paragraph.)

\unexpanded\def\spac_paragraphs_flush_intro % we make sure that the token lists expand directly
  {\normalexpanded{%                        % after another so the first code is there twice
     \ifconditional\c_spac_paragraphs_intro_each
       \ifconditional\c_spac_paragraphs_intro_next
         \glet\insertparagraphintro\spac_paragraphs_flush_intro_next
       \else
         \glet\insertparagraphintro\spac_paragraphs_flush_intro_each
       \fi
       \ifconditional\c_spac_paragraphs_intro_first
         \global\setfalse\c_spac_paragraphs_intro_first
         \global\t_spac_paragraphs_intro_first\emptytoks
         \the\t_spac_paragraphs_intro_first
       \fi
       \the\t_spac_paragraphs_intro_each
     \else
       \ifconditional\c_spac_paragraphs_intro_next
         \glet\insertparagraphintro\spac_paragraphs_flush_intro_next
       \fi
       \ifconditional\c_spac_paragraphs_intro_first
         \global\setfalse\c_spac_paragraphs_intro_first
         \global\t_spac_paragraphs_intro_first\emptytoks
         \the\t_spac_paragraphs_intro_first
       \fi
     \fi}}

\unexpanded\def\spac_paragraphs_flush_intro_next
  {\normalexpanded{%
     \global\setfalse\c_spac_paragraphs_intro_next
     \global\t_spac_paragraphs_intro_next\emptytoks
     \ifconditional\c_spac_paragraphs_intro_each
       \glet\insertparagraphintro\spac_paragraphs_flush_intro_each
       \the\t_spac_paragraphs_intro_next
       \the\t_spac_paragraphs_intro_each
     \else
       \glet\insertparagraphintro\relax
       \the\t_spac_paragraphs_intro_next
     \fi}}

\unexpanded\def\spac_paragraphs_flush_intro_each
  {\the\t_spac_paragraphs_intro_each}

%D \macros
%D   {flushatnextpar}
%D
%D This macro collects data that will be flushed at the next paragraph. By using
%D this macro you can avoid interfering nodes (writes, etc).

\let\flushpostponednodedata\relax % hook into everypar

\newbox  \b_spac_postponed_data
%newcount\c_spac_postponed_data

% \installcorenamespace {postponednodesstack}
%
% \initializeboxstack\??postponednodesstack
%
% \unexpanded\def\pushpostponednodedata
%   {\global\advance\c_spac_postponed_data\plusone
%    \savebox\??postponednodesstack{\the\c_spac_postponed_data}{\box\b_spac_postponed_data}}
%
% \unexpanded\def\poppostponednodedata
%   {\global\setbox\b_spac_postponed_data\hbox{\foundbox\??postponednodesstack{\the\c_spac_postponed_data}}%
%    \global\advance\c_spac_postponed_data\minusone
%    \ifvoid\b_spac_postponed_data\else
%      \glet\flushpostponednodedata\spac_postponed_data_flush
%    \fi}

\newtoks\everyflushatnextpar

\unexpanded\def\pushpostponednodedata
  {\globalpushbox\b_spac_postponed_data}

\unexpanded\def\poppostponednodedata
  {\globalpopbox\b_spac_postponed_data
   \ifvoid\b_spac_postponed_data\else
     \glet\flushpostponednodedata\spac_postponed_data_flush
   \fi}

\unexpanded\def\flushatnextpar
  {\begingroup
   \the\everyflushatnextpar
   \glet\flushpostponednodedata\spac_postponed_data_flush
   \dowithnextboxcs\spac_postponed_data_finish\hpack}

% \def\spac_postponed_data_finish
%   {\global\setbox\b_spac_postponed_data\hpack % to\zeropoint
%      {\box\b_spac_postponed_data\box\nextbox}%
%    \endgroup}
%
% This is better when used with protrusion which does not like too deeply nested
% boxes:
%
% \def\spac_postponed_data_finish
%   {\dontcomplain
%    \global\setbox\b_spac_postponed_data\hpack to \zeropoint
%      {\unhbox\b_spac_postponed_data\unhbox\nextbox}%
%    \endgroup}
%
% We could do this if there is content with widths ... not that is should be used
% that way, but this way we don't overflow:

\def\spac_postponed_data_finish
  {\dontcomplain
   \global\setbox\b_spac_postponed_data\hpack % to \zeropoint
     {\unhbox\b_spac_postponed_data
      \scratchdimen\wd\nextbox
      \unhbox\nextbox
      \ifcase\scratchdimen\else\kern-\b_spac_postponed_data\fi}%
   \endgroup}

\def\spac_postponed_data_flush
  {%\iftrialtypesetting \else
     \ifvoid\b_spac_postponed_data\else
       \hpack{\smashedbox\b_spac_postponed_data}% \box\b_spac_postponed_data
     \fi
     \glet\flushpostponednodedata\relax
   }%\fi}

\unexpanded\def\doflushatpar % might be renamed
  {\ifvmode
     \expandafter\flushatnextpar
   \else
     \expandafter\firstofoneargument
   \fi}

\protect \endinput