summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/koma-script/scrwfile.dtx
blob: 5db335bed1b48e225bec720476d7f3b3c0f0398c (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
% \CheckSum{306}
% \iffalse meta-comment
% ======================================================================
% scrwfile.dtx
% Copyright (c) Markus Kohm, 2010-2013
%
% 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.
%
% The KOMA-Script bundle consists of all files listed in manifest.txt.
% The work `scrwfile' consists of the files `scrwfile.dtx' and 
% `scrlogo.dtx'.
%
% If you're missing the installation batch file `scrwfile.ins', try
%    tex scrwfile.dtx
% to unpack all files. 
% The package documentation may be produced repeating
%    latex scrwfile.dtx
% at least three times.
% ----------------------------------------------------------------------
% scrwfile.dtx
% Copyright (c) Markus Kohm, 2010-2013
%
% Diese Datei ist Teil des LaTeX2e KOMA-Script-Pakets.
%
% Dieses Werk darf nach den Bedingungen der LaTeX Project Public Lizenz,
% Version 1.3c.
% Die neuste Version dieser Lizenz ist
%   http://www.latex-project.org/lppl.txt
% und Version 1.3c ist Teil aller Verteilungen von LaTeX
% Version 2005/12/01 und dieses Werks.
%
% Dieses Werk hat den LPPL-Verwaltungs-Status "author-maintained"
% (allein durch den Autor verwaltet).
%
% Der Aktuelle Verwalter und Autor dieses Werkes ist Markus Kohm.
%
% Das KOMA-Script-Paket besteht aus allen Dateien, die in manifest.txt
% genannt sind.
% Das Werk `scrwfile' besteht aus den Dateien `scrwfile.dtx' und
% `scrlogo.dtx'.
%
% Falls Sie die Installations-Batch-Datei `scrwfile.ins' vermissen,
% probieren Sie einfach einmal
%    tex scrwfile.dtx
% um alle Dateien auszupacken. Zur englischen Anleitung siehe den
% englischen Kommentar oben.
% ======================================================================
% \fi
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \iffalse
%%% From File: $Id: scrwfile.dtx 1562 2013-12-19 07:51:16Z mjk $
%<*dtx>
\def\LaTeXformat{LaTeX2e}
\ifx\fmtname\LaTeXformat
  \makeatletter
    \let\saved@@end\@@end
    \def\@@end{\csname fi\endcsname\saved@@end}
  \makeatother
  \ProvidesFile{scrwfile.dtx}
%</dtx>
%<(package&identify)|driver>\NeedsTeXFormat{LaTeX2e}[1995/06/01]
%<package&identify>\ProvidesPackage{scrwfile}
%<driver>\ProvidesFile{scrwfile.drv}
%<doc>\ProvidesFile{scrwfile.tex}
%<*dtx|(package&identify)|driver|doc>
  [2013/08/05 v0.1f-alpha LaTeX2e KOMA-Script package (write and clone files)]
%</dtx|(package&identify)|driver|doc>
%<*driver>
  \documentclass{scrdoc}
  \usepackage[ngerman,english]{babel}
  \usepackage{listings}
  \lstnewenvironment{lstcode}[1][]{%
    \lstset{language=[LaTeX]TeX,basicstyle=\ttfamily\small,#1}%
  }{}

  \CodelineIndex
  \RecordChanges
  \GetFileInfo{scrwfile.dtx}
  \title{The \KOMAScript{} package \texttt{scrwfile}%
    \footnote{This is version \fileversion\ of file \texttt{\filename}.}}
  \date{\filedate}
  \author{Markus Kohm}
  
  \newenvironment{Explain}{\par}{\par}
  \let\Macro\cs
  \let\Package\textsf
  \let\File\texttt
  \let\Option\texttt
  \let\Counter\texttt
  \let\ShowOutput\quote
  \let\endShowOutput\endquote
  \let\Parameter\marg
  \let\OParameter\oarg
  \providecommand\PParameter[1]{\mbox{\texttt{\{#1\}}}}
  \let\PName\meta
  \let\PValue\texttt
  \providecommand*{\important}[1]{}
  \providecommand*{\autoref}[1]{\expandafter\AUTOREF#1:}
  \providecommand*{\AUTOREF}{}
  \makeatletter
  \def\AUTOREF#1:#2:{%
    \edef\@tempa{#1}%
    \edef\@tempb{tab}\ifx\@tempa\@tempb table~\fi
    \edef\@tempb{sec}\ifx\@tempa\@tempb section~\fi
    \ref{#1:#2}%
  }
  \providecommand*{\IndexCmd}[2][]{}
  \providecommand*{\textnote}[2][]{}
  \makeatother

  \begin{document}
  \maketitle
  \DocInput{\filename}
  \end{document}
%</driver>
%<*dtx>
\fi
% Now the installation driver:
%</dtx>
%<*insfile>
\let\ifbeta=\iffalse % with = will be replaced by release process
\def\batchfile{scrwfile.dtx}
\input docstrip.tex
\ifToplevel{%
  \Msg{**********************************************************************}
  \Msg{*}
  \Msg{* KOMA-Script presents scrwfile}
  \Msg{* a package to define, configure and several different styles for}
  \Msg{* table of contents, list of floats and the like.}
  \Msg{*}
  \Msg{* This is `\batchfile', a batch file to unpack the package scrwfile,}
  \Msg{* the documentation of the package, and an archive of all these files.}
  \Msg{*}
  \Msg{**********************************************************************}
  \keepsilent
  \askforoverwritefalse
}

\preamble

Copyright (c) 2010-2013 by Markus Kohm <komascript(at)gmx.info>

This file was generated from file(s) of the 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 or later 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 file may only be distributed together with the file
`scrwfile.dtx' and `scrlogo.dtx'. You may however distribute the files
`scrwfile.dtx' and `scrlogo.dtx' without this file.

If this file is a beta version, you are not allowed to distribute it.

English and German manuals are part of KOMA-Script bundle.
----------------------------------------------------------

The english manual is at `scrwfile.dtx', too.

The KOMA-Script bundle (but not this file) was based upon the LaTeX2.09 
Script family created by Frank Neukam 1993 and the LaTeX2e standard 
classes created by The LaTeX3 Project 1994-1996.

THIS IS AN ALPHA VERSION!
USAGE OF THIS VERSION IS ON YOUR OWN RISK!
EVERYTHING MAY HAPPEN!
EVERYTHING MAY CHANGE IN FUTURE!
THERE IS NO SUPPORT, IF YOU USE THIS PACKAGE!

\endpreamble

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

\ifToplevel{%
  \generate{\usepreamble\defaultpreamble
    \file{scrwfile.ins}{%
      \from{scrwfile.dtx}{insfile}%
    }%
    \file{scrwfile.tex}{%
      \from{scrwfile.dtx}{doc}%
    }%
    \file{scrwfile.drv}{%
      \from{scrwfile.dtx}{driver}%
    }%
  }%
}

\ifToplevel{%
  \Msg{**********************************************************************}
  \Msg{*}
  \ifbeta
  \Msg{* THIS IS AN ALPHA OR BETA VERSION. YOU SHOULD NOT INSTALL OR USE IT!}
  \Msg{* THERE MAY BE A LOT OF BUGS AT THIS VERSION!}
  \Msg{* PLEASE INSTALL THE RELEASE YOU MAY FIND AT CTAN OR BERLIOS!}
  \else
  \Msg{* To finish the installation you have to copy the file `scrwfile.sty'}
  \Msg{* to folder `tex/latex/scrwfile/' of one of your TEXMF trees.}
  \Msg{* You should also produce the documentation using}
  \Msg{*\space\space latex scrwfile.dtx}
  \Msg{* and copy it to folder `doc/latex/scrwfile/' of one of your}
  \Msg{* TEXMF trees.}
  \Msg{*}
  \Msg{* See the manual of your TeX distribution for more informations about}
  \Msg{* package installation.}
  \fi
  \Msg{*}
  \Msg{**********************************************************************}
}

%</insfile>
%<*dtx|doc>
%<*dtx>
\csname endinput\endcsname
%</dtx>
% \fi
%
% \selectlanguage{english}
%
% \changes{v0.1}{2010/10/01}{start of new package}
%
%\iffalse^^A meta comment
\chapter[{Spare and Replace Files Using \Package{scrwfile}}]
{Spare and Replace Files Using \Package{scrwfile}%
%  \footnote{This chapter has been generated from \File{scrwfile.dtx}.}%
}
\labelbase{scrwfile}

\BeginIndex{Package}{scrwfile}

%\fi^^A meta comment
% \begin{abstract}
\TeX{} supports 18 write handles only. Handle 0 is used by \TeX{} itself (log
file). \LaTeX{} needs at least handle 1 for \Macro{@mainaux}, handle 2 for
\Macro{@partaux}, one handle for \Macro{tableofcontents}, one handle for
\Macro{listoffigures}, one handle for \Macro{listoftables}, one handle for
\Macro{makeindex}. So there are 11 left. Seams a lot and enough. But every new
type of float, every new index and several other packages, e.g.,
\Package{hyperref} need write handles, too. 

The bottom line is, that this eventually will result in the error message:
\begin{lstcode}
  ! No room for a new \write .
  \ch@ck ...\else \errmessage {No room for a new #3}
                                                    \fi 
\end{lstcode}

Last but not least, immediately opening one more write handle for every table
of contents, list of figures, list of tables, and so on has one more
disadvantage. These are not only set by the corresponding commands, they also
couldn't not be set once more, because their helper files are empty after the
corresponding commands until the end of the document.

Package \Package{scrwfile} provides a general change of the \LaTeX{} kernel,
that may solve both problems.
% \end{abstract}
%
% \tableofcontents

\section{General Modifications of the \LaTeX{} Kernel}
\label{sec:scrwfile.kernelpatches}

\LaTeX{} classes use the \LaTeX{} kernel command
\Macro{@starttoc}\IndexCmd{@starttoc} for output and opening of a new table of
contents, list of float, e.\,g., \Macro{listoffigures} or
\Macro{listoftables}, or similar. Thereby \LaTeX{} not only reads the helper
file with the contents of the directory, but opens the helper file for writing
also. Nevertheless, if afterwards new entries to these lists of floats will be
made using \Macro{addcontentsline}, then this will not write immediately to
the helper file. Instead of \LaTeX{} writes
\Macro{@writefile}\IndexCmd{@writefile} commands into the
\File{aux}-file. Only while reading the \File{aux}-file while the end of the
document, those \Macro{@writefile} commands generates real write operations
into the helper files. \LaTeX{} even doesn't close the helper files
explicitly. Instead of \LaTeX{} counts on \TeX{} to close all open files at
the end.

This procedure means, that all the helper files are open for writing from the
output of the contents, i.\,e. at the front matter of the document until the
end of the document, though their content is written after the end of the
document. \Package{scrwfile}'s basic approach is exactly this: redefinition of
\Macro{@starttoc} and \Macro{@writefile}.

Surely\textnote{Attention!}, changes of the \LaTeX{} kernel always may
potentially result in incompatibilities with other packages. In case of
\Package{scrwfile} this may be happen with all packages, that redefine
\Macro{@starttoc} or \Macro{@writefile} too. Sometimes changing the order of
loading the packages may help.

In fact, such problems with \Package{scrwfile} haven't been reported currently,
though several users have tested the package for more than a year until first
release. Nevertheless, if you find such a problem, please contact the
\KOMAScript{} author.


\section{The Single File Feature}
\label{sec:scrwfile.singlefilefeature}

Just while loading the package using, e.\,g.,
\begin{lstcode}[belowskip=\dp\strutbox]
  \usepackage{scrwfile}
\end{lstcode}
\Package{scrwfile} will redefine \Macro{@starttoc}\IndexCmd{@starttoc} to not
longer allocate a file handle or open any file for writing. Immediately before
closing the \File{aux}-file in \Macro{end}\PParameter{document} it will
redefine \Macro{@writefile} to not longer write into the usual helper files
but into one single new file with file extension \File{wrt}. After
reading the \File{aux}-file this \File{wrt}-file will be processed once per
helper file. This means, that not all of the helper file have to be open at
the same time, but only one at a time. And this single file will be closed
afterwards and the write handle is not longer needed after it is closed. An
internal write handle of \LaTeX{} is used for this. So \Package{scrwfile}
doesn't need any own write handle.

Because of this, even if only one table of contents should be generated, after
loading \Package{scrwfile} one more write file handle will be available,
e.\,g., for bibliographies, indexes, glossaries and similar, that are not
using \Macro{@starttoc}. Additionally the number of tables of contents and
lists of whatever, that use \Macro{@starttoc}\IndexCmd{@starttoc} won't be
limited any longer.


\section{The Clone File Write Feature}
\label{sec:scrwfile.clonefilefeature}

Sometimes it is useful to input a file not only once but several times. As
\Macro{@starttoc} does not open files for writing any longer, this may be
done by simply using \Macro{@starttoc} several times with the same
extension. But sometimes you may have additional entries in only some of the
content directories. Because of this, \Package{scrwfile} allows to copy all
entries of a file to another file, too. We call this cloning.

%\iffalse^^A meta-comment
\begin{Declaration}
%\fi^^A meta-comment
% \DescribeMacro{\TOCclone}
% The command
  \Macro{TOCclone}%
  \OParameter{heading}\Parameter{source}\Parameter{destination}
%\iffalse^^A meta-comment
\end{Declaration}
\BeginIndex{Cmd}{TOCclone}%
%\fi^^A meta-comment
activates the clone feature for files with extensions \PName{source} and
\PName{destination}. All entries to the file
\Macro{jobname}\File{.}\PName{source} will be added to
\Macro{jobname}\File{.}\PName{destination}, too.

If extension \PName{destination} is a new one, \PName{destination} will be
added to the list of known extensions using the \KOMAScript{} package
\Package{tocbasic}.

If the optional argument \PName{heading} is given, a new list-of macro
\Macro{listof}\PName{destination} is defined. \PName{heading} will be used
as section (or chapter) heading of this list. In this case several
\Package{tocbasic} features of the \PName{source} will be copied to
\PName{destination}, if and only if they have been set up when
\Macro{TOCclone} was used. Feature \PName{nobabel} will always be set,
because the language selection commands are part of the helper file and
would be cloned, too.

% \subparagraph*{How you can use it:} 
%\iffalse^^A meta-comment
\begin{Example}
%\fi^^A meta-comment
Assumed, you want a short table of contents
with only the chapter level but an additional entry with the table of
contents:
\begin{lstcode}
  \usepackage{scrwfile}
  \TOCclone[Short \contentsname]{toc}{stoc}
\end{lstcode}
This would create a new table of contents with the heading »Short
Contents«. The new table of contents uses a helper file with extension
\File{stoc}. All entries to the helper file with extension \File{toc} will
also be copied to this new helper file.

The new short table of contents should only have the chapter entries. This may
be done using:
\begin{lstcode}
  \addtocontents{stoc}{\protect\value{tocdepth}=0}
\end{lstcode}
Normally you cannot write into a helper file before
\Macro{begin}\PParameter{document}. But using \Package{scrwfile} changes
this. So the code above will be successful after loading \Package{scrwfile}.

To show the new short contents of helper file extension \File{stoc} we will
use
\begin{lstcode}[moretexcs={listofsttoc}]
  \listofstoc
\end{lstcode}
somewhere after \Macro{begin}\PParameter{document}.

If we also want an entry for the table of contents at the short contents, we
cannot use
\begin{lstcode}
  \addtocontents{toc}{% write to the Contents
    \protect\addcontentslinedefault{stoc}% write to Short Contents
      {chapter}% a chapter entry with
      {\contentsname}% the Contents' name
  }
\end{lstcode}
because the \Macro{addcontentsline} command would be copied to \File{stoc}
too. So we cannot add the command to the \File{toc}-file. Package
\Package{tocbasic}\important{\Package{tocbasic}} may be used to solve this:
\csname phantomsection\endcsname\label{example:scrwfile.AfterStartingTOC}
\begin{lstcode}
  \AfterStartingTOC[toc]{%
    \addcontentslinedefault{stoc}{chapter}
                    {\protect\contentsname}%
  }
\end{lstcode}
But this needs, that the file with extension \File{toc} is under control of
package \Package{tocbasic}, which is indeed the case within all \KOMAScript{}
classes.
%\iffalse^^A meta-comment
See \autoref{sec:tocbasic.toc} on
\autopageref{desc:tocbasic.cmd.AfterStartingTOC} for more information about
\Macro{AfterStartingTOC}.%
\end{Example}
\EndIndex{Cmd}{TOCclone}

\section{State of Development Note}
\label{sec:scrwfile.draft}

Though this package has been tested by several users and even is in
productivity usage several times, development is not finished yet. Because of
this especially internal functionality may be changed in future. Most likely
the package will be extended. Some code for extensions is already in the
package. Nevertheless, currently user haven't make requests for such
extension. So there's currently no user documentation for this.

\EndIndex{Package}{scrwfile}

%\fi^^A meta-comment
 
%\iffalse
%</dtx|doc>
%\fi
%
% \StopEventually{%
%   \clearpage
%   \PrintIndex\PrintChanges}
%
%
% \section{Implementation}
%
% \iffalse
%<*package>
%<*identify>
% \fi
%
%    \begin{macrocode}
\PackageWarningNoLine{scrwfile}{%
  THIS IS AN ALPHA VERSION!\MessageBreak
  USAGE OF THIS VERSION 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.%
}
%    \end{macrocode}
%\iffalse
%</identify>
%\fi
%
% \subsection{Options}
% \iffalse
%<*option>
% \fi
%
% Currently we don't need options.
%
% \iffalse
%</option>
% \fi
%
% \subsection{Body}
% \iffalse
%<*body>
% \fi
%
% \subsubsection{Needed Packages}
%
% Package \textsf{scrbase} is needed, because of using several \KOMAScript{}
% basic commands.
%    \begin{macrocode}
\RequirePackage{scrbase}[2010/09/17]
%    \end{macrocode}
%
% Package \textsf{tocbasic} is needed for the lists of cloned TOCs.
%    \begin{macrocode}
\RequirePackage{tocbasic}[2010/10/01]
%    \end{macrocode}
%
% Package \textsf{scrlfile} is needed because of the \texttt{aux} file
% handling and \verb|\protected@immediate@write|.
%    \begin{macrocode}
\RequirePackage{scrlfile}[2010/09/30]
%    \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
%  |\@starttoc| and |\@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 |\@writefile| to do so.
% \end{description}
% Every patch should be minimum invasive, so that files, that are not under
% \textsf{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 \textsf{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@only\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}
% \begin{macro}{\scrwfile@@starttoc}
% This is the internal redefinition of |\@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>  \typeout{Use 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}
% \end{macro}
%
% \begin{macro}{\scrwfile@writefile}
% \begin{macro}{\scrwfile@@writefile}
% \begin{macro}{\scrwfile@wrtout}
% \begin{macro}{\scrwfile@writefilelist}
% This is the internal redefinition of |\@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>  \typeout{Use 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}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@writefile}
% We have to add the single handle feature and the clone file feature to this.
% \begin{macro}{\scrwfile@saved@writefile}
% This is the original definition, that will be used, if the file is not under
% \texttt{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}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@starttoc}
% We have to add the single handle feature to this.
% \begin{macro}{\scrwfile@saved@starttoc}
%   \changes{v0.1e}{2013/07/24}{take care for unfriendly \textsl{hyperref}}
% This is the original definition, that will be used, if the file is not under
% \texttt{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}
% \end{macro}
%
%
% \subsubsection{Clone TOC Feature}
%
% \textsf{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{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{macro}{\TOCclone}
% 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|. \textbf{Note:} We use owner \texttt{TOCclone} for all cloned
% extensions.
%    \begin{macrocode}
\newcommand*{\TOCclone}[3][]{%
  \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}}{}%
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \iffalse
%</body>
%</package>
% \fi
%
% \Finale
%
\endinput
%
% end of file `scrwfile.dtx'

%%% Local Variables:
%%% mode: doctex
%%% mode: flyspell
%%% ispell-local-dictionary: "english"
%%% TeX-master: t
%%% End: