summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/strc-lab.mkiv
blob: ac49941aaeeffba8b507b3bff4d6d691b4671a7c (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
%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]

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

% todo: \strc_labels_command for user

\appendtoks
    \ifx\currentlabelparent\empty
       % clone => parent | subclone => clone | subsubclone => subclone
       \let\currentlabelsub\empty
       \strc_define_commands_label
         {\currentlabelsub\currentlabel}%
         \plusone
         \empty
       \edef\p_levels{\labelparameter\c!levels}%
       \dostepwiserecurse\plustwo\p_levels\plusone
         {\strc_define_commands_label
            {\v!sub\currentlabelsub\currentlabel}%
            \recurselevel
            {\currentlabelsub\currentlabel}%
          \edef\currentlabelsub{\v!sub\currentlabelsub}}%
   \else
       % clone => parent | subclone => subparent | subsubclone => subsubparent
       \let\currentlabelsub\empty
       \edef\p_levels{\labelparameter\c!levels}%
       \dorecurse\p_levels
         {\strc_define_commands_label
            {\currentlabelsub\currentlabel}%
            \recurselevel
            {\currentlabelsub\currentlabelparent}%
          \edef\currentlabelsub{\v!sub\currentlabelsub}}%
   \fi
   \edef\p_counter{\labelparameter\s!counter}% can inherit from parent
   \ifx\p_counter\empty %
       \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
   \let\constructionparameter           \labelparameter
   \let\constructionnamespace           \??label
   \let\detokenizedconstructionparameter\detokenizedlabelparameter
   \let\letconstructionparameter        \letlabelparameter
   \let\useconstructionstyleandcolor    \uselabelstyleandcolor
   \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:

\unexpanded\def\strc_labels_command{\setfalse\c_strc_constructions_number_keep\strc_descriptions_command}
\unexpanded\def\strc_labels_current{\settrue \c_strc_constructions_number_keep\strc_descriptions_command}

\unexpanded\def\strc_labels_next   {\strc_constructions_next_indeed \namedlabelparameter} % #1#2
\unexpanded\def\strc_labels_reset  {\strc_constructions_reset_indeed\namedlabelparameter} % #1#2
%unexpanded\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

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

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

\unexpanded\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