summaryrefslogtreecommitdiff
path: root/tex/context/base/supp-num.mkiv
blob: 271e2588a92388167f11f7c18d21ebb88f12e460 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
%D \module
%D   [       file=supp-num,
%D        version=1998.05.15,
%D          title=\CONTEXT\ Support Macros,
%D       subtitle=Numbers,
%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.

% See end for transition to mkiv.

\writestatus{loading}{ConTeXt Support Macros / Numbers}

\unprotect

%D \macros
%D   {digits, setdigitmode, setdigitsign}
%D
%D Depending on the digit mode the command \type {\digits}
%D normalizes number patterns depending on the language set.
%D
%D \starttyping
%D This will never be a \digits{1.000.000} seller.
%D \stoptyping
%D
%D or
%D
%D \starttyping
%D I will never grow longer than \digits 1.86 \Meter.
%D \stoptyping
%D
%D The different modes are shown in:
%D
%D \startbuffer
%D \setdigitmode 1 \digits 12.345,90 \digits 12.345.000 \digits 1,23
%D \setdigitmode 2 \digits 12.345,90 \digits 12.345.000 \digits 1,23
%D \setdigitmode 3 \digits 12.345,90 \digits 12.345.000 \digits 1,23
%D \setdigitmode 4 \digits 12.345,90 \digits 12.345.000 \digits 1,23
%D \setdigitmode 5 \digits 12.345,90 \digits 12.345.000 \digits 1,23
%D \setdigitmode 6 \digits 12.345,90 \digits 12.345.000 \digits 1,23
%D \stopbuffer
%D
%D \typebuffer
%D
%D This is typset as:
%D
%D \startlines
%D \getbuffer
%D \stoplines
%D
%D The sign can be typeset as is or within the space of a
%D digit.
%D
%D \startbuffer
%D \setdigitsign 0 \digits +12.345,90
%D \setdigitmode 1 \digits +12.345,90
%D \setdigitmode 2 \digits +12.345,90
%D \setdigitmode 3 \digits +12.345,90
%D \stopbuffer
%D
%D \typebuffer
%D
%D This is typset as:
%D
%D \startlines
%D \getbuffer
%D \stoplines

\chardef\digitoutputmode=1 % 0..6
\chardef\digitsignmode  =0 % 0..3

\def\setdigitmode{\chardef\digitoutputmode}
\def\setdigitsign{\chardef\digitsignmode}

%D The digit modes are:
%D
%D \startitemize[packed]
%D \item periods      \& comma
%D \item commas       \& period
%D \item thinmuskips  \& comma
%D \item thinmuskips  \& period
%D \item thickmuskips \& comma
%D \item thickmuskips \& period
%D \stopitemize

\let\collecteddigits \empty \chardef\digitinputmode =1
\let\saveddigits     \empty \chardef\skipdigit      =0
\let\savedpowerdigits\empty \chardef\powerdigits    =0

%D The first stage of the \type {\digit} macro takes care of
%D the grouped call, the other branch handles the fuzzy
%D delimited calls.

\ifx\mbox\undefined \let\mbox\normalhbox \fi

% \unexpanded\def\digits
%   {\bgroup\let~@\doifnextbgroupelse\dodigits\grabdigit}

\unexpanded\def\digits
  {\bgroup
   \let~@%
   \doifnextbgroupelse\dodigits{\doifnextcharelse\normalmathshift\domathdigits\grabdigit}}

\def\dodigits#1%
  {\grabdigit#1\relax}

\def\domathdigits$#1$%
  {\mbox{\grabdigit#1\relax}} % adding $ $ goes wrong in tabulate

\def\grabdigit
  {\futurelet\next\scandigit}

%D Watch the test for \type {\nextobeyedline}, because the
%D endofline token can be \type {\def'd}, not \type {\let}'d,
%D we need to do an indirect test (see \type {verb-ini.tex})
%D for details. (This probably needs an update.)

\ifx\normalmathshift\undefined \let\normalmathshift=$ \fi

\def\scandigit
  {\ifx\next\blankspace
     \let\next\handledigits
   \else\ifx\next\nextobeyedline % the indirect one
     \let\next\handledigits
   \else\ifx\next\bgroup
     \let\next\handledigits
   \else\ifx\next\egroup
     \let\next\handledigits
   \else\ifx\next\normalmathshift
     \let\next\handledigits
   \else
     \let\next\collectdigit
   \fi\fi\fi\fi\fi
   \next}

%D We store the power||of||ten (to be signaled by \type {^},
%D \type {e} or~\type {E}) in a seperate macro so that we can
%D typeset it in superscript. The space placeholders are
%D replaced by a \type {@}.

\def\savedigit#1#2%
  {\edef#1{#1\saveddigits#2}\let\saveddigits\empty}

\long\def\collectdigit#1%
  {\ifx#1~%
     \savedigit\collecteddigits @%
   \else\if#1_% tricky as can be several catcodes ... will become lua code anyway
     \savedigit\collecteddigits @%
   \else\if\noexpand#1\relax
     \let\grabdigit\handledigits
   \else\ifcase\powerdigits
     \if#1E%
       \chardef\powerdigits\plusone
     \else\if#1e%
       \chardef\powerdigits\plusone
     \else\if#1^%
       \chardef\powerdigits\plusone
     \else
       \savedigit\collecteddigits#1%
       %\doifnumberelse{#1}
       %  {\savedigit\collecteddigits#1}
       %  {\def\saveddigits{#1}}%
     \fi\fi\fi
   \else
     \savedigit\savedpowerdigits#1%
     %\doifnumberelse{#1}
     %  {\savedigit\savedpowerdigits#1}
     %  {\def\saveddigits{#1}}%
   \fi\fi\fi\fi
   \grabdigit}

\let\handlemathdigits\firstofoneargument
\let\handletextdigits\mathematics

\def\handledigits
  {%\ifcase\powerdigits
   %  \edef\collecteddigits{\collecteddigits\saveddigits}%
   %\else
   %  \edef\savedpowerdigits{\savedpowerdigits\saveddigits}%
   %\fi
   \ifmmode
     \handlemathdigits{\dohandledigits}%
   \else
     \dontleavehmode\hbox{\handletextdigits{\dohandledigits}}%
   \fi
   \egroup}

%D Although we could do with one pass, a second pass for
%D handling the stored sequence is more readable.

\ifnum\texengine=\luatexengine

    \def\dohandledigits
      {\mathcode`\,="002C \mathcode`\.="002E % pretty hard coded
       \expandafter\handletokens\collecteddigits\with\scandigits
       \ifcase\powerdigits\else\digitpowerseparator^{\savedpowerdigits}\fi}

    \chardef\mathaxisfontid\zerocount

\else

    \def\dohandledigits
      {\mathcode`\,="013B \mathcode`\.="013A % pretty hard coded
       \expandafter\handletokens\collecteddigits\with\scandigits
       \ifcase\powerdigits\else\digitpowerseparator^{\savedpowerdigits}\fi}

    \chardef\mathaxisfontid\plustwo

\fi

\def\doscandigit#1%
  {\ifcase\skipdigit\@EA\hbox\else\@EA\hphantom\fi\bgroup
     \mathematics % brr, needed because of stored punctuation
       {\ifnum\digitinputmode=#1\relax
        \ifcase\digitoutputmode
        \or .%
        \or ,%
        \or \mskip\thinmuskip
        \or \mskip\thinmuskip
        \or \mskip\thickmuskip
        \or \mskip\thickmuskip
        \fi
      \else
        \ifodd\digitoutputmode,\else.\fi
      \fi}%
   \egroup}

%D The signs can be made smaller and sqeezed into the width
%D of a digit. Watch the \type {\mathaxisheight} trickery (this
%D font related register stored the math axis).

% 0,=
% 0,==  second = results in delta(00,=)
% 0,-   is invalid, should be =
% 0,--  is invalid, should be ==

\def\digitzeroamount
  {\digitsgn\zeroamount
   \def\digitzeroamount
     {\hphantom
        {00\setbox\scratchbox\hbox{$\zeroamount$}%
         \hskip-\wd\scratchbox}%
      \let\digitzeroamount\empty}}

\def\scandigits#1%
  {\if#1.\digitsep1\else
   \if#1,\digitsep2\else
   \if#1@\digitnop \else
   \if#1_\digitnop \else
   \if#1/\digitsgn{\hphantom{+}}\chardef\skipdigit0\else
   \if#1-\ifcase\skipdigit\digitsgn-\else
           \box\digitsepbox\digitzeroamount \fi\chardef\skipdigit0\else
   \if#1+\digitsgn+\chardef\skipdigit0\else
   \if#1=\box\digitsepbox\digitzeroamount \chardef\skipdigit0\else
   \if#1s\digitsgn{\hphantom{\positive}}\chardef\skipdigit0\else
   \if#1p\digitsgn\positive\chardef\skipdigit0\else
   \if#1m\digitsgn\negative\chardef\skipdigit0\else
   \if#1n\digitsgn\negative\chardef\skipdigit0\else
         \box\digitsepbox #1\chardef\skipdigit0\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi}

\newbox\digitsepbox \chardef\autodigitmode=1

\def\digitsep#1%
  {\ifcase\autodigitmode
     \doscandigit#1%
   \else
     \setbox\digitsepbox\hbox{\doscandigit#1}%
   \fi
   \chardef\skipdigit0\relax}

% strange, does not work
%
% \def\digitnop
%   {\hphantom{\box\digitsepbox}%
%    \hphantom{0}\chardef\skipdigit1\relax}
%
% while this works

\def\digitnop
  {\hbox{\hphantom{\box\digitsepbox}}%
   \hphantom{0}\chardef\skipdigit1\relax}

% but this doesn't
%
% \def\digitnop
%   {\hphantom{\box\digitsepbox0}%
%    \chardef\skipdigit1\relax}

\def\digitsgn#1%
  {\ifcase\digitsignmode#1\else
     \hbox
       {\setbox\scratchbox\hbox{0}%
        \scratchdimen\mathaxisheight\textfont\mathaxisfontid
        \def\digitsgn##1##2%
          {\advance\scratchdimen-\mathaxisheight##1\mathaxisfontid
           \raise\scratchdimen
           \hbox to \wd\scratchbox{\hss$##2#1$\hss}}%
        \ifcase\digitsignmode\or
          \digitsgn\textfont        \textstyle        \or
          \digitsgn\scriptfont      \scriptstyle      \or
          \digitsgn\scriptscriptfont\scriptscriptstyle\fi}%
   \fi}

\ifx\undefined\zeroamount \def\zeroamount{-} \fi
\ifx\undefined\positive   \def\positive  {+} \fi
\ifx\undefined\negative   \def\negative  {-} \fi

%D The digit parser handles a bunch of special characters as
%D well as different formats. We strongly suggest you to use
%D the grouped call.
%D
%D \starttabulate[|l|l|l|]
%D \NC \type{.} \NC , . \NC comma or period \NC \NR
%D \NC \type{,} \NC , . \NC comma or period \NC \NR
%D \NC \type{@} \NC \NC invisible space \NC \NR
%D \NC \type{_} \NC \NC invisible space \NC \NR
%D \NC \type{/} \NC \NC invisible sign \NC \NR
%D \NC \type{-} \NC $-$ \NC minus sign \NC \NR
%D \NC \type{+} \NC $+$ \NC plus sign \NC \NR
%D \NC \type{s} \NC \NC invisible high sign \NC \NR
%D \NC \type{p} \NC $\positive$ \NC high plus sign \NC \NR
%D \NC \type{m} \NC $\negative$ \NC high minus sign \NC \NR
%D \NC \type{n} \NC $\negative$ \NC high minus (negative) sign \NC \NR
%D \NC \type{=} \NC $\zeroamount$ \NC zero padding \NC \NR
%D \stoptabulate
%D
%D These triggers are used in the following examples.
%D
%D \startbuffer
%D \digits 12
%D \digits{~~~.~~~.~~~.68.712,34}
%D \digits ~~~.~~~.~~~.68.712,34
%D \digits ___.___.111.68.712,34
%D \digits 111.111.111.68.712,34
%D \digits 12.345,90
%D \digits 12.345.000
%D \digits 12,34
%D \digits{392.857.230.68.712,34}
%D {\digits1234}
%D \digits{1234}
%D \digits 1234\relax
%D $\digits 123.222,00$
%D \digits 123.222,00
%D \digits 123.222,==
%D \digits 123.222,00^10
%D \digits 123.222,00e10
%D \digits /123.222,00e-12
%D \digits -123.222,00e-12
%D \digits +123.222,00e-12
%D \digits n123.222,00e-12
%D \digits s123.222,00e-12
%D \digits p123.222,00e-12
%D \stopbuffer
%D
%D \typebuffer
%D
%D \startlines
%D \getbuffer
%D \stoplines

%D \macros
%D   {Digits}
%D
%D We also permit:

\let\Digits\digits

%D These macros are complicated by the fact that we also
%D have to support cases like:
%D
%D \starttyping
%D {\digits1234}
%D \digits{1234}
%D \digits 1234\whatever
%D $\digits 123.222,00$
%D \digits 123.222,00.
%D \stoptyping
%D
%D The latter case shows us that trailing non digits are to
%D be passed untreated.
%D
%D Another interesting case is:
%D
%D \starttyping
%D \digits 123.222,00^10
%D \stoptyping
%D
%D The separator is defined as:

% \def\digitpowerseparator%
%   {\cdot10} % {\times10}

\def\digitpowerseparator
  {\ifx\collecteddigits\empty\else\cdot\fi10}

%D \macros
%D   {digittemplate}
%D
%D Users can specify the way they enter those digits by saying
%D something like:
%D
%D \starttyping
%D \digittemplate 12.000.000,00 % \digittemplate .,
%D \stoptyping

\def\digittemplate #1 %
  {\chardef\digitinputmode\zerocount
   \handletokens#1\with\scandigittemplate}

\def\scandigittemplate#1%
  {\if     #1.\ifcase\digitinputmode\chardef\digitinputmode\plusone \fi% period
   \else\if#1,\ifcase\digitinputmode\chardef\digitinputmode\plustwo \fi% comma
   \fi\fi}

\protect \endinput


\endinput

\def\digitszeropadding   {\zeroamount}
\def\digitsnegative      {\mathematics\negative}
\def\digitspositive      {\mathematics\positive}
\def\digitsspace         {\hphantom{0}}
\def\digitsseparatorspace{\hphantom{.}}
\def\digitssignspace     {\hphantom{+}}
\def\digitshighspace     {\hphantom{\mathematics\positive}}
\def\digitspower         {\high}

\starttext

% print(table.serialize(table.keys(jobs)))

local P, S, R, Cs = lpeg.P, lpeg.S, lpeg.R, lpeg.Cs

local done      = false
local swap      = false
local digit     = R("09")
local sign      = S("+-")
local power     = S("^e")
local space     = S(" ~@_")
local comma     = P(",")
local period    = P(".")
local signspace = P("/")
local positive  = P("p")
local negative  = P("n")
local highspace = P("s")
local padding   = P("=")

local space     = space     / "\\digitsspace"
local digit     = digit     / function(s) done = true return s end
local separator = comma     / function(s) if not done then return "\\digitsseparatorspace" elseif swap then return "." else return "," end end
                + period    / function(s) if not done then return "\\digitsseparatorspace" elseif swap then return "," else return "." end end
local signspace = signspace / "\\digitssignspace"
local positive  = positive  / "\\digitspositive"
local negative  = negative  / "\\digitsnegative"
local highspace = highspace / "\\digitshighspace"
local power     = power/"" * (Cs((sign + signspace)^0 * digit^1) / function(s) return "\\digitspower{" .. s .. "}" end)
local padding   = padding   / "\\digitszeropadding"

local replace = lpeg.Cs (
    (sign + signspace + positive + negative + highspace)^0
  * (separator^0 * (space + digit)^1)^1
  * power^0
)

local function digits(str)
    done = false
--~     swap = true
    local str = lpeg.match(replace,str)
--    print(str)
    tex.sprint(tex.ctxcatcodes,"\\dontleavehmode",str,"\\par")
end
-- namespaces.register("digits")

digits("12")
digits("~~~.~~~.~~~.68.712,34")
digits("~~~.~~~.~~~.68.712,34")
digits("___.___.111.68.712,34")
digits("111.111.111.68.712,34")
digits("12.345,90")
digits("12.345.000")
digits("12,34")
digits("392.857.230.68.712,34")
digits("1234")
digits("123.222,00")
digits("123.222,==")
digits("123.222,00^10")
digits("123.222,00e10")
digits("/123.222,00e-12")
digits("-123.222,00e-12")
-- digits("+123.222,00e-12")
digits("n123.222,00e-12")
digits("s123.222,00e-12")
digits("p123.222,00e/12")

\stopluacode
[[\digitsseparatorspace]]
\stoptext