ConTeXt:您如何创建自己的命令?

ConTeXt:您如何创建自己的命令?

在 ConTeXt 中似乎存在一种定义新(核心)命令的当前标准方法。例如,Wolfgang 的电子邮件状态:

现在,许多 \define... 和 \setup... 命令都是从 \installcommandhandler 自动生成的,它只生成一个设置命令,可用于全局(即 \setup…[..,..=..,..])和本地(即 \setup...[...][..,..=..,..])设置。为了向后兼容,您可以找到类似以下内容:\let\setupnotes\setupnote

我无法理解源代码,也找不到涉及的命令的任何文档。

\startnarrower例如,创建和的代码\setupnarrower来自空间-hor.mkiv

\installcorenamespace{narrower}
\installcorenamespace{narrowermethod}

% ... (some code skipped)
\installcommandhandler \??narrower {narrower} \??narrower

\appendtoks
   %% this means: \def\startmynarrower{\spac_narrower_start{mynarrower}}
   \setuevalue{\e!start\currentnarrower}{\spac_narrower_start%
     {\currentnarrower}}%
   %% this means: \def\startmynarrower{\spac_narrower_stop}
   \setuevalue{\e!stop \currentnarrower}{\spac_narrower_stop}%
\to \everydefinenarrower

% \??narrowermethod is the narrowermethod corenamespace?
\unexpanded\def\installnarrowermethod#1#2%
  {\setvalue{\??narrowermethod#1}{#2}}

% ...

\installnarrowermethod \v!left  {\global\advance\s_spac_narrower_left%
    \narrowerparameter\c!left  \relax}
\installnarrowermethod \v!middle{\global\advance\s_spac_narrower_mid%
    \narrowerparameter\c!middle\relax}

那么,这些命令的作用/含义是什么?

  • \installcorenamespace
  • \??...
  • \installcommandhandler,以及可能在多重辅助.mkiv其他地方: \installswitchcommandhandler,,,,等等。\installautocommandhandler\installsimplecommandhandler\installframedautocommandhandler
  • \setuevalue

答案1

创建命名空间

要创建命名空间,可以使用命令,\installnamespace与 相对 \installcorenamespace,后者在核心中为命令保留。它们的作用基本相同。\installnamespace{whatever}创建一个新的控制序列\????whatever,它会扩展为某个随机值,例如345>。命令\installcorenamespace创建一个只带有两个问号的命令。您永远不需要这个值,因为它只能通过控制序列访问。

创建命令处理程序

该命令\installcommandhandler的调用方式如下:

\installcommandhandler \????whatever {whatever} \????whatever

它创建了在 ConTeXts 用户界面中使用的几个命令:

\definewhatever [myA]
\setupwhatever  [key=value]
\setupwhatever  [myA] [key=value]

它还创建了许多其他命令处理程序,例如\everydefine…令牌列表。

还有一篇有趣的文章:更高级别的命令\definenamespace(见 模块命名空间):

访问值

\installcommandhandler \????whatever {whatever} \????whatever

\setupwhatever  [foo=globalbar]
\definewhatever [mywhatever]
\setupwhatever  [mywhatever] [key=value, foo=localbar]

可以通过以下方式访问值

\whateverparameter{foo}                  % ⇒ globalbar (*not* localbar!)
\namedwhateverparameter{mywhatever}{key} % ⇒ value
\namedwhateverparameter{mywhatever}{foo} % ⇒ globalbar (inherits from \setupwhatever)

\def\currentwhatever{mywhatever}

\whateverparameter{foo} % ⇒ localbar

改变价值观

可以\set…parameter{key}{value}更改或设置值。如果\current…为空,则更改全局键;如果\current…设置,则更改在本地应用。

\installnamespace{whatever}
\installcommandhandler \????whatever {whatever} \????whatever

\setupwhatever  [foo=globalbar]
\definewhatever [mywhatever]
\setupwhatever  [mywhatever] [key=value, foo=localbar]

\setwhateverparameter{foo}{newvalueglobal} % alters global foo
\whateverparameter{foo}

\def\currentwhatever{mywhatever}
\setwhateverparameter{foo}{newvaluelocal} % alters local foo

\whateverparameter{foo} % local foo changed

\let\currentwhatever\empty
\whateverparameter{foo} % global foo unchanged

公约

以下信息取自上下文 wiki - 系统宏

\s!: These are macros holding system constants, ie. values that never change
\c!: These are macros holding constant keys in key-value pairs.
     The actual definitions depend on the multi-lingual interface that is
     currently being used.
\v!: These are macros holding names of variable values in key-value pairs.
     The actual definitions depend on the multi-lingual interface that is
     currently being used.
\??: These are multi-lingual interface constant calls.
\@@: These are results of a multi-lingual interface constant expansion. 

\setuevalue定义如下

\def\setuevalue#1{\normalprotected\expandafter\edef\csname#1\endcsname}

因此它基本上充当了一个\edef

相关内容