m-system-aliasing.mkiv /size: 3545 b    last modification: 2021-10-28 13:51
1%D \module
2%D   [       file=m-system-aliasing,
3%D        version=2020.07.08,
4%D          title=\CONTEXT\ Modules,
5%D       subtitle=Loading generic stuff,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9
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\startmodule[system-aliasing]
15
16\startluacode
17    local next = next
18    local find, topattern = string.find, string.topattern
19
20    local aliases   = { }
21    local replacer  = false
22    local filenames = false
23    local wildcards = false
24    local enabled   = false
25
26    local report    = logs.reporter("system", "aliasing")
27
28    -- This is not yet perfect as we actually need to quit at a non token.
29
30    interfaces.implement {
31        name      = "registeralias",
32        public    = true,
33        arguments = { "csname", "csname" },
34        actions   = function(old,new)
35            aliases[old] = new
36            replacer = false
37        end
38    }
39
40    interfaces.implement {
41        name      = "registeraliasfile",
42        public    = true,
43        arguments = "string",
44        actions   = function(name)
45            if find(name,"%*") then
46                name = topattern(name)
47                if wildcards then wildcards[name] = true else wildcards = { [name] = true } end
48            else
49                if filenames then filenames[name] = true else filenames = { [name] = true } end
50            end
51            if not enabled then
52                utilities.sequencers.appendaction(
53                    resolvers.openers.helpers.textfileactions,
54                    "system","resolvers.macros.processgeneric"
55                )
56                utilities.sequencers.enableaction(
57                    resolvers.openers.helpers.textfileactions,
58                    "resolvers.macros.processgeneric"
59                )
60                enabled = true
61            end
62        end
63    }
64
65    local function found(name)
66        if filenames and filenames[name] then
67            return true
68        end
69        if wildcards then
70            for k, v in next, wildcards do
71                if find(name,k) then
72                    return true
73                end
74            end
75        end
76        return false
77    end
78
79    local Cs, lpegmatch = lpeg.Cs, lpeg.match
80
81    local utfchartabletopattern = lpeg.utfchartabletopattern
82    local utf8character         = lpeg.patterns.utf8character
83    local escapecharacter       = lpeg.P("\\")
84    local terminal              = lpeg.S([[`"'~@#$%^&_-+/*=(){}[]<>:;,.!?|\\]])
85                                + lpeg.P(-1)
86    local lpegmatch             = lpeg.match
87
88    function resolvers.macros.processgeneric(str,name)
89        if found(name) then
90            report("file %a",name)
91            if not replacer then
92                replacer = Cs( (
93                    escapecharacter
94                  * (utfchartabletopattern(aliases) / aliases)
95                  * terminal
96                  + utf8character
97                )^0 )
98            end
99            str = lpegmatch(replacer,str) or str
100        end
101        return str
102    end
103
104\stopluacode
105
106\registeralias \protected  \normalprotected
107\registeralias \unexpanded \normalunexpanded
108\registeralias \expanded   \normalexpanded
109
110%D \starttyping
111%D \registeraliasfile{rubish.tex}
112%D \registeraliasfile{generic/*.tex}
113%D
114%D % e.g. \def\foo{\unexpanded{test}}
115%D
116%D \input rubish.tex
117%D \input generic/foo.tex
118%D \stoptyping
119
120\stopmodule
121