From f173f5c12fe3f8741451ae643ab56320c112fc52 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Tue, 7 May 2013 18:05:45 +0200 Subject: make user whatsit generators accept an optional argument of type node --- luatexbase-attr.dtx | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/luatexbase-attr.dtx b/luatexbase-attr.dtx index 499677d..3e01827 100644 --- a/luatexbase-attr.dtx +++ b/luatexbase-attr.dtx @@ -529,10 +529,24 @@ local new_user_whatsit = function (name, package) local whatsit = newnode(whatsit_t, user_defined_t) whatsit.user_id = id --- unit -> node_t - return function ( ) return copynode(whatsit) end, id + local generator = function (proto) + local res + if proto then + local proto_id = proto.user_id + if proto_id ~= id then --- mismatch + warning("overriding user_id %d of whatsit prototype (now: %d)", + proto.user_id, id) + end + res = copynode(proto) + res.user_id = id + else + res = copynode(whatsit) + end + return res + end + return generator, id end luatexbase.new_user_whatsit = new_user_whatsit -luatexbase.new_user_whatsit_factory = new_user_whatsit --- for Stephan % \end{macrocode} % % If one knows the name of a user whatsit, its corresponding id -- cgit v1.2.3 From a9178223ec8b11a5de7937e3bd5f4492eb6ceea6 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Fri, 17 May 2013 17:37:18 +0200 Subject: have ``new_user_whatsit()`` accept prototype as first argument --- luatexbase-attr.dtx | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/luatexbase-attr.dtx b/luatexbase-attr.dtx index 3e01827..f10e2de 100644 --- a/luatexbase-attr.dtx +++ b/luatexbase-attr.dtx @@ -262,7 +262,7 @@ See the aforementioned source file(s) for copyright and licensing information. % % \subsubsection{Primitives needed} % -% First load \pk{luatexbase-modutils} (hence \pk{luatexbase-loader} +% First load \pk{luatexbase-modutils} (hence \pk{luatexbase-loader} % and \pk{luatexbase-compat}), and make sure \pk{luatex.sty} is loaded too. % % \begin{macrocode} @@ -523,28 +523,22 @@ luatexbase.new_user_whatsit_id = new_user_whatsit_id % We return a nullary function that delivers copies of the whatsit. % % \begin{macrocode} ---- string -> string -> (unit -> node_t, int) -local new_user_whatsit = function (name, package) - local id = new_user_whatsit_id(name, package) - local whatsit = newnode(whatsit_t, user_defined_t) - whatsit.user_id = id - --- unit -> node_t - local generator = function (proto) - local res - if proto then - local proto_id = proto.user_id - if proto_id ~= id then --- mismatch - warning("overriding user_id %d of whatsit prototype (now: %d)", - proto.user_id, id) - end - res = copynode(proto) - res.user_id = id - else - res = copynode(whatsit) +--- (string | node_t) -> string -> ((unit -> node_t) * int) +local new_user_whatsit = function (req, package) + local id, whatsit + if type(req) == "string" then + id = new_user_whatsit_id(req, package) + whatsit = newnode(whatsit_t, user_defined_t) + whatsit.user_id = id + else -- node + id = req.user_id + whatsit = copynode(req) + if not whatsit_ids[id] then + warning("whatsit id %d unregistered; " + .. "inconsistencies may arise", id) end - return res end - return generator, id + return function () return copynode(whatsit) end, id end luatexbase.new_user_whatsit = new_user_whatsit % \end{macrocode} @@ -632,7 +626,7 @@ local dump_registered_whatsits = function (asked_package) --- mess up line breaking, so concatenation is unusable ... local first = true for i=1, #whatsit_list do - if first then + if first then first = false else -- indent texiowrite_nl" " @@ -647,7 +641,6 @@ luatexbase.dump_registered_whatsits = dump_registered_whatsits % \begin{macrocode} luatexbase.newattribute = new_attribute luatexbase.newuserwhatsit = new_user_whatsit -luatexbase.newuserwhatsitfactory = new_user_whatsit_factory luatexbase.newuserwhatsitid = new_user_whatsit_id luatexbase.getuserwhatsitid = get_user_whatsit_id luatexbase.getuserwhatsitname = get_user_whatsit_name -- cgit v1.2.3 From e824f2826911dc90e022c522b8ad6531447c5219 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Fri, 17 May 2013 17:49:08 +0200 Subject: document alternate behavior of ``new_user_whatsit()`` --- luatexbase-attr.dtx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/luatexbase-attr.dtx b/luatexbase-attr.dtx index f10e2de..638cd31 100644 --- a/luatexbase-attr.dtx +++ b/luatexbase-attr.dtx @@ -522,6 +522,11 @@ luatexbase.new_user_whatsit_id = new_user_whatsit_id % creates the corresponding whatsit node of subtype “user-defined”. % We return a nullary function that delivers copies of the whatsit. % +% Alternatively, the first argument can be a whatsit node that +% will then be used as prototype. Note that in this case a +% \emph{copy} of the prototype will be stored in the closure, +% eliminating side-effects. +% % \begin{macrocode} --- (string | node_t) -> string -> ((unit -> node_t) * int) local new_user_whatsit = function (req, package) -- cgit v1.2.3 From 4804f72d73d764f3320742348273bd2b98d7f306 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Fri, 17 May 2013 17:55:38 +0200 Subject: reintroduce checks for prototype validity --- luatexbase-attr.dtx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/luatexbase-attr.dtx b/luatexbase-attr.dtx index 638cd31..5b7288f 100644 --- a/luatexbase-attr.dtx +++ b/luatexbase-attr.dtx @@ -535,7 +535,7 @@ local new_user_whatsit = function (req, package) id = new_user_whatsit_id(req, package) whatsit = newnode(whatsit_t, user_defined_t) whatsit.user_id = id - else -- node + elseif req.id == whatsit_t and req.subtype == user_defined_t then id = req.user_id whatsit = copynode(req) if not whatsit_ids[id] then -- cgit v1.2.3