% \iffalse meta-comment
%
% Copyright (C) 2021 by Jinwen XU 
% -------------------------------
% 
% This file 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:
%
%    http://www.latex-project.org/lppl.txt
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{mindflow.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{mindflow}
%<*package>
    [2021/05/27 mindflow environment]
%</package>
%
%<*driver>
\documentclass{article}
\usepackage{doc}
\usepackage[a4paper,top=1.2in,bottom=1.2in,left=1.6in,right=1.2in]{geometry}
\usepackage{titlesec}
\titleformat{\section}{\large\scshape\centering}{\thesection.}{.5em}{}
\titleformat{\subsection}{\large\itshape}{\thesubsection}{.5em}{}
\usepackage{titling}
\setlength{\droptitle}{-.7in}
\usepackage{enumitem}
\setlist{noitemsep}
\usepackage{mathpazo,newpxtext}
\useosf
\usepackage{microtype}
\PassOptionsToPackage{mathlines}{lineno}
\usepackage[linenumber,rightmarker]{mindflow}
\usepackage{blindtext}
\usepackage{parskip}
\usepackage{listings}
\definecolor{maintheme}{RGB}{50,100,150}
\definecolor{forestgreen}{RGB}{21,122,81}
\definecolor{lightergray}{gray}{0.99}
\lstset{language=[LaTeX]TeX,
    keywordstyle=\color{maintheme},
    basicstyle=\ttfamily,
    commentstyle=\color{forestgreen}\ttfamily,
    stringstyle=\rmfamily,
    showstringspaces=false,
    breaklines=true,
    frame=lines,
    backgroundcolor=\color{lightergray},
    flexiblecolumns=true,
    escapeinside={(*}{*)},
    % numbers=left,
    % numberstyle=\scriptsize, stepnumber=1, numbersep=5pt,
    % firstnumber=last,
} 
\providecommand{\meta}[1]{$\langle${\normalfont\itshape#1}$\rangle$}
\lstset{moretexcs=%
    {linenumbers,nolinenumbers,mindflowLeft,mindflowRight,mindflowTextFont,mindflowNumFont,mindflowMarkerFont,mindflowLineHeight,setlength,colorlet,color,
    RequirePackage,SetupKeyvalOptions,DeclareBoolOption,ProcessKeyvalOptions,PassOptionsToPackage,
    if@mindflow@twocolumn,@mindflow@incolumntrue,if@mindflow@linenumber,@mindflow@leftlinenumbertrue,if@mindflow@on,if@mindflow@off,@mindflow@offfalse,if@mindflow@incolumn,if@mindflow@leftlinenumber,if@mindflow@rightlinenumber,if@mindflow@leftmarker,if@mindflow@rightmarker,
    ifLNturnsON,LNturnsONtrue,LNturnsONfalse,ifLineNumbers,
    mindflowset,
    mindflow,endmindflow,comment,endcomment,
    mindflowON,endmindflowON,mindflowOFF,endmindflowOFF,
    mfSepLine,makeLineNumber,LineNumber,
    mindflow@makeLineNumber,LocallyStopLineNumbers,ResumeLineNumbers,
    ifdefined,linenomathpatch,cspreto,csappto,patchcmd,linenomathpatchAMS,linenomathAMS,linenomathWithnumbers,linenomath,endlinenomath,linenomathNonumbers,linenopenalty,
    define@key,setkeys,
    nowidow,
    }
}
\lstnewenvironment{code*}% 
{\setkeys{lst}{columns=fullflexible,keepspaces=true}}{}
\lstnewenvironment{code}% 
{\setkeys{lst}{columns=fullflexible,keepspaces=true,numbers=left,numberstyle=\scriptsize, stepnumber=1, numbersep=5pt,lastline=209}}{}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{mindflow.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \GetFileInfo{mindflow.dtx}
%
%
% \title{{\normalfont\textsf{mindflow}}, write your ideas in a clear way}
% \author{\scshape Jinwen Xu}
% \date{\filedate}
%
% \maketitle
%
% \section{Introduction}
%
% The \textsf{mindflow} package provides you a way to write your ideas 
% and annotations clearly. For example, with the options
% \verb|linenumber| and \verb|rightmarker|, you will get something like:
% \begin{mindflow}
%     What to write next:
%     \begin{itemize}
%         \item usage: the initialization, options and configuration;
%         \item an example;
%         \item some word on the line numbering feature;
%         \item some internal macros;
%         \item the complete code.
%     \end{itemize}
% \end{mindflow}
%
% \section{Usage}
%
% \subsection{Initialization}
% Simply load the package with 
% \iffalse
%<*example>
% \fi
\begin{code*}
  \usepackage{mindflow}
\end{code*}
% \iffalse
%</example>
% \fi
%
% \vspace{-.5\baselineskip}
% There are a few options available, which are listed in the next subsection.
%
% \subsection{Options}
% By default, the environment has no line numbers or markers. You can use the
% following options (they can be specified directly, like \verb|off|, or as a
% boolean optional, like \verb|off=false|):
%
% \begin{tabular}{ll}
%     \verb|linenumber| & Same as \verb|leftlinenumber|\\
%     \verb|leftlinenumber| & Enable left line numbers\\
%     \verb|rightlinenumber| & Enable right line numbers\\
%     \verb|leftmarker| & Enable left marker, by default it is a ``\verb|*|''\\
%     \verb|rightmarker| & Enable right marker, by default it is a ``\verb|*|''\\
%     \verb|incolumn| or \verb|twocolumn| & The separation line would fit in the column \\&(\emph{automatically applied in two-column documents})\\
%     \verb|off|, \verb|on| & Turn the \verb|mindflow| environments \emph{off} or \emph{on}
% \end{tabular}
%
% \DescribeEnv{\{mindflow\}}
% Then you can use the \verb|mindflow| environment as
% \iffalse
%<*example>
% \fi
\begin{code*}
  \begin{mindflow}
      (*\meta{text}*)
  \end{mindflow}
\end{code*}
% \iffalse
%</example>
% \fi
%
% \subsection{The configuration}
%
% \DescribeMacro{\mindflowset}
% You can use \lstinline|\mindflowset{|\meta{configuration}\lstinline|}| to
% configure the \texttt{mindflow} environment. The following keys are provided:
%
% \begin{center}
% \begin{tabular}{ll}
%     \verb|left| & The left marker\\
%     \verb|right| & The right marker\\ 
%     \verb|textcolor| & Color of the text\\
%     \verb|numcolor| & Color of line numbers\\
%     \verb|markercolor| & Color of the markers\\
%     \verb|linecolor| & Color of the separation line \\
% \end{tabular}
%
% \begin{tabular}{ll}
%     \verb|textfont| & Font of the text\\
%     \verb|numfont| & Font of line numbers\\
%     \verb|markerfont| & Font of the marker\\
%     \verb|lineheight| & Height of the separation line\\
% \end{tabular}
%\end{center}
%
% Additionally, all the options mentioned in the previous section can be set with \lstinline|\mindflowset|.
%
% \section{An example}
% With the following settings:
% \iffalse
%<*example>
% \fi
\begin{code*}
\mindflowset{
    leftlinenumber, rightlinenumber,
    leftmarker, rightmarker,
    linecolor   = green!50!black,
    textcolor   = blue!50!cyan,
    numcolor    = red!50!orange,
    markercolor = orange,
    textfont    = \footnotesize\sffamily\itshape,
    numfont     = \small\footnotesize\ttfamily,
    markerfont  = \footnotesize\sffamily,
    left        = \hspace{1em}\(\succ\),
    right       = \(\prec\)\hspace{1em},
    lineheight  = 1pt
}
\end{code*}
% \iffalse
%</example>
% \fi
% \vspace{-.5\baselineskip}
% One will have something like this:
% \begingroup
% \makeatletter
% \@mindflow@leftmarkertrue
% \@mindflow@rightlinenumbertrue
% \makeatother
% \colorlet{mindflowLine}{green!50!black}
% \colorlet{mindflowText}{blue!50!cyan}
% \colorlet{mindflowNum}{red!50!orange}
% \colorlet{mindflowMarker}{orange}
% \renewcommand{\mindflowTextFont}{\footnotesize\sffamily\itshape}
% \renewcommand{\mindflowNumFont}{\small\footnotesize\ttfamily}
% \renewcommand{\mindflowMarkerFont}{\footnotesize\sffamily}
% \renewcommand{\mindflowLeft}{\hspace{1em}\(\succ\)}
% \renewcommand{\mindflowRight}{\(\prec\)\hspace{1em}}
% \setlength{\mindflowLineHeight}{1pt}
% \begin{mindflow}
%     \blindtext
% \end{mindflow}
% \endgroup
%
% \section{About the line numbers}
% \linenumbers
%
% You can also add line numbers to the other part of your document in the usual
% way. For example, line numbers have been turned on for this paragraph with
% \lstinline|\linenumbers|.
%
% \begin{mindflow}
%   Line numbers within the \textsf{mindflow} environments are independent from
%   those of the main text.
% \end{mindflow}
% (\emph{I recommend that you put an empty line before and after the mindflow
% environments, i.e.~make the \emph{\textsf{mindflow}} environment a paragraph
% of its own. This way, your paragraph structure won't get changed when you use
% the} \verb|off|\emph{ option.})
%
% \nolinenumbers
%
% The feature of line numbering is provided by the package \textsf{lineno}. You
% can read its documentation to learn more about it. Notice that, by default,
% displayed equations are not numbered. You can add the following line before
% \lstinline|\usepackage{mindflow}|:
% \iffalse
%<*example>
% \fi
\begin{code*}
  \PassOptionsToPackage{mathlines}{lineno}
\end{code*}
% \iffalse
%</example>
% \fi
% \vspace{-.5\baselineskip}
% to enable the line numbers for displayed equations.
%
% \begin{mindflow}\vspace{-\baselineskip}
% \begin{align*}
%     (a + b)^2  &= a^2 + 2ab + b^2\\
%     (a + b)^3  &= a^3 + 3a^2b + 3ab^2 + b^3
% \end{align*}
% \end{mindflow}
%
% \clearpage
% \centerline{\itshape ---~~Readers who are not interested in technical details can skip the rest~~---}
%
% \section{Some \TeX{}nical details}
%
% \DescribeMacro{\mindflowTextFont}
% \DescribeMacro{\mindflowNumFont}
% \DescribeMacro{\mindflowMarkerFont}
% The font for text, line numbers and markers within the \verb|mindflow|
% environments can be specified by redefining \lstinline|\mindflowTextFont|,
% \lstinline|\mindflowNumFont| and \lstinline|\mindflowMarkerFont|. By default, 
% they are defined as:
% \iffalse
%<*example>
% \fi
\begin{code*}
\newcommand{\mindflowTextFont}{\footnotesize}
\newcommand{\mindflowNumFont}{\scriptsize\ttfamily}
\newcommand{\mindflowMarkerFont}{\scriptsize\ttfamily}
\end{code*}
% \iffalse
%</example>
% \fi
% \vspace{-.5\baselineskip}
%
% \DescribeMacro{\mindflowLeft}
% \DescribeMacro{\mindflowRight}
% The left and right markers is defined by
% \lstinline|\mindflowLeft| and \lstinline|\mindflowRight|. Both have the default
% value ``\verb|*|''.
%
% \DescribeMacro{\mindflowLineHeight}
% The height of the separation lines is a length macro named \lstinline|\mindflowLineHeight|, 
% with default value \verb|0.4pt|. You can change this with \lstinline|\setlength|.
%
% \DescribeMacro{mindflowLine}
% \DescribeMacro{mindflowText}
% \DescribeMacro{mindflowNum}
% \DescribeMacro{mindflowMarker}
% And finally, the colors for the separation lines, text, line numbers and
% markers within the \verb|mindflow| environments are called
% \verb|mindflowLine|, \verb|mindflowText|, \verb|mindflowNum| and
% \verb|mindflowMarker|, respectively. By default, they have the same color as
% the context, with opacity 40\% for the separation lines and text, 20\% for 
% the markers, and 8\% for the line numbers. 
%
% \section{The same example}
% With option \verb|leftlinenumber, rightlinenumber, leftmarker, rightmarker| 
% and the following settings:
% \iffalse
%<*example>
% \fi
\begin{code*}
\colorlet{mindflowLine}{green!50!black}
\colorlet{mindflowText}{blue!50!cyan}
\colorlet{mindflowNum}{red!50!orange}
\colorlet{mindflowMarker}{orange}
\renewcommand{\mindflowTextFont}{\footnotesize\sffamily\itshape}
\renewcommand{\mindflowNumFont}{\small\footnotesize\ttfamily}
\renewcommand{\mindflowMarkerFont}{\footnotesize\sffamily}
\renewcommand{\mindflowLeft}{\hspace{1em}\(\succ\)}
\renewcommand{\mindflowRight}{\(\prec\)\hspace{1em}}
\setlength{\mindflowLineHeight}{1pt}
\end{code*}
% \iffalse
%</example>
% \fi
% \vspace{-.5\baselineskip}
% One will get the same result as in the previous example.
%
% \StopEventually{}
%
% \section{Implementation}
% Below is the complete source code of this package.
%
% \begin{code}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{%
    family = @mindflow,
    prefix = @mindflow@
}
\DeclareBoolOption[true]{on}               % Turn on mindflow
\DeclareBoolOption[false]{off}             % Turn off mindflow
\DeclareBoolOption[false]{leftmarker}      % Left marker
\DeclareBoolOption[false]{rightmarker}     % Right marker
\DeclareBoolOption[false]{linenumber}      % Left line numbers
\DeclareBoolOption[false]{leftlinenumber}  % Left line numbers
\DeclareBoolOption[false]{rightlinenumber} % Right line numbers
\DeclareBoolOption[false]{twocolumn}       % Two column
\DeclareBoolOption[false]{incolumn}        % Separation line fits in column

\ProcessKeyvalOptions*\relax

\if@mindflow@on
  \@mindflow@offfalse
\fi
\if@mindflow@twocolumn
  \@mindflow@incolumntrue
\fi
\if@mindflow@linenumber
  \@mindflow@leftlinenumbertrue
\fi

%%================================
%% Initialization
%%================================
\RequirePackage{lineno}
\RequirePackage{xcolor}

\colorlet{mfSavedColor}{.}
\colorlet{mindflowLine}{mfSavedColor!40}
\colorlet{mindflowText}{mfSavedColor!40}
\colorlet{mindflowMarker}{mfSavedColor!20}
\colorlet{mindflowNum}{mfSavedColor!8}

\newcommand{\mindflowTextFont}{\footnotesize}
\newcommand{\mindflowNumFont}{\scriptsize\ttfamily}
\newcommand{\mindflowMarkerFont}{\scriptsize\ttfamily}
\newcommand{\mindflowLeft}{*}
\newcommand{\mindflowRight}{*}
\newlength{\mindflowLineHeight}
\setlength{\mindflowLineHeight}{0.4pt}

%%================================
%% The mindflow environment
%%================================
\PassOptionsToPackage{all}{nowidow}
\RequirePackage{nowidow}
\RequirePackage{verbatim}

\newif\ifLNturnsON
\def\LocallyStopLineNumbers{\LNturnsONfalse%
    \ifLineNumbers\LNturnsONtrue\fi\nolinenumbers}
\def\ResumeLineNumbers{\ifLNturnsON\linenumbers\fi}

\newcounter{recordLN}
\newcounter{mfLN}
\setcounter{mfLN}{1}

\newcommand*{\mfSepLine}{%
  \par%\nobreak%
  \LocallyStopLineNumbers%
  \vspace*{-.5\baselineskip}%
  \if@mindflow@incolumn%
    \makebox[\linewidth]{\color{mindflowLine}\rule{\linewidth}{\mindflowLineHeight}}%
  \else%
    \hspace*{-\paperwidth}%
    \makebox[\linewidth]{\color{mindflowLine}\rule{4\paperwidth}{\mindflowLineHeight}}%
  \fi%
  \par%\nobreak%
  \ResumeLineNumbers%
}

\newcommand{\mindflow@makeLineNumber}{%
  \hss%
  \if@mindflow@leftlinenumber%
    \normalfont\mindflowNumFont\color{mindflowNum}\LineNumber\hspace{1em}%
  \fi%
  \color{mindflowMarker}%
  \if@mindflow@leftmarker%
    \normalfont\mindflowMarkerFont\mindflowLeft\hspace{1em}%
  \fi%
  \rlap{\hskip\textwidth%
    \if@mindflow@rightmarker%
      \hspace{1em}\mindflowRight%
    \fi%
    \if@mindflow@rightlinenumber%
      \hspace{1em}%
      \normalfont\mindflowNumFont\color{mindflowNum}\LineNumber%
    \fi%
  }%
}%

\let\mindflowOFF=\comment
\let\endmindflowOFF=\endcomment

\def\mindflowON{%
  \postdisplaypenalty=10000
  \parskip=0pt
  \medskip%
  \setcounter{recordLN}{\value{linenumber}}%
  \setcounter{linenumber}{\value{mfLN}}%
  \mfSepLine%
  \normalfont\mindflowTextFont\color{mindflowText}%
  \linenumbers%
  \let\makeLineNumber\mindflow@makeLineNumber%
  \nopagebreak%
}
\def\endmindflowON{%
  \nowidow[3]%
  \mfSepLine%
  \setcounter{mfLN}{\value{linenumber}}%
  \setcounter{linenumber}{\value{recordLN}}%
  \medskip%
}

\if@mindflow@off
  \let\mindflow=\mindflowOFF
  \let\endmindflow=\endmindflowOFF
\else
  \let\mindflow=\mindflowON
  \let\endmindflow=\endmindflowON
\fi

%%================================
%% \mindflowset
%%================================
\define@key{mindflow}{on}[true]{%
  \csname @mindflow@on#1\endcsname%
  \if@mindflow@off%
    \let\mindflow=\mindflowOFF%
    \let\endmindflow=\endmindflowOFF%
  \else%
    \let\mindflow=\mindflowON%
    \let\endmindflow=\endmindflowON%
  \fi%
}
\define@key{mindflow}{off}[true]{%
  \csname @mindflow@off#1\endcsname%
  \if@mindflow@off%
    \let\mindflow=\mindflowOFF%
    \let\endmindflow=\endmindflowOFF%
  \else%
    \let\mindflow=\mindflowON%
    \let\endmindflow=\endmindflowON%
  \fi%
}
\define@key{mindflow}{leftmarker}[true]{\csname @mindflow@leftmarker#1\endcsname}
\define@key{mindflow}{rightmarker}[true]{\csname @mindflow@rightmarker#1\endcsname}
\define@key{mindflow}{linenumber}[true]{\csname @mindflow@linenumber#1\endcsname}
\define@key{mindflow}{leftlinenumber}[true]{\csname @mindflow@leftlinenumber#1\endcsname}
\define@key{mindflow}{rightlinenumber}[true]{\csname @mindflow@rightlinenumber#1\endcsname}
\define@key{mindflow}{twocolumn}[true]{\csname @mindflow@twocolumn#1\endcsname}
\define@key{mindflow}{incolumn}[true]{\csname @mindflow@incolumn#1\endcsname}
\define@key{mindflow}{linecolor}{\colorlet{mindflowLine}{#1}}
\define@key{mindflow}{textcolor}{\colorlet{mindflowText}{#1}}
\define@key{mindflow}{numcolor}{\colorlet{mindflowNum}{#1}}
\define@key{mindflow}{markercolor}{\colorlet{mindflowMarker}{#1}}
\define@key{mindflow}{textfont}{\renewcommand{\mindflowTextFont}{#1}}
\define@key{mindflow}{numfont}{\renewcommand{\mindflowNumFont}{#1}}
\define@key{mindflow}{markerfont}{\renewcommand{\mindflowMarkerFont}{#1}}
\define@key{mindflow}{left}{\renewcommand{\mindflowLeft}{#1}}
\define@key{mindflow}{right}{\renewcommand{\mindflowRight}{#1}}
\define@key{mindflow}{lineheight}{\setlength{\mindflowLineHeight}{#1}}
\newcommand{\mindflowset}[1]{%
  \setkeys{mindflow}{#1}%
  \if@mindflow@twocolumn%
    \@mindflow@incolumntrue%
  \fi%
  \if@mindflow@linenumber%
    \@mindflow@leftlinenumbertrue%
  \fi%
}

%%================================
%% lineno patch
%%================================
\ifdefined\linenomathpatch\else
  \RequirePackage{amsmath}
  \RequirePackage{etoolbox}
  \newcommand*\linenomathpatch[1]{%
    \cspreto{#1}{\linenomath}%
    \cspreto{#1*}{\linenomath}%
    \csappto{end#1}{\endlinenomath}%
    \csappto{end#1*}{\endlinenomath}%
  }
  \newcommand*\linenomathpatchAMS[1]{%
    \cspreto{#1}{\linenomathAMS}%
    \cspreto{#1*}{\linenomathAMS}%
    \csappto{end#1}{\endlinenomath}%
    \csappto{end#1*}{\endlinenomath}%
  }
  \expandafter\ifx\linenomath\linenomathWithnumbers
    \let\linenomathAMS\linenomathWithnumbers
    \patchcmd\linenomathAMS{\advance\postdisplaypenalty\linenopenalty}{}{}{}
  \else
    \let\linenomathAMS\linenomathNonumbers
  \fi
  \linenomathpatch{equation}
  \linenomathpatchAMS{gather}
  \linenomathpatchAMS{multline}
  \linenomathpatchAMS{align}
  \linenomathpatchAMS{alignat}
  \linenomathpatchAMS{flalign}
\fi
% \end{code}
%
% \Finale
\endinput
