diff options
Diffstat (limited to 'Master/texmf-dist/tex/luatex/luatexbase/mcb.lua')
-rw-r--r-- | Master/texmf-dist/tex/luatex/luatexbase/mcb.lua | 366 |
1 files changed, 160 insertions, 206 deletions
diff --git a/Master/texmf-dist/tex/luatex/luatexbase/mcb.lua b/Master/texmf-dist/tex/luatex/luatexbase/mcb.lua index ff4605af30d..2b65b90a22d 100644 --- a/Master/texmf-dist/tex/luatex/luatexbase/mcb.lua +++ b/Master/texmf-dist/tex/luatex/luatexbase/mcb.lua @@ -12,198 +12,154 @@ -- See source file 'luatexbase-mcb.dtx' for details. -- module('luatexbase', package.seeall) -luatexbase.provides_module({ - name = "luamcallbacks", +local err, warning, info = luatexbase.provides_module({ + name = "luatexbase-mcb", version = 0.2, date = "2010/05/12", description = "register several functions in a callback", - author = "Hans Hagen, Elie Roux and Manuel P^^c3^^a9gourie-Gonnard", - copyright = "Hans Hagen, Elie Roux and Manuel P^^c3^^a9gourie-Gonnard", + author = "Hans Hagen, Elie Roux and Manuel Pegourie-Gonnard", + copyright = "Hans Hagen, Elie Roux and Manuel Pegourie-Gonnard", license = "CC0", }) -local log = log or function(...) - luatexbase.module_log('luamcallbacks', string.format(...)) -end -local info = info or function(...) - luatexbase.module_info('luamcallbacks', string.format(...)) -end -local warning = warning or function(...) - luatexbase.module_warning('luamcallbacks', string.format(...)) -end -local err = err or function(...) - luatexbase.module_error('luamcallbacks', string.format(...)) -end local callbacklist = callbacklist or { } -local lua_callbacks_defaults = { } -local list = 1 -local data = 2 -local first = 3 -local simple = 4 +local list, data, first, simple = 1, 2, 3, 4 +local types = { + list = list, + data = data, + first = first, + simple = simple, +} local callbacktypes = callbacktypes or { - buildpage_filter = simple, - token_filter = first, - pre_output_filter = list, - hpack_filter = list, - process_input_buffer = data, - mlist_to_hlist = list, - vpack_filter = list, - define_font = first, - open_read_file = first, - linebreak_filter = list, - post_linebreak_filter = list, - pre_linebreak_filter = list, - start_page_number = simple, - stop_page_number = simple, - start_run = simple, - show_error_hook = simple, - stop_run = simple, - hyphenate = simple, - ligaturing = simple, - kerning = data, - find_write_file = first, - find_read_file = first, - find_vf_file = data, - find_map_file = data, - find_format_file = data, - find_opentype_file = data, - find_output_file = data, - find_truetype_file = data, - find_type1_file = data, - find_data_file = data, - find_pk_file = data, - find_font_file = data, - find_image_file = data, - find_ocp_file = data, - find_sfd_file = data, - find_enc_file = data, - read_sfd_file = first, - read_map_file = first, - read_pk_file = first, - read_enc_file = first, - read_vf_file = first, - read_ocp_file = first, - read_opentype_file = first, - read_truetype_file = first, - read_font_file = first, - read_type1_file = first, - read_data_file = first, + find_read_file = first, + find_write_file = first, + find_font_file = data, + find_output_file = data, + find_format_file = data, + find_vf_file = data, + find_ocp_file = data, + find_map_file = data, + find_enc_file = data, + find_sfd_file = data, + find_pk_file = data, + find_data_file = data, + find_opentype_file = data, + find_truetype_file = data, + find_type1_file = data, + find_image_file = data, + open_read_file = first, + read_font_file = first, + read_vf_file = first, + read_ocp_file = first, + read_map_file = first, + read_enc_file = first, + read_sfd_file = first, + read_pk_file = first, + read_data_file = first, + read_truetype_file = first, + read_type1_file = first, + read_opentype_file = first, + process_input_buffer = data, + process_output_buffer = data, + token_filter = first, + buildpage_filter = simple, + pre_linebreak_filter = list, + linebreak_filter = list, + post_linebreak_filter = list, + hpack_filter = list, + vpack_filter = list, + pre_output_filter = list, + hyphenate = simple, + ligaturing = simple, + kerning = simple, + mlist_to_hlist = list, + start_run = simple, + stop_run = simple, + start_page_number = simple, + stop_page_number = simple, + show_error_hook = simple, + define_font = first, } -if luatexbase.luatexversion > 42 then - callbacktypes["process_output_buffer"] = data +local lua_callbacks_defaults = { } +local original_register = original_register or callback.register +callback.register = function () + err("function callback.register has been trapped,\n" + .."please use luatexbase.add_to_callback instead.") end -local internalregister = internalregister or callback.register -local function str_to_type(str) - if str == 'list' then - return list - elseif str == 'data' then - return data - elseif str == 'first' then - return first - elseif str == 'simple' then - return simple - else - return nil - end +local function register_callback(...) + return assert(original_register(...)) end --- local -function listhandler (name) +local function listhandler (name) return function(head,...) - local l = callbacklist[name] - if l then - local done = true - for _, f in ipairs(l) do - -- the returned value is either true or a new head plus true - rtv1, rtv2 = f.func(head,...) - if type(rtv1) == 'boolean' then - done = rtv1 - elseif type (rtv1) == 'userdata' then - head = rtv1 - end - if type(rtv2) == 'boolean' then - done = rtv2 - elseif type(rtv2) == 'userdata' then - head = rtv2 - end - if done == false then - err("function \"%s\" returned false in callback '%s'", - f.description, name) - end + local ret + local alltrue = true + for _, f in ipairs(callbacklist[name]) do + ret = f.func(head, ...) + if ret == false then + warn("function '%s' returned false\nin callback '%s'", + f.description, name) + break + end + if ret ~= true then + alltrue = false + head = ret end - return head, done - else - return head, false end + return alltrue and true or head end end local function datahandler (name) - return function(data,...) - local l = callbacklist[name] - if l then - for _, f in ipairs(l) do - data = f.func(data,...) - end + return function(data, ...) + for _, f in ipairs(callbacklist[name]) do + data = f.func(data, ...) end return data end end local function firsthandler (name) return function(...) - local l = callbacklist[name] - if l then - local f = l[1].func - return f(...) - else - return nil, false - end + return callbacklist[name][1].func(...) end end local function simplehandler (name) return function(...) - local l = callbacklist[name] - if l then - for _, f in ipairs(l) do - f.func(...) - end + for _, f in ipairs(callbacklist[name]) do + f.func(...) end end end +local handlers = { + [list] = listhandler, + [data] = datahandler, + [first] = firsthandler, + [simple] = simplehandler, +} function add_to_callback (name,func,description,priority) if type(func) ~= "function" then - err("unable to add function, no proper function passed") - return + return err("unable to add function:\nno proper function passed") end if not name or name == "" then - err("unable to add function, no proper callback name passed") + err("unable to add function:\nno proper callback name passed") return elseif not callbacktypes[name] then - err("unable to add function, '%s' is not a valid callback", name) + err("unable to add function:\n'%s' is not a valid callback", name) return end if not description or description == "" then - err("unable to add function to '%s', no proper description passed", + err("unable to add function to '%s':\nno proper description passed", name) return end - if priority_in_callback(name, description) ~= 0 then - warning("function '%s' already registered in callback '%s'", + if priority_in_callback(name, description) then + err("function '%s' already registered\nin callback '%s'", description, name) + return end local l = callbacklist[name] if not l then l = {} callbacklist[name] = l if not lua_callbacks_defaults[name] then - if callbacktypes[name] == list then - internalregister(name, listhandler(name)) - elseif callbacktypes[name] == data then - internalregister(name, datahandler(name)) - elseif callbacktypes[name] == simple then - internalregister(name, simplehandler(name)) - elseif callbacktypes[name] == first then - internalregister(name, firsthandler(name)) - else - err("unknown callback type") - end + register_callback(name, handlers[callbacktypes[name]](name)) end end local f = { @@ -216,25 +172,25 @@ function add_to_callback (name,func,description,priority) elseif priority < 1 then priority = 1 end - if callbacktypes[name] == first and (priority ~= 1 or #l ~= 0) then - warning("several callbacks registered in callback '%s', " - .."only the first function will be active.", name) - end table.insert(l,priority,f) - log("inserting function '%s' at position %s in callback list for '%s'", + if callbacktypes[name] == first and #l ~= 1 then + warning("several functions in '%s',\n" + .."only one will be active.", name) + end + info("inserting '%s'\nat position %s in '%s'", description, priority, name) end function remove_from_callback (name, description) if not name or name == "" then - err("unable to remove function, no proper callback name passed") + err("unable to remove function:\nno proper callback name passed") return elseif not callbacktypes[name] then - err("unable to remove function, '%s' is not a valid callback", name) + err("unable to remove function:\n'%s' is not a valid callback", name) return end if not description or description == "" then err( - "unable to remove function from '%s', no proper description passed", + "unable to remove function from '%s':\nno proper description passed", name) return end @@ -243,68 +199,91 @@ function remove_from_callback (name, description) err("no callback list for '%s'",name) return end + local index = false for k,v in ipairs(l) do if v.description == description then - table.remove(l,k) - log("removing function '%s' from '%s'",description,name) - if not next(l) then - callbacklist[name] = nil - if not lua_callbacks_defaults[name] then - internalregister(name, nil) - end - end - return + index = k + break end end - warning("unable to remove function '%s' from '%s'",description,name) + if not index then + err("unable to remove '%s'\nfrom '%s'", description, name) + return + end + table.remove(l, index) + info("removing '%s'\nfrom '%s'", description, name) + if table.maxn(l) == 0 then + callbacklist[name] = nil + if not lua_callbacks_defaults[name] then + register_callback(name, nil) + end + end + return end -function reset_callback (name) +function reset_callback (name, make_false) if not name or name == "" then - err("unable to reset, no proper callback name passed") + err("unable to reset:\nno proper callback name passed") return elseif not callbacktypes[name] then - err("reset error, '%s' is not a valid callback", name) + err("unable to reset '%s':\nis not a valid callback", name) return end + info("resetting callback '%s'", name) + callbacklist[name] = nil if not lua_callbacks_defaults[name] then - internalregister(name, nil) + if make_false == true then + info("setting '%s' to false", name) + register_callback(name, false) + else + register_callback(name, nil) + end + end +end +function priority_in_callback (name, description) + if not name or name == "" + or not callbacktypes[name] + or not description then + return false end local l = callbacklist[name] - if l then - log("resetting callback list '%s'",name) - callbacklist[name] = nil + if not l then return false end + for p, f in pairs(l) do + if f.description == description then + return p + end end + return false end function create_callback(name, ctype, default) if not name then - err("unable to call callback, no proper name passed", name) + err("unable to call callback:\nno proper name passed", name) return nil end if not ctype or not default then - err("unable to create callback '%s': " + err("unable to create callback '%s':\n" .."callbacktype or default function not specified", name) return nil end if callbacktypes[name] then - err("unable to create callback '%s', callback already exists", name) + err("unable to create callback '%s':\ncallback already exists", name) return nil end - local temp = str_to_type(ctype) - if not temp then - err("unable to create callback '%s', type '%s' undefined", name, ctype) + ctype = types[ctype] + if not ctype then + err("unable to create callback '%s':\ntype '%s' undefined", name, ctype) return nil end - ctype = temp + info("creating '%s' type %s", name, ctype) lua_callbacks_defaults[name] = default callbacktypes[name] = ctype end function call_callback(name, ...) if not name then - err("unable to call callback, no proper name passed", name) + err("unable to call callback:\nno proper name passed", name) return nil end if not lua_callbacks_defaults[name] then - err("unable to call lua callback '%s', unknown callback", name) + err("unable to call lua callback '%s':\nunknown callback", name) return nil end local l = callbacklist[name] @@ -312,38 +291,13 @@ function call_callback(name, ...) if not l then f = lua_callbacks_defaults[name] else - if callbacktypes[name] == list then - f = listhandler(name) - elseif callbacktypes[name] == data then - f = datahandler(name) - elseif callbacktypes[name] == simple then - f = simplehandler(name) - elseif callbacktypes[name] == first then - f = firsthandler(name) - else + f = handlers[callbacktypes[name]](name) + if not f then err("unknown callback type") + return end end return f(...) end -function priority_in_callback (name, description) - if not name or name == "" - or not callbacktypes[name] - or not description then - return 0 - end - local l = callbacklist[name] - if not l then return 0 end - for p, f in pairs(l) do - if f.description == description then - return p - end - end - return 0 -end -callback.register = function () -err("function callback.register has been deleted by luamcallbacks, " -.."please use luatexbase.add_to_callback instead.") -end -- -- End of File `mcb.lua'. |