%%
%% This is the original source file exercise.sty
%%
%% Package `exercise to use with LaTeX2e
%% Copyright Didier Remy, all rights reserved.
%%
%% See http://cristal.inria.fr/~remy/latex/
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%

%% Identification

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{exercise}
         [2001/23/02 v0.92 Exercise]

%% Preliminary declarations

\RequirePackage {keyval}

\newcommand \setanswer [1]{\setkeys{answer}{#1}}

\newif \ifin@exercise \in@exercisefalse
\newif \if@answers \@answerstrue
\newif \if@answer@anchor \@answer@anchortrue
\newif \if@answer@later \@answer@latertrue

\define@key {answer}{show}[]{\@answerstrue}
\define@key {answer}{hide}[]{\@answersfalse}
\define@key {answer}{inline}[]{\@answer@laterfalse}
\define@key {answer}{later}[]{\@answer@latertrue}
\define@key {answer}{anchor}[]{\@answer@anchortrue}
\define@key {answer}{noanchor}[]{\@answer@anchorfalse}

%% Options

\DeclareOption*{\edef \exo@answer@file{\CurrentOption}}
\DeclareOption{hide}{\@answersfalse}
\DeclareOption{now}{\@answer@laterfalse}
\DeclareOption{noanchor}{\answer@anchorfalse}
\ProcessOptions\relax

%% More declarations

\def \@beginexercisehook {}
\def \AtBeginExercise {\g@addto@macro \@beginexercisehook}
\AtBeginDocument {\@beginexercisehook}
\AtBeginExercise 
  {\@ifundefined {exo@filename}{\edef \exo@filename{\jobname}}{}}
\define@key {answer}{file}{\exo@set@filename {#1}}
\def \exo@set@filename #1{\def \exo@filename {#1}}
\@onlypreamble \exo@set@filename

\def \exo@empty{}
\@ifundefined {thechapter}
   {\let \exo@chapter \exo@empty}
   {\def \exo@chapter {\thechapter}}
\newcommand {\pdf@hyperlink}[2]
  {\@ifundefined {hyperlink}{#2}{\hyperlink {#1}{#2}}}
\newcommand {\pdf@hypertarget}[2]
  {\@ifundefined {hypertarget}{#2}{\hypertarget {#1}{#2}}}

\def \exo@francais{frenchb}
\ifx \exo@francais \languagename
  \def \ExerciseName {Exercice}
  \def \AnswerName {Réponse}
\else
  \def \ExerciseName {Exercise}
  \def \AnswerName {Answer}
\fi

\newcommand {\exo@SquareBox}[2][0.033em]
  {\hbox {\vbox {\hrule height #1\hbox to #2
         {\vrule width #1\vbox to #2{\vss}\hss \vrule width #1}\hrule
         height #1}}}

\AtBeginExercise
 {\newwrite \ans@channel
  \def \open@ans
      {\immediate \openout \ans@channel \exo@filename.ans\relax
       \global \let \open@ans \relax}}
%\openans
% \let \@exercise \exercise
% \def \exercise {\openans \global \let \exercise \@exercise\exercise}

%\def \ans@write #1{\xdef \ans@put {\write\ans@channel {#1}}\ans@put}
\def \ans@write #1{\open@ans \immediate \write\ans@channel {#1}}

\newtheorem{Exercise}{\pdf@hypertarget{Exo-\theExercise}{\ExerciseName}}
\newcounter{Question}

\let \exo@endparenv \@endparenv
\def \exo@no@endparenv {\let \@endparenv \exo@endparenv}
\def \exo@nobreakafter {\let \@endparenv \exo@no@endparenv}
\newenvironment {exercise}[1][]
    {\def \@test {#1}\ifx \@test \@empty
     \begin{Exercise}\else \begin{Exercise}[#1]\fi
     \in@exercisetrue
     \setcounter {Question}{0}%
     \let \nobreakafter \exo@nobreakafter
     \ans@write {\string
         \ExerciseNew {\@currentlabel}{\exo@chapter}{\thepage}{#1}}}
    {\ifvmode \vskip -\lastskip \nopagebreak \else
     \unskip \unpenalty \unskip \penalty 100\-\hbox{}\penalty 100\fi
     \hfill \hbox to 1em{\hfil \exo@SquareBox {0.5em}}\end{Exercise}}

\def \ans@lastlabel {}
\let \fragileanswer \ans@write

%% This environment can be redefined to customize immediate answers.
\newenvironment {AnswerNow}
   {\par \em \noindent {\underline {\AnswerName}:}}
   {\par \smallskip}

\def\@answer
  {\if@answers \ifin@exercise 
     \if@answer@later
        \em
        \stepcounter{Question}%
        \if@answer@anchor
           \@ifundefined {hyperlink}{}
              {\unskip \nobreak\hfil\penalty50\hskip 1em \hbox{}\nobreak
               \hskip 0em plus 1filll 
               \pdf@hyperlink
                  {Ans-\theExercise.\theQuestion}{\hbox {\AnswerName}}\nobreak
               \hskip 1em\penalty -10000}%
        \fi 
        \ifx \ans@lastlabel \@currentlabel
           \ans@write {\string \AnswerContinued
                         {\@currentlabel}{\theQuestion}{\thepage}}%
        \else
           \ans@write {\string \AnswerNew
                         {\@currentlabel}{\theQuestion}{\thepage}}%
           \global \edef \ans@lastlabel {\@currentlabel}%
        \fi
      \else
         \let \endanswer \endAnswerNow \let \ans@copytoend \AnswerNow
      \fi
   \fi \fi
   \ans@copytoend}
\let \answer \@answer  

\def \un@inpenc@loop#1#2{%
  \@tempcnta`#1\relax
  \loop
    \catcode\@tempcnta 12
  \ifnum\@tempcnta<`#2\relax
     \advance\@tempcnta\@ne
  \repeat}

  
%% To undo accents encodings, so as to be truely verbatim
\def\uninputencoding {\@ifundefined {inputencoding}{\catcode `\^^A 12}{%
    \un@inpenc@loop\^^A\^^H%
    \un@inpenc@loop\^^K\^^K%
    \un@inpenc@loop\^^N\^^_%
    \un@inpenc@loop\^^?\^^ff%
    }}

\def \exo@answer@env{answer}
\def \exo@next@end {\csname end\exo@answer@env\endcsname
                \expandafter \endgroup \ignorespaces}
\bgroup
\catcode `\|=0 \catcode `\{=12 \catcode `\}=12
\catcode `\[=1 \catcode `\]=2  \catcode`\\=12

|gdef |ans@dotest#1\end{#2}#3|END[%
      |def |@test [#1]|ifx |@test|empty
          |def |@env [#2]|ifx |@env |exo@answer@env 
             |let |exo@next |exo@next@end 
          |fi
      |fi
      |ifx |exo@next |exo@next@end |else
        |if@answers |ans@write [|exo@arg]|fi
      |fi]
|gdef |ans@testarg#1|END[|ans@dotest#1 |relax|relax\end{|relax}|END]
|egroup


\def\ans@copytoend
  {\uninputencoding
   \let \do \@makeother \dospecials \obeylines\ans@copy@first}
{\obeylines%
\gdef \ans@copy#1
  {\def\exo@arg{#1}\ans@testarg#1\END\exo@next}%
\gdef \ans@copy@first#1
  {\def \@test{#1}\let \exo@next \ans@copy%
   \ifx \@test \empty \else \def\exo@arg{#1}\ans@testarg#1\END\fi%
   \exo@next}%
}

\def \AnswerSection {\section}
\newcommand{\inputanswers}[2][\exo@filename]
 {\if@answers \if@answer@later
    \AnswerSection {#2}%
    \immediate \write \ans@channel {\string \endinput}%
    \immediate \closeout \ans@channel
%    \@input {\exo@filename .ans}%
    \bgroup
    \newread \ans@read
    \openin \ans@read \exo@filename.ans
       \ifeof \ans@read
          \message {*** Warning! No answers to exercices}\closein  \ans@read
       \else \closein \ans@read
       \message {Including answers to exercices}\input \exo@filename.ans
       \fi 
     \egroup
  \fi \fi}

\def \AnswerContinued #1#2#3{\egroup
  \bgroup
  \par \goodbreak \pdf@hypertarget {Ans-#1.#2}{}\nobreak
  \noindent\subsubsection* 
      {\pdf@hyperlink {Exo-#1}{\ExerciseName~#1 (continued)}}
}
\def \AnswerNew #1#2#3{\egroup
  \bgroup
  \par \goodbreak\pdf@hypertarget {Ans-#1.#2}{}\nobreak
  \noindent\subsection* 
      {\pdf@hyperlink {Exo-#1}{\ExerciseName~#1, page #3}}}
\newcommand {\ExerciseNew}[4]{\setcounter {Exercise}{#1}}


%%%% extension


% \newanswer{name}
%     {before-begin}{after-begin}
%     {fragile-insertion}
%     [number-of-args][default-value]{before-end}{after-end}

\newcommand {\NewQuestion}[2]
   {\setcounter {Exercise}{#1}\setcounter {Question}{#2}%
    \pdf@hypertarget {Ans-#1.#2}{}}
\def \AnswerLink #1{\pdf@hyperlink {Ans-\theExercise.\theQuestion}{#1}}
\def \ExerciseLink #1{\pdf@hyperlink {Exo-\theExercise}{#1}}

\newcommand {\newanswer}[6]
 {\newenvironment {exo-#1}{#2}{#3}\relax
  \newenvironment {now-#1}{#4}{#5}\relax
  \newenvironment {#1}[1]
      {\def \exo@answer@env {#1}%
       \setkeys{answer}{##1}%
       \if@answers 
         \if@answer@later
           \ifin@exercise
             \stepcounter{Question}%
             \ans@write
                  {\string \NewQuestion {\theExercise}{\theQuestion}}%
           \fi
           \ans@write{\string \begin{ans-#1}#6}%
           \csname exo-#1\endcsname
         \else
           \expandafter \let \expandafter \exo@let \csname endnow-#1\endcsname
           \expandafter \let \csname end#1\endcsname \exo@let
           \expandafter \let \expandafter \exo@let \csname now-#1\endcsname
           \let \ans@copytoend \exo@let
         \fi
       \fi 
       \ans@copytoend}
      {\csname endexo-#1\endcsname
       \if@answers \ans@write{\string \end{ans-#1}}\fi
      }\relax
  \newenvironment {ans-#1}}

\newcommand {\AnswerNiceLink}[1][\AnswerName]
    {\if@answer@anchor \@ifundefined {hyperlink}{}
         {\unskip \nobreak\hfil\penalty50\hskip 1em \hbox{}\nobreak
          \hskip 0em plus 1filll 
          \AnswerLink{\hbox {#1}}%
          \nobreak
          \hskip 1em\penalty -10000}\fi}

\newanswer {myanswer}
   {\em \AnswerNiceLink}
   {}
   {\AnswerNow}
   {\endAnswerNow}
   {{\thepage}}
   [1]
   {\ifnum \theQuestion=1\relax 
      \subsection* {\ExerciseLink {Exercise \theExercise, page #1}}%
    \else
      \subsubsection* 
         {\ExerciseLink {Exercice \theExercise\space (continued)}}%
    \fi}
   {}

%%% So as to make a list of all exercises

\let \exo@chap \exo@empty
\def \exo@lparen{(}
\def \exo@diff {\futurelet \exo@next\exo@@diff}
\def \exo@@diff
  {\ifx \exo@next (\let \@do \exo@@diff@ \else \let \@do \qquad\fi \@do}
\def \exo@@diff@ (#1){\rlap {#1}\qquad}

\newcommand {\ExerciseItem}[4]
  {\def \@test {#2}\ifx \@test \exo@chap\else
   \gdef \exo@chap {#2}\subsubsection*{Chapter \exo@chap}%
   \parindent 0em \fi
   \qquad \llap {\pdf@hyperlink{Exo-#1}{#1}}%
   \quad \exo@diff #4\par}

\newcommand{\ListOfExercises}[1][\chapter {\huge List of all exercises}]
   {\newpage
    \@mkboth {\MakeUppercase {List of all exercises}}
             {\MakeUppercase {List of all exercises}}
    \null
    \medskip
    #1
    \null

    \smallskip
    \bgroup 
      \let \ExerciseNew \ExerciseItem
      \parindent 0em
      \openin \ans@read \exo@filename.loe
      \ifeof \ans@read
        \message {*** Warning! No List of Exercices}\closein \ans@read
        \else \closein \ans@read
        \message {Including List Of Exercises}\input \exo@filename.loe
      \fi 
    \egroup}
