-
Notifications
You must be signed in to change notification settings - Fork 0
/
miscellaneous.sty
137 lines (93 loc) · 3.34 KB
/
miscellaneous.sty
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
\ProvidesExplPackage{miscellaneous}{2022/03/20}{0.0.0}{Miscellaneous definitions}
\RequirePackage{genvar}
\RequirePackage{prettytok}
\RequirePackage{filecontentsdef}
\RequirePackage{tlanalysispatch}
\errorcontextlines=999
\errorstopmode
\GenerateVariantsFile:n {miscellaneous.sty}
% ======== step 1. Letin
% example
%\letin:nNn {2} \i {\pretty:n {i = \i}}
% namedef may also be useful~
% ======== miscellaneous
\prg_new_conditional:Npnn \str_if_startswith:nn #1 #2 {T, F, TF} {
\str_if_eq:xnTF
{\str_range:nnn {#1} {1} {\str_count:n {#2}}}
{#2}
{\prg_return_true:} {\prg_return_false:}
}
% ======== step 2. Precat
\cs_set_protected:Npn \precat_special_handle_regex:n #1 {
\tl_set:Nx \l_precat_index {\int_eval:n {\l_precat_index+1}}
\letin:xNn {precattoken \int_to_roman:V \l_precat_index} \precattoken { % precattoken: for example string "precattokeni"
\tl_put_right:Nx \l_precat_processed {\c_backslash_str \precattoken /}
\tl_put_right:Nn \l_precat_pending_replacements {
\regex_replace_all:nnN {\c{\precattoken}/} {#1} \precat_output
\pretty:V \precat_output
}
}
\let \l_precat_special_handled \c_true_bool
}
% ** main interface, \precat_process:n {content} {setup code} → set \precat_output to the result
% unfortunately this is unacceptably slow ><
\cs_set_protected:Npn \precat_process:nn #1 #2 {
% first convert everything to other catcode
\str_set:Nn \precat_source {#1}
% process content one by one (very inefficient but okay...)
% ========
\def\l_precat_processed {}
\tl_set:Nn \l_precat_pending_replacements {}
\tl_set:Nn \l_precat_index {0}
%\benchmark{A}
\bool_until_do:nn {
\str_if_empty_p:N \precat_source
}
{
%\pretty:x {\c_backslash_str \detokenize{cO} \c_backslash_str}
\let \l_precat_special_handled \c_false_bool
\str_if_startswith:ooT {\precat_source} {\c_backslash_str c} {
\tl_map_inline:nn {CBEMTPUDSLOA} {
\letin:cNn {c ##1} \cX {
\str_if_startswith:VxT \precat_source {\c_backslash_str c##1 \c_backslash_str}
%\str_if_eq:xxT
% {\str_range:Vnn \precat_source {1} {4}}
% {\c_backslash_str c##1 \c_backslash_str}
{
\letin:xNn {\str_item:Vn \precat_source {5}} \replacedchar {
\precat_special_handle_regex:n {\cX \replacedchar}
}
\str_set:Nx \precat_source {\str_range:Vnn \precat_source {6} {-1}}
\tl_map_break:
}
}
}
}
\bool_if:NF \l_precat_special_handled {
\tl_put_right:Nx \l_precat_processed {\str_item:Vn \precat_source {1}}
\str_set:Nx \precat_source {\str_range:Vnn \precat_source {2} {-1}}
}
}
%\benchmark{B}
\tl_set_rescan:NnV \precat_output {\newlinechar=13\relax #2 } \l_precat_processed
\l_precat_pending_replacements
}
% ======== precat environment.
\tl_set:Nx \precat_tab_in_other {\char_generate:nn {`\^^I} {12}}
% argument 1: setup code, e.g. \ExplSyntaxOn.
\newenvironment{precat}[1]{
\tl_gset:Nn \g_precat_tmp_catsetup {#1}
\let \FCDtabtomacro \precat_tab_in_other
\int_compare:nNnTF {\endlinechar} = {13} {} {
\msg_error:nn {miscellaneous} {Must~ use~ outside~ ExplSyntaxOn~ region!}
}
\filecontentsdefmacro{\precatinput}
}{
\endfilecontentsdefmacro
\precat_process:VV \precatinput \g_precat_tmp_catsetup
%\pretty:V \precat_output
\precat_output
}
% ======== precat not very good unfortunately, maybe should use regex
%\cs_new:Npn \precatTL_preprocess:Npn
% ======== step 3. Execinside.