\ProvidesPackage{dlfltxbmarkup}[2008/08/05 v0.70 by daleif]

% This package be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% 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
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2003/12/01 or later.
%% 
%% This work has the LPPL maintenance status "maintained".
%% 
%% The Current Maintainer of this work is Lars Madsen (daleif@imf.au.dk).
%%



\@ifclassloaded{memoir}{}{%
  \PackageError{felinemarkup}{This package depends on several internal
    components of the memoir class and therefore need the memoir
    class}{Use the memoir class}
}



\RequirePackage{keyval}
\RequirePackage{ragged2e}


\DeclareOption{loadsampleconfig}{\AtEndOfPackage{
    %\InputIfFileExists{dlfltxbmarkup-sample.cfg}{}{}
    \IfFileExists{dlfltxbmarkupbookkeys.sty}{
      \RequirePackage{dlfltxbmarkupbookkeys}
    }{
      \PackageError{dlfltxbmarkup}{Sample configuration package
        'dlfltxbmarkupbookkeys' not found, option 'loadsampleconfig'
        is useless.}{}
    }
}}

\ProcessOptions\relax



% HISTORY
% <2007      this package dates back to at least 2004
% 2007/03/20 added \color{black} to the margin writing devices, since
%            color at a line boundary may extend out into the margin
% 2007/03/21 added inputenc keyword
% 2007/03/21 added counter keyword
% 2007/03/21 added length keyword
% 2007/03/22 added gauss keyword
% 2007/03/23 added tabs keyword
% 2007/03/23 added nbop keyword
% 2007/03/24 fixed a bug in tabs
% 2007/03/24 fixed loads more index errors
% 2007/04/27 added \hspace{0pt} to \MarkWMargin to activate automatic hyphenation
% 2007/04/30 added \feline@MWM@inner to get better source re-use
% 2007/05/05 fixed a margin placement problem by adding \leavevmode
%            in front of \checkoddpage in \feline@MWM@inner, fix
%            supplied by Peter Wilson, after I submitted a bug report
%            about it.
% 2007/05/10 started adding support for key explanations, planning to
%            make it possible to print a generated overview of the
%            regisered keys
% 2007/05/14 all keys for my book project have now been moved to
%            felinemarkup.cfg, which wil be loaded at the end of the
%            package, if the 'loadfelinemarkupconfig' package option
%            is given
% 2007/05/15 \feline@index@cmd renamed to \felineIndexCmd
% 2007/05/16 package renamed to dlfltxbmarkup
% 2008/01/10 added a strut inside the macro that writes in the margin, 
% 2008/10/30 'sample configuration' has been renamed



% this boolean is meant to be used for example inside constructions
% that will not permit the use of \marginpar, for example the
% environments 'framed' and 'shaded', then \MarkWMargin will use
% \sidepar instead of \marginpar

\@ifundefined{ifNoMarginparAvail}{%
  \newif\ifNoMarginparAvail
  \NoMarginparAvailfalse
}{}


\newcommand\felineMarginAdjustment{%
  \ifoddpage\RaggedRight\else\RaggedLeft\fi}


% typesets the material in the margin
% we use \checkoddpage (assuming the use of \strictpagechecktrue) to
% perform a rigid test on whether or not we are on an odd page
% \RaggedRight \RaggedLeft is used to get the test 'mashed' up against
% the text, and to permit hyphenation. If the material in #1 is using
% typewriter font, then it is #1's responsability to ensure that
% hyphenation is possible in that font
\newcommand\feline@MWM@inner[1]{%
  \parbox[t]{\marginparwidth}{%
    \leavevmode\checkoddpage% needs \leavevmode since \checkoddpage
                            % leaves a whatsit
%    \ifoddpage\RaggedRight\else\RaggedLeft\fi%
    \felineMarginAdjustment%
    {\slshape\scriptsize\strut\hspace{0pt}\color{black} #1\strut\par}}}

% this might not belong here, but we will leave it for now
\strictpagechecktrue%
\reversesideparfalse%

% the main macro to write in the margin with
\newcommand\felineWriteInMargin[1]{%
  \ifNoMarginparAvail%
    \sidepar{\feline@MWM@inner{#1}}%
  \else%
    \if@feline@forcesidepar%
      \sidepar{\feline@MWM@inner{#1}}%
    \else%
      \marginpar{\feline@MWM@inner{#1}}%
    \fi%
  \fi%
}%


% because of a possible babel use, and its active charecters (in
% danish, " is an active character, it seems that we cannot directly
% add automatic english hyphenation to \cs and \css, in most cases it
% can be added by hand to the macros specified in \FelineKeyGenerator
% see felinemarkup.cfg
\DeclareRobustCommand\cs[1]{%
  \textnormal{\texttt{\symbol{'134}#1}}}

\@ifpackagewith{babel}{english}{
  \DeclareRobustCommand\css[1]{%
    \textnormal{\texttt{%
        \protect\makebox[0pt][r]{\symbol{'134}}%
        \foreignlanguage{english}{\cs{\hyphenchar\font=`\-#1}}}}}
}{
  \DeclareRobustCommand\css[1]{%
    \textnormal{\texttt{%
        \protect\makebox[0pt][r]{\symbol{'134}}%
        {#1}}}}
}

% indexing commands, first just two alias macros
\newcommand\textitindex[1]{\textit{#1}}
\newcommand\textbfindex[1]{\textbf{#1}}
% then two different indexing macros, one for italic index entries and
% one for bold. we use them like this to avoid expansion problems (or
% at least try to)
\newcommand\itindex[1]{\index{#1|textitindex}}
\newcommand\bfindex[1]{\index{#1|textbfindex}}
% default indexing command, we change this using the idxit, idxbf keys
\newcommand\felineIndexCmd{\index}


% ---------------------------------------------------------------------------



% just initial definitions
\newcommand*\feline@margin[1]{\par margin: #1\par}
\newcommand*\feline@index[1]{\par index: #1\par}
\newcommand*\feline@text[1]{\par text: #1\par}

% just a shortcut, amsmath makes the same one
\let\@xp\expandafter

\define@key{FelineIndex}{empty}[]{%
  \renewcommand*\feline@margin[1]{\par margin: ##1\par}%
  \renewcommand*\feline@index[1]{\par index: ##1\par}%
  \renewcommand*\feline@text[1]{\par text: ##1\par}%
}

% whether or not to write the text part
\newif\if@feline@txt  \@feline@txttrue
% whether or not to write in the margin
\newif\if@feline@mrgn \@feline@mrgntrue
% whether or not to add to the index
\newif\if@feline@idx  \@feline@idxtrue
% force the use of sidepar instead of the usual marginpar
\newif\if@feline@forcesidepar
% whether or not to let \markup add a \hspace{0pt} in the text
% don't know it this is still needed
\newif\if@feline@hsp

% basically stolen from another package
\newcommand*\dlf@M@setbool[2]{%
  \csname #2\ifx\relax#1\relax true \else #1\fi\endcsname}

% switch off keys
\define@key{FelineIndex}{notxt} []{\@feline@txtfalse}  % = nowr
\define@key{FelineIndex}{nowr}  []{\@feline@txtfalse}  % no writing in text
\define@key{FelineIndex}{nomk}  []{\@feline@mrgnfalse} % no mark in margin
\define@key{FelineIndex}{noidx} []{\@feline@idxfalse}  % no adding index

\define@key{FelineIndex}{hsp}   []{\@feline@hsptrue}

% locally use a different index command
\define@key{FelineIndex}{idxit} []{\renewcommand\felineIndexCmd{\itindex}}
\define@key{FelineIndex}{idxbf} []{\renewcommand\felineIndexCmd{\bfindex}}
\define@key{FelineIndex}{idxn}  []{\renewcommand\felineIndexCmd{\index}}

% \sidepar's are non-floating objects so they does not move by them
% self, we need to help if needed, the argument should be a number
% (the unit will be \onelineskip)
\newlength\feline@spvs@addto
\setlength\feline@spvs@addto{0pt}
\define@key{FelineIndex}{addtospvrt}{\addtolength{\feline@spvs@addto}{#1\onelineskip}}
\define@key{FelineIndex}{vaddtosp}  {\addtolength{\feline@spvs@addto}{#1\onelineskip}}

% force the use of \sidepar, don't think I've ever used this
\define@key{FelineIndex}{forcesidepar}[true]{\dlf@M@setbool{#1}{@feline@forcesidepar}}

% by default this macro does nothing, the idea behind it is to use
% this in an external document to generate a list of key names and
% descriptions (using the first two arguments)
\providecommand\felineMarkupDescription[5]{}

% key generator macro, has the following arguments
% #1: name of the key
% #2: description of what this key is for
% #3: code for margin command
% #4: code for index command
% #5: code for text command
% and does the following:
% * creates \feline@margin@<key>, \feline@index@<key> and
%   \feline@text@<key>
% * the \define@key part then performs three \let's:
%   \let\feline@margin\feline@margin@<key> etc.
% when using \FelineKeyGenerator, #1 refers to the main argument given
% to \markup
\newcommand\felineKeyGenerator[5]{%
  \felineMarkupDescription{#1}{#2}{#3}{#4}{#5}%
  \global\define@key{FelineIndex}{#1}[]{%
    \global\@xp\@xp\@xp\let\@xp\@xp\@xp\feline@margin\@xp\csname feline@margin@#1\endcsname%
    \global\@xp\@xp\@xp\let\@xp\@xp\@xp\feline@index\@xp\csname feline@index@#1\endcsname%
    \global\@xp\@xp\@xp\let\@xp\@xp\@xp\feline@text\@xp\csname feline@text@#1\endcsname%
  }%
  \@namedef{feline@margin@#1}##1{#3}%
  \@namedef{feline@index@#1}##1{#4}%
  \@namedef{feline@text@#1}##1{#5}%
}

% standard key er lavet ned i \setkeys, da \markup[notxt]{...} ellers ikke vil
% v�re det samme som \markup[macro,notxt]{...} 

% this is the main macro, take two arguments, a optional argument for
% key controls and the mandatory arguments for the think we want to
% markup 
% note the it starts by setting the \feline@std@key which will be the
% stadard key used if the optional argument is empty or the other keys
% does not alter the \feline@margin, \feline@text and \feline@index
\newcommand\markup[2][]{%
%  \@bsphack
  \begingroup%
  \setkeys{FelineIndex}{\felineStandardKey}%
  \setkeys{FelineIndex}{#1}%
  \ifdim\feline@spvs@addto>0pt%
  \addtolength\sideparvshift{\feline@spvs@addto}%
  \fi%
  \if@feline@txt\feline@text{#2}\fi%
  \if@feline@idx\phantomsection\feline@index{#2}\fi%
  \if@feline@mrgn\if@feline@hsp\hspace{0pt}\fi\feline@margin{#2}\fi%
  \@feline@txttrue%
  \@feline@mrgntrue%
  \@feline@idxtrue%
  \endgroup%
%  \@esphack
}

% default generated key used by \markup, in the example setup 'macro'
% is used
\def\felineStandardKey{test}

% test key
\felineKeyGenerator{test}
  {test key, does not do anything useful}%
  {\felineWriteInMargin{#1}}%
  {\felineIndexCmd{#1}}%
  {\emph{#1}}



\InputIfFileExists{dlfltxbmarkup.cfg}{}{}




