summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/lang-rep.mkxl
blob: f13e2ec7b5e5f6b1026ba15dd8a0ffd4194511eb (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
%D \module
%D   [       file=lang-rep,
%D        version=2013.04.28,
%D          title=\CONTEXT\ Language Macros,
%D       subtitle=Substitution,
%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.

%D As I needed an example of messing with nodes for the bacho\TEX\ tutorial
%D I cooked up this. In the end I decided to stick to a simpler example and
%D just finished this off in case someone really needs it.

\writestatus{loading}{ConTeXt Language Macros / Replacements}

\unprotect

\registerctxluafile{lang-rep}{autosuffix}

\definesystemattribute[replacements][public,global]

%D \starttyping
%D \startluacode
%D
%D     -- todo: other nodes (prelude to more experiments with auto dropped caps)
%D
%D     languages.replacements.add("basics", {
%D         ["aap"]   = "monkey",
%D         ["noot"]  = "nut",
%D         ["never"] = "forever",
%D         ["newer"] = "cooler",
%D         ["new"]   = "cool",
%D      -- ["special"] = "veryspe{>>>}{<<<}{=}cial",
%D     })
%D
%D \stopluacode
%D
%D \replaceword[more][this][that]
%D \replaceword[more][crap][support]
%D \replaceword[more][---][—]
%D \replaceword[basics][special][veryspe{>>>}{<<<}{=}cial]
%D
%D \start \setreplacements[basics] What the heck, it's now or never, isn't it new? \par \stop
%D \start \setreplacements[more]   Do we --- {\it really} --- need this kind of crap? \par \stop
%D \start \setreplacements[basics] All kinds of special thingies! \par \stop
%D \start \setreplacements[basics] \hsize1mm special \par \stop
%D \stoptyping
%D
%D Only in \LUA, functions:
%D
%D \startluacode
%D     languages.replacements.add("basics", {
%D         ["one"]   = "cool",
%D         ["two"]   = function(s)
%D             return "warm"
%D         end
%D     })
%D \stopluacode
%D
%D \starttyping
%D \start \setreplacements[basics] It's one in here! \stop \par
%D \start \setreplacements[basics] It's two in here! \stop \par
%D \stoptyping

\permanent\protected\def\setreplacements[#1]%
  {\clf_setreplacements{#1}}

\permanent\protected\def\resetreplacements
  {\c_attr_replacements\attributeunsetvalue}

\permanent\tolerant\protected\def\replaceword[#1]#*[#2]#*[#3]%
  {\ifarguments\or\or
     \clf_addreplacementslist{#1}{#2}%
   \or
     \clf_addreplacements{#1}{#2}{#3}%
   \fi}

\appendtoks
    \resetreplacements
\to \everyresettypesetting

\appendtoks
    \resetreplacements
\to \everyinitializeverbatim

\protect \endinput