summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/xsim/xsim.auxfile.code.tex
blob: 39a4306f593af50e5d14de676850d689d9a020ca (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
% ----------------------------------------------------------------------------
% the XSIM package - auxfile module
% 
%   eXercise Sheets IMproved
% 
% ----------------------------------------------------------------------------
% Clemens Niederberger
% Web:    https://github.com/cgnieder/xsim
% E-Mail: contact@mychemistry.eu
% ----------------------------------------------------------------------------
% Copyright 2017--2019 Clemens Niederberger
% 
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% 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.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% 
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Clemens Niederberger.
% ----------------------------------------------------------------------------
% If you have any ideas, questions, suggestions or bugs to report, please
% feel free to contact me.
% ----------------------------------------------------------------------------
\XSIMmodule{auxfile}{writing stuff to the aux file}

\cs_new_protected:Npn \xsim_write_to_aux:n #1
  { \iow_now:Nn \@auxout {#1} }
\cs_generate_variant:Nn \xsim_write_to_aux:n {x}

% --------------------------------------------------------------------------

\cs_new_protected:Npn \XSIM #1 { \use:c {XSIMaux#1} }

\AtBeginDocument
  {
    \xsim_write_to_aux:x
      { \token_to_str:N \providecommand \token_to_str:N \XSIM [1] {} }
  }

\cs_new_protected:Npn \xsim_new_aux_cs:cpn #1
  { \cs_new_protected:cpn {XSIMaux#1} }

\cs_new_protected:Npn \xsim_add_cs_to_aux:nn #1#2
  {
    \xsim_verbose:n { Writing~ command~ \XSIM {#1}~ to~ aux~ file }
    \xsim_write_to_aux:x { \XSIM {#1} #2 }
  }

% ----------------------------------------------------------------------------
% list for recording values that need to be written to the aux file and
% updated at begin document

\str_new:N \l__xsim_split_aux_lists_str
\keys_define:nn {xsim}
  {
    split-aux-lists .code:n    = \str_set:Nn \l__xsim_split_aux_lists_str {#1} ,
    split-aux-lists .initial:n = ||
  }

% define list:
% #1: name
\cs_new_protected:Npn \xsim_new_list:n #1
  {
    \xsim_verbose:n { Defining~ new~ auxfile~ list~ `#1'. }
    \prop_new:c {g__xsim_list_#1_prop}
    \seq_new:c {g__xsim_list_#1_seq}
    \seq_new:c {g__xsim_list_recorded_#1_seq}
    \xsim_new_aux_cs:cpn {#1} ##1
      {
        \prop_gclear:c {g__xsim_list_#1_prop}
        \tl_if_blank:nF {##1}
          {
            \seq_gset_split:cVx {g__xsim_list_#1_seq}
              \l__xsim_split_aux_lists_str
              { \tl_to_str:n {##1} }
            \seq_map_inline:cn {g__xsim_list_#1_seq}
              { \prop_gput:cnn {g__xsim_list_#1_prop} {####1} {} }
          }
      }
    \AtBeginDocument
      {
        \seq_gclear:c {g__xsim_list_recorded_#1_seq}
        \seq_set_eq:Nc \l__xsim_tmpa_seq {g__xsim_list_#1_seq}
        \seq_map_inline:Nn \l__xsim_tmpa_seq
          {
            \str_set:Nn \l__xsim_tmpa_str {##1}
            \seq_gput_right:cV {g__xsim_list_recorded_#1_seq} \l__xsim_tmpa_str
          }
        \seq_gremove_duplicates:c {g__xsim_list_recorded_#1_seq}
      }
  }

% add to or remove from list:
% #1: name
% #2: entry
\cs_new_protected:Npn \xsim_add_to_list:nn #1#2
  {
    \xsim_verbose:n { Adding~ `#2'~ to~ list~ `#1'. }
    \str_set:Nn \l__xsim_tmpa_str {#2}
    \prop_gput:cnn {g__xsim_list_#1_prop} {#2} {}
    \seq_if_in:cVT {g__xsim_list_#1_seq} \l__xsim_tmpa_str
      { \seq_gremove_all:cV {g__xsim_list_#1_seq} \l__xsim_tmpa_str }
    \seq_gput_right:cV {g__xsim_list_#1_seq} \l__xsim_tmpa_str
  }
\cs_generate_variant:Nn \xsim_add_to_list:nn {nx}

% #1: name
% #2: entry
\cs_new_protected:Npn \xsim_remove_from_list:nn #1#2
  {
    \xsim_verbose:n { Removing~ `#2'~ to~ list~ `#1'. }
    \str_set:Nn \l__xsim_tmpa_str {#2}
    \prop_if_in:cnT {g__xsim_list_#1_prop} {#2}
      { \prop_gremove:cn {g__xsim_list_#1_prop} {#2} }
    \seq_if_in:cVT {g__xsim_list_#1_seq} \l__xsim_tmpa_str
      { \seq_gremove_all:cV {g__xsim_list_#1_seq} \l__xsim_tmpa_str }
    \seq_if_in:cVT {g__xsim_list_recorded#1_seq} \l__xsim_tmpa_str
      { \seq_gremove_all:cV {g__xsim_list_recorded_#1_seq} \l__xsim_tmpa_str }
  }
\cs_generate_variant:Nn \xsim_remove_from_list:nn {nx}

% check if in list:
% #1: name
% #2: entry
\prg_new_conditional:Npnn \xsim_if_in_list:nn #1#2 {p,T,F,TF}
  {
    \prop_if_in:cnTF {g__xsim_list_#1_prop} {#2}
      { \prg_return_true: }
      { \prg_return_false: }
  }
\cs_generate_variant:Nn \xsim_if_in_list:nnTF {nx}
\cs_generate_variant:Nn \xsim_if_in_list:nnT  {nx}

\cs_new:Npn \xsim_list_show:n #1
  { \prop_show:c {g__xsim_list_#1_prop}  }

\bool_new:N \l__xsim_empty_lists_bool
\cs_new_protected:Npn \xsim_empty_lists:
  { \bool_set_true:N \l__xsim_empty_lists_bool }

% update list -- should be used inside \AtEndDocument:
% #1: name
\cs_new_protected:Npn \xsim_update_list:n #1
  {
    \xsim_verbose:n { Updating~ list~ `#1'. }
    \seq_remove_duplicates:c {g__xsim_list_#1_seq}
    \seq_remove_duplicates:c {g__xsim_list_recorded_#1_seq}
    \str_set:Nx \l__xsim_tmpa_str
      { \seq_use:cn {g__xsim_list_#1_seq} {} }
    \str_set:Nx \l__xsim_tmpb_str
      { \seq_use:cn {g__xsim_list_recorded_#1_seq} {} }
    \str_if_eq:NNF \l__xsim_tmpa_str \l__xsim_tmpb_str
      { \xsim_rerun: }
    \bool_if:NF \l__xsim_empty_lists_bool
      {
        \xsim_add_cs_to_aux:nn {#1}
          { { \seq_use:cV {g__xsim_list_#1_seq} \l__xsim_split_aux_lists_str } }
      }
  }

% loop over list:
% #1: name
% #2: code
\cs_new_protected:Npn \xsim_foreach_list_entry:nn #1#2
  { \seq_map_inline:cn {g__xsim_list_recorded_#1_seq} {#2} }

\cs_new_protected:Npn \xsim_foreach_new_list_entry:nn #1#2
  { \seq_map_inline:cn {g__xsim_list_#1_seq} {#2} }

% length of a list:
% #1: name
\cs_new:Npn \xsim_list_count:n #1
  { \seq_count:c {g__xsim_list_recorded_#1_seq} }

% item of a list:
% #1: name
% #2: integer
\cs_new:Npn \xsim_list_item:nn #1#2
  { \seq_item:cn {g__xsim_list_recorded_#1_seq} {#2} }

% ----------------------------------------------------------------------------
\file_input_stop: