%
% lltjp-atbegshi.sty
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lltjp-atbegshi}[2024-05-14 Patching \string\shipout\space hooks for LuaTeX-ja]

% concept: execute all hooks inside yoko direction \vbox

%<*ltshipout>-----------------------------------
%
\RequirePackage{expl3}
\ExplSyntaxOn\makeatletter
\box_new:N \l__platex_shipout_dummy_box
\if_cs_exist:N \__shipout_execute_main_cont:Nnnn %% 2021-06-01
\cs_new_eq:NN \__platex_original_shipout_execute_main_cont:Nnnn
              \__shipout_execute_main_cont:Nnnn
\cs_set:Npn \__shipout_execute_main_cont:Nnnn #1#2#3#4 {
  % if the box #1 is not a \yoko-box (= horizontal writing),
  % then make it a \yoko-box behorehand.
  \platex_if_box_yoko:NF #1 {
    \vbox_set:Nn #1 { \platex_direction_yoko: \box_use:N #1 }
  }
  % if the current direction is not \yoko,
  % then enclose \__shipout_execute_main_cont:Nnnn with
  % a dummy \yoko-box named \l__platex_shipout_dummy_box.
  \platex_if_direction_yoko:TF {
    \__platex_original_shipout_execute_main_cont:Nnnn { #1 } { #2 } { #3 } { #4 }
  }{
    \vbox_set:Nn \l__platex_shipout_dummy_box {
      \platex_direction_yoko:
      \__platex_original_shipout_execute_main_cont:Nnnn { #1 } { #2 } { #3 } { #4 }
    }
    % [Limitation] the code above may discard some contents,
    % so we'd like to put it back by \box\l__platex_shipout_dummy_box.
    % however, an infinite loop occurs if we uncomment the line below
    % so we can't.
    %\box_use:N \l__platex_shipout_dummy_box
    \box_clear:N \l__platex_shipout_dummy_box
  }
}

\else: %% 2020-10-01
\cs_new_eq:NN \__platex_original_shipout_execute_cont: \__shipout_execute_cont:
\cs_set:Npn \__shipout_execute_cont:
{
  \platex_if_box_yoko:NF \l_shipout_box {
    \vbox_set:Nn \l_shipout_box { \platex_direction_yoko: \box_use:N \l_shipout_box }
  }
  \platex_if_direction_yoko:TF {
    \__platex_original_shipout_execute_cont:
  }{
    \vbox_set:Nn \l__platex_shipout_dummy_box {
      \platex_direction_yoko: \__platex_original_shipout_execute_cont:
    }
    \box_clear:N \l__platex_shipout_dummy_box
  }
}
\fi:
%</ltshipout>-----------------------------------
\ExplSyntaxOff
\endinput
