summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/zref-check/zref-check.dtx
blob: 78fe4c089e16ab92a43a00c1020a788a31678676 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
% \iffalse meta-comment
%
% File: zref-check.dtx
%
% This file is part of the LaTeX package "zref-check".
%
% Copyright (C) 2021-2024  gusbrs
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file:
%
%    https://www.latex-project.org/lppl.txt
%
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
%
% This work is "maintained" (as per LPPL maintenance status) by gusbrs.
%
% This work consists of the files zref-check.dtx,
%                                 zref-check.ins,
%                                 zref-check-doc.tex,
%                                 zref-check-code.tex,
%                   and the files generated from them.
%
% The released version of this package is available from CTAN.
%
% -----------------------------------------------------------------------
%
% The development version of the package can be found at
%
%    https://github.com/gusbrs/zref-check
%
% for those people who are interested.
%
% -----------------------------------------------------------------------
%
% \fi
%
% \iffalse
%<*driver>
\documentclass{l3doc}
% Have \GetFileInfo pick up date and version data
\usepackage{zref-check}
\NewDocumentCommand\opt{m}{\texttt{#1}}
\MakeShortVerb{\|}
\begin{document}
  \DocInput{zref-check.dtx}
\end{document}
%</driver>
% \fi
%
%
%
% \section{Initial setup}
%
% Start the \pkg{DocStrip} guards.
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% Identify the internal prefix (\LaTeX3 \pkg{DocStrip} convention).
%    \begin{macrocode}
%<@@=zrefcheck>
%    \end{macrocode}
%
%
% For the \texttt{chapter} and \texttt{section} checks, \pkg{zref-check} uses
% the new hook system in \pkg{ltcmdhooks}, which was released with the
% 2021/06/01 \LaTeX{} kernel.  And, since we followed the move to
% \texttt{e}-type expansion, to play safe we require the 2023-11-01 kernel or
% newer.
%    \begin{macrocode}
\def\zrefcheck@required@kernel{2023-11-01}
\NeedsTeXFormat{LaTeX2e}[\zrefcheck@required@kernel]
\providecommand\IfFormatAtLeastTF{\@ifl@t@r\fmtversion}
\IfFormatAtLeastTF{\zrefcheck@required@kernel}
  {}
  {%
    \PackageError{zref-check}{LaTeX kernel too old}
      {%
        'zref-check' requires a LaTeX kernel \zrefcheck@required@kernel\space or newer.%
      }%
  }%
%    \end{macrocode}
%
% Identify the package.
%    \begin{macrocode}
\ProvidesExplPackage {zref-check} {2024-11-28} {0.3.7}
  {Flexible cross-references with contextual checks based on zref}
%    \end{macrocode}
%
% \section{Dependencies}
%
%    \begin{macrocode}
\RequirePackage { zref-user }
\RequirePackage { zref-abspage }
\RequirePackage { ifdraft }
%    \end{macrocode}
%
%
% \section{\pkg{zref} setup}
%
% Provide absolute counters for section and chapter, and respective \pkg{zref}
% properties, so that we can make checks about relation of chapters/sections
% regardless of internal counters, since we don't get those for the unnumbered
% (starred) ones.  Thanks Ulrike Fischer for suggestions at TeX.SX about the
% proper place to make the hooks for this purpose.
%    \begin{macrocode}
\newcounter { zc@abschap }
\newcounter { zc@abssec } [ zc@abschap ]
%    \end{macrocode}
%
%
% If the documentclass does not define \cs{chapter} the only thing that
% happens is that the chapter counter is never incremented, and the section
% one never reset.
%    \begin{macrocode}
\AddToHook { cmd / chapter / before }
  { \stepcounter { zc@abschap } }
\zref@newprop { zc@abschap } [0] { \int_use:N \c@zc@abschap }
\zref@addprop \ZREF@mainlist { zc@abschap }
%    \end{macrocode}
%
%    \begin{macrocode}
\AddToHook { cmd / section / before }
  { \stepcounter { zc@abssec } }
\zref@newprop { zc@abssec } [0] { \int_use:N \c@zc@abssec }
\zref@addprop \ZREF@mainlist { zc@abssec }
%    \end{macrocode}
%
%
% These are the lists of properties to be used by \pkg{zref-check}, that is,
% the list of properties the references and targets store.  This is the
% minimum set required, more properties may be added according to options.
% For user facing labels, we must use the \texttt{main} property list, so that
% \pkg{zref-clever} can also retrieve the properties it needs to refer to
% them.
%    \begin{macrocode}
\zref@newlist { zrefcheck-check }
\zref@addprops { zrefcheck-check }
  {
    page , % for messages
    abspage ,
    zc@abschap ,
    zc@abssec
  }
\zref@newlist { zrefcheck-end }
\zref@addprops { zrefcheck-end }
  {
    abspage ,
    zc@abschap ,
    zc@abssec
  }
%    \end{macrocode}
% For \pkg{zref-vario} we only need page information, since we only perform
% \opt{above} and \opt{below} checks there.
%    \begin{macrocode}
\zref@newlist { zrefcheck-zrefvario }
\zref@addprops { zrefcheck-zrefvario }
  {
    page , % for messages
    abspage ,
  }
%    \end{macrocode}
%
%
% \section{Plumbing}
%
% \subsection{Auxiliary}
%
% \begin{macro}
%   {
%      \l_@@_tmpa_tl ,
%      \l_@@_tmpb_tl ,
%      \g_@@_tmpa_tl ,
%      \l_@@_tmpa_int ,
%      \l_@@_tmpa_bool ,
%      \g_@@_tmpa_ior ,
%   }
%   Temporary scratch variables.
%    \begin{macrocode}
\tl_new:N \l_@@_tmpa_tl
\tl_new:N \l_@@_tmpb_tl
\tl_new:N \g_@@_tmpa_tl
\int_new:N \l_@@_tmpa_int
\bool_new:N \l_@@_tmpa_bool
\ior_new:N \g_@@_tmpa_ior
%    \end{macrocode}
% \end{macro}
%
% \subsection{Messages}
%
% \begin{macro}{\@@_message:nnnn, \@@_message:nnne}
%    \begin{macrocode}
\cs_new_protected:Npn \@@_message:nnnn #1#2#3#4
  {
    \use:c { msg_ \l_@@_msglevel_tl :nnnnn }
      { zref-check } {#1} {#2} {#3} {#4}
  }
\cs_generate_variant:Nn \@@_message:nnnn { nnne }
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\msg_new:nnn { zref-check } { check-failed }
  {
    Check~failed~\msg_line_context:.~
    Failed~check~'#1'~for~label~'#2'~on~page~#3.
  }
\msg_new:nnn { zref-check } { double-check }
  {
    Same~page~check~\msg_line_context:.~
    Double-check~'#1'~for~label~'#2'~on~page~#3.
  }
\msg_new:nnn { zref-check } { empty-label }
  {
    Check~failed~\msg_line_context:.~
    Failed~check~'#1'~for~empty~label.
  }
\msg_new:nnn { zref-check } { no-checks }
  { No~checks~for~'\iow_char:N\\zcheck'~\msg_line_context:. }
%    \end{macrocode}
%
%    \begin{macrocode}
\msg_new:nnn { zref-check } { check-missing }
  { Check~'#1'~not~defined~\msg_line_context:. }
\msg_new:nnn { zref-check } { property-undefined }
  { Property~'#1'~not~defined~\msg_line_context:. }
\msg_new:nnn { zref-check } { property-not-in-label }
  { Label~'#1'~has~no~property~'#2'~\msg_line_context:. }
\msg_new:nnn { zref-check } { property-not-integer }
  { Property~'#1'~for~label~'#2'~not~an~integer~\msg_line_context:. }
%    \end{macrocode}
%
%    \begin{macrocode}
\msg_new:nnn { zref-check } { hyperref-preamble-only }
  {
    Option~'hyperref'~only~available~in~the~preamble. \iow_newline:
    Use~the~starred~version~of~'\iow_char:N\\zcheck'~instead.
  }
\msg_new:nnn { zref-check } { missing-hyperref }
  { Missing~'hyperref'~package. \iow_newline: Setting~'hyperref=false'. }
\msg_new:nnn { zref-check } { ignore-ok-document-only }
  {
    Option~'#1'~only~available~in~the~document. \iow_newline:
    Use~option~'msglevel'~instead.
  }
\msg_new:nnn { zref-check } { option-preamble-only }
  { Option~'#1'~is~preamble~only~\msg_line_context:. }
\msg_new:nnn { zref-check } { closerange-not-positive-integer }
  {
    Option~'closerange'~not~a~positive~integer~\msg_line_context:.~
    Using~default~value.
  }
\msg_new:nnn { zref-check } { labelcmd-undefined }
  {
    Control~sequence~named~'#1'~used~in~option~'labelcmd'~is~not~defined.~
    Using~default~value.
  }
\msg_new:nnn { zref-check } { option-deprecated-with-alternative }
  {
    Option~'#1'~has~been~deprecated~\msg_line_context:.\iow_newline:
    Use~'#2'~instead.
  }
\msg_new:nnn { zref-check } { option-deprecated }
  { Option~'#1'~has~been~deprecated~\msg_line_context:. }
\msg_new:nnn { zref-check } { load-time-options }
  {
    'zref-check'~does~not~accept~load-time~options.~
    To~configure~package~options,~use~'\iow_char:N\\zrefchecksetup'.
  }
%    \end{macrocode}
%
%
% \subsection{Integer testing}
%
% \begin{macro}{\@@_is_integer:n, \@@_int_to_roman:w}
%   From \url{https://tex.stackexchange.com/a/244405} (thanks Enrico Gregorio,
%   aka `egreg'), also see \url{https://tex.stackexchange.com/a/19769}.
%   Following the \texttt{l3styleguide}, I made a copy of
%   \cs{__int_to_roman:w}, since it is an internal function from the
%   \texttt{int} module, but we still get a warning from \texttt{l3build doc},
%   complaining about it.  And we're using \cs{tl_if_empty:oTF} instead of
%   \cs{tl_if_blank:oTF} as in egreg's answer, since \cs{romannumeral} is
%   defined so that ``the expansion is empty if the number is zero or
%   negative'', not ``blank''.  A couple of comments about this technique: the
%   underlying \cs{romannumeral} ignores space tokens and explicit signs
%   (\texttt{+} and \texttt{-}) in the expansion and hence it can only be used
%   to test positive integers; also the technique cannot distinguish whether
%   it received an empty argument or if ``the expansion was empty'' as a
%   result of receiving number as argument, so this must also be controlled
%   for since, in our use case, this may happen.
%    \begin{macrocode}
\cs_new_eq:NN \@@_int_to_roman:w \__int_to_roman:w
\prg_new_conditional:Npnn \@@_is_integer:n #1 { p, T , F , TF }
  {
    \tl_if_empty:oTF {#1}
      { \prg_return_false: }
      {
        \tl_if_empty:oTF { \@@_int_to_roman:w -0#1 }
          { \prg_return_true:  }
          { \prg_return_false: }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_is_integer_rgx:n}
%   A possible alternative to \cs{@@_is_integer:n} is to use a straightforward
%   regexp match (see \url{https://tex.stackexchange.com/a/427559}).  It does
%   not suffer from the mentioned caveats from the \cs{__int_to_roman:w}
%   technique, however, while \cs{@@_is_integer:n} is expandable,
%   \cs{@@_is_integer_rgx:n} is not.  Also, \cs{@@_is_integer_rgx:n} is
%   probably slower.
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_is_integer_rgx:n #1 { TF }
  {
    \regex_match:nnTF { \A\d+\Z } {#1}
      { \prg_return_true:  }
      { \prg_return_false: }
  }
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{Options}
%
%
% \subsubsection*{\opt{hyperref} option}
%
% \begin{variable}{\l_@@_use_hyperref_bool, \l_@@_warn_hyperref_bool}
%    \begin{macrocode}
\bool_new:N \l_@@_use_hyperref_bool
\bool_new:N \l_@@_warn_hyperref_bool
\keys_define:nn { zref-check/setup }
  {
    hyperref .choice: ,
    hyperref / auto .code:n =
      {
        \bool_set_true:N \l_@@_use_hyperref_bool
        \bool_set_false:N \l_@@_warn_hyperref_bool
      } ,
    hyperref / true .code:n =
      {
        \bool_set_true:N \l_@@_use_hyperref_bool
        \bool_set_true:N \l_@@_warn_hyperref_bool
      } ,
    hyperref / false .code:n =
      {
        \bool_set_false:N \l_@@_use_hyperref_bool
        \bool_set_false:N \l_@@_warn_hyperref_bool
      } ,
    hyperref .initial:n = auto ,
    hyperref .default:n = auto
  }
%    \end{macrocode}
% \end{variable}
%
%    \begin{macrocode}
\AddToHook { begindocument }
  {
    \@ifpackageloaded { hyperref }
      {
        \bool_if:NT \l_@@_use_hyperref_bool
          { \RequirePackage { zref-hyperref } }
      }
      {
        \bool_if:NT \l_@@_warn_hyperref_bool
          { \msg_warning:nn { zref-check } { missing-hyperref } }
        \bool_set_false:N \l_@@_use_hyperref_bool
      }
    \keys_define:nn { zref-check/setup }
      {
        hyperref .code:n =
          { \msg_warning:nn { zref-check } { hyperref-preamble-only } }
      }
  }
%    \end{macrocode}
%
%
% \subsubsection*{\opt{msglevel} option}
%
% \begin{variable}{\l_@@_msglevel_tl}
%    \begin{macrocode}
\tl_new:N \l_@@_msglevel_tl
\keys_define:nn { zref-check/setup }
  {
    msglevel .choice: ,
    msglevel / warn .code:n =
      { \tl_set:Nn \l_@@_msglevel_tl { warning } } ,
    msglevel / info .code:n =
      { \tl_set:Nn \l_@@_msglevel_tl { info } } ,
    msglevel / none .code:n =
      { \tl_set:Nn \l_@@_msglevel_tl { none } } ,
    msglevel / infoifdraft .code:n =
      {
        \ifdraft
          { \tl_set:Nn \l_@@_msglevel_tl { info } }
          { \tl_set:Nn \l_@@_msglevel_tl { warning } }
      } ,
    msglevel / warniffinal .code:n =
      {
        \ifoptionfinal
          { \tl_set:Nn \l_@@_msglevel_tl { warning } }
          { \tl_set:Nn \l_@@_msglevel_tl { info } }
      } ,
    msglevel .value_required:n = true ,
    msglevel .initial:n = warn ,
%    \end{macrocode}
% \opt{ignore} and \opt{ok} are convenience aliases for \opt{msglevel=none},
% but only for use in the document body.
%    \begin{macrocode}
    ignore .code:n =
      { \msg_warning:nnn { zref-check } { ignore-ok-document-only } { ignore } } ,
    ignore .value_forbidden:n = true ,
    ok .code:n =
      { \msg_warning:nnn { zref-check } { ignore-ok-document-only } { ok } } ,
    ok .value_forbidden:n = true ,
  }
%    \end{macrocode}
% \end{variable}
%
%    \begin{macrocode}
\AddToHook { begindocument }
  {
    \keys_define:nn { zref-check/setup }
      {
        ignore .meta:n = { msglevel = none } ,
        ok .meta:n = { msglevel = none } ,
      }
  }
%    \end{macrocode}
%
%
% \subsubsection*{\opt{onpage} option}
%
% \begin{variable}{\l_@@_msgonpage_bool}
%    \begin{macrocode}
\bool_new:N \l_@@_msgonpage_bool
\keys_define:nn { zref-check/setup }
  {
    onpage .choice: ,
    onpage / labelseq .code:n =
      {
        \bool_set_false:N \l_@@_msgonpage_bool
      } ,
    onpage / msg .code:n =
      {
        \bool_set_true:N \l_@@_msgonpage_bool
      } ,
    onpage / labelseqifdraft .code:n =
      {
        \ifdraft
          { \bool_set_false:N \l_@@_msgonpage_bool }
          { \bool_set_true:N \l_@@_msgonpage_bool }
      } ,
    onpage / msgiffinal .code:n =
      {
        \ifoptionfinal
          { \bool_set_true:N \l_@@_msgonpage_bool }
          { \bool_set_false:N \l_@@_msgonpage_bool }
      } ,
    onpage .value_required:n = true ,
    onpage .initial:n = labelseq
  }
%    \end{macrocode}
% \end{variable}
%
%
% \subsubsection*{\opt{closerange} option}
%
% \begin{variable}{\l_@@_close_range_int}
%    \begin{macrocode}
\int_new:N \l_@@_close_range_int
\keys_define:nn { zref-check/setup }
  {
    closerange .code:n =
      {
        \@@_is_integer_rgx:nTF {#1}
          { \int_set:Nn \l_@@_close_range_int { \int_eval:n {#1} } }
          {
            \msg_warning:nn { zref-check } { closerange-not-positive-integer }
            \int_set:Nn \l_@@_close_range_int { 5 }
          }
      } ,
    closerange .value_required:n = true ,
    closerange .initial:n = 5
  }
%    \end{macrocode}
% \end{variable}
%
%
% \subsubsection*{Package options}
%
% \pkg{zref-check} does not accept load-time options.  Despite the tradition
% of so doing, Joseph Wright has a point in recommending otherwise at
% \url{https://chat.stackexchange.com/transcript/message/60360822#60360822}:
% separating ``loading the package'' from ``configuring the package'' grants
% less trouble with ``option clashes'' and with expansion of options at
% load-time.
%    \begin{macrocode}
\bool_lazy_and:nnT
  { \tl_if_exist_p:c { opt@ zref-check.sty } }
  { ! \tl_if_empty_p:c { opt@ zref-check.sty } }
  { \msg_warning:nn { zref-check } { load-time-options } }
%    \end{macrocode}
%
%
% \begin{macro}[int]{\zrefchecksetup}
%   Provide \cs{zrefchecksetup}.
%    \begin{macrocode}
\NewDocumentCommand \zrefchecksetup { m }
  { \keys_set:nn { zref-check/setup } {#1} }
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{Position on page}
%
% Method for determining relative position within the page: the sequence in
% which the labels get shipped out, inferred from the sequence in which the
% labels occur in the \file{.aux} file.
%
% Some relevant info about the sequence of things:
% \url{https://tex.stackexchange.com/a/120978} and \texttt{texdoc lthooks},
% section ``Hooks provided by |\begin{document}|''.
%
%
% One first attempt at this was to use \cs{zref@newlabel}, which is the macro
% in which \pkg{zref} stores the label information in the aux file.  When the
% \file{.aux} file is read at the beginning of the compilation, this macro is
% expanded for each of the labels.  So, by redefining this macro we can feed a
% variable (a L3 sequence), and then do what it usually does, which is to
% define each label with the internal macro \cs{@newl@bel}, when the
% \file{.aux} file is read.
%
% Patching this macro for this is not possible.  First, \cs{zref@newlabel} is
% one of those ``commands that look ahead'' mentioned in \pkg{ltcmdhooks}
% documentation.  Indeed, \cs{@newl@bel} receives 3 arguments, and
% \cs{zref@newlabel} just passes the first, the following two will be scanned
% ahead.  Second, the \pkg{ltcmdhooks} hooks are not actually available when
% the \file{.aux} file is read, they come only after |\begin{document}|.
% Hence, redefinition would be the only alternative.  My attempts at this
% ended up registered at \url{https://tex.stackexchange.com/a/604744}.  But
% the best result in these lines was:
%
% \begin{verbatim}
% \ZREF@Robust\edef\zref@newlabel#1{
%   \noexpand\seq_gput_right:Nn \noexpand\g__zrefcheck_auxfile_lblseq_seq {#1}
%   \noexpand\@newl@bel{\ZREF@RefPrefix}{#1}
% }
% \end{verbatim}
%
%
% However, better than the above is to just read it from the \file{.aux} file
% directly, which relieves us from hacking into any internals.  That's what
% David Carlisle's answer at \url{https://tex.stackexchange.com/a/147705}
% does.  This answer has actually been converted into the package
% \pkg{listlbls} by Norbert Melzer, but it is made to work with regular
% labels, not with \pkg{zref}'s.  And it also does not really expose the
% information in a retrievable way (as far as I can tell).  So, the below is
% adapted from Carlisle's answer's technique (a poor man's version of it...).
%
% There is some subtlety here as to whether this approach makes it safe for us
% to read the labels at this point without \cs{zref@wrapper@babel}.  The
% common wisdom is that babel's shorthands are only active after
% |\begin{document}| (e.g., \url{https://tex.stackexchange.com/a/98897}).
% Alas, it is more complicated than that.  Babel's documentation says (in
% section 9.5 Shorthands): ``To prevent problems with the loading of other
% packages after babel we reset the catcode of the character to the original
% one at the end of the package and of each language file (except with
% KeepShorthandsActive).  It is re-activate[d] again at |\begin{document}|.
% We also need to make sure that the shorthands are active during the
% processing of the \file{.aux} file.  Otherwise some citations may give
% unexpected results in the printout when a shorthand was used in the optional
% argument of |\bibitem| for example.''  This is done with
% |\if@filesw \immediate\write\@mainaux{...}|.  In other words, the
% catcode change is written in the \file{.aux} file itself!  Indeed, if you
% inspect the file, you'll find them there.  Besides, there is still the
% ominous ``except with KeepShorthandsActive''.
%
% However, the \emph{method} we're using here is not quite the same as the
% usual run of the \file{.aux} file, because we're actively discarding the
% lines for which the first token is not equal to \cs{zref@newlabel}.  I have
% tested the famous sensitive case for this: \pkg{babel} \opt{french} and
% labels with colons.  And things worked as expected.  Well, \emph{if}
% \opt{KeepShorthandsActive} is enabled \emph{with \opt{french}} and we load
% the package \emph{after babel} things do break, but not quite because of the
% colons in the labels.  Even \pkg{siunitx} breaks in the same
% conditions\dots{}
%
% For reference: About what are valid characters for use in labels:
% \url{https://tex.stackexchange.com/a/18312}.  About some problems with
% active colons: \url{https://tex.stackexchange.com/a/89470}.  About the
% difference between L3 strings and token lists, see
% \url{https://tex.stackexchange.com/a/446381}, in particular Joseph Wright's
% comment: ``Strings are for data that will never be typeset, for example file
% names, identifiers, etc.: if the material may be used in typesetting, it
% should be a token list.''  See also moewe's (CW) answer in the same lines.
% Which suggests using L3 strings for the reference labels might be a good
% catch all approach, and possibly more robust.  David Carlisle's comment
% about \pkg{inputenc} and how the strings work is a caveat (see
% \url{https://tex.stackexchange.com/q/446123#comment1516961_446381}, thanks
% David Carlisle).  Still\dots{} let's stick to tradition as long as it works,
% \pkg{zref} already does a great job in this regard anyway.
%
%
% \begin{variable}{\g_@@_auxfile_lblseq_prop}
%    \begin{macrocode}
\prop_new:N \g_@@_auxfile_lblseq_prop
%    \end{macrocode}
% \end{variable}
%
%    \begin{macrocode}
\tl_gset:Nn \g_@@_tmpa_tl { \c_sys_jobname_str .aux }
\file_if_exist:nT { \g_@@_tmpa_tl }
  {
%    \end{macrocode}
% Retrieve the information from the \file{.aux} file, and store it in a
% property list, so that the sequence can be retrieved in key-value fashion.
%    \begin{macrocode}
    \ior_open:Nn \g_@@_tmpa_ior { \g_@@_tmpa_tl }
    \group_begin:
    \int_zero:N \l_@@_tmpa_int
    \tl_clear:N \l_@@_tmpa_tl
    \tl_clear:N \l_@@_tmpb_tl
    \bool_set_false:N \l_@@_tmpa_bool
    \ior_map_variable:NNn \g_@@_tmpa_ior \l_@@_tmpa_tl
      {
        \tl_map_variable:NNn \l_@@_tmpa_tl \l_@@_tmpb_tl
          {
            \tl_if_eq:NnTF \l_@@_tmpb_tl { \zref@newlabel }
              {
%    \end{macrocode}
% Found a \cs{zref@label}, signal it.
%    \begin{macrocode}
                \bool_set_true:N \l_@@_tmpa_bool
              }
              {
                \bool_if:NTF \l_@@_tmpa_bool
                  {
                    \bool_set_false:N \l_@@_tmpa_bool
                    \int_incr:N \l_@@_tmpa_int
                    \prop_gput:Nee \g_@@_auxfile_lblseq_prop
                      { \l_@@_tmpb_tl }
                      { \int_use:N \l_@@_tmpa_int }
                  }
                  {
%    \end{macrocode}
% If there is not a match of the first token with \cs{zref@newlabel}, break
% the loop and discard the rest of the line, to ensure no babel calls to
% \cs{catcode} in the \file{.aux} file get expanded.  This also breaks the
% loop and discards the rest of the \cs{zref@newlabel} lines after we got the
% label we wanted, since we reset \cs{l_@@_tmpa_bool} in the \texttt{T}
% branch.
%    \begin{macrocode}
                    \tl_map_break:
                  }
              }
          }
      }
    \group_end:
    \ior_close:N \g_@@_tmpa_ior
  }
%    \end{macrocode}
%
%
%
% The alternate method I had considered (more than that...) for this was using
% yx coordinates supplied by \pkg{zref}'s \pkg{savepos} module.  However, this
% approach brought in a number of complexities, including the need to patch
% either \cs{zref@label} or \cs{ZREF@label}.  In addition, the technique was
% at the bottom fundamentally flawed.  Ulrike Fischer was very much right when
% she said that ``structure and position are two different beasts''
% (\url{https://github.com/ho-tex/zref/issues/12#issuecomment-880022576}).  It
% is true that the checks based on it behaved decently, in normal
% circumstances, and except for outrageous label placement by the user, it
% would return the expected results.  We don't really need exact coordinates
% to decide ``above/below''.  Besides, it would do an exact job for the
% dedicated target macros of this package.  It is also true that the ``page''
% for \cs{pageref} is stored with the value of where the \cs{label} is placed,
% wherever that may be.  However, I could not conceive a situation where the
% \texttt{yx} criterion would perform clearly better than the
% \texttt{labelseq} one.  And, if that's the case, and considering the
% complications it brings, this check was a slippery slope.  All in all, I've
% decided to drop it.
%
% There's an interesting answer by David Carlisle at
% \url{https://tex.stackexchange.com/a/419189} to decide whether to typeset
% ``above'' or ``below'' using a method which essentially boils down to
% ``position in the \file{.aux} file''.
%
%
%
% \subsection{Counter}
%
% We need a dedicated counter for the labels generated by the checks and
% targets.  The value of the counter is not relevant, we just need it to be
% able to set proper anchors with \cs{refstepcounter}.  And, since I couldn't
% find a \cs{refstepcounter} equivalent in L3, we use a standard 2e counter
% here.  I'm also using the technique to ensure the counter is never reset
% that is used by \file{zref-abspage.sty} and \cs{zref@require@unique}.
% Indeed, the requirements are the same, we need numbers ensured to be
% \emph{unique} in the counter.
%
%    \begin{macrocode}
\begingroup
  \let \@addtoreset \ltx@gobbletwo
  \newcounter { zrefcheck }
\endgroup
\setcounter { zrefcheck } { 0 }
%    \end{macrocode}
%
%
%
% \subsection{Label formats}
%
% \begin{macro}{\@@_check_lblfmt:n}
%   \begin{syntax}
%     \cs{@@_check_lblfmt:n} \Arg{check id int}
%   \end{syntax}
%    \begin{macrocode}
\cs_new:Npn \@@_check_lblfmt:n #1 { zrefcheck@ \int_use:N #1 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_end_lblfmt:n}
%   \begin{syntax}
%     \cs{@@_end_lblfmt:n} \Arg{label}
%   \end{syntax}
%    \begin{macrocode}
\cs_new:Npn \@@_end_lblfmt:n #1 { #1 @zrefcheck }
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{Property values}
%
%
% \begin{macro}[int]{\zrefcheck_get_astl:nnn}
%   A convenience function to retrieve property values from labels.  Uses
%   \cs{g_@@_auxfile_lblseq_prop} for \texttt{lblseq}, and calls
%   \cs{zref@extractdefault} for everything else.
%
%   We cannot use the ``return value'' of \cs{@@_get_astl:nnn} or
%   \cs{@@_get_asint:nnn} directly, because we need to use the retrieved
%   property values as arguments in the checks, however we use here a number
%   of non-expandable operations.  Hence, we receive a local \texttt{tl/int}
%   variable as third argument and set that, so that it is available (and
%   expandable) at the place of use, and also make these functions `protected'
%   (see egreg's \url{https://tex.stackexchange.com/a/572903}: ``a function
%   that performs assignments should be \texttt{protected}'').  For this
%   reason, we do not group here, because we are passing a local variable
%   around, but it is expected this function will be called within a group.
%
%   We're returning \cs{c_empty_tl} in case of failure to find the intended
%   property value (explicitly in \cs{zref@extractdefault}, but that is also
%   what \cs{tl_clear:N} does).
%
%   \begin{syntax}
%     \cs{zrefcheck_get_astl:nnn} \Arg{label} \Arg{prop} \Arg{tl var}
%   \end{syntax}
%    \begin{macrocode}
\cs_new_protected:Npn \zrefcheck_get_astl:nnn #1#2#3
  {
    \tl_clear:N #3
    \tl_if_eq:nnTF {#2} { lblseq }
      {
        \prop_get:NnNF \g_@@_auxfile_lblseq_prop {#1} #3
          {
            \msg_warning:nnnn { zref-check }
              { property-not-in-label } {#1} {#2}
          }
      }
      {
%    \end{macrocode}
% There are three things we need to check to ensure the information we are
% trying to retrieve here exists: the existence of \Arg{label}, the existence
% of \Arg{prop}, and whether the particular label being queried actually
% contains the property.  If that's all in place, the value is passed to the
% checks, and it's their responsibility to verify the consistency of this
% value.
%
% The existence of the label is an user facing issue, and a warning for this
% is placed in \cs{@@_zcheck:nnnnn} (and done with \cs{zref@refused}).  We do
% check here though for definition with \cs{zref@ifrefundefined} and silently
% do nothing if it is undefined, to reduce irrelevant warnings in a fresh
% compilation round.  The other two are more ``internal'' problems, either
% some problem with the checks, or with the configuration of \pkg{zref} for
% their consumption.
%    \begin{macrocode}
        \zref@ifrefundefined {#1}
          {}
          {
            \zref@ifpropundefined {#2}
              { \msg_warning:nnnn { zref-check } { property-undefined } {#2} }
              {
                \zref@ifrefcontainsprop {#1} {#2}
                  {
                    \exp_args:NNNo \exp_args:NNo \tl_set:Nn #3
                      { \zref@extractdefault {#1} {#2} { \c_empty_tl } }
                  }
                  {
                    \msg_warning:nnnn
                      { zref-check } { property-not-in-label } {#1} {#2}
                  }
              }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{variable}{\l_@@_integer_bool}
%   \cs{zrefcheck_get_asint:nnn} is a very convenient wrapper around the more
%   general \cs{zrefcheck_get_astl:nnn}, since almost always we'll be wanting
%   to compare numbers in the checks.  However, it is quite hard for it to
%   ensure an integer is \emph{always} returned in the case of errors.  And
%   those do occur, even in a well structured document (e.g., in a first round
%   of compilation).  To complicate things, the L3 integer predicates are
%   \emph{very} sensitive to receiving any other kind of data, and they
%   \emph{scream}.  To handle this \cs{zrefcheck_get_asint:nnn} uses
%   \cs{l_@@_integer_bool} to signal if an integer could not be returned.  To
%   use this function always set \cs{l_@@_integer_bool} to true first, then
%   call it as much as you need.  If any of these calls got is returning
%   anything which is not an integer, \cs{l_@@_integer_bool} will have been
%   set to false, and you should check that this hasn't happened before
%   actually comparing the integers (\cs{bool_lazy_and:nnTF} is your friend).
%    \begin{macrocode}
\bool_new:N \l_@@_integer_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_propval_tl}
%    \begin{macrocode}
\tl_new:N \l_@@_propval_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}[int]{\zrefcheck_get_asint:nnn}
%   \begin{syntax}
%     \cs{zrefcheck_get_asint:nnn} \Arg{label} \Arg{prop} \Arg{int var}
%   \end{syntax}
%    \begin{macrocode}
\cs_new_protected:Npn \zrefcheck_get_asint:nnn #1#2#3
  {
    \zrefcheck_get_astl:nnn {#1} {#2} { \l_@@_propval_tl }
    \@@_is_integer:nTF { \l_@@_propval_tl }
      {
%    \end{macrocode}
% Make it an integer data type.
%    \begin{macrocode}
        \int_set:Nn #3 { \int_eval:n { \l_@@_propval_tl } }
      }
      {
        \bool_set_false:N \l_@@_integer_bool
        \zref@ifrefundefined {#1}
%    \end{macrocode}
% Keep silent if ref is undefined to reduce irrelevant warnings in a fresh
% compilation round.  Again, this is also not the point to check for undefined
% references, that's a task for \cs{@@_zcheck:nnnnn}.
%    \begin{macrocode}
          { }
          {
            \msg_warning:nnnn { zref-check }
              { property-not-integer } {#2} {#1}
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
%
%
%
% \section{User interface}
%
%
% \subsection{\cs{zcheck}}
%
% \begin{macro}[int]{\zcheck}
%   The \marg{text} argument of \cs{zcheck} should not be long, since
%   \cs{hyperlink} cannot receive a long argument.  Besides, there is no
%   reason for it to be.  Note, also, that hyperlinks crossing page boundaries
%   have some known issues: \url{https://tex.stackexchange.com/a/182769},
%   \url{https://tex.stackexchange.com/a/54607},
%   \url{https://tex.stackexchange.com/a/179907}.
%
%   \begin{syntax}
%     \cs{zcheck}\meta{*}\oarg{checks/options}\marg{labels}\marg{text}
%   \end{syntax}
%
%    \begin{macrocode}
\NewDocumentCommand \zcheck { s O { } m m }
  { \zref@wrapper@babel \@@_zcheck:nnnn {#3} {#1} {#2} {#4} }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{variable}
%   {
%     \l_@@_zcheck_labels_seq ,
%     \g_@@_id_int ,
%     \l_@@_checkbeg_tl ,
%     \l_@@_link_label_tl ,
%     \l_@@_link_anchor_tl ,
%     \l_@@_link_star_bool
%   }
%    \begin{macrocode}
\seq_new:N \l_@@_zcheck_labels_seq
\int_new:N \g_@@_id_int
\tl_new:N \l_@@_checkbeg_tl
\tl_new:N \l_@@_link_label_tl
\tl_new:N \l_@@_link_anchor_tl
\bool_new:N \l_@@_link_star_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_zcheck:nnnn}
%   An intermediate internal function, which does the actual heavy lifting,
%   and places \Arg{labels} as first argument, so that it can be protected by
%   \cs{zref@wrapper@babel} in \cs{zcheck}.  This is the same procedure as the
%   one used in the definition of \cs{zref} in \file{zref-user.sty} for
%   protection of \pkg{babel} active characters.
%
%   \begin{syntax}
%     \cs{@@_zcheck:nnnn} \Arg{labels} \Arg{*} \Arg{checks/options} \Arg{text}
%   \end{syntax}
%
%    \begin{macrocode}
\cs_new_protected:Npn \@@_zcheck:nnnn #1#2#3#4
  {
    \group_begin:
%    \end{macrocode}
% Process local options and checks.  We use \cs{seq_set_split:Nnn} to set
% \cs{l_@@_zcheck_labels_seq} -- instead of \cs{seq_set_from_clist:Nn} -- to
% support empty labels.
%    \begin{macrocode}
    \keys_set:nn { zref-check/zcheck } {#3}
    \seq_set_split:Nnn \l_@@_zcheck_labels_seq { , } {#1}
%    \end{macrocode}
% Names of the labels for this zcheck call.
%    \begin{macrocode}
    \int_gincr:N \g_@@_id_int
    \tl_set:Ne \l_@@_checkbeg_tl
      { \@@_check_lblfmt:n { \g_@@_id_int } }
%    \end{macrocode}
% Set checkbeg label.
%    \begin{macrocode}
    \zref@labelbylist { \l_@@_checkbeg_tl } { zrefcheck-check }
%    \end{macrocode}
% Typeset \marg{text}, with hyperlink when appropriate.  Even though the first
% argument can receive a list of labels, there is no meaningful way to set
% links to multiple targets.  Hence, only the first one is considered for
% hyperlinking.
%    \begin{macrocode}
    \seq_get:NN \l_@@_zcheck_labels_seq \l_@@_link_label_tl
    \bool_set:Nn \l_@@_link_star_bool {#2}
    \zref@ifrefundefined { \l_@@_link_label_tl }
%    \end{macrocode}
% If the reference is undefined, just typeset.
%    \begin{macrocode}
      {#4}
      {
        \bool_if:nTF
          {
            \l_@@_use_hyperref_bool &&
            ! \l_@@_link_star_bool
          }
          {
            \exp_args:Ne \zrefcheck_get_astl:nnn
              { \l_@@_link_label_tl }
              { anchor } { \l_@@_link_anchor_tl }
            \hyperlink { \l_@@_link_anchor_tl } {#4}
          }
          {#4}
      }
%    \end{macrocode}
% Set checkend label.
%    \begin{macrocode}
    \bool_if:NT \l_@@_zcheck_end_label_bool
      {
        \zref@labelbylist
          { \@@_end_lblfmt:n { \l_@@_checkbeg_tl } }
          { zrefcheck-end }
      }
%    \end{macrocode}
% Check if \meta{labels} are defined.
%    \begin{macrocode}
    \seq_map_inline:Nn \l_@@_zcheck_labels_seq
      { \tl_if_empty:nF {##1} { \zref@refused {##1} } }
%    \end{macrocode}
% Run the checks.
%    \begin{macrocode}
    \@@_run_checks:nne { \l_@@_zcheck_checks_seq }
      { \l_@@_zcheck_labels_seq } { \l_@@_checkbeg_tl }
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Targets}
%
% \begin{macro}[int]{\zctarget}
%   \begin{syntax}
%     \cs{zctarget}\marg{label}\marg{text}
%   \end{syntax}
%    \begin{macrocode}
\NewDocumentCommand \zctarget { m +m }
  {
%    \end{macrocode}
% Group contents of \cs{zctarget} to avoid leaking the effects of
% \cs{refstepcounter} over \cs{@currentlabel}.  The same care is not needed
% for \texttt{zcregion}, since the environment is already grouped.
%    \begin{macrocode}
    \group_begin:
    \refstepcounter { zrefcheck }
    \zref@wrapper@babel \zref@label {#1}
    #2
    \tl_if_empty:nF {#2}
      {
        \zref@wrapper@babel
          \zref@labelbylist { \@@_end_lblfmt:n {#1} } { zrefcheck-end }
      }
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{zcregion}
%   \begin{syntax}
%     |\begin{zcregion}|\marg{label}
%       |  ...|
%     |\end{zcregion}|
%   \end{syntax}
%    \begin{macrocode}
\NewDocumentEnvironment {zcregion} { m }
  {
    \refstepcounter { zrefcheck }
    \zref@wrapper@babel \zref@label {#1}
  }
  {
    \zref@wrapper@babel
      \zref@labelbylist { \@@_end_lblfmt:n {#1} } { zrefcheck-end }
  }
%    \end{macrocode}
% \end{macro}
%
%
%
% \section{Checks}
%
% What is needed define a \pkg{zref-check} check?
%
% First, a conditional function defined with:
%
% \cs{prg_new_protected_conditional:Npnn} \cs{@@_check_\meta{check}:nn} |#1#2 { F }|
%
% \noindent where \meta{check} is the name of the check, the first argument is
% the \Arg{label} and the second the \Arg{reference}.  The existence of the
% check is verified by the existence of the function with this name-scheme
% (and signatures).  As usual, this function must return either
% \cs{prg_return_true:} or \cs{prg_return_false:}.  Of course, you can define
% other variants if you need them internally, it is just that what the package
% does expect and verifies is the existence of the \texttt{:nnF} variant.
%
% Note that the naming convention of the checks adopts the perspective of the
% \meta{reference}.  That is, the ``before'' check should return true if the
% \meta{label} occurs before the ``reference''.
%
% The check conditionals are expected to retrieve \pkg{zref}'s label
% information with \cs{zrefcheck_get_astl:nnn} or
% \cs{zrefcheck_get_asint:nnn}.  Also, technically speaking, the
% \meta{reference} argument is also a label, actually a pair of them, as set
% by \cs{zcheck}.  For the ``labels'', any \pkg{zref} property in \pkg{zref}'s
% main list is available, the ``references'' store the properties in the
% \texttt{zrefcheck} list.  Besides those, there is also the \texttt{lblseq}
% (fake) property (for either ``labels'' or ``references''), stored in
% \cs{g_@@_auxfile_lblseq_prop}.
%
% Second, the required properties of labels and references must be duly
% registered for \pkg{zref}.  This can be done with \cs{zref@newprop},
% \cs{zref@addprop} and friends, as usual.
%
% Third, the check must be registered as a key which gets setup in
% \cs{zcheck} by the \texttt{ zref-check / zcheck } key set.
%
% Fourth, if the check requires only a single label to work, it should be
% registered in \cs{c_@@_single_label_checks_seq}.
%
%
% \subsection{Single label checks}
%
%
% Some checks do not require an ``end label'' in \cs{zcheck}, notably the
% sectioning ones, which don't rely on page boundaries.  Hence, in case
% \cs{zcheck} only calls checks in this set, we can spare the setting of the
% end label.
%
% \begin{variable}{\c_@@_single_label_checks_seq}
%    \begin{macrocode}
\seq_const_from_clist:Nn \c_@@_single_label_checks_seq
  {
    thischap ,
    prevchap ,
    nextchap ,
    chapsbefore ,
    chapsafter ,
    thissec ,
    prevsec ,
    nextsec ,
    secsbefore ,
    secsafter ,
    manual ,
  }
%    \end{macrocode}
% \end{variable}
%
%
%
% \subsection{Setup}
%
% \begin{variable}{\l_@@_zcheck_checks_seq,\l_@@_end_label_required_bool}
%    \begin{macrocode}
\seq_new:N \l_@@_zcheck_checks_seq
\bool_new:N \l_@@_zcheck_end_label_bool
%    \end{macrocode}
% \end{variable}
%
%
% First, we inherit all the main options into the keys of \texttt{zref-check /
% zcheck}. See \url{https://github.com/latex3/latex3/issues/1254}.
% ^^A zref-check/zcheck
%    \begin{macrocode}
\keys_define:nn { zref-check } { zcheck .inherit:n = zref-check/setup }
%    \end{macrocode}
%
% Then we add the checks to it.
%    \begin{macrocode}
\clist_map_inline:nn
  {
    thispage ,
    prevpage ,
    nextpage ,
    facing ,
    otherpage ,
    pagegap ,
    above ,
    below ,
    pagesbefore ,
    ppbefore ,
    pagesafter ,
    ppafter ,
    before ,
    after ,
    thischap ,
    prevchap ,
    nextchap ,
    chapsbefore ,
    chapsafter ,
    thissec ,
    prevsec ,
    nextsec ,
    secsbefore ,
    secsafter ,
    close ,
    far ,
    manual ,
  }
  {
    \keys_define:nn { zref-check/zcheck }
      {
        #1 .code:n =
          {
            \seq_put_right:Nn \l_@@_zcheck_checks_seq {#1}
            \seq_if_in:NnF \c_@@_single_label_checks_seq {#1}
              { \bool_set_true:N \l_@@_zcheck_end_label_bool }
          } ,
        #1 .value_forbidden:n = true ,
     }
  }
%    \end{macrocode}
%
%
%
% \subsection{Running}
%
% \begin{macro}{\@@_run_checks:nnn}
%   \begin{syntax}
%     \cs{@@_run_checks:nnn} \Arg{checks} \Arg{labels} \Arg{reference}
%   \end{syntax}
%   \meta{checks} are expected to be received as a sequence variable.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_run_checks:nnn #1#2#3
  {
    \group_begin:
    \seq_map_inline:Nn #2
      {
        \seq_if_empty:NTF #1
          { \@@_message:nnnn { no-checks } { } { } { } }
          {
            \seq_map_inline:Nn #1
              { \@@_do_check:nnn {####1} {##1} {#3} }
          }
      }
    \group_end:
  }
\cs_generate_variant:Nn \@@_run_checks:nnn { nne }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{variable}
%   {
%     \l_@@_passedcheck_bool ,
%     \l_@@_onpage_bool ,
%     \l_@@_empty_label_bool ,
%     \c_@@_onpage_checks_seq
%   }
%    \begin{macrocode}
\bool_new:N \l_@@_passedcheck_bool
\bool_new:N \l_@@_onpage_bool
\bool_new:N \l_@@_empty_label_bool
\seq_const_from_clist:Nn \c_@@_onpage_checks_seq
  { above , below , before , after }
%    \end{macrocode}
% \end{variable}
%
%
% Variant not provided by \pkg{expl3}.
%    \begin{macrocode}
\cs_generate_variant:Nn \exp_args:Nnno { Nnoo }
%    \end{macrocode}
%
% \begin{macro}{\@@_do_check:nnn}
%   \begin{syntax}
%     \cs{@@_do_check:nnn} \Arg{check} \Arg{label beg} \Arg{reference beg}
%   \end{syntax}
%    \begin{macrocode}
\cs_new_protected:Npn \@@_do_check:nnn #1#2#3
  {
    \group_begin:
    \bool_set_true:N \l_@@_passedcheck_bool
    \bool_set_false:N \l_@@_onpage_bool
    \bool_set_false:N \l_@@_empty_label_bool
    \cs_if_exist:cTF { @@_check_ #1 :nnF }
      {
%    \end{macrocode}
% \meta{label beg} may be defined or not, it is arbitrary user input.  Whether
% this is the case is checked in \cs{@@_zcheck:nnnnn}, and due warning already
% ensues.  So there's no need to do it again here.  The only exception is the
% case of empty labels, for which we want to issue a failed check warning.
%    \begin{macrocode}
        \zref@ifrefundefined {#2}
          {
            \tl_if_empty:nT {#2}
              {
                \bool_set_false:N \l_@@_passedcheck_bool
                \bool_set_true:N \l_@@_empty_label_bool
              }
          }
          {
            % ``label beg'' vs ``reference beg''.
            \use:c { @@_check_ #1 :nnF }
              {#2} {#3}
              { \bool_set_false:N \l_@@_passedcheck_bool }
            % ``reference end'' \emph{may} exist or not depending on the
            % checks.
            \zref@ifrefundefined { \@@_end_lblfmt:n {#3} }
              {
                % ``label end'' \emph{may} have been created by the
                % target commands.
                \zref@ifrefundefined { \@@_end_lblfmt:n {#2} }
                  {}
                  {
                    % ``label end'' vs ``reference beg''.
                    \exp_args:Nno \use:c { @@_check_ #1 :nnF }
                      { \@@_end_lblfmt:n {#2} } {#3}
                      { \bool_set_false:N \l_@@_passedcheck_bool }
                  }
              }
              {
                % ``label beg'' vs ``reference end''.
                \exp_args:Nnno \use:c { @@_check_ #1 :nnF }
                  {#2} { \@@_end_lblfmt:n {#3} }
                  { \bool_set_false:N \l_@@_passedcheck_bool }
                % ``label end'' \emph{may} have been created by the
                % target commands.
                \zref@ifrefundefined { \@@_end_lblfmt:n {#2} }
                  {}
                  {
                    % ``label end'' vs ``reference beg''.
                    \exp_args:Nno \use:c { @@_check_ #1 :nnF }
                      { \@@_end_lblfmt:n {#2} } {#3}
                      { \bool_set_false:N \l_@@_passedcheck_bool }
                    % ``label end'' vs ``reference end''.
                    \exp_args:Nnoo \use:c { @@_check_ #1 :nnF }
                      { \@@_end_lblfmt:n {#2} }
                      { \@@_end_lblfmt:n {#3} }
                      { \bool_set_false:N \l_@@_passedcheck_bool }
                  }
              }
%    \end{macrocode}
% Handle option \opt{onpage=msg}.  This is only granted for tests which
% perform ``within this page'' checks (\opt{above}, \opt{below}, \opt{before},
% \opt{after}) \emph{and} if any of the two by two checks uses a ``within this
% page'' comparison.  If both conditions are met, signal.
%    \begin{macrocode}
            \seq_if_in:NnT \c_@@_onpage_checks_seq {#1}
              {
                \@@_check_thispage:nnT
                  {#2} {#3}
                  { \bool_set_true:N \l_@@_onpage_bool }
                \zref@ifrefundefined { \@@_end_lblfmt:n {#3} }
                  {
                    \zref@ifrefundefined { \@@_end_lblfmt:n {#2} }
                      {}
                      {
                        \@@_check_thispage:nnT
                          { \@@_end_lblfmt:n {#2} } {#3}
                          { \bool_set_true:N \l_@@_onpage_bool }
                      }
                  }
                  {
                    \@@_check_thispage:nnT
                      {#2} { \@@_end_lblfmt:n {#3} }
                      { \bool_set_true:N \l_@@_onpage_bool }
                    \zref@ifrefundefined { \@@_end_lblfmt:n {#2} }
                      {}
                      {
                        \@@_check_thispage:nnT
                          { \@@_end_lblfmt:n {#2} } {#3}
                          { \bool_set_true:N \l_@@_onpage_bool }
                        \@@_check_thispage:nnT
                          { \@@_end_lblfmt:n {#2} }
                          { \@@_end_lblfmt:n {#3} }
                          { \bool_set_true:N \l_@@_onpage_bool }
                      }
                  }
              }
          }
      }
      { \msg_warning:nnn { zref-check } { check-missing } {#1} }
    \bool_if:NTF \l_@@_passedcheck_bool
      {
        \bool_if:nT
          {
            \l_@@_msgonpage_bool &&
            \l_@@_onpage_bool
          }
          {
            \@@_message:nnne { double-check } {#1} {#2}
              { \zref@extractdefault {#3} {page} {'unknown'} }
          }
      }
      {
        \bool_if:NTF \l_@@_empty_label_bool
          { \@@_message:nnnn { empty-label } {#1} { } { } }
          {
            \@@_message:nnne { check-failed } {#1} {#2}
              { \zref@extractdefault {#3} {page} {'unknown'} }
          }
      }
    \group_end:
  }
\cs_generate_variant:Nn \@@_do_check:nnn { nnV }
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Conditionals}
%
% \begin{variable}
%   {
%     \l_@@_lbl_int ,
%     \l_@@_ref_int ,
%     \l_@@_lbl_b_int ,
%     \l_@@_ref_b_int
%   }
%   More readable scratch variables for the tests.
%    \begin{macrocode}
\int_new:N \l_@@_lbl_int
\int_new:N \l_@@_ref_int
\int_new:N \l_@@_lbl_b_int
\int_new:N \l_@@_ref_b_int
%    \end{macrocode}
% \end{variable}
%
%
% \subsubsection{This page}
%
% \begin{macro}{\@@_check_thispage:nn, \@@_check_otherpage:nn}
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_check_thispage:nn #1#2 { T , F , TF }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { abspage } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { abspage } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_int } = { \l_@@_ref_int } &&
%    \end{macrocode}
% `0' is the default value of \texttt{abspage}, but this value should not
% happen normally for this property, since even the first page, after it gets
% shipped out, will receive value `1'.  So, if we do find `0' here, better
% signal something is wrong.  This comment extends to all page number checks.
%    \begin{macrocode}
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_otherpage:nn #1#2 { T , F , TF }
  {
    \@@_check_thispage:nnTF {#1} {#2}
      { \prg_return_false: }
      { \prg_return_true:  }
  }
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{On page}
%
% \begin{macro}{\@@_check_above:nn, \@@_check_below:nn}
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_check_above:nn #1#2 { F , TF }
  {
    \group_begin:
    \@@_check_thispage:nnTF {#1} {#2}
      {
        \bool_set_true:N \l_@@_integer_bool
        \zrefcheck_get_asint:nnn {#1} { lblseq } { \l_@@_lbl_int }
        \zrefcheck_get_asint:nnn {#2} { lblseq } { \l_@@_ref_int }
        \bool_lazy_and:nnTF
          { \l_@@_integer_bool }
          {
            \int_compare_p:nNn
              { \l_@@_lbl_int } < { \l_@@_ref_int } &&
            ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
            ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
          }
          { \group_insert_after:N \prg_return_true:  }
          { \group_insert_after:N \prg_return_false: }
      }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_below:nn #1#2 { F , TF }
  {
    \@@_check_thispage:nnTF {#1} {#2}
      {
        \@@_check_above:nnTF {#1} {#2}
          { \prg_return_false: }
          { \prg_return_true:  }
      }
      { \prg_return_false: }
  }
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Before / After}
%
% \begin{macro}{\@@_check_before:nn, \@@_check_after:nn}
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_check_before:nn #1#2 { F }
  {
    \@@_check_pagesbefore:nnTF {#1} {#2}
      { \prg_return_true: }
      {
        \@@_check_above:nnTF {#1} {#2}
          { \prg_return_true:  }
          { \prg_return_false: }
      }
  }
\prg_new_protected_conditional:Npnn \@@_check_after:nn #1#2 { F }
  {
    \@@_check_pagesafter:nnTF {#1} {#2}
      { \prg_return_true: }
      {
        \@@_check_below:nnTF {#1} {#2}
          { \prg_return_true:  }
          { \prg_return_false: }
      }
  }
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Pages}
%
% \begin{macro}
%   {
%     \@@_check_nextpage:nn ,
%     \@@_check_prevpage:nn ,
%     \@@_check_pagesbefore:nn ,
%     \@@_check_ppbefore:nn ,
%     \@@_check_pagesafter:nn ,
%     \@@_check_ppafter:nn ,
%     \@@_check_pagegap:nn ,
%     \@@_check_facing:nn
%   }
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_check_nextpage:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { abspage } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { abspage } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_int } = { \l_@@_ref_int + 1 } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_prevpage:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { abspage } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { abspage } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_int } = { \l_@@_ref_int - 1 } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_pagesbefore:nn #1#2 { F , TF }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { abspage } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { abspage } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_int } < { \l_@@_ref_int } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\cs_new_eq:NN \@@_check_ppbefore:nnF \@@_check_pagesbefore:nnF
\prg_new_protected_conditional:Npnn \@@_check_pagesafter:nn #1#2 { F , TF }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { abspage } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { abspage } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_int } > { \l_@@_ref_int } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\cs_new_eq:NN \@@_check_ppafter:nnF \@@_check_pagesafter:nnF
\prg_new_protected_conditional:Npnn \@@_check_pagegap:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { abspage } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { abspage } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \int_abs:n { \l_@@_lbl_int - \l_@@_ref_int } } > { 1 } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_facing:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { abspage } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { abspage } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
%    \end{macrocode}
% There exists no ``facing'' page if the document is not twoside.
%    \begin{macrocode}
        \legacy_if_p:n { @twoside } &&
%    \end{macrocode}
% Now we test ``facing''.
%    \begin{macrocode}
        (
          (
            \int_if_odd_p:n { \l_@@_ref_int } &&
            \int_compare_p:nNn
              { \l_@@_lbl_int } = { \l_@@_ref_int - 1 }
          ) ||
          (
            \int_if_even_p:n { \l_@@_ref_int } &&
            \int_compare_p:nNn
              { \l_@@_lbl_int } = { \l_@@_ref_int + 1 }
          )
        ) &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Close / Far}
%
% \begin{macro}{\@@_check_close:nn, \@@_check_far:nn}
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_check_close:nn #1#2 { F , TF }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { abspage } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { abspage } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \int_abs:n { \l_@@_lbl_int - \l_@@_ref_int } }
          <
          { \l_@@_close_range_int + 1 } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_far:nn #1#2 { F }
  {
    \@@_check_close:nnTF {#1} {#2}
      { \prg_return_false: }
      { \prg_return_true:  }
  }
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Chapter}
%
% \begin{macro}
%   {
%     \@@_check_thischap:nn ,
%     \@@_check_nextchap:nn ,
%     \@@_check_prevchap:nn ,
%     \@@_check_chapsafter:nn ,
%     \@@_check_chapsbefore:nn
%   }
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_check_thischap:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { zc@abschap } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abschap } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_int } = { \l_@@_ref_int } &&
%    \end{macrocode}
% `0' is the default value of \texttt{zc@abschap} property, and means here no
% \cs{chapter} has yet been issued, therefore it cannot be ``this chapter'',
% nor ``the next chapter'', nor ``the previous chapter'', it is just ``no
% chapter''.  Note, however, that a statement about a ``future'' chapter does
% not require the ``current'' one to exist.  This comment extends to all
% chapter checks.
%    \begin{macrocode}
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_nextchap:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { zc@abschap } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abschap } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_int } = { \l_@@_ref_int + 1 } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_prevchap:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { zc@abschap } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abschap } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_int } = { \l_@@_ref_int - 1 } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_chapsafter:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { zc@abschap } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abschap } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_int } > { \l_@@_ref_int } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_chapsbefore:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { zc@abschap } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abschap } { \l_@@_ref_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_int } < { \l_@@_ref_int } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Section}
%
% \begin{macro}
%   {
%     \@@_check_thissec:nn ,
%     \@@_check_nextsec:nn ,
%     \@@_check_prevsec:nn ,
%     \@@_check_secsafter:nn ,
%     \@@_check_secsbefore:nn
%   }
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_check_thissec:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { zc@abssec  } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abssec  } { \l_@@_ref_int }
    \zrefcheck_get_asint:nnn {#1} { zc@abschap } { \l_@@_lbl_b_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abschap } { \l_@@_ref_b_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_b_int } = { \l_@@_ref_b_int } &&
        \int_compare_p:nNn
          { \l_@@_lbl_int } = { \l_@@_ref_int } &&
%    \end{macrocode}
% `0' is the default value of \texttt{zc@abssec} property, and means here no
% \cs{section} has yet been issued since its counter has been reset, which
% occurs at the beginning of the document and at every chapter.  Hence, as is
% the case for chapters, `0' is just ``not a section''.  The same observation
% about the need of the ``current'' section to exist to be able to refer to a
% ``future'' one also holds.  This comment extends to all section checks.
%    \begin{macrocode}
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_nextsec:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { zc@abssec  } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abssec  } { \l_@@_ref_int }
    \zrefcheck_get_asint:nnn {#1} { zc@abschap } { \l_@@_lbl_b_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abschap } { \l_@@_ref_b_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_b_int } = { \l_@@_ref_b_int } &&
        \int_compare_p:nNn
          { \l_@@_lbl_int } = { \l_@@_ref_int + 1 } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_prevsec:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { zc@abssec  } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abssec  } { \l_@@_ref_int }
    \zrefcheck_get_asint:nnn {#1} { zc@abschap } { \l_@@_lbl_b_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abschap } { \l_@@_ref_b_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_b_int } = { \l_@@_ref_b_int } &&
        \int_compare_p:nNn
          { \l_@@_lbl_int } = { \l_@@_ref_int - 1 } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_secsafter:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { zc@abssec  } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abssec  } { \l_@@_ref_int }
    \zrefcheck_get_asint:nnn {#1} { zc@abschap } { \l_@@_lbl_b_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abschap } { \l_@@_ref_b_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_b_int } = { \l_@@_ref_b_int } &&
        \int_compare_p:nNn
          { \l_@@_lbl_int } > { \l_@@_ref_int } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\prg_new_protected_conditional:Npnn \@@_check_secsbefore:nn #1#2 { F }
  {
    \group_begin:
    \bool_set_true:N \l_@@_integer_bool
    \zrefcheck_get_asint:nnn {#1} { zc@abssec  } { \l_@@_lbl_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abssec  } { \l_@@_ref_int }
    \zrefcheck_get_asint:nnn {#1} { zc@abschap } { \l_@@_lbl_b_int }
    \zrefcheck_get_asint:nnn {#2} { zc@abschap } { \l_@@_ref_b_int }
    \bool_lazy_and:nnTF
      { \l_@@_integer_bool }
      {
        \int_compare_p:nNn
          { \l_@@_lbl_b_int } = { \l_@@_ref_b_int } &&
        \int_compare_p:nNn
          { \l_@@_lbl_int } < { \l_@@_ref_int } &&
        ! \int_compare_p:nNn { \l_@@_lbl_int } = { 0 } &&
        ! \int_compare_p:nNn { \l_@@_ref_int } = { 0 }
      }
      { \group_insert_after:N \prg_return_true:  }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsubsection{Manual}
%
% \begin{macro}{ \@@_check_manual:nn }
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_check_manual:nn #1#2 { F }
  { \prg_return_false: }
%    \end{macrocode}
% \end{macro}
%
%
%
% \section{\pkg{zref-clever} integration}
%
%
% There are four tasks \pkg{zref-clever} needs to do, in order to offer
% integration with \pkg{zref-check} from the options of \cs{zcref}: i) set the
% ``beg label''; ii) set the checks options; iii) run the checks; iv)
% (possibly) set the ``end label''.  Since `ii)' can be done directly by
% running |\keys_set:nn { zref-check/zcheck }| on the options received, we
% provide convenience functions for the other three tasks.
%
%
% \begin{macro}
%   {
%     \zrefcheck_zcref_beg_label: ,
%     \zrefcheck_zcref_end_label_maybe: ,
%     \zrefcheck_zcref_run_checks_on_labels:n
%   }
%    \begin{macrocode}
\cs_new_protected:Npn \zrefcheck_zcref_beg_label:
  {
    \int_gincr:N \g_@@_id_int
    \tl_set:Ne \l_@@_checkbeg_tl
      { \@@_check_lblfmt:n { \g_@@_id_int } }
    \zref@labelbylist { \l_@@_checkbeg_tl } { zrefcheck-check }
  }
\cs_new_protected:Npn \zrefcheck_zcref_end_label_maybe:
  {
    \bool_if:NT \l_@@_zcheck_end_label_bool
      {
        \zref@labelbylist
          { \@@_end_lblfmt:n { \l_@@_checkbeg_tl } }
          { zrefcheck-end }
      }
  }
\cs_new_protected:Npn \zrefcheck_zcref_run_checks_on_labels:n #1
  {
    \@@_run_checks:nne
      { \l_@@_zcheck_checks_seq } {#1} { \l_@@_checkbeg_tl }
  }
%    \end{macrocode}
% \end{macro}
%
%
%
% \section{\pkg{zref-vario} integration}
%
%
% \begin{macro}
%   {
%     \zrefcheck_zrefvario_label: ,
%     \zrefcheck_zrefvario_run_check_on_label:n
%   }
%    \begin{macrocode}
\cs_new_protected:Npn \zrefcheck_zrefvario_label:
  {
    \int_gincr:N \g_@@_id_int
    \tl_set:Ne \l_@@_checkbeg_tl
      { \@@_check_lblfmt:n { \g_@@_id_int } }
    \zref@labelbylist { \l_@@_checkbeg_tl } { zrefcheck-zrefvario }
  }
\cs_new_protected:Npn \zrefcheck_zrefvario_run_check_on_label:nn #1#2
  { \@@_do_check:nnV {#1} {#2} \l_@@_checkbeg_tl }
\cs_generate_variant:Nn \zrefcheck_zrefvario_run_check_on_label:nn { Vn }
%    \end{macrocode}
% \end{macro}
%
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \PrintIndex
%