summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/strc-lab.mkxl
blob: 368a412a71ac8ccc6308f177a5306fdbb065adfe (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
%D \module
%D   [       file=strc-lab, % was part of strc-des,
%D        version=2008.10.20,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Labels,
%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 Structure Macros / Labels}

\unprotect

% todo: reset and increment will go

%D Labels are implemented somewhat inefficient but have the advantage that they
%D can be controlled like other constructions. Who knows when that comes in
%D handy?

\installcorenamespace{labels}

\installcommandhandler \??labels {label} \??labels % plural hash, else clash with label (or rename that one)

\installcounterassociation{label}

\let\setuplabels\setuplabel

\setuplabel
  [\c!alternative=\v!intext,
   \c!headstyle=\v!bold,
   \c!titlestyle=\v!bold,
   \c!distance=\zeropoint,
   \c!width=\v!fit,
   \c!titledistance=.5\emwidth,
   \c!margin=\v!no,
   \c!titleleft=(,
   \c!titleright=),
   \c!display=\v!no,
   \c!titlecommand=,
   \c!expansion=\v!no,
   \c!way=\v!by\v!text,
   \c!prefix=\v!no,
   \c!prefixconnector=.,
   \c!text=\currentlabel,
   \c!number=\v!yes, % else description
   \c!start=0,
   \c!state=\v!start,
   \c!levels=3]

\protected\def\strc_define_commands_label#1#2#3% current level parent
  {\doifelsenothing{#3}
     {\normalexpanded{\defineconstruction[#1][\s!handler=\v!label,\c!level=#2]}%
      \edefcsname\??label#1:\s!parent\endcsname{\??label}}%
     {\normalexpanded{\defineconstruction[#1][#3][\s!handler=\v!label,\c!level=#2]}%
      \edefcsname\??label#1:\s!parent\endcsname{\??label#3}}%
   \ifconditional\c_strc_constructions_define_commands
     \frozen\instance\protected\edefcsname\e!next #1\endcsname{\strc_labels_next {#1}{\number#2}}% obsolete
     \frozen\instance\protected\edefcsname\v!reset#1\endcsname{\strc_labels_reset{#1}{\number#2}}% obsolete % should be \e!reset anyway
     %frozen\instance\setuevalue{\c!set  #1}{\strc_labels_set  {#1}{\number#2}}% obsolete
     \ifcsname\v!current#1\endcsname
       % we play safe
     \else
       \protected\edefcsname\v!current#1\endcsname{\strc_labels_current{#1}}%          % obsolete % should be \e!current anyway
     \fi
     \frozen\instance\protected\edefcsname#1\endcsname{\strc_labels_command[#1]}%
   \fi}

% todo: \strc_labels_command for user

\let\m_strc_labels_sub\empty

\appendtoks
    \let\m_strc_labels_sub\empty
    \ifempty\currentlabelparent
       % clone => parent | subclone => clone | subsubclone => subclone
       \strc_define_commands_label
         {\m_strc_labels_sub\currentlabel}%
         \plusone
         \empty
       \edef\p_levels{\labelparameter\c!levels}%
       \dostepwiserecurse\plustwo\p_levels\plusone
         {\strc_define_commands_label
            {\v!sub\m_strc_labels_sub\currentlabel}%
            \recurselevel
            {\m_strc_labels_sub\currentlabel}%
          \edef\m_strc_labels_sub{\v!sub\m_strc_labels_sub}}%
   \else
       % clone => parent | subclone => subparent | subsubclone => subsubparent
       \edef\p_levels{\labelparameter\c!levels}%
       \dorecurse\p_levels
         {\strc_define_commands_label
            {\m_strc_labels_sub\currentlabel}%
            \recurselevel
            {\m_strc_labels_sub\currentlabelparent}%
          \edef\m_strc_labels_sub{\v!sub\m_strc_labels_sub}}%
   \fi
   \edef\p_counter{\labelparameter\s!counter}% can inherit from parent
   \ifempty\p_counter %
       \let\p_counter\currentlabel
   \fi
   \doifelsecounter\p_counter\donothing{\strc_labels_define_counter\p_counter}%
   \letlabelparameter\s!counter\p_counter
\to \everydefinelabel

\let\p_strc_constructions_title \empty
\let\p_strc_constructions_number\empty

\newconditional\c_strc_constructions_number_keep

\setvalue{\??constructioninitializer\v!label}%
  {\let         \currentlabel                    \currentconstruction
   \enforced\let\constructionparameter           \labelparameter
   \enforced\let\constructionnamespace           \??label
   \enforced\let\detokenizedconstructionparameter\detokenizedlabelparameter
   \enforced\let\letconstructionparameter        \letlabelparameter
   \enforced\let\useconstructionstyleandcolor    \uselabelstyleandcolor
   \enforced\let\setupcurrentconstruction        \setupcurrentlabel
   % shared with enumerations
   \edef\p_strc_constructions_number{\constructionparameter\c!number}%
   \ifx\p_strc_constructions_number\v!yes
     \settrue\c_strc_constructions_number_state
     \iftrialtypesetting
       \strc_counters_save\currentconstructionnumber
     \fi
     \ifconditional\c_strc_constructions_number_keep \else
       \strc_counters_increment_sub\currentconstructionnumber\currentconstructionlevel
     \fi
   \else
     \setfalse\c_strc_constructions_number_state
   \fi
   \edef\p_strc_constructions_title{\constructionparameter\c!title}%
   \ifx\p_strc_constructions_title\v!yes
     \settrue\c_strc_constructions_title_state
   \else
     \setfalse\c_strc_constructions_title_state
   \fi}

\setvalue{\??constructionfinalizer\v!label}%
  {\ifconditional\c_strc_constructions_number_state
     \iftrialtypesetting
       \strc_counters_restore\currentconstructionnumber
     \fi
   \fi}

%D Interfaces:

\protected\def\strc_labels_command{\setfalse\c_strc_constructions_number_keep\nameddescription}
\protected\def\strc_labels_current{\settrue \c_strc_constructions_number_keep\nameddescription}

\protected\def\strc_labels_next   {\strc_constructions_next_indeed \namedlabelparameter} % #1#2
\protected\def\strc_labels_reset  {\strc_constructions_reset_indeed\namedlabelparameter} % #1#2
%protected\def\strc_labels_set    {\strc_constructions_set_indeed  \namedlabelparameter} % #1#2

% similar to enumerations

\def\strc_labels_define_counter#1%
  {\definecounter[#1]%
   \registerlabelcounter{#1}}

\appendtoks
    \synchronizelabelcounters
\to \everysetuplabel

\appendtoks
    \synchronizelabelcounters
\to \everydefinelabel

% no start stop here

\letcsname\??constructionmainhandler\v!label\expandafter\endcsname\csname\??constructionmainhandler\v!description\endcsname

\protected\setevalue{\??constructioncommandhandler\v!label}%
  {\csname\??constructionstarthandler\v!construction\endcsname
   \csname\??constructionstophandler \v!construction\endcsname
   \endgroup}

\protected\setvalue{\??constructiontexthandler\v!label}%
  {\begingroup
   \useconstructionstyleandcolor\c!headstyle\c!headcolor
   \strc_labels_text
   \endgroup}

\let\strc_labels_text\strc_enumerations_text

% inline variant

\defineconstructionalternative
  [\v!intext]
  [\c!renderingsetup=\??constructionrenderings:\v!intext]

\startsetups[\??constructionrenderings:\v!intext]
    \dontleavehmode\box\constructionheadbox
\stopsetups

\protect \endinput