  %% draftinputlines.sty
  %% Copyright 2011 Seamus Bradley
  %
  % This work may 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 2005/12/01 or later.
  %
  % This work has the LPPL maintenance status `maintained'.
  % 
  % The Current Maintainer of this work is Seamus Bradley
  % tex@seamusbradley.net
  %
  % This work consists of the file draftinputlines.sty,
  % draftinputlines-man.tex and README;
  % and derived file draftinputlines-man.pdf

\ProvidesPackage{draftinputlines}[2011/08/15 v0.5]


%------------------------------
% Important packages
%------------------------------

\RequirePackage{currfile} % Loads filehook, which I also use...
\RequirePackage{etoolbox}
\RequirePackage{xkeyval}
\RequirePackage{marginnote}
\RequirePackage[excludeor]{everyhook}
\RequirePackage{graphicx}
%------------------------------
% New things
%------------------------------

\newcounter{firstline}
\newlength\mnotevadjust

\newcommand\dilmnote[1]{%
  \notmarginhookfalse%
  \marginnote{\dilfont #1}%
  \notmarginhooktrue%
}
\newcommand\dilrnote[1]{%
  \@reversemargintrue
  \dilmnote{#1}%
  \@reversemarginfalse
}

\newcommand\dilrotnote[1]{%
  \notmarginhookfalse
  \@reversemargintrue
  \marginnote{%
    \rotatebox{90}{\dilfont #1}%
  }
  \@reversemarginfalse
  \notmarginhooktrue
}
\newcommand\dilrotadjnote[1]{%
  \settowidth\mnotevadjust{#1}
  \notmarginhookfalse
  \@reversemargintrue
  \marginnote{%
    \rotatebox{90}{\dilfont #1}%
  }[-\mnotevadjust]
  \@reversemarginfalse
  \notmarginhooktrue
}

\def\dilfont{\small\ttfamily}

\newcommand\addlineno{\dilmnote{\the\inputlineno}}

\newif\ifnotmarginhook
\notmarginhooktrue

% Safe mode turns off paragraph hacking

\define@boolkey{dilfamily}{safemode}[false]{}

\define@boolkey{dilfamily}{hijackheaders}[true]{}
\define@boolkeys{dilfamily}{hijackfooters,hijackboth}[false]{}

%--------------------------------------------------
% Headers and footers
%--------------------------------------------------

\define@boolkeys{dilfamily}{scrpage,fancyhdr,memoir,plain}{}

%------------------------------
% Define some empty macros
%------------------------------
  \def\dil@insidehead{}
  \def\dil@outsidehead{}
  \def\dil@centerhead{}
  \def\dil@lefthead{}
  \def\dil@righthead{}
  \def\dil@offhead{}
  \def\dil@insidefoot{}
  \def\dil@outsidefoot{}
  \def\dil@centerfoot{}
  \def\dil@leftfoot{}
  \def\dil@rightfoot{}
  \def\dil@offfoot{}

%------------------------------
% Setup headers and footers
%------------------------------

\define@choicekey{dilfamily}{headfootpackage}{%
  scrpage,fancyhdr,memoir,plain}{%
  \setkeys{dilfamily}{#1=true}
}

%------------------------------
% The actual keys
%------------------------------
% I'm beginning to wonder whether supporting this much control is really worth it...

\define@choicekey{dilfamily}{inputlineshead}{%
  inside,outside,center,left,right,off}{%
  \AtEndPreamble{\csname dil@#1head\endcsname}%
}
\define@choicekey{dilfamily}{inputlinesfoot}{%
  inside,outside,center,left,right,off}{%
  \AtEndPreamble{\csname dil@#1foot\endcsname}%
}

\define@choicekey{dilfamily}{filenamehead}{%
  inside,outside,center,left,right,off}{%
  \AtEndPreamble{\csname dil@#1namehead\endcsname}%
}
\define@choicekey{dilfamily}{filenamefoot}{%
  inside,outside,center,left,right,off}{%
  \AtEndPreamble{\csname dil@#1namefoot\endcsname}%
}

\define@boolkeys{dilfamily}{sectionline}[true]{}
\define@key{dilfamily}{environmentline}{\dil@csvpatchenv{#1}}

\define@boolkeys{dilfamily}{
  dilinputfiles,inputfilesrule,graphicsfilename
}[true]{}
%   
% Optional lists of environments
% mathmode
% amsmath
% amsthm
% ntheorem's named envs?


%------------------------------
% Default options
%------------------------------
%I turned these off because weird stuff happened if you over-rode them.

%--------------------------------------------------
% Here comes the options
%--------------------------------------------------

\ProcessOptionsX<dilfamily>
%------------------------------
% Paragraph numbers
%------------------------------
\ifKV@dilfamily@safemode

% Safe mode adds line numbers to section headings and selected environments.

\newcommand\dil@patchenv[1]{%
  \AtBeginEnvironment{#1}{\addlineno}}
\newcommand\dil@csvpatchenv{%
  \let\do\dil@patchenv
  \docsvlist}

% Add environments to the list to have them print a line number.
\dil@csvpatchenv{itemize,enumerate,description,quote}

\ifKV@dilfamily@sectionline
\pretocmd{\@startsection}{\addlineno}
\fi

\else

% I guess this is dangerous because the paragraph hook gets used all over the place
% That's why safemode turns it off.

\PushPostHook{par}{%
  \ifnotmarginhook%
  \notmarginhookfalse%
  {\marginnote{\small\dilfont\the\inputlineno}}%
  \notmarginhooktrue%
  \fi%
}

\fi

%------------------------------
% Headers and footers
%------------------------------
\ifKV@dilfamily@hijackboth
\setkeys{dilfamily}{
  hijackheaders=true,
  hijackfooters=true
}
\fi
\ifKV@dilfamily@hijackheaders
\setkeys{dilfamily}{
  inputlineshead=outside,
  filenamehead=inside
}
\fi
\ifKV@dilfamily@hijackfooters
\setkeys{dilfamily}{
  inputlinesfoot=outside,
  filenamefoot=inside
}
\fi

\newcommand\toplineno{%
  {\dilfont\arabic{firstline}}\setcounter{firstline}{\the\inputlineno}%
  }
\newcommand\bottomlineno{%
  {\dilfont\the\inputlineno}%
}
\newcommand\toplinefile{%
  \dilfont\thefilename\xdef\thefilename{\currfilename}%
}
\newcommand\bottomlinefile{%
  \dilfont\thefilename%
}

\newcommand\set@head[2]{%
  \expandafter\def\csname dil@#1head\endcsname{%
    \csname #2\endcsname{\toplineno}}
  \expandafter\def\csname dil@#1namehead\endcsname{%
    \csname #2\endcsname{\toplinefile}}
}
\newcommand\set@foot[2]{
  \expandafter\def\csname dil@#1foot\endcsname{%
    \csname #2\endcsname{\bottomlineno}}
  \expandafter\def\csname dil@#1namefoot\endcsname{%
    \csname #2\endcsname{\bottomlinefile}}
}

%------------------------------
% Define commands for scrpage
%------------------------------

%This section is tremendously inelegant.



\ifKV@dilfamily@scrpage
\RequirePackage{scrpage2}
\pagestyle{scrheadings}

\newcommand\scrlefthead[1]{%
  \lohead{#1}\lehead{#1}%
}
\newcommand\scrrighthead[1]{%
  \rohead{#1}\rehead{#1}%
}
\newcommand\scrleftfoot[1]{%
  \lofoot{#1}\lefoot{#1}%
}
\newcommand\scrrightfoot[1]{%
  \rofoot{#1}\refoot{#1}%
}

\set@head{inside}{ihead}
\set@head{outside}{ohead}
\set@head{center}{chead}
\set@head{left}{scrlefthead}
\set@head{right}{scrrighthead}
\set@foot{inside}{ifoot}
\set@foot{outside}{ofoot}
\set@foot{center}{cfoot}
\set@foot{left}{scrleftfoot}
\set@foot{right}{scrrightfoot}

\ifKV@dilfamily@hijackboth
\cfoot{\thepage}
\fi
\fi

%------------------------------
% Define commands for fancyhdr 
%------------------------------


\ifKV@dilfamily@fancyhdr
\RequirePackage{fancyhdr}
\pagestyle{fancy}

\newcommand\fcinsidehead[1]{%
  \fancyhead[RE,LO]{#1}%
}
\newcommand\fcoutsidehead[1]{%
  \fancyhead[RO,LE]{#1}%
}
\newcommand\fcinsidefoot[1]{%
  \fancyfoot[RE,LO]{#1}
}
\newcommand\fcoutsidefoot[1]{%
  \fancyfoot[RO,LE]{#1}
}

\set@head{inside}{fcinsidehead}
\set@head{outside}{fcoutsidehead}
\set@head{center}{chead}
\set@head{left}{lhead}
\set@head{right}{rhead}
\set@foot{inside}{fcinsidefoot}
\set@foot{outside}{fcoutsidefoot}
\set@foot{center}{cfoot}
\set@foot{left}{lfoot}
\set@foot{right}{rfoot}


\ifKV@dilfamily@hijackboth
\cfoot{\thepage}
\fi

\fi

\ifKV@dilfamily@memoir
\makepagestyle{dil}
\pagestyle{dil}

\fi

%------------------------------
% Sections, environments
%------------------------------

% Oops. I deleted the environment stuff...

%------------------------------
% Inputs and inclusions
%------------------------------
\ifKV@dilfamily@graphicsfilename
\pretocmd\Gin@setfile{
      \dilrotnote{#3}
}{}{}
\fi
\ifKV@dilfamily@dilinputfiles
\AtBeginOfInputs{%
  \ifKV@dilfamily@inputfilesrule%
  \noindent\rule{\linewidth}{1pt}%
  \fi
  \dilrotnote{\dilfont\currfilename}
}

\AtEndOfInputs{%
  \ifKV@dilfamily@inputfilesrule%
  \noindent\rule{\linewidth}{1pt}%
  \fi%
  \dilrotadjnote{\dilfont End \currfilename}%
}
\AtBeginOfIncludes{\dilmnote{Include file: {\dilfont\currfilename} at line {\dilfont\the\inputlineno}}}
\AtEndOfIncludes{\dilmnote{End of {\dilfont\currfilename}}}
\fi

\AtBeginDocument{%
  \setcounter{firstline}{\the\inputlineno}%
  \xdef\thefilename{\currfilename}
}
