\def\OutputMode{cpp}
\input keymap-utils.tex
\input keymap-abstract.tex
\input keymap-keyboard.tex

\def\nullkey{\addoutput{k::null}}
\def\UndefKey{\addoutput{k::undef}}
\def\Cmd#1{\beginStrEsc\addoutput{k::cmd(\SimQuotes#1\SimQuotes)}\endEsc}
\def\GoMode#1{\addoutput{k::goMode(\SimQuotes#1\SimQuotes)}}
\input keymap-map.tex

\def\writeOnEnd{}

\long\def\addoutput#1{\immediate\write\file{#1}}
\long\def\addoutputOnEnd#1{\long\xdef\writeOnEnd{\writeOnEnd#1}}

\def\PrintKeyboard#1{
\getUsedMod
\for{iter}\in{\allmod}
{
	\setKeyMod{\iter}
	\letcs{tmp}{activeModM\keyMod}
	\if\tmp1%
		\addoutput{\{\{"\ModeName",0b\keyMod\},\{\{}
		\for i 0 \maph{
			\addoutput{\{}
			\for j 0 \mapw{
				\setKeyBase{\the\counti}{\the\countj}
				\csname keymapK\key\endcsname
				\addoutput{,}
			}
			\addoutput{\},}
		}
		\addoutput{\}\}\},}
	\fi
}
}

\input keymap-catcode-to-file.tex
\def\beginStrEsc{%
\def"{\SimBackslash\SimQuotes}%
\def\\{\SimBackslash\SimBackslash}%
}

\newwrite\file

\def\openOutputFile#1{
\immediate\openout\file = #1
}

\def\beginIn{
\addoutput{map<pair<string,int>,KeyMode> keyModes = \{}
}

\def\endIn{
\addoutput{\};}
\addoutput{\writeOnEnd}
\closeout\file
}
