strc-lab.mkxl /size: 6817 b    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=strc-lab, % was part of strc-des,
3%D        version=2008.10.20,
4%D          title=\CONTEXT\ Structure Macros,
5%D       subtitle=Labels,
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 / Labels}
15
16\unprotect
17
18% todo: reset and increment will go
19
20%D Labels are implemented somewhat inefficient but have the advantage that they
21%D can be controlled like other constructions. Who knows when that comes in
22%D handy?
23
24\installcorenamespace{labels}
25
26\installcommandhandler \??labels {label} \??labels % plural hash, else clash with label (or rename that one)
27
28\installcounterassociation{label}
29
30\let\setuplabels\setuplabel
31
32\setuplabel
33  [\c!alternative=\v!intext,
34   \c!headstyle=\v!bold,
35   \c!titlestyle=\v!bold,
36   \c!distance=\zeropoint,
37   \c!width=\v!fit,
38   \c!titledistance=.5\emwidth,
39   \c!margin=\v!no,
40   \c!titleleft=(,
41   \c!titleright=),
42   \c!display=\v!no,
43   \c!titlecommand=,
44   \c!expansion=\v!no,
45   \c!way=\v!by\v!text,
46   \c!prefix=\v!no,
47   \c!prefixconnector=.,
48   \c!text=\currentlabel,
49   \c!number=\v!yes, % else description
50   \c!start=0,
51   \c!state=\v!start,
52   \c!levels=3]
53
54\protected\def\strc_define_commands_label#1#2#3% current level parent
55  {\ifempty{#3}%
56     \normalexpanded{\defineconstruction[#1][\s!handler=\v!label,\c!level=#2]}%
57     \edefcsname\??label#1:\s!parent\endcsname{\??label}%
58   \else
59     \normalexpanded{\defineconstruction[#1][#3][\s!handler=\v!label,\c!level=#2]}%
60     \edefcsname\??label#1:\s!parent\endcsname{\??label#3}%
61   \fi
62   \ifconditional\c_strc_constructions_define_commands
63     \frozen\instance\protected\edefcsname\e!next #1\endcsname{\strc_labels_next {#1}{\number#2}}% obsolete
64     \frozen\instance\protected\edefcsname\v!reset#1\endcsname{\strc_labels_reset{#1}{\number#2}}% obsolete % should be \e!reset anyway
65     %frozen\instance\protected\edefcsname\c!set  #1\endcsname{\strc_labels_set  {#1}{\number#2}}% obsolete
66     \ifcsname\v!current#1\endcsname
67       % we play safe
68     \else
69       \protected\edefcsname\v!current#1\endcsname{\strc_labels_current{#1}}%          % obsolete % should be \e!current anyway
70     \fi
71     \frozen\instance\protected\edefcsname#1\endcsname{\strc_labels_command[#1]}%
72   \fi}
73
74% todo: \strc_labels_command for user
75
76\lettonothing\m_strc_labels_sub
77
78\appendtoks
79    \lettonothing\m_strc_labels_sub
80    \ifempty\currentlabelparent
81       % clone => parent | subclone => clone | subsubclone => subclone
82       \strc_define_commands_label
83         {\m_strc_labels_sub\currentlabel}%
84         \plusone
85         \empty
86       \edef\p_levels{\labelparameter\c!levels}%
87       \dostepwiserecurse\plustwo\p_levels\plusone
88         {\strc_define_commands_label
89            {\v!sub\m_strc_labels_sub\currentlabel}%
90            \recurselevel
91            {\m_strc_labels_sub\currentlabel}%
92          \edef\m_strc_labels_sub{\v!sub\m_strc_labels_sub}}%
93   \else
94       % clone => parent | subclone => subparent | subsubclone => subsubparent
95       \edef\p_levels{\labelparameter\c!levels}%
96       \dorecurse\p_levels
97         {\strc_define_commands_label
98            {\m_strc_labels_sub\currentlabel}%
99            \recurselevel
100            {\m_strc_labels_sub\currentlabelparent}%
101          \edef\m_strc_labels_sub{\v!sub\m_strc_labels_sub}}%
102   \fi
103   \edef\p_counter{\labelparameter\s!counter}% can inherit from parent
104   \ifempty\p_counter %
105       \let\p_counter\currentlabel
106   \fi
107   \doifelsecounter\p_counter\donothing{\strc_labels_define_counter\p_counter}%
108   \letlabelparameter\s!counter\p_counter
109\to \everydefinelabel
110
111\newconditional\c_strc_constructions_number_keep
112
113\defcsname\??constructioninitializer\v!label\endcsname
114  {\let         \currentlabel                    \currentconstruction
115   \enforced\let\constructionparameter           \labelparameter
116   \enforced\let\constructionnamespace           \??label
117   \enforced\let\detokenizedconstructionparameter\detokenizedlabelparameter
118   \enforced\let\letconstructionparameter        \letlabelparameter
119   \enforced\let\useconstructionstyleandcolor    \uselabelstyleandcolor
120   \enforced\let\setupcurrentconstruction        \setupcurrentlabel
121   % shared with enumerations
122   \ifcstok{\constructionparameter\c!number}\v!yes
123     \c_strc_constructions_number_state\conditionaltrue
124     \iftrialtypesetting
125       \strc_counters_save\currentconstructionnumber
126     \fi
127     \ifconditional\c_strc_constructions_number_keep \else
128       \strc_counters_increment_sub\currentconstructionnumber\currentconstructionlevel
129     \fi
130   \else
131     \c_strc_constructions_number_state\conditionalfalse
132   \fi
133   \ifcstok{\constructionparameter\c!title}\v!yes
134     \c_strc_constructions_title_state\conditionaltrue
135   \else
136     \c_strc_constructions_title_state\conditionalfalse
137   \fi}
138
139\defcsname\??constructionfinalizer\v!label\endcsname
140  {\ifconditional\c_strc_constructions_number_state
141     \iftrialtypesetting
142       \strc_counters_restore\currentconstructionnumber
143     \fi
144   \fi}
145
146%D Interfaces:
147
148\protected\def\strc_labels_command{\c_strc_constructions_number_keep\conditionalfalse\nameddescription}
149\protected\def\strc_labels_current{\c_strc_constructions_number_keep\conditionaltrue\nameddescription}
150
151\protected\def\strc_labels_next   {\strc_constructions_next_indeed \namedlabelparameter} % #1#2
152\protected\def\strc_labels_reset  {\strc_constructions_reset_indeed\namedlabelparameter} % #1#2
153%protected\def\strc_labels_set    {\strc_constructions_set_indeed  \namedlabelparameter} % #1#2
154
155% similar to enumerations
156
157\def\strc_labels_define_counter#1%
158  {\definecounter[#1]%
159   \registerlabelcounter{#1}}
160
161\appendtoks
162    \synchronizelabelcounters
163\to \everysetuplabel
164
165\appendtoks
166    \synchronizelabelcounters
167\to \everydefinelabel
168
169% no start stop here
170
171\letcsname\??constructionmainhandler\v!label\expandafter\endcsname\csname\??constructionmainhandler\v!description\endcsname
172
173\protected\edefcsname\??constructioncommandhandler\v!label\endcsname
174  {\csname\??constructionstarthandler\v!construction\endcsname
175   \csname\??constructionstophandler \v!construction\endcsname
176   \endgroup}
177
178\protected\defcsname\??constructiontexthandler\v!label\endcsname
179  {\begingroup
180   \useconstructionstyleandcolor\c!headstyle\c!headcolor
181   \strc_labels_text
182   \endgroup}
183
184\let\strc_labels_text\strc_enumerations_text
185
186% inline variant
187
188\defineconstructionalternative
189  [\v!intext]
190  [\c!renderingsetup=\??constructionrenderings:\v!intext]
191
192\startsetups[\??constructionrenderings:\v!intext]
193    \dontleavehmode\box\constructionheadbox
194\stopsetups
195
196\protect \endinput
197