%
%  Cezary Sobaniec <cezary.sobaniec@cs.put.poznan.pl>
%
%  $Id: dcslib.sty 3118 2012-11-22 09:13:56Z sobaniec $
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{dcslib}[2012/03/01 v2.2 DCS Group library package]

\RequirePackage{listings}
\RequirePackage{longtable}
\RequirePackage{ifthen}
\RequirePackage{array}
\RequirePackage{graphicx}
\RequirePackage{xcolor}
%\RequirePackage[hyphens]{url}
\RequirePackage{url}
\RequirePackage{soul}
\RequirePackage{prettyref}
\RequirePackage{caption}
\RequirePackage{setspace}
\RequirePackage{translator}
\RequirePackage{subfig}
\RequirePackage{iftex}
\RequirePackage{amsmath}
\RequirePackage{amssymb}


% support for XeTeX and LuaTeX
\newif\ifunicodeengine
\ifXeTeX
  \unicodeenginetrue
\else
  \ifLuaTeX
    \unicodeenginetrue
  \else
    \RequirePackage[utf8]{inputenc}
    \RequirePackage[T1]{fontenc}
  \fi
\fi
\ifunicodeengine
  \RequirePackage{mathspec}
  \defaultfontfeatures{Ligatures=TeX}
\fi


\usedictionary{translator-theorem-dictionary}

% colors
\definecolor{white}{rgb}{1, 1, 1}
\definecolor{green}{rgb}{0,0.45,0}
\definecolor{red}{rgb}{0.8, 0.0, 0.0}
\definecolor{blue}{rgb}{0.0, 0.0, 0.8}
\definecolor{gray}{rgb}{0.6,0.6,0.6}
\definecolor{dkgreen}{rgb}{0,0.35,0}
\definecolor{dkblue}{rgb}{0, 0.0, 0.5}
\definecolor{dkred}{rgb}{0.6, 0.0, 0.0}
\definecolor{ltgray}{rgb}{0.8,0.8,0.8}
\definecolor{midgray}{rgb}{0.4,0.4,0.4}
\definecolor{dkgray}{rgb}{0.2,0.2,0.2}

\definecolor{bgcolor}{rgb}{0.90,0.90,0.90}
\definecolor{framebg}{rgb}{0.985,0.985,0.985}
\definecolor{framerule}{rgb}{0.8,0.8,0.8}
\definecolor{directive}{rgb}{0.35,0.0,0.35}
\definecolor{linkcolor}{rgb}{0.1,0.1,0.6}
\definecolor{pathcolor}{rgb}{0.5,0,0}
\definecolor{namecolor}{rgb}{0.5,0,0.5}
\definecolor{cmdcolor}{rgb}{0,0.3,0}
\definecolor{mancolor}{rgb}{0.5,0,0}
\definecolor{codecolor}{rgb}{0.6,0,0}
\definecolor{seccolor}{rgb}{0,0,0}

\newcommand{\colorfillbox}[3]%
  {\colorbox{#1}{\parbox[b]{#2}{\rule{0pt}{#3}\mbox{}}}}


% fonts
\newcommand{\condensed}[1]{\ttfamily\fontsize{#1}{#1}\selectfont}
\newcommand{\condensedx}[2]{\ttfamily\fontsize{#1}{#2}\selectfont}


% url
\def\UrlFont{\ttfamily}

% DCS module character styles
\newcommand{\ttsloppy}{\spaceskip .5em plus .3em minus .3em\hyphenchar\font=`\-\relax}
\newcommand{\dcsemph}[1]{\textit{#1}}
\newcommand{\dcsstrong}[1]{\textbf{#1}}
\newcommand{\dcscode}[1]{\textcolor{codecolor}{\texttt{\ttsloppy #1}}}
\newcommand{\dcspath}[1]{\textcolor{pathcolor}{\path{#1}}}
\newcommand{\dcscmd}[1]{\textcolor{cmdcolor}{\texttt{\ttsloppy #1}}}
\newcommand{\dcskbd}[1]{\raisebox{0.1ex}{\colorbox{bgcolor}{\textcolor{dkgray}{\bfseries\small\sffamily{}#1}}}}
\newcommand{\dcsname}[1]{\textcolor{namecolor}{\textsf{#1}}}


% listings setup
\newlength{\lstxframesep}
\setlength{\lstxframesep}{0.5em}
\newlength{\lstyframesep}
\setlength{\lstyframesep}{0.5ex}
\newlength{\lstframewidth}
\setlength{\lstframewidth}{0.4pt}
\newlength{\lstxmargin}
\setlength{\lstxmargin}{\lstxframesep}
\addtolength{\lstxmargin}{\lstframewidth}

% default settings
\lstset{tabsize=2,extendedchars=true,
        framerule=\lstframewidth,
        escapeinside={/*@}{@*/},
        basicstyle=\ttfamily\small,
        showspaces=false,showstringspaces=false,
        includerangemarker=false,rangeprefix=//,
        language=,numberstyle=\tiny\sffamily,
        inputencoding=utf8,
        belowcaptionskip=\medskipamount}
\lstdefinestyle{plain}{%
        frame=,backgroundcolor=,
        framexleftmargin=0pt,framexrightmargin=0pt,
        framextopmargin=0pt,framexbottommargin=0pt,
        xleftmargin=0pt,xrightmargin=0pt,
        aboveskip=\medskipamount,belowskip=\medskipamount}
\lstdefinestyle{linesbg}{%
        frame=tb,framesep=0pt,
        rulecolor=\color{framerule},backgroundcolor=\color{framebg},
        framexleftmargin=\lstxframesep,framexrightmargin=\lstxframesep,
        framextopmargin=\lstyframesep,framexbottommargin=\lstyframesep,
        xleftmargin=\lstxmargin,xrightmargin=\lstxmargin,
        aboveskip=\bigskipamount,belowskip=\bigskipamount}
\lstdefinestyle{linesbgn}{style=linesbg,numbers=left,
                          framexleftmargin=3em,xleftmargin=3em}
\lstdefinestyle{rpcgen}{language=c,morekeywords={program,version}}
\lstdefinestyle{ldif}{language=}

% patch for compatibility between listings and XeTeX based on
% http://tex.stackexchange.com/questions/25391/the-listings-package-and-utf-8
\ifunicodeengine
  \input{listings-xetex-pl.tex}
\else
  \RequirePackage{listingsutf8}
  \lstset{inputencoding=utf8/latin2}
\fi

\newcommand{\codeinc}[4][]{%
  \lstinputlisting[frame=lines,
        framexleftmargin=\lstxframesep,framexrightmargin=\lstxframesep,
        framextopmargin=\lstyframesep,framexbottommargin=\lstyframesep,
        xleftmargin=\lstxframesep,xrightmargin=\lstxframesep,
        aboveskip=0ex,belowskip=0ex,float,
        caption=#3,label=#4,#1]{#2}%
  }
\newcommand{\codeincn}[4][]{%
  \lstinputlisting[frame=lines,
        framexleftmargin=4.5\lstxframesep,framexrightmargin=\lstxframesep,
        framextopmargin=\lstyframesep,framexbottommargin=\lstyframesep,
        numbers=left,xleftmargin=4.5\lstxframesep,xrightmargin=\lstxframesep,
        aboveskip=0ex,belowskip=0ex,float,
        caption=#3,label=#4,#1]{#2}%
  }

\newlength{\codemargin}
\newcommand{\resetcodemargin}{%
  \advance\@listdepth by 1%
  \setlength{\codemargin}{\csname leftmargin\romannumeral\the\@listdepth\endcsname}
  \advance\@listdepth by -1%
}

\newcommand{\codeincp}[2][]{
  \resetcodemargin%
  \lstinputlisting[style=plain,xleftmargin=\codemargin,#1]{#2}%
}

\lstnewenvironment{codeblock}[1][]{%
  \resetcodemargin\lstset{xleftmargin=\codemargin,#1}}{}


% manpages
\newcommand{\manwi}[3][]{%
  \ifthenelse{#3=1\or#3=8}{\dcscmd{#2(#3)}}{%
  \ifthenelse{#3=5}{\dcspath{#2(#3)}}{%
  \ifthenelse{#3=2\or#3=3}{\dcscode{#2(#3)}}{%
  \dcsname{#2(#3)}}}}}

\newcommand{\funidx}[1]{\index{funkcja!\texttt{#1}}}
\newcommand{\cmdidx}[1]{\index{polecenie!\texttt{#1}}}
\newcommand{\srvidx}[1]{\index{us\l{}uga!\texttt{#1}}}

\newcommand{\man}[3][]{%
  \manwi{#2}{#3}%
  \ifthenelse{#3=1\or#3=8}{\cmdidx{#2}}{%
  \ifthenelse{#3=2\or#3=3}{\funidx{#2}}{%
  \ifthenelse{#3=5}{%
    \ifthenelse{\equal{#1}{}}{\index{plik konfiguracyjny!\texttt{#2}}}{%
    \index{plik konfiguracyjny!\texttt{#1}}}}{%
  }}}}


% Java documentation
\newcommand{\japiurlbase}{http://docs.oracle.com/javase/6/docs/api/}
\newcommand{\japipkg}[2][]{\href{\japiurlbase/#2/package-summary.html}%
  {\ifthenelse{\equal{#1}{}}{#2}{#1}}}
\newcommand{\japiclass}[2][]{\href{\japiurlbase/#2.html}%
  {\ifthenelse{\equal{#1}{}}{#2}{#1}}}


% arrays/tabular
\setlength{\extrarowheight}{0.3ex}


% math
\providecommand{\implies}{\Rightarrow}
\renewcommand{\iff}{\Leftrightarrow}
\setlength\arraycolsep{1.4\p@}


% polish translations
\newcommand{\techreportname}{Technical Report}
\DeclareOption{polish}{
  \renewcommand{\techreportname}{Raport badawczy}
  \renewcommand{\lstlistingname}{Przyk\l{}ad}
  % pretty references
  \newrefformat{def}{definicja~\ref{#1}}
  \newrefformat{lem}{lemat~\ref{#1}}
  \newrefformat{thm}{twierdzenie~\ref{#1}}
  \newrefformat{cha}{rozdzia\l{}~\ref{#1}}
  \newrefformat{sec}{punkt~\ref{#1}}
  \newrefformat{sub}{punkt~\ref{#1}}
  \newrefformat{tab}{tab.~\ref{#1}}
  \newrefformat{fig}{rys.~\ref{#1}}
  \newrefformat{alg}{alg.~\ref{#1}}
  \newrefformat{lst}{przyk\l{}ad~\ref{#1}}
}
\DeclareOption{english}{
  \renewcommand{\techreportname}{Technical Report}
  \renewcommand{\lstlistingname}{Listing}
  % pretty references
  \newrefformat{def}{Definition~\ref{#1}}
  \newrefformat{lem}{Lemma~\ref{#1}}
  \newrefformat{thm}{Theorem~\ref{#1}}
  \newrefformat{cha}{Chapter~\ref{#1}}
  \newrefformat{sec}{Section~\ref{#1}}
  \newrefformat{sub}{Subsection~\ref{#1}}
  \newrefformat{tab}{Table.~\ref{#1}}
  \newrefformat{fig}{Fig.~\ref{#1}}
  \newrefformat{alg}{Alg.~\ref{#1}}
  \newrefformat{lst}{Example~\ref{#1}}
}
\DeclareOption{american}{
  \renewcommand{\techreportname}{Technical Report}
  \renewcommand{\lstlistingname}{Listing}
  % pretty references
  \newrefformat{def}{Definition~\ref{#1}}
  \newrefformat{lem}{Lemma~\ref{#1}}
  \newrefformat{thm}{Theorem~\ref{#1}}
  \newrefformat{cha}{Chapter~\ref{#1}}
  \newrefformat{sec}{Section~\ref{#1}}
  \newrefformat{sub}{Subsection~\ref{#1}}
  \newrefformat{tab}{Table.~\ref{#1}}
  \newrefformat{fig}{Fig.~\ref{#1}}
  \newrefformat{alg}{Alg.~\ref{#1}}
  \newrefformat{lst}{Example~\ref{#1}}
}


% captions
\captionsetup{font={sf,small},labelfont=bf,
              margin=2\parindent,
              skip=\bigskipamount}
\captionsetup[table]{singlelinecheck=false,position=top,
                     aboveskip=\medskipamount,belowskip=0cm,
                     margin={0pt,2\parindent},oneside}
\captionsetup[lstlisting]{singlelinecheck=false,position=top,
                          aboveskip=\medskipamount,belowskip=0cm,
                          margin={0pt,2\parindent},oneside}
\captionsetup[subfigure]{font={sf,footnotesize},margin=0pt,format=hang}


% standard typewriter font
\DeclareOption{lmtt}{
  \renewcommand{\ttdefault}{lmtt}
  \lstset{basewidth=0.5em}
}


% extra commands
\providecommand{\email}[1]{\texttt{\small\ttsloppy #1}}


% widows and orphans (club lines) — up to 1000
\widowpenalty=300
\clubpenalty=300

\ProcessOptions*
