% \iffalse meta-comment
% 
% This is file `caption-koma.dtx'.
% 
% Copyright (C) 2004-2023 Axel Sommerfeldt (axel.sommerfeldt@f-m.fm)
% 
% --------------------------------------------------------------------------
% 
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2003/12/01 or later.
% 
% This work has the LPPL maintenance status "maintained".
% 
% This Current Maintainer of this work is Axel Sommerfeldt.
% 
% This work consists of the files
%   caption.ins, caption.dtx, caption-light.dtx, caption2.dtx, caption3.dtx,
%   caption-ams-smf.dtx, caption-beamer.dtx, caption-elsarticle.dtx,
%   caption-koma.dtx, caption-memoir.dtx, caption-ntg.dtx,
%   caption-thesis.dtx, bicaption.dtx, ltcaption.dtx, subcaption.dtx,
% the derived files
%   caption.sty, caption-light.sty, caption2.sty, caption3.sty,
%   caption-ams-smf.sto, caption-beamer.sto, caption-elsarticle.sto,
%   caption-koma.sto, caption-memoir.sto, caption-ntg.sto,
%   caption-thesis.sto, bicaption.sty, ltcaption.sty, subcaption.sty.
% 
% \fi
%
% \CheckSum{262}
%
% \iffalse
%<*driver>
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesFile{caption-koma.drv}[2023/07/10 v2.0 Implementation of the caption-koma package]
%\errorcontextlines=3
%
\documentclass{captiondoc}
\hypersetup{pdfkeywords={LaTeX, package, caption-koma}}
%
\begin{document}
  \DocInput{caption-koma.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{caption-koma.drv}
% \let\docdate\filedate
% \let\docversion\fileversion
% \GetFileInfo{caption-koma.sto}
%
% \title{\texorpdfstring
%   {The adaption of the \pkg{caption} package to the \KOMAScript\ document classes\thanks{%^^A
%    This adaption has version number \docversion.}}%^^A
%   {The adaption of the caption package to the KOMA-Script document classes}}
% \author{Axel Sommerfeldt\\
%         \url{https://gitlab.com/axelsommerfeldt/caption}}
% \date{\docdate}
% \maketitle
%
% \begin{abstract}
% This package adapts the \pkg{caption} package to the \KOMAScript\ document classes.
% \end{abstract}
%
% \section*{User manual}
%
% This document is describing the code implementation only.
% The user documentation can be found in
% \nopagebreak\begin{quote}
% \begin{tabular}{ll}
% \href{http://mirror.ctan.org/macros/latex/contrib/caption/caption.pdf}%
%      {\texttt{caption.pdf}} & The caption package documentation \\
% \end{tabular}
% \end{quote}
%
% \section*{State of this package}
%
% Please note that the last major revision of this code was done in the year 2007,
% afterwards the \KOMAScript\ support was adapted to changes in the \pkg{caption} package only.
%
% So nearly everything which has changed in the caption support of \KOMAScript\ since 2007 is not reflected here.
% (So for example |\set|\-|cap|\-|dyn|\-|width| is not emulated yet etc.)
%
% \StopEventually{}
% \iffalse
% \clearpage
% \tableofcontents
% \fi
%
% \iffalse
% --------------------------------------------------------------------------- %
% \fi
%
% \DoNotIndex{\\,\_,\ ,\@@par}
% \DoNotIndex{\@bsphack}
% \DoNotIndex{\@car,\@cdr,\@classoptionslist,\@cons,\@currext,\@currname}
% \DoNotIndex{\@ehc,\@ehd,\@empty,\@esphack,\@expandtwoargs}
% \DoNotIndex{\@for,\@firstofone,\@firstoftwo}
% \DoNotIndex{\@gobble,\@gobblefour,\@gobbletwo,\@hangfrom}
% \DoNotIndex{\if@minipage,\@ifnextchar,\@ifpackagelater,\@ifpackageloaded}
% \DoNotIndex{\@ifstar,\@ifundefined,\@latex@error,\@minipagefalse,\@minipagetrue}
% \DoNotIndex{\@namedef,\@nameuse}
% \DoNotIndex{\@onlypreamble,\@parboxrestore,\@plus,\@ptionlist}
% \DoNotIndex{\@removeelement,\@restorepar,\@secondoftwo,\@setminipage,\@setpar}
% \DoNotIndex{\@tempa,\@tempboxa,\@tempdima,\@tempdimb,\@tempdimc,\@tempb,\@tempc}
% \DoNotIndex{\@testopt}
% \DoNotIndex{\@undefined,\@unprocessedoptions,\@unusedoptionlist}
% \DoNotIndex{\p@,\z@}
% \DoNotIndex{\active,\addtocounter,\addtolength,\advance,\aftergroup}
% \DoNotIndex{\baselineskip,\begin,\begingroup,\bfseries,\box}
% \DoNotIndex{\catcode,\centering,\changes,\csname,\def,\divide,\do,\downarrow}
% \DoNotIndex{\edef,\else,\empty,\end,\endcsname,\endgraf,\endgroup,\expandafter}
% \DoNotIndex{\fi,\footnotesize,\global}
% \DoNotIndex{\hangindent,\hbox,\hfil,\hsize,\hskip,\hspace,\hss}
% \DoNotIndex{\ifcase,\ifdim,\ifnum,\ifodd,\ifvoid,\ifvmode}
% \DoNotIndex{\ifx,\ignorespaces,\itshape}
% \DoNotIndex{\kernel@ifnextchar}
% \DoNotIndex{\Large,\large,\leavevmode,\leftmargini,\leftskip,\let,\linewidth}
% \DoNotIndex{\llap,\long,\m@ne,\margin,\mdseries,\message}
% \DoNotIndex{\newcommand,\newdimen,\newlength,\newline,\newif,\newsavebox}
% \DoNotIndex{\next,\nobreak,\nobreakspace,\noexpand,\noindent,\numberline}
% \DoNotIndex{\normalcolor,\normalfont,\normalsize,\or,\par,\parbox,\parfillskip}
% \DoNotIndex{\parindent,\parskip,\prevdepth,\protect,\protected@edef,\protected@write}
% \DoNotIndex{\providecommand,\quad}
% \DoNotIndex{\raggedleft,\raggedright,\relax,\renewcommand,\RequirePackage}
% \DoNotIndex{\rightskip,\rmfamily}
% \DoNotIndex{\sbox,\scriptsize,\scshape,\setbox,\setlength,\sffamily,\slshape}
% \DoNotIndex{\small,\string,\space,\strut}
% \DoNotIndex{\textheight,\the,\toks@,\typeout,\ttfamily}
% \DoNotIndex{\unvbox,\uparrow,\upshape,\usebox,\usepackage}
% \DoNotIndex{\value,\vbox,\vsize,\vskip,\wd,\width,\z@skip}
% \DoNotIndex{\AtBeginDocument,\AtEndOfPackage,\CurrentOption,\DeclareOption}
% \DoNotIndex{\ExecuteOptions,\GenericWarning,\IfFileExists,\InputIfFileExists}
% \DoNotIndex{\NeedsTeXFormat,\MessageBreak}
% \DoNotIndex{\PackageError,\PackageInfo,\PackageWarning,\PackageWarningNoLine}
% \DoNotIndex{\PassOptionsToPackage,\ProcessOptions,\ProvidesPackage}
%
% \iffalse
% --------------------------------------------------------------------------- %
% \fi
%
% \setlength{\parskip}{0pt plus 1pt}
% \newcommand*\Note[2][Note]{\par{\small\emph{#1:} #2}\par}
%
% \changes{v1.0a}{2004/01/18}{Minimum adaptation to \KOMAScript\ added}
% \changes{v1.0h}{2005/08/22}{\KOMAScript\ compatibility options added}
% \changes{v1.0i}{2005/11/17}{\KOMAScript\ compatibility commands added}
% \changes{v1.0l}{2007/02/18}{\KOMAScript\ compatibility revised}
% \changes{v1.1}{2007/03/17}{\KOMAScript\ compatibility options removed}
% \changes{v1.1}{2007/03/31}{\KOMAScript\ classes support added}
% \changes{v1.1}{2007/04/05}{\KOMAScript\ compatibility revised \& enhanced}
% \changes{v2.0}{2020/07/27}{\KOMAScript\ class support adapted to \pkg{caption3}~\version{2.0}}
%
% \iffalse
% --------------------------------------------------------------------------- %
% \fi
%
% \clearpage
%
% \iffalse
%<*package>
% \fi
%
% \section{Identification}
%
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesFile{caption-koma.sto}[2023/09/08 v2.0e Adaption of the caption package to the KOMA-Script document classes (AR)]
%    \end{macrocode}
%
% \section{Margin or width}
%
% \begin{macro}{\setcapwidth}
% \changes{v1.9}{2018/12/26}{Bugfix: Missing curly braces added}
% Patch |\setcapwidth| so it will set the \pkg{caption3} width setting, too.
%    \begin{macrocode}
\expandafter\let\expandafter\caption@koma@setcapwidth
                \csname\string\setcapwidth\endcsname
\@namedef{\string\setcapwidth}[#1]#2{%
  \caption@koma@setcapwidth[{#1}]{#2}%
  \caption@setcapwidth@opt{#1}%
  \caption@setcapwidth}
%    \end{macrocode}
% The optional argument of \cs{setcapwidth} if not supported (yet),
% so we issue a warning if used.
% (Since this does not seem to have an negative effect when used
%  by the \texttt{captionbeside} environment, we suppress the warning here.)
%    \begin{macrocode}
\newcommand*\caption@setcapwidth@opt[1]{}
\AtCaptionPackage{\renewcommand*\caption@setcapwidth@opt[1]{%
  \ifx\\#1\\\else
    \caption@ifdefined\cap@margin{%
      \def\@tempa{captionbeside}%
      \ifx\@tempa\@currenvir\else\caption@Warning{%
        Ignoring optional argument [#1] of \string\setcapwidth\MessageBreak}%
      \fi}{}%
  \fi}}
%    \end{macrocode}
%    \begin{macrocode}
\newcommand*\caption@setcapwidth{%
  \captionsetup{width=\cap@width}}
%    \end{macrocode}
%    \begin{macrocode}
\def\caption@tempa{\hsize}%
\ifx\caption@tempa\cap@width \else
  \caption@setcapwidth
\fi
%    \end{macrocode}
% \end{macro}
%
% \emph{TODO:} |\setcapdynwidth|
%
% \begin{macro}{\setcapmargin}
% Patch |\setcapmargin| so it will set the \pkg{caption3} margin setting, too.
%    \begin{macrocode}
\expandafter\let\expandafter\caption@koma@setcapmargin
                \csname\string\@setcapmargin\endcsname
\@namedef{\string\@setcapmargin}[#1]#2{%
  \caption@koma@setcapmargin[{#1}]{#2}%
  \caption@setcapmargin}
%    \end{macrocode}
%    \begin{macrocode}
\expandafter\let\expandafter\caption@koma@@setcapmargin
                \csname\string\@@setcapmargin\endcsname
\@namedef{\string\@@setcapmargin}[#1]#2{%
  \caption@koma@@setcapmargin[{#1}]{#2}%
  \caption@setcapmargin}
%    \end{macrocode}
%    \begin{macrocode}
\newcommand*\caption@setcapmargin{%
  \begingroup
    \let\onelinecaptionsfalse\relax
    \def\@twoside{0}%
    \def\if@twoside{\def\@twoside{1}\iffalse}%
    \cap@margin
    \def\@tempa{\endgroup}%
    \ifx\cap@left\hfill\else\ifx\cap@right\hfill\else
      \def\hspace##1##{\@firstofone}%
      \edef\@tempa{\endgroup
        \noexpand\captionsetup{%
          twoside=\@twoside,slc=0,%
          margin={\cap@left,\cap@right}}}%
    \fi\fi
    \@tempa}
%    \end{macrocode}
%    \begin{macrocode}
\ifx\cap@margin\relax \else
  \caption@setcapmargin
\fi
%    \end{macrocode}
% \end{macro}
%
% \section{Indentions}
%
% \begin{macro}{\setcapindent}
% Patch |\setcapindent| so it will set the \pkg{caption3} indention setting, too.
%    \begin{macrocode}
\let\caption@koma@setcapindent\@setcapindent
\renewcommand*\@setcapindent[1]{%
  \caption@koma@setcapindent{#1}%
  \caption@setcapindent}
%    \end{macrocode}
%    \begin{macrocode}
\let\caption@koma@@setcapindent\@@setcapindent
\renewcommand*\@@setcapindent[1]{%
  \caption@koma@@setcapindent{#1}%
  \caption@setcapindent}
%    \end{macrocode}
%    \begin{macrocode}
\newcommand*\caption@setcapindent{%
  \captionsetup{indent=\ifdim\cap@indent<\z@\z@\else\cap@indent\fi}}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\caption@ifdefined\cap@indent{\caption@setcapindent}{}
%    \end{macrocode}
%
% \section{Single-line-check}
%
% \begin{macro}{\ifonelinecaptions}
% \changes{v1.1g}{2008/03/01}{\cs{def} changed to \cs{g@addto@macro}}
% \changes{v2.0d}{2023/09/08}{Re-written using \cs{DeclareCaptionSinglelinecheck} and \cs{SetCaptionDefault}}
% Define an own single-line-check which depends on |\ifonelinecaptions| offered by \KOMAScript.
%    \begin{macrocode}
\DeclareCaptionSinglelinecheck{koma}{%
  \ifonelinecaptions
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}
%    \end{macrocode}
% Make this new single-line-check the default one.
%    \begin{macrocode}
\SetCaptionDefault{singlelinecheck}{koma}
%    \end{macrocode}
% Patch |\onelinecaptionstrue| and |onelinecaptionsfalse| so they will set the corresponding \pkg{caption3} setting, too.
%    \begin{macrocode}
\g@addto@macro\onelinecaptionstrue{\caption@setsinglelinecheck{koma}}
\g@addto@macro\onelinecaptionsfalse{\caption@setsinglelinecheck{koma}}
%    \end{macrocode}
% \end{macro}
%
% \section{Format}
%
% The `koma' caption format was taken from \KOMAScript\ \cs{@makecaption} and adapted.
%    \begin{macrocode}
\DeclareCaptionFormat{koma}[#1#2#3\par]{%
  \ifdofullc@p
    \caption@useformat{hang}{#1}{#2}{#3}%
  \else
    #1#2%
    \ifdim\cap@indent<\z@
      \par
      \noindent\hspace*{-\cap@indent}%
    \else\if@capbreak
      \par
    \fi\fi
    #3\par
  \fi}
%    \end{macrocode}
%    \begin{macrocode}
\SetCaptionDefault{format}{koma}
%    \end{macrocode}
%
% \section{Label format}
%
%    \begin{macrocode}
\DeclareCaptionLabelFormat{koma}{\bothIfFirst{#1}{\nobreakspace}#2\autodot}
%    \end{macrocode}
% The `fallback' caption label format maps to `koma'.
%    \begin{macrocode}
\SetCaptionFallback{labelformat}{koma}
%    \end{macrocode}
%
% \section{Label separator}
%
% The `default' caption label separator maps to \cs{captionformat}.
%    \begin{macrocode}
\DeclareCaptionLabelSeparator{koma}{\captionformat}
%    \end{macrocode}
%    \begin{macrocode}
\SetCaptionDefault{labelseparator}{koma}
%    \end{macrocode}
%
% \section{Fonts}
%
% The `default' fonts map to \cs{scr@fnt@caption} or \cs{scr@fnt@captonlabel}.
%    \begin{macrocode}
\DeclareCaptionFont{scr@font}{\scr@fnt@caption}
\DeclareCaptionFont{scr@labelfont}{\scr@fnt@captionlabel}
\SetCaptionDefault{font}{scr@font}
\SetCaptionDefault{labelfont}{scr@labelfont}
%    \end{macrocode}
%
% \section{Positioning}
%
% Here we patch the caption related \KOMAScript\ commands to set \pkg{caption} package settings as well.
% Furthermore we take over the caption related settings from the \KOMAScript\ classes.
%
% \begin{macro}{\if@captionabove}
% \changes{v1.0j}{2006/03/21}{Bugfix 2006-03-21: \cs{let}\cs{caption@setposition}\cs{@gobble} added}
% \changes{v1.0n}{2006/03/09}{Accidentally this got broken in \version{1.0m}, fixed}
% \changes{v1.1}{2007/03/31}{We redefine \cs{captionabovetrue/false} now instead of \cs{captionabove/below}}
% \changes{v1.1a}{2007/09/14}{Bugfix 2007-09-14: Redefinition of \cs{@captionabovetrue} \& \cs{@captionabovefalse} for \env{longtable} added}
% \changes{v1.1g}{2008/03/01}{\cs{def} changed to \cs{g@addto@macro}}
% \changes{v1.1k}{2009/10/09}{\opt{figureposition} and \opt{tableposition} will issue a warning now}
% \changes{v1.8e}{2019/09/11}{\opt{figureposition} and \opt{tableposition} will now set the position anyway since it could be used by other packages}
% \changes{v2.0a}{2020/09/12}{Faulty \cs{AtBeginCaption} replaced with correct \cs{AfterCaptionPackage}}
% Patch |\@captionabovetrue| and |\@captionabovefalse| so they will set the \pkg{caption3} position setting, too.
% Note that these are stronger than the \opt{position} setting, therefore we override the options
% \opt{figureposition} and \opt{tableposition} to typeout a warning.
%    \begin{macrocode}
\g@addto@macro\@captionabovetrue{\caption@setposition{t}}%
\g@addto@macro\@captionabovefalse{\caption@setposition{b}}%
%    \end{macrocode}
%    \begin{macrocode}
\if@captionabove
  \@captionabovetrue
\else
  \@captionabovefalse
\fi
%    \end{macrocode}
% |\captionabove| \& |\captionbelow| for longtable:
%    \begin{macrocode}
\AfterCaptionPackage{\caption@AtBeginLongtable{%
  \def\@captionabovetrue{\LT@captionsetup{position=t}}%
  \def\@captionabovefalse{\LT@captionsetup{position=b}}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\if@tablecaptionabove}
% \changes{v1.1g}{2008/03/01}{\cs{def} changed to \cs{g@addto@macro}}
% Patch |\@tablecaptionabovetrue| and |\@tablecaptionabovefalse| so they will set the \pkg{caption3} position setting, too.
%    \begin{macrocode}
\g@addto@macro\@tablecaptionabovetrue{\captionsetup*[table]{position=t}}%
\g@addto@macro\@tablecaptionabovefalse{\captionsetup*[table]{position=b}}%
%    \end{macrocode}
%    \begin{macrocode}
\if@tablecaptionabove
  \@tablecaptionabovetrue
\else
  \@tablecaptionabovefalse
\fi
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\if@figurecaptionabove}
% \changes{v1.5}{2013/02/15}{Support of \cs{if@figurecaptionabove} added}
% Patch |\@figurecaptionabovetrue| and |\@figurecaptionabovefalse| so they will set the \pkg{caption3} position setting, too.
%    \begin{macrocode}
\@ifundefined{@figurecaptionabovetrue}{}{%
  \g@addto@macro\@figurecaptionabovetrue{\captionsetup*[figure]{position=t}}%
  \g@addto@macro\@figurecaptionabovefalse{\captionsetup*[figure]{position=b}}%
%    \end{macrocode}
%    \begin{macrocode}
  \if@figurecaptionabove
    \@figurecaptionabovetrue
  \else
    \@figurecaptionabovefalse
  \fi}
%    \end{macrocode}
% \end{macro}
%
% Since the \KOMAScript\ position setting overwrites the one from the \pkg{caption} package,
% we re-define the options |figure|\-|position| and |table|\-|position| to issue a warning.
% \Note{But we set the value anyway since it will be used by sub-captions.}
%
%    \begin{macrocode}
\AtCaptionPackage{%
  \let\caption@koma@figureposition\KV@caption@figureposition
  \DeclareCaptionOption{figureposition}{%
    \caption@WarningNoLine{%
      Option `figureposition=#1' has no effect\MessageBreak
      when used with a KOMA-Script document class}%
    \caption@koma@figureposition{#1}}
%    \end{macrocode}
%    \begin{macrocode}
  \let\caption@koma@tableposition\KV@caption@tableposition
  \DeclareCaptionOption{tableposition}{%
    \caption@WarningNoLine{%
      Option `tableposition=#1' has no effect\MessageBreak
      when used with a KOMA-Script document class}%
    \caption@koma@tableposition{#1}}}
%    \end{macrocode}
%
% \section{Adaption of \cs{caption} command}
%
% \begin{macro}{\scr@caption}
% \KOMAScript\ contains the code
% |\AtBeginDocument{\let\scr@caption\caption}|
% so we need to update |\scr@caption| after the \pkg{caption} package has re-defined |\caption|.
%    \begin{macrocode}
\AtBeginDocument{\let\scr@caption\caption}
%    \end{macrocode}
% \end{macro}
%
% \iffalse
%</package>
% \fi
%
% \iffalse
% --------------------------------------------------------------------------- %
% \fi
%
% \clearpage
% \Finale
%
\endinput