%%
%% This is file `marginfix.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% marginfix.dtx  (with options: `package')
%% 
%% IMPORTANT NOTICE:
%% 
%% For the copyright see the source file.
%% 
%% Any modified versions of this file must be renamed
%% with new filenames distinct from marginfix.sty.
%% 
%% For distribution of the original source see the terms
%% for copying and modification in the file marginfix.dtx.
%% 
%% This generated file may be distributed as long as the
%% original source files, as listed above, are part of the
%% same distribution. (The sources need not necessarily be
%% in the same archive or directory.)
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{marginfix}%
           [2010/08/28 v0.9.1 Fix Margin Paragraphs]
\makeatletter
\chardef\@ight=8
\chardef\f@ur=4
\chardef\@c=100
\newif\ifmfx@ypos
\DeclareOption{ypos}{\mfx@ypostrue}
\ProcessOptions\relax
\let\mfx@marginlist\@empty
\newbox\Mfx@marginbox
\newdimen\Mfx@marginboxspace
\newdimen\Mfx@marginpos
\newdimen\Mfx@marginheight
\newdimen\Mfx@mparshift
\newdimen\marginheightadjustment
\newdimen\marginposadjustment
\def\@addmarginpar{%
  \@next\@marbox\@currlist{}\MFX@bug
  \MFX@getypos
  \MFX@cons\mfx@marginlist{% TODO: later this will be a run@marginlist
    \noexpand\mfx@margin@note\@marbox\@currbox{\mfx@ypos}% (^i.e. for phantoms)
    \noexpand\mfx@margin@skip{\the\marginparpush}%
  }%
}
\def\MFX@cons#1#2{%
  \edef\temp@{#2}%
  \expandafter\expandafter\expandafter\gdef
  \expandafter\expandafter\expandafter#1%
  \expandafter\expandafter\expandafter{\expandafter#1\temp@}%
}

\def\MFX@snoc#1#2{%
  \edef\temp@{#2}%
  \expandafter\expandafter\expandafter\gdef
  \expandafter\expandafter\expandafter#1%
  \expandafter\expandafter\expandafter{\expandafter\temp@#1}%
}
\def\MFX@getypos{%
  \edef\mfx@ypos{%
    \the\dimexpr\@pageht - \Mfx@strutheight
       + \marginposadjustment + \Mfx@mparshift\relax}%
  \global\Mfx@mparshift\z@
}
\newdimen\Mfx@strutheight
\edef\marginpar{%
  \unexpanded{\setbox\@tempboxa\hbox{\strut}\Mfx@strutheight\ht\@tempboxa}%
  \expandafter\unexpanded\expandafter{\marginpar}%
}
\expandafter\def\expandafter\@combinefloats\expandafter{\expandafter
  \MFX@combinefloats@before\@combinefloats}
\def\MFX@combinefloats@before{%
  \MFX@buildmargin
  \MFX@attachmargin
  \Mfx@marginheight\marginheightadjustment
}
\def\MFX@attachmargin{%
  \ifdim\ht\@outputbox<\ht\Mfx@marginbox
    \setbox\@outputbox\vbox to \ht\Mfx@marginbox{%
      \box\@outputbox
      \vfill
    }%
  \else
    \setbox\Mfx@marginbox\vbox to \ht\@outputbox{%
      \unvbox\Mfx@marginbox
      \vfill
    }%
  \fi
  \setbox\@outputbox\vbox to \ht\@outputbox{%
    \hbox to \wd\@outputbox{%
      \if\MFX@leftmargin
        \llap{\box\Mfx@marginbox\hskip\marginparsep}%
        \box\@outputbox
      \else
        \box\@outputbox
        \rlap{\hskip\marginparsep\box\Mfx@marginbox}%
      \fi
  }}%
}
\def\MFX@buildmargin{%
  \advance\Mfx@marginheight\@colroom
  \MFX@buildmargin@down
  \MFX@buildmargin@up
}
\def\MFX@buildmargin@down{%
  \let\mfx@margin@note\MFX@margin@note@down
  \let\mfx@margin@skip\@gobble
  \let\mfx@margin@clear\MFX@margin@clear@down
  \let\mfx@marginout\@empty
  \expandafter\global\expandafter\let
  \expandafter\mfx@marginlist\expandafter\@empty
  \mfx@marginlist
}
\def\MFX@margin@note@down#1#2#3{%
  \ifdim#3>\Mfx@marginpos
    \dimen@\dimexpr#3-\Mfx@marginpos\relax
    \MFX@snoc\mfx@marginout{\noexpand\mfx@margin@compressible{\the\dimen@}}%
    \advance\Mfx@marginpos\the\dimen@
  \fi
  \MFX@whichbox#1#2%
  \advance\Mfx@marginboxspace\dimexpr\ht\@marbox+\dp\@marbox\relax
  \ifdim\Mfx@marginboxspace>\Mfx@marginheight
    \advance\Mfx@marginboxspace\dimexpr-\ht\@marbox-\dp\@marbox\relax
    \mfx@margin@clear
    \mfx@margin@note#1#2{#3}%
  \else
    \advance\Mfx@marginpos\dimexpr\ht\@marbox+\dp\@marbox\relax
    \MFX@snoc\mfx@marginout{%
      \noexpand\mfx@margin@note\@marbox
      \noexpand\@cons\noexpand\@freelist#1%
      \noexpand\@cons\noexpand\@freelist#2%
    }%
  \fi
  \let\mfx@margin@skip\MFX@margin@skip@down
}
\def\MFX@whichbox#1#2{%
  \if\MFX@leftmargin
    \def\@marbox{#1}%
  \else
    \def\@marbox{#2}%
  \fi
}
\def\MFX@margin@skip@down#1{%
  \advance\Mfx@marginpos#1\relax
  \advance\Mfx@marginboxspace#1\relax
  \MFX@snoc\mfx@marginout{\noexpand\mfx@margin@skip{#1}}%
}
\def\MFX@margin@clear@down{%
  \def\mfx@margin@note##1##2##3{%
    \MFX@cons\mfx@marginlist{\noexpand\mfx@margin@note##1##2{\MFX@minus@inf}}}%
  \def\mfx@margin@skip##1{%
    \MFX@cons\mfx@marginlist{\noexpand\mfx@margin@skip{##1}}}%
  \def\mfx@margin@clear{%
    \MFX@cons\mfx@marginlist{\noexpand\mfx@margin@clear}}%
}
\def\MFX@minus@inf{-4000pt}
\def\MFX@leftmargin{%
  00\fi % close out the \if
  \@tempcnta\@ne
  \if@mparswitch
    \unless\ifodd\c@page
      \@tempcnta\m@ne
    \fi
  \fi
  \if@reversemargin
    \@tempcnta-\@tempcnta
  \fi
  \ifnum\@tempcnta<\z@ % start a new \if
}
\def\MFX@buildmargin@up{%
  \let\mfx@margin@note\MFX@margin@note@up
  \let\mfx@margin@compressible\MFX@margin@skip@gobble@up
  \let\mfx@margin@skip\MFX@margin@skip@gobble@up
  \mfx@marginout
}
\def\MFX@margin@skip@gobble@up#1{%
  \advance\Mfx@marginpos-#1\relax
}
\def\MFX@margin@note@up#1{%
  \setbox\Mfx@marginbox\vbox{\box#1\unvbox\Mfx@marginbox}%
  \let\mfx@margin@skip\MFX@margin@skip@up
  \let\mfx@margin@compressible\MFX@margin@compressible@up
}
\def\MFX@margin@skip@up#1{%
  \setbox\Mfx@marginbox\vbox{\vskip#1\relax\unvbox\Mfx@marginbox}%
}
\def\MFX@margin@compressible@up#1{%
  \dimen@#1\relax
  \ifdim\Mfx@marginpos>\Mfx@marginheight
    \advance\dimen@\dimexpr\Mfx@marginheight-\Mfx@marginpos\relax
    \ifdim\dimen@<\z@
      \dimen@\z@
    \fi
    \advance\Mfx@marginpos\dimexpr\dimen@-#1\relax
  \fi
  \ifdim\dimen@>\z@
    \MFX@margin@skip@up\dimen@
  \fi
}
\def\dumpmargins{%
  \loop
  \unless\ifx\mfx@marginlist\@empty
    \let\temp@\mfx@marginlist
    \vbox{}\clearpage
    \ifx\temp@\mfx@marginlist
      \PackageError{marginfix}{lost some margin notes%
      }\@eha
      \let\mfx@marginlist\@empty % be nicer by just dropping one?
      % TODO: also, set an emergency mode to allow oversized notes
    \fi
  \repeat
}
\AtEndDocument{\dumpmargins}
\def\marginskip#1{%
  \MFX@cons\mfx@marginlist{\noexpand\mfx@margin@skip{#1}}%
}
\def\clearmargin{%
  \MFX@cons\mfx@marginlist{\noexpand\mfx@margin@clear}%
}
\def\softclearmargin{%
  \marginskip{\the\textheight}%
}
\def\extendmargin#1{%
  \advance\Mfx@marginheight#1\relax
}
\def\mparshift#1{%
  \advance\Mfx@mparshift#1\relax
}
\makeatother
\endinput
%%
%% End of file `marginfix.sty'.
