%%^^A%%  fontspec-code-patches.dtx -- part of FONTSPEC <wspr.io/fontspec>

% \section{Patching code}
%
%    \begin{macrocode}
%<*fontspec>
%    \end{macrocode}
%
%
% \subsection{Verbatims}
%
% Many thanks to Apostolos Syropoulos for discovering this problem and writing the  redefinion of \LaTeX's |verbatim| environment and \cs{verb*} command.
%
% \begin{macro}{\fontspec_visible_space:}
% Print \unichar{2423}{Open box}, which is used to visibly display a space character.
%    \begin{macrocode}
\cs_new_protected:Nn \fontspec_visible_space:
  {
    \@@_primitive_font_glyph_if_exist:NnTF \font {"2423}
      { \char"2423\scan_stop: }
      { \fontspec_visible_space_fallback: }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fontspec_visible_space_fallback:}
% If the current font doesn't have \unichar{2423}{Open box}, use Latin Modern Mono instead.
%    \begin{macrocode}
\cs_new_protected:Nn \fontspec_visible_space_fallback:
  {
    {
      \usefont{\g_fontspec_encoding_tl}{lmtt}{\f@series}{\f@shape}
      \textvisiblespace
    }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fontspec_print_visible_spaces:}
% Helper macro to turn spaces (\verb|^^20|) active and print visible space instead.
%    \begin{macrocode}
\group_begin:
\char_set_catcode_active:n{"20}%
\cs_gset:Npn\fontspec_print_visible_spaces:{%
\char_set_catcode_active:n{"20}%
\cs_set_eq:NN^^20\fontspec_visible_space:%
}%
\group_end:
%    \end{macrocode}
% \end{macro}
%
% In 2019 there will be a new kernel-supported command for generalising `verbatim visible spaces'.
% For now we check if the new command is defined and disable all patching if so.
% In 2020, I suppose, I'll remove all the patching code here.
%
%    \begin{macrocode}
\AtBeginDocument
  {
    \cs_if_exist:NF \verbvisiblespace
      {
				\@@_patch_verb:
				\@@_patch_verbatim:
				\@@_patch_moreverb:
				\@@_patch_fancyvrb:
				\@@_patch_listings:
			}
  }
%    \end{macrocode}
%
%
% \begin{macro}{\verb}
% \begin{macro}{\verb*}
% Redefine \cmd\verb\ to use \cmd\fontspec_print_visible_spaces:.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_patch_verb:
  {
		\def\verb
			{
				\relax\ifmmode\hbox\else\leavevmode\null\fi
				\bgroup
					\verb@eol@error \let\do\@makeother \dospecials
					\verbatim@font\@noligs
					\@ifstar\@@sverb\@verb
			}
		\def\@@sverb{\fontspec_print_visible_spaces:\@sverb}
	}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{environment}{verbatim*}
% With the \pkg{verbatim} package.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_patch_verbatim:
  {
    \@ifpackageloaded{verbatim}
      {
        \cs_set:cpn {verbatim*}
          {
            \group_begin: \@verbatim \fontspec_print_visible_spaces: \verbatim@start
          }
      }
%    \end{macrocode}
% This is for vanilla \LaTeX.
%    \begin{macrocode}
      {
        \cs_set:cpn {verbatim*}
          {
            \@verbatim \fontspec_print_visible_spaces: \@sxverbatim
          }
      }
 }
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{listingcont*}
% This is for \pkg{moreverb}.
% The main |listing*| environment inherits this definition.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_patch_moreverb:
  {
    \@ifpackageloaded{moreverb}
      {
        \cs_set:cpn {listingcont*}
          {
            \cs_set:Npn \verbatim@processline
              {
                \thelisting@line \global\advance\listing@line1\relax
                \the\verbatim@line\par
              }
            \@verbatim \fontspec_print_visible_spaces: \verbatim@start
          }
      }{}
  }
%    \end{macrocode}
% \end{environment}
%
% \pkg{listings} and \pkg{fancvrb} make things nice and easy:
%    \begin{macrocode}
\cs_new_protected:Npn \@@_patch_fancyvrb:
  {
    \@ifpackageloaded{fancyvrb}
      {
        \cs_set_eq:NN \FancyVerbSpace \fontspec_visible_space:
      }{}
  }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new_protected:Npn \@@_patch_listings:
  {
    \@ifpackageloaded{listings}
      {
        \cs_set_eq:NN \lst@visiblespace \fontspec_visible_space:
      }{}
  }
%    \end{macrocode}
%
%
%
%    \begin{macrocode}
%</fontspec>
%    \end{macrocode}


\endinput

% /©
% ------------------------------------------------
% The FONTSPEC package  <wspr.io/fontspec>
% ------------------------------------------------
% Copyright  2004-2019  Will Robertson, LPPL "maintainer"
% Copyright  2009-2015  Khaled Hosny
% Copyright  2013       Philipp Gesang
% Copyright  2013-2016  Joseph Wright
% ------------------------------------------------
% This package is free software and may be redistributed and/or modified under
% the conditions of the LaTeX Project Public License, version 1.3c or higher
% (your choice): <http://www.latex-project.org/lppl/>.
% ------------------------------------------------
% ©/
