-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodependent.sty
More file actions
7160 lines (6677 loc) · 251 KB
/
codependent.sty
File metadata and controls
7160 lines (6677 loc) · 251 KB
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% ===================================================================
%% Section 1: Identification
%% ===================================================================
\NeedsTeXFormat{LaTeX2e}[2021/06/01]
\ProvidesPackage{codependent}[2026/04/10 v1.0 %
Pavlov-style atom numbering and back-reference display]
%% ===================================================================
%% Section 2: Required packages
%% ===================================================================
\RequirePackage{etoolbox}
\RequirePackage{pgfkeys}
%% ===================================================================
%% Section 3: Options
%% ===================================================================
% Depth: how many sectioning levels in the atom display number.
%% @utility
\newcommand*{\codep@depth}{1}
% Back-references: inline (default), appendix, or none.
%% @utility
\newcommand*{\codep@backrefmode}{inline}
\newbool{codep@backrefs}
\booltrue{codep@backrefs}
\newbool{codep@appendix}
\boolfalse{codep@appendix}
% Proofs: on (default) or off.
\newbool{codep@proofsnumbered}
\booltrue{codep@proofsnumbered}
% Proof warnings: on (default) or off.
% Controls the "proof not adjacent to tracked environment" warning.
\newbool{codep@proofwarnings}
\booltrue{codep@proofwarnings}
% Auto-attribution conflict warnings: on (default) or off.
% Controls the D3 warning when a proof heading overrides adjacency.
\newbool{codep@warnattributionconflicts}
\booltrue{codep@warnattributionconflicts}
% Concept warnings: on (default) or off.
\newbool{codep@conceptwarnings}
\booltrue{codep@conceptwarnings}
% Paragraphs: on (default) or off.
% When off, bare paragraphs get no atom numbers, no SBL records, no backrefs.
\newbool{codep@paragraphs}
\booltrue{codep@paragraphs}
% Equation tracking mode: outer (default), all, off.
%% @utility
\newcommand*{\codep@equationmode}{outer}
% Rendering configuration (backref style, fonts).
%% @utility
\newcommand*{\codep@backrefstyle}{inline}
% String constants for \ifx comparisons in rendering macros.
%% @utility
\def\codep@const@inline{inline}
%% @utility
\def\codep@const@off{off}
%% @utility
\def\codep@const@outer{outer}
%% @utility
\def\codep@const@all{all}
%% @utility
\newcommand*{\codep@backreffont}{\scriptsize\sffamily}
%% @utility
\newcommand*{\codep@backrefcolor}{}
%% @utility
\newcommand*{\codep@backrefprefix}{}
%% @utility
\newcommand*{\codep@backreflabel}{Used in}
%% @utility
\newcommand*{\codep@marginfont}{\scriptsize}
%% @utility
\edef\codep@appendix@pagenum@template{%
\detokenize{##1~(p.\@~##2)}}%
%% @utility
\def\codep@setappendixpagenumformat#1{%
\toks@{#1}%
\edef\codep@appendix@pagenum@template{%
\detokenize\expandafter{\the\toks@}}%
}%
%% @utility
\def\codep@renderappendixpagenumtemplate#1#2{%
\begingroup%
\endlinechar=-1\relax%
\edef\codep@tmp@appendixpagenum{%
\endgroup%
\noexpand\makeatletter%
\noexpand\scantokens{%
\def\noexpand\codep@tmp@appendixpagenum@stored{%
\codep@appendix@pagenum@template}}%
\noexpand\makeatother%
}%
\codep@tmp@appendixpagenum%
\edef\codep@tmp@appendixpagenum{%
\def\noexpand\codep@tmp@appendixpagenum@apply####1####2{%
\codep@tmp@appendixpagenum@stored}}%
\codep@tmp@appendixpagenum%
\codep@tmp@appendixpagenum@apply{#1}{#2}%
}%
\pgfkeys{%
/codep/.is family,%
/codep/.cd,%
depth/.store in = \codep@depth,%
depth/.default = 1,%
backrefs/.is choice,%
backrefs/inline/.code = {%
\booltrue{codep@backrefs}%
\boolfalse{codep@appendix}%
\renewcommand*{\codep@backrefmode}{inline}%
},%
backrefs/appendix/.code = {%
\booltrue{codep@backrefs}%
\booltrue{codep@appendix}%
\renewcommand*{\codep@backrefmode}{appendix}%
},%
backrefs/none/.code = {%
\boolfalse{codep@backrefs}%
\boolfalse{codep@appendix}%
\renewcommand*{\codep@backrefmode}{none}%
},%
backrefs/.default = inline,%
proofs/.is choice,%
proofs/on/.code = {\booltrue{codep@proofsnumbered}},%
proofs/off/.code = {\boolfalse{codep@proofsnumbered}},%
proofs/.default = on,%
proof-warnings/.is choice,%
proof-warnings/on/.code = {\booltrue{codep@proofwarnings}},%
proof-warnings/off/.code = {\boolfalse{codep@proofwarnings}},%
proof-warnings/.default = on,%
% see @behavior B-WARN-ATTR-CONFLICTS at
% \codep@proof@maybeheadingoverrideadjacent
warn-attribution-conflicts/.is choice,%
warn-attribution-conflicts/true/.code = {%
\booltrue{codep@warnattributionconflicts}},%
warn-attribution-conflicts/false/.code = {%
\boolfalse{codep@warnattributionconflicts}},%
warn-attribution-conflicts/on/.code = {%
\booltrue{codep@warnattributionconflicts}},%
warn-attribution-conflicts/off/.code = {%
\boolfalse{codep@warnattributionconflicts}},%
warn-attribution-conflicts/.default = true,%
conceptwarnings/.is choice,%
conceptwarnings/on/.code = {\booltrue{codep@conceptwarnings}},%
conceptwarnings/off/.code = {\boolfalse{codep@conceptwarnings}},%
conceptwarnings/.default = on,%
paragraphs/.is choice,%
paragraphs/on/.code = {\booltrue{codep@paragraphs}},%
paragraphs/off/.code = {\boolfalse{codep@paragraphs}},%
paragraphs/.default = on,%
equations/.is choice,%
equations/all/.code = {\def\codep@equationmode{all}},%
equations/outer/.code = {\def\codep@equationmode{outer}},%
equations/off/.code = {\def\codep@equationmode{off}},%
equations/.default = outer,%
backref-style/.is choice,%
backref-style/below/.code = {%
\PackageError{codependent}{%
backref-style=below|margin removed in v2.0 --- use %
\string\codepbackrefs\space in a custom %
\string\newtheoremstyle\space endmark %
(see docs/COOKBOOK.md)}{%
The below and margin render styles were replaced by %
\string\codepbackrefs, a user-driven hook that places the %
backref wherever the theorem style prefers.}%
},%
backref-style/inline/.code = {\renewcommand*{\codep@backrefstyle}{inline}},%
backref-style/margin/.code = {%
\PackageError{codependent}{%
backref-style=below|margin removed in v2.0 --- use %
\string\codepbackrefs\space in a custom %
\string\newtheoremstyle\space endmark %
(see docs/COOKBOOK.md)}{%
The below and margin render styles were replaced by %
\string\codepbackrefs, a user-driven hook that places the %
backref wherever the theorem style prefers.}%
},%
backref-style/.default = inline,%
backref-font/.store in = \codep@backreffont,%
backref-color/.store in = \codep@backrefcolor,%
backref-prefix/.store in = \codep@backrefprefix,%
backref-label/.store in = \codep@backreflabel,%
appendix/pagenum-format/.code = {%
\codep@setappendixpagenumformat{#1}%
},%
appendix-display/.code={\pgfkeys{/codep/appendix-display/.cd,#1}},%
margin-font/.store in = \codep@marginfont,%
}
% Collect all options and forward to pgfkeys.
%% @utility
\def\codep@rawopts{}%
\DeclareOption*{%
\ifx\codep@rawopts\@empty
\edef\codep@rawopts{\CurrentOption}%
\else
\edef\codep@rawopts{\codep@rawopts,\CurrentOption}%
\fi
}%
\ProcessOptions\relax
\ifx\codep@rawopts\@empty\else
%% @utility
\edef\codep@tmp@opts{%
\noexpand\pgfkeys{/codep/.cd,\codep@rawopts}%
}%
\codep@tmp@opts
\fi
% \codepsetup{key=value,...} Configure codependent in preamble or body.
%% @utility
\newcommand*{\codepsetup}[1]{\pgfkeys{/codep/.cd,#1}}
%% ===================================================================
%% Section 4: Internal state
%% ===================================================================
% Base counter name: auto-detected from the first env in \codeptrack.
%% @utility
\newcommand*{\codep@basecounter}{}
% Monotone internal source-atom IDs (a1, a2, ...).
\newcount\codep@atomid
\codep@atomid=0\relax
% Monotone internal target IDs (q1, q2, ...).
\newcount\codep@targetid
\codep@targetid=0\relax
% Source-context stack depth.
\newcount\codep@ctxdepth
\codep@ctxdepth=0\relax
% Paragraph suppression depth.
\newcount\codep@suppressdepth
\codep@suppressdepth=0\relax
\newcount\codep@sourceblockdepth
\codep@sourceblockdepth=0\relax
% Tracked theorem-like nesting depth.
\newcount\codep@trackeddepth
\codep@trackeddepth=0\relax
% Restatable replay depth.
\newcount\codep@replaydepth
\codep@replaydepth=0\relax
% Flag: true when a paragraph atom is currently open.
\newif\ifcodep@paragraphopen
\codep@paragraphopenfalse
% Flag: true when the current proof still lacks display identity.
\newif\ifcodep@proofdisplaypending
\codep@proofdisplaypendingfalse
% Flag: true only while the current proof heading may auto-bind itself.
\newif\ifcodep@autoproofof@eligible
\codep@autoproofof@eligiblefalse
% Scratch count for proof-heading reference auto-detection.
\newcount\codep@autoproofof@refcount
\codep@autoproofof@refcount=0\relax
% Document-scoped counter for unresolved-reference identities.
\newcount\codep@autoproofof@unresrefcount
\codep@autoproofof@unresrefcount=0\relax
% Flag: true when the last proof-of resolver call bound a target.
\newif\ifcodep@proofof@resolved
\codep@proofof@resolvedfalse
% Flag: true when the current proof has a staged adjacent candidate
% waiting for post-heading-scan commitment.
%% @utility
\newif\ifcodep@proof@adjcommit@needed
\codep@proof@adjcommit@neededfalse
% Per-proof count of successful explicit/auto proof-attribution
% resolutions. Stored in csname family
% \codep@proofof@attribcount@<proof-id> so repeated explicit
% attributions in one proof can be treated as intentional joint
% attribution rather than a key rename.
% Flag: abort finalization — set by \codep@enddoc@evaluator@pre when
% a fatal enddoc state is detected (e.g. unsettled proof-binding slot,
% or a subsystem counter reporting incomplete work we cannot recover
% from). The orchestrator branches on this flag: when true, drains
% are skipped and \codep@enddoc@halt fires \PackageError.
% Runtime-only; NOT written to .aux, so a restart-retry begins clean.
%% @utility
\newif\ifcodep@abortfinalize
\codep@abortfinalizefalse
% Subsystem counter registry — walked by the two-tier evaluator.
% Each subsystem tracks a (total, failed) pair: `total` increments
% every time a tracked operation is attempted at non-replay time;
% `failed` increments when an operation cannot complete. The
% orchestrator's pre-drain evaluator flips \codep@abortfinalizetrue
% when any registered subsystem has failed==total && total>0 (i.e.
% EVERY operation failed — drains would emit only stale records).
% The post-drain evaluator emits \PackageWarning per subsystem with
% 0<failed<total (partial success).
%% @utility
\newcommand*{\codep@subsystems@list}{}
% \codep@subsystem@init{name}
% Declare a subsystem counter pair and register it on the
% subsystems list. Called four times at package load (see
% Section 9b).
%% @utility
%@effect state-only
\def\codep@subsystem@init#1{%
\expandafter\newcount\csname codep@subsys@#1@total\endcsname%
\csname codep@subsys@#1@total\endcsname=0\relax%
\expandafter\newcount\csname codep@subsys@#1@failed\endcsname%
\csname codep@subsys@#1@failed\endcsname=0\relax%
\g@addto@macro\codep@subsystems@list{\do{#1}}%
}%
% \codep@subsystem@recordtotal{name}
% Increment the total-attempts counter for subsystem <name>.
% Guarded on \codep@replaydepth so restatable theorems do not
% double-count. This guard is authoritative — callsites
% should NOT double-wrap in a replay-depth check.
%% @utility
\def\codep@subsystem@recordtotal#1{%
\ifnum\codep@replaydepth=0\relax%
\global\advance\csname codep@subsys@#1@total\endcsname by 1\relax%
\fi%
}%
% \codep@subsystem@recordfailed{name}
% Increment the failed-operations counter for subsystem <name>.
% Same replay guard as recordtotal.
%% @utility
\def\codep@subsystem@recordfailed#1{%
\ifnum\codep@replaydepth=0\relax%
\global\advance\csname codep@subsys@#1@failed\endcsname by 1\relax%
\fi%
}%
% Four subsystem registrations. `proofof` tracks \codepproofof
% resolution attempts (recordfailed on slot-overwrite + unsettled
% slot at enddoc). `atomref` tracks \codep@writerefevent calls.
% `label-bind` tracks \codep@recordlabelowner calls. `backref-render`
% tracks render-layer preparebackref calls (no-op count when render
% layer is absent, since the no-op stub in codependent.sty:342
% does not increment).
\codep@subsystem@init{proofof}%
\codep@subsystem@init{atomref}%
\codep@subsystem@init{label-bind}%
\codep@subsystem@init{backref-render}%
% Pending proof-adjacent result atom ID.
%% @utility
\newcommand*{\codep@pendingresultid}{}
% Current proof atom ID (empty outside proofs).
%% @utility
\newcommand*{\codep@currentproofid}{}
% Labels captured by the proof-heading auto-detect scanner.
%% @utility
\newcommand*{\codep@autoproofof@pendinglist}{}
% Same-scan provisional source key for unresolved proof-heading refs.
%% @utility
\newcommand*{\codep@autoproofof@provkey}{}
% Proof target metadata accumulator keys. The authoritative per-proof
% payload is stored in csnames keyed by the stable proof:a<N> identity;
% this do-list only remembers which proof keys need aux / cdp emission.
%% @utility
\gdef\codep@prooftargetmetalist{}
% Saved nested-proof state.
\newtoks\codep@proofneststack
\codep@proofneststack={}
% Deferred proof-binding marker. Non-empty when a proof-binding
% declaration (e.g., \codepproofin — planned future API) was made
% but the proof environment has not yet begun to consume it.
% Cleared at every \let\@empty site; the sibling slots below
% carry the contextual details needed by the overwrite and
% unsettled-slot warnings.
%% @utility
\newcommand*{\codep@pendingproofbind}{}
% Sibling capture slots for the pending proof-binding. Populated
% alongside \codep@pendingproofbind whenever a binding is
% declared; used by \codep@pendingproofbind@checkoverwrite and by
% \codep@enddoc@drain@proofs when warning about an unsettled
% slot. Kept empty when \codep@pendingproofbind is empty.
%% @utility
\newcommand*{\codep@pendingproofbind@line}{}
%% @utility
\newcommand*{\codep@pendingproofbind@label}{}
% \codep@pendingproofbind@checkoverwrite
% Called immediately before any site that \let\@empty-clears
% \codep@pendingproofbind outside the save/restore pair. If
% the slot is non-empty at check time, emits one
% \PackageWarning + \codep@subsystem@recordfailed{proofof} and
% clears the sibling slots; the caller then performs its own
% \let\@empty on the main slot. Safe to call when the slot is
% already empty (no-op).
%% @utility
\def\codep@pendingproofbind@checkoverwrite{%
\ifx\codep@pendingproofbind\@empty\else%
\codep@subsystem@recordfailed{proofof}%
\PackageWarning{codependent}{proof-bind slot overwritten -- previous binding to '\codep@pendingproofbind@label' at input line \codep@pendingproofbind@line\space was lost}%
\global\let\codep@pendingproofbind@line\@empty%
\global\let\codep@pendingproofbind@label\@empty%
\fi%
}%
% Saved atom number for the outermost tracked env. When nesting
% occurs, \theatom at end-time reflects the LAST \refstepcounter
% (the inner env), not the outermost. This macro captures the
% correct value after the outermost env's \refstepcounter fires.
%% @utility
\newcommand*{\codep@outeratom}{}
% Pending-clear stashes for the per-atom \codep@manuallyemitted@<key>
% flag. Populated at the source hook site (where the atom key is
% still resolvable), consumed at the *post-env* hook site (after the
% ctx stack pop). See Section 6 (\codep@hooktheorem orchestrator)
% and Section 6 (\codep@hookproof@afterend).
%% @utility
\newcommand*{\codep@endenv@pendingclear}{}
%% @utility
\newcommand*{\codep@proofend@pendingclear}{}
% Nested-proof save/restore stashes for adjacent-proof state.
%% @utility
\newcommand*{\codep@proofadj@pendinganchormapkey}{}
%% @utility
\newcommand*{\codep@proofadj@pendinganchormapanchor}{}
% Flag: true when \codeptrack has been called.
\newbool{codep@tracked}
\boolfalse{codep@tracked}
% Flag: true when a sectioning heading paragraph is pending.
% Set by the \@startsection wrapper, cleared after one para/begin.
\newbool{codep@sectioning}
\boolfalse{codep@sectioning}
% Flag: true inside a tracked equation environment.
\newbool{codep@inequation}
\boolfalse{codep@inequation}
% Track 1 / Track 2 source-ownership state for equations.
%% @utility
\newcommand*{\codep@currenteqsourceid}{}
%% @utility
\newcommand*{\codep@eqfallbacksourceid}{}
% Track 1: current equation atom (set at \ref time for single-number envs).
%% @utility
\newcommand*{\codep@eqatom}{}
% Track 2: pending ref targets for multi-number envs.
\newtoks\codep@eqpending
\codep@eqpending={}
\newcount\codep@eqpendingcount
\codep@eqpendingcount=0\relax
% Track 2: pending-append guard cleanup tokens (SW5c commit 2).
% Cleared at each equation-block begin to reset \codep@arw@pending@<tgt>.
\newtoks\codep@eqpendguards
\codep@eqpendguards={}
% Track 2: saved equation counter at \begin{align}.
\newcount\codep@eq@startcount
\codep@eq@startcount=0\relax
% Track 2: dedicated anchor name for the block.
%% @utility
\newcommand*{\codep@eq@anchor}{}
% Track 2 flag: distinguishes Track 1 vs Track 2 envs.
\newbool{codep@eqmultiline}
\boolfalse{codep@eqmultiline}
% Flag: true when the current equation block produced a number.
\newbool{codep@eqnumbered}
\boolfalse{codep@eqnumbered}
% Standalone-equation label type pending.
% Set globally at \AtBeginEnvironment for standalone equations so
% that deferred \label calls (amsmath fires them after environment end)
% can still record the correct type. Cleared in \codep@writelbltype
% after use, and globally cleared at environment end as a safety net.
%% @utility
\newcommand*{\codep@eq@lbltype@pending}{}
% Accumulated appendix data (token register).
% Still referenced by \codepappendix (Section 10); Wave 3 rewires
% its population path per §8a.6.g.
\newtoks\codep@appendixdata
% Count-indexed atom array for appendix mode (§8a.6.g).
% Each \codep@atom@<n> expands to {<num>}{<type>}.
% O(1) append via csname array; O(N) walk in \codepappendix.
\newcount\codep@atomcount
% Current section number and title for appendix section grouping.
% Updated by the \addcontentsline hook when a toc/section entry
% is written. Stored per-atom at registration time so that
% \codepappendix can emit section headers.
%% @utility
\newcommand*{\codep@cursecnum}{}%
%% @utility
\newcommand*{\codep@cursectitle}{}%
% Sidecar (.cdp) stream state (§9a).
\newbool{codep@cdpopen}
\boolfalse{codep@cdpopen}
% Deferred sbl writes: preamble cmddef records accumulate here
% and flush once the .cdp stream opens at begindocument/before.
%% @utility
\def\codep@cdp@deferred{}
%% --- Rendering hook points (no-op defaults) ---
%% These are redefined to their live implementations after all
%% rendering code is loaded (see "Rendering hook activation" below).
%% When tracking runs without a rendering layer, these are inert.
%% @utility
\def\codep@render@refresh#1{}%
%% @utility
\def\codep@render@flushbackref#1{}%
%% @utility
\def\codep@render@thm@preparebackref#1{}%
%% @utility
\def\codep@render@thm@afterend#1{}%
%% @utility
\def\codep@render@proof@end{}%
%% @utility
\def\codep@render@proof@afterend{}%
%% @utility
\def\codep@render@para@emit#1#2{}%
%% @utility
\def\codep@render@manual@emit#1{}%
% \codep@manuallyemitted@clear@key{atomkey}
% Globally undefine \codep@manuallyemitted@<atomkey>. The csname
% form must use \@undefined (not \relax) so that subsequent
% \ifcsdef tests at auto-flush sites correctly return false.
% Mirrors the undef idioms in \codep@ctxpop and
% \codep@proofstaterestore.
%% @utility
\def\codep@manuallyemitted@clear@key#1{%
\csgundef{codep@manuallyemitted@#1}%
}%
%% ===================================================================
%% Section 4a: Runtime helpers
%% ===================================================================
% Proof atoms are identified by their opaque key (proof:a<N>) only;
% see @behavior B-PROOF-OPAQUE-ID at \codep@proof@identitykey.
% there is no display-keyed alias. \hyperref[proof:1.2]{...} is NOT
% supported -- users must reference proof atoms by their proof:a<N>
% key (extractable from the .cdp file).
%
% A generic display->atom sidecar rail will be added in Wave E
% (sub-item E1) to support display-keyed reverse lookup for joint
% proofs, per-result ordinals, continued theorems, and cross-doc.
%% @utility
% \codep@allocatomid{macro}{kind}
% Allocate a fresh opaque source-atom ID (aN) into macro.
\def\codep@allocatomid#1#2{%
\global\advance\codep@atomid by 1\relax
\xdef#1{a\the\codep@atomid}%
\csxdef{codep@atomkind@#1}{#2}%
}%
%% @utility
% \codep@alloctargetid{macro}{kind}
% Allocate a fresh opaque target ID (qN) into macro.
\def\codep@alloctargetid#1#2{%
\global\advance\codep@targetid by 1\relax
\xdef#1{q\the\codep@targetid}%
\csxdef{codep@targetkind@#1}{#2}%
}%
%% @utility
% \codep@ctxpush{atom-id}{kind}
% Push one source context entry onto the global stack.
\def\codep@ctxpush#1#2{%
\global\advance\codep@ctxdepth by 1\relax%
\csxdef{codep@ctxid@\the\codep@ctxdepth}{#1}%
\csxdef{codep@ctxkind@\the\codep@ctxdepth}{#2}%
}%
%% @utility
% \codep@ctxpop{kind}
% Pop one source context entry, hard-erroring on mismatch.
\def\codep@ctxpop#1{%
\ifnum\codep@ctxdepth<1\relax%
\PackageError{codependent}{Context stack underflow on '#1'}{%
codependent detected an internal stack underflow while%
closing '#1'.}%
\else%
\edef\codep@tmp@ctxkind{%
\csuse{codep@ctxkind@\the\codep@ctxdepth}}%
\def\codep@tmp@expected{#1}%
\ifx\codep@tmp@ctxkind\codep@tmp@expected\else%
\PackageError{codependent}{Context stack mismatch on '#1'}{%
Expected to pop '#1' but found '\codep@tmp@ctxkind'.}%
\fi%
\csgundef{codep@ctxid@\the\codep@ctxdepth}%
\csgundef{codep@ctxkind@\the\codep@ctxdepth}%
\global\advance\codep@ctxdepth by -1\relax%
\fi%
}%
%% @utility
% \codep@ctxpeekid{macro}
% Store the top source-context ID into macro, else \@empty.
\def\codep@ctxpeekid#1{%
\ifnum\codep@ctxdepth<1\relax%
\let#1\@empty%
\else%
\letcs#1{codep@ctxid@\the\codep@ctxdepth}%
\fi%
}%
%% @utility
% \codep@ctxpeekkind{macro}
% Store the top source-context kind into macro, else \@empty.
\def\codep@ctxpeekkind#1{%
\ifnum\codep@ctxdepth<1\relax%
\let#1\@empty%
\else%
\letcs#1{codep@ctxkind@\the\codep@ctxdepth}%
\fi%
}%
%% @utility
% \codep@atomsetkey{atom-id}{prefixed-key}
% Persist the legacy prefixed key derived for this atom.
\def\codep@atomsetkey#1#2{%
\csxdef{codep@atomkey@#1}{#2}%
\csxdef{codep@idbykey@#2}{#1}%
\expandafter\codep@splitprefix#2\@nil%
\csxdef{codep@atomdisplay@#1}{%
\codep@tmp@num}%
}%
%% @utility
% \codep@atomgetkey{atom-id}{macro}
% Store the legacy prefixed key for atom-id into macro.
\def\codep@atomgetkey#1#2{%
\ifx#1\@empty%
\let#2\@empty%
\else%
\ifcsundef{codep@atomkey@#1}{%
\let#2\@empty%
}{%
\letcs#2{codep@atomkey@#1}%
}%
\fi%
}%
%% @utility
% \codep@atomgetdisplay{atom-id}{macro}
% Store the bare legacy display number for atom-id into macro.
\def\codep@atomgetdisplay#1#2{%
\ifx#1\@empty%
\let#2\@empty%
\else%
\ifcsundef{codep@atomdisplay@#1}{%
\let#2\@empty%
}{%
\letcs#2{codep@atomdisplay@#1}%
}%
\fi%
}%
%% @utility
% \codep@atomgetkind{atom-id}{macro}
% Store the registered kind for atom-id into macro.
\def\codep@atomgetkind#1#2{%
\ifx#1\@empty%
\let#2\@empty%
\else%
\ifcsundef{codep@atomkind@#1}{%
\let#2\@empty%
}{%
\letcs#2{codep@atomkind@#1}%
}%
\fi%
}%
%% @utility
% \codep@keytoid{prefixed-key}{macro}
% Resolve a legacy prefixed key back to an internal atom ID.
\def\codep@keytoid#1#2{%
\ifcsundef{codep@idbykey@#1}{%
\let#2\@empty%
}{%
\letcs#2{codep@idbykey@#1}%
}%
}%
%% @implements \codep@writerefevent
% \codep@getcurrentownerid{macro}
% Resolve the active owning atom ID, honouring proof pending state.
\def\codep@getcurrentownerid#1{%
\ifnum\codep@replaydepth>0\relax%
\let#1\@empty%
\else%
\codep@ctxpeekkind\codep@tmp@ctxkind%
\codep@ctxpeekid\codep@tmp@ctxid%
\def\codep@tmp@proofkind{proof}%
\ifx\codep@tmp@ctxkind\@empty%
\let#1\@empty%
\else\ifx\codep@tmp@ctxkind\codep@tmp@proofkind%
\ifcodep@proofdisplaypending%
\let#1\@empty%
\else%
\let#1\codep@tmp@ctxid%
\fi%
\else%
\let#1\codep@tmp@ctxid%
\fi\fi%
\fi%
}%
%% @implements \codep@writerefevent
% \codep@getcurrentatomkey{macro}
% Resolve the active source atom's legacy prefixed key.
\def\codep@getcurrentatomkey#1{%
\ifnum\codep@sourceblockdepth>0\relax%
\let#1\@empty%
\else%
\codep@getcurrentownerid\codep@tmp@ownerid%
\codep@atomgetkey{\codep@tmp@ownerid}{#1}%
\fi%
}%
%% @implements \codep@recordlabelowner
% \codep@getcurrentatomdisplay{macro}
% Resolve the active source atom's bare legacy display number.
\def\codep@getcurrentatomdisplay#1{%
\codep@getcurrentatomkey\codep@tmp@atomkey%
\ifx\codep@tmp@atomkey\@empty%
\let#1\@empty%
\else%
\expandafter\codep@splitprefix\codep@tmp@atomkey\@nil%
\let#1\codep@tmp@num%
\fi%
}%
%% @implements \codep@recordlabelowner
% \codep@getcurrentatomkind{macro}
% Resolve the active source atom's legacy type prefix.
\def\codep@getcurrentatomkind#1{%
\codep@getcurrentatomkey\codep@tmp@atomkey%
\ifx\codep@tmp@atomkey\@empty%
\let#1\@empty%
\else%
\expandafter\codep@splitprefix\codep@tmp@atomkey\@nil%
\let#1\codep@tmp@type%
\fi%
}%
%% @behavior B-NUM-PARA
% \codep@closeparagraphifopen
% Finalise the current paragraph atom, if any.
\def\codep@closeparagraphifopen{%
\ifcodep@paragraphopen%
\codep@ctxpop{paragraph}%
\global\codep@paragraphopenfalse%
\fi%
}%
%% @behavior B-NUM-EQINDEP
% \codep@recordlabelowner{label}
% Write the legacy label-type ownership record for label, and
% (W03-P03) populate the in-memory label->entity binding table +
% emit the \codep@lblentity aux record. Same replay guard covers
% both.
%@effect immediate-write
\def\codep@recordlabelowner#1{%
\if@filesw%
\ifnum\codep@replaydepth>0\relax%
\else%
\codep@subsystem@recordtotal{label-bind}%
\ifx\codep@eq@lbltype@pending\@empty%
\codep@getcurrentatomkind\codep@tmp@labelkind%
\ifx\codep@tmp@labelkind\@empty\else%
\csgdef{codep@ltype@#1}{%
\codep@tmp@labelkind}%
\protected@write\@auxout{}{%
\string\codep@lbltype{#1}{\codep@tmp@labelkind}}%
\fi%
\else%
\csgdef{codep@ltype@#1}{%
\codep@eq@lbltype@pending}%
\protected@write\@auxout{}{%
\string\codep@lbltype{#1}{\codep@eq@lbltype@pending}}%
\fi%
% W03-P03: in-memory label->entity binding + cross-run aux record.
% Skip if no owning atom-key is resolvable at this point (e.g.,
% \label outside any tracked env, or equation label before the
% equation's atom-key is materialised).
\codep@getcurrentatomkey\codep@tmp@lblentity%
\ifx\codep@tmp@lblentity\@empty\else%
% \xdef (not \gdef) so the csname stores the expanded
% atom-key string, not a reference to \codep@tmp@lblentity
% whose binding is local to this \codep@recordlabelowner
% call and is undefined outside.
\csxdef{codep@lblentity@#1}{%
\codep@tmp@lblentity}%
\csgdef{codep@lblentity@samerun@#1}{1}%
\protected@write\@auxout{}{%
\string\codep@lblentity{#1}{\codep@tmp@lblentity}}%
\fi%
\fi%
\fi%
}%
%% @behavior B-DEDUP
%% @behavior B-EDGE-HEADING
% \codep@writerefevent{label}
% Emit one legacy atomref write from the active source atom.
%@effect immediate-write
\def\codep@writerefevent#1{%
\if@filesw%
\codep@autoproofof@ifheadsuppressed{#1}{}{%
\codep@proof@ifadjacenttargetsuppressed{#1}{}{%
\codep@getcurrentatomkey\codep@tmp@srcatom%
\ifx\codep@tmp@srcatom\@empty\else%
\codep@subsystem@recordtotal{atomref}%
\codep@dedupwrite{\codep@tmp@srcatom}{#1}%
\fi%
}%
}%
\fi%
}%
% \codep@proof@identitykey{proof-id}{macro}
% Return the stable proof:a<N> source key for proof-id.
%% @behavior B-PROOF-OPAQUE-ID
\def\codep@proof@identitykey#1#2{%
\edef#2{proof:#1}%
}%
% \codep@proof@setidentity{proof-id}
% Make proof:a<N> the current atom key for proof-id.
%% @implements \codep@bindproofparent
\def\codep@proof@setidentity#1{%
\codep@proof@identitykey{#1}{\codep@tmp@proofidentitykey}%
\codep@atomsetkey{#1}{\codep@tmp@proofidentitykey}%
}%
% \codep@proof@markparent{proof-id}{target-key}
% Remember that proof-id has at least one target parent.
%% @implements \codep@bindproofparent
\def\codep@proof@markparent#1#2{%
\ifcsundef{codep@proofparent@#1}{%
\csxdef{codep@proofparent@#1}{#2}%
}{%
\ifcsempty{codep@proofparent@#1}{%
\csxdef{codep@proofparent@#1}{#2}%
}{}%
}%
}%
% \codep@proof@maybeheadingoverrideadjacent{proof-id}{target-key}{display}
% D3 policy: first tracked heading target supersedes adjacent binding.
%% @behavior B-PROOF-HEADING-OVERRIDE
%% @behavior B-WARN-ATTR-CONFLICTS
\def\codep@proof@maybeheadingoverrideadjacent#1#2#3{%
\ifcsundef{codep@proofadjtarget@#1}{}{%
\ifcsundef{codep@proofheadingoverride@#1}{%
\edef\codep@tmp@adjtarget{\csuse{codep@proofadjtarget@#1}}%
\edef\codep@tmp@headingtarget{#2}%
\ifx\codep@tmp@adjtarget\codep@tmp@headingtarget\else%
\ifbool{codep@warnattributionconflicts}{%
\PackageWarning{codependent}{auto-attribution to #3\space%
supersedes adjacent \csuse{codep@proofadjdisplay@#1}}%
}{}%
\codep@proof@suppressadjacenttarget{#1}{\codep@tmp@adjtarget}%
\fi%
\codep@proof@identitykey{#1}{\codep@tmp@overrideproofkey}%
\codep@prooftargetmeta@clear{\codep@tmp@overrideproofkey}%
\csxdef{codep@proofparent@#1}{}%
\global\cslet{codep@proofheadingoverride@#1}\@empty%
}{}%
}%
}%
% \codep@proof@suppressadjacenttarget{proof-id}{target-key}
% Record the adjacent target displaced by proof-heading attribution.
%% @implements \codep@bindproofparent
\def\codep@proof@suppressadjacenttarget#1#2{%
\csxdef{codep@proofsuppressadjacent@#1}{#2}%
}%
% \codep@proof@ifadjacenttargetsuppressed{label}{then}{else}
% Suppress later proof-body references to the displaced adjacent target.
%% @implements \codep@writerefevent
\def\codep@proof@ifadjacenttargetsuppressed#1#2#3{%
\ifx\codep@currentproofid\@empty%
#3%
\else%
\ifcsundef{%
codep@proofsuppressadjacent@%
\codep@currentproofid}{%
#3%
}{%
\ifcsundef{codep@lblentity@#1}{%
#3%
}{%
\edef\codep@tmp@suppressadjacent{%
\csuse{codep@proofsuppressadjacent@%
\codep@currentproofid}}%
\edef\codep@tmp@suppresstarget{\csuse{codep@lblentity@#1}}%
\ifx\codep@tmp@suppressadjacent\codep@tmp@suppresstarget%
#2%
\else%
#3%
\fi%
}%
}%
\fi%
}%
%% @behavior B-PROOF-ADJ
%% @behavior B-PROOF-SEP
%% @behavior B-PROOF-JOINT
% \codep@bindproofparent{proof-id}{parent-id}{mode}
% Bind proof-id to parent-id using the stable proof:a<N> key and
% append parent-id to the proof target metadata list.
% The third argument (mode) is currently unused at the call site;
% the parent slot's emptiness alone drives the renderer's
% ADJACENT-or-REBOUND vs. STANDALONE classifier (see
% \codep@graph@getentityparent). Retained in the signature for
% call-site clarity.
% The parent slot stores the parent atom's prefixed key (e.g.
% "theorem:1.1") rather than the raw atom-id so the entitymeta
% parent value is consistent across adjacent and rebound paths
% (\codep@proofof@resolve also writes a prefixed key). The
% classifier still only checks emptiness.
\def\codep@bindproofparent#1#2#3{%
\codep@proof@setidentity{#1}%
\codep@proof@identitykey{#1}{\codep@tmp@proofkey}%
\codep@atomgetkey{#2}{\codep@tmp@parentprefixedkey}%
\codep@atomgetdisplay{#2}\codep@tmp@parentdisplay%
\ifx\codep@tmp@parentprefixedkey\@empty\else%
\codep@proof@markparent{#1}{\codep@tmp@parentprefixedkey}%
% see @behavior B-PROOF-JOINT at \codep@proofof@capturejointmeta.
\codep@prooftargetmeta@append{\codep@tmp@proofkey}{%
\codep@tmp@parentprefixedkey}{\codep@tmp@parentdisplay}{*}%
\fi%
}%
%% ===================================================================
%% Section 5: Core numbering machinery
%% ===================================================================
% \codep@removefromreset{counter}{parent}
% Remove counter from parent's reset list.
%% @utility
%@effect state-only
\newcommand*{\codep@removefromreset}[2]{%
\ifcsundef{@removefromreset}{%
% Manual: rewrite \cl@parent without #1.
\begingroup
\letcs\codep@resetlist{cl@#2}%
\global\cslet{cl@#2}\@empty%
\def\@elt##1{%
\def\codep@cur{##1}%
\def\codep@tgt{#1}%
\ifx\codep@cur\codep@tgt\else
\expandafter\g@addto@macro%
\csname cl@#2\endcsname{\@elt{##1}}%
\fi
}%
\codep@resetlist%
\endgroup
}{%
\@removefromreset{#1}{#2}%
}%
}
% \codep@setupcounter
% Create the atom counter and wire it to the base counter
% (auto-detected from the first env in \codeptrack).
% Called by \codeptrack.
%% @behavior B-NUM-SHARED