% $Id: csquotes.sty,v 3.1 2005/08/29 18:07:39 lehman pub $
%
% Copyright (c) 2003--2005 Philipp Lehman, author-maintained.
%
% Permission is granted to copy, distribute and/or modify this
% software under the terms of the LaTeX Project Public License
% (LPPL), version 1.3.
%
% This software is provided 'as is', without warranty of any kind,
% either expressed or implied, including, but not limited to, the
% implied warranties of merchantability and fitness for a particular
% purpose.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{csquotes}[2005/08/29 v3.1 quotation facilities]
\RequirePackage{keyval}

%% General things

\newif\ifcsq@reset
\newif\ifcsq@block
\newif\ifcsq@quiet
\newif\ifcsq@strict
\newif\ifcsq@active

\newcount\csq@qlevel
\newcount\csq@glevel
\newcount\csq@gtype
\newcount\csq@lastsf
\newcount\csq@threshold

\newtoks\csq@ltxeverypar

\def\csq@ifcsundefined#1{%
  \begingroup
  \expandafter\ifx\csname#1\endcsname\relax
    \endgroup\expandafter\@firstoftwo
  \else
    \endgroup\expandafter\@secondoftwo
  \fi}

\def\csq@ifundefined#1{%
  \begingroup
  \escapechar\m@ne
  \expandafter\csq@ifcsundefined
  \expandafter{\string #1}
    {\endgroup\@firstoftwo}
    {\endgroup\@secondoftwo}}

\csq@ifcsundefined{eTeXversion}
  {\let\csq@ifetex\@secondoftwo}
  {\let\csq@ifetex\@firstoftwo}

% Hook management (and some syntactic sugar)

\long\def\csq@add#1#2{%
  \csq@ifundefined{#1}{\let#1\@empty}{}%
  \begingroup
    \toks@=\expandafter{#1#2}%
    \edef#1{\endgroup\def\noexpand#1{\the\toks@}}%
  #1}

\long\def\csq@eadd#1#2{%
  \csq@ifundefined{#1}{\let#1\@empty}{}%
  \begingroup
    \toks@=\expandafter{#1}%
    \edef#1{\endgroup\def\noexpand#1{\the\toks@#2}}%
  #1}

\long\def\csq@gadd#1{%
  \csq@ifundefined{#1}{\let#1\@empty}{}%
  \g@addto@macro#1}

\long\def\csq@xadd#1#2{%
  \csq@ifundefined{#1}{\let#1\@empty}{}%
  \begingroup
    \toks@=\expandafter{#1}%
    \xdef#1{\the\toks@#2}%
  \endgroup}

\def\csq@csgdef#1{\expandafter\gdef\csname #1\endcsname}
\def\csq@csxdef#1{\expandafter\xdef\csname #1\endcsname}
\def\csq@csgadd#1{\expandafter\csq@gadd\csname #1\endcsname}
\def\csq@csxadd#1{\expandafter\csq@xadd\csname #1\endcsname}

\def\csq@addto@dolist#1{%
  \expandafter\csq@addto@dolist@i\csname\string #1\endcsname}

\def\csq@addto@dolist@i#1#2#3{%
  \begingroup
  \@tempswatrue
  \def#3##1{\ifnum`#1=`##1\relax\@tempswafalse\fi}#2%
  \expandafter\endgroup\if@tempswa
    \csq@add#2{#3#1}%
  \fi}

\def\csq@addto@everypar#1{%
  \let\csq@everypar\everypar
  \csq@ltxeverypar=\expandafter{\the\everypar}%
  \csq@everypar={\the\csq@ltxeverypar #1}%
  \let\everypar\csq@ltxeverypar
  \let\csq@addto@everypar\@gobble}

% Miscellaneous things

\begingroup
  \catcode`\&=3

  \long\gdef\csq@ifblank#1{% by D. Arseneau
    \csq@ifblank@i#1&&\@secondoftwo\@firstoftwo&}%
  \long\gdef\csq@ifblank@i#1#2&#3#4#5&{#4}

  \long\gdef\csq@ifsingle#1{\csq@ifblank@i#1&&{\expandafter
    \csq@ifsingle@i\string #1&&\@secondoftwo\@firstoftwo&}%
    \@secondoftwo&}%
  \long\gdef\csq@ifsingle@i#1#2#3&#4#5#6&{#5}

\endgroup

\newcommand*{\ifstringblank}{\csq@ifblank}
\newcommand*{\ifblockquote}{%
  \ifcsq@block
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\def\csq@ifmmode{%
  \ifmmode
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\csq@ifetex
  {\def\csq@bqgroup{%
     \bgroup\leavevmode
     \csq@glevel\currentgrouplevel
     \csq@gtype\currentgrouptype}
   \def\csq@eqgroup{%
     \ifnum\csq@glevel=\currentgrouplevel
       \ifnum\csq@gtype=\currentgrouptype
       \else
	 \csq@err@mismatch{%
           I have detected a group type mismatch. Verify that all
           groups\MessageBreak and all active quotes are properly
           balanced and nested. If you\MessageBreak continue, I will
           write the level and type of all previously\MessageBreak
           opened groups to the transcript file}%
	 \aftergroup\showgroups
       \fi
     \else
       \csq@err@mismatch{%
	 I have detected a group level mismatch. Verify that all
	 groups\MessageBreak and all active quotes are properly
	 balanced and nested. If you\MessageBreak continue, I will
	 write the level and type of all previously\MessageBreak
	 opened groups to the transcript file}%
       \aftergroup\showgroups
     \fi
     \egroup}}
  {\let\csq@bqgroup\begingroup
   \let\csq@eqgroup\endgroup}

%% User feedback

\def\csq@error#1#2{%
  \PackageError{csquotes}{#1}{#2.}}

\def\csq@warning#1#2#3#4{%
  \ifcsq@strict
    \PackageError{csquotes}{#3}{#4.}%
  \else
    \PackageWarning{csquotes}{#1.\MessageBreak #2}%
  \fi}

\def\csq@info#1{%
  \ifcsq@quiet
  \else
    \PackageInfo{csquotes}{#1\csq@noline}%
  \fi}

\let\csq@noline\@gobble
\AtEndOfPackage{\let\csq@noline\@empty}

% Errors

\def\csq@err@mismatch#1{%
  \csq@errormark
  \csq@error
    {Unbalanced groups or invalid nesting}
    {#1}}

\def\csq@err@mmode#1{%
  \csq@error
    {Command `\string #1' invalid in math mode}
    {This command may not be used in math mode}}

\def\csq@err@notdef#1{%
  \csq@error
    {Command or environment `\string #1' not defined}
    {This command or environment environment appears to
     be undefined}}

\def\csq@err@alloc#1{%
  \csq@error
    {Character \string #1 already allocated}
    {This character is already allocated as active quote,
     delimiter,\MessageBreak or closing mark}}

\def\csq@err@invquote{%
  \csq@error
    {Invalid argument}
    {One of the arguments you have supplied is invalid.
     Only single\MessageBreak characters with category
     code 7-8 and 12-13 may be used as\MessageBreak
     active quotes}}

\def\csq@err@invdelim{%
  \csq@error
    {Invalid argument}
    {One of the arguments you have supplied is invalid.
     Only single\MessageBreak characters with category
     code 3-4, 7-8, 12-13 may be used as\MessageBreak
     delimiters or closing marks}}

% Warnings

\def\csq@warn@nobabel#1{%
  \csq@warning
    {The `babel' package is not available}
    {#1}
    {#1}
    {Try loading the `babel' package if you want to use this
     feature}}

\def\csq@warn@noomega#1{%
  \csq@warning
    {Quote style `#1' requires Omega}
    {Some features will be missing}
    {Quote style `#1' requires Omega}
    {Try compiling the document with `lambda' instead of `latex'}}

\def\csq@warn@location#1{%
  \csq@warning
    {Command `\string #1' invalid at this point}
    {Ignoring command}
    {Command `\string #1' invalid at this point}
    {This command may not be used at this particular location}}

% Notices

\def\csq@info@legacy#1#2{%
  \csq@info{%
    Command `\string #1' is depreciated.\MessageBreak
    Please use `\string #2' instead}%
  #2}

%% Opening quotation mark

% Outer marks

\def\csq@oqmark{%
  \ifnum\csq@qlevel>\z@
    \csq@err@mismatch{%
      You have requested a level 1 quote, but
      the current quotation\MessageBreak level
      is already \the\csq@qlevel}%
  \else
    \csq@bqgroup
    \csq@qlevel\@ne
    \csq@resetstyle
    \csq@init
    \let\csq@oqmark\csq@qclose
    \let\csq@kerntok@i\csq@theqmark@oopen
    \csq@theqmark@oopen
    \csq@setmark@open
    \expandafter\csq@restkern
  \fi}

% Inner marks

\def\csq@iqmark{%
  \ifnum\csq@qlevel>\@ne
    \csq@err@mismatch{%
      You have requested a level 2 quote, but
      the current quotation\MessageBreak level
      is already \the\csq@qlevel}%
  \else
    \csq@bqgroup
    \csq@qlevel\tw@
    \csq@resetstyle
    \csq@init
    \let\csq@iqmark\csq@qclose
    \csq@addkern@open
    \csq@theqmark@iopen
  \fi}

% Nestable marks

\def\csq@qopen{%
  \csq@bqgroup\csq@qopen@i}

\def\csq@qopen@i{%
  \ifnum\csq@qlevel>\@ne
    \csq@err@mismatch{%
      You have requested a level 1 or 2 quote,
      but the current\MessageBreak quotation
      level is already \the\csq@qlevel}%
    \csq@eqgroup
  \else
    \advance\csq@qlevel\@ne
    \csq@resetstyle
    \csq@init
    \ifnum\csq@qlevel=\@ne
      \let\csq@kerntok@i\csq@theqmark@oopen
      \csq@theqmark@oopen
      \csq@setmark@open
      \expandafter\expandafter
      \expandafter\csq@restkern
    \else
      \csq@addkern@open
      \csq@theqmark@iopen
    \fi
  \fi}

\def\csq@iqopen{%
  \csq@bqgroup\csq@iqopen@i}

\def\csq@iqopen@i{%
  \ifnum\csq@qlevel>\@ne
    \csq@err@mismatch{%
      You have requested a level 2 quote, but
      the current quotation\MessageBreak level
      is already \the\csq@qlevel}%
    \csq@eqgroup
  \else
    \csq@qlevel\tw@
    \csq@resetstyle
    \csq@init
    \csq@addkern@open
    \csq@theqmark@iopen
  \fi}

% Nestable marks + babel

\def\csq@qopen@lang#1{\csq@bqgroup\csq@lang{#1}\csq@qopen@i}
\def\csq@qopen@hyph#1{\csq@bqgroup\csq@hyph{#1}\csq@qopen@i}

%% Closing quotation mark

\def\csq@qclose{%
  \ifnum\csq@qlevel=\@ne
    \ifdim\lastkern=\csq@frenchmark
      \unkern\unkern
      \csq@eqgroup
    \else
      \csq@addkern@close
      \csq@theqmark@oclose
      \let\csq@kerntok@i\csq@theqmark@oclose
      \csq@qclose@i
      \expandafter\expandafter
      \expandafter\csq@restkern
    \fi
  \else
    \ifnum\csq@qlevel=\tw@
      \csq@theqmark@iclose
      \csq@setmark@close
      \let\csq@kerntok@i\csq@theqmark@iclose
      \csq@qclose@i
      \expandafter\expandafter
      \expandafter\csq@restkern
    \else
      \csq@err@mismatch{%
        You have requested that the current quotation
        be closed, but\MessageBreak the current
        quotation level is already \the\csq@qlevel}%
    \fi
  \fi}

\def\csq@qclose@i{%
  \csq@setlastsf
  \expandafter\def
  \expandafter\csq@tempa
  \expandafter{%
    \expandafter\csq@eqgroup
    \expandafter\def
    \expandafter\csq@kerntok@i
    \expandafter{\csq@kerntok@i}}%
  \csq@eadd\csq@tempa{\csq@lastsf\the\csq@lastsf\relax}%
  \csq@tempa}

%% Style initialization

\def\csq@init{%
  \csq@setsfcodes
  \ifnum\csq@qlevel=\@ne
    \csq@theqmark@oinit
    \ifx\csq@theqmark@omiddle\@empty
    \else
      \let\csq@theeverypar\csq@theqmark@omiddle
      \leavevmode
      \csq@addto@everypar{\csq@theeverypar}%
    \fi
    \begingroup
      \settowidth{\@tempdima}{%
        \csq@theqmark@oopen\csq@theqmark@iopen}%
      \settowidth{\@tempdimb}{%
        \csq@theqmark@oopen\unkern\csq@theqmark@iopen}%
      \ifdim\@tempdima=\@tempdimb
        \def\csq@tempa{\endgroup
          \let\csq@thekern@open\csq@theqmark@kern}%
      \else
        \addtolength{\@tempdima}{-\@tempdimb}%
        \edef\csq@tempa{\endgroup
          \def\noexpand\csq@thekern@open{\the\@tempdima}}%
      \fi
    \csq@tempa
    \begingroup
      \settowidth{\@tempdima}{%
        \csq@theqmark@iclose\csq@theqmark@oclose}%
      \settowidth{\@tempdimb}{%
        \csq@theqmark@iclose\unkern\csq@theqmark@oclose}%
      \ifdim\@tempdima=\@tempdimb
        \def\csq@tempa{\endgroup
          \let\csq@thekern@close\csq@theqmark@kern}%
      \else
        \addtolength{\@tempdima}{-\@tempdimb}%
        \edef\csq@tempa{\endgroup
          \def\noexpand\csq@thekern@close{\the\@tempdima}}%
      \fi
    \csq@tempa
  \else
    \csq@theqmark@iinit
    \ifx\csq@theqmark@imiddle\@empty
    \else
      \let\csq@theeverypar\csq@theqmark@imiddle
      \leavevmode
      \csq@addto@everypar{\csq@theeverypar}%
    \fi
  \fi}

\csq@add\@parboxrestore{%
  \let\csq@bquote@i\csq@bquote@iii
  \csq@qlevel\z@
  \csq@blockfalse}

%% Restore kerning

\def\csq@restkern{%
  \expandafter\expandafter\expandafter
  \expandafter\expandafter\expandafter
  \expandafter\csq@restkern@i}

\def\csq@restkern@i{%
  \futurelet\csq@kerntok@ii\csq@restkern@ii}

\def\csq@restkern@ii{%
  \ifcat a\noexpand\csq@kerntok@ii
    \csq@restkern@iii
  \fi
  \ifcat 1\noexpand\csq@kerntok@ii
    \csq@restkern@iii
  \fi}

\def\csq@restkern@iii{%
  \begingroup
    \settowidth{\@tempdima}{%
      \csq@kerntok@i\csq@kerntok@ii}%
    \settowidth{\@tempdimb}{%
      \csq@kerntok@i\relax\csq@kerntok@ii}%
    \ifdim\@tempdima=\@tempdimb
    \else
      \addtolength{\@tempdima}{-\@tempdimb}%
      \penalty\@M\hskip\@tempdima\relax
    \fi
  \endgroup}

%% Detect and kern adjoining quotes

\def\csq@kernmark{13sp}
\def\csq@frenchmark{17sp}
\let\csq@themark\csq@kernmark

\def\csq@setmark@open{%
  \penalty\@M
  \hskip-\csq@themark\relax
  \hskip\csq@themark\relax}

\def\csq@setmark@close{%
  \kern-\csq@themark
  \kern\csq@themark}

\def\csq@addkern@open{%
  \ifdim\lastskip=\csq@kernmark
    \unskip\unskip\unpenalty
    \unkern % EC fix
    \ifdim\csq@thekern@open=\z@
    \else
      \penalty\@M\hskip\csq@thekern@open\relax
    \fi
  \fi}

\def\csq@addkern@close{%
  \ifdim\lastkern=\csq@kernmark
    \unkern\unkern
    \ifdim\csq@thekern@close=\z@
    \else
      \penalty\@M\hskip\csq@thekern@close\relax
    \fi
  \fi}

%% Detect terminal punctuation

\def\csq@setsfcodes{%
  \csq@ifcsundefined{csq@sfcodes@\f@encoding}
    {}
    {\@nameuse{csq@sfcodes@\f@encoding}}%
  \sfcode`\)=\z@
  \sfcode`\]=\z@
  \ifnum\sfcode`\.>2000
    \csq@setencodes
  \else
    \csq@setfrcodes
  \fi
  \let\frenchspacing\csq@setfrcodes
  \let\nonfrenchspacing\csq@setencodes}

\def\csq@setfrcodes{%
  \sfcode`\,=1001
  \sfcode`\;=1002
  \sfcode`\:=1003
  \sfcode`\.=1004
  \sfcode`\!=1005
  \sfcode`\?=1006
}

\def\csq@setencodes{%
  \sfcode`\,=1250
  \sfcode`\;=1500
  \sfcode`\:=2000
  \sfcode`\.=3000
  \sfcode`\!=3001
  \sfcode`\?=3002
}

\@namedef{csq@frcode@1250}{1001}
\@namedef{csq@frcode@1500}{1002}
\@namedef{csq@frcode@2000}{1003}
\@namedef{csq@frcode@3000}{1004}
\@namedef{csq@frcode@3001}{1005}
\@namedef{csq@frcode@3002}{1006}

\def\csq@setlastsf{%
  \ifhmode
    \csq@lastsf\spacefactor
    \csq@ifcsundefined{csq@frcode@\the\csq@lastsf}
      {}
      {\csq@lastsf\@nameuse{csq@frcode@\the\csq@lastsf}\relax}%
  \else
    \csq@lastsf\@m
  \fi}

\newcommand*{\ifquotepunct}{%
  \ifnum\csq@lastsf>\@m
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\newcommand*{\ifquotecomma}{%
  \ifnum\csq@lastsf=1001
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\newcommand*{\ifquotesemicolon}{%
  \ifnum\csq@lastsf=1002
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\newcommand*{\ifquotecolon}{%
  \ifnum\csq@lastsf=1003
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\newcommand*{\ifquoteterm}{%
  \ifnum\csq@lastsf>1003
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\newcommand*{\ifquoteperiod}{%
  \ifnum\csq@lastsf=1004
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\newcommand*{\ifquoteexclam}{%
  \ifnum\csq@lastsf=1005
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\newcommand*{\ifquotequestion}{%
  \ifnum\csq@lastsf=1006
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

%% Babel interface

\def\csq@setstyle#1{%
  \csq@ifcsundefined{csq@qstyle@#1}
    {\csq@warning
       {Quote style `#1' not defined}
       {Ignoring command}
       {Quote style `#1' not defined}
       {Define the style or alias first before using it}}
    {\edef\csq@currentstyle{#1}%
     \@nameuse{csq@qstyle@#1}}}

\def\csq@resetstyle{%
  \ifcsq@reset
    \ifx\csq@currentstyle\languagename
    \else
      \csq@ifcsundefined{csq@qstyle@\languagename}
        {\csq@warning
           {No quote style for language `\languagename'}
           {Using fallback style}
           {No quote style for language `\languagename'}
           {Define a style or alias for this language first}%
         \csq@setstyle{fallback}}
        {\csq@setstyle{\languagename}}%
    \fi
  \fi}

\def\csq@savelang{%
  \csq@ifcsundefined{csq@mainlang}
    {\edef\csq@mainlang{\languagename}}
    {}}

\def\csq@resetlang{%
  \csq@ifcsundefined{csq@mainlang}
    {}
    {\@nameuse{otherlanguage*}{\csq@mainlang}}}

% \lowercase: workaround for page headers of standard classes

\def\csq@lang#1{%
  \csq@savelang
  \lowercase{\@nameuse{otherlanguage*}{#1}}}

\def\csq@nolang#1{%
  \begingroup
    \lowercase{\def\csq@tempa{#1}}%
    \csq@warn@nobabel{Cannot switch to language `\csq@tempa'}%
  \endgroup}

\def\csq@hyph#1{%
  \csq@savelang
  \lowercase{\hyphenrules{#1}}}

\def\csq@nohyph#1{%
  \begingroup
    \lowercase{\def\csq@tempa{#1}}%
    \csq@warn@nobabel{No hyphenation rules for `\csq@tempa'}%
  \endgroup}

\csq@add\csq@mkbabel{%
  \csq@add\@parboxrestore{\csq@resetlang}%
  \csq@ifcsundefined{l@usenglish}
    {\csq@ifcsundefined{l@USenglish}
       {}
       {\let\l@usenglish\l@USenglish}}
    {}%
  \csq@ifcsundefined{l@ukenglish}
    {\csq@ifcsundefined{l@UKenglish}
       {}
       {\let\l@ukenglish\l@UKenglish}}
    {}}

\csq@add\csq@mknobabel{%
  \ifcsq@reset
    \csq@warn@nobabel{Disabling multilingual support}%
    \csq@resetfalse
  \fi
  \def\csq@resetqstyle{%
    \csq@warn@nobabel{Cannot enable multilingual support}}%
  \let\csq@lang\csq@nolang
  \let\csq@hyph\csq@nohyph}

%% Markup with control sequences

% Argument parser

\def\csq@getcargs#1{%
  \@ifnextchar[%]
    {\csq@getcargs@i{#1}}
    {\csq@getcargs@i{#1}[]}}

\long\def\csq@getcargs@i#1[#2]{%
  \@ifnextchar[%]
    {\csq@getcargs@ii{#1}{#2}}
    {\csq@getcargs@ii{#1}{#2}[]}}

\long\def\csq@getcargs@ii#1#2[#3]{%
  #1{\mkcitation}{#2}{#3}}

% Integrated argument parser

\def\csq@getccargs#1{%
  \@ifnextchar[%]
    {\csq@getccargs@i{#1}{}}
    {\csq@getccargs@ii{#1}{}}}

\long\def\csq@getccargs@i#1#2[#3]{%
  \@ifnextchar[%]
    {\csq@getccargs@i{#1}{#2[{#3}]}}
    {\csq@getccargs@ii{#1}{#2[{#3}]}}}

\long\def\csq@getccargs@ii#1#2#3{%
  \@ifnextchar[%]
    {\csq@getccargs@iii{#1}{#2}{#3}}
    {\csq@getccargs@iii{#1}{#2}{#3}[]}}

\long\def\csq@getccargs@iii#1#2#3[#4]{%
  #1{\mkccitation}{\csq@cite#2{#3}}{#4}}

% Plain quotes

\DeclareRobustCommand*{\enquote}{%
  \csq@ifmmode
    {\csq@err@mmode\enquote}
    {\@ifstar
      {\csq@quote@ii{}}
      {\csq@quote@i{}}}}

\long\def\csq@quote@i#1#2{%
  \csq@bqgroup #1\csq@qopen@i #2\csq@qclose}
\long\def\csq@quote@ii#1#2{%
  \csq@bqgroup #1\csq@iqopen@i #2\csq@qclose}

% Plain quotes + babel

\DeclareRobustCommand*{\foreignquote}{%
  \csq@ifmmode
    {\csq@err@mmode\foreignquote}
    {\@ifstar\csq@fquote@ii\csq@fquote@i}}

\def\csq@fquote@i#1{\csq@quote@i{\csq@lang{#1}}}
\def\csq@fquote@ii#1{\csq@quote@ii{\csq@lang{#1}}}

\DeclareRobustCommand*{\hyphenquote}{%
  \csq@ifmmode
    {\csq@err@mmode\hyphenquote}
    {\@ifstar\csq@hquote@ii\csq@hquote@i}}

\def\csq@hquote@i#1{\csq@quote@i{\csq@hyph{#1}}}
\def\csq@hquote@ii#1{\csq@quote@ii{\csq@hyph{#1}}}

% PDF strings

\long\def\csq@pdf@quote#1{%
  \if #1*\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
    {\csq@pdf@quote@ii}
    {\csq@pdf@quote@i{#1}}}

\long\def\csq@pdf@quote@i#1{%
  \csq@pdf@ooqmark #1\csq@pdf@coqmark}
\long\def\csq@pdf@quote@ii#1{%
  \csq@pdf@oiqmark #1\csq@pdf@ciqmark}

\long\def\csq@pdf@fquote#1{%
  \if #1*\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
    {\expandafter\csq@pdf@quote@ii\@gobble}
    {\csq@pdf@quote@i}}

\csq@add\csq@mkpdfstrings{%
  \let\enquote\csq@pdf@quote
  \let\foreignquote\csq@pdf@fquote
  \let\hyphenquote\csq@pdf@fquote}

% Text quotes

\DeclareRobustCommand*{\textquote}{%
  \csq@ifmmode
    {\csq@err@mmode\textquote}
    {\@ifstar
       {\csq@getcargs{\csq@tquote{\csq@iqopen@i}}}
       {\csq@getcargs{\csq@tquote{\csq@qopen@i}}}}}

% {<init><macro>}{<hook>}{<cite>}{<punct>}{<text>}

\long\def\csq@tquote#1#2#3#4#5{%
  \csq@checkblockcite
  \csq@bqgroup
    \def\csq@tempa{%
      \csq@qclose\mkmidtextpunct{#4}}%
    \csq@ifblank{#3}
      {}
      {\csq@add\csq@tempa{#2{#3}}}%
    #1\quotetext #5\endquotetext
  \csq@tempa\mkfintextpunct{#4}}

% Text quotes + babel

\DeclareRobustCommand*{\foreigntextquote}{%
  \csq@ifmmode
    {\csq@err@mmode\foreigntextquote}
    {\@ifstar\csq@ftquote@ii\csq@ftquote@i}}

\def\csq@ftquote@i#1{%
  \csq@getcargs{\csq@tquote{\csq@lang{#1}\csq@qopen@i}}}
\def\csq@ftquote@ii#1{%
  \csq@getcargs{\csq@tquote{\csq@lang{#1}\csq@iqopen@i}}}

\DeclareRobustCommand*{\hyphentextquote}{%
  \csq@ifmmode
    {\csq@err@mmode\hyphentextquote}
    {\@ifstar\csq@htquote@ii\csq@htquote@i}}

\def\csq@htquote@i#1{%
  \csq@getcargs{\csq@tquote{\csq@hyph{#1}\csq@qopen@i}}}
\def\csq@htquote@ii#1{%
  \csq@getcargs{\csq@tquote{\csq@hyph{#1}\csq@iqopen@i}}}

% Integrated text quotes

\DeclareRobustCommand*{\textcquote}{%
  \csq@ifmmode
    {\csq@err@mmode\textcquote}
    {\@ifstar
       {\csq@getccargs{\csq@tquote{\csq@iqopen@i}}}
       {\csq@getccargs{\csq@tquote{\csq@qopen@i}}}}}

% Integrated text quotes + babel

\DeclareRobustCommand*{\foreigntextcquote}{%
  \csq@ifmmode
    {\csq@err@mmode\foreigntextcquote}
    {\@ifstar\csq@ftcquote@ii\csq@ftcquote@i}}

\def\csq@ftcquote@i#1{%
  \csq@getccargs{\csq@tquote{\csq@lang{#1}\csq@qopen@i}}}
\def\csq@ftcquote@ii#1{%
  \csq@getccargs{\csq@tquote{\csq@lang{#1}\csq@iqopen@i}}}

\DeclareRobustCommand*{\hyphentextcquote}{%
  \csq@ifmmode
    {\csq@err@mmode\hyphentextcquote}
    {\@ifstar\csq@htcquote@ii\csq@htcquote@i}}

\def\csq@htcquote@i#1{%
  \csq@getccargs{\csq@tquote{\csq@hyph{#1}\csq@qopen@i}}}
\def\csq@htcquote@ii#1{%
  \csq@getccargs{\csq@tquote{\csq@hyph{#1}\csq@iqopen@i}}}

% PDF strings

\csq@add\csq@mkpdfstrings{%
  \def\textquote{\csq@warn@pdfstrings\textquote}%
  \def\foreigntextquote{\csq@warn@pdfstrings\foreigntextquote}%
  \def\hyphentextquote{\csq@warn@pdfstrings\hyphentextquote}%
  \def\textcquote{\csq@warn@pdfstrings\textcquote}%
  \def\foreigntextcquote{\csq@warn@pdfstrings\foreigntextcquote}%
  \def\hyphentextcquote{\csq@warn@pdfstrings\hyphentextcquote}}

% Block quotes

\DeclareRobustCommand*{\blockquote}{%
  \csq@ifmmode
    {\csq@err@mmode\blockquote}
    {\csq@getcargs{\csq@bquote{}}}}

% {<init>}{<hook>}{<cite>}{<punct>}{<text>}

\def\csq@bquote{%
  \begingroup
    \let\if@tempswa\ifcsq@block
    \ifnum\csq@qlevel>\z@
      \@tempswatrue
    \fi
  \expandafter\endgroup\if@tempswa
    \csq@err@mismatch{%
      You have nested a block quotation. Block quotation
      commands\MessageBreak may only be used on quotation
      level 0}%
    \expandafter\@gobblefive
  \else
    \expandafter\csq@bquote@i
  \fi}

\providecommand{\@gobblefive}[5]{}

\long\def\csq@bquote@i#1#2#3#4#5{%
  \begingroup
    \def\@elt##1{\global\value{##1}\the\value{##1}\relax}%
    \edef\csq@tempa{\cl@@ckpt}%
    \let\@elt\relax
    \let\csq@bquote\@gobblefive
    \let\csq@error\@gobbletwo
    \let\csq@warning\@gobblefour
    \let\csq@info\@gobble
    \let\label\@gobble
    \let\index\@gobble
    \let\glossary\@gobble
    \@fileswfalse
    \csq@blockfalse
    \def\par{\csq@blocktrue\@par}%
    \setbox\@tempboxa=\vbox{%
      \parindent\z@
      \vrule \@height1\baselineskip \@width\z@ #1#5%
      \ifcsq@block\aftergroup\csq@blocktrue\fi}%
    \ifdim\ht\@tempboxa>\csq@threshold\baselineskip
      \csq@blocktrue
    \fi
    \csq@tempa
  \expandafter\endgroup\ifcsq@block
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
    {\csq@bquote@ii{#1}{#2}{#3}{#4}{#5}}
    {\csq@bquote@iii{#1}{#2}{#3}{#4}{#5}}}

\long\def\csq@bquote@ii#1#2#3#4#5{%
  \csq@checkblockcite
  \begin{\csq@blockenvironment}%
    \csq@blocktrue
    \begingroup #1%
      \csq@setsfcodes
      \quoteblock
        #5%
      \endquoteblock
      \csq@setlastsf
      \mkmidblockpunct{#4}%
    \expandafter\endgroup
    \expandafter\csq@lastsf\the\csq@lastsf\relax
    \csq@ifblank{#3}
      {}
      {#2{#3}}%
    \mkfinblockpunct{#4}%
  \end{\csq@blockenvironment}}

\long\def\csq@bquote@iii#1#2#3#4#5{%
  \csq@checkblockcite
  \csq@bqgroup
    \def\csq@tempa{%
      \csq@qclose\mkmidblockpunct{#4}}%
    \csq@ifblank{#3}
      {}
      {\csq@add\csq@tempa{#2{#3}}}%
    #1\csq@qopen@i\quotetext #5\endquotetext
  \csq@tempa\mkfinblockpunct{#4}}

% Block quotes + babel

\DeclareRobustCommand*{\foreignblockquote}[1]{%
  \csq@ifmmode
    {\csq@err@mmode\foreignblockquote}
    {\csq@getcargs{\csq@bquote{\csq@lang{#1}}}}}

\DeclareRobustCommand*{\hyphenblockquote}[1]{%
  \csq@ifmmode
    {\csq@err@mmode\hyphenblockquote}
    {\csq@getcargs{\csq@bquote{\csq@hyph{#1}}}}}

% Integrated block quotes

\DeclareRobustCommand*{\blockcquote}{%
  \csq@ifmmode
    {\csq@err@mmode\blockcquote}
    {\csq@getccargs{\csq@bquote{}}}}

% Integrated block quotes + babel

\DeclareRobustCommand*{\foreignblockcquote}[1]{%
  \csq@ifmmode
    {\csq@err@mmode\foreignblockcquote}
    {\csq@getccargs{\csq@bquote{\csq@lang{#1}}}}}

\DeclareRobustCommand*{\hyphenblockcquote}[1]{%
  \csq@ifmmode
    {\csq@err@mmode\hyphenblockcquote}
    {\csq@getccargs{\csq@bquote{\csq@hyph{#1}}}}}

% PDF strings

\csq@add\csq@mkpdfstrings{%
  \def\blockquote{\csq@warn@pdfstrings\blockquote}%
  \def\foreignblockquote{\csq@warn@pdfstrings\foreignblockquote}%
  \def\hyphenblockquote{\csq@warn@pdfstrings\hyphenblockquote}%
  \def\blockcquote{\csq@warn@pdfstrings\blockcquote}%
  \def\foreignblockcquote{\csq@warn@pdfstrings\foreignblockcquote}%
  \def\hyphenblockcquote{\csq@warn@pdfstrings\hyphenblockcquote}}

% Display quotes

\newenvironment*{displayquote}
  {\csq@getcargs{\csq@dquote{}}}
  {\csq@edquote}

% {<init>}{<hook>}{<cite>}{<punct>}

\def\csq@dquote#1#2#3#4{%
  \csq@checkblockcite
  \csq@ifblank{#3}
    {\let\csq@tempb\@empty}
    {\def\csq@tempb{#2{#3}}}%
  \def\csq@tempc{#4}%
  \csq@bdquote{#1}}

\def\csq@bdquote#1{%
  \@nameuse{\csq@blockenvironment}%
  \csq@blocktrue
  \begingroup #1%
  \csq@setsfcodes
  \quoteblock\ignorespaces}

\def\csq@edquote{%
  \ifdim\lastskip>\z@\unskip\fi
  \endquoteblock
  \csq@setlastsf
  \expandafter\mkmiddisppunct
  \expandafter{\csq@tempc}%
  \expandafter\endgroup
  \expandafter\csq@lastsf\the\csq@lastsf\relax
  \csq@tempb
  \expandafter\mkfindisppunct
  \expandafter{\csq@tempc}%
  \@nameuse{end\csq@blockenvironment}}

% Display quotes + babel

\newenvironment*{foreigndisplayquote}[1]
  {\csq@getcargs{\csq@dquote{\csq@lang{#1}}}}
  {\csq@edquote}

\newenvironment*{hyphendisplayquote}[1]
  {\csq@getcargs{\csq@dquote{\csq@hyph{#1}}}}
  {\csq@edquote}

% Integrated display quotes

\newenvironment*{displaycquote}
  {\csq@getccargs{\csq@dcquote{}}}
  {\csq@edquote}

% {<init>}{<hook>}{<cite>}{<punct>}

\def\csq@dcquote#1#2#3#4{%
  \def\csq@tempb{#2{#3}}%
  \def\csq@tempc{#4}%
  \csq@bdquote{#1}}

% Integrated display quotes + babel

\newenvironment*{foreigndisplaycquote}[1]
  {\csq@getccargs{\csq@dcquote{\csq@lang{#1}}}}
  {\csq@edquote}

\newenvironment*{hyphendisplaycquote}[1]
  {\csq@getccargs{\csq@dcquote{\csq@hyph{#1}}}}
  {\csq@edquote}

% Hooks

\newcommand*{\mkcitation}[1]{\space (#1)}
\newcommand*{\mkccitation}[1]{\space #1}

\newcommand*{\mkmidtextpunct}[1]{}
\newcommand*{\mkfintextpunct}[1]{#1}
\newcommand*{\mkmidblockpunct}[1]{}
\newcommand*{\mkfinblockpunct}[1]{#1}
\newcommand*{\mkmiddisppunct}[1]{}
\newcommand*{\mkfindisppunct}[1]{#1}

\newenvironment*{quotetext}{}{}
\newenvironment*{quoteblock}{}{}

% Legacy commands

\def\csq@err@blockcite{%
  \csq@error
    {Command `\string\blockcite' is depreciated}
    {Please use `\string\mkcitation' instead}}
\let\blockcite\csq@err@blockcite

\def\csq@checkblockcite{%
  \ifx\blockcite\csq@err@blockcite
  \else
    \csq@warning
      {`\string\blockcite' is superseded by `\string\mkcitation'}
      {As a temporary solution, I will copy the current
       definition\MessageBreak of `\string\blockcite' to
       `\string\mkcitation'}
      {Command `\string\blockcite' is depreciated}
      {Please use `\string\mkcitation' instead}%
    \let\mkcitation\blockcite
    \let\blockcite\csq@err@blockcite
  \fi}

\newcommand*{\cquote}{%
  \csq@info@legacy\cquote\textcquote}
\newcommand*{\foreigncquote}{%
  \csq@info@legacy\foreigncquote\foreigntextcquote}
\newcommand*{\hyphencquote}{%
  \csq@info@legacy\hyphencquote\hyphentextcquote}

%% Markup with active characters

% Manage active characters

\let\csq@dospecials\@empty
\let\csq@dodelimiters\@empty

\def\csq@do@dospecial#1#2{%
  \ifnum\catcode`#1=\active
    \csq@info{Defining active character \string #1}%
  \else
    \csq@info{Making \string #1 an active character}%
  \fi
  \csq@edefspecial{#1}{\noexpand\csq@special{\number`#1}}}

\def\csq@do@undospecial#1#2{%
  \ifnum#2=\active
    \csq@info{Resetting active character \string #1}%
    \catcode`#1=\active
    \expandafter\let
    \expandafter #1\csname csq@saved@\number`#1\endcsname
  \else
    \csq@info{Resetting catcode of character \string #1}%
    \catcode`#1=#2
  \fi}

\def\csq@do@verbspecial#1#2{%
  \ifnum`#1<128
    \csq@edefspecial{#1}{\string #1}%
  \else
    \csq@edefspecial{#1}{%
      \noexpand\@nameuse{csq@saved@\number`#1}}%
  \fi}

\def\csq@do@fixdelim#1#2{%
  \ifnum\catcode`#1=#2
  \else
    \csq@info{Restoring catcode of character \string #1}%
    \catcode`#1=#2
  \fi}

% Register active characters

\def\csq@addspecial#1#2#3{%
  \csq@checkquote{#1}{\csq@addspecial@i{#1}{#2}{#3}}}

% {<char>}{<activedef>}{<pdfstrdef>}

\def\csq@addspecial@i#1#2#3{%
  \@namedef{csq@string@\number`#1}{\string #1}%
  \@namedef{csq@active@\number`#1}{#2}%
  \@namedef{csq@pdfstr@\number`#1}{#3}%
  \ifnum\catcode`#1=\active
    \expandafter\let\csname csq@saved@\number`#1\endcsname #1%
  \else
    \@namedef{csq@saved@\number`#1}{#1}%
  \fi
  \csq@eadd\csq@dospecials{%
    \noexpand\do{\noexpand #1}{\the\catcode`#1}}%
  \ifnum`#1<128
    \csq@addto@dolist{#1}{\dospecials}{\do}%
  \fi
  \csq@addto@dolist{#1}{\@sanitize}{\@makeother}}

% {<char>}{<omark>}{<cmark>}

\def\csq@addpdfspecial#1#2#3{%
  \csq@add\csq@mkpdfstrings{\csq@defpdfspecial{#1}{#2}{#3}}}

\def\csq@addbspecial#1#2#3#4{%
  \csq@checkquote{#1}{%
    \@namedef{csq@saved@\number`#1}{}%
    \csq@checkdelim{#2}{%
      \@namedef{csq@delim@\number`#2}{}%
      \csq@checkdelim{#3}{%
        \@namedef{csq@delim@\number`#3}{}%
        \ifnum`#2=`#3
          \csq@err@alloc{#2}%
        \else
          \csq@addbspecial@i{#1}{#2}{#3}{#4}%
        \fi}}}}

% {<char1>}{<delim>}{<char2>}{<activedef>}

\def\csq@addbspecial@i#1#2#3#4{%
  \begingroup
    \edef\csq@tempa{\endgroup
      \noexpand\csq@addspecial@i
        {\noexpand #1}
        {\csname csq@block@\number`#1\endcsname}%
        {\noexpand\csq@warn@pdfstrings{\string #1}}%
      \noexpand\csq@defbspecial
        {\csname csq@block@\number`#1\endcsname}}%
    \csq@add\csq@tempa{{#2}{#3}{#4}}%
  \csq@tempa
  \csq@eadd\csq@dodelimiters{%
    \noexpand\do{\noexpand #2}{\the\catcode`#2}%
    \noexpand\do{\noexpand #3}{\the\catcode`#3}}}

% Define and use active characters

\def\csq@edefspecial#1#2{%
  \begingroup
    \catcode`\~=\active
    \uccode`\~=`#1\relax
  \uppercase{\endgroup\edef~}{#2}%
  \catcode`#1=\active}

\def\csq@defpdfspecial#1#2#3{%
  \begingroup
    \catcode`\~=\active
    \uccode`\~=`#1\relax
  \uppercase{\endgroup\def~##1~}{#2##1#3}}

% {<csname>}{<delim>}{<char2>}{<macro>}

\def\csq@defbspecial#1#2#3#4{%
  \long\def#1##1#3{\csq@bspecial{##1}{#2}{#4}}}

\def\csq@special{%
  \ifx\if@safe@actives\iftrue
    \expandafter\csq@special@string
  \else
    \ifx\protect\@typeset@protect
      \ifmmode
        \expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter
        \expandafter\csq@special@saved
      \else
        \expandafter\expandafter\expandafter
        \expandafter\expandafter\expandafter
        \expandafter\csq@special@active
      \fi
    \else
      \ifx\protect\string
        \string\csq@special
      \else
        \noexpand\csq@special
      \fi
    \fi
  \fi}

\def\csq@special@saved#1{\@nameuse{csq@saved@#1}}
\def\csq@special@string#1{\@nameuse{csq@string@#1}}
\def\csq@special@active#1{\@nameuse{csq@active@#1}}
\def\csq@special@pdfstr#1{\@nameuse{csq@pdfstr@#1}}

\csq@add\csq@mkpdfstrings{\let\csq@special\csq@special@pdfstr}

% {<text>}{<delim>}{<macro>}

\begingroup
  \catcode`\&=3

  \long\gdef\csq@bspecial#1#2#3{%
    \long\def\csq@bspecial@i##1#2##2&{%
      \csq@ifblank{##2}
        {#3{}{}{#1}}
        {\csq@bspecial@ii#1&}}%
    \long\def\csq@bspecial@ii##1#2##2&{%
      #3{##2}{}{##1}}%
    \csq@bspecial@i#1#2&}

\endgroup

% Validate user input

\def\csq@checkquote#1{%
  \csq@ifsingle{#1}
    {\csq@ifccode@quote{#1}
       {\csq@ifcsundefined{csq@saved@\number`#1}
          {\csq@ifcsundefined{csq@delim@\number`#1}
             {\@firstofone}
             {\csq@err@alloc{#1}\@gobble}}
          {\csq@err@alloc{#1}\@gobble}}
       {\csq@err@invquote\@gobble}}
    {\csq@err@invquote\@gobble}}

\def\csq@checkdelim#1{%
  \csq@ifsingle{#1}
    {\csq@ifccode@delim{#1}
       {\csq@ifcsundefined{csq@saved@\number`#1}
          {\@firstofone}
          {\csq@err@alloc{#1}\@gobble}}
       {\csq@err@invdelim\@gobble}}
    {\csq@err@invdelim\@gobble}}

\def\csq@ifccode@quote#1{%
  \begingroup
    \@tempswafalse
    \ifcase\catcode`#1 %  0 escape
    \or                %  1 begin group
    \or                %  2 end group
    \or                %  3 math shift
    \or                %  4 alignment
    \or                %  5 eol
    \or                %  6 parameter
    \or\@tempswatrue   %  7 superscript
    \or\@tempswatrue   %  8 subscript
    \or                %  9 ignored
    \or                % 10 space
    \or                % 11 letter
    \or\@tempswatrue   % 12 other
    \or\@tempswatrue   % 13 active
    \or                % 14 comment
    \else              % 15 invalid
    \fi
  \expandafter\endgroup\if@tempswa
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\def\csq@ifccode@delim#1{%
  \begingroup
    \@tempswafalse
    \ifcase\catcode`#1 %  0 escape
    \or                %  1 begin group
    \or                %  2 end group
    \or\@tempswatrue   %  3 math shift
    \or\@tempswatrue   %  4 alignment
    \or                %  5 eol
    \or                %  6 parameter
    \or\@tempswatrue   %  7 superscript
    \or\@tempswatrue   %  8 subscript
    \or                %  9 ignored
    \or                % 10 space
    \or                % 11 letter
    \or\@tempswatrue   % 12 other
    \or\@tempswatrue   % 13 active
    \or                % 14 comment
    \else              % 15 invalid
    \fi
  \expandafter\endgroup\if@tempswa
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

% User interface

\newcommand*{\MakeOuterQuote}[1]{%
  \csq@addspecial{#1}{\csq@oqmark}{}%
  \csq@addpdfspecial{#1}{\csq@pdf@ooqmark}{\csq@pdf@coqmark}}
\@onlypreamble\MakeOuterQuote

\newcommand*{\MakeInnerQuote}[1]{%
  \csq@addspecial{#1}{\csq@iqmark}{}%
  \csq@addpdfspecial{#1}{\csq@pdf@oiqmark}{\csq@pdf@ciqmark}}
\@onlypreamble\MakeInnerQuote

\newcommand*{\MakeAutoQuote}[2]{%
  \csq@addspecial{#1}{\csq@qopen}{\csq@pdf@ooqmark}%
  \csq@addspecial{#2}{\csq@qclose}{\csq@pdf@coqmark}}
\@onlypreamble\MakeAutoQuote

\newcommand*{\MakeForeignQuote}[3]{%
  \csq@addspecial{#2}{\csq@qopen@lang{#1}}{\csq@pdf@ooqmark}%
  \csq@addspecial{#3}{\csq@qclose}{\csq@pdf@coqmark}}
\@onlypreamble\MakeForeignQuote

\newcommand*{\MakeHyphenQuote}[3]{%
  \csq@addspecial{#2}{\csq@qopen@hyph{#1}}{\csq@pdf@ooqmark}%
  \csq@addspecial{#3}{\csq@qclose}{\csq@pdf@coqmark}}
\@onlypreamble\MakeHyphenQuote

\newcommand*{\MakeBlockQuote}[3]{%
  \csq@addbspecial{#1}{#2}{#3}{%
    \csq@bquote{}{\mkcitation}}}
\@onlypreamble\MakeBlockQuote

\newcommand*{\MakeForeignBlockQuote}[4]{%
  \csq@addbspecial{#2}{#3}{#4}{%
    \csq@bquote{\csq@lang{#1}}{\mkcitation}}}
\@onlypreamble\MakeForeignBlockQuote

\newcommand*{\MakeHyphenBlockQuote}[4]{%
  \csq@addbspecial{#2}{#3}{#4}{%
    \csq@bquote{\csq@hyph{#1}}{\mkcitation}}}
\@onlypreamble\MakeHyphenBlockQuote

\newcommand*{\RestoreQuotes}{%
  \csq@warn@location\RestoreQuotes}
\newcommand*{\DisableQuotes}{%
  \csq@warn@location\DisableQuotes}

\AtBeginDocument{%
  \def\RestoreQuotes{%
    \let\do\csq@do@dospecial
    \csq@dospecials
    \let\do\csq@do@fixdelim
    \csq@dodelimiters
    \csq@activetrue
    \let\do\noexpand}%
  \def\DisableQuotes{%
    \let\do\csq@do@undospecial
    \csq@dospecials
    \csq@activefalse
    \let\do\noexpand}}

% Author interface

\newcommand*{\@restorequotes}{}
\newcommand*{\@disablequotes}{}
\newcommand*{\@verbatimquotes}{}

\AtBeginDocument{%
  \def\@restorequotes{%
    \csq@quiettrue
    \let\do\csq@do@dospecial
    \csq@dospecials
    \let\do\csq@do@fixdelim
    \csq@dodelimiters
    \csq@quietfalse
    \csq@activetrue
    \let\do\noexpand}%
  \def\@disablequotes{%
    \csq@quiettrue
    \let\do\csq@do@undospecial
    \csq@dospecials
    \csq@quietfalse
    \csq@activefalse
    \let\do\noexpand}%
  \def\@verbatimquotes{%
    \ifcsq@active
      \csq@quiettrue
      \let\do\csq@do@verbspecial
      \csq@dospecials
      \csq@quietfalse
      \let\do\noexpand
    \fi}}

%% Switch quote styles manually

\newcommand*{\setquotestyle}{%
  \@ifstar
    {\csq@resetqstyle}
    {\@ifnextchar[%]
       {\csq@setqstyle}
       {\csq@setqstyle[]}}}

\def\csq@setqstyle[#1]#2{%
  \ifcsq@reset
    \csq@info{Disabling multilingual support}%
    \csq@resetfalse
  \fi
  \csq@ifblank{#1}
    {\csq@setstyle{#2}}
    {\csq@setstyle{#2/#1}}}

\def\csq@resetqstyle{%
  \csq@info{Enabling multilingual support}%
  \csq@resettrue}

%% Define quote styles

\newcommand*{\DeclareQuoteStyle}[2][]{%
  \begingroup
  \csq@ifblank{#1}
    {\def\csq@tempa{#2}}
    {\def\csq@tempa{#2/#1}}%
  \csq@ifcsundefined{csq@qstyle@\csq@tempa}
    {}
    {\csq@info{Redefining quotation style `\csq@tempa'}}%
  \@ifnextchar[%]
    {\csq@declareqstyle@i}
    {\csq@declareqstyle@i[]}}

\def\csq@declareqstyle@i[#1]{%
  \csq@csgdef{csq@qstyle@\csq@tempa}{%
    \def\csq@theqmark@oinit{#1}}%
  \@ifnextchar[%]
    {\csq@declareqstyle@ii}
    {\csq@declareqstyle@ii[]}}

\def\csq@declareqstyle@ii[#1]#2{%
  \csq@csgadd{csq@qstyle@\csq@tempa}{%
    \def\csq@theqmark@iinit{#1}%
    \def\csq@theqmark@oopen{#2}}%
  \@ifnextchar[%]
    {\csq@declareqstyle@iii}
    {\csq@declareqstyle@iii[]}}

\def\csq@declareqstyle@iii[#1]#2{%
  \csq@csgadd{csq@qstyle@\csq@tempa}{%
    \def\csq@theqmark@omiddle{#1}%
    \def\csq@theqmark@oclose{#2}}%
  \@ifnextchar[%]
    {\csq@declareqstyle@iv}
    {\csq@declareqstyle@iv[]}}

\def\csq@declareqstyle@iv[#1]#2{%
  \csq@ifblank{#1}
    {\def\csq@theqmark@kern{\z@}}
    {\def\csq@theqmark@kern{#1}}%
  \csq@csxadd{csq@qstyle@\csq@tempa}{%
    \def\noexpand\csq@theqmark@kern{\csq@theqmark@kern}}%
  \csq@csgadd{csq@qstyle@\csq@tempa}{%
    \def\csq@theqmark@iopen{#2}}%
  \@ifnextchar[%]
    {\csq@declareqstyle@v}
    {\csq@declareqstyle@v[]}}

\def\csq@declareqstyle@v[#1]#2{%
  \csq@csgadd{csq@qstyle@\csq@tempa}{%
    \def\csq@theqmark@imiddle{#1}%
    \def\csq@theqmark@iclose{#2}}%
  \endgroup}

\@onlypreamble\DeclareQuoteStyle

%% Define quote aliases

\newcommand*{\DeclareQuoteAlias}[3][]{%
  \begingroup
  \csq@ifblank{#1}
    {\def\csq@tempa{#2}}
    {\def\csq@tempa{#2/#1}}%
  \csq@ifcsundefined{csq@qstyle@\csq@tempa}
    {\csq@error
       {Quote style `\csq@tempa' not defined}
       {Define the style or alias first before using it}}
    {\csq@ifcsundefined{csq@qstyle@#3}
       {}
       {\csq@info{Redefining alias `#3' -> `\csq@tempa'}}%
     \csq@csxdef{csq@qstyle@#3}{%
       \noexpand\@nameuse{csq@qstyle@\csq@tempa}}}%
  \endgroup}

\@onlypreamble\DeclareQuoteAlias

%% Define quotes for PDF strings

\newcommand*{\DeclarePlainStyle}[4]{%
  \gdef\csq@pdf@ooqmark{#1}%
  \gdef\csq@pdf@coqmark{#2}%
  \gdef\csq@pdf@oiqmark{#3}%
  \gdef\csq@pdf@ciqmark{#4}}

\@onlypreamble\DeclarePlainStyle

%% Define package options

\newcommand*{\DeclareQuoteOption}[1]{%
  \define@key{csq}{#1}{\DeclareQuoteAlias{#1/##1}{#1}}}

\AtEndOfPackage{%
  \def\DeclareQuoteOption#1{%
    \csq@warn@location\DeclareQuoteOption}}

%% Set defaults

\newcommand*{\SetBlockThreshold}[1]{%
  \csq@threshold=#1\relax}

\newcommand*{\SetBlockEnvironment}[1]{%
  \csq@ifcsundefined{#1}
    {\csq@err@notdef{#1}}
    {\def\csq@blockenvironment{#1}}}

\newcommand*{\SetCiteCommand}[1]{%
  \csq@ifundefined{#1}
    {\csq@err@notdef{#1}}
    {\def\csq@cite{#1}}}

% Legacy commands

\newcommand*{\setblockthreshold}{%
  \csq@info@legacy\setblockthreshold\SetBlockThreshold}

\newcommand*{\setblockenvironment}{%
  \csq@info@legacy\setblockenvironment\SetBlockEnvironment}

%% Frontend to internal marks 

\newcommand*{\textooquote}{%
  \csq@resetstyle
  \csq@theqmark@oopen}

\newcommand*{\textmoquote}{%
  \csq@resetstyle
  \csq@theqmark@omiddle}

\newcommand*{\textcoquote}{%
  \csq@resetstyle
  \csq@theqmark@oclose}

\newcommand*{\textoiquote}{%
  \csq@resetstyle
  \csq@theqmark@iopen}

\newcommand*{\textmiquote}{%
  \csq@resetstyle
  \csq@theqmark@imiddle}

\newcommand*{\textciquote}{%
  \csq@resetstyle
  \csq@theqmark@iclose}

%% Space factor codes

\@namedef{csq@sfcodes@OT1}{%
  \sfcode 96=\z@ % textquoteleft
  \sfcode 39=\z@ % textquoteright
  \sfcode 92=\z@ % textquotedblleft
  \sfcode 34=\z@ % textquotedblright
}

\@namedef{csq@sfcodes@T1}{%
  \sfcode 96=\z@ % textquoteleft
  \sfcode 39=\z@ % textquoteright
  \sfcode 16=\z@ % textquotedblleft
  \sfcode 17=\z@ % textquotedblright
  \sfcode 13=\z@ % quotesinglbase
  \sfcode 18=\z@ % quotedblbase
  \sfcode 14=\z@ % guilsinglleft
  \sfcode 15=\z@ % guilsinglright
  \sfcode 19=\z@ % guillemotleft
  \sfcode 60=\z@ % guillemotleft  (<< ligature)
  \sfcode 20=\z@ % guillemotright
  \sfcode 62=\z@ % guillemotright (>> ligature)
  \sfcode 34=\z@ % textquotedbl
}

\@namedef{csq@sfcodes@LY1}{%
  \sfcode 96=\z@ % textquoteleft
  \sfcode 39=\z@ % textquoteright
  \sfcode147=\z@ % textquotedblleft
  \sfcode148=\z@ % textquotedblright
  \sfcode130=\z@ % quotesinglbase
  \sfcode132=\z@ % quotedblbase
  \sfcode139=\z@ % guilsinglleft
  \sfcode155=\z@ % guilsinglright
  \sfcode171=\z@ % guillemotleft
  \sfcode187=\z@ % guillemotright
  \sfcode129=\z@ % textquotesingle
  \sfcode 34=\z@ % textquotedbl
}

%% Auxiliary commands for some styles

% Wrappers for Omega primitives

\newcommand*{\seteverylineleft}[1]{%
  \csq@ifcsundefined{localleftbox}
    {\csq@warn@noomega\csq@currentstyle}
    {\leavevmode\localleftbox{#1}}}

\newcommand*{\seteverylineright}[1]{%
  \csq@ifcsundefined{localrightbox}
    {\csq@warn@noomega\csq@currentstyle}
    {\localrightbox{#1}}}

% Improved spacing for French

\newcommand*{\frenchnbspace}{%
  \penalty\@M\hskip 0.8\fontdimen2\font
   plus 0.3\fontdimen3\font minus 0.8\fontdimen4\font}

\newcommand*{\mkfrenchopenquote}[1]{%
  \leavevmode #1\frenchnbspace}

\newcommand*{\mkfrenchclosequote}[1]{%
  \frenchnbspace #1}

\newcommand*{\initfrenchquotes}{%
  \let\csq@themark\csq@frenchmark}

% Prevent undesirable T1 ligatures (?` and !`)

\newcommand*{\fixligatures}{%
  \ifhmode
    \ifnum\spacefactor>3000
      \relax
    \else
      \ifnum\spacefactor=1005
	\relax
      \else
	\ifnum\spacefactor=1006
	  \relax
	\fi
      \fi
    \fi
  \fi}

%% Built-in backend styles

\DeclareQuoteStyle[quotes]{danish}
  {\quotedblbase}
  {\textquotedblleft}
  [0.05em]
  {\textquoteright}% ???
  {\textquoteright}% ???
\DeclareQuoteStyle[guillemets]{danish}
  {\guillemotright}
  {\guillemotleft}
  {\textquoteright}% ???
  {\textquoteright}% ???
\DeclareQuoteStyle{dutch}
  {\quotedblbase}
  {\textquotedblright}
  [0.05em]
  {\quotesinglbase}% ???
  {\textquoteright}% ???
\DeclareQuoteStyle[american]{english}% verified
  {\textquotedblleft}
  {\textquotedblright}
  [0.05em]
  {\textquoteleft}
  {\textquoteright}
\DeclareQuoteStyle[british]{english}% verified
  {\textquoteleft}
  {\textquoteright}
  [0.05em]
  {\textquotedblleft}
  {\textquotedblright}
\DeclareQuoteStyle[oldstyle]{english}% verified
  [\seteverylineleft{\textquoteleft}]
  {\textquoteleft}
  {\textquoteright}
  [0.05em]
  {\textquotedblleft}
  {\textquotedblright}
\DeclareQuoteStyle{finnish}
  {\textquotedblright}
  {\textquotedblright}
  [0.05em]
  {\textquoteright}
  {\textquoteright}
\DeclareQuoteStyle[quotes]{french}% verified
  {\mkfrenchopenquote{\guillemotleft}}
  {\mkfrenchclosequote{\guillemotright}}
  {\mkfrenchopenquote{\textquotedblleft}}
  {\mkfrenchclosequote{\textquotedblright}}
\DeclareQuoteStyle[guillemets]{french}
  [\initfrenchquotes]
  {\mkfrenchopenquote{\guillemotleft}}
  [\mkfrenchopenquote{\guillemotleft}]
  {\mkfrenchclosequote{\guillemotright}}
  {\mkfrenchopenquote{\guillemotleft}}
  {\mkfrenchclosequote{\guillemotright}}
\DeclareQuoteStyle[guillemets*]{french}
  [\initfrenchquotes]
  {\mkfrenchopenquote{\guillemotleft}}
  [\mkfrenchopenquote{\guillemotright}]
  {\mkfrenchclosequote{\guillemotright}}
  {\mkfrenchopenquote{\guillemotleft}}
  {\mkfrenchclosequote{\guillemotright}}
\DeclareQuoteStyle[oldstyle]{french}
  [\initfrenchquotes
   \seteverylineleft{\mkfrenchopenquote{\guillemotleft}}]
  {\mkfrenchopenquote{\guillemotleft}}
  {\mkfrenchclosequote{\guillemotright}}
  {\mkfrenchopenquote{\guillemotleft}}
  {\mkfrenchclosequote{\guillemotright}}
\DeclareQuoteStyle[imprimerie]{french}
  [\initfrenchquotes]
  [\seteverylineleft{\mkfrenchopenquote{\guillemotleft}}]
  {\mkfrenchopenquote{\guillemotleft}}
  [\mkfrenchopenquote{\guillemotleft}]
  {\mkfrenchclosequote{\guillemotright}}
  {\mkfrenchopenquote{\guillemotleft}}
  [\relax]
  {\mkfrenchclosequote{\guillemotright}}
\DeclareQuoteStyle[quotes]{german}% verified
  {\quotedblbase}
  {\textquotedblleft}
  [0.05em]
  {\quotesinglbase}
  {\fixligatures\textquoteleft}
\DeclareQuoteStyle[guillemets]{german}% verified
  {\guillemotright}
  {\guillemotleft}
  [0.025em]
  {\guilsinglright}
  {\guilsinglleft}
\DeclareQuoteStyle[swiss]{german}% verified
  {\guillemotleft}
  {\guillemotright}
  [0.025em]
  {\guilsinglleft}
  {\guilsinglright}
\DeclareQuoteStyle[quotes]{italian}
  {\textquotedblleft}
  {\textquotedblright}
  [0.05em]
  {\textquoteleft}
  {\textquoteright}
\DeclareQuoteStyle[guillemets]{italian}
  {\guillemotleft}
  [\textquotedblright]% ???
  {\guillemotright}
  {\textquotedblleft}
  {\textquotedblright}
\DeclareQuoteStyle[guillemets]{norwegian}
  {\guillemotleft}
  {\guillemotright}
  [0.025em]
  {\guilsinglleft}
  {\guilsinglright}
\DeclareQuoteStyle[quotes]{norwegian}
  {\quotedblbase}
  {\textquotedblright}
  [0.05em]
  {\quotesinglbase}
  {\textquoteright}
\DeclareQuoteStyle[quotes]{swedish}% verified
  {\textquotedblright}
  {\textquotedblright}
  [0.05em]
  {\textquoteright}
  {\textquoteright}
\DeclareQuoteStyle[guillemets]{swedish}% verified
  {\guillemotright}
  {\guillemotright}
  [0.025em]
  {\guilsinglright}
  {\guilsinglright}

% Fallback style

\DeclareQuoteStyle{fallback}
  {\csq@dummymark}
  {\csq@dummymark}
  {\csq@dummymark}
  {\csq@dummymark}

\def\csq@dummymark{\mbox{\kern0.1em\textbf{?}\kern0.1em}}
\def\csq@errormark{\mbox{\kern0.1em\rule{1ex}{1ex}\kern0.1em}}

% PDF strings

\DeclarePlainStyle
  {\string"}
  {\string"}
  {\string'}
  {\string'}

%% Built-in aliases

\DeclareQuoteAlias[american]{english}{american}
\DeclareQuoteAlias[american]{english}{canadian}
\DeclareQuoteAlias[american]{english}{english}
\DeclareQuoteAlias[british]{english}{british}
\DeclareQuoteAlias[guillemets]{norwegian}{norwegian}
\DeclareQuoteAlias[quotes]{danish}{danish}
\DeclareQuoteAlias[quotes]{french}{french}
\DeclareQuoteAlias[quotes]{german}{austrian}
\DeclareQuoteAlias[quotes]{german}{german}
\DeclareQuoteAlias[quotes]{italian}{italian}
\DeclareQuoteAlias[quotes]{swedish}{swedish}
\DeclareQuoteAlias[swiss]{german}{swiss}

% Babel aliases

\DeclareQuoteAlias{american}{USenglish}
\DeclareQuoteAlias{american}{usenglish}
\DeclareQuoteAlias{british}{UKenglish}
\DeclareQuoteAlias{british}{ukenglish}
\DeclareQuoteAlias{german}{ngerman}
\DeclareQuoteAlias{austrian}{naustrian}
\DeclareQuoteAlias{norwegian}{norsk}
\DeclareQuoteAlias{norwegian}{nynorsk}

%% Package options

% Core options

\define@key{csq}{strict}[true]{%
  \expandafter\ifx\csname if#1\endcsname\iftrue
    \csq@info{Enabling strict warnings}%
    \csq@stricttrue
  \else
    \csq@info{Disabling strict warnings}%
    \csq@strictfalse
  \fi}

\define@key{csq}{babel}[true]{%
  \expandafter\ifx\csname if#1\endcsname\iftrue
    \csq@info{Enabling multilingual support}%
    \csq@resettrue
  \else
    \csq@info{Disabling multilingual support}%
    \csq@resetfalse
  \fi}

\define@key{csq}{style}{%
  \csq@quiettrue
  \DeclareQuoteAlias{#1}{default}%
  \csq@quietfalse
  \csq@info{Disabling multilingual support}%
  \csq@resetfalse}

% Language options

\DeclareQuoteOption{danish}
\DeclareQuoteOption{english}
\DeclareQuoteOption{french}
\DeclareQuoteOption{german}
\DeclareQuoteOption{italian}
\DeclareQuoteOption{norwegian}
\DeclareQuoteOption{swedish}

%% Initial setup

% Set defaults

\DeclareQuoteAlias[american]{english}{default}

\csq@ifcsundefined{quote}
  {\csq@info{%
     The `quote' environment appears to be undefined.\MessageBreak
     I'm defining a typical `quote' environment now}%
   \def\quote{%
     \list{}{\rightmargin\leftmargin}%
     \item\relax}%
   \def\endquote{\endlist}}
  {}

\SetBlockEnvironment{quote}
\SetBlockThreshold{3}
\SetCiteCommand{\cite}

% Load configuration file

\csq@info{Trying to load configuration file `csquotes.cfg'..}

\let\csq@tempa\ExecuteOptions
\def\ExecuteOptions#1{\setkeys{csq}{#1}}

\InputIfFileExists{csquotes.cfg}
  {\csq@info{... configuration file loaded successfully}}
  {\csq@info{... configuration file not used}}

\let\ExecuteOptions\csq@tempa
\let\csq@tempa\@undefined

% Process options

\DeclareOption*{%
  \begingroup
  \edef\csq@tempa{\endgroup
    \noexpand\setkeys{csq}{\CurrentOption}}%
  \csq@tempa}

\ProcessOptions*

%% Last minute setup

\AtBeginDocument{%
  \csq@setstyle{default}%
  \let\do\csq@do@dospecial
  \csq@dospecials
  \let\do\csq@do@fixdelim
  \csq@dodelimiters
  \csq@activetrue
  \let\do\noexpand
  \@ifpackageloaded{babel}
    {\csq@mkbabel}
    {\csq@mknobabel}%
  \@ifpackageloaded{hyperref}
    {\let\csq@warn@pdfstrings\pdfstringdefWarn
     \csq@add\pdfstringdefPreHook{\csq@mkpdfstrings}}
    {\let\csq@warn@pdfstrings\@gobble}%
  \csq@add\@noligs{\@verbatimquotes}%
  \@ifpackageloaded{verbatim}
    {\addto@hook\every@verbatim{\@verbatimquotes}}
    {}%
  \@ifpackageloaded{fancyvrb}
    {\csq@add\FV@CatCodes{\@verbatimquotes}}
    {}%
  \csq@checkblockcite}

\endinput

% AUTHOR INTERFACE

% Controlling active quotes

% This package provides three commands for class and package authors
% who need to temporarily disable the active quotes for some reason.
%
% \@disablequotes
% 
% This command restores the status quo ante of all active quotes.
% This can mean one of two things:
%
% 1) If a character had been active when it was defined as active
%    quote, its previous definition will be restored.
%
% 2) If a character had not been active when it was defined as active
%    quote, its previous category code will be restored.
%
% Note that all active quotes are made active \AtBeginDocument hence
% you will only need \@disablequotes if you're defining anything
% \AtBeginDocument or later.
%
% \@restorequotes
%
% This command restores csquotes's definitions of all active quotes
% and makes them active. The category codes of delimiter characters
% (used for block quotes) are also restored if they have changed.
%
% All changes made by \@disablequotes and \@verbatimquotes are local
% and may thus be restricted to a group. If you use proper grouping
% you may omit \@restorequotes.
%
% The author commands \@restorequotes and \@disablequotes differ from
% the user commands \RestoreQuotes and \DisableQuotes in two ways:
%
% 1) When used in the preamble, they will do nothing (the user
%    commands would issue a warning since the active quotes are not
%    yet defined at this point).
%
% 2) When used in the document body, they will essentially behave
%    like the user commands, except that all informational messages
%    are suppressed. The author commands will work quietly behind the
%    scenes.
%
% \@verbatimquotes
% 
% For verbatim environments and comparable applications, use
% \@verbatimquotes instead of \@disablequotes. \@verbatimquotes
% redefines the active quotes in a way that it suitable for a
% verbatim context. It will do the following:
%
% 1) If a character is in the Ascii range (character code <128), it
%    will be redefined so that it expands to itself with category
%    code 12.
%
% 2) If a character is not in the Ascii range, there are two
%    possibilities:
%
%    a) If the character had been active when it was defined as
%       active quote, it will be redefined so that it expands to its
%       previous definition.
%
%    b) If the character had not been active when it was defined as
%       active quote, it will be redefined so that it expands to
%       itself with its previous category code.
%
% In addition to that, all Ascii characters are added to LaTeX's
% \dospecials list (which may override 1 above, but yield the same
% result). Non-Ascii characters are not added to \dospecials and may
% remain active, allowing the inputenc package to handle them in a
% verbatim context (due to 2a above, which implies that inputenc must
% be loaded before any active quotes are defined). Case 2b is usually
% undesirable in a verbatim context. However, if inputenc is loaded
% and the input encoding is declared properly, this will not happen.
%
% \@verbatimquotes is independent of any \dospecials processing. You
% may typeset all active quotes verbatim by using \@verbatimquotes
% exclusively. The advantage of \@verbatimquotes is that it does not
% depend on any category code changes. In contrast to \dospecials and
% \@makeother, the following is perfectly possible:
%
%    \newcommand{\printverbquotes}[1]{{\@verbatimquotes #1}}
%
% Note that the standard LaTeX verbatim environments as well as all
% environments defined by way of the verbatim and fancyvrb packages
% are catered for automatically. When using an environment provided
% by these packages or when defining new environments with the aid of
% one of them, there is no need to add \@verbatimquotes explicitly.
% This also applies to the \verb command and the shortvrb package.

% Frontend to internal marks 

% In some cases it may be desirable to gain access to the internal
% quotation marks of this package without any grouping or nesting
% control. They may be accessed by way of the following commands:
%
%   \textooquote    opening outer quotation mark
%   \textmoquote    middle  outer quotation mark
%   \textcoquote    closing outer quotation mark
%
%   \textoiquote    opening inner quotation mark
%   \textmiquote    middle  inner quotation mark
%   \textciquote    closing inner quotation mark
%
% These commands will simply print the respective quotation mark as
% defined with \DeclareQuoteStyle. Note that the initialization hooks
% for the respective quotation style are not expanded in this case.
% The internal marks will reflect all changes of the quotation style.
% If the multilingual interface is enabled, they are also synced with
% the current language.
