strc-enu.mklx /size: 13 Kb    last modification: 2023-12-21 09:44
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
279   \fi}
280
281\protected\def\strc_enumerations_inject_extra_text
282  {\ifconditional\c_strc_constructions_title_state
283     \clf_doifelselisthastitle{\currentconstructionmain}\numexpr\currentconstructionlistentry\relax
284       \strc_enumerations_inject_extra_text_indeed
285       \donothing
286   \fi}
287
288\protected\def\strc_enumerations_inject_text
289  {\constructionparameter\c!numbercommand
290    {\constructionparameter\c!text}}
291
292% head ( number title )
293
294\protected\def\strc_enumerations_inject_extra_text_indeed
295  {\begingroup
296   \hskip\constructionparameter\c!titledistance % we listen to the headstyle
297   \useconstructionstyleandcolor\c!titlestyle\c!titlecolor
298   \constructionparameter\c!titlecommand
299     {\constructionparameter\c!titleleft
300      \clf_savedlisttitle{\currentconstructionmain}\currentconstructionlistentry\relax
301      \constructionparameter\c!titleright}%
302   \endgroup}
303
304\protected\def\strc_enumerations_inject_symbol % todo check
305  {\begingroup
306   \useconstructionstyleandcolor\c!numberstyle\c!numbercolor
307   \constructionparameter\c!left
308   \constructionparameter\c!symbol
309   \constructionparameter\c!right
310   \endgroup}
311
312\protected\def\strc_enumerations_inject_number
313  {\begingroup
314   \useconstructionstyleandcolor\c!numberstyle\c!numbercolor
315   \constructionparameter\c!left
316   \constructionparameter\c!starter
317   \clf_savedlistprefixednumber{\currentconstructionmain}\currentconstructionlistentry\relax
318   \constructionparameter\c!stopper
319   \constructionparameter\c!right
320   \endgroup}
321
322\protected\def\strc_enumerations_inject_text_and_number
323  {\constructionparameter\c!numbercommand
324     {\strc_enumerations_inject_text_with_space
325      \strc_enumerations_inject_number}}
326
327\protected\def\strc_enumerations_inject_text_and_symbol
328  {\constructionparameter\c!numbercommand
329     {\strc_enumerations_inject_text_with_space
330      \strc_enumerations_inject_symbol}}
331
332\protected\def\strc_enumerations_full_number_yes
333  {\constructionparameter\c!headcommand
334     {\begstrut
335      \strc_enumerations_inject_text_and_number
336      \strc_enumerations_inject_extra_text
337      \endstrut}}
338
339\protected\def\strc_enumerations_full_number_nop
340  {\constructionparameter\c!headcommand
341     {\begstrut
342      \strc_enumerations_inject_text
343      \strc_enumerations_inject_extra_text
344      \endstrut}}
345
346\protected\def\strc_enumerations_full_number_symbol
347  {\constructionparameter\c!headcommand
348     {\begstrut
349      \strc_enumerations_inject_text_and_symbol
350      \strc_enumerations_inject_extra_text
351      \endstrut}}
352
353% to be used in for instance:
354%
355% coupling=questions .... coupling=answers
356%
357% we will provide some more control, like skipping etc or reference relates
358
359% to do / to be checked
360
361\protected\def\strc_enumerations_full_number_coupling
362  {\iflocation
363     \strc_enumerations_full_number_yes
364   \else
365     \iftrialtypesetting \else
366       % \doenumerationregistercoupling
367     \fi
368     \strc_enumerations_full_number_yes
369     \edef\p_coupling{\constructionparameter\c!coupling}%
370     \ifempty\p_coupling \else
371       \symbolreference[order(construction:\p_coupling:\the\locationorder)]%
372     \fi
373   \fi}
374
375\protected\def\strc_enumerations_skip_number_coupling[#tag]% e.g. for questions with no answer
376  {\clf_setnextreferenceorder{construction}{#tag}}
377
378\protect \endinput
379