summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/koma-script/source/scrwfile.dtx
blob: 5da27e5ee06cd88036107c8f3b7bb2444491b24b (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
% \iffalse meta-comment
% ======================================================================
% scrwfile.dtx
% Copyright (c) Markus Kohm, 2010-2022
%
% This file is part of the LaTeX2e KOMA-Script bundle.
%
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, version 1.3c of the license.
% 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 and of this work.
%
% This work has the LPPL maintenance status "author-maintained".
%
% The Current Maintainer and author of this work is Markus Kohm.
%
% This work consists of all files listed in manifest.txt.
% ======================================================================
%%% From File: $Id: scrwfile.dtx 3773 2022-03-29 06:27:55Z kohm $
%<*dtx>
\ifx\ProvidesFile\undefined\def\ProvidesFile#1[#2]{}\fi
\ProvidesFile{scrwfile.dtx}
%</dtx>
%<(package&identify)|driver>\NeedsTeXFormat{LaTeX2e}[1995/06/01]
%<package&identify>\ProvidesPackage{scrwfile}
%<driver>\ProvidesFile{scrwfile.drv}
%<*dtx|(package&identify)|driver>
  [2021/05/30 v0.1.10 KOMA-Script package (write and clone files)]
%</dtx|(package&identify)|driver>
%<*dtx>
\ifx\documentclass\undefined
  \input scrdocstrip.tex
  \@@input scrkernel-version.dtx
  \@@input scrstrip.inc
  \KOMAdefVariable{COPYRIGHTFROM}{2010}

  \generate{\usepreamble\defaultpreamble
    \file{scrwfile.sty}{%
      \from{scrwfile.dtx}{package,trace,scrwfile,identify,option,body}%
      \from{scrlogo.dtx}{logo}%
    }%
  }

  \@@input scrstrop.inc
\else
  \let\endbatchfile\relax
\fi
\endbatchfile
%</dtx>
%<*driver>
\documentclass[USenglish]{koma-script-source-doc}
\usepackage{babel}
\usepackage{hvlogos}
\setcounter{StandardModuleDepth}{2}
\begin{document}
\DocInput{scrwfile.dtx}
\end{document}
%</driver>
% \fi
%
% \changes{v0.1}{2010/10/01}{start of new package}
% \changes{v0.1.7}{2014/10/13}{manual moved to \KOMAScript{} manual}
% \changes{v0.1.9}{2021/05/30}{version number scheme changed}
% \changes{v0.1.10}{2022/02/05}{switch over from \cls*{scrdoc} to
%   \cls*{koma-script-source-doc}}
%
% \GetFileInfo{scrwfile.dtx}
% \title{Saving Write Handles with the Experimental
%   \href{https://komascript.de}{\KOMAScript} Package \pkg*{scrwfile}}
% \author{\href{mailto:komascript@gmx.info}{Markus Kohm}}
% \date{Version \fileversion{} of \filedate}
% \maketitle
% \begin{abstract}
%   The \TeX{} engines \TeX, \pdfTeX, and \XeTeX{} (but not \LuaTeX) do
%   provide only 16 handles for simultaneously write open files. Some of them
%   are already used by \LaTeX{}. Moreover, for every file using
%   \cs{@starttoc} \LaTeX{} keeps a write file open from usage of
%   \cs{@starttoc}, e.g., inside \cs{tableofcontents}, \cs{listoffigures}
%   etc., until the end of the document. Additional write files are used for
%   the index, glossary etc. So sometimes it happens, that there are not
%   enough handles to open another one. \pkg*{scrwfile} was made to change the
%   \LaTeX{} internal handling of all the helper files bases on \cs{@starttoc}
%   to not keep them open simultaneously, but use only one file handle for all
%   of them. So you should never run out of write file handles.
% \end{abstract}
% 
% \section{User Manual}
%
% You can find the user manual of \pkg*{scrwfile} in the \KOMAScript{} manual,
% either the German \file{scrguide} or the English \file{scrguien}. But please
% note:
% \begin{quote}\bfseries
%   This is an experimental package. Usage of this package is on your own
%   risk. There is no support, if you are using this package, not only for
%   this package but also for the class you are using or other packages, that
%   use \cs{@starttoc} or \cs{@writefile} like \pkg{caption}, \pkg{float},
%   \pkg*{tocbasic}, \pkg{listings} and many more.
% \end{quote}
%
% \MaybeStop{\PrintIndex}
%
%
% \section{Implementation of \pkg*{scrwfile}}
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
%    \begin{macrocode}
%<*identify>
\PackageWarningNoLine{scrwfile}{%
  THIS IS AN EXPERIMENTAL PACKAGE!\MessageBreak
  USAGE OF THIS PACKAGE IS ON YOUR OWN RISK!\MessageBreak
  EVERYTHING MAY HAPPEN!\MessageBreak
  EVERYTHING MAY CHANGE IN FUTURE!\MessageBreak
  THERE IS NO SUPPORT, IF YOU USE THIS PACKAGE!\MessageBreak
  Maybe, it would be better not to load this package%
}
%</identify>
%    \end{macrocode}
%\iffalse
%\fi
%
% \subsection{Options}
% \iffalse
%<*option>
% \fi
% Currently we don't need options.
% \iffalse
%</option>
% \fi
%
% \subsection{Body}
%    \begin{macrocode}
%<*body>
%    \end{macrocode}
%
% \subsubsection{Needed Packages}
%
% Package \pkg*{scrbase} is needed, because of using several \KOMAScript{}
% basic commands.
%    \begin{macrocode}
\RequirePackage{scrbase}[2010/09/17]
%    \end{macrocode}
%
% Package \pkg*{scrlfile} is needed because of the \file{aux}-file
% handling and \cs{protected@immediate@write}.
%    \begin{macrocode}
\RequirePackage{scrlfile}[2010/09/30]
%    \end{macrocode}
%
% \changes{v0.1.10}{2022/02/04}{\pkg{iftex} is required}
% Package \pkg{iftex} is used to detect Lua\LaTeX.
%    \begin{macrocode}
\RequirePackage{iftex}
%    \end{macrocode}
%
% \subsubsection{Is is needed?}
%
% \changes{v0.1.9}{2021/05/30}{with Lua\LaTeX{} using is not recommended}
% \changes{v0.1.10}{2022/02/04}{using \cs{ifluatex}}
% \changes{v0.1.10}{2022/02/04}{\cs{PackageInfoNoLine} replaced by
% \cs{PackageWarningNoLine}}
% If the user uses Lua\LaTeX{} usage of \pkg*{scrwfile} should not be
% needed.
%    \begin{macrocode}
\ifluatex
  \PackageWarningNoLine{scrwfile}{LuaLaTeX detected.\MessageBreak
    With LuaLaTeX you should never get an error message:\MessageBreak
    \space\space`No room for a new \string\write'.\MessageBreak
    So scrwfile could make much more harm than benefit\MessageBreak
    and using it is not recommended}%
\fi
%    \end{macrocode}
%
% \subsubsection{\LaTeX{} Kernel Patchs}
%
% For some features we need to patch \LaTeX{} kernel macros. Those features
% and macros are:
% \begin{description}
% \item[Single handle feature] means, that \LaTeX{} will no longer need a file
%   handle for every help file, but only one for all files. We will patch
%  \cs{@starttoc} and \cs{@writefile} to do so.
% \item[Clone file feature] means, that every write to one file may be done to
%   another file, too. We will patch \cs{@writefile} to do so.
% \end{description}
% Every patch should be minimum invasive, so that files, that are not under
% \pkg*{scrwfile}'s control are changed as little as possible.
%
% \begin{macro}{\scrwfile@if@only}
% First of all we check, if the file should be handled by \pkg*{scrwfile}.
%    \begin{macrocode}
\newcommand*{\scrwfile@if@only}[1]{%
  \begingroup
    \scr@ifundefinedorrelax{scrwfile@only}{\@tempswatrue}{%
      \@tempswafalse
      \edef\reserved@b{#1}%
      \@for\reserved@a:=\scrwfile@only\do
        {\ifx\reserved@a\reserved@b\@tempswatrue\fi}%
    }%
    \if@tempswa
      \scr@ifundefinedorrelax{scrwfile@never}{}{%
        \edef\reserved@b{#1}%
        \@for\reserved@a:=\scrwfile@never\do
          {\ifx\reserved@a\reserved@b\@tempswafalse\fi}%
      }%
    \fi
  \expandafter\endgroup
  \if@tempswa
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\scrwfile@starttoc,\scrwfile@@starttoc}
% This is the internal redefinition of \cs{@starttoc}. First of all test, if it
% should be used, then use it or not.
%    \begin{macrocode}
\newcommand*{\scrwfile@starttoc}[1]{%
  \scrwfile@if@only{#1}{\scrwfile@@starttoc}{\scrwfile@saved@starttoc}{#1}%
}
\newcommand*{\scrwfile@@starttoc}[1]{%
%<trace>  \PackageInfo{scrwfile}{%
%<trace>    Using my own \string\@starttoc\space for `#1'}%
  \begingroup
    \if@filesw
      \xdef\scrwfile@writefilelist{\scrwfile@writefilelist,#1}%
    \fi
    \@fileswfalse
    \scrwfile@saved@starttoc{#1}%
  \endgroup
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\scrwfile@writefile,\scrwfile@@writefile,\scrwfile@wrtout,
%               \scrwfile@writefilelist}
% This is the internal redefinition of \cs{@writefile}. First of all test, if
% it should be used, then use it or not.
%    \begin{macrocode}
\newcommand*{\scrwfile@writefile}[1]{%
  \scrwfile@if@only{#1}{\scrwfile@@writefile}{\scrwfile@saved@writefile}{#1}%
}
\newcommand{\scrwfile@@writefile}[2]{%
%<trace>  \PackageInfo{scrwfile}{%
%<trace>    Using my own \string\@writefile\space for `#1'}%
  \ifnum\scrwfile@wrtout>0
    \begingroup
      \@temptokena{#2}%
      \immediate\write\scrwfile@wrtout{%
        \string\@writefile{#1}{\the\@temptokena}%
      }%
%    \end{macrocode}
% This was the entry for the real file. But we also may have clone files:
%    \begin{macrocode}
      \scrwfile@process@clones{#1}%
    \endgroup
  \fi
}
\chardef\scrwfile@wrtout\z@
\newcommand*{\scrwfile@writefilelist}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@writefile,\scrwfile@saved@writefile}
% We have to add the single handle feature and the clone file feature to
% \cs{@writefile} and therefore save the original
% definition. \cs{scrwfile@saved@writefile} is used, whenever a file is not
% under \pkg*{scrwfile}'s control.
%    \begin{macrocode}
\newcommand*{\scrwfile@saved@writefile}{}
\BeforeClosingMainAux{%
  \ifx\scrwfile@writefilelist\@empty\else
    \let\scrwfile@saved@writefile\@writefile
    \let\scrwfile@wrtout\@partaux
    \immediate\openout\scrwfile@wrtout \jobname.wrt
    \let\@writefile\scrwfile@writefile
  \fi
}
\AfterReadingMainAux{%
  \ifx\scrwfile@writefilelist\@empty\else
    \immediate\closeout\scrwfile@wrtout
    \chardef\scrwfile@wrtout\z@
    \begingroup
      \let\@writefile\scrwfile@saved@writefile
      \@for\@currext:=\scrwfile@writefilelist\do{%
        \begingroup
          \ifx\@currext\@empty\else
            \scr@ifundefinedorrelax{tf@\@currext}{%
%<trace>              \typeout{Process extension: `\@currext'}
              \immediate\openout\@partaux \jobname.\@currext
              \expandafter\let\csname tf@\@currext\endcsname\@partaux
              \@input@{\jobname.wrt}%
              \immediate\closeout\@partaux
            }{}%
          \fi
        \endgroup
      }%
    \endgroup
  \fi
}
%    \end{macrocode}
% \changes{v0.1.8}{2021/05/30}{added order rule for
%   \pkg*{scrlayer-notecolumn}}
% Note: Here we use a \LaTeX{} version test, because a do-nothing-definition
% like the one of \pkg{latexrelease} wouldn't be correct, if the command is
% not supported by the current \LaTeX{} kernel version setting.
%    \begin{macrocode}
\IfLTXAtLeastTF{2020/10/01}{%
  \DeclareHookRule{enddocument/afteraux}%
                  {scrwfile}{before}{scrlayer-notecolumn}%
}{%
  \@ifpackageloaded{scrlayer-notecolumn}{%
    \PackageWarningNoLine{scrwfile}{%
      Dangerous package order detected!\MessageBreak
      As a general rule, you should load scrwfile as soon\MessageBreak
      as possible, maybe even before `\string\documentclass'\MessageBreak
      (using `\string\RequirePackage' instead of
      `\string\usepackage').\MessageBreak
      Following packages should be loaded after scrwfile:\MessageBreak
      \space - scrlayer-notecolumn}%
  }{}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@starttoc,\scrwfile@saved@starttoc}
% We have to add the single handle feature to \cs{@starttoc} and therefore
% save the original definition.
% \changes{v0.1.5}{2013/07/24}{take care for unfriendly \textsl{hyperref}}
% \cs{scrwfile@saved@starttoc} is be used, whenever the file is not under
% \pkg*{scrwfile}'s control. Because of package \textsl{hyperref}, that does
% a hard redefinition without any care for changed definitions, we have to
% take care for that unfriendly package and cannot do a simple
% \begin{verbatim}
% \let\scrwfile@saved@starttoc\@starttoc
% \let\@starttoc\scrwfile@starttoc
% \end{verbatim}
%    \begin{macrocode}
\newcommand*{\scrwfile@saved@starttoc}{}
\AtBeginDocument{%
  \begingroup
    \@ifpackageloaded{hyperref}{%
      \scr@ifundefinedorrelax{Hy@AtBeginDocument}{%
        \PackageWarning{scrwfile}{%
          Incompatible `hyperref` package detected!\MessageBreak
          You are using a `hyperref` version, that\MessageBreak
          doesn't provide `\string\Hy@AtBeginDocument`.\MessageBreak
          This may or may not work.\MessageBreak
          Using immediate redefinition%
        }%
        \aftergroup\@firstofone
      }{%
        \def\reserved@a{\AtBeginDocument}%
        \ifx\Hy@AtBeginDocumentHook\@undefined
          \PackageInfo{scrwfile}{%
            Using immediate redefinition%
          }%
          \aftergroup\@firstofone
        \else
          \PackageInfo{scrwfile}{%
            Using `\string\Hy@AtBeginDocument`%
          }%
          \aftergroup\Hy@AtBeginDocument
        \fi
      }%
    }{%
      \aftergroup\@firstofone
    }%
  \endgroup
  {%
    \PackageInfo{scrwfile}{%
      Extending `\string\@starttoc`
    }%
    \let\scrwfile@saved@starttoc\@starttoc
    \let\@starttoc\scrwfile@starttoc
  }%
}
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Clone TOC Feature}
%
% \pkg*{scrwfile} may clone a TOC, that means, every entry to one file will
% be copied to other files, too.  You must not clone recursively!
%
% \begin{description}
% \item[ToDo:]The whole feature should be moved to \pkg*{tocbasic}. But this
%   would need a complete re-implementation and would result in one more write
%   handle for every cloned file. So maybe it wouldn't be a really good idea
%   to do so.
% \end{description}
%
% \begin{macro}{\scrwfile@process@clones}
%    \begin{macrocode}
\newcommand*{\scrwfile@process@clones}[1]{%
  \scr@ifundefinedorrelax{scrwfile@clone@#1}{}{%
    \begingroup
      \let\@@protect\protect\let\protect\@empty\afterassignment\restore@protect
      \edef\reserved@b{\csname scrwfile@clone@#1\endcsname}%
      \edef\reserved@c{,#1}%
      \@for \reserved@a:=\reserved@b\do {%
        \@tempswatrue
        \@for \reserved@d:=\reserved@c\do {%
          \ifx\reserved@d\reserved@a\@tempswafalse\fi
        }%
        \if@tempswa
%<trace>          \typeout{clone entry from `#1' to `\reserved@a'}%
          \immediate\write\scrwfile@wrtout{%
            \string\@writefile{\reserved@a}{\the\@temptokena}%
          }%
          \edef\reserved@c{\reserved@c,\reserved@a}%
        \fi
      }%
    \endgroup
  }%
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{command}{\TOCclone}
% \changes{v0.1.8}{2019/11/18}{\cs{ifstr} umbenannt in \cs{Ifstr}}
% \changes{v0.1.8}{2019/11/19}{\cs{ifattoclist} replaced by \cs{Ifattoclist}}
% \changes{v0.1.8}{2019/11/19}{\cs{iftocfeature} replaced by
%   \cs{Iftocfeature}}
% \changes{v0.1.8}{2020/02/25}{spurious space in warning message removed}
% \changes{v0.1.9}{2021/05/30}{requires package \pkg*{tocbasic}}
% \changes{v0.1.9}{2021/05/30}{can be used only in preamble}
% Clone the entries from the second (first mandatory) argument TOC to the
% third (second mandatory) argument TOC.  If the first (optional) argument was
% given, define |\listof#3name| to this and also define |\listof#3| and clone
% the toc features \texttt{leveldown}, \texttt{numbered}, \texttt{onecolumn}
% and \texttt{totoc} of |#2| to |#3|. The toc feature \texttt{nobabel} will
% always be set, because the babel entries at TOC |#3| will be cloned from TOC
% |#2|.
% \begin{description}
% \item{Note:} We use owner \texttt{TOCclone} for all cloned extensions.
% \end{description}
%    \begin{macrocode}
\newcommand*{\TOCclone}[3][]{%
  \RequirePackage{tocbasic}%
  \scr@ifundefinedorrelax{scrwfile@clone@#2}{%
    \expandafter\protected@edef\csname scrwfile@clone@#2\endcsname{%
      #3,\protect\csname scrwfile@clone@#3\endcsname
    }%
  }{%
    \edef\reserved@b{\csname scrwfile@clone@#2\endcsname}%
    \expandafter\protected@edef\csname scrwfile@clone@#2\endcsname{%
      \csname scrwfile@clone@#2\endcsname,%
      #3,%
      \protect\csname scrwfile@clone@#3\endcsname
    }%
  }%
  \scr@ifundefinedorrelax{scrwfile@clone@#3}{%
    \expandafter\let\csname scrwfile@clone@#3\endcsname\@empty
  }{}%
  \Ifattoclist{#3}{%
    \PackageWarning{scrwfile}{`#3' already under control of
      tocbasic.\MessageBreak
      Nevertheless features will be set%
    }%
  }{%
    \addtotoclist[TOCclone]{#3}%
  }%
  \setuptoc{#3}{nobabel}%
  \Ifstr{#1}{}{%
  }{%
    \@namedef{listof#3name}{#1}%
    \@namedef{listof#3}{\listoftoc{#3}}%
    \Iftocfeature{#2}{leveldown}{\setuptoc{#3}{leveldown}}{}%
    \Iftocfeature{#2}{numbered}{\setuptoc{#3}{numbered}}{}%
    \Iftocfeature{#2}{onecolumn}{\setuptoc{#3}{leveldownonecolumn}}{}%  
    \Iftocfeature{#2}{totoc}{\setuptoc{#3}{totoc}}{}%
  }%
}
\@onlypreamble\TOCClone
%</body>
%    \end{macrocode}
% \end{command}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \Finale
% \PrintChanges
%
% \endinput
% Local Variables:
% mode: doctex
% ispell-local-dictionary: "en_US"
% eval: (flyspell-mode 1)
% TeX-master: t
% TeX-engine: luatex-dev
% eval: (setcar (or (cl-member "Index" (setq-local TeX-command-list (copy-alist TeX-command-list)) :key #'car :test #'string-equal) (setq-local TeX-command-list (cons nil TeX-command-list))) '("Index" "mkindex %s" TeX-run-index nil t :help "makeindex for dtx"))
% End: