summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/partoken.ch
blob: c7cec2aff8eb75c4d5957983001f4da52a9419b4 (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
% $Id$
% Implementation of |\partokenname| and |\partokencontext| primitives.
% Public domain. Originally written by Petr Olsak, 2021.

% TeX inserts (let's say) "par-token" at empty lines and in other situations.
% The primitive meaning of "par-token" is (let's say) "end-paragraph",
%   but it can be changed by the macro programmer (by |\def\par|, for example).
% The name of the auto-emitted "par-token" is fixed as |\par| in
%   classical TeX. 
% The new |\partokenname| primitive defined here allows changing this cs name.
% After |\partokenname <control-sequence>| is given, the specified
%   <control-sequence> will be emitted at empty lines, etc., not |\par|. 
% Moreover, the given <control-sequence> plays the role of |\par| when
%   processing not-\long macros ("runaway error").
% The setting of |\partokenname| is global.
%
% The integer register |\partokencontext| allows controlling the places where
% "par-token" is auto-emitted. The value of |\partokencontext| can be:
%   0 ... "par-token" is emitted as in classical TeX, i.e. at empty lines,
%         before \end, \vskip, \hrule, \unvbox, and \halign (if horizontal mode
%         is current) and in several error recovery situations.
%         This is the default value.
%   1 ... "par-token" is emitted as before and at the end of \vbox,
%         \vtop, and \vcenter if horizontal mode is current here.
%   2 ... "par-token" is emitted as before and at the end of \noalign,
%         \vadjust, \output, \insert, and \valign items, if horizontal
%         mode is current here.
% Classical TeX does a direct call of the "end-paragraph" routine at the
% places mentioned in cases 1 and 2.
% 
% See the user documentation in the pdftex manual for more,
% the tests/partoken-test.tex file for basic examples,
% and the OpTeX format (eventually) for the practical case inspiring
% this idea.

% New internal constant for integer register |\partokencontext|
@x
@d web2c_int_pars=web2c_int_base+4 {total number of web2c's integer parameters}
@y
@d partoken_context_code=web2c_int_base+4 {controlling where |partoken| inserted}
@d web2c_int_pars=web2c_int_base+5 {total number of web2c's integer parameters}
@z

% Allocation of integer register |\partokencontext|
@x
@d tracing_stack_levels==int_par(tracing_stack_levels_code)
@y
@d tracing_stack_levels==int_par(tracing_stack_levels_code)
@d partoken_context==int_par(partoken_context_code)
@z

% Printing |\show\partokencontext|
@x
tracing_stack_levels_code:print_esc("tracingstacklevels");
@y
tracing_stack_levels_code:print_esc("tracingstacklevels");
partoken_context_code:print_esc("partokencontext");
@z

% Declaration of primitives |\partokenname| and |\partokencontext|
@x
  primitive("tracingstacklevels",assign_int,int_base+tracing_stack_levels_code);@/
@!@:tracing_stack_levels_}{\.{\\tracingstacklevels} primitive@>
@y
  primitive("tracingstacklevels",assign_int,int_base+tracing_stack_levels_code);@/
@!@:tracing_stack_levels_}{\.{\\tracingstacklevels} primitive@>
  primitive("partokenname",partoken_name,0);@/
@!@:partoken_name_}{\.{\\partokenname} primitive@>
  primitive("partokencontext",assign_int,int_base+partoken_context_code);@/
@!@:partoken_context_}{\.{\\partokencontext} primitive@>
@z

% Printing |\show\partokenname|
@x
vrule: print_esc("vrule");
@y
vrule: print_esc("vrule");
partoken_name: print_esc("partokenname");
@z

% End of |\vbox| and |\vtop|
@x
vbox_group: begin end_graf; package(0);
  end;
vtop_group: begin end_graf; package(vtop_code);
  end;
@y
vbox_group: if (partoken_context>0) and (mode=hmode) then 
  begin
     back_input; cur_tok := par_token; back_input; token_type := inserted;
  end
  else  begin end_graf; package(0); end;
vtop_group: if (partoken_context>0) and (mode=hmode) then
  begin
     back_input; cur_tok := par_token; back_input; token_type := inserted;
  end 
  else begin end_graf; package(vtop_code); end;
@z

% End of |\insert| or |\vadjust|
@x
insert_group: begin end_graf; q:=split_top_skip; add_glue_ref(q);
@y
insert_group: if (partoken_context>1) and (mode=hmode) then
  begin
     back_input; cur_tok := par_token; back_input; token_type := inserted;
  end 
  else begin end_graf; q:=split_top_skip; add_glue_ref(q);
@z

% End of |\output|
@x
output_group: @<Resume the page builder...@>;
@y
output_group: if (partoken_context>1) and (mode=hmode) then
  begin
     back_input; cur_tok := par_token; back_input; token_type := inserted;
  end 
  else @<Resume the page builder...@>;
@z

% End of item in |\valign|
@x
vmode+endv,hmode+endv: do_endv;
@y
vmode+endv,hmode+endv: if (partoken_context>1) and (mode=hmode) then
  begin
     back_input; cur_tok := par_token; back_input; token_type := inserted;
  end
  else do_endv;
@z

% End of |\noalign| in |\halign|
@x
no_align_group: begin end_graf; unsave; align_peek;
  end;
@y
no_align_group: if (partoken_context>1) and (mode=hmode) then
  begin
     back_input; cur_tok := par_token; back_input; token_type := inserted;
  end 
  else begin end_graf; unsave; align_peek;
  end;
@z

% End of |\vcenter|
@x
vcenter_group: begin end_graf; unsave; save_ptr:=save_ptr-2;
@y
vcenter_group: if (partoken_context>0) and (mode=hmode) then 
  begin
     back_input; cur_tok := par_token; back_input; token_type := inserted;
  end else begin end_graf; unsave; save_ptr:=save_ptr-2;
@z

% Setting new name of the |par-token|
@x
any_mode(after_group):begin get_token; save_for_after(cur_tok);
  end;
@y
any_mode(after_group):begin get_token; save_for_after(cur_tok);
  end;
any_mode(partoken_name):begin get_token;
  if cur_cs > 0 then begin
     par_loc := cur_cs; par_token := cur_tok;
     end; 
  end;
@z

% Undump |par-token| from the format
@x
undump(hash_base)(hash_top)(par_loc);
@y
undump_int(par_loc);
@z