% Copyright 2022 by Qrrbrbirlbel
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%

\usetikzlibrary{calendar}
\pgfutil@usemodule{pgfcalendar-ext}

% Shift between days

\def\tikz@lib@cal@xshift{\pgfkeysvalueof{/tikz/day xshift}}
\def\tikz@lib@cal@yshift{\pgfkeysvalueof{/tikz/day yshift}}
\pgfkeyslet{/tikz/day xshift/.@cmd}\@undefined
\pgfkeyslet{/tikz/day xshift/.@body}\@undefined
\pgfkeyslet{/tikz/day xshift/.@def}\@undefined
\pgfkeyslet{/tikz/day yshift/.@cmd}\@undefined
\pgfkeyslet{/tikz/day yshift/.@body}\@undefined
\pgfkeyslet{/tikz/day yshift/.@def}\@undefined
\tikzset{day xshift/.initial=3ex,day yshift/.initial=3.5ex}

% Shift between months

\def\tikz@lib@cal@month@xshift{\pgfkeysvalueof{/tikz/month xshift}}
\def\tikz@lib@cal@month@yshift{\pgfkeysvalueof{/tikz/month yshift}}
\pgfkeyslet{/tikz/month xshift/.@cmd}\@undefined
\pgfkeyslet{/tikz/month xshift/.@body}\@undefined
\pgfkeyslet{/tikz/month xshift/.@def}\@undefined
\pgfkeyslet{/tikz/month yshift/.@cmd}\@undefined
\pgfkeyslet{/tikz/month yshift/.@body}\@undefined
\pgfkeyslet{/tikz/month xshift/.@def}\@undefined
\tikzset{month xshift/.initial=9ex,month yshift/.initial=9ex}

% Internal option for storing the "width" of a calendar as a number of
% days

\def\tikz@lib@cal@width{\pgfkeysvalueof{/tikz/tikz@lib@cal@width}}
\pgfkeyslet{/tikz/tikz@lib@cal@width/.@cmd}\@undefined
\pgfkeyslet{/tikz/tikz@lib@cal@width/.@body}\@undefined
\pgfkeyslet{/tikz/tikz@lib@cal@width/.@def}\@undefined
\tikzset{tikz@lib@cal@width/.initial=1}

% Weeks

\def\tikzweekcode{\pgfkeysvalueof{/tikz/week code}}
\def\tikzweektext{\pgfkeysvalueof{/tikz/week text}}

\tikzset{
  week code/.initial={%
    \node[every week]{\pgfkeysvalueof{/tikz/week text}};},
  week text/.initial={\%n=},
  every week/.style=,
  week label left/.style={
    every week/.append style={
      anchor=base east,
      xshift=-1.25*\pgfkeysvalueof{/tikz/day xshift}},
    execute before day scope=%
      \ifdate{Monday, equals=\pgfcalendarbeginiso}{\tikzweekcode}{}}}

%
% Overwriting original \tikz@lib@cal@if@else@code
%
\def\tikz@lib@cal@if@else@code#1#2#3{%
  \pgfutil@addto@macro\tikz@lib@cal@ifs{\ifdate{#1}{#2}{#3}}%
  \tikz@lib@cal@scanner%
}

%
% Overwriting original \tikz@lib@cal@stop
%
\def\tikz@lib@cal@stop{%
    \pgftransformshift{\tikz@node@at}%
    \expandafter\pgfcalendar\expandafter{\tikz@fig@name}{\tikz@lib@cal@start}{\tikz@lib@cal@end}%
    {%
      \tikz@before@day%
      \scope%
        \tikz@atbegin@day%
        \let\pgfutil@addto@macro\pgfutil@secondoftwo
        \tikz@lib@cal@ifs%
        \tikzdaycode%
        \tikz@atend@day%
      \endscope%
      \tikz@after@day%
    }%
  \endgroup%
}

\def\pgfutil@addto@macro#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}

\pgfmathdeclarefunction{weeksinmonthofyear}{3}{% #1 = first weekday, #2 = month, #3 = year
  \begingroup
    \pgfmathlastdayinmonthofyear@{#2}{#3}%
    \pgfmathadd@{#1}{\pgfmathresult}%
    \pgfmathdivide@{\pgfmathresult}{7}%
    \pgfmathceil@{\pgfmathresult}%
    \pgfmathint@{\pgfmathresult}%
    \pgfmathsmuggle\pgfmathresult
  \endgroup
}
\pgfmathdeclarefunction{lastdayinmonthofyear}{2}{% #1 = month, #2 = year
  \begingroup
    \pgfcalendar@getlastYMX{#2}{#1}{\pgfutil@tempcnta}%
    \edef\pgfmathresult{\the\pgfutil@tempcnta}%
    \pgfmathsmuggle\pgfmathresult
  \endgroup
}