%\iffalse
%
% file: monthly.dtx
% Copyright (C) 1996-1997 by Frank Bennett.  All rights reserved.
%
% IMPORTANT NOTICE:
% 
% You are not allowed to change this file.  You may however copy
% this file to a file with a different name and then change the
% copy if (a) you do not charge for the modified code, (b) you
% acknowledge the author of this file in the new file, if it
% is distributed to others, and (c) you attach these same
% conditions to the new file.
% 
% You are not allowed to distribute this file alone.  You are not
% allowed to take money for the distribution or use of this file
% (or a changed version) except for a nominal charge for copying
% etc.
% 
% You are allowed to distribute this file under the condition that
% it is distributed with all of its contents, intact.
% 
% For error reports, or offers to help make this a more powerful,
% friendlier, and altogether more thrilling package, please contact me on
% fb@soas.ac.uk.  Better yet, make a contribution to
% my pension.  Account details available on request.
%
%\fi
%
% \def\fileversion{3.1.1.1}
% \def\filedate{1998/01/17 18:15:10}
%
% \iffalse
%
%<*driver>
\documentclass{ltxdoc}
%\CodelineIndex
%\EnableCrossrefs
\begin{document}
\OnlyDescription    % Comment out for implementation details
\DocInput{monthly.dtx}
\end{document}
%</driver>
%\fi
%
% \changes{v1.0}{1996/02/15}{Initial Version}
% \changes{v1.0}{1997/02/17}{Style now stable enough to
%  justify recording changes.  Added protection for definition
%  of cal@entry@text.  It is now possible to put a pound
%  sign in front of entries.  A change to the
%  dates module allows entries on the same day each
%  month.  So we can now record standing charges against
%  bank accounts.  Oh joy.}
% \changes{v1.0}{1997/02/20}{First release version.
%   Merged all code into the class file.
%   Thanks to Robin Fairbairns for
%   his help on the vertical sizing of
%   tables, and to David Carlisle for pointing out
%   the \texttt{protected@edef} command.}
% \changes{v1.1}{1997/03/01}{Modified stacking method
%   for items to permit line wrapping.  Code could be more
%   concise and clear, but it runs well enough.}
% \changes{v1.1}{1997/03/01}{Adapted documentation from
%  the Timetable package.}
% \changes{v1.2}{1997/05/12}{Page formatting is now handled
%  through the \texttt{geometry} package.  Landscaping can
%  also handled through \texttt{geometry}, as suggested by
%  Christian Horn.  Faulty linespacing has now been fixed.
%  Documentation has also been improved.}
% \changes{v1.3}{1997/05/19}{Support is now provided for Japanese
%  as well as English calendars.  There is no support for Imperial
%  dates; users have to put these in by hand using the \texttt{title}
%  option, if Imperial dates are required.  The changes principally
%  affected the Dates package.}
% \changes{v1.3}{1997/05/20}{Fixed bug in \texttt{firstday} option.
%  Labels were not shifting with the calendar start day.
%  Thanks due to Miroslav Novak for reporting this bug.}
% \changes{v1.3}{1997/05/20}{Added German support.  Thanks due to
%  Christian Horn.}
% \changes{v1.4}{1997/05/29}{Amended documentation, including
%  a description of a proper set of options for handling page
%  orientation and layout.  Still need to do the coding, though.}
% \changes{v1.4}{1997/05/29}{Completed coding of orientation
%  and layout options.  Pretty simple, once the strategy was
%  clear.}
% \changes{v1.4}{1997/05/30}{Added linklandscape option and
%  added documentation on the new method of handling format
%  and orientation.}
% \changes{v1.4}{1997/06/02}{Tidied up the way rotation and
%  page layout options are reconciled.}
% \changes{v1.4}{1997/06/02}{Added \cs{noindent} to suppress
%  indentation internally.}
% \changes{v1.4}{1997/06/03}{Generalized code for setting page
%  sizes and adjusting typefaces.  Also added automatic selection
%  of abbreviated day names for narrow calendars.  Ready for
%  final release.}
% \changes{v1.4}{1997/06/06}{Major rearrangements were needed to
%  fix problems with page formatting and rotation, reported by
%  Christian Horn.}
% \changes{v1.5}{1997/06/09}{Further improvements to calendar
%  sizing.  An over-wide margin with B4 paper continues to cause
%  problems.  Could Japanese B4 paper be a different size?}
% \changes{v1.6}{1997/10/18}{New public release of all styles
%  and modules incorporating global bug-fixes.}
%
%\iffalse
% Source tree moved under RCS
% ---------------------------
% monthly.dtx,v
% Revision 3.1.1.1  1998/01/17 18:15:10  root
% Release code, checksum verified
%
% Revision 3.1  1998/01/17 17:14:10  root
% Release code
%
% Revision 2.8  1998/01/17 15:36:01  root
% *** empty log message ***
%
% Revision 2.7  1998/01/17 15:34:30  root
% *** empty log message ***
%
% Revision 2.6  1998/01/17 11:31:00  root
% *** empty log message ***
%
% Revision 2.5  1998/01/16 17:05:53  root
% *** empty log message ***
%
% Revision 2.4  1998/01/16 09:14:25  root
% *** empty log message ***
%
% Revision 2.3  1998/01/16 08:04:38  root
% *** empty log message ***
%
% Revision 2.2  1997/11/07 10:44:05  root
% Release code.
%
% Revision 2.1.1.1  1997/11/04 05:06:07  root
% Development branch
%
% Revision 2.1  1997/11/01 14:56:45  bennett
% Release code.
%
% Revision 1.1  1997/10/29 08:17:52  root
% Initial revision
%
%\fi
%
% \title{User's Guide to the Monthly Calendar 
%    package\thanks{This file is version number
%    \fileversion{}.  It was last revised on
%    \filedate{}.}}
%
% \author{Frank G. Bennett, Jr.}
%
% \maketitle
%
% \setcounter{StandardModuleDepth}{1}
% \DeleteShortVerb{\|}
% \MakeShortVerb{\"}
%
% \CheckSum{381}
%
% Please see the file \texttt{calguide.tex} for documentation
% on this calendar style.
%
% \StopEventually{\PrintIndex}
%
% \section{The Class File}
%
% \subsection{Preliminaries}
%
% Use \LaTeXe, and tell the user who we are.
%
%    \begin{macrocode}
%<*class>
\NeedsTeXFormat{LaTeX2e}[1995/06/01]
\ProvidesPackage{monthly}
          [1998/01/17 18:15:10 3.1.1.1 Monthly Calendar (Frank Bennett)]
%    \end{macrocode}
% Define some more variables and switches for use in the body
% of the style.
%    \begin{macrocode}
\newcount\mon@tempcount
\newcount\mon@firstday
\newtoks\mon@tempreg@b
\newlength\mon@tablewidth
\newtoks\mon@tempreg@a
\newlength{\mon@vh}
\newlength{\mon@hw}
\newlength{\mon@templen}
\newlength{\mon@heightlessboxes}
\newlength{\mon@widthlessboxes}
\newif\ifmon@explicittitle
\newif\ifmon@shortdays
\newlength{\mon@oddsidemargin}
\newlength{\mon@evensidemargin}
\newlength{\mon@topmargin}
\newif\ifmon@usedgeometry
\newlength{\mon@adjustment}
\newlength{\mon@textwidth}
\newlength{\mon@textheight}
%    \end{macrocode}
%
% \subsection{Options}
%
% In common with all other packages in the Calendar
% bundle, we include options for all of the drivers supported
% by the color package and the graphics package, and
% include some code for declaring languages.
%
%    \begin{macrocode}
\input calopts.cfg
\InputIfFileExists{dates.cfg}{}{}
\ProcessOptions
%    \end{macrocode}
%
% \subsection{Option Postprocessing}
%
% Now that the options lists are available to us,
% we can start loading packages.  The following packages
% are always loaded, and always with the same options.
%    \begin{macrocode}
\RequirePackage{array}[1996/06/14]
\RequirePackage{calendar}
%    \end{macrocode}
% Next, we set the dimensions
% of the table that will contain the calendar.
%    \begin{macrocode}
  \def\mon@settableheight{%
    \setlength{\mon@vh}{\vsize}%
    \addtolength{\mon@vh}{-0.001pt}}%
  \def\mon@settablewidth{%
    \setlength{\mon@tablewidth}{\hsize}}%
%    \end{macrocode}
% Typefaces, and abbreviated day names versus full day names
% are selected by options.
%    \begin{macrocode}

%    \end{macrocode}
%
% \subsection{Calendar Setup}
%
% Define some key values for use by the calendar environment.
%    \begin{macrocode}
\define@key{opt}{title}{%
  \def\mon@title{#1}\mon@explicittitletrue}
\define@key{opt}{labels}{%
  \@storelabels#1,,{}}
\define@key{opt}{shortdays}{%
  \mon@shortdaystrue}
\define@key{opt}{firstday}{%
  \mon@firstday=#1}
\define@key{opt}{titletype}{\def\mon@titletype{#1}}
\define@key{opt}{daynametype}{\def\mon@nametype{#1}}
\define@key{opt}{datetype}{\def\mon@datetype{#1}}
\define@key{opt}{texttype}{\def\mon@texttype{#1}}
\mon@firstday=0%
%    \end{macrocode}
% Define a utility macro for use by the \texttt{labels} key.
% This stores a comma-delimited list of labels to a single
% macro, which can then be used as a line in the table
% environment that makes up the calendar.
%    \begin{macrocode}
\def\@storelabels{%
  \def\mon@labeltext{}%
  \@@storelabels}
\def\@@storelabels#1,#2#{%
  \ifcat$#1$%
    \mon@tempreg@a=\expandafter\expandafter\expandafter{%
      \expandafter\@gobble\mon@labeltext}%
    \protected@edef\mon@labeltext{\the\mon@tempreg@a}%
    \let\next\@gobble%
  \else%
    \let\next\@@storelabels%
    \mon@tempreg@a=\expandafter{\mon@labeltext}%
    \protected@edef\mon@labeltext{\the\mon@tempreg@a&#1}%
  \fi%
    \next#2{}}
%    \end{macrocode}
% Define default values for the environment keys.  The title is set
% inside the scope of \cs{newcalendar}
%    \begin{macrocode}
\def\mon@theweekday{\ifmon@shortdays\theshortweekday\else%
  \theweekday\fi}%
\def\mon@titletype{\large\bfseries}%
\def\mon@daynametype{\bfseries}%
\def\mon@datetype{\bfseries}%
\def\mon@texttype{\tiny}%
\mon@firstday=0%
%    \end{macrocode}
%
% \subsection{Calendar Definition}
%
% The calendar definition itself consists of a single command
% and its associated arguments.
%    \begin{macrocode}
\newcalendar%
%    \end{macrocode}
% We give the environment a name.
%    \begin{macrocode}
{monthly}
%    \end{macrocode}
% We're producing DVI, so we use the standard
% macro for dropping text into the hopper.
%    \begin{macrocode}
{\cal@insert}
%    \end{macrocode}
% We start the calendar on Sunday.  Season to taste.
% Use integers "0" through "6".
%    \begin{macrocode}
{\mon@firstday}
%    \end{macrocode}
% The length of a group is the length of the month.
%    \begin{macrocode}
{\dates@days}
%    \end{macrocode}
% The length of a subgroup is one week.
%    \begin{macrocode}
{7}
%    \end{macrocode}
% The delimiter to go before every item but the first
% is a table separator.
%    \begin{macrocode}
{&}
%    \end{macrocode}
% Increment the date counter with every item.
%    \begin{macrocode}
{I}
%    \end{macrocode}
% Define the header.  The macro inserted here is defined
% below, in the block of code run after the date is read,
% and before the table is opened.
%    \begin{macrocode}
{\mon@header}
%    \end{macrocode}
% We define the end of the calendar.
%    \begin{macrocode}
{\\\hline\end{tabular}}
%    \end{macrocode}
% We define a way to open a new group.
%    \begin{macrocode}
{\cal@footer\newpage\cal@header}
%    \end{macrocode}
% We define the way to end a subgroup.  In this case,
% we want to start a new line and lay down a rule.
%    \begin{macrocode}
{\\\hline}
%    \end{macrocode}
% We set up to require a month in the environment typed
% into the user's document.
%    \begin{macrocode}
{\dates@requiremonthtrue}
%    \end{macrocode}
% The next section runs after the calendar dates have been
% scanned from the environment, but before the table is
% opened.
% \textit{Whatever month was (last) indicated in the environment
% section of the calendar, we want to start on the first
% day, and make a calendar for one month only.  We accomplish
% this by adjusting the human-side date, and pushing the
% result through to the long date with \texttt{dates@fix}.
% We then readjust the human side data with \texttt{caldate}.
% This puts us on the first day of the month, and we
% set the counter \texttt{cal@range@start} to this long date.}
%
%    \begin{macrocode}
{\dates@day=1%
  \dates@fix%
  \caldate%
  \cal@range@start\dates@date%
%    \end{macrocode}
% \textit{We tell the user where we're starting.}
%    \begin{macrocode}
  \message{^^JStart: \theshortweekday\space%
    \theday\space\theshortmonth\space\theyear}%
%    \end{macrocode}
% \textit{Then we jump to the last day of the last
% month in the range given by the user, and
% do the same readjustment with \texttt{dates@fix} and
% \texttt{caldate}.  We set the end of the range to this
% value.}
%    \begin{macrocode}
  \dates@date\cal@range@end%
  \caldate%
  \dates@day\dates@days%
  \dates@fix%
  \caldate%
  \cal@range@end\dates@date%
%    \end{macrocode}
% \textit{We let the user know where we plan to stop.}
%    \begin{macrocode}
  \message{^^JEnd: \theshortweekday\space%
    \theday\space\theshortmonth\space\theyear}%
%    \end{macrocode}
% \textit{We jump back to the starting date and reset the
% human-side values.}
%    \begin{macrocode}
  \dates@date=\cal@range@start%
  \caldate%
%    \end{macrocode}
% \textit{Now that the date adjustments have been taken care of,
% it's time to figure out the size of the calendar.  We start by
% getting some initial sizes from the height and width of the
% text area of the current page.}
%    \begin{macrocode}
  \mon@settablewidth%
  \mon@settableheight%
%    \end{macrocode}
% \textit{We add \cs{extrarowheight} to prim up the headings a little.
% We will still get text touching lines with \cs{Huge} typefaces,
% but this is better than nothing.}
%    \begin{macrocode}
%  \setlength{\extrarowheight}{3pt}%
%    \end{macrocode}
% \textit{Fetch the full height of the title to heightlessboxes.}
%    \begin{macrocode}
  \settoheight{\mon@templen}{\vbox{\boxmaxdepth=0pt%
    \begin{tabular}{c}%
      \mon@titletype%
      \the\dates@monthname%
    \end{tabular}}}%
  \setlength{\mon@heightlessboxes}{\mon@templen}%
%    \end{macrocode}
% \textit{Fetch the full height of the day labels to heightlessboxes.}
%    \begin{macrocode}
  \settoheight{\mon@templen}{\vbox{\boxmaxdepth=0pt%
    \begin{tabular}{c}%
      \mon@daynametype%
      Monday%
    \end{tabular}}}%
  \addtolength{\mon@heightlessboxes}{\mon@templen}%
%    \end{macrocode}
% \textit{Add 10 $\times$ the rule width and the space between double-rules
% to heightlessboxes.}
%    \begin{macrocode}
  \addtolength{\mon@heightlessboxes}{10\arrayrulewidth}%
  \addtolength{\mon@heightlessboxes}{\doublerulesep}%
%    \end{macrocode}
% \textit{Finally, we make some adjustments that I arrived
% at by trial and error; I have no idea why these particular
% measurements work.  If anyone wants to improve this, be my
% guest.  I have run out of patience on this one.}
%    \begin{macrocode}
   \addtolength{\mon@heightlessboxes}{\mon@adjustment}%
   \addtolength{\mon@heightlessboxes}{2pt}%
%    \end{macrocode}
% \textit{The height of the boxes is the full width of
% the text minus heightlessboxes.  Divide
% by 6 for the height of an individual box.}
%    \begin{macrocode}
  \addtolength{\mon@vh}{-\mon@heightlessboxes}%
  \divide\mon@vh by6%
%    \end{macrocode}
% \textit{The width is simpler.  We have 8 rules, and
% a total of 14 gaps between text and rules.
% Add it all up, subtract the total from textheight,
% divide by 7 and Thunderbirds are Go.}
%    \begin{macrocode}
  \setlength{\mon@hw}{\mon@tablewidth}%
  \setlength{\mon@widthlessboxes}{8\arrayrulewidth}%
  \addtolength{\mon@hw}{-\mon@widthlessboxes}%
  \addtolength{\mon@hw}{-14\tabcolsep}%
  \divide\mon@hw by7\relax%
%    \end{macrocode}
% \textit{Well, almost go.  We need to build the header macro.
% Our first step is to create the default labels,
% if the user has not specified any.}
%    \begin{macrocode}
  \@ifundefined{mon@labeltext}{%
    \def\mon@defaultlabels{}%
    \mon@tempcount=1%
    \advance\dates@date by-\dates@weekd%
    \advance\dates@date by\mon@firstday%
    \caldate%
    \loop%
      \mon@tempreg@a=\expandafter{\mon@defaultlabels}%
      \edef\mon@defaultlabels{\the\mon@tempreg@a\mon@theweekday,}%
    \ifnum\mon@tempcount<7%
     \advance\dates@date by1%
     \caldate%
     \advance\mon@tempcount by 1\relax%
    \repeat%
    \expandafter\@storelabels\mon@defaultlabels,,{}%
    \dates@date\cal@range@start%
    \caldate}%
    {}%
%    \end{macrocode}
% \textit{Then we define the header itself.}
%    \begin{macrocode}
  \def\mon@settitle{\ifmon@explicittitle\else%
     \def\mon@title{\themonth\space\theyear}\fi}%
  \protected@edef\mon@header{%
    \noexpand\mon@settitle%
    \noexpand\begin{tabular}{|c|c|c|c|c|c|c|}%
    \noexpand\hline%
      \noexpand\multicolumn{7}{|c|}{\mon@titletype%
        \noexpand\mon@title}\noexpand\\%
    \noexpand\hline\noexpand\hline%
      \mon@labeltext\noexpand\\%
    \noexpand\hline}}%
%    \end{macrocode}
% Events are delimited by \cs{newline}.
%    \begin{macrocode}
{\par}%
%    \end{macrocode}
% The next argument is dropped on DVI for each item.
% We use it here to fetch the text of appointments and
% events to the standard list macros, using the
% standard text-fetching commands.  Each generates a
% stack delimited by the phrase that was set above.
%    \begin{macrocode}
{\cal@get@appointments\cal@get@events}%
%    \end{macrocode}
% This sets \cs{cal@entry@text}.  It is executed for every
% event or appointment in the database that applies
% to a given item.  We differentiate between whole-day events,
% pinpointed events and ranged events.
%    \begin{macrocode}
{\relax%
   \ifnum\dates@time@start>0\relax%
    \ifnum\dates@time@start=\dates@time@end%
      \protected@edef\cal@entry@text{%
        \thetimestart\space%
        \cal@textcolor{\calcolor}{\theshorttext}}%
    \else%
      \protected@edef\cal@entry@text{%
        \thetimestart--\thetimeend\space%
        \cal@textcolor{\calcolor}{\theshorttext}}%
    \fi%
  \else%
      \protected@edef\cal@entry@text{%
        \cal@textcolor{\calcolor}{\theshorttext}}%
  \fi}%
%    \end{macrocode}
% Finally we tell the style how to create the
% text for each item (which is an individual date
% box in this case).  We use \texttt{vbox} to make
% the height static.
% Thanks are due to Christian Horn for forwarding a
% handwritten sample of a tabular environment, demonstrating
% that an error in the linespacing was \textit{not} produced
% by \LaTeXe's internals.  In the end, the problem turned out
% to be due to grouping; \LaTeX{}, I now remember, sets
% linespacing according to the typeface in force at the end
% of a paragraph or parbox.
%    \begin{macrocode}
{\vbox to\mon@vh{%
    \vskip\medskipamount%
    \hbox{\parbox{\mon@hw}{\mon@datetype\theday}}%
       \ifcat$\theevents$%
       \else%
        \vskip\smallskipamount%
        \hbox{\parbox{\mon@hw}{\raggedright%
          \mon@texttype\theevents}}%
       \fi%
       \ifcat$\theappointments$%
       \else%
        \vskip\smallskipamount%
        \hbox{\parbox{\mon@hw}{\raggedright%
          \mon@texttype\theappointments}}%
       \fi%
 \vfil}}%
%</class>
%    \end{macrocode}
% \Finale \PrintChanges