ConTeXt and Lua programming/Module writing/Module namespaces

Unofficial ConTeXt Wiki mirror

Last modified: 2025-01-16

Naming Conventions

To avoid clashes with existing and future commands, any module should adhere to the following rules:

Generating a Namespace for Your Module

See also Wolfgang’s talk from context meeting 2024!

ConTeXt provides the user command \installnamespace that generates a namespace for macros and variables and enables simple setups for advanced techniques (defining instances, parameter setup & handling).

Suppose you have a module called My Module (preferably in a file t-mymodule.tex) and want to reserve the namespace MyMod:

\installnamespace{MyMod}
% defines \????MyMod, only accessible after …
\unprotect
% … up to
\protect

Module factory

Depending on your needs, you can then use one or several of the “factory” macros that define the usual \define… (instances), \setup… etc. commands and parameter handling for you:

\installcommandhandler\????MyMod

creates

Through \setupMyMod you can now set the variables that you want to use in your module — it is already a mature command that conforms to the overall ConTeXt style and can take a parameter list, a list of key-value pairs or both as arguments.

Any parameter defined that way can now be retrieved via \MyModparameter{…}.

\usemodule[MyMod]
% setting a parameter within the namespace
\setupMyMod[
  yamp=Yet another module parameter,
]

% command sequence that does something with the parameter
\def\blueparameter{\color[blue]{\MyModparameter{yamp}}}

% deploying the new user command
\starttext
\blueparameter
\stoptext

or within the module:

\getparameters[\????MyMod][width=10dk]

Framed commands

If you want a new \framed command which takes its values from \setupMyMod, use \installframedcommandhandler or single \installinheritedframed. To avoid clashes with other MyMod settings, the namespace and command name should be different from the default one.

\installinheritedframed \????MyModFrame {MyModFrame}

… then you get additionally:

Local values

In some cases you want to accept values as assignments which are local to the command or environment. This can be done with the predefined \getdummyparameters.

The values can be retrieved with \dummyparameter{...}

\def\MyModText[#1]{#2}%
{\begingroup
\getdummyparameters[blabla=,#1]%
\dummyparameter{blabla}: #2%
\endgroup

Background information

Internally the namespaces are represented as ordinary command sequences with a prefix of 4 question marks.

Outside of the \unprotect … \protect section, this is represented as something like \2DD> which can’t be used in normal documents and ensures that no user can modify the values.

The internal representation of \getparameters[\????MyMod][width=10dk] would be \def\2DD>width{10dk}.

Further Information