% \iffalse meta-comment
%
% Copyright (C) 2025 Dimitri Theulings <rector@becclesparish.org.uk>
% ------------------------------------------------------------------
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% 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.3c or later is part of all distributions of LaTeX
% version 2008-05-04 or later.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{liturgy-cw.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[2020-02-02]
%<package>\ProvidesPackage{liturgy-cw}
%<*package>
[2025-08-13 v0.3 LaTeX package for typesetting Common Worship liturgies]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{liturgy-cw}[2025-08-13]
\usepackage{xcolor}
\usepackage{verse}
\usepackage[most]{tcolorbox}
\newtcolorbox{resultbox}{%
	left=3pt, top=3pt, bottom=3pt,
	before skip=11pt, after skip=11pt, arc=1mm,
	borderline={0.5pt}{0pt}{black},colback=white, colframe=white}
\newtcblisting{commandbox}{%
	breakable,enhanced,verbatim ignore percent,
	left=3pt, top=3pt, bottom=3pt,
	before skip=11pt, after skip=11pt, arc=1mm,
	borderline={0.5pt}{0pt}{black},colback=blue!10!white,colframe=blue!50!black,
	listing only,
	listing options={style=tcblatex,inputencoding=utf8,basicstyle=\ttfamily\small}}
\EnableCrossrefs
\RecordChanges
\begin{document}
	\OnlyDescription
	\DocInput{liturgy-cw.dtx}
	\PrintChanges
	\PrintIndex
\end{document}
%</driver>
% \fi
%
% \changes{v0.1}{2025-05-02}{Initial version}
% \changes{v0.2}{2025-06-03}{Initial release}
% \changes{v0.3}{2025-08-13}{Minor improvements}
%
% \GetFileInfo{liturgy-cw.dtx}
%
% \title{The \textsf{liturgy-cw} package\thanks{This document
%   relates to \textsf{liturgy-cw}~\fileversion, last revised \filedate.}}
% \author{Dimitri Theulings \\ \small\texttt{rector@becclesparish.org.uk} \\
%	\small\texttt{https://github.com/dimitrit/liturgy-cw}}
%
% \maketitle
%
% \begin{abstract}
%	The \textsf{liturgy-cw} package greatly simplifies the typesetting of
%	service sheets and booklets in the style of the Common Worship liturgical
%	resources of the Church~of~England. The package provides commands for a
%	number of liturgical elements, including rubrics, responsories and
%   `required part' indicators.
% \end{abstract}
%
% \section{Introduction}
%
% For the past quarter century \textit{Common Worship Prayers and Services}\cite{commonworship}
% has been the authorised liturygy for worship in the Church of England.
% For practical and (not least) financial reasons many churches have produced 
% their own service sheets and booklets. These materials are often created 
% using widely available word processing and desktop publishing software 
% packages with varying success.
%
% The \textsf{liturgy-cw} package arose from the need to create service
% sheets and booklets quickly and easily, while ensuring these retain a
% consistent visual style that is familiar to both worshipers and service
% leaders. Given the familiarity that these users have with the visual style
% of Common Worship made its design the obvious target for this package.
%
% Morgan's account of the making of Common Worship\cite{morgan} details
% the design, together with rationales for various decisions that had to
% be made in its implementation. While the \textsf{liturgy-cw} package
% falls far short of an exact, or even complete, implementation of this
% design, it should nevertheless prove useful in fulfilling its basic
% objectives.
%
% \section{Limitations}
%
% Because of its dependencies, including \textsf{fontspec}, the
% \textsf{liturgy-cw} package requires Lua\TeX\ or other supported
% \TeX\ engine to function.
%
% For best results you should have both \textit{Gill Sans MT} and
% \textit{Noto Sans Liturgy} fonts installed on your system. The latter
% is required for the symbol functions, |\Rx|, |\Pause| and |\Cross|.
%
% \section{Bug reports and feature requests}
% \label{repo}
% The source code, together with instructions for building and installing
% the package, can be found on github;
% \url{https://github.com/dimitrit/liturgy-cw}.
% Please raise a github issue if you find any bugs or would like to suggest
% a new feature.
%
% \section{Package documentation}
%
% \subsection{Basic usage}
% The \textsf{liturgy-cw} package provides functions and environments to
% create service booklets and sheets in the style of Common Worship. The
% simple example below typesets the contemporary version of the Lord's
% prayer as a `required part'. A comprehensive example, `\textit{An
% Order for Night Prayer (Compline)}' is included in the source repository.
%
% \vspace*{1ex}
%
% \noindent\begin{commandbox}
% \begin{required}
%  \rubric{The Lord's Prayer is said}
%
%  \minister{
%    Rejoicing in God’s new creation,\\
%    as our Saviour taught us, so we pray}
%  \lpc
% \end{required}
% \end{commandbox}
%
% \vspace*{1ex}
%
% \noindent\begin{resultbox}
% \setmainfont[Ligatures=TeX]{Gill Sans MT}
% \setlength\vbarhskip{-3pt}
% \begin{required}
%   \rubric{The Lord's Prayer is said}
%
%   \minister{Rejoicing in God’s new creation,\\
%     as our Saviour taught us, so we pray}
%   \lpc
% \end{required}
% \end{resultbox}
%
% \subsection{Commands for typesetting liturgies}
%
% \subsubsection{Environments}
% \DescribeEnv{required}
% Required parts of Common Worship liturgy are indicated by a vertical red
% bar. The |required| environment can be used to mark such sections. The
% position of the vertical bar is defined by |\vbarhskip|.
%
% \subsubsection{Functions}
% \DescribeMacro{rubric}
% The |\rubric|\marg{directive} function adds a direction for the conduct of
% the service in red, italicised print. For example:
%
% \vspace*{1ex}
%
% \noindent\begin{commandbox}
% \rubric{The appointed psalmody is said.}
% \end{commandbox}
%
% \vspace*{1ex}
%
% \noindent\begin{resultbox}
% \setmainfont[Ligatures=TeX]{Gill Sans MT}
% \rubric{The appointed psalmody is said.}
% \end{resultbox}
%
% \DescribeMacro{minister}
% Words spoken by clergy are included using the |\minister|\oarg{role}\marg{words}
% function. Using the optional \textit{role} argument it is possible to specify
% a ministry role, for example:
%
% \vspace*{1ex}
%
% \noindent\begin{commandbox}
%  \minister[President]{%
%    The harvest of the Spirit is love, joy and peace.\\
%    The peace of the Lord be always with you}
%  \all{and also with you.}
%
%  \minister[Deacon]{Let us offer one another a sign of peace.}
% \end{commandbox}
%
% \vspace*{1ex}
%
% \noindent\begin{resultbox}
% \setmainfont[Ligatures=TeX]{Gill Sans MT}
%  \minister[President]{The harvest of the Spirit is love, joy and peace.\\
%    The peace of the Lord be always with you}
%  \all{and also with you.}
%
%  \minister[Deacon]{Let us offer one another a sign of peace.}
% \end{resultbox}
%
% \DescribeMacro{all}
% |\all|\marg{words} is used to highlight the words that are to be spoken by
% the whole congregation. Words are printed in bold, preceded by the italicised
% word \textit{All} in red, printed in the margin. The \textit{All} directive
% is omitted when using the starred version, |\all*|\marg{words}, as shown in
% the example:
%
% \vspace*{1ex}
%
% \noindent\begin{commandbox}
%  \minister{Lord, hear us.}
%  \all*{Lord, graciously hear us.}
% \end{commandbox}
%
% \vspace*{1ex}
%
% \noindent\begin{resultbox}
% \setmainfont[Ligatures=TeX]{Gill Sans MT}
%  \minister{Lord, hear us.}
%  \all*{Lord, graciously hear us.}
% \end{resultbox}
%
% \subsubsection{Symbols}
% The symbol functions require the \textit{Noto Sans Liturgy} font to be
% installed on your system. This font can be downloaded from the package
% source repository (see `\textit{Bug reports and feature requests}' on page
% \pageref{repo}).
%
% \DescribeMacro{Cross} The \Cross\ symbol can be included in the liturgy
% using |\Cross|:
%
% \vspace*{1ex}
%
% \noindent\begin{commandbox}
%	\textit{N}, I baptize you\\
% 	in the name of the Father, \Cross\\
%	and of the Son,\\
%	and of the Holy Spirit.
% \end{commandbox}
%
% \vspace*{1ex}
%
% \noindent\begin{resultbox}
% \setmainfont[Ligatures=TeX]{Gill Sans MT}
%	\textit{N}, I baptize you\\
% 	in the name of the Father, \Cross\\
%	and of the Son,\\
%	and of the Holy Spirit.
% \end{resultbox}
%
% \DescribeMacro{Pause}
% \DescribeMacro{Rx} In the Common Worship Psalter pauses and refrains are
% indicated using the \Pause\ and \Rx\ symbols respectively. Support for
% these symbols is provided through the |\Pause| and |\Rx| functions:
%
% \vspace*{1ex}
%
% \noindent\begin{commandbox}
% \textit{\textcolor{red}{Refrain:} The Lord shall keep you from all evil.}
% \begin{verse}
%	\flagverse{1}I lift up my eyes to the hills; \Pause \\
%	from where is my help to come?
%
%	\flagverse{2}My help comes from the Lord, \Pause \\
%	the maker of heaven and earth.
%
%	\flagverse{3}He will not suffer your foot to stumble; \Pause \\
%	he who watches over you will not sleep.
%
%	\flagverse{4}Behold, he who keeps watch over Israel \Pause \\
%	shall neither slumber nor sleep. \Rx
%
%	\flagverse{5}The Lord himself watches over you; \Pause \\
%	the Lord is your shade at your right hand,
%
%	\flagverse{6}So that the sun shall not strike you by day, \Pause \\
%	neither the moon by night.
%
%	\flagverse{7}The Lord shall keep you from all evil; \Pause \\
%	it is he who shall keep your soul.
%
%	\flagverse{8}The Lord shall keep watch over your going out \\
%	\vin and your coming in, \Pause \\
%	from this time forth for evermore.
% \end{verse}
% \end{commandbox}
%
% \vspace*{1ex}
%
% \noindent\begin{resultbox}
% \setmainfont[Ligatures=TeX]{Gill Sans MT}
% \textit{\textcolor{red}{Refrain:} The Lord shall keep you from all evil.}
% \begin{verse}
%	\flagverse{1}I lift up my eyes to the hills; \Pause \\
%	from where is my help to come?
%
%	\flagverse{2}My help comes from the Lord,  \Pause \\
%	the maker of heaven and earth.
%
%	\flagverse{3}He will not suffer your foot to stumble;  \Pause \\
%	he who watches over you will not sleep.
%
%	\flagverse{4}Behold, he who keeps watch over Israel  \Pause \\
%	shall neither slumber nor sleep. \Rx
%
%	\flagverse{5}The Lord himself watches over you;  \Pause \\
%	the Lord is your shade at your right hand,
%
%	\flagverse{6}So that the sun shall not strike you by day,  \Pause \\
%	neither the moon by night.
%
%	\flagverse{7}The Lord shall keep you from all evil;  \Pause \\
%	it is he who shall keep your soul.
%
%	\flagverse{8}The Lord shall keep watch over your going out \\
%	\vin and your coming in,  \Pause \\
%	from this time forth for evermore.
% \end{verse}
% \end{resultbox}
%
% \subsubsection{Shortcuts}
% A number of shortcuts are provided for frequently used liturical elements.
% Two versions are provided for each shortcut. The standard version of each
% shortcut typesets the liturgical element as spoken the whole congregation,
% i.e. in bold print, preceded by \textit{All}. The starred version typesets
% the plain text, as shown below.
%
% \DescribeMacro{amen} Declaration of affirmation:
% {\setmainfont[Ligatures=TeX]{Gill Sans MT}\amen*}
%
% \DescribeMacro{glorybe} Lesser doxology:
%
% \vspace*{1ex}
%
% \noindent\begin{resultbox}
%	\setmainfont[Ligatures=TeX]{Gill Sans MT}
%   \glorybe*
% \end{resultbox}
%
% \DescribeMacro{lpc} The contemporary version of the Lord's prayer:
%
% \vspace*{1ex}
%
% \noindent\begin{resultbox}
% 	\setmainfont[Ligatures=TeX]{Gill Sans MT}
%   \lpc*
% \end{resultbox}
%
% \DescribeMacro{lpt} The traditional version of the Lord's prayer:
%
% \vspace*{1ex}
%
% \noindent\begin{resultbox}
%   \setmainfont[Ligatures=TeX]{Gill Sans MT}
%   \lpt*
% \end{resultbox}
%
% \subsection{Options}
% Default formatting options can be applied using
% |\usepackage|\oarg{options}|{literature-cw}|.
%
% \DescribeMacro{font} Sets the document font to Gill Sans MT. If this
% typeface is not available on the system, the package will attempt to use
% Lato as an alternative. An error will result with this option if neither
% of these typefaces are available on the system.
%
% \DescribeMacro{titles} Formats chapter, section, and subsection titles in
% line with the Common Worship design. Note that as chapters, sections, etc.
% are not numbered in Common Worship, it is best to use the starred versions
% of title commands, i.e |\chapter*|, |\section*|, etc.
%
% \StopEventually{%
% \begin{thebibliography}{1}
%	\bibitem{commonworship}
%		\textsc{Church of England}.
%		\newblock \textit{Common Worship: Services and Prayers for the Church of England}.
%		\newblock Church House Publishing, London, 2000.
% 	\bibitem{morgan}
%		\textsc{John Morgan}.
%		\newblock \textit{An account of the making of Common worship: services and prayers for the Church of England} (Typography papers 5).
%		\newblock Department of Typography \& Graphic Communication, University of Reading, 2022/2003.
% \end{thebibliography}
% }
%    \begin{macrocode}
\RequirePackage{array}
\RequirePackage{bibleref}
\RequirePackage{booktabs}
\RequirePackage{calc}
\RequirePackage{fontspec}
\RequirePackage{geometry}
\RequirePackage{framed}
\RequirePackage[pagestyles]{titlesec}
\RequirePackage{xcolor}

%% The typeface used in Common Worship is Monotype Gill Sans.
\DeclareOption{font}{
	\IfFontExistsTF{Gill Sans MT}{
		\setmainfont[Ligatures=TeX]{Gill Sans MT}
	}{
		\PackageWarning{liturgy-cw}{%
			The Gill Sans MT typeface is not available on this system; falling back to Lato.}
		\setmainfont[Ligatures=TeX]{Lato}
	}
}
\DeclareOption{titles}{
	\ifdefined\chapter
		\newlength{\@cwpw}
		\settowidth{\@cwpw}{\huge\P}
		\titleformat{\chapter}[display]
		{\huge\color{red}}{\thechapter}{0pt}{\hspace*{\dimexpr-\@cwpw+10mm}\P\hspace*{5mm}\itshape\begin{minipage}[t]{\linewidth-15mm}}[\end{minipage}]
		\titlespacing{\chapter}{0pt}{1\baselineskip plus .333\baselineskip minus .125\baselineskip}{0pt plus .125\baselineskip minus .125\baselineskip}
	
		\let\@cwchapter\chapter
		\renewcommand{\chapter}{\@ifstar{\@cwchapter*}{\@cwchapter*}}
	\else
		\PackageWarning{liturgy-cw}{chapter not defined; skipping.}
	\fi
	\newcommand{\@cwpadright}[1]{#1\hspace*{15mm}}
	\titleformat{\section}
	{\large\raggedleft\bfseries}{\thesection}{0pt}{\@cwpadright}
	\titlespacing{\section}{0pt}{1\baselineskip plus .333\baselineskip minus .125\baselineskip}{0pt plus .125\baselineskip minus .125\baselineskip}

	\titleformat{\subsection}
	{\large\raggedleft\color{red}\itshape}{\thesubsection}{0pt}{\@cwpadright}
	\titlespacing{\subsection}{0pt}{1\baselineskip plus .333\baselineskip minus .125\baselineskip}{0pt plus .125\baselineskip minus .125\baselineskip}

	\titleformat{\subsubsection}
	{\bfseries}{\thesubsubsection}{0pt}{}
	\titlespacing{\subsubsection}{15mm}{1\baselineskip plus .333\baselineskip minus .125\baselineskip}{0pt plus .125\baselineskip minus .125\baselineskip}

	\let\@cwsection\section
	\renewcommand{\section}{\@ifstar{\@cwsection*}{\@cwsection*}}

	\let\@cwsubsection\subsection
	\renewcommand{\subsection}{\@ifstar{\@cwsubsection*}{\@cwsubsection*}}

	\let\@cwsubsubsection\subsubsection
	\renewcommand{\subsubsection}{\@ifstar{\@cwsubsubsection*}{\@cwsubsubsection*}}

	\newpagestyle{cwstyle}{\setfoot[\thepage][][]{}{}{\thepage}}
	\pagestyle{cwstyle}
}
\ProcessOptions\relax

\NewDocumentCommand{\all}{sm}{%
	\ignorespaces\noindent\strut\IfBooleanTF#1%
	{\hspace*{15mm}}%
	{\parbox[t]{10mm}%
		{\raggedleft\normalfont\textcolor{red}{\textit{All}}}%
		\hspace*{5mm}}%
	\begin{minipage}[t]{\dimexpr\linewidth-15mm}%
		\bfseries #2
\end{minipage}\ignorespacesafterend}

\def\changemargin#1#2{\list{}{\topsep0pt\parskip0pt\partopsep0pt\parsep0pt\itemsep0pt\rightmargin#2\leftmargin#1}\item[]}
\let\endchangemargin=\endlist

\newcommand{\attrib}[2][15mm]{%
	\nopagebreak{\raggedleft\itshape #2\hspace*{#1}\par}}

\NewDocumentCommand{\minister}{O{}m}{%
	\ignorespaces\noindent\begin{changemargin}{-15mm}{0mm}\item[]%
	\parbox[t]{25mm}%
		{\raggedleft\normalfont\textcolor{red}{\textit{#1}}}%
		\hspace*{5mm}%
	\begin{minipage}[t]{\dimexpr\linewidth-15mm}%
		\normalfont #2
\end{minipage}\strut\end{changemargin}\ignorespacesafterend}

\NewDocumentCommand{\rubric}{m}{%
	\noindent\hspace*{15mm}\begin{minipage}[t]{\dimexpr\linewidth-15mm}%
		{\textcolor{red}{\itshape #1}}
\end{minipage}\strut}

\NewDocumentCommand{\alternative}{}{\rubric{\textit{(or)}}}
%% Shortcuts
\NewDocumentCommand{\amen}{s}{%
	\IfBooleanTF#1%
	{Amen.}%
	{\all{\amen*}}%
}
\NewDocumentCommand{\glorybe}{s}{%
	\IfBooleanTF#1%
	{Glory to the Father and to the Son\\
	and to the Holy Spirit;\\
	as it was in the beginning is now\\
	and shall be for ever. \amen*}%
	{\all{\glorybe*}}%
}
\NewDocumentCommand{\lpt}{s}{%
	\IfBooleanTF#1%
	{Our Father, who art in heaven,\\
	hallowed be thy name;\\
	thy kingdom come;\\
	thy will be done;\\
	on earth as it is in heaven.\\
	Give us this day our daily bread.\\
	And forgive us our trespasses,\\
	as we forgive those who trespass against us.\\
	And lead us not into temptation;\\
	but deliver us from evil.\\
	For thine is the kingdom,\\
	the power and the glory,\\
	for ever and ever.\\
	\amen*}%
	{\all{\lpt*}}%
}
\NewDocumentCommand{\lpc}{s}{%
	\IfBooleanTF#1%
	{Our Father in heaven,\\
	hallowed be your name,\\
	your kingdom come,\\
	your will be done,\\
	on earth as in heaven.\\
	Give us today our daily bread.\\
	Forgive us our sins\\
	as we forgive those who sin against us.\\
	Lead us not into temptation\\
	but deliver us from evil.\\
	For the kingdom, the power,\\
	and the glory are yours\\
	now and for ever.\\
	\amen*}%
	{\all{\lpc*}}%
}

\newlength{\vbarhskip}
\setlength\vbarhskip{-51pt}
\newenvironment{required}{%
	\def\FrameCommand{%
		{\hskip\vbarhskip\color{red}\vrule width 3pt}%
		\fboxsep=\FrameSep\hspace{\dimexpr\vbarhskip*-1-3pt}%
	}%
	\MakeFramed{\advance\hsize-\width\FrameRestore}%
}
{\endMakeFramed}

\IfFontExistsTF{Noto Sans Liturgy}{%
	\newfontface{\nsl}{Noto Sans Liturgy}
	\NewDocumentCommand{\Rx}{}{{\nsl\itshape\textcolor{red}{\char"211E}}}
	\NewDocumentCommand{\Pause}{}{{\nsl\textcolor{red}{\char"2666}}}
	\NewDocumentCommand{\Cross}{}{{\nsl\textcolor{red}{\char"2720}}}
}{
	\PackageWarning{liturgy-cw}{%
		The Noto Sans Liturgy typeface is not available on this system which
		means liturgical symbols are not defined.}
}

\newbiblerefstyle{cw}{ % Common Worship reference style
	\biblerefstyle{default}
	\renewcommand{\BRchvsep}{.}
	\renewcommand{\BRvrsep}{--}
}

\biblerefstyle{cw}

\setlength{\parindent}{0pt}
%    \end{macrocode}
%
% \Finale
\endinput
