%D \module
%D   [       file=typo-spa,
%D        version=2009.03.27, % code moved from cors-spa.mkiv
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Spacing,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=\PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Typesetting Macros / Spacing}

\unprotect

\registerctxluafile{typo-spa}{1.001}

\definesystemattribute[spacing]

% experimental spacing
%
% test: oeps {\setcharacterspacing[frenchpunctuation]x: xx \bfd x: xx} oeps: test

\newcount \maxcharacterspacingid

\def\definecharacterspacing[#1]%
  {\ifcsname\??ch#1\endcsname \else
     \global\advance\maxcharacterspacingid\plusone
     \setxvalue{\??ch:#1}{\the\maxcharacterspacingid}%
   \fi}

\def\setupcharacterspacing
  {\dotripleargument\dosetupcharacterspacing}

\def\dosetupcharacterspacing[#1][#2][#3]%
  {\ifcsname\??ch:#1\endcsname
     \begingroup % for the moment we use modes, in ordere to avoid interface translation
     \getparameters[\??ch][\c!left=0,\c!right=0,\c!alternative=0,#3]%
     \ctxlua{spacings.setspacing(\getvalue{\??ch:#1},\number#2,\@@chleft,\@@chright,\@@chalternative)}%
     \endgroup
   \fi}

\def\setcharacterspacing
  {\ctxlua{spacings.enabled=true}%
   \gdef\setcharacterspacing[##1]{\dosetattribute{spacing}{\csname\??ch:##1\endcsname}}%
   \setcharacterspacing}

\def\resetcharacterspacing
  {\doresetattribute{spacing}}

\letvalue{\??ch:\s!reset}\attributeunsetvalue

% \setcharacterspacing[frenchpunctuation]
% «\type{bla}»\crlf « \type{bla}»\crlf
% «bla »\crlf « bla»\crlf « bla »\crlf
% bla: bla\crlf bla : bla

\definecharacterspacing [frenchpunctuation] % name may change / unit is em

\setupcharacterspacing [frenchpunctuation] ["003A] [\c!left =.25,\c!alternative=1] % : % strip preceding space(char)
\setupcharacterspacing [frenchpunctuation] ["003B] [\c!left =.25,\c!alternative=1] % ; % strip preceding space(char)
\setupcharacterspacing [frenchpunctuation] ["003F] [\c!left =.25,\c!alternative=1] % ? % strip preceding space(char)
\setupcharacterspacing [frenchpunctuation] ["0021] [\c!left =.25,\c!alternative=1] % ! % strip preceding space(char)
\setupcharacterspacing [frenchpunctuation] ["00AB] [\c!right=.25,\c!alternative=1] % guillemotleft/leftguillemot   % strip following space(char)
\setupcharacterspacing [frenchpunctuation] ["00BB] [\c!left =.25,\c!alternative=1] % guillemotright/rightguillemot % strip preceding space(char)

\protect \endinput
