| 1 |
\ifx\DOLINES\relax\endinput\else\let\DOLINES=\relax\fi % \input only once |
| 2 |
% |
| 3 |
% dolines.tex: Meta-macros to separate arguments by newlines and by |
| 4 |
% empty lines. |
| 5 |
% version: 1.0 release: 15 April 1991 |
| 6 |
% |
| 7 |
% copyright (c) 1991 Marcel R. van der Goot |
| 8 |
% You can use these macros to typeset documents. You may |
| 9 |
% distribute this file freely, provided that you also distribute |
| 10 |
% the accompanying documentation. |
| 11 |
% You may make changes to this file, or extract portions |
| 12 |
% of it for inclusion in other files, provided that |
| 13 |
% (1) you change the name of the file; |
| 14 |
% (2) you give proper credit and include copyright |
| 15 |
% information where applicable; |
| 16 |
% (3) explain how an unchanged version can be obtained; and |
| 17 |
% (4) document the usage of your macros/changes (if usage |
| 18 |
% of your macros is not worth documenting, they must not |
| 19 |
% be worth using). |
| 20 |
% You are not allowed to use the name ``Midnight Macros'' for |
| 21 |
% any changed files. |
| 22 |
% The above rules for making changes do not apply where it |
| 23 |
% is explicitly noted in this file that something can be changed |
| 24 |
% to conform to your local installation. |
| 25 |
% |
| 26 |
% USAGE: |
| 27 |
% See the file dolines.doc |
| 28 |
% You need Midnight/loop.tex to use these macros. |
| 29 |
% |
| 30 |
% original: csvax.cs.caltech.edu [131.215.131.131] in pub/tex |
| 31 |
% (use anonymous ftp). Also in various archives. |
| 32 |
% |
| 33 |
% Caltech, Pasadena --- Marcel van der Goot |
| 34 |
% [email protected] |
| 35 |
% Caltech 256--80 |
| 36 |
% Pasadena, CA 91125 |
| 37 |
% USA |
| 38 |
% (818) 356--4603 |
| 39 |
% |
| 40 |
|
| 41 |
% update history: |
| 42 |
% version 1.0: This one. |
| 43 |
% release 15 April 1991: This one. |
| 44 |
% release 8 February 1991: Used \next instead of \do_next, which |
| 45 |
% occasionally conflicted with use of \next in other macros. |
| 46 |
|
| 47 |
%%%%%% CODE: (you don't need to read this to use the macros) |
| 48 |
|
| 49 |
\input Midnight/loop % CHANGE this filename to correspond to your installation |
| 50 |
|
| 51 |
%%%%%%% Reading from a different file |
| 52 |
% This is relatively simple, it can be done with a loop. The read function |
| 53 |
% reads a single line (that is what \read does). A file that is being |
| 54 |
% \read always ends with an empty line, therefore no test for eof(lines_in) |
| 55 |
% is needed in the inner loop. |
| 56 |
% |
| 57 |
% while (!eof(lines_in)) |
| 58 |
% { |
| 59 |
% read(lines_in, _ln); |
| 60 |
% if (_ln != empty_ln) |
| 61 |
% { |
| 62 |
% beforelines(); |
| 63 |
% do { everyline(_ln); |
| 64 |
% read(lines_in, _ln); |
| 65 |
% } while (_ln != empty_ln); |
| 66 |
% afterlines(); |
| 67 |
% } |
| 68 |
% } |
| 69 |
% |
| 70 |
|
| 71 |
{\catcode`\^^M=12 \endlinechar=-1 % 12 = other |
| 72 |
\catcode`\_=11 % 11 = letter (used to make macros private) |
| 73 |
|
| 74 |
\newread\lines_in |
| 75 |
|
| 76 |
\globaldefs=1 |
| 77 |
|
| 78 |
\def\empty_ln{\par} |
| 79 |
|
| 80 |
\def\filedolines#1% |
| 81 |
{{\openin\lines_in=#1 % |
| 82 |
\ifeof\lines_in\message{File #1 not found.}\fi |
| 83 |
\Loop\ifeof\lines_in \Break1 \fi |
| 84 |
\read\lines_in to\_ln |
| 85 |
\ifx\_ln\empty_ln |
| 86 |
\else {\beforelines |
| 87 |
\Loop\everyline{\_ln}% |
| 88 |
\read\lines_in to\_ln |
| 89 |
\ifx\_ln\empty_ln \Break1 \fi |
| 90 |
\Pool |
| 91 |
\afterlines |
| 92 |
}% |
| 93 |
\fi |
| 94 |
\Pool |
| 95 |
\closein\lines_in |
| 96 |
}} |
| 97 |
|
| 98 |
} % restore \catcode`\^^M, \catcode`\_, etc. |
| 99 |
|
| 100 |
|
| 101 |
%%%%%%% Reading from the current file |
| 102 |
% This is more complicated. The problem is that a line can only be |
| 103 |
% read at the end of a macro, because otherwise a part of the macro |
| 104 |
% itself will be read. Therefore, the above solution with \Loop cannot |
| 105 |
% be used. Instead, this is implemented as a finite state machine. The |
| 106 |
% FSM has two major states, outer_loop and inner_loop, corresponding to the |
| 107 |
% program above. In these states, the value of _ln is used to determine |
| 108 |
% what actions to take, and what the next state is. The next state |
| 109 |
% is indicated by the control-sequence \do_next. |
| 110 |
% There are two minor states, get_out and get_in. In each of them |
| 111 |
% the next line is read into _ln. After get_out, the next state is always |
| 112 |
% outer_loop; after get_in it is always inner_loop. |
| 113 |
% Here is a ``picure.'' `(...)' is a state, `...?' denotes the value |
| 114 |
% of _ln, and other texts are actions. ctrl-M stands for an empty line. |
| 115 |
% |
| 116 |
% /---<----\ |
| 117 |
% (get_out) | |
| 118 |
% \ ctrl-M? |
| 119 |
% begindolines V / |
| 120 |
% \->bgroup -> (get_out) -> (outer_loop) -> enddolines? -> egroup |
| 121 |
% ^ | ^ \-> (finish) |
| 122 |
% / | \ |
| 123 |
% afterlines --/ other? \-- afterlines |
| 124 |
% ^ V ^ |
| 125 |
% | beforelines | |
| 126 |
% ctrl-M? <--\ | /--> enddolines? |
| 127 |
% \ V / |
| 128 |
% (inner_loop) |
| 129 |
% / ^ |
| 130 |
% other? \ |
| 131 |
% | (get_in) |
| 132 |
% V | |
| 133 |
% \-everyline->/ |
| 134 |
% |
| 135 |
|
| 136 |
|
| 137 |
{\catcode`\^^M=12 \endlinechar=-1 % 12 = other |
| 138 |
\catcode`\_=11 % 11 = letter (used to make macros private) |
| 139 |
\globaldefs=1 |
| 140 |
|
| 141 |
|
| 142 |
\long\def\get_out#1^^M% here a line is read (as #1) |
| 143 |
{\gdef\_ln{#1}% |
| 144 |
\outer_loop |
| 145 |
} |
| 146 |
|
| 147 |
\long\def\get_in#1^^M% here a line is read (as #1) |
| 148 |
{\gdef\_ln{#1}% |
| 149 |
\inner_loop |
| 150 |
} |
| 151 |
|
| 152 |
\def\outer_loop |
| 153 |
{\ifx\_ln\enddolines \egroup\let\do_next=\finishdolines |
| 154 |
\else\ifx\_ln\empty \let\do_next=\get_out |
| 155 |
\else \beforelines\let\do_next=\inner_loop |
| 156 |
\fi \fi |
| 157 |
\do_next |
| 158 |
} |
| 159 |
|
| 160 |
\def\inner_loop |
| 161 |
{\ifx\_ln\enddolines \afterlines\let\do_next=\outer_loop |
| 162 |
\else\ifx\_ln\empty \afterlines\let\do_next=\outer_loop |
| 163 |
\else \everyline\_ln\let\do_next=\get_in |
| 164 |
\fi \fi |
| 165 |
\do_next |
| 166 |
} |
| 167 |
|
| 168 |
\def\begindolines |
| 169 |
{\bgroup |
| 170 |
\catcode`\^^M=12 % |
| 171 |
\get_out |
| 172 |
} |
| 173 |
|
| 174 |
} % restore \catcode`\^^M, \catcode`\_, etc. |
| 175 |
|
| 176 |
% We define \finishdolines and \enddolines, in case someone forgets: |
| 177 |
|
| 178 |
\let\finishdolines=\relax |
| 179 |
\def\enddolines{\enddolines} |
| 180 |
|