| 1 |
\ifx\BeginCatcodeRegime\undefined\else\expandafter\endinput\fi |
| 2 |
|
| 3 |
\ifx |
| 4 |
\ProvidesPackage\undefined\begingroup\def\ProvidesPackage |
| 5 |
#1#2[#3]{\endgroup\immediate\write-1{Package: #1 #3}} |
| 6 |
\fi |
| 7 |
\ProvidesPackage{luatexbase} |
| 8 |
[2015/10/04 v1.3 |
| 9 |
luatexbase interface to LuaTeX |
| 10 |
] |
| 11 |
\edef\emuatcatcode{\the\catcode`\@} |
| 12 |
\catcode`\@=11 |
| 13 |
\ifx\@setrangecatcode\@undefined |
| 14 |
\ifx\RequirePackage\@undefined |
| 15 |
\input{ctablestack.sty}% |
| 16 |
\else |
| 17 |
\RequirePackage{ctablestack} |
| 18 |
\fi |
| 19 |
\fi |
| 20 |
\def\RequireLuaModule#1{\directlua{require("#1")}\@gobbleoptarg} |
| 21 |
\ifdefined\@ifnextchar |
| 22 |
\def\@gobbleoptarg{\@ifnextchar[\@gobble@optarg{}}% |
| 23 |
\else |
| 24 |
\long\def\@gobbleoptarg#1{\ifx[#1\expandafter\@gobble@optarg\fi#1}% |
| 25 |
\fi |
| 26 |
\def\@gobble@optarg[#1]{} |
| 27 |
\let\CatcodeTableIniTeX\catcodetable@initex |
| 28 |
\let\CatcodeTableString\catcodetable@string |
| 29 |
\let\CatcodeTableLaTeX\catcodetable@latex |
| 30 |
\let\CatcodeTableLaTeXAtLetter\catcodetable@atletter |
| 31 |
\newcatcodetable\CatcodeTableOther |
| 32 |
\@setcatcodetable\CatcodeTableOther{% |
| 33 |
\catcodetable\CatcodeTableString |
| 34 |
\catcode32 12 } |
| 35 |
\newcatcodetable\CatcodeTableExpl |
| 36 |
\@setcatcodetable\CatcodeTableExpl{% |
| 37 |
\catcodetable\CatcodeTableLaTeX |
| 38 |
\catcode126 10 % tilde is a space char |
| 39 |
\catcode32 9 % space is ignored |
| 40 |
\catcode9 9 % tab also ignored |
| 41 |
\catcode95 11 % underscore letter |
| 42 |
\catcode58 11 % colon letter |
| 43 |
} |
| 44 |
\def\BeginCatcodeRegime#1{% |
| 45 |
\@pushcatcodetable |
| 46 |
\catcodetable#1\relax} |
| 47 |
\def\EndCatcodeRegime{% |
| 48 |
\@popcatcodetable} |
| 49 |
\let\PushCatcodeTableNumStack\@pushcatcodetable |
| 50 |
\let\PopCatcodeTableNumStack\@popcatcodetable |
| 51 |
\let\SetCatcodeRange\@setrangecatcode |
| 52 |
\let\setcatcodetable\@setcatcodetable |
| 53 |
\directlua{ |
| 54 |
function luatexbase.reset_callback(name,make_false) |
| 55 |
for _,v in pairs(luatexbase.callback_descriptions(name)) |
| 56 |
do |
| 57 |
luatexbase.remove_from_callback(name,v) |
| 58 |
end |
| 59 |
if make_false == true then |
| 60 |
luatexbase.disable_callback(name) |
| 61 |
end |
| 62 |
end |
| 63 |
luatexbase.base_add_to_callback=luatexbase.add_to_callback |
| 64 |
function luatexbase.add_to_callback(name,fun,description,priority) |
| 65 |
local priority= priority |
| 66 |
if priority==nil then |
| 67 |
priority=\string#luatexbase.callback_descriptions(name)+1 |
| 68 |
end |
| 69 |
if(luatexbase.callbacktypes[name] == 3 and |
| 70 |
priority == 1 and |
| 71 |
\string#luatexbase.callback_descriptions(name)==1) then |
| 72 |
luatexbase.module_warning("luatexbase", |
| 73 |
"resetting exclusive callback: " .. name) |
| 74 |
luatexbase.reset_callback(name) |
| 75 |
end |
| 76 |
local saved_callback={},ff,dd |
| 77 |
for k,v in pairs(luatexbase.callback_descriptions(name)) do |
| 78 |
if k >= priority then |
| 79 |
ff,dd= luatexbase.remove_from_callback(name, v) |
| 80 |
saved_callback[k]={ff,dd} |
| 81 |
end |
| 82 |
end |
| 83 |
luatexbase.base_add_to_callback(name,fun,description) |
| 84 |
for k,v in pairs(saved_callback) do |
| 85 |
luatexbase.base_add_to_callback(name,v[1],v[2]) |
| 86 |
end |
| 87 |
return |
| 88 |
end |
| 89 |
luatexbase.catcodetables=setmetatable( |
| 90 |
{['latex-package'] = \number\CatcodeTableLaTeXAtLetter, |
| 91 |
ini = \number\CatcodeTableIniTeX, |
| 92 |
string = \number\CatcodeTableString, |
| 93 |
other = \number\CatcodeTableOther, |
| 94 |
latex = \number\CatcodeTableLaTeX, |
| 95 |
expl = \number\CatcodeTableExpl, |
| 96 |
expl3 = \number\CatcodeTableExpl}, |
| 97 |
{ __index = function(t,key) |
| 98 |
return luatexbase.registernumber(key) or nil |
| 99 |
end} |
| 100 |
)} |
| 101 |
\ifnum\luatexversion<80 % |
| 102 |
\def\newcatcodetable#1{% |
| 103 |
\e@alloc\catcodetable\chardef |
| 104 |
\e@alloc@ccodetable@count\m@ne{"8000}#1% |
| 105 |
\initcatcodetable\allocationnumber |
| 106 |
{\escapechar=\m@ne |
| 107 |
\directlua{luatexbase.catcodetables['\string#1']=% |
| 108 |
\the\allocationnumber}}% |
| 109 |
} |
| 110 |
\fi |
| 111 |
\directlua{ |
| 112 |
function luatexbase.priority_in_callback (name,description) |
| 113 |
for i,v in ipairs(luatexbase.callback_descriptions(name)) |
| 114 |
do |
| 115 |
if v == description then |
| 116 |
return i |
| 117 |
end |
| 118 |
end |
| 119 |
return false |
| 120 |
end |
| 121 |
luatexbase.is_active_callback = luatexbase.in_callback |
| 122 |
luatexbase.base_provides_module=luatexbase.provides_module |
| 123 |
function luatexbase.errwarinf(name) |
| 124 |
return |
| 125 |
function(s,...) return luatexbase.module_error(name, s:format(...)) end, |
| 126 |
function(s,...) return luatexbase.module_warning(name, s:format(...)) end, |
| 127 |
function(s,...) return luatexbase.module_info(name, s:format(...)) end, |
| 128 |
function(s,...) return luatexbase.module_info(name, s:format(...)) end |
| 129 |
end |
| 130 |
function luatexbase.provides_module(info) |
| 131 |
luatexbase.base_provides_module(info) |
| 132 |
return luatexbase.errwarinf(info.name) |
| 133 |
end |
| 134 |
} |
| 135 |
\ifnum\luatexversion<80 % |
| 136 |
\def\newattribute#1{% |
| 137 |
\e@alloc\attribute\attributedef |
| 138 |
\e@alloc@attribute@count\m@ne\e@alloc@top#1% |
| 139 |
{\escapechar=\m@ne |
| 140 |
\directlua{luatexbase.attributes['\string#1']=% |
| 141 |
\the\allocationnumber}}% |
| 142 |
} |
| 143 |
\fi |
| 144 |
\ifx\@percentchar\@undefined |
| 145 |
{\catcode`\%=12 \gdef\@percentchar{%}} |
| 146 |
\fi |
| 147 |
\directlua{% |
| 148 |
local copynode = node.copy |
| 149 |
local newnode = node.new |
| 150 |
local nodesubtype = node.subtype |
| 151 |
local nodetype = node.id |
| 152 |
local stringformat = string.format |
| 153 |
local tableunpack = unpack or table.unpack |
| 154 |
local texiowrite_nl = texio.write_nl |
| 155 |
local texiowrite = texio.write |
| 156 |
local whatsit_t = nodetype"whatsit" |
| 157 |
local user_defined_t = nodesubtype"user_defined" |
| 158 |
local unassociated = "__unassociated" |
| 159 |
local user_whatsits = { __unassociated = { } } |
| 160 |
local whatsit_ids = { } |
| 161 |
local anonymous_whatsits = 0 |
| 162 |
local anonymous_prefix = "anon" |
| 163 |
local new_user_whatsit_id = function (name, package) |
| 164 |
if name then |
| 165 |
if not package then |
| 166 |
package = unassociated |
| 167 |
end |
| 168 |
else % anonymous |
| 169 |
anonymous_whatsits = anonymous_whatsits + 1 |
| 170 |
warning("defining anonymous user whatsit no. \@percentchar |
| 171 |
d", anonymous_whatsits) |
| 172 |
package = unassociated |
| 173 |
name = anonymous_prefix .. tostring(anonymous_whatsits) |
| 174 |
end |
| 175 |
|
| 176 |
local whatsitdata = user_whatsits[package] |
| 177 |
if not whatsitdata then |
| 178 |
whatsitdata = { } |
| 179 |
user_whatsits[package] = whatsitdata |
| 180 |
end |
| 181 |
|
| 182 |
local id = whatsitdata[name] |
| 183 |
if id then %- warning |
| 184 |
warning("replacing whatsit \@percentchar s:\@percentchar |
| 185 |
s (\@percentchar d)", package, name, id) |
| 186 |
else %- new id |
| 187 |
id=luatexbase.new_whatsit(name) |
| 188 |
whatsitdata[name] = id |
| 189 |
whatsit_ids[id] = { name, package } |
| 190 |
end |
| 191 |
return id |
| 192 |
end |
| 193 |
luatexbase.new_user_whatsit_id = new_user_whatsit_id |
| 194 |
local new_user_whatsit = function (req, package) |
| 195 |
local id, whatsit |
| 196 |
if type(req) == "string" then |
| 197 |
id = new_user_whatsit_id(req, package) |
| 198 |
whatsit = newnode(whatsit_t, user_defined_t) |
| 199 |
whatsit.user_id = id |
| 200 |
elseif req.id == whatsit_t and req.subtype == user_defined_t then |
| 201 |
id = req.user_id |
| 202 |
whatsit = copynode(req) |
| 203 |
if not whatsit_ids[id] then |
| 204 |
warning("whatsit id \@percentchar d unregistered; " |
| 205 |
.. "inconsistencies may arise", id) |
| 206 |
end |
| 207 |
end |
| 208 |
return function () return copynode(whatsit) end, id |
| 209 |
end |
| 210 |
luatexbase.new_user_whatsit = new_user_whatsit |
| 211 |
local get_user_whatsit_id = function (name, package) |
| 212 |
if not package then |
| 213 |
package = unassociated |
| 214 |
end |
| 215 |
return user_whatsits[package][name] |
| 216 |
end |
| 217 |
luatexbase.get_user_whatsit_id = get_user_whatsit_id |
| 218 |
local get_user_whatsit_name = function (asked) |
| 219 |
local id |
| 220 |
if type(asked) == "number" then |
| 221 |
id = asked |
| 222 |
elseif type(asked) == "function" then |
| 223 |
%- node generator |
| 224 |
local n = asked() |
| 225 |
id = n.user_id |
| 226 |
else %- node |
| 227 |
id = asked.user_id |
| 228 |
end |
| 229 |
local metadata = whatsit_ids[id] |
| 230 |
if not metadata then % unknown |
| 231 |
warning("whatsit id \@percentchar d unregistered; |
| 232 |
inconsistencies may arise", id) |
| 233 |
return "", "" |
| 234 |
end |
| 235 |
return tableunpack(metadata) |
| 236 |
end |
| 237 |
luatexbase.get_user_whatsit_name = get_user_whatsit_name |
| 238 |
local dump_registered_whatsits = function (asked_package) |
| 239 |
local whatsit_list = { } |
| 240 |
if asked_package then |
| 241 |
local whatsitdata = user_whatsits[asked_package] |
| 242 |
if not whatsitdata then |
| 243 |
error("(no user whatsits registered for package |
| 244 |
\@percentchar s)", asked_package) |
| 245 |
return |
| 246 |
end |
| 247 |
texiowrite_nl("(user whatsit allocation stats for " .. |
| 248 |
asked_package) |
| 249 |
for name, id in next, whatsitdata do |
| 250 |
whatsit_list[\string#whatsit_list+1] = |
| 251 |
stringformat("(\@percentchar s:\@percentchar |
| 252 |
s \@percentchar d)", asked_package, name, id) |
| 253 |
end |
| 254 |
else |
| 255 |
texiowrite_nl("(user whatsit allocation stats") |
| 256 |
texiowrite_nl(stringformat(" ((total \@percentchar d)\string\n |
| 257 |
(anonymous \@percentchar d))", |
| 258 |
current_whatsit, anonymous_whatsits)) |
| 259 |
for package, whatsitdata in next, user_whatsits do |
| 260 |
for name, id in next, whatsitdata do |
| 261 |
whatsit_list[\string#whatsit_list+1] = |
| 262 |
stringformat("(\@percentchar s:\@percentchar |
| 263 |
s \@percentchar d)", package, name, id) |
| 264 |
end |
| 265 |
end |
| 266 |
end |
| 267 |
texiowrite_nl" (" |
| 268 |
local first = true |
| 269 |
for i=1, \string#whatsit_list do |
| 270 |
if first then |
| 271 |
first = false |
| 272 |
else % indent |
| 273 |
texiowrite_nl" " |
| 274 |
end |
| 275 |
texiowrite(whatsit_list[i]) |
| 276 |
end |
| 277 |
texiowrite"))\string\n" |
| 278 |
end |
| 279 |
luatexbase.dump_registered_whatsits = dump_registered_whatsits |
| 280 |
luatexbase.newattribute = new_attribute |
| 281 |
luatexbase.newuserwhatsit = new_user_whatsit |
| 282 |
luatexbase.newuserwhatsitid = new_user_whatsit_id |
| 283 |
luatexbase.getuserwhatsitid = get_user_whatsit_id |
| 284 |
luatexbase.getuserwhatsitname = get_user_whatsit_name |
| 285 |
luatexbase.dumpregisteredwhatsits = dump_registered_whatsits |
| 286 |
} |
| 287 |
\let\newluatexattribute\newattribute |
| 288 |
\let\setluatexattribute\setattribute |
| 289 |
\let\unsetluatexattribute\unsetattribute |
| 290 |
\let\newluatexcatcodetable\newcatcodetable |
| 291 |
\let\setluatexcatcodetable\setcatcodetable |
| 292 |
\let\luatexbase@directlua\directlua |
| 293 |
\let\luatexbase@ensure@primitive\@gobble |
| 294 |
\let\luatexattribute\attribute |
| 295 |
\let\luatexattributedef\attributedef |
| 296 |
\let\luatexcatcodetable\catcodetable |
| 297 |
\let\luatexluaescapestring\luaescapestring |
| 298 |
\let\luatexlatelua\latelua |
| 299 |
\let\luatexoutputbox\outputbox |
| 300 |
\let\luatexscantextokens\scantextokens |
| 301 |
\catcode`\@=\emuatcatcode\relax |