# -*- CPERL -*-
package LaTeXML::Package::Pool;
use strict;
use LaTeXML::Package;

# return: unique ID for variable
sub cvar_id {
  my ($id) = @_;
  $id = ToString($id);
  if (!$id) {
    $id=LookupValue('cvar_id') || 0;
    AssignValue('cvar_id', $id + 1, 'global'); }
  "cvar.$id"; }#$
DefConstructor('\vname[]{}',
  "<ltx:XMWrap role='ID' xml:id='&cvar_id(#1)'>#2</ltx:XMWrap>",
  requireMath=>1);
# \vnref{<reference>}
DefMacro('\vnref{}','\@XMRef{cvar.#1}');

# variants for declaring variables
DefMacro('\uivar[]{}{}',    '\vname[#1]{{#2}^{#3}}');
DefMacro('\livar[]{}{}',    '\vname[#1]{{#2}_{#3}}');
DefMacro('\ulivar[]{}{}{}', '\vname[#1]{{#2}^{#3}_{#4}}');
DefMacro('\primvar[]{}',    '\vname[#1]{#2^\prime}');
DefMacro('\pprimvar[]{}',   '\vname[#1]{#2^{\prime\prime}}');

# \nappa{<function>}{<(const)(,\1)*>}
# @#1(#2)
DefConstructor('\nappa{}{}',
  "<ltx:XMApp>"
    ."<ltx:XMTok meaning='#1' />"
    ."<ltx:XMArg>#2</ltx:XMArg>"
  ."</ltx:XMApp>");

# \@napp@seq{<function>}{start <const>}{end <const>}
# @#1(@sequence(#2,sequencefromto,#3))
DefConstructor('\@napp@seq{}{}{}',
  "<ltx:XMApp>"
  ."<ltx:XMTok meaning='#1' />"
  ."<ltx:XMArg>"
    ."<ltx:XMApp>"
      ."<ltx:XMTok meaning='sequence' />"
      ."<ltx:XMArg>#2</ltx:XMArg>"
      ."<ltx:XMArg><ltx:XMTok meaning='sequencefromto' /></ltx:XMArg>"
      ."<ltx:XMArg>#3</ltx:XMArg>"
    ."</ltx:XMApp>"
  ."</ltx:XMArg>"
  ."</ltx:XMApp>");

DefMacro('\nappe{}{}{}',    '\@napp@seq{#1}{#2}{#3}');
DefMacro('\nappf{}{}{}{}',  '\@napp@seq{#1}{#2{#3}}{#2{#4}}');
DefMacro('\nappli{}{}{}{}', '\@napp@seq{#1}{#2_{#3}}{#2_{#4}}');
DefMacro('\nappui{}{}{}{}', '\@napp@seq{#1}{#2^{#3}}{#2^{#4}}');

1;
