%D \module
%D   [       file=typo-cap,
%D        version=2009.03.27, % code moved from core-spa.mkiv
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Capping,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 / Caps}

\unprotect

%D Maybe we need a more clever system: either command or style mode etc. so
%D that we can avoid the grouped mess in a simple style switch.

\registerctxluafile{typo-cap}{optimize}

% \definesystemattribute[case][public] % already predefined

%D \macros
%D   {setupcapitals}
%D
%D By default we use pseudo small caps in titles. This can be
%D set up with:
%D
%D \showsetup{setupcapitals}

\installcorenamespace{capitals}

\installcommandhandler \??capitals {capitals} \??capitals

%D Beware, these are not really defines (yet).

\definecapitals[\v!WORD]            % all upper
\definecapitals[\v!capital]         % one upper + font
\definecapitals[\v!Capital]         % some upper + font
\definecapitals[\v!mixed]           % UpperCase
\definecapitals[\v!WORD]            % all lower
\definecapitals[\v!Word]            % one upper + font
\definecapitals[\v!Words]           % some upper
\definecapitals[\v!camel]           % lowers first
\definecapitals[\v!word][\c!style=] % nothing

%D \macros
%D   {Word, Words, WORD, WORDS}
%D
%D This is probably not the right place to present the next set
%D of macros.
%D
%D \starttyping
%D \Word {far too many words}
%D \Words{far too many words}
%D \WORD {far too many words}
%D \WORDS{far too many words}
%D \stoptyping
%D
%D \typebuffer
%D
%D This calls result in:
%D
%D \startlines
%D \getbuffer
%D \stoplines
%D
%D \showsetup{Word}
%D \showsetup{Words}
%D \showsetup{WORD}
%D \showsetup{WORDS}

% test \WORD{test TEST \TeX} test
% test \word{test TEST \TeX} test
% test \Word{test TEST \TeX} test

\unexpanded\def\setcharactercasing[#1]%
  {\clf_setcharactercasing{#1}\fontid\font}

% todo: names casings

% \unexpanded\def\WORD {\groupedcommand{\setcharactercasing[\v!WORD ]}{}}
% \unexpanded\def\word {\groupedcommand{\setcharactercasing[\v!word ]}{}}
% \unexpanded\def\Word {\groupedcommand{\setcharactercasing[\v!Word ]}{}}
% \unexpanded\def\Words{\groupedcommand{\setcharactercasing[\v!Words]}{}}
% \unexpanded\def\camel{\groupedcommand{\setcharactercasing[\v!camel]}{}}

\unexpanded\def\typo_capitale_WORD {\clf_setcharactercasing{\v!WORD }\fontid\font}
\unexpanded\def\typo_capitale_word {\clf_setcharactercasing{\v!word }\fontid\font}
\unexpanded\def\typo_capitale_Word {\clf_setcharactercasing{\v!Word }\fontid\font}
\unexpanded\def\typo_capitale_Words{\clf_setcharactercasing{\v!Words}\fontid\font}
\unexpanded\def\typo_capitale_camel{\clf_setcharactercasing{\v!camel}\fontid\font}

\unexpanded\def\WORD {\triggergroupedcommandcs\typo_capitale_WORD }
\unexpanded\def\word {\triggergroupedcommandcs\typo_capitale_word }
\unexpanded\def\Word {\triggergroupedcommandcs\typo_capitale_Word }
\unexpanded\def\Words{\triggergroupedcommandcs\typo_capitale_Words}
\unexpanded\def\camel{\triggergroupedcommandcs\typo_capitale_camel}

\let\WORDS\WORD
\let\words\word

%D \macros
%D   {kap,KAP,Kap,Kaps,nokap,userealcaps,usepseudocaps}
%D
%D We already introduced \type{\cap} as way to capitalize
%D words. This command comes in several versions:
%D
%D \startbuffer
%D \cap {let's put on a \cap{cap}}
%D \cap {let's put on a \nocap{cap}}
%D \CAP {let's put on a \\{cap}}
%D \Cap {let's put on a \\{cap}}
%D \Caps{let's put on a cap}
%D \stopbuffer
%D
%D \typebuffer
%D
%D Note the use of \type{\nocap}, \type{\\} and the nested
%D \type{\cap}.
%D
%D \startlines
%D \getbuffer
%D \stoplines
%D
%D These macros show te main reason why we introduced the
%D smaller \type{\tx} and \type{\txx}.
%D
%D \starttyping
%D \cap\romannumerals{1995}
%D \stoptyping
%D
%D This at first sight unusual capitilization is completely
%D legal.
%D
%D \showsetup{smallcapped}
%D \showsetup{notsmallcapped}
%D \showsetup{CAPPED}
%D \showsetup{SmallCapped}
%D \showsetup{SmallCaps}
%D
%D The difference between pseudo and real caps is demonstrated
%D below:
%D
%D \startbuffer
%D \usepseudocaps \cap{Hans Hagen}
%D \userealcaps   \cap{Hans Hagen}
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer
%D
%D The \type {\bgroup} trickery below is needed because of
%D \type {\groupedcommand}.

\let\disablepseudocaps\relax % maybe used elsewhere

\newconditional\c_typo_capitals_pseudo

\unexpanded\def\usepseudocaps{\settrue \c_typo_capitals_pseudo}
\unexpanded\def\userealcaps  {\setfalse\c_typo_capitals_pseudo}

\usepseudocaps

% we use char0 as placeholder for the larger font
%
% here we keep the \groupedcommand

\def\typo_capitals_set_fake#1%
  {\edef\currentcapitals{#1}%
   \clf_setcharactercasing{\currentcapitals}\fontid\font
   \usecapitalsstyleparameter\c!style}

\def\typo_capitals_set_real#1%
  {\edef\currentcapitals{#1}%
   \sc
   \clf_setcharactercasing{\currentcapitals}\fontid\font}

% \unexpanded\def\pseudosmallcapped{\groupedcommand{\typo_capitals_set_fake\v!WORD   }\donothing} % all upper
% \unexpanded\def\pseudoSmallcapped{\groupedcommand{\typo_capitals_set_fake\v!capital}\donothing} % one upper + font
% \unexpanded\def\pseudoSmallCapped{\groupedcommand{\typo_capitals_set_fake\v!Capital}\donothing} % some upper + font
% \unexpanded\def\pseudoMixedCapped{\groupedcommand{\typo_capitals_set_fake\v!mixed  }\donothing} % UpperCase
%
% \unexpanded\def\realsmallcapped  {\groupedcommand{\typo_capitals_set_real\v!WORD   }\donothing} % all lower
% \unexpanded\def\realSmallcapped  {\groupedcommand{\typo_capitals_set_real\v!Word   }\donothing} % one upper + font
% \unexpanded\def\realSmallCapped  {\groupedcommand{\typo_capitals_set_real\v!Words  }\donothing} % some upper
%
% \unexpanded\def\notsmallcapped   {\groupedcommand{\typo_capitals_set_fake\v!word   }\donothing}

\unexpanded\def\pseudosmallcapped{\triggergroupedcommandcs\font_style_pseudosmallcapped}
\unexpanded\def\pseudoSmallcapped{\triggergroupedcommandcs\font_style_pseudoSmallcapped}
\unexpanded\def\pseudoSmallCapped{\triggergroupedcommandcs\font_style_pseudoSmallCapped}
\unexpanded\def\pseudoMixedCapped{\triggergroupedcommandcs\font_style_pseudoMixedCapped}

\unexpanded\def\realsmallcapped  {\triggergroupedcommandcs\font_style_realsmallcapped}
\unexpanded\def\realSmallcapped  {\triggergroupedcommandcs\font_style_realSmallcapped}
\unexpanded\def\realSmallCapped  {\triggergroupedcommandcs\font_style_realSmallCapped}

\unexpanded\def\notsmallcapped   {\triggergroupedcommandcs\font_style_notsmallcapped}

\unexpanded\def\font_style_pseudosmallcapped{\typo_capitals_set_fake\v!WORD   } % all upper
\unexpanded\def\font_style_pseudoSmallcapped{\typo_capitals_set_fake\v!capital} % one upper + font
\unexpanded\def\font_style_pseudoSmallCapped{\typo_capitals_set_fake\v!Capital} % some upper + font
\unexpanded\def\font_style_pseudoMixedCapped{\typo_capitals_set_fake\v!mixed  }

\unexpanded\def\font_style_realsmallcapped  {\typo_capitals_set_real\v!WORD   } % all lower
\unexpanded\def\font_style_realSmallcapped  {\typo_capitals_set_real\v!Word   } % one upper + font
\unexpanded\def\font_style_realSmallCapped  {\typo_capitals_set_real\v!Words  } % some upper

\unexpanded\def\font_style_notsmallcapped   {\typo_capitals_set_fake\v!word   }

\unexpanded\def\typo_capitals_smallcaps
  {\ifconditional\c_typo_capitals_pseudo
     \expandafter\firstoftwoarguments
   \else
     \expandafter\secondoftwoarguments
   \fi}

\unexpanded\def\smallcapped{\typo_capitals_smallcaps\pseudosmallcapped\realsmallcapped}
\unexpanded\def\Smallcapped{\typo_capitals_smallcaps\pseudoSmallcapped\realSmallcapped}
\unexpanded\def\SmallCapped{\typo_capitals_smallcaps\pseudoSmallCapped\realSmallCapped}

\unexpanded\def\font_style_smallcapped{\typo_capitals_smallcaps\font_style_pseudosmallcapped\font_style_realsmallcapped}
\unexpanded\def\font_style_Smallcapped{\typo_capitals_smallcaps\font_style_pseudoSmallcapped\font_style_realSmallcapped}
\unexpanded\def\font_style_SmallCapped{\typo_capitals_smallcaps\font_style_pseudoSmallCapped\font_style_realSmallCapped}

\unexpanded\def\autocap{\ifmmode\expandafter\normalcap\else\expandafter\smallcapped\fi}

\appendtoks
    \let\normalcap\cap % mathmode cap
    \let\cap\autocap
\to \everydump

\let\kap\cap          % for old times sake
\let\Caps\SmallCapped % for old times sake

\let\mixedcaps\pseudoMixedCapped

\let\normalsmallcapped\smallcapped
\let\normalWORD       \WORD
\let\normalword       \word

\let\font_style_normalsmallcapped\font_style_smallcapped
\let\font_style_normalWORD       \WORD
\let\font_style_normalword       \word

%D Further tweaks

\let\normalsmallcapped\smallcapped

\appendtoks
    \ifx\currentcapitals\empty
      \doifelse{\directcapitalsparameter\c!title}\v!yes
        {\definealternativestyle[\v!capital  ][\font_style_normalsmallcapped][\font_style_normalsmallcapped]%
         \definealternativestyle[\v!smallcaps][\setsmallcaps][\setsmallcaps]}
        {\definealternativestyle[\v!capital  ][\font_style_normalsmallcapped][\font_style_normalWORD]%
         \definealternativestyle[\v!smallcaps][\setsmallcaps][\font_style_normalWORD]}%
      \doifelse{\directcapitalsparameter\s!sc}\v!yes
        \userealcaps
        \usepseudocaps
    \fi
\to \everysetupcapitals

\let\uppercased\normalWORD
\let\lowercased\normalword

\setupcapitals
  [\c!title=\v!yes,
   \c!style=\tx,
   \s!sc=\v!no] % no \c!sc any longer

% \definefont
%   [MixedCaps]
%   [\v_font_string_a\v_font_string_c*default cp \the\exheight]
%
% \definefont
%   [MixedCaps]
%   [MixedCaps*default cp \the\exheight]

% \definefontfeature
%   [mixeddefault]
%   [default]
%   [extend=1.2]

\definefont
  [MixedCaps]
  [CurrentFont*default cp 1.2\exheight]

\setupcapitals
  [\v!mixed]
  [\c!style=MixedCaps]

% \definestartstop is not yet in available at core-spa time
%
% \startrandomized \input tufte \stoprandomized
%
% \definestartstop[randomized][\c!before=\dosetattribute{case}{8},\c!after=]

% \unexpanded\def\randomizetext{\groupedcommand{\attribute\caseattribute\pluseight}{}}

\unexpanded\def\randomizetext{\triggergroupedcommand{\attribute\caseattribute\pluseight}}

\definestartstop[randomized][\c!before=\dosetattribute{case}{8},\c!after=]

\protect \endinput
