-\def\ltj@pkgpatchlist{}
-\def\ltj@pkgpatch#1{
- \@ifpackageloaded{#1}{\usepackage{lltjp-#1}}{
- \@ifpackageloaded{filehook}{\AtEndOfPackageFile*{#1}{\usepackage{lltjp-#1}}}{}
- }
- \ifx\ltj@pkgpatchlist\@empty
- \gdef\ltj@pkgpatchlist{#1}
- \else
- \xdef\ltj@pkgpatchlist{\ltj@pkgpatchlist, #1}
- \fi
-}
+% I (HK) don't know why the code below is not necessary.
+%\let\ltj@@orig@end=\@@end
+%\outer\protected\def\@@end{%
+% \directlua{luatexja.direction.remove_end_whatsit()}\ltj@@orig@end}
+
+% Globally add to the head of a macro (cf. LaTeX2e's \g@addto@macro)
+\long\def\ltj@g@addto@macro#1#2{
+ \begingroup
+ \def\ltj@tmpa{#2}
+ \toks@\expandafter\expandafter\expandafter{\expandafter\ltj@tmpa#1}
+ \xdef#1{\the\toks@}
+ \endgroup}
+
+\def\ltj@AtBeginDocument{\ltj@g@addto@macro\@begindocumenthook}
+
+\def\ltj@pkgpatch#1{\ltj@AtBeginDocument{\@ifpackageloaded{#1}{\usepackage{lltjp-#1}}{}}}