strc-enu.mklx /size: 14 Kb    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=strc-enu, % moved from strc-des
3%D        version=2008.10.20,
4%D          title=\CONTEXT\ Structure Macros,
5%D       subtitle=Enumerations,
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\writestatus{loading}{ConTeXt Structure Macros / Enumerations}
15
16\unprotect
17
18% command -> headcommand
19
20%D Enumerations are just super descriptions. Therefore we implement
21%D them as an extension. After all, the keys don't clash. In order
22%D to be able to differentiate between specific settings we use
23%D some flags. Unfortunately we have some historic differences
24%D in default values. Therefore we push an extra step in the
25%D parent chain (we might do the same for regular descriptions).
26
27%D In order to be more flexible with theorems Aditya Mahajan added
28%D support for titles and endsymbols. At the same time we added more
29%D flexible support for inheriting numbers.
30%D
31%D \startbuffer
32%D \defineenumeration[one]
33%D \defineenumeration[two]   [one] % clone one
34%D \defineenumeration[three] [counter=one,style=slanted] % only use counter of one
35%D \defineenumeration[four]  [three] % clone three
36%D \defineenumeration[five]  [three] [counter=five] % clone three and use own counter
37%D \defineenumeration[six]   [three] [counter=four] % clone tree and use counter four (undefined)
38%D
39%D \startone   test test  6 \stopone
40%D \starttwo   test test  7 \stoptwo
41%D \startthree test test  8 \stopthree
42%D \startfour  test test  9 \stopfour
43%D \startfive  test test  2 \stopfive
44%D \startsix   test test 10 \stopsix
45%D \stopbuffer
46%D
47%D \typebuffer \start \getbuffer \stop
48%D
49%D \starttyping
50%D \setupenumerations[one]  [prefix=yes,prefixsegments=section]
51%D \setupenumerations[two]  [prefix=yes,prefixsegments=section]
52%D \setupenumerations[three][prefix=yes,prefixsegments=section]
53%D \setupenumerations[four] [prefix=yes,prefixsegments=chapter:section]
54%D \setupenumerations[five] [prefix=yes,prefixsegments=chapter:section]
55%D \setupenumerations[six]  [prefix=yes,prefixsegments=chapter:section]
56%D \stoptyping
57
58% list and titles are experimental
59%
60% \definedescription[test] [location=left,hang=4,headalign={right},distance=1em,list=test]
61% \defineenumeration[lemma][title=yes,right=:,textdistance=1em, location=top, titlestyle=\bs,list=lemma]
62% \defineenumeration[ammel][title=yes,right=:,textdistance=.5em,location=left,titlestyle=\it,width=9em]
63%
64% \placelist[enumeration:lemma]
65% \placelist[description:test][width=0pt]
66%
67% \starttest  {something something something}    \input zapf  \stoptest
68% \startlemma {with a title of a certain length} \input tufte \stoplemma
69% \startammel {with a title}                     \input zapf  \stopammel
70%
71% \defineenumeration[lemma][...]
72% \defineenumeration[titledlemma][lemma][title=yes,right=:,text=lemma,list=lemma]
73
74\installcorenamespace{enumeration}
75
76\installcommandhandler \??enumeration {enumeration} \??enumeration
77
78\installcounterassociation{enumeration}
79
80\let\setupenumerations\setupenumeration
81
82\setupenumeration % check with old
83  [\c!alternative=\v!top,
84   \c!headstyle=\v!bold,
85   \c!titlestyle=\v!bold,
86  %\c!style=,
87  %\c!color=,
88  %\c!headcolor=,
89  %\c!titlecolor=,
90   \c!width=8\emwidth,
91  %\c!distance=\zeropoint,
92   \c!distance=\emwidth,
93   \c!titledistance=.5\emwidth,
94  %\c!hang=,
95  %\c!sample=,
96  %\c!align=,
97  %\c!headalign=,
98   \c!margin=\v!no,
99   \c!before=\blank,
100   \c!inbetween=\blank,
101   \c!after=\blank,
102   \c!indentnext=\v!yes,
103   \c!indenting=\v!never,
104   \c!titleleft=(,
105   \c!titleright=),
106  %\c!closesymbol=,
107   \c!closecommand=\wordright,
108   \c!display=\v!yes,
109  %\c!command=,
110   \c!titlecommand=,
111   \c!expansion=\v!no,
112  %\c!xmlsetup=,
113  %\s!catcodes=,
114   \c!way=\v!by\v!text,
115   \c!prefix=\v!no,
116   \c!prefixconnector=.,
117  %\c!starter=,
118  %\c!stopper=,
119   \c!text=\currentenumeration,
120   \c!number=\v!yes, % else description
121   \c!start=0,
122   \c!state=\v!start,
123   \c!levels=4,
124   \c!define=\v!yes]
125
126% to be considered:
127%
128% \let\??construction\??enumeration
129%
130% with push/pop (also at definition time)
131
132\protected\def\strc_define_commands_enumeration#tag#level#parent%
133  {\ifempty{#parent}%
134     \normalexpanded{\defineconstruction[#tag][\s!handler=\v!enumeration,\c!level=#level]}%
135     \edefcsname\??enumeration#tag:\s!parent\endcsname{\??enumeration}%
136   \else
137     \normalexpanded{\defineconstruction[#tag][#parent][\s!handler=\v!enumeration,\c!level=#level]}%
138     \edefcsname\??enumeration#tag:\s!parent\endcsname{\??enumeration#parent}%
139   \fi
140   \ifcstok{\enumerationparameter\c!define}\v!yes
141     \frozen\protected\instance\edefcsname\e!next #tag\endcsname{\strc_enumerations_next{#tag}{\number#level}}%  obsolete
142     \frozen\protected\instance\edefcsname\c!reset#tag\endcsname{\strc_enumerations_reset{#tag}{\number#level}}% obsolete
143     %frozen\protected\instance\edefcsname\c!set  #tag\endcsname{\strc_enumerations_set{#tag}{\number#level}}%   obsolete
144     \frozen\protected\instance\edefcsname        #tag\endcsname{\namedenumeration[#tag]}%
145     \frozen\protected\instance\edefcsname\e!start#tag\endcsname{\startnamedenumeration[#tag]}%
146     \frozen\protected\instance \defcsname\e!stop #tag\endcsname{\stopnamedenumeration}%
147   \fi}
148
149\lettonothing\m_strc_enumeration_sub
150
151\appendtoks
152    \lettonothing\m_strc_enumeration_sub
153    \ifempty\currentenumerationparent
154       % clone => parent | subclone => clone | subsubclone => subclone
155       \strc_define_commands_enumeration
156         {\m_strc_enumeration_sub\currentenumeration}%
157         \plusone
158         \empty
159       \edef\p_levels{\enumerationparameter\c!levels}%
160       \dostepwiserecurse\plustwo\p_levels\plusone
161         {\strc_define_commands_enumeration
162            {\v!sub\m_strc_enumeration_sub\currentenumeration}%
163            \recurselevel
164            {\m_strc_enumeration_sub\currentenumeration}%
165          \edef\m_strc_enumeration_sub{\v!sub\m_strc_enumeration_sub}}%
166   \else
167       % clone => parent | subclone => subparent | subsubclone => subsubparent
168       \edef\p_levels{\enumerationparameter\c!levels}%
169       \dorecurse\p_levels
170         {\strc_define_commands_enumeration
171            {\m_strc_enumeration_sub\currentenumeration}%
172            \recurselevel
173            {\m_strc_enumeration_sub\currentenumerationparent}%
174          \edef\m_strc_enumeration_sub{\v!sub\m_strc_enumeration_sub}}%
175   \fi
176   \edef\p_counter{\enumerationparameter\s!counter}% can inherit from parent
177   \ifempty\p_counter %
178       \let\p_counter\currentenumeration
179   \fi
180   \doifelsecounter\p_counter\donothing{\strc_enumerations_define_counter\p_counter}%
181   \letenumerationparameter\s!counter\p_counter
182  %\strc_enumerations_setup_counter\currentenumeration
183\to \everydefineenumeration
184
185\protected\defcsname\??constructioninitializer\v!enumeration\endcsname
186  {\let         \currentenumeration              \currentconstruction
187   \enforced\let\constructionparameter           \enumerationparameter
188   \enforced\let\constructionnamespace           \??enumeration
189   \enforced\let\detokenizedconstructionparameter\detokenizedenumerationparameter
190   \enforced\let\letconstructionparameter        \letenumerationparameter
191   \enforced\let\useconstructionstyleandcolor    \useenumerationstyleandcolor
192   \enforced\let\setupcurrentconstruction        \setupcurrentenumeration
193   \ifcstok{\constructionparameter\c!number}\v!yes
194     \c_strc_constructions_number_state\conditionaltrue
195     \iftrialtypesetting
196       \strc_counters_save\currentconstructionnumber
197     \fi
198     \strc_counters_increment_sub\currentconstructionnumber\currentconstructionlevel
199   \else
200     \c_strc_constructions_number_state\conditionalfalse
201   \fi
202   \ifcstok{\constructionparameter\c!title}\v!yes
203     \c_strc_constructions_title_state\conditionaltrue
204   \else
205     \c_strc_constructions_title_state\conditionalfalse
206   \fi}
207
208\protected\defcsname\??constructionfinalizer\v!enumeration\endcsname
209  {\ifconditional\c_strc_constructions_number_state
210     \iftrialtypesetting
211       \strc_counters_restore\currentconstructionnumber
212     \fi
213   \fi}
214
215%D Interfaces:
216
217\let\namedenumeration     \nameddescription
218\let\startnamedenumeration\startnameddescription
219\let\stopnamedenumeration \stopnameddescription
220
221\protected\def\strc_enumerations_next {\strc_constructions_next_indeed \namedenumerationparameter} % #1#2
222\protected\def\strc_enumerations_reset{\strc_constructions_reset_indeed\namedenumerationparameter} % #1#2
223%protected\def\strc_enumerations_set  {\strc_constructions_set_indeed  \namedenumerationparameter} % #1#2
224
225%D Counters (maybe we can share this code): (not ok yet, messy with notes)
226
227% Maybe we should move counters to the construction level as more derived mechanisms
228% use it \unknown\ so don't depend on names here!
229
230\def\strc_enumerations_define_counter#tag% todo: move inline
231  {\definecounter[#tag]%
232   \registerenumerationcounter{#tag}}
233
234\appendtoks
235    \synchronizeenumerationcounters
236\to \everysetupenumeration
237
238\appendtoks
239    \synchronizeenumerationcounters
240\to \everydefineenumeration
241
242%D Initializer:
243
244%D The handlers.
245
246\newtoks\everyenumeration
247
248\letcsname\??constructionmainhandler   \v!enumeration\expandafter\endcsname\csname\??constructionmainhandler   \v!description\endcsname
249\letcsname\??constructioncommandhandler\v!enumeration\expandafter\endcsname\csname\??constructioncommandhandler\v!description\endcsname
250\letcsname\??constructionstarthandler  \v!enumeration\expandafter\endcsname\csname\??constructionstarthandler  \v!description\endcsname
251\letcsname\??constructionstophandler   \v!enumeration\expandafter\endcsname\csname\??constructionstophandler   \v!description\endcsname
252
253\protected\defcsname\??constructiontexthandler\v!enumeration\endcsname
254  {\begingroup
255   \useconstructionstyleandcolor\c!headstyle\c!headcolor
256   \strc_enumerations_text
257   \endgroup}
258
259\protected\def\strc_enumerations_text
260  {\expand\everyenumeration % will become obsolete
261   \ifconditional\c_strc_constructions_number_state
262     \ifx\currentconstructionreference\!!plustoken
263       \strc_enumerations_full_number_symbol
264     \orelse\ifx\currentconstructionreference\!!minustoken
265       \strc_enumerations_full_number_nop
266     \else
267       \strc_enumerations_full_number_coupling
268     \fi
269   \else
270     \strc_enumerations_full_number_nop
271   \fi}
272
273% \protected\def\strc_enumerations_inject_text_with_space
274%   {\edef\p_text{\constructionparameter\c!text}%
275%    \ifempty\p_text \else
276%       \p_text
277%       \removeunwantedspaces
278%       \nobreakspace % should be a option
279%    \fi}
280
281%D \starttyping
282%D \defineenumeration
283%D   [theorem]
284%D   [text=\labeltexts{theorem},
285%D    alternative=serried,
286%D    width=fit,
287%D    space=no]
288%D
289%D \setuplabeltext
290%D   [en]
291%D   [theorem={{XYZ },{ PQR}}]
292%D
293%D \starttheorem
294%D     blablabla
295%D \stoptheorem
296%D \stoptyping
297
298\protected\def\strc_enumerations_inject_text_with_space#1%
299  {\edef\p_text{\constructionparameter\c!text}%
300   \ifempty\p_text
301      \edef\p_text{\constructionparameter\c!label}%
302      \ifempty\p_text
303        #1%
304      \else
305        \labeltexts{\p_text}{#1}%
306      \fi
307   \orelse\ifcstok{\constructionparameter\c!space}\v!no
308     \p_text{#1}%
309   \else
310     \p_text{\removeunwantedspaces\nobreakspace#1}%
311   \fi}
312
313\protected\def\strc_enumerations_inject_extra_text
314  {\ifconditional\c_strc_constructions_title_state
315     \clf_doifelselisthastitle{\currentconstructionmain}{\currentconstructionlistentry}%
316       \strc_enumerations_inject_extra_text_indeed
317       \donothing
318   \fi}
319
320\protected\def\strc_enumerations_inject_text
321  {\constructionparameter\c!numbercommand
322     {\constructionparameter\c!text}}
323
324% head ( number title )
325
326\protected\def\strc_enumerations_inject_extra_text_indeed
327  {\begingroup
328   \hskip{\constructionparameter\c!titledistance}% we listen to the headstyle
329   \useconstructionstyleandcolor\c!titlestyle\c!titlecolor
330   \constructionparameter\c!titlecommand
331     {\constructionparameter\c!titleleft
332      \clf_savedlisttitle{\currentconstructionmain}\currentconstructionlistentry\relax
333      \constructionparameter\c!titleright}%
334   \endgroup}
335
336\protected\def\strc_enumerations_inject_symbol % todo check
337  {\begingroup
338   \useconstructionstyleandcolor\c!numberstyle\c!numbercolor
339   \constructionparameter\c!left
340   \constructionparameter\c!symbol
341   \constructionparameter\c!right
342   \endgroup}
343
344\protected\def\strc_enumerations_inject_number
345  {\begingroup
346   \useconstructionstyleandcolor\c!numberstyle\c!numbercolor
347   \constructionparameter\c!left
348   \constructionparameter\c!starter
349   \clf_savedlistprefixednumber{\currentconstructionmain}\currentconstructionlistentry\relax
350   \constructionparameter\c!stopper
351   \constructionparameter\c!right
352   \endgroup}
353
354\protected\def\strc_enumerations_inject_text_and_number
355  {\constructionparameter\c!numbercommand
356     {\strc_enumerations_inject_text_with_space
357      \strc_enumerations_inject_number}}
358
359\protected\def\strc_enumerations_inject_text_and_symbol
360  {\constructionparameter\c!numbercommand
361     {\strc_enumerations_inject_text_with_space
362      \strc_enumerations_inject_symbol}}
363
364\protected\def\strc_enumerations_full_number_yes
365  {\constructionparameter\c!headcommand
366     {\begstrut
367      \strc_enumerations_inject_text_and_number
368      \strc_enumerations_inject_extra_text
369      \endstrut}}
370
371\protected\def\strc_enumerations_full_number_nop
372  {\constructionparameter\c!headcommand
373     {\begstrut
374      \strc_enumerations_inject_text
375      \strc_enumerations_inject_extra_text
376      \endstrut}}
377
378\protected\def\strc_enumerations_full_number_symbol
379  {\constructionparameter\c!headcommand
380     {\begstrut
381      \strc_enumerations_inject_text_and_symbol
382      \strc_enumerations_inject_extra_text
383      \endstrut}}
384
385% to be used in for instance:
386%
387% coupling=questions .... coupling=answers
388%
389% we will provide some more control, like skipping etc or reference relates
390
391% to do / to be checked
392
393\protected\def\strc_enumerations_full_number_coupling
394  {\iflocation
395     \strc_enumerations_full_number_yes
396   \else
397     \iftrialtypesetting \else
398       % \doenumerationregistercoupling
399     \fi
400     \strc_enumerations_full_number_yes
401     \edef\p_coupling{\constructionparameter\c!coupling}%
402     \ifempty\p_coupling \else
403       \symbolreference[order(construction:\p_coupling:\the\locationorder)]%
404     \fi
405   \fi}
406
407\protected\def\strc_enumerations_skip_number_coupling[#tag]% e.g. for questions with no answer
408  {\clf_setnextreferenceorder{construction}{#tag}}
409
410\protect \endinput
411