+% cleanup at end of the document
+%\let\ltj@@orig@end=\@@end
+%\protected\def\@@end{%
+% \directlua{luatexja.ext_cleanup()}\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}}{}}}