\ProvidesFile{marginnote.sty}
\ProvidesPackage{marginnote}

\RequirePackage{ifvide}
\RequirePackage{amstext}

% Define new insertions
\newinsert\top@margin@note
\newinsert\bot@margin@note

% The maximal size of these insertions
\dimen\top@margin@note=0.2\vsize % or \mardimen
\dimen\bot@margin@note=0.2\vsize % or \mardimen

% I do not remenber what it is
\count\top@margin@note
\count\bot@margin@note

% I do not remenber what it is
\skip\bot@margin@note=0pt
\skip\top@margin@note=0pt

% New counters
\newcounter{topmarginnotenumber}
\renewcommand{\thetopmarginnotenumber}{\arabic{topmarginnotenumber}}
\newcounter{botmarginnotenumber}
\renewcommand{\thebotmarginnotenumber}{\arabic{botmarginnotenumber}}

% The user may define the font used in the notes,
\newcommand{\topmarginnotestyle}{\footnotesize}
\newcommand{\botmarginnotestyle}{\footnotesize}
% the space between two notes
\newcommand{\topmarginnoteskipamount}{\bigskipamount}
\newcommand{\botmarginnoteskipamount}{\bigskipamount}
% and the way the numbers are typeset.
\newcommand{\maketopmarginnotenumber}{%
  \ensuremath{{}^{[\thetopmarginnotenumber]}}}
\newcommand{\makebotmarginnotenumber}{%
  \ensuremath{{}^{(\thebotmarginnotenumber)}}}

% Il y a toutefois un « léger » problème : la commande \insert
% n'a aucun effet quand elle est entourée d'accolades 
% en mode mathématique (avec le mode mathématique seul ou les 
% accolades seules, ça marche).

\newcommand{\topmarginnote}[1]{%
  \stepcounter{topmarginnotenumber}%
  \maketopmarginnotenumber
  \insert\top@margin@note{%
    \vskip\topmarginnoteskipamount
    \noindent\parbox{\marginparwidth}{%
      \parindent=0pt
      \topmarginnotestyle
      \maketopmarginnotenumber
      \hspace{1em}%
      #1}}}

\newcommand{\botmarginnote}[1]{%
  \stepcounter{botmarginnotenumber}%
  \makebotmarginnotenumber
  \insert\bot@margin@note{%
    \vskip\botmarginnoteskipamount
    \noindent\parbox{\marginparwidth}{%
      \parindent=0pt
      \botmarginnotestyle
      \makebotmarginnotenumber
      \hspace{1em}%
      #1}}}

%% The knowledgeable user may wish to redefine (equivalents of) the 
%% \topmarginnote and \botmarginnote commands. See
%% bibliomarginnote.sty for an example. 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\@outputpage{%
\begingroup           % the \endgroup is put in by \aftergroup
  \let \protect \noexpand
  \@resetactivechars
  \let\-\@dischyph
  \let\'\@acci\let\`\@accii\let\=\@acciii
  \let\\\@normalcr
  \let\par\@@par          %% 15 Sep 87
  \shipout \vbox{%
    \set@typeset@protect
    \aftergroup \endgroup
    \aftergroup \set@typeset@protect
                                % correct? or just restore by ending
                                % the group?
  \if@specialpage
    \global\@specialpagefalse\@nameuse{ps@\@specialstyle}%
  \fi
  \if@twoside
    \ifodd\count\z@ \let\@thehead\@oddhead \let\@thefoot\@oddfoot
         \let\@themargin\oddsidemargin
    \else \let\@thehead\@evenhead
       \let\@thefoot\@evenfoot \let\@themargin\evensidemargin
    \fi
  \fi
  \reset@font 
  \normalsize
  \baselineskip\z@skip \lineskip\z@skip \lineskiplimit\z@
    \@begindvi
    \vskip \topmargin
    \moveright\@themargin \vbox {%
      \setbox\@tempboxa \vbox to\headheight{%
        \vfil
        \color@hbox
          \normalcolor
          \hb@xt@\textwidth {%
            \let \label \@gobble
            \let \index \@gobble
            \let \glossary \@gobble %% 21 Jun 91
            \@thehead
            }%
        \color@endbox
        }%                        %% 22 Feb 87
      \dp\@tempboxa \z@
      \box\@tempboxa
      \vskip \headsep
        %%%%%% \begin{MODIF} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      \rlap{\hbox{\hspace{\hsize}%
          \vbox to 0pt{%
            \ifvoid\top@margin@note\else
            \hbox{%
              \hspace{\marginparsep}%
              \hspace{-3.2pt}\vrule width 0.4pt\hspace{2.8pt}%
              \vbox{%
                \vskip -\topmarginnoteskipamount
                \box\top@margin@note}}%
            \fi
            \vss
            }}}%
        %%%%%% \end{MODIF} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      \hbox{\fbox{\box\@outputbox}}%
      %
        %%%%%% \begin{MODIF} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      \rlap{\hbox{\hspace{\hsize}%
          \vbox to 0pt{%
            \vss
            \ifvoid\bot@margin@note\else
            \hbox{%
              \hspace{\marginparsep}%
              \hspace{-3.2pt}\vrule width 0.4pt\hspace{2.8pt}%
              \vbox{%
                \vskip -\botmarginnoteskipamount
                \box\bot@margin@note}}%
            \fi
            }}}%
        %%%%%% \end{MODIF} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %
      \baselineskip \footskip
      \color@hbox
        \normalcolor
        \hb@xt@\textwidth{%
          \let \label \@gobble
          \let \index \@gobble      %% 22 Feb 87
          \let \glossary \@gobble   %% 21 Jun 91
          \@thefoot
          }%
      \color@endbox
      }%
    }%
\global \@colht \textheight
\stepcounter{page}%
\let\firstmark\botmark
}


