From b05a48f1a220aa87d6ac208529d7975e092cec3a Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Mon, 6 May 2013 22:04:07 +0000 Subject: luatexbase (6may13) git-svn-id: svn://tug.org/texlive/trunk@30250 c570f23f-e606-0410-a88d-b1316a301751 --- .../texmf-dist/source/luatex/luatexbase/Makefile | 4 +- .../source/luatex/luatexbase/luatexbase-attr.dtx | 288 ++++++++++++++++++++- .../source/luatex/luatexbase/luatexbase-cctb.dtx | 12 +- .../source/luatex/luatexbase/luatexbase-loader.dtx | 18 +- .../source/luatex/luatexbase/luatexbase-mcb.dtx | 40 ++- .../luatex/luatexbase/luatexbase-modutils.dtx | 204 ++++++++++++--- .../source/luatex/luatexbase/luatexbase.dtx | 6 +- 7 files changed, 496 insertions(+), 76 deletions(-) (limited to 'Master/texmf-dist/source/luatex') diff --git a/Master/texmf-dist/source/luatex/luatexbase/Makefile b/Master/texmf-dist/source/luatex/luatexbase/Makefile index 6fdc7fb4693..c3b8e894aa1 100644 --- a/Master/texmf-dist/source/luatex/luatexbase/Makefile +++ b/Master/texmf-dist/source/luatex/luatexbase/Makefile @@ -33,7 +33,7 @@ UNPACKEDTL = $(UNPACKED_MCB) $(UNPACKED_REGS) $(UNPACKED_ATTR) $(UNPACKED_CCTB) $(UNPACKED_BASE) COMPILED = $(DOC) GENERATED = $(COMPILED) $(UNPACKED) -SOURCE = $(DTX) $(DTXSTY) README TODO Changes Makefile +SOURCE = $(DTX) $(DTXSTY) README TODO NEWS Makefile # used for check TEST_LOADER = test-loader @@ -42,7 +42,7 @@ TEST_MCB = test-mcb-aux.tex # Files grouped by installation location RUNFILES = $(filter-out test-%, $(UNPACKEDTL)) -DOCFILES = $(DOC) $(filter test-%, $(UNPACKEDTL)) README TODO Changes +DOCFILES = $(DOC) $(filter test-%, $(UNPACKEDTL)) README TODO NEWS SRCFILES = $(DTX) Makefile # The following definitions should be equivalent diff --git a/Master/texmf-dist/source/luatex/luatexbase/luatexbase-attr.dtx b/Master/texmf-dist/source/luatex/luatexbase/luatexbase-attr.dtx index 923ad973e64..aa5e987473d 100644 --- a/Master/texmf-dist/source/luatex/luatexbase/luatexbase-attr.dtx +++ b/Master/texmf-dist/source/luatex/luatexbase/luatexbase-attr.dtx @@ -116,7 +116,7 @@ See the aforementioned source file(s) for copyright and licensing information. % \begin{abstract} % In addition to the registers existing in \tex and \etex, \luatex introduces % a new concept: attributes. This package takes care of attribute allocation -% just like Plain TeX and LaTeX do for other registers, and also provides a +% just like Plain \tex and \latex do for other registers, and also provides a % Lua interface. % \end{abstract} % @@ -145,7 +145,7 @@ See the aforementioned source file(s) for copyright and licensing information. % number of the attribute associated to |\fooattr| assuming it was defined % using |\newluatexattribute\fooattr|, something that \luatex currently % doesn't support (you can get the current value of the associated attribute -% as |tex.atrribute.fooattr|, but not the attribute number). +% as |tex.attribute.fooattr|, but not the attribute number). % % There are several ways to work around this. For example, it is possible to % extract the number at any time from the |\meaning| of |\fooattr|. @@ -158,7 +158,7 @@ See the aforementioned source file(s) for copyright and licensing information. % % Also, two Lua functions are provided that are analogous to the above \tex % macros (actually, the macros are wrappers around the functions): -% |luatexbase.new_attributes|\parg{name} allocates a new attribute, without +% |luatexbase.new_attribute|\parg{name} allocates a new attribute, without % defining a corresponding \tex control sequence (only an entry in % |luatexbase.attributes| is created. It usually returns the number of the % allocated attribute. If room is missing, it raises an error, unless the @@ -283,21 +283,23 @@ See the aforementioned source file(s) for copyright and licensing information. % \subsubsection{Load supporting Lua module} % % First load \pk{luatexbase-loader} (hence \pk{luatexbase-compat}), then -% the supporting Lua module. +% the supporting Lua module. We make sure \verb|luatex.sty| is loaded. % % \begin{macrocode} \begingroup\expandafter\expandafter\expandafter\endgroup \expandafter\ifx\csname RequirePackage\endcsname\relax \input luatexbase-loader.sty + \input luatex.sty \else \RequirePackage{luatexbase-loader} + \RequirePackage{luatex} \fi \luatexbase@directlua{require('luatexbase.attr')} % \end{macrocode} % % \subsection{User macros} % -% The allocaton macro is merely a wrapper around the Lua function, but +% The allocation macro is merely a wrapper around the Lua function, but % handles error and logging in \tex, for consistency with other allocation % macros. % @@ -342,19 +344,81 @@ See the aforementioned source file(s) for copyright and licensing information. % % \begin{macrocode} %<*luamodule> -module('luatexbase', package.seeall) +--- locals +local copynode = node.copy +local newnode = node.new +local nodesubtype = node.subtype +local nodetype = node.id +local stringfind = string.find +local stringformat = string.format +local tableunpack = unpack or table.unpack +local texiowrite_nl = texio.write_nl +local texiowrite = texio.write +--- luatex internal types +local whatsit_t = nodetype"whatsit" +local user_defined_t = nodesubtype"user_defined" +local unassociated = "__unassociated" +luatexbase = luatexbase or { } +local luatexbase = luatexbase +% \end{macrocode} +% +% We improvise a basic logging facility. +% +% \begin{macrocode} +local reporter = function (log, category, ...) + if log == true then + texiowrite_nl("log", "("..category..") ") + texiowrite("log", stringformat(...)) + else + texiowrite_nl("("..category..") ") + texiowrite(stringformat(...)) + end +end +local warning = function (...) reporter (false, "warning", ...) end +----- info = function (...) reporter (false, "info", ...) end +local log = function (...) reporter (true, "log", ...) end % \end{macrocode} % % This table holds the values of the allocated attributes, indexed by name. % % \begin{macrocode} -attributes = {} +luatexbase.attributes = luatexbase.attributes or { } +local attributes = luatexbase.attributes +% \end{macrocode} +% +% Scoping: we use locals for the attribute functions. +% +% \begin{macrocode} +local new_attribute +local unset_attribute % \end{macrocode} % -% The allocaton function. Unlike other registers, allocate starting from 1. -% Some code (eg, font handling coming from Con\tex{}t) behaves strangely -% with \verb+\attribute0+ and since there is plenty of room here, it -% doesn't seem bad to ``loose'' one item in order to avoid this problem. +% In the \luatex ecosystem there are currently two functions that create a +% new attribute. +% One is in |oberdiek| bundle, the other is this one. We will hack a little +% in order to make them compatible. The other function uses +% |LuT@AllocAttribute| as attribute counter, we will keep it in sync with +% ours. A possible problem might also appear: the other function starts +% attribute allocation at 0, which will break luaotfload. We output an +% error if a new attribute has already been allocated with number 0. +% +% \begin{macrocode} +local luatex_sty_counter = 'LuT@AllocAttribute' +if tex.count[luatex_sty_counter] then + if tex.count[luatex_sty_counter] > -1 then + error("luatexbase error: attribute 0 has already been set by \newattribute" + .."macro from luatex.sty, not belonging to this package, this makes" + .."luaotfload unusable. Please report to the maintainer of luatex.sty") + else + tex.count[luatex_sty_counter] = 0 + end +end +% \end{macrocode} +% +% The allocation function. Unlike other registers, allocate starting from 1. +% Some code (e.~g., font handling coming from Con\tex{}t) behaves strangely +% with \verb+\attribute0+ set, and since there is plenty of room here, it +% doesn't seem bad to ``lose'' one item in order to avoid this problem. % % \begin{macrocode} local last_alloc = 0 @@ -366,15 +430,22 @@ function new_attribute(name, silent) error("No room for a new \\attribute", 1) end end + local lsc = tex.count[luatex_sty_counter] + if lsc and lsc > last_alloc then + last_alloc = lsc + end last_alloc = last_alloc + 1 + if lsc then + tex.setcount('global', luatex_sty_counter, last_alloc) + end attributes[name] = last_alloc unset_attribute(name) if not silent then - texio.write_nl('log', string.format( - 'luatexbase.attributes[%q] = %d', name, last_alloc)) + log('luatexbase.attributes[%q] = %d', name, last_alloc) end return last_alloc end +luatexbase.new_attribute = new_attribute % \end{macrocode} % % Unset an attribute the correct way depending on \luatex's version. @@ -384,6 +455,197 @@ local unset_value = (luatexbase.luatexversion < 37) and -1 or -2147483647 function unset_attribute(name) tex.setattribute(attributes[name], unset_value) end +luatexbase.unset_attribute = unset_attribute +% \end{macrocode} +% +% User whatsit allocation (experimental). +% +% \begin{macrocode} +--- cf. luatexref-t.pdf, sect. 8.1.4.25 +local user_whatsits = { --- (package, (name, id hash)) hash + __unassociated = { }, --- those without package name +} +local whatsit_ids = { } --- (id, (name * package)) hash +local whatsit_cap = 2^53 --- Lua numbers are doubles +local current_whatsit = 0 +local anonymous_whatsits = 0 +local anonymous_prefix = "anon" +% \end{macrocode} +% +% The whatsit allocation is split into two functions: +% \verb|new_user_whatsit_id| registers a new id (an integer) +% and returns it. It is up to the user what he actually does +% with the return value. +% +% Registering whatsits without a name, though supported, is +% not exactly good style. In these cases we generate a name +% from a counter. +% +% In addition to the whatsit name, it is possible and even +% encouraged to specify the name of the package that will be +% using the whatsit as the second argument. +% +% \begin{macrocode} +--- string -> string -> int +local new_user_whatsit_id = function (name, package) + if name then + if not package then + package = unassociated + end + else -- anonymous + anonymous_whatsits = anonymous_whatsits + 1 + warning("defining anonymous user whatsit no. %d", anonymous_whatsits) + warning("dear package authors, please name your whatsits!") + package = unassociated + name = anonymous_prefix .. tostring(anonymous_whatsits) + end + + local whatsitdata = user_whatsits[package] + if not whatsitdata then + whatsitdata = { } + user_whatsits[package] = whatsitdata + end + + local id = whatsitdata[name] + if id then --- warning + warning("replacing whatsit %s:%s (%d)", package, name, id) + else --- new id + current_whatsit = current_whatsit + 1 + if current_whatsit >= whatsit_cap then + warning("maximum of %d integral user whatsit ids reached", + whatsit_cap) + warning("further whatsit allocation may be inconsistent") + end + id = current_whatsit + whatsitdata[name] = id + whatsit_ids[id] = { name, package } + end + log("new user-defined whatsit %d (%s:%s)", id, package, name) + return id +end +luatexbase.new_user_whatsit_id = new_user_whatsit_id +% \end{macrocode} +% +% \verb|new_user_whatsit| first registers a new id and then also +% creates the corresponding whatsit of subtype “user defined”. +% 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 + return function ( ) return copynode(whatsit) end, 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 whatsit, its corresponding id +% can be retrieved by means of \verb|get_user_whatsit_id|. +% +% \begin{macrocode} +--- string -> string -> int +local get_user_whatsit_id = function (name, package) + if not package then + package = unassociated + end + return user_whatsits[package][name] +end +luatexbase.get_user_whatsit_id = get_user_whatsit_id +% \end{macrocode} +% +% The inverse lookup is also possible via \verb|get_user_whatsit_name|. +% Here it finally becomes obvious why it is beneficial to supply a package +% name -- it adds information about who created and might be relying on the +% whatsit in question. First return value is the whatsit name, the second +% the package identifier it was registered with. +% +% We issue a warning and return empty strings in case the asked whatsit is +% unregistered. +% +% \begin{macrocode} +--- int | fun | node -> (string, string) +local get_user_whatsit_name = function (asked) + local id + if type(asked) == "number" then + id = asked + elseif type(asked) == "function" then + --- node generator + local n = asked() + id = n.user_id + else --- node + id = asked.user_id + end + local metadata = whatsit_ids[id] + if not metadata then -- unknown + warning("whatsit id %d unregistered; inconsistencies may arise", id) + return "", "" + end + return tableunpack(metadata) +end +luatexbase.get_user_whatsit_name = get_user_whatsit_name +% \end{macrocode} +% +% For the curious as well as the cautious who are interesting in +% what they are dealing with, we add a function that outputs the +% current allocation status to the terminal. +% +% \begin{macrocode} +--- string -> unit +local dump_registered_whatsits = function (asked_package) + local whatsit_list = { } + if asked_package then + local whatsitdata = user_whatsits[asked_package] + if not whatsitdata then + error("(no user whatsits registered for package %s)", + asked_package) + return + end + texiowrite_nl("(user whatsit allocation stats for " .. asked_package) + for name, id in next, whatsitdata do + whatsit_list[#whatsit_list+1] = + stringformat("(%s:%s %d)", asked_package, name, id) + end + else + texiowrite_nl("(user whatsit allocation stats") + texiowrite_nl(stringformat(" ((total %d)\n (anonymous %d))", + current_whatsit, anonymous_whatsits)) + for package, whatsitdata in next, user_whatsits do + for name, id in next, whatsitdata do + whatsit_list[#whatsit_list+1] = + stringformat("(%s:%s %d)", package, name, id) + end + end + end + + texiowrite_nl" (" + --- in an attempt to be clever the texio.write* functions + --- mess up line breaking, so concatenation is unusable ... + local first = true + for i=1, #whatsit_list do + if first then + first = false + else -- indent + texiowrite_nl" " + end + texiowrite(whatsit_list[i]) + end + texiowrite"))\n" +end +luatexbase.dump_registered_whatsits = dump_registered_whatsits +% \end{macrocode} +% Lastly, we define a couple synonyms for convenience. +% \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 +luatexbase.dumpregisteredwhatsits = dump_registered_whatsits % \end{macrocode} % % \begin{macrocode} diff --git a/Master/texmf-dist/source/luatex/luatexbase/luatexbase-cctb.dtx b/Master/texmf-dist/source/luatex/luatexbase/luatexbase-cctb.dtx index f869c5bfa3d..1e95379223c 100644 --- a/Master/texmf-dist/source/luatex/luatexbase/luatexbase-cctb.dtx +++ b/Master/texmf-dist/source/luatex/luatexbase/luatexbase-cctb.dtx @@ -649,17 +649,20 @@ See the aforementioned source file(s) for copyright and licensing information. % % \begin{macrocode} %<*luamodule> -module('luatexbase', package.seeall) +luatexbase = luatexbase or { } +local luatexbase = luatexbase % \end{macrocode} % % The number associated to a CS name is remembered in the |catcodetables| % table. % % \begin{macrocode} -catcodetables = {} -function catcodetabledef_from_tex(name, number) +luatexbase.catcodetables = luatexbase.catcodetables or { } +local catcodetables = luatexbase.catcodetables +local function catcodetabledef_from_tex(name, number) catcodetables[name] = tonumber(number) end +luatexbase.catcodetabledef_from_tex = catcodetabledef_from_tex % \end{macrocode} % % The next function creates some shortcuts for better readability in lua @@ -667,7 +670,7 @@ end % |luatexbase.catcodetables.CatcodeTableLaTeX|. % % \begin{macrocode} -function catcodetable_do_shortcuts() +local function catcodetable_do_shortcuts() local cat = catcodetables cat['latex'] = cat.CatcodeTableLaTeX cat['latex-package'] = cat.CatcodeTableLaTeXAtLetter @@ -678,6 +681,7 @@ function catcodetable_do_shortcuts() cat['string'] = cat.CatcodeTableString cat['other'] = cat.CatcodeTableOther end +luatexbase.catcodetable_do_shortcuts = catcodetable_do_shortcuts % \end{macrocode} % % \begin{macrocode} diff --git a/Master/texmf-dist/source/luatex/luatexbase/luatexbase-loader.dtx b/Master/texmf-dist/source/luatex/luatexbase/luatexbase-loader.dtx index 77b9b9b7773..11918a3e884 100644 --- a/Master/texmf-dist/source/luatex/luatexbase/luatexbase-loader.dtx +++ b/Master/texmf-dist/source/luatex/luatexbase/luatexbase-loader.dtx @@ -274,7 +274,8 @@ See the aforementioned source file(s) for copyright and licensing information. % % \begin{macrocode} %<*luamodule> -module('luatexbase', package.seeall) +luatexbase = luatexbase or { } +local luatexbase = luatexbase % \end{macrocode} % % Just in case it's called from a \TeX Lua script... @@ -283,6 +284,17 @@ module('luatexbase', package.seeall) kpse.set_program_name("luatex") % \end{macrocode} % +% In \LaTeX, it's traditional to print the included file paths. We don't want +% to do this for scripts using texlua... Currently there is no perfect check +% of texlua vs. luatex, so we check for the token table. +% +% \begin{macrocode} +local print_included_path = false +if token then + print_included_path = true +end +% \end{macrocode} +% % Emulate (approximatively) kpse's lua format search. More precisely, % combine the search path of |texmfscripts| and |tex| in order to % approximate |LUAINPUTS|. But we need to handle suffixes ourselves. @@ -357,7 +369,9 @@ local function load_module(mod) if not loader then return "\n\t[luatexbase.loader] Loading error:\n\t"..error end - texio.write_nl("("..file..")") + if print_included_path then + texio.write_nl("("..file..")") + end return loader end % \end{macrocode} diff --git a/Master/texmf-dist/source/luatex/luatexbase/luatexbase-mcb.dtx b/Master/texmf-dist/source/luatex/luatexbase/luatexbase-mcb.dtx index 8b99a5257c7..0c04d4d346c 100644 --- a/Master/texmf-dist/source/luatex/luatexbase/luatexbase-mcb.dtx +++ b/Master/texmf-dist/source/luatex/luatexbase/luatexbase-mcb.dtx @@ -142,7 +142,7 @@ See the aforementioned source file(s) for copyright and licensing information. % % \luatex provides an extremely interesting feature, named callbacks. It % allows to call some Lua functions at some points of the \TeX\ algorithm (a -% \emph{callback}), like when \TeX\ breaks likes, puts vertical spaces, etc. +% \emph{callback}), like when \TeX\ breaks lines, puts vertical spaces, etc. % The \luatex core offers a function called \texttt{callback.register} that % enables to register a function in a callback. % @@ -157,7 +157,7 @@ See the aforementioned source file(s) for copyright and licensing information. % \begin{description} % \item[simple] is for functions that don't return anything: they are called % in order, all with the same argument; -% \item[data] is for functions receiving a piece of data of nay type +% \item[data] is for functions receiving a piece of data of any type % except node list head (and possibly other arguments) and returning it % (possibly modified): the functions are called in order, and each is % passed the return value of the previous (and the other arguments @@ -167,8 +167,8 @@ See the aforementioned source file(s) for copyright and licensing information. % modified node list, or the boolean values |true| or |false|. The % functions are chained the same way as for \emph{data} except that for % the following. If -% one function returns |false|, then |false| is immediately return and the -% following functions are \emph{not} called. If one function returns +% one function returns |false|, then |false| is immediately returned and +% the following functions are \emph{not} called. If one function returns % |true|, then the same head is passed to the next function. If all % functions return |true|, then |true| is returned, otherwise the return % value of the last function not returning |true| is used. @@ -392,8 +392,9 @@ See the aforementioned source file(s) for copyright and licensing information. % \subsubsection{Module identification} % % \begin{macrocode} -module('luatexbase', package.seeall) -local err, warning, info = luatexbase.provides_module({ +luatexbase = luatexbase or { } +local luatexbase = luatexbase +local err, warning, info, log = luatexbase.provides_module({ name = "luatexbase-mcb", version = 0.5, date = "2013/04/13", @@ -404,6 +405,17 @@ local err, warning, info = luatexbase.provides_module({ }) % \end{macrocode} % +% First we declare the function references for the entire scope. +% +% \begin{macrocode} +local add_to_callback +local call_callback +local create_callback +local priority_in_callback +local remove_from_callback +local reset_callback +% \end{macrocode} +% % \subsubsection{Housekeeping} % % The main table: keys are callback names, and values are the associated @@ -687,9 +699,10 @@ function add_to_callback (name,func,description,priority) warning("several functions in '%s',\n" .."only one will be active.", name) end - info("inserting '%s'\nat position %s in '%s'", + log("inserting '%s'\nat position %s in '%s'", description, priority, name) end +luatexbase.add_to_callback = add_to_callback % \end{macrocode} % % Remove a function from a callback. First check arguments. @@ -733,7 +746,7 @@ function remove_from_callback (name, description) return end table.remove(l, index) - info("removing '%s'\nfrom '%s'", description, name) + log("removing '%s'\nfrom '%s'", description, name) if #l == 0 then callbacklist[name] = nil if not lua_callbacks_defaults[name] then @@ -742,6 +755,7 @@ function remove_from_callback (name, description) end return end +luatexbase.remove_from_callback = remove_from_callback % \end{macrocode} % % Remove all the functions registered in a callback. Unregisters the @@ -756,17 +770,18 @@ function reset_callback (name, make_false) err("unable to reset '%s':\nis not a valid callback", name) return end - info("resetting callback '%s'", name) + log("resetting callback '%s'", name) callbacklist[name] = nil if not lua_callbacks_defaults[name] then if make_false == true then - info("setting '%s' to false", name) + log("setting '%s' to false", name) register_callback(name, false) else register_callback(name, nil) end end end +luatexbase.reset_callback = reset_callback % \end{macrocode} % % Get a function's priority in a callback list, or false if the function is @@ -788,6 +803,7 @@ function priority_in_callback (name, description) end return false end +luatexbase.priority_in_callback = priority_in_callback % \end{macrocode} % % \subsubsection{Public functions for user-defined callbacks} @@ -823,10 +839,11 @@ function create_callback(name, ctype, default) err("unable to create callback '%s':\ntype '%s' undefined", name, ctype) return nil end - info("creating '%s' type %s", name, ctype) + log("creating '%s' type %s", name, ctype) lua_callbacks_defaults[name] = default callbacktypes[name] = ctype end +luatexbase.create_callback = create_callback % \end{macrocode} % % This function calls a callback. It can only call a callback created by @@ -855,6 +872,7 @@ function call_callback(name, ...) end return f(...) end +luatexbase.call_callback = call_callback % \end{macrocode} % % That's all folks! diff --git a/Master/texmf-dist/source/luatex/luatexbase/luatexbase-modutils.dtx b/Master/texmf-dist/source/luatex/luatexbase/luatexbase-modutils.dtx index 6f500da0acd..cd1458a6aa6 100644 --- a/Master/texmf-dist/source/luatex/luatexbase/luatexbase-modutils.dtx +++ b/Master/texmf-dist/source/luatex/luatexbase/luatexbase-modutils.dtx @@ -111,7 +111,7 @@ See the aforementioned source file(s) for copyright and licensing information. % Grave accent \` Left brace \{ Vertical bar \| % Right brace \} Tilde \~} % -% \pkdate{luatexbase-modutils}{v0.5 2013-04-13} +% \pkdate{luatexbase-modutils}{v0.6 2013-05-04} % % \maketitle % @@ -144,17 +144,10 @@ See the aforementioned source file(s) for copyright and licensing information. % % \medskip % -% It is important to notice that Lua's standard function |module()| is -% completely orthogonal with the present package. It has nothing to do with -% identification and deals only with namespaces: more precisely, it -% modifies the current environment. So, you should continue to -% use it normally regardless of whether you chose to use this package's -% features for identification. -% -% It is recommended to always use |module()| or any other method that ensure -% the global name space remains clean. For example, you may heavily use the -% |local| keyword and explicitly qualify the name of every non-local symbol. -% Chapter 15 of \href{http://www.lua.org/pil/}{Programming in Lua, 1st ed.} +% It is important to note that Lua's unction |module()| is deprecated in +% Lua 5.2 and should be avoided. For examples of good practices for creating +% modules, see section~\ref{template}. Chapter 15 +% of \href{http://www.lua.org/pil/15.html}{Programming in Lua, 3rd ed.} % discusses various methods for managing packages. % % \subsection{\tex macros} @@ -229,30 +222,35 @@ See the aforementioned source file(s) for copyright and licensing information. % standard Lua function, so you may want to avoid overwriting it, hence the % use of |err| in the above example.) % -% \subsection{Templates} +% \begin{qcode} +% local module_info = luatexbase.get_module_info(\meta{name}) +% local version = luatexbase.get_module_version(\meta{name}) +% local date = luatexbase.get_module_date(\meta{name}) +% local date_int = luatexbase.get_module_date_int(\meta{name}) +% local is_loaded = luatexbase.is_module_loaded(\meta{name}) +% \end{qcode} +% These functions check for the availability or version of a module, and can +% even return a copy of the |module_info| table. % -% Let me emphasize again that, while |luatexbase.require_module()| is meant to -% be used as a replacement for |require()|, the function -% |luatexbase.provides_module()| \emph{is not} a replacement for |module()|: -% they just don't do the same thing (declaring information vs changing the -% current name space). +% \subsection{Templates}\label{template} % -% Now, here is how you module may begin: +% Now, here is a module header template showing all the recommended elements: % \begin{verbatim} % local err, warn, info, log = luatexbase.provides_module({ % -- required -% name = 'mymodule', +% name = 'mymodule', % -- recommended -% date = '1970/01/01', -% version = 0.0, -- or version = '0.0a', -% description = 'a Lua module template', +% date = '1970/01/01', +% version = 0.0, -- or version = '0.0a', +% description = 'a Lua module template', % -- optional and ignored -% author = 'A. U. Thor', -% licence = 'LPPL v1.3+', +% author = 'A. U. Thor', +% licence = 'LPPL v1.3+', % }) % -% module('mynamespace', package.seeall) -% -- or any other method (see chapter 15 of PIL for examples) +% mynamespace = mynamespace or { } +% local mynamespace = mynamespace +% % \end{verbatim} % % Alternatively, if you don't want to assume \pk{luatexbase-modutils} is @@ -276,8 +274,8 @@ See the aforementioned source file(s) for copyright and licensing information. % }) % end % -% module('mynamespace', package.seeall) -% -- or any other method (see chapter 15 of PIL for examples) +% mynamespace = mynamespace or { } +% local mynamespace = mynamespace % % local function err(msg) % -- etc. @@ -457,7 +455,9 @@ See the aforementioned source file(s) for copyright and licensing information. % % \begin{macrocode} %<*luamodule> -module("luatexbase", package.seeall) +luatexbase = luatexbase or { } +local luatexbase = luatexbase +local string_gsub = string.gsub % \end{macrocode} % % \subsection{Internal functions and data} @@ -474,7 +474,8 @@ local modules = modules or {} % % \begin{macrocode} local function date_to_int(date) - local numbers = string.gsub(date, "(%d+)/(%d+)/(%d+)", "%1%2%3") + if date == '' then return -1 end + local numbers = string_gsub(date, "(%d+)/(%d+)/(%d+)", "%1%2%3") return tonumber(numbers) end % \end{macrocode} @@ -495,44 +496,49 @@ end local function module_error_int(mod, ...) error(msg_format('error', mod, ...), 3) end -function module_error(mod, ...) +local function module_error(mod, ...) module_error_int(mod, ...) end +luatexbase.module_error = module_error % \end{macrocode} % % Split the lines explicitly in order not to depend on the value of % |\newlinechar|. % % \begin{macrocode} -function module_warning(mod, ...) +local function module_warning(mod, ...) for _, line in ipairs(msg_format('warning', mod, ...):explode('\n')) do texio.write_nl(line) end end -function module_info(mod, ...) +luatexbase.module_warning = module_warning +local function module_info(mod, ...) for _, line in ipairs(msg_format('info', mod, ...):explode('\n')) do texio.write_nl(line) end end +luatexbase.module_info = module_info % \end{macrocode} % % No line splitting or advanced formating here. % % \begin{macrocode} -function module_log(mod, msg, ...) +local function module_log(mod, msg, ...) texio.write_nl('log', mod..': '..msg:format(...)) end +luatexbase.module_log = module_log % \end{macrocode} % % Produce custom versions of the reporting functions. % % \begin{macrocode} -function errwarinf(name) +local function errwarinf(name) return function(...) module_error_int(name, ...) end, function(...) module_warning(name, ...) end, function(...) module_info(name, ...) end, function(...) module_log(name, ...) end end +luatexbase.errwarinf = errwarinf % \end{macrocode} % % For our own convenience, local functions for warning and errors in the @@ -547,19 +553,19 @@ local err, warn = errwarinf('luatexbase.modutils') % Load a module with mandatory name checking and optional version checking. % % \begin{macrocode} -function require_module(name, req_date) +local function require_module(name, req_date) require(name) local info = modules[name] if not info then warn("module '%s' was not properly identified", name) - elseif req_date and info.version then - if not (info.date and date_to_int(info.date) > date_to_int(req_date)) - then + elseif req_date and info.date then + if date_to_int(info.date) < date_to_int(req_date) then warn("module '%s' required in version '%s'\n" .. "but found in version '%s'", name, req_date, info.date) end end end +luatexbase.require_module = require_module % \end{macrocode} % % Provide identification information for a module. As a bonus, custom @@ -567,7 +573,7 @@ end % everything done in |require_module()|. % % \begin{macrocode} -function provides_module(info) +local function provides_module(info) if not (info and info.name) then err('provides_module: missing information') end @@ -576,6 +582,107 @@ function provides_module(info) modules[info.name] = info return errwarinf(info.name) end +luatexbase.provides_module = provides_module +% \end{macrocode} +% +% \subsubsection{module availability and version checking} +% +% A simple table copy function. +% +% \begin{macrocode} +local fastcopy +fastcopy = table.fastcopy or function(old) + if old then + local new = { } + for k,v in next, old do + if type(v) == "table" then + new[k] = fastcopy(v) + else + new[k] = v + end + end + local mt = getmetatable(old) + if mt then + setmetatable(new,mt) + end + return new + else + return { } + end +end +% \end{macrocode} +% +% Gives the table of the infos on a module, as given in |provides_module|. +% +% \begin{macrocode} +local function get_module_info(name) + local module_table = modules[name] + if not module_table then + return nil + else + return fastcopy(module_table) + end +end +luatexbase.get_module_info = get_module_info +% \end{macrocode} +% +% Gives the version of a module, nil if the module is not loaded and empty +% string if the module did not set its date. +% +% \begin{macrocode} +function get_module_version(name) + local module_table = modules[name] + if not module_table then + return nil + else + return module_table.version + end +end +luatexbase.get_module_version = get_module_version +% \end{macrocode} +% +% Gives the date string of a module, nil if the module is not loaded and +% empty string of the modules did not set its date. +% +% \begin{macrocode} +function get_module_date(name) + local module_table = modules[name] + if not module_table then + return nil + else + return module_table.date + end +end +luatexbase.get_module_date = get_module_date +% \end{macrocode} +% +% Gives the date number of a module, for date comparison, nil if the +% module is not loaded and -1 if the module did not set its date. The number +% is formated as |yyyymmdd|. +% +% \begin{macrocode} +function get_module_date_int(name) + local module_table = modules[name] + if not module_table then + return nil + else + return module_table.date and date_to_int(module_table.date) + end +end +luatexbase.get_module_date_int = get_module_date_int +% \end{macrocode} +% +% Returns true if the module is loaded, false otherwise. +% +% \begin{macrocode} +function is_module_loaded(name) + if modules[name] then + return true + else + return false + end +end +luatexbase.is_module_loaded = is_module_loaded % \end{macrocode} % % \begin{macrocode} @@ -594,8 +701,23 @@ local err, warn, info, log = luatexbase.provides_module { version = 1, description = 'dummy test package', } +luatexbase.provides_module { + name = 'test-modutils2', + date = '', + version = 1, + description = 'dummy test package', +} info('It works!\nOh, rly?\nYeah rly!') log("I'm a one-line info.") +info("1 = "..luatexbase.get_module_version('test-modutils')) +if is_module_loaded('test-modutils') then + info("ok!") +else + err("problem!") +end +info("2000/01/01 = "..luatexbase.get_module_info('test-modutils').date) +info("20000101 = "..luatexbase.get_module_date_int('test-modutils')) +info("-1 = "..luatexbase.get_module_date_int('test-modutils2')) % % \end{macrocode} % diff --git a/Master/texmf-dist/source/luatex/luatexbase/luatexbase.dtx b/Master/texmf-dist/source/luatex/luatexbase/luatexbase.dtx index 49b0c07749c..2849818adbe 100644 --- a/Master/texmf-dist/source/luatex/luatexbase/luatexbase.dtx +++ b/Master/texmf-dist/source/luatex/luatexbase/luatexbase.dtx @@ -93,7 +93,7 @@ See the aforementioned source file(s) for copyright and licensing information. % Grave accent \` Left brace \{ Vertical bar \| % Right brace \} Tilde \~} % -% \pkdate{luatexbase}{v0.5 2013-04-13} +% \pkdate{luatexbase}{v0.6 2013-05-04} % % \maketitle % @@ -116,7 +116,7 @@ See the aforementioned source file(s) for copyright and licensing information. % \subpk{regs} extented allocation scheme for registers and the like, % \emph{\`a la} \pk{etex}. % \subpk{cctb} catcode table allocation. -% \subpk{attr} attribute allocation. +% \subpk{attr} attribute and whatsit node allocation. % \subpk{mcb} callbacks extensions allowing to register many functions in a % single callback, and declare new callbacks for packages. % \subpk{modutils} Lua module declaration, including version checks. @@ -214,7 +214,7 @@ See the aforementioned source file(s) for copyright and licensing information. \let\x\ProvidesPackage \fi \expandafter\endgroup -\x{luatexbase}[2013/04/13 v0.5 Module utilities for LuaTeX] +\x{luatexbase}[2013/05/04 v0.6 Module utilities for LuaTeX] % \end{macrocode} % % Make sure \luatex is used. -- cgit v1.2.3