%%
%% This is file `mathwrap.sty',
%% generated with the docstrip utility.
%%
%% Copyright (C) 2026 by Lee Yiu Sing <yslee.pika@connect.hku.hk>
%% ---------------------------------------------------------------------------
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   https://latex-project.org
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2008-05-04 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is Lee Yiu Sing.
%%
%% This work consists of the file mathwrap.sty.
%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mathwrap}[2026/11/23 v1.3 Automatic Inline Math Wrapping]

\newif\if@mathwrap@comma
\@mathwrap@commatrue

\newif\if@mathwrap@penalties
\@mathwrap@penaltiestrue

\newif\if@mathwrap@stretch
\@mathwrap@stretchtrue

\DeclareOption{comma}{\@mathwrap@commatrue}
\DeclareOption{nocomma}{\@mathwrap@commafalse}
\DeclareOption{penalties}{\@mathwrap@penaltiestrue}
\DeclareOption{nopenalties}{\@mathwrap@penaltiesfalse}
\DeclareOption{stretch}{\@mathwrap@stretchtrue}
\DeclareOption{nostretch}{\@mathwrap@stretchfalse}
\ProcessOptions\relax

% Allow line breaks after commas in inline math (\textstyle) while preserving spacing
\newcommand{\mathwrap@break}{%
  \mathchoice
    {}%
    {\penalty0\mskip0mu plus 2mu\relax}%
    {}%
    {}%
}

\newcommand{\mathwrap@comma@plain}{%
  \mathchar\mathwrap@orig@commacode
  \mathwrap@break
}

\newcommand{\mathwrap@comma@icomma}{%
  \futurelet\@let@token\mathwrap@icomma@check
}

\newcommand{\mathwrap@icomma@check}{%
  \ifx\@let@token\@sptoken
    \mathchar\mathwrap@orig@commacode
    \mathwrap@break
  \else
    \ifx\@let@token\space
      \mathchar\mathwrap@orig@commacode
      \mathwrap@break
    \else
      \mathord\mathchar\mathwrap@orig@commacode
    \fi
  \fi
}

\let\mathwrap@comma\mathwrap@comma@plain

\newcommand{\mathwrapOn}{%
  \if@mathwrap@comma
    \begingroup
      \lccode`\~=`\,%
      \lowercase{\endgroup\let~\mathwrap@comma}%
    \mathcode`\,="8000\relax
  \fi
  \if@mathwrap@penalties
    \binoppenalty=100\relax
    \relpenalty=100\relax
  \fi
  \if@mathwrap@stretch
    \ifdim\emergencystretch<1em\relax
      \emergencystretch=1em\relax
    \fi
  \fi
}

\newcommand{\mathwrapOff}{%
  \if@mathwrap@comma
    \mathcode`\,=\mathwrap@orig@mathcode\relax
    \begingroup
      \lccode`\~=`\,%
      \lowercase{\endgroup\let~\mathwrap@orig@activecomma}%
  \fi
  \binoppenalty=\mathwrap@orig@binoppenalty\relax
  \relpenalty=\mathwrap@orig@relpenalty\relax
  \emergencystretch=\mathwrap@orig@emergencystretch\relax
}

\newcommand{\mathwrap@init}{%
  \edef\mathwrap@orig@mathcode{\the\mathcode`\,}%
  \begingroup
    \lccode`\~=`\,%
    \lowercase{\endgroup\global\let\mathwrap@orig@activecomma~}%
  \ifnum\mathcode`\,="8000\relax
    \@ifpackageloaded{icomma}{%
      \ifx\mathcomma\@undefined
        \mathchardef\mathwrap@orig@commacode="613B\relax
      \else
        \mathchardef\mathwrap@orig@commacode=\mathcomma\relax
      \fi
      \let\mathwrap@comma\mathwrap@comma@icomma
    }{%
      \mathchardef\mathwrap@orig@commacode="613B\relax
      \let\mathwrap@comma\mathwrap@comma@plain
    }%
  \else
    \mathchardef\mathwrap@orig@commacode=\mathcode`\,\relax
    \let\mathwrap@comma\mathwrap@comma@plain
  \fi
  \edef\mathwrap@orig@binoppenalty{\the\binoppenalty}%
  \edef\mathwrap@orig@relpenalty{\the\relpenalty}%
  \edef\mathwrap@orig@emergencystretch{\the\emergencystretch}%
  \mathwrapOn
}

\ifx\AddToHook\@undefined
  \AtBeginDocument{\mathwrap@init}%
\else
  \AddToHook{begindocument/end}{\mathwrap@init}%
\fi

\endinput