\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{Sweave}{}

\RequirePackage{ifthen}
\newboolean{Sweave@gin}
\setboolean{Sweave@gin}{true}
\newboolean{Sweave@inconsolata}
\setboolean{Sweave@inconsolata}{false}
\newboolean{Sweave@fontenc}
\setboolean{Sweave@fontenc}{true}

\DeclareOption{nogin}{\setboolean{Sweave@gin}{false}}
\DeclareOption{noae}{\setboolean{Sweave@fontenc}{false}}  % retained for backward compatibility
\DeclareOption{inconsolata}{\setboolean{Sweave@inconsolata}{true}}
\DeclareOption{nofontenc}{\setboolean{Sweave@fontenc}{false}}
\ProcessOptions

%% disable fontenc for engines other than pdfTeX (requires iftex)
\ifthenelse{\boolean{Sweave@fontenc}}{%
  \IfFileExists{iftex.sty}{\RequirePackage{iftex}}{}
  \@ifpackageloaded{iftex}{\ifPDFTeX\else\setboolean{Sweave@fontenc}{false}\fi}{}
}{}%

\RequirePackage{graphicx,fancyvrb,textcomp}
%% inspired by an earlier version of upquote.sty
\begingroup
\catcode`'=\active \catcode``=\active
\g@addto@macro\@noligs{\let`\textasciigrave \let'\textquotesingle}
\endgroup

\ifthenelse{\boolean{Sweave@gin}}{\setkeys{Gin}{width=0.8\textwidth}}{}%
\ifthenelse{\boolean{Sweave@fontenc}}{%
  \RequirePackage[T1]{fontenc}
}{}%
%% see comment in Rd.sty
\ifthenelse{\boolean{Sweave@inconsolata}}{%
 \IfFileExists{zi4.sty}{\usepackage[noupquote]{zi4}}{\usepackage{inconsolata}}}{}

\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}

\ifdefined\Schunk%
  \message{\string Environment Schunk is already defined, stay with former definition}%
\else
  \newenvironment{Schunk}{}{}%
\fi

\newcommand{\Sconcordance}[1]{%
  \ifx\pdfoutput\undefined%
  \csname newcount\endcsname\pdfoutput\fi%
  \ifcase\pdfoutput\special{#1}%
  \else%
   \begingroup%
     \pdfcompresslevel=0%
     \immediate\pdfobj stream{#1}%
     \pdfcatalog{/SweaveConcordance \the\pdflastobj\space 0 R}%
   \endgroup%
  \fi}
