lang-mis.mkxl /size: 16 Kb    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=lang-mis,
3%D        version=1997.03.20, % used to be supp-lan.tex
4%D          title=\CONTEXT\ Language Macros,
5%D       subtitle=Compounds,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D This one will be updated stepwise to \LMTX. See lang-mis.mkiv for previous
15%D implementations and removed code.
16
17\writestatus{loading}{ConTeXt Language Macros / Compounds}
18
19%D More or less replaced.
20
21%D \gdef\starttest#1\stoptest{\starttabulate[|l|l|p|]#1\stoptabulate}
22%D \gdef\test     #1{\NC\detokenize{#1}\NC\hyphenatedword{#1}\NC#1\NC\NR}
23
24\unprotect
25
26%D One of \TEX's strong points in building paragraphs is the way hyphenations are
27%D handled. Although for real good hyphenation of non||english languages some
28%D extensions to the program are needed, fairly good results can be reached with the
29%D standard mechanisms and an additional macro, at least in Dutch.
30%D
31%D \CONTEXT\ originates in the wish to typeset educational materials, especially in
32%D a technical environment. In production oriented environments, a lot of compound
33%D words are used. Because the Dutch language poses no limits on combining words, we
34%D often favor putting dashes between those words, because it facilitates reading,
35%D at least for those who are not that accustomed to it.
36%D
37%D In \TEX\ compound words, separated by a hyphen, are not hyphenated at all. In
38%D spite of the multiple pass paragraph typesetting this can lead to parts of words
39%D sticking into the margin. The solution lays in saying \type
40%D {spoelwater||terugwinunit} instead of \type {spoelwater-terugwinunit}. By using a
41%D one character command like \type {|}, delimited by the same character \type {|},
42%D we get ourselves both a decent visualization (in \TEXEDIT\ and colored verbatim
43%D we color these commands yellow) and an efficient way of combining words.
44%D
45%D The sequence \type{||} simply leads to two words connected by a hyphen. Because
46%D we want to distinguish such a hyphen from the one inserted when \TEX\ hyphenates
47%D a word, we use a bit longer one.
48%D
49%D \hyphenation {spoel-wa-ter te-rug-win-unit}
50%D
51%D \starttest
52%D \test {spoelwater||terugwinunit}
53%D \stoptest
54%D
55%D As we already said, the \type{|} is a command. This commands accepts an optional
56%D argument before it's delimiter, which is also a \type{|}.
57%D
58%D \hyphenation {po-ly-meer che-mie}
59%D
60%D \starttest
61%D \test {polymeer|*|chemie}
62%D \stoptest
63%D
64%D Arguments like \type{*} are not interpreted and inserted directly, in contrary to
65%D arguments like:
66%D
67%D \starttest
68%D \test {polymeer|~|chemie}
69%D \test {|(|polymeer|)|chemie}
70%D \test {polymeer|(|chemie|)| }
71%D \stoptest
72%D
73%D Although such situations seldom occur |<|we typeset thousands of pages before we
74%D encountered one that forced us to enhance this mechanism|>| we also have to take
75%D care of comma's.
76%D
77%D  \hyphenation {uit-stel-len}
78%D
79%D  \starttest
80%D  \test {op||, in|| en uitstellen}
81%D  \stoptest
82%D
83%D The next special case (concerning quotes) was brought to my attention by Piet
84%D Tutelaers, one of the driving forces behind rebuilding hyphenation patterns for
85%D the dutch language.\footnote{In 1996 the spelling of the dutch language has been
86%D slightly reformed which made this topic actual again.} We'll also take care of
87%D this case.
88%D
89%D \starttest
90%D \test {AOW|'|er}
91%D \test {cd|'|tje}
92%D \test {ex|-|PTT|'|er}
93%D \test {rock|-|'n|-|roller}
94%D \stoptest
95%D
96%D Tobias Burnus pointed out that I should also support something like
97%D
98%D \starttest
99%D \test {well|_|known}
100%D \stoptest
101%D
102%D to stress the compoundness of hyphenated words.
103%D
104%D Of course we also have to take care of the special case:
105%D
106%D \starttest
107%D \test {text||color and ||font}
108%D \stoptest
109
110%D \macros
111%D   {installdiscretionaries}
112%D
113%D The mechanism described here is one of the older inner parts of \CONTEXT. The
114%D most recent extensions concerns some special cases as well as the possibility to
115%D install other characters as delimiters. The prefered way of specifying compound
116%D words is using \type{||}, which is installed by:
117%D
118%D \starttyping
119%D \installdiscretionary | -
120%D \stoptyping
121%D
122%D We used to have an installable mechanism but in the perspective of \MKIV\ and
123%D especialy \LMTX\ it no longer makes sense to complicate the code, so from now on
124%D we only deal with the active bar. Older code can be seen in the archives. It also
125%D means that we now just hardcode the bar. We also deal with math differently.
126
127%D \macros
128%D   {compoundhyphen}
129%D
130%D Now let's go to the macros. First we define some variables. In the main \CONTEXT\
131%D modules these can be tuned by a setup command. Watch the (maybe) better looking
132%D compound hyphen.
133
134% hm why ex
135
136\ifdefined\compoundhyphen \else
137    \permanent\protected\def\compoundhyphen{\hbox{-\kern-.10775\emwidth-}} % .25\exheight
138\fi
139
140%D The last two variables are needed for subsentences |<|like this one|>| which we
141%D did not yet mention. We want to enable breaking but at the same time don't want
142%D compound characters like |-| or || to be separated from the words. \TEX\ hackers
143%D will recognise the next two macro's:
144
145\ifdefined\prewordbreak \else \permanent\protected\def\prewordbreak    {\penalty\plustenthousand\hskip\zeroskip\relax} \fi
146\ifdefined\postwordbreak\else \permanent\protected\def\postwordbreak   {\penalty\zerocount      \hskip\zeroskip\relax} \fi
147\ifdefined\hspaceamount \else                     \def\hspaceamount#1#2{.16667\emwidth}                                 \fi % will be overloaded
148
149\permanent\protected\def\permithyphenation{\ifhmode\wordboundary\fi} % doesn't remove spaces
150
151%D \macros
152%D   {beginofsubsentence,endofsubsentence,
153%D    beginofsubsentencespacing,endofsubsentencespacing}
154%D
155%D In the previous macros we provided two hooks which can be used to support nested
156%D sub||sentences. In \CONTEXT\ these hooks are used to insert a small space when
157%D needed.
158%D
159%D The following piece of code is a torture test compound handling. The \type
160%D {\relax} before the \type {\ifmmode} is needed because of the alignment scanner
161%D (in \ETEX\ this problem is not present because there a protected macro is not
162%D expanded. Thanks to Tobias Burnus for providing this example.
163%D
164%D \startformula
165%D   \left|f(x_n)-{1\over2}\right| =
166%D      {\cases{|{1\over2}-x_n| &for $0\le x_n < {1\over2}$\cr
167%D              |x_n-{1\over2}| &for ${1\over2}<x_n\le1$   \cr}}
168%D \stopformula
169
170\installcorenamespace{discretionaryaction}
171\installcorenamespace{discretionarymode}
172
173\aliased\let\installdiscretionaries\gobbletwoarguments % this alias will go
174\aliased\let\installdiscretionary  \gobbletwoarguments % this alias will go
175
176\setnewconstant\discretionarymode\plusone
177
178\permanent\protected\def\ignorediscretionaries{\discretionarymode\zerocount}
179\permanent\protected\def\obeydiscretionaries  {\discretionarymode\plusone}
180
181\def\lang_discretionaries_command
182  {\begincsname\??discretionarymode
183     \ifcase\discretionarymode
184       n%
185     \else
186       t%
187     \fi
188   \endcsname}
189
190% \catcode\barasciicode\activecatcode
191% \amcode \barasciicode\othercatcode
192
193\letcatcodecommand\ctxcatcodes\barasciicode\lang_discretionaries_command
194
195%D The macro \type {\lang_discretionaries_check_before} takes care of loners like
196%D \type {||word}, while it counterpart \type {\lang_discretionaries_check_after} is
197%D responsible for handling the comma.
198
199\newconditional\punctafterdiscretionary
200\newconditional\spaceafterdiscretionary
201
202\def\lang_discretionaries_check_before %is used grouped
203  {\ifvmode
204     \dontleavehmode
205   \fi
206   \ifhmode
207    %\begingroup
208    %\setbox\scratchbox\lastbox
209    %\ifzeropt\wd\scratchbox
210    %  \box\scratchbox\relax
211    %  \endgroup
212    %  \let\postwordbreak\prewordbreak
213    %\else
214    %  \box\scratchbox\relax
215    %  \endgroup
216    %\fi
217   \fi}
218
219\def\lang_discretionaries_check_after
220  {\punctafterdiscretionary\conditionalfalse
221   \spaceafterdiscretionary\conditionalfalse
222   \ifx\blankspace\nexttoken \spaceafterdiscretionary\conditionaltrue \orelse
223   \ifx\space     \nexttoken \spaceafterdiscretionary\conditionaltrue \orelse
224   \ifx          .\nexttoken \punctafterdiscretionary\conditionaltrue \orelse
225   \ifx          ,\nexttoken \punctafterdiscretionary\conditionaltrue \orelse
226   \ifx          :\nexttoken \punctafterdiscretionary\conditionaltrue \orelse
227   \ifx          ;\nexttoken \punctafterdiscretionary\conditionaltrue \fi}
228
229\edefcsname\??discretionarymode n\endcsname
230  {\detokenize{|}}
231
232\defcsname\??discretionarymode t\endcsname#1|%
233  {\bgroup
234   \def\next{\lang_discretionaries_handle{#1}}%
235   \futurelet\nexttoken\next}%
236
237\permanent\protected\def\lang_discretionaries_handle#1%
238  {\edef\lang_discretionaries_token{\detokenize{#1}}%
239   \lang_discretionaries_check_after
240   \ifempty\lang_discretionaries_token
241     \ifx|\nexttoken % takes care of |||
242       \ifcsname\??discretionaryaction\string|\endcsname
243         \lastnamedcs
244       \orelse\ifconditional\spaceafterdiscretionary
245         \wordboundary\hbox{\letterbar}\relax
246       \orelse\ifconditional\punctafterdiscretionary
247         \wordboundary\hbox{\letterbar}\wordboundary
248       \else
249         \wordboundary\hbox{\letterbar}\wordboundary
250       \fi
251       \def\next{\afterassignment\egroup\let\next=}%
252     \else
253       \lang_discretionaries_check_before
254       \ifcsname\??discretionaryaction\endcsname
255         \lastnamedcs
256       \orelse\ifconditional\spaceafterdiscretionary
257         \wordboundary\defaultdiscretionaryhyphen\relax
258       \orelse\ifconditional\punctafterdiscretionary
259         \wordboundary\defaultdiscretionaryhyphen\relax
260       \else
261         \wordboundary\defaultdiscretionaryhyphen\wordboundary
262       \fi
263       \let\next\egroup
264     \fi
265   \orelse\ifcsname\??discretionaryaction\lang_discretionaries_token\endcsname
266     \lastnamedcs
267     \let\next\egroup
268   \else
269     \lang_discretionaries_check_before
270     \ifconditional\spaceafterdiscretionary
271       \wordboundary\hbox{#1}\relax
272     \orelse\ifconditional\punctafterdiscretionary
273       \wordboundary\hbox{#1}\relax
274     \else
275       \wordboundary\discretionary{\hbox{#1}}{}{\hbox{#1}}\wordboundary
276      %\discretionary options \plusthree{\hbox{#1}}{}{\hbox{#1}}%
277     \fi
278     \let\next\egroup
279   \fi
280   \next}
281
282%D \macros
283%D   {directdiscretionary}
284%D
285%D In those situations where the nature of characters is less predictable, we can
286%D use the more direct approach:
287
288\permanent\protected\def\directdiscretionary
289  {\begincsname\??discretionarymode
290     \ifcase\discretionarymode
291       n%
292     \else
293       d%
294     \fi
295   \endcsname}
296
297\permanent\protected\def\indirectdiscretionary
298  {\begincsname\??discretionarymode
299     \ifcase\discretionarymode
300       n%
301     \else
302       i%
303     \fi
304   \endcsname}
305
306\protected\defcsname\??discretionarymode d\endcsname#1%
307  {\ifcsname\??discretionaryaction\detokenize{#1}\endcsname
308     \expandafter\lastnamedcs
309   \else
310     \expandafter\indirectdiscretionary
311   \fi{#1}}
312
313\protected\defcsname\??discretionarymode i\endcsname#1%
314  {\wordboundary\discretionary{\hbox{#1}}{}{\hbox{#1}}\wordboundary}
315 %{\discretionary options \plusthree{\hbox{#1}}{}{\hbox{#1}}}
316
317\permanent\protected\def\definetextmodediscretionary #1
318  {\defcsname\??discretionaryaction\detokenize{#1}\endcsname}
319
320% \start \hsize 1mm
321% test |||test test|||, test\blank
322% test test|-|, test|-| and test|-|test\blank
323% test test|_|, test|_| and test|_|test\blank
324% test cd|'|tje\blank
325% test |(|test test|)|, test\blank
326% test test test|x|, test\blank
327% test|~|test
328% test|^|test
329% \stop
330
331% x\discretionary{1}{2}{3}xxxxxxx
332% xxxxxxx\discretionary{1}{2}{3}x
333%
334% xxx3xxx
335% xxx1<newline>2xxx
336
337\def\lang_discretionaries_hyphen_like#1#2%
338  {\ifconditional\spaceafterdiscretionary
339     \wordboundary\hbox{#1}\relax
340   \orelse\ifconditional\punctafterdiscretionary
341     \wordboundary\hbox{#1}\relax
342   \else
343     \wordboundary#2\wordboundary
344   \fi}
345
346\definetextmodediscretionary {}
347  {\lang_discretionaries_hyphen_like\textmodehyphen\textmodehyphendiscretionary}
348
349\definetextmodediscretionary -
350  {\lang_discretionaries_hyphen_like\normalhyphen\normalhyphendiscretionary}
351
352\definetextmodediscretionary _
353  {\lang_discretionaries_hyphen_like\composedhyphen\composedhyphendiscretionary}
354
355\definetextmodediscretionary )
356  {\lang_discretionaries_hyphen_like{)}{\discretionary{-)}{}{)}}}
357
358\definetextmodediscretionary (
359  {\ifdim\lastskip>\zeropoint
360     (\wordboundary
361   \else
362     \wordboundary\discretionary{}{(-}{(}\wordboundary
363    %\discretionary options \plusthree{}{(-}{(}%
364   \fi}
365
366\definetextmodediscretionary ~
367  {\wordboundary\discretionary{-}{}{\thinspace}\wordboundary}
368 %{\discretionary options \plusthree{-}{}{\thinspace}}
369
370\definetextmodediscretionary '
371  {\wordboundary\discretionary{-}{}{'}\wordboundary}
372 %{\discretionary options \plusthree{-}{}{'}}
373
374\definetextmodediscretionary ^
375  {\wordboundary
376   \discretionary{\hbox{\normalstartimath|\normalstopimath}}{}{\hbox{\normalstartimath|\normalstopimath}}%
377   \wordboundary} % bugged
378 %{\discretionary options \plusthree{\hbox{\normalstartimath|\normalstopimath}}{}{\hbox{\normalstartimath|\normalstopimath}}}
379
380\definetextmodediscretionary <
381  {\beginofsubsentence\wordboundary\beginofsubsentencespacing
382   \aftergroup\ignorespaces} % tricky, we need to go over the \nextnextnext
383
384\definetextmodediscretionary >
385  {\removeunwantedspaces
386   \endofsubsentencespacing\wordboundary\endofsubsentence}
387
388\definetextmodediscretionary =
389  {\removeunwantedspaces
390   \wordboundary\midsentence\wordboundary
391   \aftergroup\ignorespaces}
392
393% french
394
395\definetextmodediscretionary : {\removeunwantedspaces\wordboundary\kern\hspaceamount\empty{:}:}
396\definetextmodediscretionary ; {\removeunwantedspaces\wordboundary\kern\hspaceamount\empty{;};}
397\definetextmodediscretionary ? {\removeunwantedspaces\wordboundary\kern\hspaceamount\empty{?}?}
398\definetextmodediscretionary ! {\removeunwantedspaces\wordboundary\kern\hspaceamount\empty{!}!}
399
400\definetextmodediscretionary * {\wordboundary\discretionary{-}{}{\kern.05\emwidth}\wordboundary}
401
402% spanish
403
404\definetextmodediscretionary ?? {\wordboundary\questiondown}
405\definetextmodediscretionary !! {\wordboundary\exclamdown}
406
407\permanent\protected\def\defaultdiscretionaryhyphen{\compoundhyphen}
408
409%D \macros
410%D   {fakecompoundhyphen}
411%D
412%D In headers and footers as well as in active pieces of text we need a dirty hack.
413%D Try to imagine what is needed to savely break the next text across a line and at
414%D the same time make the words interactive.
415%D
416%D \starttyping
417%D \goto{Some||Long||Word}
418%D \stoptyping
419
420\permanent\protected\def\fakecompoundhyphen
421  {\enforced\permanent\protected\def\|{\mathortext\vert\lang_compounds_fake_hyphen}}
422
423\def\lang_compounds_fake_hyphen
424  {\enforced\permanent\protected\def##1|%
425     {\ifempty{##1}\compoundhyphen\else##1\fi
426      \wordboundary % was a signal
427      \allowbreak}}
428
429%D \macros
430%D   {midworddiscretionary}
431%D
432%D If needed, one can add a discretionary hyphen using \type
433%D {\midworddiscretionary}. This macro does the same as \PLAIN\ \TEX's \type {\-},
434%D but, like the ones implemented earlier, this one also looks ahead for spaces and
435%D grouping tokens.
436
437\permanent\protected\def\midworddiscretionary
438  {\futurelet\nexttoken\lang_discretionaries_mid_word}
439
440\def\lang_discretionaries_mid_word
441  {\ifx\nexttoken\blankspace\orelse
442   \ifx\nexttoken\bgroup    \orelse
443   \ifx\nexttoken\egroup    \orelse
444     \discretionary{-}{}{}%
445   \fi}
446
447% \aliased\let\ignorecompoundcharacter\relax
448
449%D \macros
450%D   {disablediscretionaries,disablecompoundcharacter}
451%D
452%D Occasionally we need to disable this mechanism. For the moment we assume that
453%D \type {|} is used.
454
455\aliased\let\disablediscretionaries   \ignorediscretionaries
456%aliased\let\disablecompoundcharacters\ignorecompoundcharacter
457
458%D \macros
459%D   {normalcompound}
460%D
461%D Handy in for instance XML. (Kind of obsolete)
462
463\ifdefined\normalcompound \else \aliased\let\normalcompound=| \fi
464
465%D \macros
466%D   {compound}
467%D
468%D We will overload the already active \type {|} so we have to save its meaning in
469%D order to be able to use this handy macro.
470%D
471%D \starttyping
472%D so test\compound{}test can be used instead of test||test
473%D \stoptyping
474
475\permanent\protected\gdef\compound#1{|#1|}
476
477\appendtoks
478    \enforced\permanent\protected\def|#1|{\ifx#1\empty\empty-\else#1\fi}%
479\to \everysimplifycommands
480
481%D Here we hook some code into the clean up mechanism needed for verbatim data.
482
483\appendtoks
484    %disablecompoundcharacters
485    \disablediscretionaries
486\to \everycleanupfeatures
487
488%D Here:
489%D
490%D \startbuffer
491%D {\red somelongword}{\blue \compounddiscretionary}{\green somelongword}
492%D \stopbuffer
493%D
494%D \typebuffer \blank {\hsize3mm\getbuffer\par} \blank
495
496\permanent\protected\def\compounddiscretionary
497   {\discretionary
498      options \plusthree
499      {\ifnum\prehyphenchar >\zerocount\char\prehyphenchar \fi}%
500      {\ifnum\posthyphenchar>\zerocount\char\posthyphenchar\fi}%
501      {\ifnum\posthyphenchar>\zerocount\char\posthyphenchar\fi}}
502
503% \setcatcodetable\prtcatcodes % because we activated the bar
504
505\protect \endinput
506