% (The MIT License)
%
% Copyright (c) 2021-2022 Yegor Bugayenko
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the 'Software'), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
% SOFTWARE.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ffcode}[2022/08/13 0.4.3 Fixed Font Code]

\RequirePackage{xkeyval}

\makeatletter
\DeclareOptionX{nopygments}{\PassOptionsToPackage{draft=true}{minted}}
\newif\ifff@noframes
\DeclareOptionX{noframes}{\ff@noframestrue}
\newif\ifff@nobars
\DeclareOptionX{nobars}{\ff@nobarstrue}
\newif\ifff@nonumbers
\DeclareOptionX{nonumbers}{\ff@nonumberstrue}
\newif\ifff@nocn
\DeclareOptionX{nocn}{\ff@nocntrue}
\ProcessOptionsX\relax
\makeatother

\RequirePackage{minted}

\setminted{breaklines}
\setminted{escapeinside=||,mathescape}
\setminted{highlightcolor=gray!30}
\usemintedstyle{bw}

\makeatletter\ifff@nonumbers
  \ifff@nobars
    \newminted[ffcode]{text}{}
  \else
    \newminted[ffcode]{text}{
      framesep=6pt,framerule=1pt,rulecolor=gray,frame=leftline
    }
  \fi
\else
  \renewcommand{\theFancyVerbLine}{\textcolor{gray}{\tiny\oldstylenums{\ttfamily\arabic{FancyVerbLine}}}}
  \ifff@nocn
    \ifff@nobars
      \newminted[ffcode]{text}{
        linenos,numbersep=2pt
      }
    \else
      \newminted[ffcode]{text}{
        framesep=6pt,framerule=1pt,rulecolor=gray,frame=leftline,
        linenos,numbersep=2pt
      }
    \fi
  \else
    \ifff@nobars
      \newminted[ffcode]{text}{
        firstnumber=last,linenos,numbersep=2pt
      }
    \else
      \newminted[ffcode]{text}{
        framesep=6pt,framerule=1pt,rulecolor=gray,frame=leftline,
        firstnumber=last,linenos,numbersep=2pt
      }
    \fi
  \fi
\fi\makeatother

\makeatletter
\newcommand\ff@print{\begingroup\obeyspaces\ff@printx}
\newcommand\ff@printx[1]{\texttt{#1}\endgroup}
\makeatother

\makeatletter\newcommand\ff@rule{\vrule height 6pt depth 1pt width 0pt}
\makeatletter\ifff@noframes
  \newcommand\ff[1]{\ff@rule\ff@print{#1}}
\else
  \RequirePackage{tcolorbox}
    \newtcbox\ff@box{nobeforeafter,colframe=gray!80!white,
    colback=gray!5!white,boxrule=0.1pt,arc=1pt,
    boxsep=1.2pt,left=0.5pt,right=0.5pt,top=0.2pt,bottom=0.2pt,
    tcbox raise base}
  \newcommand\ff[1]{\relax\ifmmode{\ff@rule\ff@print{#1}}\else{\ff@box{\ff@rule\ff@print{#1}}}\fi}
\fi\makeatother

\endinput
