%
% ltj-latex.sty
%

%! これは外から読まれない
%% Avoid multiple loading.
\csname luatexjalatexLoaded\endcsname
\edef\ltj@latex@AtEnd{%
\endlinechar=\the\endlinechar
\relax}
\endlinechar\m@ne%
\def\luatexjalatexLoaded{\endinput}

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ltj-latex}[2025-06-08 LaTeX support of LuaTeX-ja]

\endlinechar=13

%%% LaTeX version detection
\directlua{%
  luatexbase.add_to_callback('pre_shipout_filter',luatexja.direction.shipout_lthook, 'ltj.direction', 1)
}

%%% patch for LaTeX
\usepackage{lltjfont}
\usepackage{lltjdefs}
\@ifl@t@r\fmtversion{2025-06-01}{\usepackage{lltjcore}}{\usepackage{lltjcore-241201}}
\RequirePackage{lltjp-atbegshi}

%%% patching commands
%% \ltj@ExecuteBeforePackage <package><code>  and \ltj@ExecuteAfterPackage <package><code>
%% no-starred version: do nothing if the <package> is loaded
%% starred version: do <code> right away if the <package> is loaded
\def\ltj@ExecuteAfterPackage{\@ifstar\@@ltj@ExecuteAfterPackage@star\@@ltj@ExecuteAfterPackage}
\def\ltj@ExecuteBeforePackage{\@ifstar\@@ltj@ExecuteBeforePackage@star\@@ltj@ExecuteBeforePackage}
\@ifl@t@r\fmtversion{2021-11-15}{% 2021-11-15
  \def\@@ltj@ExecuteAfterPackage@star#1{\AddToHook{package/#1/after}}
  \def\@@ltj@ExecuteBeforePackage@star#1{\AddToHook{package/#1/before}}
  \long\def\@@ltj@ExecuteAfterPackage#1#2{%
    \@ifpackageloaded{#1}{}{\AddToHook{package/#1/after}{#2}}%
  }
  \long\def\@@ltj@ExecuteBeforePackage#1#2{%
    \@ifpackageloaded{#1}{}{\AddToHook{package/#1/before}{#2}}%
  }
}{
  \def\@@ltj@ExecuteAfterPackage#1{\AddToHook{package/after/#1}}
  \def\@@ltj@ExecuteBeforePackage#1{\AddToHook{package/before/#1}}
  \long\def\@@ltj@ExecuteAfterPackage@star#1#2{%
    \@ifpackageloaded{#1}{#2}{\@@ltj@ExecuteAfterPackage{#1}{#2}}%
  }
  \long\def\@@ltj@ExecuteBeforePackage@star#1#2{%
    \@ifpackageloaded{#1}{#2}{\@@ltj@ExecuteBeforePackage{#1}{#2}}%
  }
}
\def\ltj@pkgpatch@im#1{\@ifnextchar[{\ltj@pkgpatch@im@{#1}}{\ltj@pkgpatch@im@{#1}[lltjp-#1]}}%]
\def\ltj@pkgpatch@im@#1[#2]{\ltj@ExecuteAfterPackage*{#1}{\ltj@pkgpatch@im@@{#2}}}
\def\ltj@pkgpatch@im@@#1{%
  \@ifpackageloaded{#1}{}{%
    \if\catcode`@=11\let\ltj@temp\relax\else
      \edef\ltj@temp{\noexpand\catcode`@=\the\catcode`@\relax}\makeatletter
    \fi
    \@@input{#1.\@pkgextension}\ltj@temp
  }%
}

\def\ltj@pkgpatch#1{\@ifnextchar[{\ltj@pkgpatch@{#1}}{\ltj@pkgpatch@{#1}[lltjp-#1]}}%]
\def\ltj@pkgpatch@@{\AddToHook{begindocument/before}[luatexja/autopatch]}
\def\ltj@pkgpatch@#1[#2]{%
  \ltj@pkgpatch@@{%
    \@ifpackageloaded{#1}{\@ifpackageloaded{#2}{}{\RequirePackage{#2}}}{}%
  }%
}
\expandafter\let\csname JY3/mc/m/n/10\endcsname\relax

%%% patch for packages
\ltj@pkgpatch@im{listings}
\ltj@pkgpatch{xunicode}  %% at \begin{document}
\ltj@pkgpatch@im{fontspec}
\ltj@pkgpatch{footmisc}  %% at \begin{document}
\ltj@pkgpatch{tascmac}
\ltj@pkgpatch{ascmac}[lltjp-tascmac]
\ltj@pkgpatch@im{stfloats}
\ltj@pkgpatch{array}    %% at \begin{document}
\ltj@pkgpatch{collcell} %% at \begin{document}
\ltj@pkgpatch@im{unicode-math}
\ltj@pkgpatch@im{siunitx}
\ltj@pkgpatch@im{microtype}

\RequirePackage{lltjp-geometry}

\ltj@ExecuteBeforePackage{breqn}{%
  \let\ltj@@vadjust@breqn\vadjust \let\ltj@@insert@breqn\insert
  \let\vadjust\ltj@@orig@vadjust  \let\insert\ltj@@orig@insert
}
\ltj@ExecuteAfterPackage{breqn}{%
  \let\vadjust\ltj@@vadjust@breqn   \let\insert\ltj@@insert@breqn
  \let\ltj@@vadjust@breqn\undefined \let\ltj@@insert@breqn\undefined
}

\def\ltj@@q@stop{\ltj@@q@stop}
\ifdefined\ltj@stop@overwrite@primitive
  \begingroup
    \def\ltj@loop#1{%
      \unless\ifx#1\ltj@@q@stop
        \escapechar-1
        \ifcsname ltj@@orig@\string#1\endcsname
          \ifcsname ltj@@\string#1\endcsname
            \edef\ltj@@next{\unexpanded{\global\let#1}\csname ltj@@orig@\string#1\endcsname}%
            \edef\ltj@@temp{ltj@@orig@\string#1}%
            \escapechar92
            \PackageInfo{luatexja}{%
              Restoring \string#1\space into its orignal meaning\MessageBreak
              (\expandafter\meaning\csname\ltj@@temp\endcsname)}%
            \ltj@@next%
          \fi
        \fi
        \let\ltj@@next\ltj@loop
      \else\let\ltj@@next\relax\fi\ltj@@next
    }%
    \expandafter\ltj@loop\ltj@stop@overwrite@primitive\ltj@@q@stop
  \endgroup
\fi
\def\ltj@overwrite@primitive#1{%
  \begingroup
    \def\ltj@loop##1{%
      \unless\ifx##1\ltj@@q@stop
        \escapechar-1
        \edef\ltj@@next{\unexpanded{\let\ltj@@temp}\csname ltj@@\string##1\endcsname}\ltj@@next
        \ifx\ltj@@temp\relax
        \else\ifx\ltj@@temp##1
           \escapechar92
           \PackageInfo{luatexja}{No need to redefine \string##1}%
        \else
          \edef\ltj@@next{\unexpanded{\global\let##1}\csname ltj@@\string##1\endcsname}%
           \escapechar92\PackageInfo{luatexja}{Redefining \string##1}\ltj@@next%
        \fi\fi
        \let\ltj@@next\ltj@loop
      \else\let\ltj@@next\relax\fi\ltj@@next
    }%
    \ltj@loop#1\ltj@@q@stop
  \endgroup
}

%%------------------ all done
\ltj@latex@AtEnd
\endinput
%% EOF
