% \leftedline
% ===========
% 12 octobre 2014
% Note (2020): somewhat obsoleted for many years by my usage of
% everbatim and everbatim* environments.
\makeatletter
\newif\ifinlefted
\newcommand*\leftedline {%
      \ifhmode \\\relax
        \def\leftedline@{\hss\egroup\hskip\z@skip\ignorespaces }%
      \else
        \def\leftedline@{\hss\egroup }%
      \fi
      \afterassignment\@leftedline
      \let\next=}
\def\@leftedline
    {\hbox to \linewidth \bgroup \inleftedtrue
                         \everbatimeverypar
                         \bgroup
                         \aftergroup\leftedline@ }
\makeatother
% verbatim macros and environments
% ================================
%
% June 2013, then October 2014.
% -----------------------------
%
\makeatletter
\catcode`_ 11
% some of my verbatim environments do not make the space active (\lverb e.g.). Then
% \do@noligs must be modified, \char`#1 must be followed by a space token, else,
% the `#1 expansion will swallow one space.
\def\do@noligs #1{%
  \catcode`#1\active
  \begingroup
     \lccode`~`#1\relax
     \lowercase{%
  \endgroup\def~{\leavevmode\kern\z@\char`#1 }}%
}
% \lowast
\def\lowast{\raisebox{-.25\height}{*}}
\catcode`* 13
\def\makestarlowast {\let*\lowast\catcode`\*\active}%
\catcode`* 12


%--- straight quotes, added (finally...) Nov 2, 2014
%--- obsolete with use of newtxtt 1.05, late 2014
\begingroup\makeatletter
  \catcode`\'\active
  \catcode`\`\active
\@firstofone {\endgroup
  \def\makequotesstraight{% assumes textcomp package
% à propos textcomp est automatique avec pdflatex depuis Février 2020
     \let`\textasciigrave
     \let'\textquotesingle
     \catcode39\active
     \catcode96\active }%
}
% for Unicode engines, I think fontspec by default renders \ttfamily quotes straight
% else think \defaultfontfeatures[\ttfamily]{} or something like that
\ifdefined\XeTeXinterchartoks\let\makequotesstraight\empty\fi
\ifdefined\directlua\let\makequotesstraight\empty\fi

% \MacroFont and \MicroFont
% =========================
\def\restoreMicroFont {\def\MicroFont {\ttfamily
  \makestarlowast
%    \ifinlefted\else\ifineverb\else\color[named]{verbatim}\fi\fi
%    \ifinlefted\else\color[named]{saddlebrown}\fi
%    \ifinlefted\else\color[named]{sedate}\fi
    \ifinlefted\else\color[named]{Olive}\fi
}}
\restoreMicroFont
% Notice that \macrocode uses \macro@font which stores the \MacroFont meaning
% in force at \begin{document}. But doc.sty's verbatim uses current \MacroFont
% not the meaning at \begin{document}. Comprenne qui pourra...
\def\restoreMacroFont {\def\MacroFont {\ttfamily
%    \ifinlefted\else\ifineverb\else\color[named]{Blue}\fi\fi
%    \ifinlefted\else\color[named]{Blue}\fi
%    \ifinlefted\else\color[named]{sedate}\fi
    \ifinlefted\else\color[named]{Olive}\fi
}}
\restoreMacroFont
% \verb
% =====
% Initially, June 2013, then Sep 9, 2014, and Oct 9-12 2014
%
% Initial motivation was simply that doc.sty and related classes \verb
% macro is with a hard-coded \ttfamily. There were further issues.
%
% 1. With |stuff with space|, paragraph reformatting in the Emacs/AUCTeX
% buffer caused havoc. Thus I wanted the input to accept linebreaks in
% its contents.
%
% 2. Hence I did not want to have obeyed spaces obeyed, (Emacs reflowing
% of paragraph in certain contexts often adds spaces at beginning of a line)
%
% 3. Also I wanted to allow hyphenated output, at least at some
% locations. I did a first version which treated spaces, \, {, and }
% specially.
%
% 4. At some point I wanted to add some colored background (I have
% dropped that since due to pdf file size increase).
%
% 5. And also I got fed up from the non-compatibility with footnotes due
% to catcode freeze.
%
% Because of 5. I opted for a \scantokens approach, hence for a macro
% with delimited argument. Here is what I do now, this is compatible
% with short verbs.
\def\verb
{%
  \relax \ifmmode\else\leavevmode\null\fi
  \bgroup
  \let\do\@makeother \dospecials
  \@ifstar{\@sverb}% \verb* is used in the index (obsolete: no indices at 1.3e),
                   % leave it using ambient font
          {\MicroFont % used to change font (ttbfamily=slashed 0), color,
                      % will make * active via \makestarlowast
           \catcode 32 10 \endlinechar 32 % allows to fetch across line breaks
           \frenchspacing % done globally in document
           \@@jfverb}%
}%
% Note (Oct 12, 2014): in the improbable situation a newlinechar is
% found in the ##1, \scantokens will convert this to an end of line in
% its "write" phase, which will be then ignored in its "read" phase due
% to \endlinechar-1. This also avoids possible creation of \par which
% would defeat \@@jfverb@@. Thus it is good.
\def\@@jfverb #1{%
   \ifcat\noexpand#1\noexpand~\catcode`#1\active\fi
% No problem with the EOL for the line where the short verb delimiter stands.
   \def\next ##1#1{%
            \@vobeyspaces\everyeof{\relax}\endlinechar\m@ne
            \expandafter\@@jfverb_a\scantokens\expandafter{##1}}%
% hack with \@empty to prevent brace stripping if catcodes have been
% frozen earlier, like in footnotes.
   \next \@empty
}
% We don't want a \discretionary at the very start.
% But then an empty argument is forbidden!
\def\@@jfverb_a #1{#1\@@jfverb_b }

\def\@@jfverb_b #1{\ifx\relax #1%
        \egroup
      \else
% \penalty\z@, or rather (Oct 11, 2014) but I then adjust the textwidth
% precisely:
      \discretionary{\copy\SoftWrapIcon}{}{}%
        #1\expandafter\@@jfverb_b\fi
}
% \SoftWrapIcon box for line-breaking using discretionaries
% =========================================================
\DeclareFontFamily{U}{MdSymbolC}{}
\DeclareFontShape {U}{MdSymbolC}{m}{n}{<-> MdSymbolC-Regular}{}
\newbox\SoftWrapIcon
\colorlet {softwrapicon}{blue}
% Emacs/AUCTeX uses very strange comment-like highlighting for \usefont{U}...
\def\SetSoftWrapIcon{%
    \setbox\SoftWrapIcon\hb@xt@\z@
    {\hb@xt@\fontdimen2\font
        {\hss{\color{softwrapicon}\usefont{U}{MdSymbolC}{m}{n}\char"97}\hss}%
     \hss}%
   }
\AtBeginDocument {\SetSoftWrapIcon }% ttzfamily déjà fait

\catcode`_ 8
\makeatother

% everbatim environment
% =====================

% October 13-14, 2014
% Verbatim with an \everypar hook, mainly to have background color, followed by
% execution of the contents (not limited by a group-scope)

\makeatletter
\catcode`_ 11

% Je modifie Mardi 18 février 2020 \MacroFont
% pour essayer couleur de foreground,
% je supprime donc le \ifineverb de \MacroFont.
% \newif\ifineverb
\def\everbatimtop {\MacroFont \small}
\let\everbatimbottom\relax
\let\everbatimhook\relax

\def\everbatim {\s@everbatim\@everbatim }
\@namedef{everbatim*}{\s@everbatim\expandafter\@everbatimx\expandafter
                      {\the\newlinechar}}

\def\everbatimeverypar{\strut
                   {\color[named]{WhiteSmoke}\vrule\@width\linewidth }%
                   \kern-\linewidth
                   \kern\everbatimindent }
\def\everbatimindent {\z@}
% voir plus loin atbegindocument

\def\endeverbatim  {\if@newlist \leavevmode\fi\endtrivlist }

\@namedef{endeverbatim*}{\endeverbatim\aftergroup\everbatimundoparskip}
%\def\everbatimundoparskip{\ifdim\parskip>\z@\vskip-\parskip\fi}
\def\everbatimundoparskip{\vbox{}\kern-\baselineskip\kern-\parskip}

\def\s@everbatim {%
%     \ineverbtrue
     \everbatimtop % put there size changes
       \topsep    \z@skip
       \partopsep \z@skip
       \itemsep   \z@skip
       \parsep    \z@skip
       \parskip   \z@skip
       \lineskip  \z@skip
     \let\do\@makeother \dospecials
     \let\do\do@noligs  \verbatim@nolig@list
     \makestarlowast
     \makequotesstraight
     \everbatimhook
     \trivlist
       \@topsepadd \z@skip
       \item\relax
       \leftskip   \@totalleftmargin
       \rightskip  \z@skip
       \parindent  \z@
       \parfillskip\@flushglue
       \parskip    \z@skip
       \@@par
       \def\par{\leavevmode\null\@@par\pagebreak[1]}%
       \everypar\expandafter{\the\everypar \unpenalty
                \everbatimeverypar
                \everypar \expandafter{\the\everypar\everbatimeverypar}%
       }%
       \obeylines \@vobeyspaces
}

\begingroup
\lccode`X 13
\catcode`X \active
\lccode`Y `* % this is because of \makestarlowast.
% I have to think whether this is useful: obviously if I were to provide
% everbatim and everbatim*  in a package I wouldn't do that.
\catcode`Y  \active
\catcode`| 0 \catcode`[ 1 \catcode`] 2 \catcode`* 12
\catcode`{ 12 \catcode`} 12 |catcode`\\ 12
|lowercase[|endgroup% both freezes catcodes and converts X to active ^^M
|def|@everbatim #1X#2\end{everbatim}%
  [#2|end[everbatim]|everbatimbottom ]
|def|@everbatimx #1#2X#3\end{everbatimY}]%
  {#3\end{everbatim*}%
     \everbatimbottom
     \newlinechar 13
     \everbatimxprehook\ttfamily
     \scantokens {#3}%
     \newlinechar #1\relax
     \rmfamily\everbatimxposthook
}%

% L'espace venant du endofline final mis par \scantokens sera inhibé si #3 se
% termine par un % ou un \x, etc...

\def\everbatimxprehook {\colorlet{everbsavedcolor}{.}\color[named]{MidnightBlue}}
\def\everbatimxposthook {\color{everbsavedcolor}}
\ifpdf
   \def\everbatimxprehook
      {\pdfcolorstack\@pdfcolorstack push{0.8837 0.4109 0 0.4109 k 0.8837 0.4109 0 0.4109 K}\relax}
   \def\everbatimxposthook
      {\pdfcolorstack\@pdfcolorstack pop\relax}
\else
\ifxetex
   \def\everbatimxprehook  {\special{color push cmyk 0.8837 0.4109 0 0.4941}}
   \def\everbatimxposthook {\special{color pop}}
%- \else
%- \ifnum\Withdvipdfmx=1
%-     \def\everbatimxprehook  {\special{pdf:bcolor  OrangeRed}}
%-     \def\everbatimxposthook {\special{pdf:ecolor}}
%-\fi
\fi\fi



% \everb
% ======
%
% Original was called \dverb and I did it in June 2013.
% Then after doing everbatim, I transformed \dverb, now called \everb
% for itself being as compatible as standard verbatim with list making
% surrounding environments.
% Supposed to be used as
% \everb|@ this will be ignored
% stuff
% escape character: "
% | not necessarily starting a line.
% I chose @ as comment character, mainly for pretty-formatting of the
% source, this can be changed by \everbhook.

% " comme caractère d'échappement. Par exemple pour colorier des parties.
\def\restoreeverbhook{\def\everbhook{%
     \def\"{\begingroup\catcode123 1 \catcode 125 2 \everbescape }%
     \catcode`\" 0 \catcode`\@ 14
}}\restoreeverbhook

\def\everbescape #1;!{#1\endgroup }

\def\everb {%
    \bgroup
    \let\everbatimhook\everbhook
    \s@everbatim
    \@everb
}

\def\@everb #1{\catcode`#1\active
               \lccode`\~`#1%
               \lowercase{\def~{\if@newlist \leavevmode\fi
                                \endtrivlist
                                \egroup
                                \@doendpe
                                \everbatimbottom }}%
              }%

\catcode`_8
\makeatother

% \printnumber
% ============

\catcode`_ 11
\makeatletter
\catcode`& 3
\def\allowsplits_a {\futurelet\printnumber_token\allowsplits_b }%
\def\allowsplits_b{\ifx\printnumber_token\@sptoken\space\fi\allowsplits_c }
\def\allowsplits_c #1{\ifx &#1\xint_dothis\xint_gobble_i\fi
                    \if ,#1\xint_dothis {\discretionary{\rlap,}{}{,}}\fi
                    \xint_orthat{\discretionary
                           {\copy\SoftWrapIcon}%
                           {}%
                           {}#1}\allowsplits_a }%

\def\printnumber #1{\expandafter\allowsplits_a \romannumeral-`0#1&}%
\hyphenpenalty \z@

\catcode`& 4
\makeatother
\catcode`_ 8

