1
2
3
4
5
6
7
8
9
10
11
12
13
14\startmodule[systemaliasing]
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
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
111
112
113
114
115
116
117
118
119
120\stopmodule
121 |