% Copyright (c) 2003-2011 Philipp Lehman.
%           (c) 2015-2019,2021,2022,2024 Joseph Wright.
%
% Permission is granted to copy, distribute and/or modify this
% software under the terms of the LaTeX Project Public License
% (LPPL), version 1.3c or any later version.
%
% 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}[2005/12/01]
\ProvidesPackage{csquotes}
  [2024-04-04 v5.2o context-sensitive quotations (JAW)]

%% Compatibility and requirements

\RequirePackage{etoolbox}
\RequirePackage{keyval}

\@ifpackagelater{etoolbox}{2008/06/28}
  {}
  {\PackageError{csquotes}
     {Outdated 'etoolbox' package}
     {Upgrade to etoolbox v1.7 (2008/06/28) or later.\MessageBreak
      I found: '\csuse{ver@etoolbox.sty}'.\MessageBreak
      This is a fatal error. I'm aborting now.}
   \endinput}

\@ifpackageloaded{inputenc}
  {}
  {\AtEndPreamble{%
     \@ifpackageloaded{inputenc}
       {\csq@warn@loadfirst{inputenc}}
       {}}}

\@ifpackageloaded{inputenx}
  {}
  {\AtEndPreamble{%
     \@ifpackageloaded{inputenx}
       {\csq@warn@loadfirst{inputenx}}
       {}}}

\@ifpackageloaded{ucs}
  {\let\csq@ifucs\@firstoftwo}
  {\let\csq@ifucs\@secondoftwo
   \AtEndPreamble{%
     \@ifpackageloaded{ucs}
       {\let\csq@ifucs\@firstoftwo}
       {}}}

\AtEndPreamble{%
  \@ifpackageloaded{biblatex}
    {\@ifpackagelater{biblatex}{2009/05/30}
       {}
       {\csq@error
          {Outdated 'biblatex' package}
          {Upgrade to biblatex v0.8d (2009/05/30) or later.\MessageBreak
           I found: '\csuse{ver@biblatex.sty}'}}}
    {}}

%% Category codes

\def\csq@catcodes{\do\~\do\&\do\:\do\"\do\<\do\>}
\def\do#1{\catcode\number`#1=\the\catcode`#1\relax}
\edef\csq@catcodes{\csq@catcodes}
\let\do\noexpand

\catcode`\~=\active
\catcode`\&=3
\@makeother\:
\@makeother\"
\@makeother\<
\@makeother\>

%% General

\newtoggle{csq@tempsw}
\newtoggle{csq@quiet}
\newtoggle{csq@active}
\newtoggle{csq@block}
\newtoggle{csq@parbox}
\newtoggle{csq@parthreshold}
\newbool{csdisplay}

\newcount\csq@reset
\newcount\csq@gtype
\newcount\csq@glevel
\newcount\csq@qlevel
\newcount\csq@maxlvl
\newcount\csq@tshold

\providecommand{\@gobblethree}[3]{}
\providecommand{\@gobblefive}[5]{}
\providecommand{\@gobbletwo@optarg}[2][]{}

% {<item>}{<list>}{<separator>}

\def\csq@addto@list#1{%
  \expandafter\csq@addto@list@i\csname\string#1\endcsname}
\def\csq@addto@list@i#1#2#3{%
  \begingroup
  \@tempswatrue
  \def#3##1{\ifnum`#1=`##1\relax\@tempswafalse\fi}#2%
  \expandafter\endgroup\if@tempswa
    \appto#2{#3#1}%
  \fi}

% {<macro>}

\def\csq@everypar#1{%
  \let\csq@theeverypar#1%
  \ifx\csq@theeverypar\@empty
  \else
    \csq@everypar@i
  \fi}

\def\csq@everypar@i{%
  \leavevmode
  \let\csq@new@everypar\everypar
  \csq@ltx@everypar=\expandafter{\the\everypar}%
  \csq@new@everypar={\the\csq@ltx@everypar\csq@theeverypar}%
  \let\everypar\csq@ltx@everypar
  \let\csq@everypar@i\relax}

\newtoks\csq@ltx@everypar

% Miscellaneous tests

\newcommand*{\ifblockquote}{\iftoggle{csq@block}}

\def\csq@ifsafeactives{%
  \ifx\if@safe@actives\iftrue
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\def\csq@iftypeset{%
  \ifx\protect\@typeset@protect
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\def\csq@ifstrings{%
  \ifx\protect\string
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\def\csq@ifnested{%
  \iftoggle{csq@block}
    {\@firstoftwo}
    {\ifnumgreater\csq@qlevel\z@}}

% Parsers for UTF-8

\def\csq@string#1{\csq@string@i#1&}
\def\csq@string@i#1#2&{%
  \string#1\ifblank{#2}{}{\csq@string@i#2&}}

\def\csq@number#1{\csq@number@i#1&}
\def\csq@number@i#1#2&{%
  \number`#1 \ifblank{#2}{}{.\csq@number@i#2&}}

\def\csq@utfprint#1{u8:\csq@utfprint@i#1&}
\def\csq@utfprint@i#1#2&{%
  \number`#1 \ifblank{#2}{}{:\csq@utfprint@i#2&}}

% Grouping control

\protected\def\csq@bqgroup{%
  \begingroup\leavevmode
  \csq@glevel\currentgrouplevel
  \csq@gtype\currentgrouptype}

\protected\def\csq@eqgroup{%
  \ifnum\csq@gtype=\currentgrouptype
    \ifnum\csq@glevel=\currentgrouplevel
    \else
      \csq@err@glevel
    \fi
  \else
    \csq@err@gtype
  \fi
  \endgroup}

\AtEndDocument{%
  \ifnum\csq@qlevel>\z@
    \csq@err@gleft
  \fi}

%% User feedback

\protected\def\csq@warning@loose#1{%
  \csq@warning{#1}\@gobbletwo}
\protected\def\csq@warning@strict#1{%
  \csq@error}
\let\csq@warnorerr\csq@warning@loose

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

\protected\def\csq@warning#1{%
  \PackageWarning{csquotes}{#1}}

\protected\def\csq@warning@noline#1{%
  \PackageWarningNoLine{csquotes}{#1}}

\protected\def\csq@info#1{%
  \iftoggle{csq@quiet}
    {}
    {\PackageInfo{csquotes}{#1\csq@noline}}}
\let\csq@noline\@gobble
\AtBeginDocument{\let\csq@noline\@empty}

\protected\def\csq@logdebug#1{%
  \begingroup
  \def\MessageBreak{^^J(csquotes) \@spaces\@spaces\@spaces\space}%
  \set@display@protect
  \immediate\write\m@ne{Package csquotes Debug: #1\on@line.}%
  \endgroup}

\let\csq@debug\@gobble

% Errors

\protected\def\csq@errormark{%
  \rule[0.25ex]{1.25ex}{1.25ex}}

\protected\def\csq@eqerror{%
  \csq@eqgroup
  \csq@errormark}

\protected\def\csq@mismatch#{%
  \csq@errormark
  \csq@err@mismatch}

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

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

\protected\def\csq@err@undef#1{%
  \csq@error
    {'\string#1' not defined}
    {This command or environment environment appears to
     be\MessageBreak undefined}}

\protected\def\csq@err@opt#1{%
  \csq@error
    {Option '#1' invalid}
    {The option you have supplied is invalid. See the csquotes
     manual\MessageBreak for valid option keys and values}}

\protected\def\csq@err@sfcodes#1{%
  \csq@error
    {'\string#1' not available}
    {'\string#1' may not be used here because the\MessageBreak
     punctuation tracker is not working at this point}}

\protected\def\csq@err@glevel{%
  \csq@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 a summary of all previously opened
    groups\MessageBreak to the transcript file}%
  \showgroups}

\protected\def\csq@err@gtype{%
  \csq@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 a summary of all previously opened
    groups\MessageBreak to the transcript file}%
  \showgroups}

\protected\def\csq@err@gleft{%
  \csq@err@mismatch{%
    Still \number\csq@qlevel\space quote\ifnum\csq@qlevel>\@ne s\fi
    \space open. If you continue, I will write a summary\MessageBreak
    of all previously opened groups to the transcript file}%
  \showgroups}

\protected\def\csq@err@alloc{%
  \csq@error
    {Invalid argument}
    {One of the arguments you have supplied is already allocated
     as\MessageBreak active quote or delimiter}}

\protected\def\csq@err@char{%
  \csq@error
    {Invalid argument}
    {Only single characters with category code 12 or 13 may
     be\MessageBreak allocated as active quotes. Numbers,
     punctuation marks, and\MessageBreak characters which
     are part of LaTeX's syntax or reserved\MessageBreak
     for a specific purpose are invalid}}

\protected\def\csq@err@utf{%
  \csq@error
    {Invalid argument}
    {Only valid UTF-8 sequences representing a single
     character\MessageBreak may be allocated as active quotes}}

\protected\def\csq@err@nbquote{%
  \csq@mismatch{%
    You have nested a \@backslashchar blockquote. Block quotation
    commands\MessageBreak may only be used on quotation level 0}}%

\protected\def\csq@err@patch#1{%
  \csq@error
    {Patching #1 failed}
    {This is an internal issue typically caused by a
     conflict\MessageBreak between csquotes and some
     other package. Modifying\MessageBreak the package
     loading order may fix the problem}}

\protected\def\csq@err@ucs{%
  \csq@error
    {ucs package not supported}
    {The ucs package is not supported. If you want to use
     UTF-8\MessageBreak encoded active quotes, try
     '\string\usepackage[utf8]{inputenc}'.\MessageBreak
     If you continue now, I will reject all UTF-8 encoded
     arguments\MessageBreak as invalid}}

% Warnings

\protected\def\csq@warn@loadfirst#1{%
  \csq@warnorerr
    {Load '#1' before 'csquotes'}
    {'#1' package loaded too late}
    {Always load '#1' before 'csquotes'}}

\protected\def\csq@warn@multilang#1{%
  \csq@warnorerr
    {No multilingual support.\MessageBreak #1}
    {#1}
    {Multilingual support does not seem to be available.\MessageBreak
     Try loading the 'babel' or the 'polyglossia' package}}

\protected\def\csq@warn@style#1{%
  \csq@warnorerr
    {No style for language '#1'.\MessageBreak
     Using fallback style}
    {No style for language '#1'}
    {Define a style or alias for this language first}}

\protected\def\csq@warn@location#1{%
  \csq@warnorerr
    {'\string#1' invalid at this point.\MessageBreak
     Ignoring command}
    {'\string#1' invalid at this point}
    {This command may not be used at this particular
     point.\MessageBreak See the manual of the 'csquotes'
     package for details}}

\protected\def\csq@warn@sfcodes#1{%
  \csq@warnorerr
    {No space factor codes for '#1' encoding.\MessageBreak
     Punctuation tracker may fail}
    {Punctuation tracker may fail at this point}
    {No space factor codes for '#1' encoding.
     See \string\DeclareQuoteGlyph\MessageBreak
     in the 'csquotes' manual for details}}

%% Outer quotations

\protected\def\csq@oqmark{%
  \csq@bqgroup
  \ifnum\csq@qlevel>\z@
    \csq@mismatch{%
      Level 1 quote invalid at this point.
      The current level is \number\csq@qlevel}%
    \advance\csq@qlevel\@ne
    \let\csq@oqmark\csq@eqerror
  \else
    \csq@qlevel\@ne
    \let\csq@oqmark\csq@qclose
    \expandafter\csq@qopen
  \fi}

%% Inner quotations

\protected\def\csq@iqmark{%
  \csq@bqgroup
  \ifnum\csq@qlevel>\@ne
    \csq@mismatch{%
      Level 2 quote invalid at this point.
      The current level is \number\csq@qlevel}%
    \advance\csq@qlevel\@ne
    \let\csq@iqmark\csq@eqerror
  \else
    \csq@qlevel\tw@
    \let\csq@iqmark\csq@qclose
    \expandafter\csq@qopen
  \fi}

%% Nestable quotations

\protected\def\csq@oqopen{%
  \csq@bqgroup
  \csq@oqopen@i}

\def\csq@oqopen@i{%
  \advance\csq@qlevel\@ne
  \csq@qopen}

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

\def\csq@iqopen@i{%
  \ifnum\csq@qlevel=\z@
    \advance\csq@qlevel\tw@
  \else
    \advance\csq@qlevel\@ne
  \fi
  \csq@qopen}

%% Opening marks

\def\csq@qopen{%
  \ifnum\csq@qlevel>\csq@maxlvl
    \csq@mismatch{%
      Level \number\csq@qlevel\space quote invalid at this point.
      The maximum level is \number\csq@maxlvl}%
  \else
    \csq@resetstyle
    \csq@init
    \csq@addkern@open
    \ifodd\csq@qlevel
      \let\csq@kernchar@i\csq@thequote@oopen
      \csq@thequote@oopen
    \else
      \let\csq@kernchar@i\csq@thequote@iopen
      \csq@thequote@iopen
    \fi
    \csq@setmarker@open
    \expandafter\csq@fixkern
  \fi}

%% Closing marks

\protected\def\csq@qclose{%
  \csq@qclose@i{}}

\def\csq@qclose@i{%
  \ifnum\csq@qlevel=\z@
    \csq@mismatch{%
      Cannot close any quote. The current level is 0}%
  \else
    \ifnum\csq@qlevel>\csq@maxlvl
      \csq@eqerror
    \else
      \expandafter\expandafter
      \expandafter\csq@qclose@ii
    \fi
  \fi}

\def\csq@qclose@ii#1{%
  \ifdim\lastkern=\csq@omitmarker
    #1\csq@eqgroup
  \else
    \csq@addkern@close
    \ifodd\csq@qlevel
      \csq@thequote@oclose
      \let\csq@kernchar@i\csq@thequote@oclose
    \else
      \csq@thequote@iclose
      \let\csq@kernchar@i\csq@thequote@iclose
    \fi
    \ifnum\csq@qlevel>\@ne
      \csq@setmarker@close
    \fi
    \ifblank{#1}{}{\expandafter#1}%
    \expandafter\csq@eqgroup
      \expandafter\def
      \expandafter\csq@kernchar@i
      \expandafter{\csq@kernchar@i}%
    \expandafter\csq@fixkern
  \fi}

%% Style initialization

\def\csq@init{%
  \csq@setsfcodes
  \ifodd\csq@qlevel
    \csq@thequote@oinit
    \csq@everypar\csq@thequote@omiddle
  \else
    \csq@thequote@iinit
    \csq@everypar\csq@thequote@imiddle
  \fi
  \csq@init@kern{open}{o}{i}%
  \csq@init@kern{close}{i}{o}}

\def\csq@init@kern#1#2#3{%
  \begingroup
  \settowidth{\@tempdima}{%
    \csuse{csq@thequote@#2#1}%
    \csuse{csq@thequote@#3#1}}%
  \settowidth{\@tempdimb}{%
    \csuse{csq@thequote@#2#1}\unkern
    \csuse{csq@thequote@#3#1}}%
  \ifdim\@tempdima=\@tempdimb
    \let\csq@tempa\endgroup
  \else
    \edef\csq@tempa{\endgroup
      \csdef{csq@thekern@#1}{%
        \the\dimexpr\@tempdima-\@tempdimb\relax}}%
  \fi
  \csq@tempa}

\def\blx@hook@parboxrestore{%
  \let\csq@theeverypar\@empty
  \csq@qlevel\z@
  \toggletrue{csq@parbox}%
  \togglefalse{csq@block}}

\protected\def\blx@parboxrestore{%
  \ifx\protect\noexpand % \@outputpage
  \else
    \blx@hook@parboxrestore
  \fi}

\appto\@parboxrestore{\blx@parboxrestore}

%% Restore kerning

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

\def\csq@fixkern@i{%
  \futurelet\csq@kernchar@ii\csq@fixkern@ii}

\def\csq@fixkern@ii{%
  \ifcat a\noexpand\csq@kernchar@ii
    \expandafter\csq@fixkern@iii
  \else
    \ifcat 1\noexpand\csq@kernchar@ii
      \expandafter\expandafter
      \expandafter\csq@fixkern@iii
    \fi
  \fi}

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

%% Detect and kern adjoining quotes

\def\csq@kernmarker{13sp}
\def\csq@omitmarker{17sp}
\let\csq@themarker\csq@kernmarker
\def\csq@thekern@open{\csq@thequote@kern}
\def\csq@thekern@close{\csq@thequote@kern}
\def\csq@thequote@kern{0.1em}

\def\csq@setmarker@open{%
  \penalty\@M
  \hskip-\csq@themarker\relax
  \hskip\csq@themarker\relax}

\def\csq@setmarker@close{%
  \kern-\csq@themarker\relax
  \kern\csq@themarker\relax}

\def\csq@addkern@open{%
  \ifdim\lastskip=\csq@kernmarker
    \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@kernmarker
    \unkern\unkern
    \ifdim\csq@thekern@close=\z@
    \else
      \penalty\@M\hskip\csq@thekern@close\relax
    \fi
  \fi}

%% Punctuation

% 1001       apostrophe (biblatex only)
% 1002       abbreviation dot (biblatex only)
% 1003/1250  comma
% 1004/1500  semicolon
% 1005/2000  colon
% 1006/3000  period
% 1007/3001  exclamation mark
% 1008/3002  question mark
% 1009       suppress punctuation (biblatex only)

\mathchardef\csq@sf@comma=1003
\mathchardef\csq@sf@semicolon=1004
\mathchardef\csq@sf@colon=1005
\mathchardef\csq@sf@period=1006
\mathchardef\csq@sf@exclam=1007
\mathchardef\csq@sf@question=1008
\mathchardef\csq@sf@threshold@low=1002
\mathchardef\csq@sf@threshold@term=1005
\mathchardef\csq@sf@threshold@high=1009

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

\def\csq@ifsfcodes{%
  \ifnumequal\csq@sf@question
    {\ifcsdef{csq@sfactor@\the\sfcode`\?}
       {\csuse{csq@sfactor@\the\sfcode`\?}}
       {\sfcode`\?}}}

\def\csq@setfrcodes{%
  \ifnum\sfcode`\A=\@m
  \else
    \csq@setazcodes
  \fi
  \sfcode`\,=1003
  \sfcode`\;=1004
  \sfcode`\:=1005
  \sfcode`\.=1006
  \sfcode`\!=1007
  \sfcode`\?=1008
}

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

\begingroup
\let\csq@setazcodes\@empty
\def\csq@tempa{%
  \xdef\csq@setazcodes{\csq@setazcodes\sfcode\the\@tempcnta=\@m}
  \ifnum\@tempcnta<\@tempcntb
    \advance\@tempcnta\@ne
    \expandafter\csq@tempa
  \fi}
\@tempcnta`\A
\@tempcntb`\Z
\csq@tempa
\ifnum\inputlineno=\m@ne\else
  \@tempcnta"80
  \@tempcntb"9C
  \csq@tempa
  \@tempcnta"C0
  \@tempcntb"DF
  \csq@tempa
\fi
\endgroup

\csdef{csq@sfactor@1250}{1003}
\csdef{csq@sfactor@1500}{1004}
\csdef{csq@sfactor@2000}{1005}
\csdef{csq@sfactor@3000}{1006}
\csdef{csq@sfactor@3001}{1007}
\csdef{csq@sfactor@3002}{1008}

\def\csq@spacefactor{%
  \ifhmode
    \ifcsdef{csq@sfactor@\the\spacefactor}
      {\csuse{csq@sfactor@\the\spacefactor}}
      {\the\spacefactor}%
  \else
    1000%
  \fi}

% {<true>}{<false>}

\newcommand*{\ifpunct}{%
  \csq@ifsfcodes
    {\ifnumgreater\csq@spacefactor\csq@sf@threshold@low
       {\ifnumless\csq@spacefactor\csq@sf@threshold@high}
       {\@secondoftwo}}
    {\csq@err@sfcodes\ifpunct
     \@secondoftwo}}

% {<text>}{<true>}{<false>}

\newrobustcmd*{\iftextpunct}[1]{%
  \csq@ifpuncttest{#1}{\ifpunct}}

% {<true>}{<false>}

\newcommand*{\ifterm}{%
  \csq@ifsfcodes
    {\ifnumgreater\csq@spacefactor\csq@sf@threshold@term
       {\ifnumless\csq@spacefactor\csq@sf@threshold@high}
       {\@secondoftwo}}
    {\csq@err@sfcodes\ifterm
     \@secondoftwo}}

% {<text>}{<true>}{<false>}

\newrobustcmd*{\iftextterm}[1]{%
  \csq@ifpuncttest{#1}{\ifterm}}

% {<character>}{<true>}{<false>}

\newrobustcmd*{\ifpunctmark}[1]{%
  \csq@ifsfcodes
    {\ifbool{hmode}
       {\ifnumequal{\sfcode`#1}\spacefactor}
       {\@secondoftwo}}
    {\csq@err@sfcodes\ifpunctmark
     \@secondoftwo}}

% {<text>}{<character>}{<true>}{<false>}

\newrobustcmd*{\iftextpunctmark}[2]{%
  \csq@ifpuncttest{#1}{\ifpunctmark{#2}}}

\def\csq@ifpuncttest#1#2{%
  \begingroup
  \setbox\@tempboxa=\hbox{%
    \csq@ifsfcodes\relax\csq@setsfcodes
    #1#2
      {\aftergroup\@firstoftwo}
      {\aftergroup\@secondoftwo}}%
  {\endgroup\@firstoftwo}
  {\endgroup\@secondoftwo}}

%% Multilingual interface

\def\csq@setstyle#1{%
  \begingroup
  \let\if@safe@actives\iftrue
  \ifcsdef{csq@qstyle@#1}
    {\edef\csq@tempa{\endgroup
       \def\noexpand\csq@currentstyle{#1}%
       \noexpand\csuse{csq@qstyle@#1}}%
     \csq@tempa}
    {\csq@warnorerr
       {Quote style '#1' not defined.\MessageBreak Ignoring command}
       {Quote style '#1' not defined}
       {Define the style or alias first}
     \endgroup}}

\def\csq@resetstyle{%
  \ifnum\csq@reset=\@ne
    \ifundef\babelname
      {\ifundef\languagename
         {\csq@warn@multilang{Cannot detect current language}}
         {\csq@resetstyle@i{\languagename}}}%
      {\csq@resetstyle@i{\babelname}}%
  \fi}

\def\csq@resetstyle@i#1{%
  \ifx\csq@currentstyle#1\relax
  \else
    \ifcsundef{csq@qstyle@#1}
      {\csq@warn@style#1
       \csq@setstyle{fallback}}
      {\csq@setstyle{#1}}%
  \fi}

\def\csq@savelang{%
  \ifdef\csq@mainlang
    {}
    {\ifdef\babelname
       {\edef\csq@mainlang{\babelname}}
       {\ifdef\languagename
          {\edef\csq@mainlang{\languagename}}
          {\csq@warn@multilang{Cannot detect main document language}}}}}

\newcommand*{\csq@otherlang@star}{\csuse{otherlanguage*}}
\newcommand*{\csq@otherlang@star@end}{\csuse{endotherlanguage*}}

\def\csq@resetlang{%
  \ifdef\csq@mainlang
    {\expandafter\csq@otherlang@star\expandafter{\csq@mainlang}%
     \let\csq@resetlang\relax}
    {}}

\protected\long\def\csq@switchlang#1{%
  \ifdef\csq@mainlang
    {\begingroup
     \expandafter\csq@otherlang@star\expandafter{\csq@mainlang}%
     #1%
     \csq@otherlang@star@end
     \endgroup}
    {#1}}

% \lowercase: workaround for page headers of book.cls et al.

\def\csq@lang#1{%
  \csq@savelang
  \lowercase{\csq@otherlang@star{#1}}}
\def\csq@endlang{%
  \csq@otherlang@star@end}

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

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

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

\def\csq@hyphenrules#1{%
  \language\csname l@#1\endcsname\relax
  \ifcsundef{#1hyphenmins}
    {\csq@sethyphenmins\tw@\thr@@}
    {\expandafter\expandafter\expandafter\csq@sethyphenmins
       \csname#1hyphenmins\endcsname}}

\def\csq@sethyphenmins#1#2{%
  \lefthyphenmin#1\relax
  \righthyphenmin#2\relax}

\appto\csq@hook@multilang{%
  \appto\blx@hook@parboxrestore{\csq@resetlang}%
  \ifundef\l@usenglish
    {\ifundef\l@USenglish
       {}
       {\let\l@usenglish\l@USenglish}}
    {}%
  \ifundef\l@ukenglish
    {\ifundef\l@UKenglish
       {}
       {\let\l@ukenglish\l@UKenglish}}
    {}%
  \ifcase\csq@reset
  \or
  \or
    \csq@info{Enabling multilingual quotes}%
    \csq@reset\@ne
  \else
    \csq@info{Adjusting default style}%
    \ifcsdef{csq@qstyle@\csq@main@language}
      {\DeclareQuoteAlias{\csq@main@language}{default}}
      {\csq@warn@style\csq@main@language
       \DeclareQuoteAlias{fallback}{default}}%
  \fi}

\appto\csq@hook@nomultilang{%
  \ifcase\csq@reset
  \or
    \csq@warn@multilang{Cannot enable multilingual quotes}%
  \or
  \or
    \csq@warn@multilang{Cannot adjust default style}%
  \fi
  \csq@reset\z@
  \let\csq@hyph\csq@nohyph
  \let\csq@endhyph\@empty
  \let\csq@lang\csq@nolang
  \let\csq@endlang\@empty
  \let\csq@switchlang\@firstofone
  \def\csq@resetqstyle{%
    \csq@warn@multilang{Cannot enable multilingual quotes}}}

%% Markup with control sequences

% Argument parsers

% {<macro>}[<cite>][<punct>] =>
%  <macro>{\mkcitation}{<cite>}{<punct>}

\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}}

% {<macro>}[<pre>][<post>]{<key>}[<punct>] =>
%  <macro>{\mkccitation}{\csq@cite[<pre>][<post>]{<key>}}{<punct>}

\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}}

% {<macro>}<punct> => <macro>{<apunct>}<tpunct>

\protected\long\def\csq@getpunct#1{%
  \begingroup
  \def\csq@tempa{\endgroup#1}%
  \futurelet\@let@token\csq@getpunct@i}

\def\csq@getpunct@i{%
  \expandafter\csq@getpunct@ii\csq@allpunct&}

\def\csq@getpunct@ii#1{%
  \ifx#1&%
    \expandafter\csq@getpunct@v
  \fi
  \ifx#1\@let@token
    \expandafter\csq@getpunct@iii
  \fi
  \csq@getpunct@ii}

\def\csq@getpunct@iii#1\csq@getpunct@ii#2&{%
  \expandafter\csq@getpunct@iv\csq@autopunct&}

\def\csq@getpunct@iv#1{%
  \ifx#1&%
    \expandafter\csq@getpunct@vi
  \fi
  \ifx#1\@let@token
    \expandafter\csq@getpunct@vii
  \fi
  \csq@getpunct@iv}

\def\csq@getpunct@v#1\csq@getpunct@ii{\csq@tempa{}\@empty}
\def\csq@getpunct@vi#1\csq@getpunct@iv{\csq@tempa{}}
\def\csq@getpunct@vii#1\csq@getpunct@iv#2&#3{\csq@tempa{#3}\@empty}

% Punctuation

\newrobustcmd*{\DeclareAutoPunct}[1]{%
  \ifblank{#1}
    {\def\csq@autopunct{}}
    {\def\csq@autopunct{#1}}}

\let\csq@autopunct\@empty
\def\csq@allpunct{.,;:!?}

\AfterEndPreamble{% babel
  \begingroup
  \makeatletter
  \endlinechar\m@ne
  \edef\csq@tempa{%
    \noexpand\scantokens{%
      \gdef\noexpand\csq@autopunct{\csq@autopunct}%
      \gdef\noexpand\csq@allpunct{\csq@allpunct}}}%
  \csq@tempa
  \endgroup}

% Plain quotes

\newrobustcmd*{\enquote}{%
  \@ifstar{\csq@quote@ii{}{}}{\csq@quote@i{}{}}}

% {<init>}{<endinit>}{<text>}

\long\def\csq@quote@i#1#2#3{%
  \csq@bqgroup\expandafter\let\expandafter\csq@font\the\font
  #1\csq@oqopen@i#3%
  \expandafter\ifx\the\font\csq@font\else\expandafter\csq@font\fi
  \csq@qclose@i{#2}}
\long\def\csq@quote@ii#1#2#3{%
  \csq@bqgroup\expandafter\let\expandafter\csq@font\the\font
  #1\csq@iqopen@i#3%
  \expandafter\ifx\the\font\csq@font\else\expandafter\csq@font\fi
  \csq@qclose@i{#2}}

% Plain quotes + language switching

\newrobustcmd*{\foreignquote}{%
  \@ifstar\csq@fquote@ii\csq@fquote@i}

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

\newrobustcmd*{\hyphenquote}{%
  \@ifstar\csq@hquote@ii\csq@hquote@i}

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

% 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}}

\appto\csq@hook@hyperref{%
  \let\enquote\csq@pdf@quote
  \let\foreignquote\csq@pdf@fquote
  \let\hyphenquote\csq@pdf@fquote}

% Text quotes

\newrobustcmd*{\textquote}{%
  \@ifstar
    {\csq@getcargs{\csq@tquote{}{}{\csq@iqopen@i}}}
    {\csq@getcargs{\csq@tquote{}{}{\csq@oqopen@i}}}}

% {<init>}{<endinit>}{<qopen>}{<citehook>}{<cite>}{<punct>}{<text>}

\long\def\csq@tquote#1#2#3#4#5#6#7{%
  \csq@getpunct{\csq@tquote@i{#1}{#2}{#3}{#4}{#5}{#6}{#7}}}

% {<init>}{<endinit>}{<qopen>}{<citehook>}{<cite>}{<punct>}{<text>}{<apunct>}<tpunct>

\long\def\csq@tquote@i#1#2#3#4#5#6#7#8#9{%
  \begingroup
  \csq@setsfcodes
  \edef\csq@tempa{%
    \unexpanded{%
      \mktextquote
      {#3}%
      {#7}%
      {\csq@qclose@i{#2}}%
      {#6}{#8}}%
    {\ifblank{#5}
       {}
       {\unexpanded{\csq@switchlang{#4{#5}}}}}}%
  \csq@bqgroup#1\csq@tempa#9%
  \endgroup}

% {<qopen>}{<text>}{<qclose>}{<punct>}{<apunct>}{<cite>}

\newcommand{\mktextquote}[6]{#1#2#4#3#6#5}

% Text quotes + language switching

\newrobustcmd*{\foreigntextquote}{%
  \@ifstar\csq@ftquote@ii\csq@ftquote@i}

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

\newrobustcmd*{\hyphentextquote}{%
  \@ifstar\csq@htquote@ii\csq@htquote@i}

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

% Integrated text quotes

\newrobustcmd*{\textcquote}{%
  \@ifstar
    {\csq@getccargs{\csq@tquote{}{}{\csq@iqopen@i}}}
    {\csq@getccargs{\csq@tquote{}{}{\csq@oqopen@i}}}}

% Integrated text quotes + language switching

\newrobustcmd*{\foreigntextcquote}{%
  \@ifstar\csq@ftcquote@ii\csq@ftcquote@i}

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

\newrobustcmd*{\hyphentextcquote}{%
  \@ifstar\csq@htcquote@ii\csq@htcquote@i}

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

% PDF strings

\appto\csq@hook@hyperref{%
  \def\textquote{\pdfstringdefWarn\textquote}%
  \def\foreigntextquote{\pdfstringdefWarn\foreigntextquote}%
  \def\hyphentextquote{\pdfstringdefWarn\hyphentextquote}%
  \def\textcquote{\pdfstringdefWarn\textcquote}%
  \def\foreigntextcquote{\pdfstringdefWarn\foreigntextcquote}%
  \def\hyphentextcquote{\pdfstringdefWarn\hyphentextcquote}}

% Block quotes

\newrobustcmd*{\blockquote}{%
  \csq@getcargs{\csq@bquote{}{}}}

% {<init>}{<endinit>}{<citehook>}{<cite>}{<punct>}{<text>}

\long\def\csq@bquote#1#2#3#4#5#6{%
  \csq@ifnested
    {\csq@err@nbquote}
    {\csq@getpunct{%
       \ifboolexpr{ bool {inner} and bool {hmode} }
         {\csq@bquote@ii}
         {\ifbool{csdisplay}
            {\csq@bquote@i}
            {\iftoggle{csq@parbox}\csq@bquote@ii\csq@bquote@i}}%
         {#1}{#2}{#3}{#4}{#5}{#6}}}}

% {<init>}{<endinit>}{<citehook>}{<cite>}{<punct>}{<text>}{<apunct>}<tpunct>

\long\def\csq@bquote@i#1#2#3#4#5#6{%
  \begingroup
  \blockquote@prehook
  \togglefalse{csq@tempsw}%
  \setbox\@tempboxa=\vbox{%
    \csuse{csq@bcount@\csq@thresholdtype}%
    \parindent\z@
    \blockquote@parsehook
    #1#6#5#2\endgraf\expandafter}\expandafter
  \@tempcnta\number\prevgraf\relax
  \iftoggle{csq@tempsw}
    {\csq@debug{%
       \@backslashchar blockquote has \number\@tempcnta\space
       \csq@thresholdtype\space and manual breaks.\MessageBreak
       Setting as display}}
    {\ifnumgreater\@tempcnta\csq@tshold
       {\toggletrue{csq@tempsw}}
       {}%
     \csq@debug{%
       \@backslashchar blockquote has \number\@tempcnta\space
       \csq@thresholdtype. Threshold is \number\csq@tshold.\MessageBreak
       Setting \iftoggle{csq@tempsw}{as display}{inline}}}%
  \blockquote@posthook
  \iftoggle{csq@tempsw}
    {\aftergroup\@firstoftwo}
    {\aftergroup\@secondoftwo}%
  \endgroup
  \csq@bquote@iii\csq@bquote@ii{#1}{#2}{#3}{#4}{#5}{#6}}

\def\csq@bcount@lines{%
  \iftoggle{csq@parthreshold}
    {\def\par{\global\toggletrue{csq@tempsw}\@@par}%
     \let\newline\par
     \let\\\par}
    {}%
  \parfillskip\z@\@plus1fil\relax}

\def\csq@bcount@words{%
  \iftoggle{csq@parthreshold}
    {\def\par{\global\toggletrue{csq@tempsw}\space}%
     \let\newline\par
     \let\\\par}
    {\let\par\space
     \let\newline\space
     \let\\\space}%
  \let\nobreakspace\space
  \let~\space
  \hsize\z@
  \hyphenpenalty\@M
  \exhyphenpenalty\csq@splitcomp
  \relpenalty\@M
  \binoppenalty\@M
  \let\allowbreak\relax
  \hfuzz\maxdimen}

\newcommand*{\blockquote@prehook}{%
  \def\@elt##1{\global\value{##1}\the\value{##1}\relax}%
  \edef\csq@tempa{\cl@@ckpt}%
  \let\@elt\relax
  \@fileswfalse}

\newcommand*{\blockquote@posthook}{%
  \csq@tempa}

\newcommand*{\blockquote@parsehook}{%
  \let\csq@nbgobble\@gobble
  \let\csq@bquote\@gobblefive
  \let\csq@warnorerr\@gobblethree
  \let\csq@error\@gobbletwo
  \let\csq@warning\@gobble
  \let\csq@info\@gobble
  \def\textelp{\@ifstar\@gobble\@gobble}%
  \let\textins\textelp
  \let\marginpar\@gobbletwo@optarg
  \let\label\@gobble
  \let\index\@gobble
  \let\glossary\@gobble}

\newrobustcmd*{\BlockquoteDisable}{%
  \gappto\blockquote@parsehook}

\let\csq@nbgobble\@firstofone

% {<init>}{<endinit>}{<citehook>}{<cite>}{<punct>}{<text>}{<apunct>}<tpunct>

\def\csq@bquote@ii#1#2{\csq@tquote@i{#1}{#2}{\csq@oqopen@i}}

% {<init>}{<endinit>}{<citehook>}{<cite>}{<punct>}{<text>}{<apunct>}<tpunct>

\long\def\csq@bquote@iii#1#2#3#4#5#6#7#8{%
  \begin{\csq@blockenvironment}%
  \toggletrue{csq@block}%
  \csq@setsfcodes
  \edef\csq@tempa{%
    \unexpanded{%
      \mkblockquote
      {#6}%
      {#5}{#7}}%
    {\ifblank{#4}
       {}
       {\unexpanded{\csq@switchlang{#3{#4}}}}}}%
  #1\csq@tempa#8#2%
  \end{\csq@blockenvironment}}

% {<text>}{<punct>}{<apunct>}{<cite>}

\newcommand{\mkblockquote}[4]{#1#2#4#3}

% Block quotes + language switching

\newrobustcmd*{\foreignblockquote}[1]{%
  \csq@getcargs{\csq@bquote{\csq@lang{#1}}{\csq@endlang}}}

\newrobustcmd*{\hyphenblockquote}[1]{%
  \csq@getcargs{\csq@bquote{\csq@hyph{#1}}{\csq@endhyph}}}

\newrobustcmd*{\hybridblockquote}[1]{%
  \csq@getcargs{%
    \csq@bquote
      {\iftoggle{csq@block}{\csq@lang}{\csq@hyph}{#1}}
      {\iftoggle{csq@block}{\csq@endlang}{\csq@endhyph}}}}

% Integrated block quotes

\newrobustcmd*{\blockcquote}{%
  \csq@getccargs{\csq@bquote{}{}}}

% Integrated block quotes + language switching

\newrobustcmd*{\foreignblockcquote}[1]{%
  \csq@getccargs{\csq@bquote{\csq@lang{#1}}{\csq@endlang}}}

\newrobustcmd*{\hyphenblockcquote}[1]{%
  \csq@getccargs{\csq@bquote{\csq@hyph{#1}}{\csq@endhyph}}}

\newrobustcmd*{\hybridblockcquote}[1]{%
  \csq@getccargs{%
    \csq@bquote
      {\iftoggle{csq@block}{\csq@lang}{\csq@hyph}{#1}}
      {\iftoggle{csq@block}{\csq@endlang}{\csq@endhyph}}}}

% PDF strings

\appto\csq@hook@hyperref{%
  \def\blockquote{\pdfstringdefWarn\blockquote}%
  \def\foreignblockquote{\pdfstringdefWarn\foreignblockquote}%
  \def\hyphenblockquote{\pdfstringdefWarn\hyphenblockquote}%
  \def\blockcquote{\pdfstringdefWarn\blockcquote}%
  \def\foreignblockcquote{\pdfstringdefWarn\foreignblockcquote}%
  \def\hyphenblockcquote{\pdfstringdefWarn\hyphenblockcquote}}

% Display quotes

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

% {<init>}{<endinit>}{<citehook>}{<cite>}{<punct>}

\def\csq@bdquote#1#2#3#4#5{%
  \csuse{\csq@blockenvironment}%
  \toggletrue{csq@block}%
  \csq@setsfcodes
  #1\ifblank{#4}
    {\def\csq@tempb{\mkenddispquote{#5}{}#2}%
     \mkbegdispquote{#5}{}}
    {\def\csq@tempb{\mkenddispquote{#5}{\csq@switchlang{#3{#4}}}#2}%
     \mkbegdispquote{#5}{\csq@switchlang{#3{#4}}}}%
  \ignorespaces}

\def\csq@edquote{%
  \unspace\csq@tempb
  \csuse{end\csq@blockenvironment}}

\providerobustcmd*{\unspace}{%
  \ifbool{hmode}
    {\ifdimgreater\lastskip\z@
       {\unskip\unspace}
       {\ifnumgreater\lastpenalty\z@
          {\unpenalty\unspace}
          {}}}
    {}}

% {<punct>}{<cite>}

\newcommand{\mkbegdispquote}[2]{}
\newcommand{\mkenddispquote}[2]{#1#2}

% Display quotes + language switching

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

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

% Integrated display quotes

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

% Integrated display quotes + language switching

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

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

% Hooks

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

%% Markup with active characters

\let\csq@dospecials\@empty
\let\csq@doutfspecials\@empty

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

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

\def\csq@do@verbspecial#1#2{%
  \csq@info{Redefining character \string#1}%
  \ifnum`#1<128\relax
    \csq@edefspecial{#1}{\string#1}%
  \else
    \csq@letspecial{#1}{csq@saved@\number`#1}%
  \fi}

\def\csq@do@doutfspecial#1{%
  \csq@info{Redefining character \csq@utfprint{#1}}%
  \csletcs{u8:\csq@string{#1}}{csq@special@\csq@number{#1}}}

\def\csq@do@undoutfspecial#1{%
  \csq@info{Resetting character \csq@utfprint{#1}}%
  \csletcs{u8:\csq@string{#1}}{csq@saved@\csq@number{#1}}}

\def\csq@do@delspecial#1#2{%
  \csq@info{Deallocating character \string#1}%
  \csundef{csq@special@\number`#1}%
  \csundef{csq@saved@\number`#1}%
  \csundef{csq@string@\number`#1}%
  \csundef{csq@textdef@\number`#1}%
  \csundef{csq@xtxtdef@\number`#1}%
  \csundef{csq@pdfdef@\number`#1}}

\def\csq@do@delutfspecial#1{%
  \csq@info{Deallocating character \csq@utfprint{#1}}%
  \csundef{csq@special@\csq@number{#1}}%
  \csundef{csq@saved@\csq@number{#1}}%
  \csundef{csq@string@\csq@number{#1}}%
  \csundef{csq@textdef@\csq@number{#1}}%
  \csundef{csq@xtxtdef@\csq@number{#1}}%
  \csundef{csq@pdfdef@\csq@number{#1}}}

\def\csq@mkenable{%
  \let\do\csq@do@dospecial
  \csq@dospecials
  \let\do\csq@do@doutfspecial
  \csq@doutfspecials
  \let\do\noexpand
  \toggletrue{csq@active}}

\def\csq@mkdisable{%
  \iftoggle{csq@active}
    {\let\do\csq@do@undospecial
     \csq@dospecials
     \let\do\csq@do@undoutfspecial
     \csq@doutfspecials
     \let\do\noexpand
     \togglefalse{csq@active}}
    {}}

\def\csq@mkverbatim{%
  \iftoggle{csq@active}
    {\let\do\csq@do@verbspecial
     \csq@dospecials
     \let\do\csq@do@undoutfspecial
     \csq@doutfspecials
     \let\do\noexpand}
    {}}

\def\csq@mkdelete{%
  \csq@mkdisable
  \let\do\csq@do@delspecial
  \csq@dospecials
  \let\do\csq@do@delutfspecial
  \csq@doutfspecials
  \let\do\noexpand
  \let\csq@dospecials\@empty
  \let\csq@doutfspecials\@empty
  \let\csq@mkpdfstrings\@empty}

\let\csq@mkpdfstrings\@empty
\appto\csq@hook@hyperref{\csq@mkpdfstrings}

\ifdef\inpenc@prehook
  {\AtBeginDocument{%
     \addto@hook\inpenc@prehook{%
        \csq@info{Input encoding change}%
        \csq@mkdelete}}}
  {}

% Regular specials

% {<char>}{<textdef>}{<pdfdef1>}{<pdfdef2>}

\def\csq@addspecial#1#2#3#4{%
  \csq@validate{#1}
    {\csq@addspecial@i@s{#1}{\csqQQ}{#2}{#3}{#4}}
    {\csq@addspecial@i@u{#1}{\csqQQ}{#2}{#3}{#4}}}

% {<char>}{<macro>}{<textdef>}{<pdfdef1>}{<pdfdef2>}

\def\csq@addspecial@i@s#1#2#3#4#5{%
  \csq@info{Allocating character \string#1}%
  \csedef{csq@special@\number`#1}{\noexpand#2{\number`#1}}%
  \ifnum\catcode`#1=\active
    \cslet{csq@saved@\number`#1}{#1}%
  \else
    \csdef{csq@saved@\number`#1}{#1}%
  \fi
  \csedef{csq@string@\number`#1}{\string#1}%
  \csdef{csq@textdef@\number`#1}{#3}%
  \ifblank{#5}
    {\csdef{csq@pdfdef@\number`#1}{#4}}
    {\appto\csq@mkpdfstrings{\csq@defpspecial{#1}{#4}{#5}}}%
  \eappto\csq@dospecials{\noexpand\do{\string#1}{\the\catcode`#1}}%
  \ifnum`#1<128\relax
    \csq@addto@list{#1}{\dospecials}{\do}%
    \csq@addto@list{#1}{\@sanitize}{\@makeother}%
  \fi}

% {<char>}{<macro>}{<textdef>}{<pdfdef1>}{<pdfdef2>}

\def\csq@addspecial@i@u#1#2#3#4#5{%
  \csq@info{Allocating character \csq@utfprint{#1}}%
  \csedef{csq@special@\csq@number{#1}}{%
    \noexpand#2{\csq@number{#1}}}%
  \ifcsundef{u8:\csq@string{#1}}
    {\csq@error
       {\csq@utfprint{#1} not supported by inputenc}
       {This UTF-8 sequence does not seem to be supported by the
        inputenc\MessageBreak package}%
     \csdef{csq@saved@\csq@number{#1}}{}}
    {\csletcs{csq@saved@\csq@number{#1}}{u8:\csq@string{#1}}}%
  \csedef{csq@string@\csq@number{#1}}{\csq@string{#1}}%
  \csdef{csq@textdef@\csq@number{#1}}{#3}%
  \ifblank{#5}
    {\csdef{csq@pdfdef@\csq@number{#1}}{#4}}
    {\appto\csq@mkpdfstrings{\csq@defupspecial{#1}{#4}{#5}}}%
  \eappto\csq@doutfspecials{\noexpand\do{\csq@string{#1}}}}

% Block specials

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

\def\csq@addbspecial#1#2#3#4{%
  \csq@validate{#1}
    {\csq@addbspecial@i@s{#1}{#2}{#3}{#4}}
    {\csq@addbspecial@i@u{#1}{#2}{#3}{#4}}}

\def\csq@addbspecial@i@s#1#2#3#4{%
  \begingroup
  \def\csq@tempa{\endgroup\csq@addspecial@i@s{#1}{\csqBQ}{}}%
  \eappto\csq@tempa{%
    {\expandafter\noexpand\csname csq@saved@\number`#1\endcsname
     \noexpand\pdfstringdefWarn{\string#1}}{}}%
  \csq@tempa
  \csq@validate{#2}
    {\csq@addbspecial@ii@s{\number`#1}{#2}{#3}{#4}}
    {\csq@addbspecial@ii@u{\number`#1}{#2}{#3}{#4}}}

\def\csq@addbspecial@i@u#1#2#3#4{%
  \begingroup
  \def\csq@tempa{\endgroup\csq@addspecial@i@u{#1}{\csqBQ}{}}%
  \eappto\csq@tempa{%
    {\expandafter\noexpand\csname csq@saved@\csq@number{#1}\endcsname
     \noexpand\pdfstringdefWarn{\csq@utfprint{#1}}}{}}%
  \csq@tempa
  \csq@validate{#2}
    {\csq@addbspecial@ii@s{\csq@number{#1}}{#2}{#3}{#4}}
    {\csq@addbspecial@ii@u{\csq@number{#1}}{#2}{#3}{#4}}}

\def\csq@addbspecial@ii@s#1#2#3#4{%
  \csq@addspecial@i@s{#2}{\csqBQsep}{}{}{}%
  \begingroup
  \let\csq@tempa\@empty
  \def\csq@tempb{{#1}}%
  \uccode`\~=`#2\relax
  \uppercase{\appto\csq@tempb{{~}}}%
  \appto\csq@tempb{{#3}{#4}}%
  \csq@validate{#3}
    {\expandafter\def\expandafter\csq@tempa
     \expandafter{\expandafter\csq@addbspecial@iii@s\csq@tempb}}
    {\expandafter\def\expandafter\csq@tempa
     \expandafter{\expandafter\csq@addbspecial@iii@u\csq@tempb}}%
  \expandafter\endgroup\csq@tempa}

\def\csq@addbspecial@ii@u#1#2#3#4{%
  \csq@addspecial@i@u{#2}{\csqBQsep}{}{}{}%
  \csq@validate{#3}
    {\csq@addbspecial@iii@s{#1}{#2}{#3}{#4}}
    {\csq@addbspecial@iii@u{#1}{#2}{#3}{#4}}}

\def\csq@addbspecial@iii@s#1#2#3#4{%
  \csq@addspecial@i@s{#3}{\csqBQend}{}{}{}%
  \begingroup
  \def\csq@tempa{{#1}{#2}}%
  \uccode`\~=`#3\relax
  \uppercase{\appto\csq@tempa{~}}%
  \appto\csq@tempa{{#4}}%
  \expandafter\endgroup
  \expandafter\csq@addbspecial@iv\csq@tempa}

\def\csq@addbspecial@iii@u#1#2#3#4{%
  \csq@addspecial@i@u{#3}{\csqBQend}{}{}{}%
  \csq@addbspecial@iv{#1}{#2}{#3}{#4}}

\def\csq@addbspecial@iv#1#2#3#4{%
  \expandafter\csq@defbspecial
  \expandafter{\csname csq@textdef@#1\endcsname}{#2}{#3}{#4}%
  \expandafter\csq@defxbspecial
  \expandafter{\csname csq@xtxtdef@#1\endcsname}{#4}}

% Specials

\newcommand*{\csqQQ}[1]{%
  \csq@ifsafeactives
    {\csuse{csq@string@#1}}
    {\csq@iftypeset
       {\ifbool{mmode}
          {\csuse{csq@saved@#1}}
          {\csuse{csq@textdef@#1}}}
       {\csq@ifstrings
          {\csuse{csq@string@#1}}
          {\noexpand\csqQQ{#1}}}}}

\newcommand*{\csqBQ}[1]{%
  \csq@ifsafeactives
    {\csuse{csq@string@#1}}
    {\csq@iftypeset
       {\ifbool{mmode}
          {\csuse{csq@saved@#1}}
          {\csq@ifnested
             {\csq@err@nbquote}
             {\csq@nbgobble{\csuse{csq@textdef@#1}}}}}
       {\csq@ifstrings
          {\csuse{csq@string@#1}}
          {\noexpand\csqBQbeg{#1}}}}}

\newcommand*{\csqBQbeg}[1]{%
  \csq@ifsafeactives
    {\csuse{csq@string@#1}}
    {\csq@iftypeset
       {\ifbool{mmode}
          {\csuse{csq@saved@#1}}
          {\csq@ifnested
             {\csq@err@nbquote}
             {\csq@nbgobble{\csuse{csq@xtxtdef@#1}}}}}
       {\csq@ifstrings
          {\csuse{csq@string@#1}}
          {\noexpand\csqBQbeg{#1}}}}}

\newcommand*{\csqBQsep}[1]{%
  \csq@ifsafeactives
    {\csuse{csq@string@#1}}
    {\csq@iftypeset
       {\csuse{csq@saved@#1}}
       {\csq@ifstrings
          {\csuse{csq@string@#1}}
          {\noexpand\csqBQsep{#1}}}}}

\newcommand*{\csqBQend}[1]{%
  \csq@ifsafeactives
    {\csuse{csq@string@#1}}
    {\csq@iftypeset
       {\csuse{csq@saved@#1}}
       {\csq@ifstrings
          {\csuse{csq@string@#1}}
          {\noexpand\csqBQend{#1}}}}}

\appto\csq@hook@hyperref{%
  \def\csqQQ#1{\csuse{csq@pdfdef@#1}}%
  \def\csqBQ#1{\csuse{csq@pdfdef@#1}}%
  \def\csqBQbeg#1{\csuse{csq@pdfdef@#1}}%
  \def\csqBQsep#1{\csuse{csq@saved@#1}}%
  \def\csqBQend#1{\csuse{csq@saved@#1}}}

% Define specials

% {<char>}{<def>}

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

% {<char>}{<csname>}

\def\csq@letspecial#1#2{%
  \begingroup
  \uccode`\~=`#1\relax
  \uppercase{\def\csq@tempa{\endgroup\let~}}%
  \expandafter\csq@tempa\csname#2\endcsname}

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

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

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

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

% {<macro>}{<def>}

\def\csq@defxbspecial#1#2{%
  \long\def#1##1\csqBQend##2{\csq@xbspecial@i{##1}{#2}}}

% {<text>}{<macro>}

\long\def\csq@xbspecial@i#1#2{%
  \long\def\csq@xbspecial@ii##1\csqBQsep##2&{%
    \ifblank{##2}
      {#2{}{}{#1}}
      {\csq@xbspecial@iii#1&}}%
  \long\def\csq@xbspecial@iii##1\csqBQsep##2##3&{%
    #2{##3}{}{##1}}%
  \csq@xbspecial@ii#1\csqBQsep&}

% {<char>}{<pdfomark>}{<pdfcmark>}

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

% {<char>}{<pdfomark>}{<pdfcmark>}

\def\csq@defupspecial#1#2#3{%
  \expandafter\def\csname u8:\csq@string{#1}\endcsname##1#1{#2##1#3}}

% Validate user input

% {<char>}{<macro>}{<utfmacro>}

\def\csq@validate#1#2#3{%
  \csq@ifutfchar{#1}
    {\csq@ifvalidutf{#1}
       {\ifcsundef{csq@special@\csq@number{#1}}
           {#3}
           {\csq@err@alloc}}
       {\csq@err@utf}}
    {\csq@ifvalidchar{#1}
       {\ifcsundef{csq@special@\number`#1}
          {#2}
          {\csq@err@alloc}}
       {\csq@err@char}}}

% {<char>}{<true>}{<false>}

\def\csq@ifutfchar#1{%
  \ifundef\inputencodingname
    {\@secondoftwo}
    {\csq@ifutfenc}%
  {\csq@ifsingle{#1}
     {\ifnum`#1<128\relax
        \expandafter\@secondoftwo
      \else
        \csq@err@utf
        \expandafter\@gobbletwo
      \fi}
     {\@firstoftwo}}
  {\csq@ifsingle{#1}
     {\@secondoftwo}
     {\csq@err@char
      \@gobbletwo}}}

\ifundef\inputencodingname
  {\def\csq@ifutfenc{%
     \csq@ifucs
       {\csq@err@ucs\@secondoftwo}
       {\begingroup
        \def\@inpenc@undefined@##1{\def\inputencodingname{##1}}%
        \@inpenc@undefined
        \ifdefstring\inputencodingname{utf8}
          {\aftergroup\@firstoftwo}
          {\aftergroup\@secondoftwo}%
        \endgroup}}}
  {\def\csq@ifutfenc{%
     \csq@ifucs
       {\csq@err@ucs\@secondoftwo}
       {\ifdefstring\inputencodingname{utf8}}}}

% {<char>}{<true>}{<false>}

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

% {<char>}{<true>}{<false>}

\def\csq@ifvalidchar#1{%
  \begingroup
  \@tempswafalse
  \ifnum\active=\catcode`#1\relax
    \@tempswatrue
  \else\ifnum12=\catcode`#1\relax
    \@tempswatrue
  \fi\fi
  \if@tempswa
    \csq@ifvalidchar@i{#1}%
  \fi
  \if@tempswa
    \aftergroup\@firstoftwo
  \else
    \aftergroup\@secondoftwo
  \fi
  \endgroup}

\def\csq@ifvalidchar@i#1{%
  \def\break\fi##1\relax{\fi\@tempswafalse}%
  \def\do##1{\ifnum`##1=`#1\relax\break\fi}%
  \csq@resrvdchars\relax}

\def\csq@resrvdchars{%
  \do\[\do\]\do\*\do\@\do\~\do\-\do\`\do\'\do\.\do\,\do\;\do\:%
  \do\!\do\?\do\0\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9}

% {<char>}{<true>}{<false>}

\def\csq@ifvalidutf#1{%
  \ifblank{#1}{\@secondoftwo}{\csq@ifvalidutf@i#1&}}

\def\csq@ifvalidutf@i#1#2&{%
  \ifblank{#2}
    {\@secondoftwo}% 0x00-0x7F handled elsewhere
    {\csq@ifutfstart{#1}{\csq@ifvalidutf@ii#2&}{\@secondoftwo}}}

\def\csq@ifvalidutf@ii#1#2&{%
  \csq@ifutfnext{#1}
    {\ifblank{#2}{\@firstoftwo}{\csq@ifvalidutf@ii#2&}}
    {\@secondoftwo}}

% 0xC0-0xFD, 0x80-0xBF, 0x80-0xBF, ...

\def\csq@ifutfstart#1{%
  \ifnum`#1>191\relax
    \ifnum`#1<254\relax
      \expandafter\expandafter
      \expandafter\@firstoftwo
    \else
      \expandafter\expandafter
      \expandafter\@secondoftwo
    \fi
  \else
    \expandafter\@secondoftwo
  \fi}

\def\csq@ifutfnext#1{%
  \ifnum`#1>127\relax
    \ifnum`#1<192\relax
      \expandafter\expandafter
      \expandafter\@firstoftwo
    \else
      \expandafter\expandafter
      \expandafter\@secondoftwo
    \fi
  \else
    \expandafter\@secondoftwo
  \fi}

\def\csq@isvalid#1#2#3{#2}

% User interface

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

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

\newrobustcmd*{\MakeAutoQuote}{%
  \@ifstar
    {\csq@mkaquote\csq@iqopen}
    {\csq@mkaquote\csq@oqopen}}
\def\csq@mkaquote#1#2#3{%
  \csq@addspecial{#2}{#1}{\csq@pdf@ooqmark}{}%
  \csq@addspecial{#3}{\csq@qclose}{\csq@pdf@coqmark}{}}

\newrobustcmd*{\MakeForeignQuote}{%
  \@ifstar
    {\csq@mkfquote\csq@iqopen@i}
    {\csq@mkfquote\csq@oqopen@i}}
\def\csq@mkfquote#1#2#3#4{%
  \csq@addspecial{#3}
    {\csq@bqgroup\csq@lang{#2}#1}
    {\csq@pdf@ooqmark}{}%
  \csq@addspecial{#4}
    {\csq@qclose@i{\csq@endlang}}
    {\csq@pdf@coqmark}{}}

\newrobustcmd*{\MakeHyphenQuote}{%
  \@ifstar
    {\csq@mkhquote\csq@iqopen@i}
    {\csq@mkhquote\csq@oqopen@i}}
\def\csq@mkhquote#1#2#3#4{%
  \csq@addspecial{#3}
    {\csq@bqgroup\csq@hyph{#2}#1}
    {\csq@pdf@ooqmark}{}%
  \csq@addspecial{#4}
    {\csq@qclose@i{\csq@endhyph}}
    {\csq@pdf@coqmark}{}}

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

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

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

\newrobustcmd*{\MakeHybridBlockQuote}[4]{%
  \csq@addbspecial{#2}{#3}{#4}%
    {\csq@bquote
       {\iftoggle{csq@block}{\csq@lang}{\csq@hyph}{#1}}
       {\iftoggle{csq@block}{\csq@endlang}{\csq@endhyph}}
       {\mkcitation}}}

\newrobustcmd*{\EnableQuotes}{}
\newrobustcmd*{\DisableQuotes}{}
\newrobustcmd*{\VerbatimQuotes}{}
\newrobustcmd*{\DeleteQuotes}{\csq@mkdelete}

\AtBeginDocument{%
  \protected\def\EnableQuotes{\csq@mkenable}%
  \protected\def\DisableQuotes{\csq@mkdisable}%
  \protected\def\VerbatimQuotes{\csq@mkverbatim}}

% Author interface

\newrobustcmd*{\@enablequotes}{%
  \toggletrue{csq@quiet}%
  \csq@mkenable
  \togglefalse{csq@quiet}}
\newrobustcmd*{\@disablequotes}{%
  \toggletrue{csq@quiet}%
  \csq@mkdisable
  \togglefalse{csq@quiet}}
\newrobustcmd*{\@verbatimquotes}{%
  \toggletrue{csq@quiet}%
  \csq@mkverbatim
  \togglefalse{csq@quiet}}
\newrobustcmd*{\@deletequotes}{%
  \toggletrue{csq@quiet}%
  \csq@mkdelete
  \togglefalse{csq@quiet}}

%% Switch quote styles manually

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

\def\csq@setqstyle[#1]#2{%
  \ifnum\csq@reset>\z@
    \csq@info{Disabling multilingual quotes}%
    \csq@reset\z@
  \fi
  \ifblank{#1}
    {\DeclareQuoteAlias{#2}{default}}
    {\DeclareQuoteAlias{#2/#1}{default}}}

\def\csq@resetqstyle{%
  \csq@info{Enabling multilingual quotes}%
  \csq@reset\@ne}

\AtEndPreamble{%
  \def\csq@setqstyle[#1]#2{%
    \ifnum\csq@reset=\@ne
      \csq@info{Disabling multilingual quotes}%
      \csq@reset\z@
    \fi
    \ifblank{#1}
      {\csq@setstyle{#2}}
      {\csq@setstyle{#2/#1}}}}

%% Define quote styles

\newrobustcmd*{\DeclareQuoteStyle}[2][]{%
  \begingroup
  \let\if@safe@actives\iftrue
  \ifblank{#1}
    {\edef\csq@tempa{#2}}
    {\edef\csq@tempa{#2/#1}}%
  \ifcsundef{csq@qstyle@\csq@tempa}
    {}
    {\csq@info{Redefining quotation style '\csq@tempa'}}%
  \@ifnextchar[%]
    {\csq@declareqstyle@i}
    {\csq@declareqstyle@i[]}}

\def\csq@declareqstyle@i[#1]{%
  \csgdef{csq@qstyle@\csq@tempa}{%
    \def\csq@thequote@oinit{%
      \let\csq@themarker\csq@kernmarker#1}}%
  \@ifnextchar[%]
    {\csq@declareqstyle@ii}
    {\csq@declareqstyle@ii[]}}

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

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

\def\csq@declareqstyle@iv[#1]#2{%
  \ifblank{#1}
    {\def\csq@thequote@kern{\z@}}
    {\def\csq@thequote@kern{#1}}%
  \csxappto{csq@qstyle@\csq@tempa}{%
    \def\noexpand\csq@thequote@kern{\csq@thequote@kern}}%
  \csgappto{csq@qstyle@\csq@tempa}{%
    \def\csq@thequote@iopen{#2}}%
  \@ifnextchar[%]
    {\csq@declareqstyle@v}
    {\csq@declareqstyle@v[]}}

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

\@onlypreamble\DeclareQuoteStyle

%% Define quote aliases

\newrobustcmd*{\DeclareQuoteAlias}[3][]{%
  \begingroup
  \let\if@safe@actives\iftrue
  \ifblank{#1}
    {\edef\csq@tempa{#2}}
    {\edef\csq@tempa{#2/#1}}%
  \ifcsundef{csq@qstyle@\csq@tempa}
    {\csq@error
       {Quote style not defined}
       {Define the style or alias '\csq@tempa' first}}
    {\ifcsundef{csq@qstyle@#3}
       {}
       {\csq@info{Redefining alias '#3' -> '\csq@tempa'}}%
     \csxdef{csq@qstyle@#3}{%
       \noexpand\csuse{csq@qstyle@\csq@tempa}}}%
  \endgroup}

\@onlypreamble\DeclareQuoteAlias

%% Define quotes for PDF strings

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

\@onlypreamble\DeclarePlainStyle

%% Define package options

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

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

%% Space factor codes

\newrobustcmd*{\DeclareQuoteGlyph}[2]{%
  \csxappto{csq@sfcodes@#1}{\sfcode\detokenize{#2}=\z@}}

%% Set defaults

\newrobustcmd*{\SetBlockThreshold}[1]{%
  \csq@tshold=#1\relax}

\newrobustcmd*{\SetBlockEnvironment}[1]{%
  \ifcsundef{#1}
    {\csq@err@undef{#1}}
    {\def\csq@blockenvironment{#1}}}

\newrobustcmd*{\SetCiteCommand}[1]{%
  \ifundef#1%
    {\csq@err@undef{#1}}
    {\def\csq@cite{#1}}}

%% Interface for biblatex

\newrobustcmd*{\@setquotesfcodes}{\csq@setsfcodes}

\newcommand*{\@quotelevel}{}
\let\@quotelevel\csq@qlevel

\newcommand*{\@quotereset}{}
\let\@quotereset\csq@reset

\newcommand*{\@ifquotemarker}{%
  \ifdim\lastskip=\csq@kernmarker\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

%% Author interface to auto quotes

\newrobustcmd*{\openautoquote}{%
  \ifbool{mmode}
    {\csq@err@mmode\openautoquote}
    {\csq@oqopen}}

\newrobustcmd*{\closeautoquote}{%
  \ifbool{mmode}
    {\csq@err@mmode\closeautoquote}
    {\csq@qclose}}

\newrobustcmd*{\openinnerquote}{%
  \ifbool{mmode}
    {\csq@err@mmode\openinnerquote}
    {\csq@iqopen}}

\newrobustcmd*{\closeinnerquote}{%
  \ifbool{mmode}
    {\csq@err@mmode\closeinnerquote}
    {\csq@qclose}}

\appto\csq@hook@hyperref{%
  \def\openautoquote{\csq@pdf@ooqmark}%
  \def\closeautoquote{\csq@pdf@coqmark}%
  \def\openinnerquote{\csq@pdf@oiqmark}%
  \def\closeinnerquote{\csq@pdf@ciqmark}}

%% Author interface to internal marks

\newrobustcmd*{\initoquote}{%
  \csq@resetstyle
  \csq@qlevel\@ne
  \csq@init}

\newrobustcmd*{\initiquote}{%
  \csq@resetstyle
  \csq@qlevel\tw@
  \csq@init}

\newrobustcmd*{\textooquote}{%
  \csq@resetstyle
  \csq@addkern@open
  \let\csq@kernchar@i\csq@thequote@oopen
  \csq@thequote@oopen
  \csq@setmarker@open
  \csq@fixkern}

\newrobustcmd*{\textmoquote}{%
  \csq@resetstyle
  \csq@thequote@omiddle}

\newrobustcmd*{\textcoquote}{%
  \ifdim\lastkern=\csq@omitmarker
  \else
    \csq@resetstyle
    \csq@addkern@close
    \csq@thequote@oclose
    \let\csq@kernchar@i\csq@thequote@oclose
    \csq@setmarker@close
    \expandafter\csq@fixkern
  \fi}

\newrobustcmd*{\textoiquote}{%
  \csq@resetstyle
  \csq@addkern@open
  \let\csq@kernchar@i\csq@thequote@iopen
  \csq@thequote@iopen
  \csq@setmarker@open
  \csq@fixkern}

\newrobustcmd*{\textmiquote}{%
  \csq@resetstyle
  \csq@thequote@imiddle}

\newrobustcmd*{\textciquote}{%
  \ifdim\lastkern=\csq@omitmarker
  \else
    \csq@resetstyle
    \csq@addkern@close
    \csq@thequote@iclose
    \let\csq@kernchar@i\csq@thequote@iclose
    \csq@setmarker@close
    \expandafter\csq@fixkern
  \fi}

%% Ellipses

\newrobustcmd*{\textelp}{%
  \begingroup
  \@ifstar
    {\@tempswatrue\csq@textelp}
    {\@tempswafalse\csq@textelp}}

\long\def\csq@textelp#1{%
  \ifblank{#1}
    {\endgroup\mktextelp}
    {\expandafter\endgroup\if@tempswa
       \expandafter\mktextinselp
     \else
       \expandafter\mktextelpins
     \fi
     {#1}}}

\newrobustcmd*{\textins}{%
  \@ifstar\mktextmod\mktextins}

\newrobustcmd*{\textdel}{%
  \mktextdel}

\newcommand{\mktextelp}{[\textellipsis\unkern]\@}
\newcommand{\mktextelpins}[1]{[\textellipsis\unkern]\@ [#1]}
\newcommand{\mktextinselp}[1]{[#1] [\textellipsis\unkern]\@}
\newcommand{\mktextins}[1]{[#1]}
\newcommand{\mktextmod}[1]{[#1]}
\newcommand{\mktextdel}[1]{[]}

%% Auxiliary commands for some styles

% French

\newrobustcmd*{\initfrenchquotes}{%
  \let\csq@themarker\csq@omitmarker}

\newrobustcmd*{\mkfrenchopenquote}[1]{%
  \leavevmode#1\@frenchquotespace}

\newrobustcmd*{\mkfrenchclosequote}[1]{%
  \leavevmode\@frenchquotespace#1}

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

\AtEndPreamble{%
  \@ifpackageloaded{polyglossia}
    {\@ifpackagelater{polyglossia}{2009/11/20}
       {\let\@frenchquotespace\@empty}
       {}%
     \@ifpackagelater{polyglossia}{2019/10/27}
       {}
       {\csq@warning@noline{%
          Outdated 'polyglossia' version detected.\MessageBreak
          csquotes works best with 'polyglossia' v1.45\MessageBreak
          (2019/10/27) or above, but you are using\MessageBreak
          '\csuse{ver@polyglossia.sty}'}}}
    {}}

% German

\newrobustcmd*{\fixligatures}{%
  \ifhmode
    \ifnum\spacefactor>3000
      \kern0pt
    \else
      \ifnum\spacefactor=1007
        \kern0pt
      \else
        \ifnum\spacefactor=1008
          \kern0pt
        \fi
      \fi
    \fi
  \fi}

% Special styles

\DeclareQuoteStyle{debug}
  {\textbf{[\number\@quotelevel]}}
  [\textbf{[\number\@quotelevel]}]
  {\textbf{[\number\@quotelevel]}}
  {\textbf{[\number\@quotelevel]}}
  [\textbf{[\number\@quotelevel]}]
  {\textbf{[\number\@quotelevel]}}

\DeclareQuoteStyle{fallback}
  {\textbf{?}}
  {\textbf{?}}
  {\textbf{?}}
  {\textbf{?}}

%% Package options

\newrobustcmd*{\ExecuteQuoteOptions}{\setkeys{csq}}
\@onlypreamble\ExecuteQuoteOptions

% Core options

\define@key{csq}{strict}[true]{%
  \ifstrequal{#1}{true}
    {\csq@info{Enabling strict warnings}%
     \let\csq@warnorerr\csq@warning@strict}
    {\csq@info{Disabling strict warnings}%
     \let\csq@warnorerr\csq@warning@loose}}

\define@key{csq}{debug}[true]{%
  \ifstrequal{#1}{true}
    {\let\csq@debug\csq@logdebug}
    {\let\csq@debug\@gobble}}

\define@key{csq}{autopunct}[true]{%
  \ifstrequal{#1}{true}
    {\let\csq@autopunct\csq@allpunct}
    {\let\csq@autopunct\@empty}}

\define@key{csq}{style}{%
  \csq@opt@autostyle@false
  \DeclareQuoteAlias{#1}{default}}

\define@key{csq}{autostyle}[true]{%
  \ifcsdef{csq@opt@autostyle@#1}
    {\csuse{csq@opt@autostyle@#1}}
    {\csq@err@opt{autostyle=#1}}}
\def\csq@opt@autostyle@false{%
  \csq@info{Disabling multilingual quotes}%
  \csq@reset\z@}
\def\csq@opt@autostyle@true{%
  \csq@info{Enabling multilingual quotes}%
  \csq@reset\@ne}
\def\csq@opt@autostyle@try{%
  \csq@reset\tw@}
\def\csq@opt@autostyle@once{%
  \csq@reset\thr@@}
\def\csq@opt@autostyle@tryonce{%
  \csq@reset4\relax}

\define@key{csq}{babel}[true]{%
  \csq@info{%
    Option 'babel' depreciated.\MessageBreak
    Using 'autostyle'}%
  \setkeys{csq}{autostyle=#1}}

\define@key{csq}{maxlevel}{%
  \ifnumless{#1}\tw@
    {\csq@err@opt{maxlevel=#1}}
    {\csq@maxlvl#1\relax}}

\define@key{csq}{threshold}{%
  \SetBlockThreshold{#1}}

\define@key{csq}{thresholdtype}[lines]{%
  \ifcsdef{csq@bcount@#1}%
    {\def\csq@thresholdtype{#1}}
    {\csq@err@opt{thresholdtype=#1}}}

\define@key{csq}{parthreshold}[true]{%
  \settoggle{csq@parthreshold}{#1}}

\define@key{csq}{splitcomp}[true]{%
  \ifstrequal{#1}{true}
    {\let\csq@splitcomp\z@}
    {\let\csq@splitcomp\@M}}

\define@key{csq}{csdisplay}[true]{%
  \setbool{csdisplay}{#1}}

\define@key{csq}{version}{%
  \ifdef\csq@getversion
    {}
    {\input{csq-compat.def}}%
  \csq@getversion{#1}}

% Initialize

\setkeys{csq}{thresholdtype=lines,parthreshold,splitcomp=false}

%% Initial setup

% Provide 'quote' environment

\ifundef\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}}
  {}

% Deferred last minute setup

\AtEndPreamble{%
  \ifnumgreater\csq@reset\z@
    {\csq@info{Checking for multilingual support..}%
     \@ifpackageloaded{polyglossia}
       {\csq@info{... found 'polyglossia' package}%
        \ifundef\mainbabelname
          {\def\csq@main@language{\xpg@main@language}}
          {\def\csq@main@language{\mainbabelname}}%
        \csq@hook@multilang}
       {\@ifpackageloaded{babel}
          {\csq@info{... found 'babel' package}%
           \def\csq@main@language{\bbl@main@language}%
           \csq@hook@multilang}
          {\csq@info{... none found}%
           \csq@hook@nomultilang}}}
    {\@ifpackageloaded{polyglossia}
       {}
       {\@ifpackageloaded{babel}
          {}
          {\csq@hook@nomultilang}}}%
  \undef\csq@hook@multilang
  \undef\csq@hook@nomultilang
  \@ifpackageloaded{hyperref}
    {\expandafter\pdfstringdefDisableCommands
     \expandafter{\csq@hook@hyperref}}
    {}%
  \undef\csq@hook@hyperref
  \appto\@noligs{\@verbatimquotes}%
  \@ifpackageloaded{verbatim}
    {\addto@hook\every@verbatim{\@verbatimquotes}}
    {}%
  \@ifpackageloaded{fancyvrb}
    {\appto\FV@CatCodes{\@verbatimquotes}}
    {}%
  \@ifpackageloaded{endnotes}
    {\BlockquoteDisable{%
       \renewcommand{\endnote}[2][]{%
         \ifblank{#1}
           {\endnotemark\relax}
           {\endnotemark[#1]}}%
       \renewcommand{\endnotetext}[2][]{}%
       \let\addtoendnotes\@gobble}}
    {}%
  \ifdef\collect@body % amsmath
    {\pretocmd\collect@body{\@disablequotes}
       {}
       {\csq@err@patch{'amsmath' package}}}
    {}}

\AtBeginDocument{%
  \csq@setstyle{default}%
  \csq@mkenable}

% Restore catcodes

\csq@catcodes
\undef\csq@catcodes

% Load predefined styles

\input{csquotes.def}

% Load configuration file

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

% Process options

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

\ProcessOptions*

\endinput
