% This file is part of the CTAN package named plain-grid.
% 
%   plaingridM.tex: text
%                   and support for math and boxes in vertical mode
%   Version 1.0, 05.05.2026
%
%   Copyright (C) 2026  Udo Wermuth (author)
%
%   This program is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation, either version 3 of the License, or
%   (at your option) any later version.
%
%   This program is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this program.  If not, see <http://www.gnu.org/licenses/>.
%

\input plaingridT.tex
{\toks255=\expandafter{\gridrestoreparams}%
 \xdef\gridrestoreparams{\the\toks255
 \abovedisplayskip=\the\abovedisplayskip
 \belowdisplayskip=\the\belowdisplayskip
 \abovedisplayshortskip=
    \the\abovedisplayshortskip
 \belowdisplayshortskip=
    \the\belowdisplayshortskip
 \predisplaypenalty=\the\predisplaypenalty
 \postdisplaypenalty=
    \the\postdisplaypenalty
 \displaywidowpenalty=
    \the\displaywidowpenalty
 \everydisplay=\noexpand\grideverydisplay}}
\abovedisplayskip=0pt \belowdisplayskip=0pt
\abovedisplayshortskip=0pt
\belowdisplayshortskip=0pt
\predisplaypenalty=0 \postdisplaypenalty=0
\displaywidowpenalty=0
\outer\def\gridproclaim #1. #2\par{%
 \GRIDmakeavailable3
 \ifnum\GRIDfreelines=\GRIDlines%top of page
  \line{\hfil}\kern-0.5\GRIDgrid% keep space
 \else \gridskiphalfaline \fi
 \noindent{\bf#1.\enspace}{\sl#2\par}%
 \gridskiphalfaline}
\newcount\GRIDboxlines % see \GRIDboxheight
\def\GRIDboxheight#1(#2:#3){% create a vbox
 % of a certain height; #1: vbox number; #2
 \begingroup % and #3: skips surrounding #1
  % store data needed in the reporting
  \edef\GRIDnext{GRID line \the\inputlineno:
   box (\the\ht#1+\the\dp#1)}%
  {\setbox2=\vbox{#2\relax#3}\xdef\GRIDtmp
    {[changed by (\the\ht2+\the\dp2)]}%
   \global\GRIDboxlines=\ht2
   \global\advance\GRIDboxlines by \dp2 }%
  % transform ht+dp of vbox #1 into lines
  \advance\GRIDboxlines by \ht#1\relax
  \advance\GRIDboxlines by \dp#1\relax
  \GRIDcnt=\GRIDboxlines   % now compute the
  \divide\GRIDcnt by \GRIDgrid %       lines
  \multiply\GRIDcnt by \GRIDgrid
  \advance\GRIDcnt by -\GRIDboxlines
  \ifnum\GRIDcnt<0 %           need >0 lines
   \advance\GRIDboxlines by \GRIDgrid
  \fi \global     % make \GRIDboxlines known
   \divide\GRIDboxlines by \GRIDgrid
  % final step: build vbox with \GRIDdim
  \GRIDdim=\GRIDboxlines\GRIDgrid
  \global\setbox#1=\vbox to \GRIDdim{%
   #2\unvbox#1#3}%
  \GRIDlog3{\GRIDnext\space\GRIDtmp\space
   output as (\the\ht#1+\the\dp#1) in
   \the\GRIDboxlines\space lines}%
 \endgroup \ignorespaces}
\newtoks\grideverydisplay    % \everydisplay
\grideverydisplay=\everydisplay   % is saved
\everydisplay={\GRIDdisplay}% use next macro
\newbox\GRIDbox    % box for a lot of macros
\newdimen\GRIDdisplaydimen % <>0pt: override
\def\GRIDdisplay#1$${% #1: delimited display
 $$\vskip-\GRIDgrid \penalty0
 \GRIDlinesavailable \begingroup
  \setbox\GRIDbox=\vbox{\normalbaselines
   \everydisplay=\grideverydisplay $$#1$$}%
  \ifdim\GRIDdisplaydimen=0pt %   compute ht
   \GRIDboxheight\GRIDbox
    (\vskip\jot\vfil:\vfil)% build grid vbox
  \else\GRIDlog3{GRID line \the\inputlineno:
    user's height \the\GRIDboxlines\space
    lines or \the\GRIDdisplaydimen; top plus
    \the\GRIDdim}\setbox\GRIDbox=\vbox
    to \GRIDdisplaydimen{\vss \vskip
     \GRIDdim \unvbox\GRIDbox \vss}%
   \global\GRIDdisplaydimen=0pt %use it once
  \fi \ifnum\GRIDboxlines>\GRIDfreelines
   \GRIDlog0{GRID: unexpected page
    ejection}\grideject \fi \box\GRIDbox
 \endgroup \noindent \ignorespaces}
\def\griddisplay#1:#2$${%#1: number of lines
 % #2: space added at top of display in \jot
 \GRIDboxlines=#1\relax \GRIDdim=#2\jot
 \GRIDdisplaydimen=\GRIDboxlines\GRIDgrid
 $$}%            start the display math mode
\def\gridskip1/#1{% set shift #1: 1,2,3,4
 \def\GRIDinline{#1 }\GRIDshift
 \ignorespaces}% <shift> is #1x\baselineskip
\def\GRIDshift{% output \kern<shift>
 \edef\GRIDtmp{\noexpand\kern % comp <shift>
  \ifcase\GRIDinline 0\or 1\or 0.5\or
   0.33333\or 0.25\else 0\fi\GRIDgrid}%
 \ifvmode\GRIDtmp\else\vadjust{\GRIDtmp}\fi}
\def\GRIDsmash#1{% #1: horizontal material
 \setbox\GRIDbox=\hbox{#1}\dp\GRIDbox=0pt
 \ht\GRIDbox=0pt \box\GRIDbox}% zero ht & dp
\def\gridinline#1$#2${% #1: new value for
 % \GRIDinline (optional); #2: the math
 \def\GRIDtmp{#1}\GRIDsmash{$#2$}%
 \ifx\GRIDtmp\empty  % keep the skip's value
 \else \def\GRIDinline{#1 }\fi % a new value
 \GRIDshift}%     apply previous or new skip
\def\gridbeginbox{\par \GRIDprevgrid
 \setbox\GRIDbox=\vbox\bgroup}
\def\gridendbox{\egroup       % end the vbox
 \GRIDboxheight\GRIDbox(\vfil:\vfil)%
 \GRIDmakeavailable\GRIDboxlines % output on
 \box\GRIDbox}%       page with enough space
