\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{kbexo}[2020/09/20 private exo macros]

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{kb}}

\newif\ifexopar
\exoparfalse
\DeclareOption{exopar}{\exopartrue}{}

\newif\ifnotheo
\notheotrue
\DeclareOption{theo}{\notheofalse}{}

\DeclareOption{english}{\AtBeginDocument{\selectlanguage{english}}}

\ProcessOptions
\usepackage{kb, a4wide}

\ifnotheo
  \def\thetheo{\arabic{theo}}
  \def\thecoro{\arabic{coro}}
  \def\thealgo{\arabic{algo}}
\fi

\def\ANNEE{2022--2023}
\def\my@university{Université de Bordeaux}
\def\my@subject{Mathématiques}

\def\university#1{\gdef\my@university{#1}}
\def\course#1{\gdef\my@course{#1}}
\def\formation#1{\gdef\my@subject{#1}}
\def\week#1{\gdef\my@week{#1}}
\def\num#1{\gdef\my@num{#1}}

\def\ExoName{Exercice}
\def\ProblemName{Problème}
\def\ExoTitleName{FEUILLE D'EXERCICES}
\@ifpackagewith{kbexo}{english}{
  \def\ExoName{Exercise}
  \def\ProblemName{Problem}
  \def\ExoTitleName{EXERCISES, SESSION}
}{}

\def\chapeauagreg{\par\medskip\noindent{\small\emph{Il est rappelé que le jury n'exige
pas une compréhension exhaustive du texte. Vous êtes laissé(e) libre
d'organiser votre discussion comme vous l'entendez. Il vous est conseillé de
mettre en lumière vos connaissances à partir du fil conducteur constitué par le
texte. Le jury demande que la discussion soit accompagnée d'exemples traités
sur ordinateur. Il est souhaitable que vous organisiez votre présentation comme
si le jury n'avait pas connaissance du texte. Le jury aura néanmoins le texte
sous les yeux pendant votre exposé.}}\par}

\def\Maple{{\sc Sage}\xspace}

\def\hint#1{[{\it#1\/}]}
\def\pmat#1{\begin{pmatrix}#1\end{pmatrix}}

\def\initQpb{%
  \gdef\theQ{{\alph{Q}}}
  \gdef\theqq{{\roman{qq}}}
}
\def\initQexo{%
 \gdef\theQ {{\arabic{Q}}}
 \gdef\theqq{{\alph{qq}}}
}
\newcounter{pb}[section] \def\thepb{{\Roman{pb}}} \def\theexo{{\arabic{pb}}}
\newcounter {Q}[section]
\newcounter{qq}[section] \initQexo
\newif\ifsol%\solfalse
\newif\ifeno%\enofalse

\def\inputeno#1{\setcounter{page}{1}\thispagestyle{firstpage}%
  \enotrue\solfalse\begingroup\enotitle%
  \input #1\endgroup}
\def\inputenoCONT#1{\newpage\enotrue\solfalse\begingroup\input #1\endgroup}
\def\inputsol#1{\ifeno\newpage\fi%
  \setcounter{page}{1}\thispagestyle{firstpage}%
  \soltrue\enofalse\begingroup\soltitle%
  \input #1\endgroup}
\def\inputsolCONT#1{\newpage\enofalse\soltrue\begingroup\input #1\endgroup}

%% Problem
\def\pb{\stepcounter{pb}%
\setcounter{Q}{0}\setcounter{qq}{0}%
\gdef\@currentlabel{\thepb}%
\@ifnextchar[{\@ppb}{\@pb{}}
}

\def \@ppb[#1]{\@pb{\quad(#1)}} % Problem \thepb (#1)

\def\setlabel#1{\gdef\@currentlabel{#1}}
\def\pbinit{}
\def \@pb#1{\def\pbinit{\thepb.}%
\par \vskip\pbskip\goodbreak
\centerline{\mypb #1}\nobreak\smallskip\nobreak\noindent\ignorespaces}
% EndProblem

% Q = Question
\def \Q{\stepcounter{Q}%
\setcounter{qq}{0}%
\gdef\@currentlabel{\pbinit\theQ}%
\@ifnextchar[{\@ql}{\@ifnextchar*{\@qstar}{\@q{}}}}

\def \@q#1{\smallbreak\vskip\Qskip
\noindent{#1}{\bf\theQ)}\ \ignorespaces}

\gdef\@ql[#1]{\@q{}\label{\string q:#1}} % \Q + crossref to q:#1

\gdef\@qstar*{\@ifnextchar*{\@qstarstar}{\@q{\@lstar}}}

\gdef\@qstarstar*{\@q{\llap{$\bigstar\bigstar$\ \,}}}
% EndQ

% qq = sub-Question
\def\qq{\stepcounter{qq}%
\gdef\@currentlabel{\pbinit\theQ.\theqq}%
\@ifnextchar*{\@qqstar}{\@qq}}
\def\@qq{\par\vskip 0pt plus 1pt \theqq)\ \ignorespaces}
\def\@qqstar*{\smallbreak\noindent{\@lstar\indent\theqq)\ \ignorespaces}}

% \Q + \qq: 1) a) 
\def\Qqq{\stepcounter{Q}\setcounter{qq}{1}%
\par\medskip\noindent
\gdef\@currentlabel{\pbinit\theQ.\theqq}%
\hbox to\parindent{\bf\theQ)\hfil}\theqq)\ \ignorespaces}

%% Exercice
\def\enotitle{\relax}
\def\soltitle{\relax}

\def\line#1{\hbox to\hsize{#1}}
% univ    ....  
% matière ....  annee
%    ...   #1   ...      #1 = titre
\def\@title#1{
  \line{\my@university\hfil \textbf{\my@course}}
  \line{\my@subject\hfil \ANNEE}
  \leavevmode\vskip 0pt plus 10pt
  \line{\hfil#1\hfil}
}
\def\simpletitle#1{\@title{\large\textbf{#1}}}

\def\agregtitle#1{
  \course{Préparation à l'agrégation}
  \simpletitle{#1}\chapeauagreg}

\def\@@exotitle{
  \@title{
    \textbf{\ExoTitleName~\no$\my@num$}
    \ifsol\hint{Correction}\fi
  }
}
\def\@exotitle[#1] {\@@exotitle{}\par\vskip 5pt plus 5pt%
\centerline{\textbf{#1}}\par\vskip 10pt plus 5pt}
\def\exotitle{\define@exos% 
  \@ifnextchar [ {\@exotitle}{\@@exotitle} %]
}

\def\mapletitle#1{\define@exos%
  \line{\my@university\hfil \textbf{\my@course}}
  \line{\my@subject\hfil \ANNEE}
  \line{\hfil \Maple/ \my@num}
  \line{\hfil\bf\large #1\ifsol\quad ({\bf Correction})\fi\hfil}
}

\def\pbmtitleMAIN{\define@exos%
   \setcounter{pb}{0}
   \setcounter{Q}{0}
   \@title{{\bf DEVOIR \no$\my@num$}\quad
      \ifsol({\bf Correction})\else
            (pour la semaine du \my@week)\fi}}
\def\pbmtitle{\def\enotitle{\pbmtitleMAIN}
              \def\soltitle{\pbmtitleMAIN}}

\def\pbtitle#1{\define@exos%
   \@title{{\bf #1} \ifsol\quad({\bf Correction}) \fi}}

\def\define@exos{\ifsol
  \setcounter{pb}{0}
  % corrected exercise, print correction
  \long\gdef\exocor##1\cor##2\endexo{% gobble #1, print \cor #2 \endexo
    \stepcounter{pb}%
    \setcounter{Q}{0}%
    \@exo{} ##1 \par\medskip\noindent\textbf{\slshape Correction.}
      \setcounter{Q}{0} ##2}

  % uncorrected, ignore
  \long\gdef\exo##1\endexo{\stepcounter{pb}}
\else
  \gdef\exo{%
    \stepcounter{pb}%
    \setcounter{Q}{0}%
    \@ifnextchar [ {\@exoname}%
    {\@ifnextchar * {\@exostar}%
    {\@exo{}}}}
  \let\exocor=\exo
  \long\gdef\cor##1\endexo{}
  \def\endexo{}
\fi}

\def\kb@empty{}
\def\myexo{{\ifx\theexo\kb@empty%
\bf\ExoName\else%
\bf\ExoName~\theexo\fi}}

\def\mypb{{\ifx\thepb\kb@empty%
\bf\ProblemName\else%
\bf\ProblemName~\thepb\fi}}

\def\exoskip{3pt plus 10pt minus 1pt}
\def\Qskip{0pt plus 3pt}
\def\pbskip{3pt plus 10pt}
\def\@exo#1{%
  \gdef\@currentlabel{\theexo}%
\medbreak\vskip\exoskip\noindent{#1}\myexo~--%
\ifexopar{\par\noindent\ignorespaces}\else\ \nobreak\fi}
\def\@@exo#1#2{\@exo{#2}[{\sc #1}]\par\noindent\ignorespaces}

\def\@lstar{\llap{$\bigstar$\quad}}
\def\@exostar*{\@ifnextchar[ {\@exostarname} {\@exo{\@lstar}}}
\def\@exoname[#1]    {\@@exo{#1}{}}
\def\@exostarname[#1]{\@@exo{#1}{\@lstar}}
\newenvironment{mynote}{\small\misctitle{Note}}

\newenvironment{noteSS}{\small\smallskip%
\noindent\llap{$\bigstar\bigstar$~}{\bf Note~:}}

\def\pow{\^{}\hskip0pt}
\def\til{{\raise-0.3em\hbox{\~{}}}}
\def\kbd#1{\texttt{#1}}
\def\var#1{\textit{#1}}

% verbatim mode, leave alone $ and _
% @ made active: assume verbatim text doesn't contain it
\newif\ifnopar
\let\sharp\#
{\catcode`\^=13\global\let^=\pow\obeyspaces\global\let \ }
\def\ttverb{%
  \nopartrue
  \catcode`\\=12%
  \catcode`\{=12%
  \catcode`\}=12%
  \catcode`\&=12%
  \catcode`\#=12%
  \catcode`\%=12%
  \catcode`\:=12%
  \catcode`\^=13%
  \catcode`\~=13\def~{{\til}}%
  \catcode`\@=0%
  \def\par{\futurelet\next\dopars}%
  \def\dopars{%
    \ifnopar \noparfalse%
    \else% treat two consecutive \par specialy
      \ifx\next\par \vskip4pt plus 1pt\nopartrue%
      \else \leavevmode\endgraf\fi\fi}%
  \obeyspaces\obeylines\tt}

% back to normalcy
\def\unverb{%
\catcode`\\=0%
\catcode`\{=1%
\catcode`\}=2%
\catcode`\$=3%
\catcode`\&=4%
\catcode`\#=6%
\catcode`\^=7%
\catcode`\_=8%
\catcode`\^^I=10}

\def\bprogpart{\begingroup%
  \vskip 0pt plus 1pt%
  \leavevmode\parskip=0pt plus 1pt%
  \interlinepenalty2000\clubpenalty9000\widowpenalty9000%
  \ttverb}

% complete verbatim (including _ and $)
\def\bprogfile#1{\bprog\input#1\eprog}
\def\bprog{\bprogpart\initcor\catcode`\_=12\catcode`\$=12}
\def\eprog{\endgroup\par}
% poor man's verbatim
\def\Bprog{\begingroup\tt\obeylines\everypar{\quad}}
\let\Eprog=\eprog
\def\initcor{\def\#{\begingroup\unverb\comstart}}
{\obeylines
% \gdef\com{\begingroup\unverb\comstart}
  \gdef\comstart#1^^M{\it#1\endgroup
} % newline after @endgroup is important
}
% comments
\def\Ccom{\begingroup\unverb\Ccomstart}
\def\Ccomstart#1*/{\rm#1\endgroup*/}
\define@exos % default definition, possibly overridden by \exotitle
\catcode`\@=12

\newenvironment{mapleblock}
  {\par\smallskip\noindent\tt$\left|\begin{tabular}{ll}> }
  {\end{tabular}\right.$\medskip}

% Some silly abbreviations [ personal... ]
\def\L{\mathcal{L}}
\def\F{\mathcal{F}}
\def\pt{\noindent$\bullet$ }
\def\q{\quad}
\def\e{\varepsilon}
\def\z{\zeta}
\def\phi{\varphi}

\usepackage[noend]{algorithm, algorithmic}
\def\thealgorithm{\arabic{algorithm}.}
\def\algorithmiccomment#1{\quad\{\emph{#1}\}}

\def\R{\mathbb{R}}
\newcommand\danger{%
 \makebox[1.4em][c]{%
 \makebox[0pt][c]{\raisebox{.1em}{\small!}}%
 \makebox[0pt][c]{\color{red}\Large$\bigtriangleup$}}}%
