summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/spac-lin.mkiv
blob: ef87536f2db54d5d581fdf1670eee379617dbac6 (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
%D \module
%D   [       file=spac-lin,
%D        version=2012.01.08, % 2009.10.16 (core-ver) 1997.03.31 (core-spa)
%D          title=\CONTEXT\ Spacing Macros,
%D       subtitle=Vertical,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Spacing Macros / Lines}

\unprotect

%D When spacing is active we need to handle commands in
%D a special way:
%D
%D \starttyping
%D \setuplines[space=on]
%D
%D \startlines
%D Let's talk about this{\ttsl\gobbleoneargument or}that.
%D \stoplines
%D
%D \startlines
%D Let's talk about this{\getvalue{ttsl}or}that.
%D \stoplines
%D \stoptyping
%D
%D One can indent in several ways:
%D
%D \starttyping
%D \setupindenting[medium] \setuplines[indenting=odd] % no yes odd even
%D
%D \startlines
%D first
%D second
%D third
%D fourth
%D \stoplines
%D \stoptyping

%D Contrary to \MKII\ we can now define classes of lines (generalized by
%D Wolfgang). I will probably rewrite bits in \LUA.

\installcorenamespace{lines}

\installcommandhandler \??lines {lines} \??lines

\setuplines
  [\c!option=,
   \c!command=,
   \c!before=\blank,
   \c!after=\blank,
   \c!inbetween=\blank,
   \c!indenting=\v!no,
   \c!space=\v!default]

\appendtoks
   \setuevalue{\e!start\currentlines}{\spac_lines_start[\currentlines]}%
   \letvalue  {\e!stop \currentlines }\spac_lines_stop
\to \everydefinelines

\unexpanded\def\spac_lines_start[#1]%
  {\bgroup
   \edef\currentlines{#1}%
   \obeylines
   \dosingleempty\spac_lines_start_indeed}

\let\p_lines_option\empty

\let\spac_lines_break            \relax
\let\spac_after_first_obeyed_line\relax
\let\spac_lines_indent           \relax

%D See \type {indentation-005.tex}:
%D
%D \starttyping
%D \setuplines[indentnext={0pt,10pt,15pt,20pt}]
%D
%D \setupindenting[yes,1em]
%D \setuplines[indentnext={0pt,*,*}]
%D
%D \setuplines[indentnext={0pt,1em,*}]
%D \starttyping

\newcount\c_spac_lines_indent_cnt
\newcount\c_spac_lines_indent_max
\newdimen\d_spac_lines_indent

\def\spac_lines_indent_indeed
  {\ifnum\c_spac_lines_indent_cnt=\c_spac_lines_indent_max
     \c_spac_lines_indent_cnt\plusone
   \else
     \advance\c_spac_lines_indent_cnt\plusone
   \fi
   \getfromcommacommand[\m_spac_lines_indentnext][\c_spac_lines_indent_cnt]%
   \ifx\commalistelement\wildcardsymbol
     \hskip\d_spac_lines_indent
   \else
     \scratchdimen\dimexpr\commalistelement\relax
     \hskip\ifdim\scratchdimen=\zeropoint
       \zeropoint
     \else
       \scratchdimen
       \d_spac_lines_indent\scratchdimen
     \fi
   \fi
   \relax}

\def\spac_lines_start_indeed[#1]% new: optional second argument (WS)
  {\iffirstargument
     \setupcurrentlines[#1]%
   \fi
   \edef\p_lines_option{\linesparameter\c!option}%
   \ifx\p_lines_option\v!packed
     \let\spac_lines_break\nobreak
   \else
     \let\spac_lines_break\relax
   \fi
   \linesparameter\c!before
   \push_macro_checkindentation
   \whitespace
   \dostarttaggedchained\t!lines\currentlines\??lines
   \begingroup
   \uselinesstyleandcolor\c!style\c!color
   \useindentingparameter\linesparameter
   \usealignparameter\linesparameter
   \typesettinglinestrue
   \setupwhitespace[\v!none]% todo use fast variant
   %\obeylines % move upwards to keep spaces in the first line due to optional argument
   %
   \edef\m_spac_lines_indentnext{\linesparameter\c!indentnext}%
   \getcommacommandsize[\m_spac_lines_indentnext]%
   \c_spac_lines_indent_max\commalistsize
   \c_spac_lines_indent_cnt\zerocount
   \ifnum\c_spac_lines_indent_max>\plusone
     \letlinesparameter\c!indentnext\empty
     \let\spac_lines_indent\spac_lines_indent_indeed
     \d_spac_lines_indent\d_spac_indentation_par
   \else
     \let\spac_lines_indent\relax
   \fi
   %
   \ignorespaces
   \glet\spac_after_first_obeyed_line\spac_lines_after_first_obeyed_line_a
   \let\obeyedline\spac_lines_obeyed_line
   \activatespacehandler{\linesparameter\c!space}%
   \dostarttagged\t!line\empty
   \ignorepars}


% we could have states instead and handle option in there

\def\spac_lines_after_first_obeyed_line_a % tzt two pass, like itemize
  {\linesparameter\c!command
   \spac_lines_indent
   \linesparameter\c!left
   \glet\spac_after_first_obeyed_line\spac_lines_after_first_obeyed_line_b}

\def\spac_lines_after_first_obeyed_line_b
  {\spac_lines_break
   \linesparameter\c!command
   \spac_lines_indent
   \linesparameter\c!left}

\def\spac_lines_obeyed_line
  {\ifdone\linesparameter\c!right\fi
   \dostoptagged % can be a dummy one as we don't look ahead
   \par
   \dostarttagged\t!line\empty
   \futurelet\next\spac_lines_between}

\unexpanded\def\spac_lines_stop
  {\dostoptagged
   \endgroup
   \dostoptagged
   \pop_macro_checkindentation
   \linesparameter\c!after
   \egroup}

\def\spac_lines_between
  {\ifx\next\spac_lines_stop
     \donefalse
   \else
     \doifelsemeaning\next\obeyedline % brrr
       {\donefalse\linesparameter\c!inbetween}
       {\donetrue\spac_after_first_obeyed_line}%
   \fi}

\definelines[\v!lines]

\unexpanded\def\emptylines
  {\dosingleempty\spac_lines_empty}

% \def\spac_lines_empty[#1]%
%   {\endgraf
%    \dorecurse{\iffirstargument#1\else3\fi}\crlf}

\def\spac_lines_empty[#1]%
  {\endgraf
   \begingroup
  %\forgetall % debatable
   \ifhmode
      \crlf % finish the current line
   \fi
   \dorecurse{\iffirstargument#1\else3\fi}{\strut\crlf}%
   \endgroup}

\ifdefined\startlines \else
    \expandafter\let\expandafter\startlines\csname\e!start\v!lines\endcsname
    \expandafter\let\expandafter\stoplines \csname\e!stop \v!lines\endcsname
\fi

\protect \endinput