From b424a6bfe42a419372ec6b71ee65e39608002d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 29 Mar 2010 19:19:13 +0200 Subject: Move code around, finish local/global sorting. --- luatexbase-mcb.dtx | 296 +++++++++++++++++++++++++++-------------------------- 1 file changed, 149 insertions(+), 147 deletions(-) (limited to 'luatexbase-mcb.dtx') diff --git a/luatexbase-mcb.dtx b/luatexbase-mcb.dtx index a055813..924bb3e 100644 --- a/luatexbase-mcb.dtx +++ b/luatexbase-mcb.dtx @@ -241,7 +241,7 @@ See source file '\inFileName' for details. \let\x\ProvidesPackage \fi \expandafter\endgroup -\x{luatexbase-mcb}[2010/09/11 v0.93 Callbacks hanndling for LuaTeX (mpg)] +\x{luatexbase-mcb}[2010/09/11 v0.93 Callback management for LuaTeX (mpg)] % \end{macrocode} % % Make sure \luatex is used. @@ -456,77 +456,94 @@ local function str_to_type(str) end % \end{macrocode} % -% This first function creates a new callback. The signature is -% \texttt{create(name, ctype, default)} where \texttt{name} is the name of -% the new callback to create, \texttt{ctype} is the type of callback, and -% \texttt{default} is the default function to call if no function is -% registered in this callback. -% -% The created callback will behave the same way Lua\TeX\ callbacks do, you -% can add and remove functions in it. The difference is that the callback -% is not automatically called, the package developer creating a new -% callback must also call it, see next function. +% This function and the following ones are only internal. This one is the +% handler for the first type of callbacks: the ones that take a list head +% and return true, false, or a new list head. % % \begin{macrocode} -local function create(name, ctype, default) - if not name then - err("unable to call callback, no proper name passed", name) - return nil - end - if not ctype or not default then - err("unable to create callback '%s': " - .."callbacktype or default function not specified", name) - return nil - end - if callbacktypes[name] then - err("unable to create callback '%s', callback 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) - return nil +-- 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 + end + return head, done + else + return head, false + end end - ctype = temp - lua_callbacks_defaults[name] = default - callbacktypes[name] = ctype end % \end{macrocode} % -% This function calls a callback. It can only call a callback created by -% the \texttt{create} function. +% The handler for callbacks taking datas and returning modified ones. % % \begin{macrocode} -local function call(name, ...) - if not name then - err("unable to call callback, no proper name passed", name) - return nil - end - if not lua_callbacks_defaults[name] then - err("unable to call lua callback '%s', unknown callback", name) - return nil +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 + end + return data end - local l = callbacklist[name] - local f - 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) +end +% \end{macrocode} +% +% This function is for the handlers that don't support more than one +% functions in them. In this case we only call the first function of the +% list. +% +% \begin{macrocode} +local function firsthandler (name) + return function(...) + local l = callbacklist[name] + if l then + local f = l[1].func + return f(...) else - err("unknown callback type") + return nil, false + end + end +end +% \end{macrocode} +% +% Handler for simple functions that don't return anything. +% +% \begin{macrocode} +local function simplehandler (name) + return function(...) + local l = callbacklist[name] + if l then + for _, f in ipairs(l) do + f.func(...) + end end end - return f(...) end % \end{macrocode} % +% \subsubsection{Public functions} +% % The main function. The signature is \texttt{add (name, % func, description, priority)} with \texttt{name} being the name of the % callback in which the function is added; \texttt{func} is the added @@ -547,7 +564,7 @@ end % to their function themselves. Most of the time, the priority is not needed. % % \begin{macrocode} -local function add (name,func,description,priority) +function add (name,func,description,priority) if type(func) ~= "function" then err("unable to add function, no proper function passed") return @@ -606,36 +623,13 @@ local function add (name,func,description,priority) end % \end{macrocode} % -% This function tells if a function has already been registered in a -% callback, and gives its current priority. The arguments are the name of -% the callback and the description of the function. If it has already been -% registered, it gives its priority, and if not it returns false. -% -% \begin{macrocode} -function get_priority (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 -% \end{macrocode} -% % The function that removes a function from a callback. The signature is % \texttt{mcallbacks.remove (name, description)} with \texttt{name} being % the name of callbacks, and description the description passed to % \texttt{mcallbacks.add}. % % \begin{macrocode} -local function remove (name, description) +function remove (name, description) if not name or name == "" then err("unable to remove function, no proper callback name passed") return @@ -674,7 +668,7 @@ end % This function removes all the functions registered in a callback. % % \begin{macrocode} -local function reset (name) +function reset (name) if not name or name == "" then err("unable to reset, no proper callback name passed") return @@ -693,89 +687,97 @@ local function reset (name) end % \end{macrocode} % -% This function and the following ones are only internal. This one is the -% handler for the first type of callbacks: the ones that take a list head -% and return true, false, or a new list head. -% -% \begin{macrocode} --- 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 - end - return head, done - else - return head, false - end - end -end -% \end{macrocode} +% This first function creates a new callback. The signature is +% \texttt{create(name, ctype, default)} where \texttt{name} is the name of +% the new callback to create, \texttt{ctype} is the type of callback, and +% \texttt{default} is the default function to call if no function is +% registered in this callback. % -% The handler for callbacks taking datas and returning modified ones. +% The created callback will behave the same way Lua\TeX\ callbacks do, you +% can add and remove functions in it. The difference is that the callback +% is not automatically called, the package developer creating a new +% callback must also call it, see next function. % % \begin{macrocode} -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 - end - return data +function create(name, ctype, default) + if not name then + err("unable to call callback, no proper name passed", name) + return nil + end + if not ctype or not default then + err("unable to create callback '%s': " + .."callbacktype or default function not specified", name) + return nil end + if callbacktypes[name] then + err("unable to create callback '%s', callback 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) + return nil + end + ctype = temp + lua_callbacks_defaults[name] = default + callbacktypes[name] = ctype end % \end{macrocode} % -% This function is for the handlers that don't support more than one -% functions in them. In this case we only call the first function of the -% list. +% This function calls a callback. It can only call a callback created by +% the \texttt{create} function. % % \begin{macrocode} -local function firsthandler (name) - return function(...) - local l = callbacklist[name] - if l then - local f = l[1].func - return f(...) +function call(name, ...) + if not name then + err("unable to call callback, no proper name passed", name) + return nil + end + if not lua_callbacks_defaults[name] then + err("unable to call lua callback '%s', unknown callback", name) + return nil + end + local l = callbacklist[name] + local f + 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 - return nil, false + err("unknown callback type") end end + return f(...) end % \end{macrocode} % -% Handler for simple functions that don't return anything. +% This function tells if a function has already been registered in a +% callback, and gives its current priority. The arguments are the name of +% the callback and the description of the function. If it has already been +% registered, it gives its priority, and if not it returns false. % % \begin{macrocode} -local function simplehandler (name) - return function(...) - local l = callbacklist[name] - if l then - for _, f in ipairs(l) do - f.func(...) - end +function get_priority (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 % \end{macrocode} % -- cgit v1.2.3