| 1 |
-- |
| 2 |
-- This is file `luainputenc.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- luainputenc.dtx (with options: `lua') |
| 8 |
-- This is a generated file. |
| 9 |
-- |
| 10 |
-- Written in 2009, 2010 by Manuel Pegourie-Gonnard and Elie Roux. |
| 11 |
-- |
| 12 |
-- This work is under the CC0 license. As an exception, the files |
| 13 |
-- luainputenc.sty, lutf8.def and lutf8x.def have more restrictions. |
| 14 |
-- See these file for more details. |
| 15 |
-- |
| 16 |
-- People who helped on this package: |
| 17 |
-- Javier Bezos |
| 18 |
-- Will Robertson |
| 19 |
-- Hans Hagen |
| 20 |
-- Heiko Oberdiek |
| 21 |
-- Javier Mugica |
| 22 |
-- |
| 23 |
-- This work consists of the main source file luainputenc.dtx |
| 24 |
-- and the derived files |
| 25 |
-- luainputenc.sty, luainputenc.lua, lutf8.def, lutf8x.def, |
| 26 |
-- and luainputenc.pdf |
| 27 |
-- |
| 28 |
|
| 29 |
module('luainputenc', package.seeall) |
| 30 |
|
| 31 |
luainputenc.module = { |
| 32 |
name = "luainputenc", |
| 33 |
version = 0.97, |
| 34 |
date = "2010/05/10", |
| 35 |
description = "Lua simple inputenc package.", |
| 36 |
author = "Elie Roux", |
| 37 |
copyright = "Elie Roux", |
| 38 |
license = "CC0", |
| 39 |
} |
| 40 |
|
| 41 |
luatexbase.provides_module(luainputenc.module) |
| 42 |
|
| 43 |
local format = string.format |
| 44 |
|
| 45 |
luainputenc.log = luainputenc.log or function(...) |
| 46 |
luatexbase.module_log('luainputenc', format(...)) |
| 47 |
end |
| 48 |
|
| 49 |
|
| 50 |
luainputenc.encoding = "utf8" |
| 51 |
luainputenc.package_option = nil |
| 52 |
|
| 53 |
function luainputenc.set_option(option) |
| 54 |
luainputenc.package_option = option |
| 55 |
if option == "lutf8" or option == "lutf8x" or option == "utf8x" or option == "unactivate" then |
| 56 |
luainputenc.encoding = "utf8" |
| 57 |
else |
| 58 |
luainputenc.encoding = option |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
|
| 63 |
local char, utfchar, byte, format, gsub, utfbyte, utfgsub = |
| 64 |
string.char, unicode.utf8.char, string.byte, string.format, string.gsub, unicode.utf8.byte, unicode.utf8.gsub |
| 65 |
|
| 66 |
|
| 67 |
function luainputenc.byte_to_utf(ch) |
| 68 |
return utfchar(byte(ch)) |
| 69 |
end |
| 70 |
|
| 71 |
|
| 72 |
function luainputenc.fake_utf_read(buf) |
| 73 |
return gsub(buf,"(.)", luainputenc.byte_to_utf) |
| 74 |
end |
| 75 |
|
| 76 |
|
| 77 |
function luainputenc.utf_to_byte(ch) |
| 78 |
return char(utfbyte(ch)) |
| 79 |
end |
| 80 |
|
| 81 |
|
| 82 |
function luainputenc.fake_utf_write(buf) |
| 83 |
return utfgsub(buf,"(.)", luainputenc.utf_to_byte) |
| 84 |
end |
| 85 |
|
| 86 |
|
| 87 |
if tex.luatexversion > 42 then |
| 88 |
|
| 89 |
function luainputenc.register_callbacks() |
| 90 |
luatexbase.add_to_callback('process_output_buffer', luainputenc.fake_utf_write, 'luainputenc.fake_utf_write') |
| 91 |
luatexbase.add_to_callback('process_input_buffer', luainputenc.fake_utf_read, 'luainputenc.fake_utf_read') |
| 92 |
end |
| 93 |
|
| 94 |
else |
| 95 |
|
| 96 |
|
| 97 |
local started, stopped = 1, 0 |
| 98 |
|
| 99 |
luainputenc.state = stopped |
| 100 |
|
| 101 |
function luainputenc.setstate(state) |
| 102 |
if state == luainputenc.state then |
| 103 |
return |
| 104 |
elseif state == started then |
| 105 |
luainputenc.start() |
| 106 |
else |
| 107 |
luainputenc.stop() |
| 108 |
end |
| 109 |
end |
| 110 |
|
| 111 |
function luainputenc.setstarted() |
| 112 |
luainputenc.setstate(started) |
| 113 |
end |
| 114 |
|
| 115 |
function luainputenc.setstopped() |
| 116 |
luainputenc.setstate(stopped) |
| 117 |
end |
| 118 |
|
| 119 |
function luainputenc.start() |
| 120 |
luatexbase.add_to_callback('process_input_buffer', luainputenc.fake_utf_read, |
| 121 |
'luainputenc.fake_utf_read') |
| 122 |
luainputenc.state = started |
| 123 |
if luainputenc.callback_registered == 0 then |
| 124 |
luainputenc.register_callback() |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
function luainputenc.stop() |
| 129 |
luatexbase.remove_from_callback('process_input_buffer', 'luainputenc.fake_utf_read') |
| 130 |
luainputenc.state = stopped |
| 131 |
return |
| 132 |
end |
| 133 |
|
| 134 |
|
| 135 |
luainputenc.unicode_extentions = { |
| 136 |
['.aux'] = 1, -- basic files |
| 137 |
['.toc'] = 1, |
| 138 |
['.gls'] = 1, |
| 139 |
['.ind'] = 1, |
| 140 |
['.idx'] = 1, |
| 141 |
['.vrb'] = 1, -- beamer and powerdot |
| 142 |
['.nav'] = 1, -- other beamer extentions |
| 143 |
['.sol'] = 1, |
| 144 |
['.qsl'] = 1, |
| 145 |
['.snm'] = 1, |
| 146 |
['.pgn'] = 1, -- pagereference |
| 147 |
['.cpg'] = 1, -- AlProTeX |
| 148 |
['.pst'] = 1, -- pst-tree |
| 149 |
['.tmp'] = 1, -- sauerj/collect |
| 150 |
['.sym'] = 1, -- listofsymbols |
| 151 |
['.sub'] = 1, -- listofsymbols |
| 152 |
['.lof'] = 1, -- preprint |
| 153 |
['.lot'] = 1, -- preprint |
| 154 |
['mtc1'] = 1, -- minitoc |
| 155 |
['.ovr'] = 1, -- thumbss |
| 156 |
['.fff'] = 1, -- endplate |
| 157 |
['.sbb'] = 1, -- splitbib |
| 158 |
['.bbl'] = 1, -- latex |
| 159 |
['.ain'] = 1, -- authorindex |
| 160 |
['.abb'] = 1, -- juraabbrev |
| 161 |
['.ent'] = 1, -- endnotes |
| 162 |
['.end'] = 1, -- fn2end |
| 163 |
['.thm'] = 1, -- ntheorem |
| 164 |
['.xtr'] = 1, -- extract |
| 165 |
['.han'] = 1, -- linguho |
| 166 |
['.bnd'] = 1, -- bibref |
| 167 |
['.bbl'] = 1, -- bibref |
| 168 |
['.col'] = 1, -- mwrite |
| 169 |
['.ttt'] = 1, -- endfloat |
| 170 |
['.fax'] = 1, -- lettre |
| 171 |
['.tns'] = 1, -- lettre |
| 172 |
['.odt'] = 1, -- lettre |
| 173 |
['.etq'] = 1, -- lettre |
| 174 |
['.emd'] = 1, -- poemscol |
| 175 |
['.emx'] = 1, -- poemscol |
| 176 |
['.ctn'] = 1, -- poemscol |
| 177 |
['.hst'] = 1, -- vhistory |
| 178 |
['.acr'] = 1, -- crosswrd |
| 179 |
['.dwn'] = 1, -- crosswrd |
| 180 |
['.ttc'] = 1, -- talk |
| 181 |
-- ['.txt'] = 1, -- coverpage, but not sure it's safe to include it... |
| 182 |
['.eve'] = 1, -- calend0 |
| 183 |
['.scn'] = 1, -- cwebmac |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
luainputenc.unicode_files = {} |
| 188 |
|
| 189 |
luainputenc.non_unicode_files = {} |
| 190 |
|
| 191 |
function luainputenc.set_unicode_file(filename) |
| 192 |
if luainputenc.non_unicode_files[filename] == 1 then |
| 193 |
luainputenc.non_unicode_files[filename] = nil |
| 194 |
end |
| 195 |
luainputenc.unicode_files[filename] = 1 |
| 196 |
end |
| 197 |
|
| 198 |
function luainputenc.set_non_unicode_file(filename) |
| 199 |
if luainputenc.unicode_files[filename] == 1 then |
| 200 |
luainputenc.unicode_files[filename] = nil |
| 201 |
end |
| 202 |
luainputenc.non_unicode_files[filename] = 1 |
| 203 |
end |
| 204 |
|
| 205 |
function luainputenc.set_unicode_extention(ext) |
| 206 |
luainputenc.unicode_extention[ext] = 1 |
| 207 |
end |
| 208 |
|
| 209 |
function luainputenc.set_non_unicode_extention(ext) |
| 210 |
if luainputenc.unicode_extentions[ext] == 1 then |
| 211 |
luainputenc.unicode_extentions[ext] = nil |
| 212 |
end |
| 213 |
end |
| 214 |
|
| 215 |
function luainputenc.unset_file(filename) |
| 216 |
if luainputenc.unicode_files[filename] == 1 then |
| 217 |
luainputenc.unicode_files[filename] = nil |
| 218 |
elseif luainputenc.non_unicode_files[filename] == 1 then |
| 219 |
luainputenc.non_unicode_files[filename] = nil |
| 220 |
end |
| 221 |
end |
| 222 |
|
| 223 |
local unicode, non_unicode = stopped, started |
| 224 |
|
| 225 |
function luainputenc.find_state(filename) |
| 226 |
if luainputenc.unicode_files[filename] == 1 then |
| 227 |
return unicode |
| 228 |
elseif luainputenc.non_unicode_files[filename] == 1 then |
| 229 |
return non_unicode |
| 230 |
else |
| 231 |
local ext = filename:sub(-4) |
| 232 |
if luainputenc.unicode_extentions[ext] == 1 then |
| 233 |
return unicode |
| 234 |
else |
| 235 |
return non_unicode |
| 236 |
end |
| 237 |
end |
| 238 |
end |
| 239 |
|
| 240 |
|
| 241 |
function luainputenc.pre_read_file(env) |
| 242 |
if not env.path then |
| 243 |
return |
| 244 |
end |
| 245 |
local currentstate = luainputenc.state |
| 246 |
luainputenc.setstate(luainputenc.find_state(env.filename)) |
| 247 |
env.previousstate = currentstate |
| 248 |
end |
| 249 |
|
| 250 |
function luainputenc.close(env) |
| 251 |
luainputenc.setstate(env.previousstate) |
| 252 |
end |
| 253 |
|
| 254 |
luainputenc.callback_registered = 0 |
| 255 |
|
| 256 |
function luainputenc.register_callback() |
| 257 |
if luainputenc.callback_registered == 0 then |
| 258 |
luatexbase.add_to_callback('pre_read_file', luainputenc.pre_read_file, |
| 259 |
'luainputenc.pre_read_file') |
| 260 |
luatexbase.add_to_callback('file_close', luainputenc.close, 'luainputenc.close') |
| 261 |
luainputenc.callback_registered = 1 |
| 262 |
end |
| 263 |
end |
| 264 |
|
| 265 |
end |
| 266 |
|
| 267 |
|
| 268 |
luainputenc.activated_characters = {} |
| 269 |
luainputenc.characters_are_activated = false |
| 270 |
|
| 271 |
function luainputenc.declare_character(c) |
| 272 |
luainputenc.activated_characters[tonumber(c)] = true |
| 273 |
end |
| 274 |
|
| 275 |
function luainputenc.force_characters_activated () |
| 276 |
luainputenc.characters_are_activated = true |
| 277 |
end |
| 278 |
|
| 279 |
function luainputenc.activate_characters() |
| 280 |
if not luainputenc.characters_are_activated then |
| 281 |
for n, _ in pairs(luainputenc.activated_characters) do |
| 282 |
tex.sprint(string.format('\\catcode %d\\active',n)) |
| 283 |
end |
| 284 |
luainputenc.characters_are_activated = true |
| 285 |
end |
| 286 |
end |
| 287 |
|
| 288 |
function luainputenc.desactivate_characters() |
| 289 |
if luainputenc.characters_are_activated then |
| 290 |
for n, _ in pairs(luainputenc.activated_characters) do |
| 291 |
tex.sprint(string.format('\\catcode %d=11',n)) |
| 292 |
end |
| 293 |
luainputenc.characters_are_activated = false |
| 294 |
end |
| 295 |
end |
| 296 |
|
| 297 |
-- |
| 298 |
-- End of File `luainputenc.lua'. |