summaryrefslogtreecommitdiff
path: root/macros/generic/namedef/namedef.dtx
blob: 01e2cb8aec98f8d65927f69ee0e3a9db841e223a (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
% \iffalse meta-comment
% Copyright (C) 2019--2020
% by Phelype Oleinik <phelype.oleinik@latex-project.org>
% 
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.3c of this license
% or (at your option) any later version. The latest version of this
% license is in
% 
%     http://www.latex-project.org/lppl.txt
% 
% and version 1.3c or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% 
% This work is "maintained" (as per the LPPL maintenance status)
% by Phelype Oleinik.
% 
%<*package>
\def\namedefDate{2020-06-20}
\def\namedefVersion{1.0}
%</package>
%<*driver>
\documentclass[a4paper,full,inline]{l3doc}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{namedef}
\usepackage{bookmark}
\usepackage{xcolor}

\def\TODO#1#{\begingroup\afterassignment\TODODO\let\tmp = }
\def\TODODO{\setbox0=\vbox\bgroup\aftergroup\ENDTODO}
\def\ENDTODO{\endgroup\ignorespaces}

\begingroup
  \lccode`~=`\%
  \lowercase{\endgroup
  \def\VRBCodes{^^A
    \catcode`~=13
    \def~{\%\global\linenofalse}}
}
\makeatletter
\define@key{FV}{labelformat}{%
  \def\FV@LabelFormat{#1}%
  \ifx\FV@LabelFormat\FV@None
    \let\FV@LabelFormat\relax
  \fi}
\def\FV@Label@ii[#1]#2\@nil{%
  \def\@tempa{#1}%
  \ifx\@tempa\empty
    \def\FV@LabelBegin{\FV@LabelFormat#2}%
  \else
    \def\FV@LabelBegin{\FV@LabelFormat#1}%
    \def\FV@LabelPositionBottomLine{\@ne}%
  \fi
  \def\FV@LabelEnd{\FV@LabelFormat#2}}
\makeatother
\DefineVerbatimEnvironment%
  {example}{Verbatim}
  {
    gobble=3,
    frame=single,
    commandchars=|<>,
    codes={\VRBCodes},
    numbers=left,
    labelformat=\rmfamily\bfseries,
  }
\def\mshow#1{> \string#1=\meaning#1}
\newif\iflineno\linenotrue%\fi
\def\theFancyVerbLine{^^A
  \rmfamily
  \scriptsize
  \color{gray}^^A
  \iflineno
    \arabic{FancyVerbLine}^^A
  \else
    \global\addtocounter{FancyVerbLine}{-1}^^A
  \fi
  \global\linenotrue
}
\pdfstringdefDisableCommands{\let\footnotemark\relax}

% Substitute lmtt/it by lmtt/sl
\DeclareFontShape{T1}{lmtt}{lc}{it}{<->ssub*lmtt/lc/sl}{}

\begin{document}
  \overfullrule=5pt
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{\jobname.sty}
%
% \title{^^A
%   The \pkg{namedef} package\\
%   Named parameters in \TeX{} definitions^^A
%   \thanks{This file has version number \namedefVersion,
%           last revised \namedefDate.}
% }
%
% \author{^^A
%  Phelype H. Oleinik\thanks
%    {^^A
%      E-mail:
%        \href{mailto:phelype.oleinik@latex-project.org}
%          {phelype.oleinik\meta{at}latex-project.org}^^A
%    }^^A
% }
%
% \date{Released \namedefDate}
%
% \maketitle
%
% \begin{documentation}
%
% \section{Introduction}
%
% This package provides a somewhat dubious, however useful way to make
% \TeX{} definitions using \cs{def} and the like. The usual way to
% define a ``hello world'' macro is |\def\hello#1{Hello #1!}|. Sure,
% this is easy (for people using this package, at least), but sometimes
% I stumbled upon the case where I had a macro defined with something
% like |\def\macro#1#2#3#4#5#6{#6#1#4#3#5#2}| and then I had to, for
% whatever reason, add another argument before |#1|. Oh, the pain. But
% OK, occupational hazard. And then I change my mind and needed to
% revert.  This package provides a way to give a semantic meaning to the
% arguments of a definition such that it becomes clearer in meaning and
% easier to change when an argument's identifier defines what it means
% rather than its position in a list.
%
% \subsection{Features}
%
% This package defines a macro, \cs{named}, which acts like a prefix for
% \cs{def} (the same way as \cs{long} and the like), and allows one to
% rewrite the \cs{hello} macro like this:
% |\named\def\hello#[who]{Hello #[who]!}|. Sure, a macro with one
% argument won't see much benefit from it, but as soon as that number
% increases, a better description of the arguments comes in handy.
%
% The package also defines a macro \cs{NamedDelim} which allows the user
% to change the delimiter of the named parameters to their liking. For
% example, after |\NamedDelim //| the example above changes to:
% |\named\def\hello#/who/{Hello #/who/!}|. The default is
% |\NamedDelim []|.
%
% The only dependency of this package is the \LaTeX3 programming layer,
% \pkg{expl3}, thus this package works in any format and engine
% supported by \pkg{expl3}. In \LaTeXe, load it with
% |\usepackage{namedef}|, and in other formats with
% |\input namedef.sty|.
%
% \subsection{Feedback}
%
% Bugs, feature requests, or suggestions are most welcome.  You can send
% them by e-mail or to the source code repository at
% \url{https://github.com/PhelypeOleinik/namedef}.
%
% \section{Usage}
%
% \begin{function}{\named}
%   \begin{syntax}
%     \cs{named}\meta{other~prefixes}\meta{\cs[no-index]{(e,g,x)def}}^^A
%\meta{parameter text}\marg{replacement text}
%   \end{syntax}
%   The \cs{named} macro will grab the \meta{other~prefixes}, the
%   \cs{def} command, the \meta{parameter~text} and
%   \meta{replacement~text}, and will replace every ocurrence of
%   |#[|\meta{text}|]| by a suitable |#|\meta{number} for \TeX{} to
%   carry on with the definition.
%
%   The \meta{other~prefixes} can be any combination of \cs{long},
%   \cs{outer}, \cs{global}, and \cs{protected}. As for \TeX, their
%   relative order doesn't matter, except that \cs{named} must appear
%   \emph{before} any \cs{global} prefix.  Other prefixes are detected
%   whether placed before or after \cs{named}.
%
%   The \cs{def} command can be anything. The package will not check the
%   validity of the command, it will simply drag the token along until
%   the point where the definition is made. This allows one to use any
%   of \TeX's primitive \cs{def} commands as well as, for instance,
%   \pkg{expl3}'s \cs{cs_new:Npn} or the like. However \cs{named} will
%   not work with \LaTeX's \cs{newcommand} or \pkg{xparse}'s
%   \cs{NewDocumentCommand} and the like because their syntax is
%   completely different.
%
%   The \meta{parameter~text} is the same \meta{parameter~text} that
%   would be used if the \cs{named} prefix wasn't used with the
%   difference that all |#|\meta{number} must be replaced by a
%   |#[|\meta{name}|]|. The characters |[| and |]| are mandatory and the
%   character |]| cannot appear in \meta{name}, however |[| and |]| can
%   still appear in the \meta{parameter~text} without restriction; the
%   special meaning is only applied after a parameter token (|#|).
%   \meta{name} can be anything that when fully expanded returns a
%   string of characters (anything valid in |\csname|\dots|\endcsname|).
%   The \meta{parameter~text} cannot, however, contain numbered
%   parameters anymore (|#1|,~|#2|,~\dots).
%   \TODO{This is not enforced... What should be done in this case?}
%
%   The \meta{replacement~text} is also the same \meta{replacement~text}
%   that would be used otherwise, with all |#|\meta{number} replaced by
%   |#[|\meta{name}|]|.
% \end{function}
%
% \begin{function}{\NamedDelim,\globalNamedDelim}
%   \begin{syntax}
%     \cs{NamedDelim}\meta{begin-token}\meta{end-token}
%     \cs{globalNamedDelim}\meta{begin-token}\meta{end-token}
%   \end{syntax}
%   These macros change the delimiter of the named parameters from the
%   default |#[|\meta{name}|]| to
%   |#|\meta{begin-token}\meta{name}\meta{end-token}. Both delimiters
%   must be one single non-macro token.  Valid delimiters are character
%   tokens with catcode $3$, $4$, $7$, $8$, $11$, $12$, or $13$
%   (see section~\ref{sec:delimiters}).
%
%   \cs{globalNamedDelim} is the same as \cs{NamedDelim} except that the
%   effect of the former has global scope, while the latter is local to
%   a group. While you can use both, you should be careful not to
%   interleave them too much or you might exhaust \TeX's save stack.
%
%   Delimiters are matched based on their character code (using \cs{if})
%   so changes in their category code doesn't matter as long as that
%   change doesn't prevent the character from becoming a token or the
%   category code isn't \enquote{too special} :-) \quad (see above).
%
%   The choice of delimiter is mostly ``what you like better''. Neither
%   the delimter tokens nor the name of the parameter make it to the
%   macro itself. They are just code-level abstractions. Thus the
%   delimiter should be something that the person writing the code can
%   easily distinguish from the rest of the code. For example, the code
%   |\NamedDelim xz \named\def\test#xyz{xyz#xyzxyz}| works, but its
%   readability is questionable.
% \end{function}
%
% \subsection{Limitations}
%
% As already stated the command does not work (and I don't intend to
% make it work) with \LaTeXe's \cs{newcommand} and its family because
% a) the argument specification is by the number of arguments, so you
% can't ``declare'' them as with \cs{def}, and b) because it's supposed
% to be used for user-level interfaces, which usually (and preferably)
% have a low argument count, so numbering shouldn't be a problem.
% That said, see section~\ref{sec:newcommand}.
%
% For \pkg{xparse}'s \cs{NewDocumentCommand} the situation is the same.
% Other than these, \cs{named} should work for whatever type of
% definition that uses \TeX's \cs{def} syntax.
%
% Another limitation that I'd like to change in a future release (but
% still don't know the best way to make the interface) is to support
% definition commands which go beyond the 
% \meta{parameter~text}|{|\meta{replacement~text}|}| syntax. For
% instance, in \pkg{expl3} a conditional that checks whether its
% argument is negative can be defined like this (for integers, of
% course):
% \begin{Verbatim}
%   \prg_new_conditional:Npnn \namedef_if_negative:n #1 { T, F, TF }
%     {
%       \if_int_compare:w #1 < 0
%         \prg_return_true:
%       \else:
%         \prg_return_false:
%       \fi:
%     }
% \end{Verbatim}
% And if one tried to use \cs{named} in that definition it would fail
% because this command takes one extra argument before the
% \meta{replacement~text}. Something could be done to allow one (or
% more) extra argument before the \meta{replacement~text}.
%
% \medskip
% One serious limitation is when used with definitions that expand their
% argument, namely \cs{edef} and \cs{xdef}. This type of definition
% expands tokens as it reads them, possibly changing their meaning
% during the process. \cs{named}, however, first grabs the definition as
% argument to process the named arguments before actually performing the
% definition, so these ``unexpected'' changes of meaning might make the
% code misbehave. While writing this manual I could think of two (and a
% half) situations which will be problematic and how to work around them
% (sorry, no solution for now; suggestions are welcome :).
%
%^^A \begingroup
% \edef\hash{\string#}
% \edef\¿{\csname @gobble\expandafter\endcsname\string\\}
% \def\nedeft{\ttfamily\¿named\¿edef\¿test}
%
% \subsubsection{\nedeft\{\¿string\#[arg]\}}
% \label{sec:edef wa1}
%
% The normal (no \cs{named}) counterpart of this one is a perfectly
% valid definition: |\edef\test{\string#1}|. While expanding the
% \meta{replacement~text}, \cs{string} turns the parameter token |#|$_6$
% into a character |#|$_{12}$, thus defining |\test| to expand to the
% two characters |#1|. When using \cs{named}, however, the replacement
% routine doesn't know what \cs{string} does to the token next to it, so
% it goes on and treats |#[arg]| as one named argument only to find out
% that it was never defined in the \meta{parameter~text}, so it aborts
% the definition with an error.
%
% This will occur in the specific case where the user wishes to have the
% macro expand to the characters |#[arg]|, without replacement by a
% parameter. In this case the work-around is to temporarily switch the
% delimiter tokens of \cs{named}'s scanner:
% \begin{Verbatim}
%   \NamedDelim||
%   \named\edef\test{\string#[arg]}
%   \NamedDelim[]
% \end{Verbatim}
% in which case the scanner will still see the |#| as a parameter token
% but since it is no longer followed by a delimiter, it will be simply
% passed on to the definition. Afterwards, at the time \TeX{} tries to
% carry on with the definition, \cs{string} will do its thing to |#|
% without further problems.
%
% \subsubsection{\nedeft\#[arg]\{\¿string\}\#[arg]\}}
% \label{sec:edef wa2}
%
% This one, as the previous, works fine without \cs{named}:
% |\edef\test#1{\string}#1}|. Again, when \TeX{} scans this definition,
% it will expand \cs{string} which will turn the end group token |}|$_2$
% into a character |}|$_{12}$, which will have \TeX{} end the definition
% with the next~|}|$_2$, after the |#1|. This only works because \TeX{}
% does not grab the whole thing as argument before expanding. Which is
% precisely what \cs{named} does.
%
% When \cs{named} grabs the definition as argument the first |}|
% ends the \meta{replacement~text}, so what \cs{named} effectively sees
% is |\edef\test#[arg]{\string}|, which is then processed (|#[arg]| is
% replaced by |#1|) and left back in the input stream for \TeX{} to do
% its thing, however the replacement |#[arg]| is never replaced:
% |\edef\test#1{\string}#[arg]}|, then when \TeX{} tries to do the
% definition it will complain about an ``! Illegal parameter number in
% definition of |\test|.''
%
% The work-around in this case is to do a dirty brace trick to lure
% \cs{named} into grabbing the whole thing as argument, but which will
% disappear at the time \TeX{} performs the expansion and definition.
% One option is |\iffalse{\fi|:
% \begin{Verbatim}
%   \named\edef\test#[arg]{%
%     \iffalse{\fi \string}#[arg]}
% \end{Verbatim}
% In this example \cs{named} will process everything, but at the time of
% the definition the entire |\iffalse{\fi| thing expands to nothing and
% the final definition becomes the same as the one without \cs{named}.
% The |\iffalse{\fi| block can also be left in the definition
% \emph{without} named and the result will be the same. One could argue
% that using the brace hack is safer because it doesn't change the
% definition, yet avoid problems when grabbing the definition as
% argument in a general situation.
%
% \let\SVsubsec\thesubsubsection
% \def\thesubsubsection{^^A
%   \thesubsection .\number\numexpr\value{subsubsection}-1,5}
%
% \subsubsection{\nedeft\#[arg]\{\¿string\{\#[arg]\}}
% \label{sec:edef wa3}
%
% \let\thesubsubsection\SVsubsec
%^^A \endgroup
%
% This is rather similar to the previous one, except that the brace
% later-to-be-detokenized begins a group: |\edef\test#1{\string{#1}|.
% Here \TeX{} also expands \cs{string} and makes |{|$_1$ a |{|$_{12}$
% which does not count in the brace balancing. \cs{named}, however, will
% count it when grabbing the definition as argument and will require one
% more |}|$_2$. If the code is run as is \TeX{} will probably report a
% ``File ended while scanning use of \dots'' error unless there happens
% to be a matching |}|$_2$ somewhere else, in which case the definition
% will just go wrong. The work-around here is the same as the one
% before, with |}| instead:
% \begin{Verbatim}
%   \named\edef\test#[arg]{%
%     \string{#[arg]\iffalse}\fi}
% \end{Verbatim}
% This will ensure that \cs{named} will see the |}| it needs to grab the
% definition correctly and will disappear once the definition is done.
%
% \subsection{Invalid delimiters}
% \label{sec:delimiters}
%
% The delimiters that can be used should be character tokens with
% catcode $3$, $4$, $7$, $8$, $11$, $12$, or $13$.  Characters with
% catcode $0$, $5$, $9$, $14$, and $15$ don't produce tokens to start
% with, so they can't possibly be used.  The remaining category codes
% are currently disallowed in the code because they make the input
% ambiguous or because they make the implementation more complex with no
% real advantage.
%
% Catcodes $1$ and $2$ (begin-/end-group) cannot be used because they
% become indistinguishable from the braces that delimit the
% \meta{parameter~text} of the definition, so the input is ambiguous.
%
% Catcode $6$ (macro parameter) cannot be used because it gets hard to
% distinguish a named parameter from some text surrounded by parameter
% tokens. For example in: |\named\edef\foo#name#{\string#then#name#}|,
% \pkg{namedef} would raise an error on |#then#| (unknown parameter)
% without knowing that the first |#|$_{6}$ becomes |#|$_{12}$ and the
% actual parameter is |#name#|\dots Or is it? I'm not entirely convinced
% of my own argument, so this might be implemented in the future.
%
% Catcode $10$ (blank space) is possible but it requires a hanful of
% extra precautions to avoid losing the space when passing arguments
% around.  Since it makes for a strange-looking syntax (our eyes are
% trained to ignore spaces), this is not supported.
%
% \section{Boring examples}
%
% The following examples show two definitions each, which are the same,
% but the second uses \cs{named}. The third line in each example shows
% the \cs{meaning} of the defined macro.
%
% First the basics, replacing a numbered parameter by a named one:
%   \named\def\hello#[who]{Hello #[who]!}
% \begin{example}[label=Basics]
%         \def\hello#1{Hello #1!}
%   \named\def\hello#[who]{Hello #[who]!}
%   |mshow|hello
% \end{example}
% \smallskip
%
% Prefixes can be added at will after the \cs{named} prefix:
%   \named\protected\long\edef\hello#[who]{Hello #[who]!}
% \begin{example}[label=Prefixes]
%         \protected\long\edef\hello#1{Hello #1!}
%   \named\protected\long\edef\hello#[who]{Hello #[who]!}
%   |mshow|hello
% \end{example}
% \smallskip
%
%^^A Whatever prefixes put before \cs{named} will be lost. Notice that
%^^A even though \cs{protected} is used, the macro itself is not defined
%^^A with \cs{protected}:
%^^A   \protected\named\long\edef\hello#[who]{Hello #[who]!}
%^^A \begin{example}[label=Wrong Prefixes]
%^^A   \protected      \long\edef\hello#1{Hello #1!}
%^^A   \protected\named\long\edef\hello#[who]{Hello #[who]!}
%^^A   |mshow|hello
%^^A \end{example}
%^^A \smallskip
%
% This example is just to show that the named argument delimiter doesn't
% interfere with the text in the macro:
%   \named\def\hello[#[who]]{Hello #[who]!}
% \begin{example}[label=Argument delimited by \texttt{[} and \texttt{]}]
%         \def\hello[#1]{Hello #1!}
%   \named\def\hello[#[who]]{Hello #[who]!}
%   |mshow|hello
% \end{example}
% \smallskip
%
% However, for readability, the delimiter can be changed to something
% else:
%   \named\def\hello[#[who]]{Hello #[who]!}
% \begin{example}[label=Argument delimited by \texttt{[} and \texttt{]},
%   commandchars=/<>,firstnumber=0]
%   \NamedDelim{|}{|}
%         \def\hello[#1]{Hello #1!}
%   \named\def\hello[#|who|]{Hello #|who|!}
%   /mshow/hello
% \end{example}
% \smallskip
%
% This example demonstrates multiple arguments and arbitrary
% \meta{parameter~text}:
%   \named\def\cfbox #[color] to #[width]#[content]{%
%           \fbox{\hbox to #[width]{\color{#[color]}#[content]}}}
% \begin{example}[label=More arguments]
%         \def\cfbox #1 to #2#3{\fbox{\hbox to #2{\color{#1}#3}}}
%   \named\def\cfbox #[color] to #[width]#[content]{%
%           \fbox{\hbox to #[width]{\color{#[color]}#[content]}}}
%   |mshow|cfbox
% \end{example}
% \smallskip
%
% \TeX's weird |#{| argument can be used as well:
%   \named\def\cbox #[color] to #[width]#{^^A
%      \hbox to #[width]\bgroup\color{#[color]}\let\next= }
% \begin{example}[label=Weird arguments]
%         \def\cbox #1 to #2#{\hbox to #2\bgroup\color{#1}\let\next= }
%   \named\def\cbox #[color] to #[width]#{%
%           \hbox to #[width]\bgroup\color{#[color]}\let\next= }
%   |mshow|cbox
% \end{example}
% \smallskip
%
%  {\NamedDelim XX
%   \named\xdef\test{\string#[arg]}}
% \begin{example}[commandchars=/<>, label=
%   \texttt{\string\edef} workaround \ref{sec:edef wa1}]
%         \edef\test{\string#[arg]}
%   \NamedDelim || /global/linenofalse
%   \named\edef\test{\string#[arg]} /global/linenofalse
%   \NamedDelim []
%   /mshow/test
% \end{example}
% \smallskip
%
%   \named\edef\test#[arg]{\iffalse{\fi\string}#[arg]}
% \begin{example}[label=
%   \texttt{\string\edef} workaround \ref{sec:edef wa2}]
%         \edef\test#1{\string}#1}
%   \named\edef\test#[arg]{\iffalse{\fi\string}#[arg]}
%   |mshow|test
% \end{example}
% \smallskip
%
%   \named\edef\test#[arg]{\string{#[arg]\iffalse}\fi}
% \begin{example}[label=
%   \texttt{\string\edef} workaround \ref{sec:edef wa3}]
%         \edef\test#1{\string{#1}
%   \named\edef\test#[arg]{\string{#[arg]\iffalse}\fi}
%   |mshow|test
% \end{example}
% \smallskip
%
% \section{Interesting\protect\footnotemark\ examples}
% \footnotetext{Terms and Conditions may apply.}
%
% These examples shows a few more elaborate ways to use \pkg{namedef}.
%
% \subsection{Extended \cs{newcommand}}
% \label{sec:newcommand}
%
% Here's an implementation to allow the syntax of \pkg{namedef} in
% \cs{newcommand}. It uses \pkg{xparse} to handle optional arguments,
% and uses \cs{newcommand} itself to define (possibly) optional argument
% handling, so the resulting command uses \LaTeXe's command parsing
% machinery.
%
% The syntax of the defined command is:
%
% |\newnamedcommand|\meta{*}\cs[no-index]{\meta{cmd}}\oarg{arg-list}^^A
%   [\meta{opt}=\meta{default}]\marg{definition}
%
% \noindent where everything is the same as in regular \cs{newcommand},
% except for the optional arguments \meta{arg-list} and
% \meta{opt}=\meta{default}.  \meta{arg-list} should be a
% comma-separated list of named parameters as \cs{named} would expect,
% and \meta{opt} is a named parameter, and \meta{default} is its default
% value.  The usage would be something like:
%
% |\newnamedcommand\foo[#[one],#[two]][#[opt]=default]%|\par
% \quad|{#[one], #[two], and #[opt]}|
%
% \noindent which translates to:
%
% |\newcommand\foo[3][default]%|\par
% \quad|{#2, #3, and #1}|
%
% First, load \pkg{xparse} and \pkg{namedef}, and define the top-level
% commands to use a common |\NNC_newcommand:NnNnnn|.
% |\NNC_newcommand:NnNnnn| will store the mandatory arguments in
% a \texttt{seq} variable for easier access, then call
% |\__NNC_newcommand:NnNn| to do the \cs{newcommand} part of the job,
% and |\__NNC_named_def:nNnn| to \cs{named} part.
% |\new...|, |\renew...|, and |\provide...| versions are defined, but
% since a \cs{def} is used later with no checking, the behaviour is
% not exactly the same as you'd get with \cs{newcommand} in this regard.
%
% \begin{example}
%   \usepackage{namedef}
%   \usepackage{xparse}
%   \ExplSyntaxOn
%   \seq_new:N \l__NNC_args_seq
%   \scan_new:N \s__NNC
%   \NewDocumentCommand \newnamedcommand { s m o o m }
%     { \NNC_newcommand:NnNnnn \newcommand {#1} #2 {#3} {#4} {#5} }
%   \NewDocumentCommand \renewnamedcommand { s m o o m }
%     { \NNC_newcommand:NnNnnn \renewcommand {#1} #2 {#3} {#4} {#5} }
%   \NewDocumentCommand \providenamedcommand { s m o o m }
%     { \NNC_newcommand:NnNnnn \providecommand {#1} #2 {#3} {#4} {#5} }
%   \named \cs_new_protected:Npn \NNC_newcommand:NnNnnn
%       #[newcmd] #[star] #[cmd] #[args] #[opt] #[defn]
%     {
%       \seq_clear:N \l__NNC_args_seq
%       \IfValueT {#[args]}
%         { \seq_set_from_clist:Nn \l__NNC_args_seq {#[args]} }
%       \__NNC_newcommand:NnNn #[newcmd] {#[star]} #[cmd] {#[opt]}
%       \__NNC_named_def:nNnn {#[star]} #[cmd] {#[opt]} {#[defn]}
%     }
% \end{example}
%
% |\__NNC_newcommand:NnNn| does the \cs{newcommand} part of the job. It
% takes the arguments read in by \pkg{xparse}, and translates them into
% the \cs{newcommand} syntax. The number of items in the |#[args]|
% parameter is counted and left within brackets, and the default value
% of the optional argument is also left within another pair of brackets.
% This step is executed with an empty definition because the named
% parameters will cause havoc in \cs{newcommand}.
%
% \begin{example}[firstnumber=last]
%   \named \cs_new_protected:Npn \__NNC_newcommand:NnNn
%       #[newcmd] #[star] #[cmd] #[opt]
%     {
%       \use:x
%         {
%           \exp_not:N #[newcmd]
%           \IfBooleanT {#[star]} { * }
%           \exp_not:N #[cmd]
%           \seq_if_empty:NF \l__NNC_args_seq
%             { [ \seq_count:N \l__NNC_args_seq ] }
%           \IfValueT {#[opt]} { [ \__NNC_opt_value:w #[opt] \s__NNC ] }
%             { }
%         }
%     }
%   \cs_new:Npn \__NNC_opt_value:w #1 = #2 \s__NNC {#2}
% \end{example}
%
% Now |\__NNC_named_def:nNnn| will do the \cs{named} part.
% First the |#[star]| argument (if not present) becomes \cs{long}, and
% then comes \cs{named} and \cs{def}.  Then, if an optional argument was
% given, the command we need to define is |\\foo| rather than |\foo|, so
% use take care of that with \cs{token_to_str:N}, and then leave the
% named parameter given for the optional argument within brackets.
% If there's no optional argument, we just define |#[cmd]| (pretty
% boring).  Then we call \cs{seq_use:Nn} on the mandatory arguments to
% lay them flat for \cs{named}, and then the parameter text (|#[defn]|),
% unexpanded.
%
% \begin{example}[firstnumber=last]
%   \named \cs_new_protected:Npn \__NNC_named_def:nNnn
%       #[star] #[cmd] #[opt] #[defn]
%     {
%       \use:x
%         {
%           \IfBooleanF {#[star]} { \long }
%           \named \def
%           \IfValueTF {#[opt]}
%             {
%               \exp_not:c { \token_to_str:N #[cmd] }
%                 [ \exp_not:o { \__NNC_opt_name:w #[opt] \s__NNC } ]
%             }
%             { \exp_not:N #[cmd] }
%           \seq_use:Nn \l__NNC_args_seq { }
%           { \exp_not:n {#[defn]} }
%         }
%     }
%   \cs_new:Npn \__NNC_opt_name:w #1 = #2 \s__NNC {#1}
%   \ExplSyntaxOff
% \end{example}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{namedef} Implementation}
%
%    \begin{macrocode}
%<*package>
%<@@=namedef>
%    \end{macrocode}
%
% \subsection{Loading}
%
% \begin{macro}{\@@_end_package_hook:}
% Load \pkg{expl3}, either through \cs{RequirePackage} or through
% inputting the generic loader, depending on the format in use
% (copied from Bruno Le Floch's \pkg{gtl}).
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname RequirePackage\endcsname\relax
  \input expl3-generic.tex
\else
  \RequirePackage{expl3}[2018-05-15]
\fi
\ExplSyntaxOn
\cs_if_exist:NTF \ProvidesExplPackage
  {
    \cs_new_eq:NN \@@_end_package_hook: \prg_do_nothing:
    \ExplSyntaxOff
    \ProvidesExplPackage
  }
  {
    \cs_new_eq:NN \@@_end_package_hook: \ExplSyntaxOff
    \group_begin:
    \ExplSyntaxOff
    \cs_set_protected:Npn \@@_tmp:w #1#2#3#4
      {
        \group_end:
        \tl_gset:cx { ver @ #1 . sty } { #2 ~ v#3 ~ #4 }
        \cs_if_exist_use:NF \wlog { \iow_log:x }
          { Package: ~ #1 ~ #2 ~ v#3 ~ #4 }
      }
    \@@_tmp:w
  }
      {namedef} {\namedefDate} {\namedefVersion}
      {Named parameters in TeX definitions (PHO)}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Declarations}
%
% \begin{macro}{\flag @@_parm_count}
%   A flag (mis)used as a counter to keep track of the parameter number.
%    \begin{macrocode}
\flag_new:n { @@_parm_count }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c_@@_prefix_tl}
%   A prefix to use as name space for temporary macros.
%    \begin{macrocode}
\tl_const:Nn \c_@@_prefix_tl { namedef~parm~-> }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_@@_macro_tl}
%   A token list to store the name of the macro meing defined for error
%   messages.
%    \begin{macrocode}
\tl_new:N \l_@@_macro_tl
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\q_@@_mark,\q_@@_stop}
%   Quarks used throughout the package.
%    \begin{macrocode}
\quark_new:N \q_@@_mark
\quark_new:N \q_@@_stop
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\s_@@}
%   Scan mark used to skip code.
%    \begin{macrocode}
\scan_new:N \s_@@
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_skip_to_scan_mark:w, \@@_skip_to_scan_mark:nw}
%   Consume everything up to \cs{s_@@}.
%    \begin{macrocode}
\cs_new:Npn \@@_skip_to_scan_mark:w  #1    \s_@@ {  }
\cs_new:Npn \@@_skip_to_scan_mark:nw #1 #2 \s_@@ {#1}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_tmp:w}
%   A scratch macro.
%    \begin{macrocode}
\cs_new_eq:NN \@@_tmp:w ?
%    \end{macrocode}
% \end{macro}
%
% \subsection{The top-level \cs{named} macro}
%
% \begin{macro}{\named}
% \begin{macro}{\@@_grab_prefix:nN}
%   Starts scanning ahead for prefixes and the definition
%   command. Once finished the scanning of prefixes, call
%   \cs{@@_replace_named:nNnn} to do the heavy lifting.
%    \begin{macrocode}
\cs_new_protected:Npn \named { \@@_grab_prefix:nN { } }
\cs_new_protected:Npn \@@_grab_prefix:nN #1 #2
  {
    \@@_if_prefix:NTF #2
      { \@@_grab_prefix:nN }
      { \@@_detect_prefixes:Nn \@@_kill_outer:nN }
    { #1#2 }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[pTF]{\@@_if_prefix:N}
%   Checks against a list of valid prefixes and returns |true| or
%   |false| accordingly.
%    \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_prefix:N #1 { TF }
  {
    \if_int_compare:w 0
        \if_meaning:w \tex_protected:D #1 1 \fi:
        \if_meaning:w \tex_global:D    #1 1 \fi:
        \if_meaning:w \tex_outer:D     #1 1 \fi:
        \if_meaning:w \tex_long:D      #1 1 \fi:
        \if_meaning:w \scan_stop:      #1 1 \fi:
        = 1 \exp_stop_f:
      \prg_return_true:
    \else:
      \prg_return_false:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_detect_prefixes:Nn}
% \begin{macro}[rEXP]{\@@_extract_prefixes:w}
% \begin{macro}[rEXP]{
%     \@@_extract_protected:n, \@@_extract_protected_aux:w,
%     \@@_extract_long:n,      \@@_extract_long_aux:w,
%     \@@_extract_outer:n,     \@@_extract_outer_aux:w,
%   }
%   Defines a scratch macro \cs{@@_tmp:w} and queries its prefixes, then
%   forwards them to the the next macro to perform the parameter
%   replacement and definition.
%
%   This code would be quite a lot simpler if \cs{outer} didn't exist.
%   First extract the meaning of \cs{@@_tmp:w}, and pass the prefixes
%   (before ``|macro:|'') to \cs{@@_extract_prefixes:w}, and then to
%   \cs{@@_extract_protected:n}, \cs{@@_extract_long:n}, and
%   \cs{@@_extract_outer:n} in turn to check if each of these prefixes
%   is there.
%
%   \cs{global} can't be checked this way because it's different from
%   other prefixes in the sense that it affects the definition \emph{at
%   the time} of the definition, rather than at the time it is used.
%   I don't know if it's possible to detect a \cs{global} after it's
%   already consumed by \TeX.
%
%   \TODO{\cs{afterassignment} tokens are lost.  It would be nice if
%     it were possible to retrieve it and save until later.}
%    \begin{macrocode}
\cs_new_protected:Npn \@@_detect_prefixes:Nn #1 #2
  {
    \cs_set_nopar:Npn \@@_tmp:w { }
    \use:x
      {
        \exp_not:N #1
          {
            \exp_after:wN \exp_after:wN
              \exp_after:wN \@@_extract_prefixes:w
                \exp_after:wN \token_to_meaning:N
                  \cs:w @@_tmp:w \cs_end: \s_@@
            \exp_not:n {#2}
          }
      }
  }
\use:x
  {
    \cs_new:Npn \exp_not:N \@@_extract_prefixes:w ##1
        \tl_to_str:n { macro: } ##2 \s_@@
      {
        \exp_not:N \@@_extract_protected:n {##1}
        \exp_not:N \@@_extract_long:n {##1}
        \exp_not:N \@@_extract_outer:n {##1}
      }
  }
\cs_set_protected:Npn \@@_tmp:w #1 #2
  {
    \use:x
      {
        \cs_new:cpn { @@_extract_#1:n } ####1
          {
            \exp_not:c { @@_extract_#1_aux:w } ####1
              \token_to_str:N #2 \scan_stop: \token_to_str:N #2 \s_@@
          }
        \cs_new:cpn { @@_extract_#1_aux:w } ####1
            \token_to_str:N #2 ####2 \token_to_str:N #2 ####3 \s_@@
          {
            \exp_not:N \if_meaning:w \scan_stop: ####2
            \exp_not:N \else:
              \exp_not:c { tex_#1:D }
            \exp_not:N \fi:
          }
      }
  }
\@@_tmp:w { protected } { \protected }
\@@_tmp:w { long } { \long }
\@@_tmp:w { outer } { \outer }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Main routine}
%
% \begin{macro}{\@@_kill_outer:nN}
% \begin{macro}{\@@_start:nNp, \@@_replace_named:nNnn}
% \begin{macro}{\@@_replace_parameter:Nn, \@@_parameter_output:nnw}
% \begin{macro}{\@@_handle_parameter:nN}
% \begin{macro}{\@@_define:nnnN}
%   Here we play dirty: abuse the fact that \cs{exp_not:N} temporarily
%   makes the \cs{noexpand}ed control sequence temporarily equal to
%   \cs{relax}.  But rather than using it in an \cs{edef} or whatnot,
%   hit \cs{exp_not:N} with \cs{exp_after:wN}, and then quickly grab
%   it with \cs{@@_start:nNp}, so it's safe to grab it, even if it's
%   \cs{outer}.  If that wasn't bad enough, do it once again to make
%   it equal to \cs{relax} in the scope of \cs{@@_replace_named:nNnn}
%   so that it doesn't blow up.
%
%   \TODO{Currently |\named\def{\foo}{bar}| works, but perhaps it
%     shouldn't.}
%    \begin{macrocode}
\cs_new_protected:Npn \@@_kill_outer:nN #1
  {
    \cs_set:Npn \@@_tmp:w { \@@_start:nNp {#1} }
    \exp_after:wN \@@_tmp:w \exp_not:N
  }
\cs_new_protected:Npn \@@_start:nNp #1 #2 #3 #
  {
    \group_begin:
      \int_set:Nn \tex_escapechar:D { `\\ }
      \exp_after:wN \cs_set_eq:NN \exp_not:N #2 \scan_stop:
      \@@_replace_named:nNnn {#1} #2 {#3}
  }
%    \end{macrocode}
%
%   Here the actual replacement of named parameters by numbered ones
%   takes place. A group is started to revert the flag and all the
%   defined temporary macros.
%
%   \cs{@@_replace_parameter:Nn} \cs{@@_in_parameter:nN} starts
%   replacing a dummy macro in the generic parameter replacement routine
%   by the macro which counts the parameters and aliases the named
%   parameters with numbered ones. Finally it starts
%   \cs{@@_replace_parm:Nn}, which scans the \meta{parameter~text} for
%   the named parameters and replaces them by numbered ones. The second
%   output argument of \cs{@@_replace_parm:Nn} is a list of definitions
%   which assign a number to each named parameter so that they can be
%   used in the next step.
%
%   \cs{@@_replace_parameter:Nn} \cs{@@_in_replacement:nN} then starts
%   by replacing the same dummy macro by one which will replace the
%   named parameter by its number. Again \cs{@@_replace_parm:Nn} is
%   started, and its output is the already-processed part of the
%   \meta{replacement~text}.
%
%   The output of both steps is inserted after \cs{@@_define:nnnN} (it's
%   missing two arguments in the definition of
%   \cs{@@_replace_named:nNnn}). After all that is done, all the named
%   parameters were replaced by numbered ones, so \cs{@@_define:nnnN}
%   can do its thing.
%
%   A final quark is put in the input stream for recovery from errors.
%   In a successful run this quark is removed by \cs{@@_define:nnnN}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replace_named:nNnn #1 #2 #3 #4
  {
      \tl_set:Nx \l_@@_macro_tl { \token_to_str:N #2 }
      \@@_replace_parameter:Nn \@@_in_parameter:nN {#3}
      \@@_replace_parameter:Nn \@@_in_replacement:nN {#4}
      \@@_define:nnnN     {#1} #2
      \s_@@
  }
\cs_new_protected:Npn \@@_define:nnnN #1 #2 #3 #4
  {
    \group_end:
    #3#4#2{#1}
  }
\cs_new_protected:Npn \@@_replace_parameter:Nn #1 #2
  {
    \cs_set_eq:NN \@@_handle_parameter:nN #1
    \@@_replace_parm:Nn \@@_parameter_output:nnw {#2}
  }
\cs_new_eq:NN \@@_handle_parameter:nN ?
\cs_new_protected:Npn \@@_parameter_output:nnw #1 #2
    #3 \@@_define:nnnN
  { #2 #3 \@@_define:nnnN {#1} }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_in_parameter:nN, \@@_in_replacement:nN}
%   These two functions handle the named parameters when they are found
%   in the \meta{parameter~text} and \meta{replacement~text},
%   respectively.
%
%   \cs{@@_in_parameter:nN} checks if the named parameter already exists
%   (with \cs{relax} being |true|) and, in such case, throws an error
%   and inserts the number already set for that named parameter.
%   Otherwise the parameter is \cs{let} to
%   \cs{relax} so that if it is found later an error is issued. Setting
%   a macro to \cs{relax} is an expandable way to define it (the same
%   approach as in \pkg{l3flag}). After that,
%   the \cs{flag @@_parm_count} is raised once and its height is used as
%   the parameter number. The current parameter tokens and the parameter
%   number are flushed to the first (left) output slot, and a definition
%   |\cs_set:cpn {|\cs{c_@@_prefix_tl}~\meta{name}|} {|\meta{number}|}|
%   is appended to the second (right) output slot so that the names can
%   be used in the \meta{replacement~text}.
%
%   In case of a repeated parameter it is tricky to do anything
%   sensible.  In a normal definition, when \TeX{} sees a repeated
%   parameter number (like in |\def\foo#1#1{}|) it just uses the wrong
%   number to a parameter number not yet taken, or ignores the parameter
%   if there's already nine before that.  However here we can't guess
%   the name of the next parameter, so we can't do much.  The easiest
%   way out is to just use the same parameter number as before and go
%   on with out job:  at the end, \TeX{} will complain again about this.
%    \begin{macrocode}
\cs_new:Npn \@@_in_parameter:nN #1
  {
    \if_cs_exist:w \c_@@_prefix_tl #1 \cs_end:
      \exp_after:wN \use_i:nn
    \else:
      \exp_after:wN \use_ii:nn
    \fi:
      { \msg_expandable_error:nnn { namedef } { repeated-parm } {#1} }
      {
        \exp_after:wN \use_none:n \cs:w \c_@@_prefix_tl #1 \cs_end:
        \flag_raise:n { @@_parm_count }
      }
    \exp_args:Nf \@@_append_output:nnNwnn
      { \flag_height:n { @@_parm_count } }
      {#1}
  }
%    \end{macrocode}
%
%   \cs{@@_in_replacement:nN} also checks if the named parameter exists,
%   however now it will be \emph{not} be \cs{relax}, but the number
%   defined earlier, so \cs{cs_if_exist:cTF} can be safely used. If the
%   parameter does not exist it was never declared in the
%   \meta{parameter~text} (somewhat like |\def#1{#2}|), then raise an
%   error and abort.
%   Otherwise just flush |#|\meta{number}.
%    \begin{macrocode}
\cs_new:Npn \@@_in_replacement:nN #1 #2
  {
    \cs_if_exist:cTF { \c_@@_prefix_tl #1 }
      {
        \exp_args:Nf \@@_append_output:nnNwnn
          { \use:c { \c_@@_prefix_tl #1 } }
          { }
      }
      {
        \msg_expandable_error:nnn { namedef } { unknown-parm } {#1}
        \exp_args:Ne \@@_append_output:nnNwnn
          { #2 \@@_begin_name_token: #1 \@@_end_name_token: }
          { \cs_end: { } \use_none:nn }
      }
        #2
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Scanning routine}
%
% \begin{macro}[EXP]{\@@_replace_parm:Nn}
% \begin{macro}[EXP]{\@@_replace_loop:w}
% \begin{macro}[EXP]{\@@_replace_end:wnn, \@@_flush:nw}
% \begin{macro}[EXP]{\@@_append_output:nnNwnn}
% \begin{macro}[EXP]{\@@_abort_definition:w}
%   \cs{@@_replace_parm:Nn} uses the same looping principle as in
%   \pkg{l3tl}'s \cs{__tl_act:NNNnn}.  It scans the input (here, the
%   \meta{parameter~text} and \meta{replacement~text}, separately) token
%   by token, differentiating spaces, braced tokens (groups), and
%   \enquote{normal} tokens.
%    \begin{macrocode}
\cs_new:Npn \@@_replace_parm:Nn #1 #2
  {
    \exp_after:wN #1
      \exp:w
      \@@_replace_loop:w #2
        \q_@@_mark \q_@@_stop { } { }
  }
\cs_new:Npn \@@_replace_loop:w #1 \q_@@_stop
  {
    \tl_if_head_is_N_type:nTF {#1}
      { \@@_replace_normal:N }
      {
        \tl_if_head_is_group:nTF {#1}
          { \@@_replace_group:n }
          { \@@_replace_space:w }
      }
    #1 \q_@@_stop
  }
\cs_new:Npn \@@_replace_end:wnn \q_@@_stop #1 #2
  { \exp_end: {#1} {#2} }
\cs_new:Npn \@@_flush:nw #1
    #2 \q_@@_stop #3 #4
  { \@@_replace_loop:w #2 \q_@@_stop { #3 #1 } {#4} }
%    \end{macrocode}
%
%   \cs{@@_append_output:nnNwnn} takes three arguments (a parameter
%   number, a parameter name, and a parameter token) and the two output
%   slots as |#5| and |#6|.  It appends the parameter token and number
%   to the first output slot, and a definition
%   |\cs_set:cpn {|\cs{c_@@_prefix_tl}~\meta{name}|} {|\meta{number}|}|
%   to the second output slot.
%    \begin{macrocode}
\cs_new:Npn \@@_append_output:nnNwnn #1 #2 #3
    #4 \q_@@_stop #5 #6
  {
    \@@_replace_loop:w #4 \q_@@_stop
      { #5 #3#1 }
      { #6 \cs_set:cpn { \c_@@_prefix_tl #2 } {#1} }
  }
%    \end{macrocode}
%
%   This macro doesn't really abort the definition at the time it's
%   called because it's called from within an |f|-expansion context, so
%   an attempt to end that prematurely will hardly end well.  Instead
%   it hijacks the process by inserting
%   \cs{@@_skip_to_scan_mark:w} in the second output slot, so that
%   the definition end as soon as the scanning ends.
%   \TODO{Can be simplified further.}
%    \begin{macrocode}
\cs_new:Npn \@@_abort_definition:w
    #1 \q_@@_stop #2 #3
  {
    \@@_replace_loop:w #1 \q_@@_stop
      {#2} { #3 \group_end: \@@_skip_to_scan_mark:w }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_replace_normal:N}
% \begin{macro}[EXP]{\@@_replace_group:n, \@@_flush_group:nnw}
% \begin{macro}[EXP]{\@@_replace_space:w}
%   Spaces are just passed through: they aren't parameter tokens nor
%   valid delimiters, so need no special treatment.
%
%   Braced tokens are recursively scanned by \cs{@@_replace_parm:Nn},
%   and the output is flushed inside a pair of braces (explicit catcode
%   $1$ and $2$ tokens are normalised to |{|_$1$ and |}|_$2$,
%   respectively)
%   \TODO{Since \cs{__tl_act:NNNnn} isn't used anymore, this doesn't
%   have to be |f|-expandable, so this restriciton can probably be
%   lifted.}
%
%   The remaining tokens are examined for their meaning.  If the token
%   is the quark \cs{q_@@_mark}, the scanning stops; if the token is a
%   parameter token, what follows is examined with \cs{@@_grab_parm:Nw}
%   to check if a replacement should be done; otherwise it's fluhsed to
%   the output.
%    \begin{macrocode}
\cs_new:Npn \@@_replace_normal:N #1
  {
    \token_if_eq_meaning:NNTF \q_@@_mark #1
      { \@@_replace_end:wnn }
      {
        \token_if_parameter:NTF #1
          { \@@_grab_parm:Nw }
          { \@@_flush:nw }
            {#1}
      }
  }
\cs_new:Npn \@@_replace_group:n #1
  { \@@_replace_parm:Nn \@@_flush_group:nnw {#1} }
\cs_new:Npn \@@_flush_group:nnw #1 #2
  { \@@_flush:nw { {#1} } }
\exp_last_unbraced:NNo
\cs_new:Npn \@@_replace_space:w \c_space_tl
  { \@@_flush:nw { ~ } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Parsing a parameter}
%
% \begin{macro}[EXP]{\@@_grab_parm:Nw}
% \begin{macro}[EXP]{\@@_grab_parm_aux:NNw, \@@_grab_parm_noop:NNw}
% \begin{macro}[EXP]{\@@_grab_parm_scan:NNw}
% \begin{macro}[EXP]{\@@_grab_parm_loop:nw, \@@_grab_parm_end:nw}
% \begin{macro}[EXP]{
%     \@@_parm_get_normal:nN,
%     \@@_parm_get_group:nn,
%     \@@_parm_get_space:nw
%   }
%   These macros are the final pieces of the parameter replacement
%   routine. \cs{@@_grab_parm:Nw} checks if the next token in the stream
%   is a valid |N|-type. If it is, then \cs{@@_grab_parm_aux:NNw} checks
%   if its character code is equal to \cs{@@_begin_name_token:}, and if
%   it is, then call \cs{@@_grab_parm_scan:NNw} to scan ahead for the
%   named parameter.  In all other cases, the tokens grabbed are not
%   named parameters, so they are flushed to the output.
%    \begin{macrocode}
\cs_new:Npn \@@_grab_parm:Nw #1 #2 \q_@@_stop
  {
    \tl_if_head_is_N_type:nTF {#2}
      { \@@_grab_parm_aux:NNw }
      { \@@_flush:nw }
        #1 #2 \q_@@_stop
  }
\cs_new:Npn \@@_grab_parm_aux:NNw #1 #2
  {
    \exp_args:No \token_if_eq_charcode:NNTF
        { \@@_begin_name_token: } #2
      { \@@_grab_parm_scan:NNw }
      { \@@_grab_parm_noop:NNw }
        #1 #2
  }
%    \end{macrocode}
%
%   Here we have to take care not to flush \cs{q_@@_mark} to the output.
%    \begin{macrocode}
\cs_new:Npn \@@_grab_parm_noop:NNw #1 #2
  {
    \token_if_eq_meaning:NNTF \q_@@_mark #2
      { \@@_flush:nw { #1 } #2 }
      { \@@_flush:nw { #1 #2 } }
  }
%    \end{macrocode}
%
%   Here's the actual scanning routine.  It would be a lot faster to
%   just define a delimiter macro with the right tokens, however this
%   would have two consequences:  first, missing delimiters would be
%   rather catastrophic, and second, the catcode of the |end| delimiter
%   would need to match.  With a manual scanning, we can kill off those
%   two items at the cost of some performance.
%
%   The scanning routine is pretty standard: a looping macro, an output
%   slot, the tokens to be scanned, \cs{q_@@_stop} to delimit the whole
%   thing (\cs{q_@@_mark} is redundant here: the one from the main
%   scanning rountine is already in place), and the parameter token
%   safely stored at the end:
%    \begin{macrocode}
\cs_new:Npn \@@_grab_parm_scan:NNw #1 #2 #3 \q_@@_stop
  { \@@_grab_parm_loop:nw { } #3 \q_@@_stop {#1} }
\cs_new:Npn \@@_grab_parm_loop:nw #1 #2 \q_@@_stop
  {
    \tl_if_head_is_N_type:nTF {#2}
      { \@@_parm_get_normal:nN }
      {
        \tl_if_head_is_group:nTF {#2}
          { \@@_parm_get_group:nn }
          { \@@_parm_get_space:nw }
      }
    {#1} #2 \q_@@_stop
  }
%    \end{macrocode}
%
%   If the end of the token list was reached (signalled by
%   \cs{q_@@_mark}), the |end| delimiter is missing.  If so, raise an
%   error and recover as gracefully as possible.  Otherwise, if the
%   current token is the same character as the \cs{@@_end_name_token:},
%   then the scaning is complete.
%    \begin{macrocode}
\cs_new:Npn \@@_parm_get_normal:nN #1 #2
  {
    \token_if_eq_meaning:NNTF \q_@@_mark #2
      {
        \msg_expandable_error:nn { namedef } { missing-end }
        \@@_grab_parm_end:nw {#1} #2
      }
      {
        \exp_args:No \token_if_eq_charcode:NNTF
            { \@@_end_name_token: } #2
          { \@@_grab_parm_end:nw {#1} }
          { \@@_grab_parm_loop:nw {#1#2} }
      }
  }
\cs_new:Npn \@@_parm_get_group:nn #1 #2
  { \@@_grab_parm_loop:nw { #1{#2} } }
\cs_new:Npn \@@_parm_get_space:nw #1 ~
  { \@@_grab_parm_loop:nw { #1~ } }
\cs_new:Npn \@@_grab_parm_end:nw #1 #2 \q_@@_stop #3
  { \@@_handle_parameter:nN {#1} #3 #2 \q_@@_stop }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Changing delimiters}
%
% \begin{macro}[EXP]{\@@_begin_name_token:, \@@_end_name_token:}
%   These two hold the delimiters for named parameters.  They are
%   initialised here so that we can use \cs{named} (just to show off)
%   ahead, and they are redefined every time \cs{NamedDelim} is used.
%    \begin{macrocode}
\cs_new:Npn \@@_begin_name_token: { [ }
\cs_new:Npn \@@_end_name_token: { ] }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NamedDelim, \globalNamedDelim}
% \begin{macro}{\@@_named_delim_set:Nnn}
% \begin{macro}{\@@_check_delimiter:n}
%   At this point everything for the \cs{named} macro is set up,
%   so we can start using it. Now just some syntactic sugar to
%   allow the modification of the named argument delimiters.
%
%   Both \cs{NamedDelim} and \cs{globalNamedDelim} take two arguments,
%   an initial and final delimiters for the named argument. Both
%   delimters should be single non-control sequence tokens. Some of
%   these restrictions could be lifted, but it's not really necessary
%   because the choice of delimiter should not influence the working of
%   the code, only the readability. A code with |\NamedDelim[]| and
%   |\def\test#[1][#[2]]{[#[1]][#[2]]}| should work without problems;
%   the only restriction is that \cs{@@_end_name_token:} (\emph{i.e.,}
%   the second argument of \cs{NamedDelim}) cannot appear in the
%   parameter name.
%    \begin{macrocode}
\cs_new_protected:Npn \NamedDelim
  { \@@_named_delim_set:Nnn \cs_set:Npn  }
\cs_new_protected:Npn \globalNamedDelim
  { \@@_named_delim_set:Nnn \cs_gset:Npn }
\named \cs_new_protected:Npn \@@_named_delim_set:Nnn
    #[def] #[begin] #[end]
  {
    \tl_trim_spaces_apply:nN {#[begin]} \@@_check_delimiter:n
    \tl_trim_spaces_apply:nN {#[end]}   \@@_check_delimiter:n
    #[def] \@@_begin_name_token: {#[begin]}
    #[def] \@@_end_name_token: {#[end]}
    \s_@@
  }
%    \end{macrocode}
%
%   Here the \meta{token} is checked against a bunch of forbidden cases.
%    \begin{macrocode}
\named \cs_new_protected:Npn \@@_check_delimiter:n #[token]
  {
%    \end{macrocode}
%   It can't be empty (nor a space: they were trimmed above):
%    \begin{macrocode}
    \tl_if_empty:nT {#[token]}
      {
        \msg_error:nn { namedef } { blank-delim }
        \@@_skip_to_scan_mark:w
      }
%    \end{macrocode}
%   It can't be multiple tokens:
%    \begin{macrocode}
    \tl_if_single_token:nF {#[token]}
      {
        \msg_error:nnn { namedef } { multi-token-delim } {#[token]}
        \@@_skip_to_scan_mark:w
      }
%    \end{macrocode}
%   It can't be an implicit begin- or end-group token:
%    \begin{macrocode}
    \bool_lazy_or:nnT
        { \token_if_group_begin_p:N #[token] }
        { \token_if_group_end_p:N #[token] }
      {
        \msg_error:nnx { namedef } { group-delim }
          { \cs_to_str:N #[token] }
        \@@_skip_to_scan_mark:w
      }
%    \end{macrocode}
%   It can't be a parameter token:
%   \TODO{Maybe it can, but maybe it will be ambiguous.  Must check.}
%    \begin{macrocode}
    \token_if_parameter:NT #[token]
      {
        \msg_error:nnx { namedef } { param-delim }
          { \cs_to_str:N #[token] }
        \@@_skip_to_scan_mark:w
      }
%    \end{macrocode}
%   It can't be a control sequence:
%   \TODO{It probably can, but I'm not sure I want to allow this.}
%    \begin{macrocode}
    \token_if_cs:NT #[token]
      {
        \msg_error:nnx { namedef } { macro-delim }
          { \c_backslash_str \cs_to_str:N #[token] }
        \@@_skip_to_scan_mark:w
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Messages}
%
% Now we define the messages used throughout the package.
%
%    \begin{macrocode}
\msg_new:nnn { namedef } { repeated-parm }
  {
    Parameter~\iow_char:N\#[#1]~duplicated~in~
    definition~of~\l_@@_macro_tl.
  }
\msg_new:nnn { namedef } { unknown-parm }
  {
    Unknown~parameter~\iow_char:N\#[#1]~in~
    definition~of~\l_@@_macro_tl.
  }
\msg_new:nnn { namedef } { multi-token-delim }
  {
    Invalid~\iow_char:N\\named~parameter~delimiter~`#1'.~
    Delimiters~for~named~parameters~must~be~single~tokens.
  }
\msg_new:nnn { namedef } { macro-delim }
  {
    Invalid~\iow_char:N\\named~parameter~delimiter~`#1'.~
    Delimiters~for~named~parameters~can't~be~control~sequence~nor~
    active~characters.
  }
\msg_new:nnn { namedef } { group-delim }
  {
    Invalid~\iow_char:N\\named~parameter~delimiter~`\iow_char:N\\#1'.~
    Delimiters~for~named~parameters~can't~be~
      begin-/end-group~character~tokens.
  }
\msg_new:nnn { namedef } { blank-delim }
  {
    Invalid~\iow_char:N\\named~parameter~delimiter.~
    Delimiters~for~named~parameters~can't~be~empty~nor~space~tokens.
  }
\msg_new:nnn { namedef } { param-delim }
  {
    Invalid~\iow_char:N\\named~parameter~delimiter.~
    Delimiters~for~named~parameters~can't~be~parameter~tokens.
  }
\msg_new:nnn { namedef } { missing-end }
  {
    Missing~\@@_end_name_token:\iow_char:N\ inserted~in~
    definition~of~\l_@@_macro_tl.
  }
%    \end{macrocode}
%
% Now execute the end package hook (in \LaTeX{} it is
% \cs{prg_do_nothing:}, but in plain \TeX{} it does \cs{ExplSyntaxOff}).
%
%    \begin{macrocode}
\@@_end_package_hook:
%    \end{macrocode}
%
% \iffalse
%</package>
% \fi
%
% \end{implementation}
%
%^^A \par\CodedocExplainEXP
%^^A \par\CodedocExplainREXP
%^^A \par\CodedocExplainTF
%
% \PrintIndex
%
\endinput