%
% lltjp-xunicode.sty
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lltjp-xunicode}[2013/05/14 Patch to xunicode for LuaTeX-ja]

\def\UTFencname{EU2}\def\XeTeXpicfile{}
\RequirePackage{xunicode,luatexja}
{\catcode`\"=12
\gdef\check@hexcom@digits@#1#2#3#4#5@!@#6#7#8{%
 \ifx @#4\relax
  \UTFacc@warning@{insufficient hex digits #7 for #6-\string#8}%
 \else
  \ifcat \active\noexpand#8%
   \ifx\add@flag\@ne %
    \expandafter\def\csname\UTFencname\string#8\endcsname{\ltjalchar"#1#2#3#4\relax}%
    \ifx\unDeFiNed@#8%
     \ifx\cf@encoding\UTFencname
      \DeclareTextCommand{#8}{OT1}{\undefined}%
     \else
      \DeclareTextCommand{#8}{\cf@encoding}{\undefined}%
     \fi
    \else {% macro #8 exists already ...
      \let\protect\noexpand
      \edef\UTF@testi{#8}\def\UTF@testii{#8}%
      \ifx\UTF@testi\UTF@testii\aftergroup\UTF@ignore
      \else\aftergroup\UTF@doit\fi
     }%
     \iffalse
      % ... but when it isn't robust, make it so
      \expandafter\let\csname?-\string#8\endcsname#8\relax
      \edef\next@UTF@{{\cf@encoding}%
        {\expandafter\noexpand\csname?-\string#8\endcsname}}%
      \expandafter\DeclareTextCommand\expandafter
         {\expandafter#8\expandafter}\next@UTF@
     \fi
    \fi %
   \else % \add@flag \z@
    \expandafter\global\expandafter
      \let\csname\UTFencname\string#8\endcsname\relax
   \fi % end of \add@flag switch
  \else % not active catcode --- shouldn't happen
  % \typeout{*** did you really mean #8 ? ***}%
   \ifx\add@flag\@ne %
    \edef\tmp@name{\expandafter\string\csname\UTFencname\endcsname
      \expandafter\string\csname#8\endcsname}%
    \expandafter\def\csname\tmp@name\endcsname{\ltjalchar"#1#2#3#4\relax}%
    \ifx\cf@encoding\UTFencname
     \expandafter\DeclareTextCommand\expandafter
       {\csname#8\endcsname}{OT1}{\undefined}%
    \else
     \expandafter\DeclareTextCommand\expandafter
       {\csname#8\endcsname}{\cf@encoding}{\undefined}%
    \fi
   \else % \add@flag \z@
    \expandafter\global\expandafter\let\csname#8\endcsname\relax
   \fi % end of \add@flag switch
  \fi % end of \ifcat
 \fi}
\gdef\check@hex@digits@#1#2#3#4#5@!@#6#7#8#9{%
 \ifx @#4\relax
  \UTFacc@warning@{insufficient hex digits #7 for #6-\string#8#9}%
 \else
  \def\UTFchar{\ltjalchar"#1#2#3#4\relax}%
  \expandafter\expandafter\expandafter\declare@utf@composite
  \expandafter\expandafter\expandafter
   {\expandafter\csname#6\endcsname}{\UTFchar}{#8}{#9}\relax
 \fi}
\gdef\add@UTF@accent#1#2#3{\ifx\relax#2\relax\ltjalchar"#3\else
 \ifx\ #2\relax\ltjalchar"#3\else
 \expandafter\ifx\UTF@space#2\relax\ltjalchar"#3\else
 \ifx~#2\ltjalchar"#3\else#2\ltjalchar"#1\fi\fi\fi\fi\relax}
\gdef\add@UTF@accents#1#2#3{#2\ltjalchar"#1\ltjalchar"#3\relax}
}
% If unicode-math is already loaded, the following error occurs:
%    ! Undefined control sequence.
%    \hbar ->\hslash
% Therefore temporarily set \hbar=\relax.
\let\ltj@tmpa\hbar
\let\hbar\relax
\let\ltj@tmpb\iftipaonetoken
\let\iftipaonetoken\relax
\ReloadXunicode{EU2}
\makeatletter
\let\hbar\ltj@tmpa
\let\iftipaonetoken\ltj@tmpb
\let\ltj@tmpb\relax % no more an if-token

%% ticket 30501
{\catcode`\!0\catcode`\\11!catcode`!2 11!catcode`!?11!catcode`!-11
 !gdef!EU2\nobreakspace{!?-\nobreakspace}%
}

\endinput
