%\iffalse
% ====================================================================
%  @LaTeX-file{
%     filename        = "acmtrans.dtx",
%     version         = "1.5 beta",
%     date            = "30 October 1997",
%     time            = "12:53:50 EST",
%     author          = "David M. Jones",
%     address         = "MIT Laboratory for Computer Science
%                        Room NE43-316
%                        545 Technology Square
%                        Cambridge, MA 02139
%                        USA",
%     telephone       = "(617) 253-5936",
%     FAX             = "(617) 253-3480",
%     checksum        = "34285 2458 7196 58471",
%     email           = "dmjones@theory.lcs.mit.edu",
%     codetable       = "ISO/ASCII",
%     keywords        = "",
%     supported       = "yes",
%     docstring       = "The checksum field above contains a CRC-16
%                        checksum as the first value, followed by the
%                        equivalent of the standard UNIX wc (word
%                        count) utility output of lines, words, and
%                        characters.  This is produced by Robert
%                        Solovay's checksum utility.",
%  }
% ====================================================================
%\fi
%
%\iffalse
%<*driver>
\documentclass[draft]{ltxdoc}
\newcommand*{\Lopt}[1]{\textsf {#1}}
\newcommand*{\Lenv}[1]{\texttt {#1}}
\newcommand*{\cls}[1]{\texttt {#1}}
\newcommand*{\file}[1]{\texttt {#1}}
\newcommand*{\fnc}[1]{\texttt {#1}}
\newcommand*{\bst}[1]{\texttt {#1}}
\newcommand*{\fld}[1]{\texttt {#1}}
\newcommand*{\typ}[1]{\texttt {#1}}
\makeatletter
\def\function{%
    \def\SpecialMainIndex##1{%
        \@bsphack
        \special@index{%
            ##1\actualchar
            \string\verb
            \quotechar*\verbatimchar
            \string##1\verbatimchar
            \encapchar main%
        }%
        \@esphack
    }%
    \begingroup
        \catcode`\\12
        \MakePrivateLetters
        \m@cro@ {\csname iftrue\endcsname}%
}
\let\endfunction \endtrivlist
\makeatother
\CodelineIndex
\setcounter{IndexColumns}{2}
\begin{document}
\DocInput{acmtrans.dtx}
\PrintIndex
% ^^A\PrintChanges
\end{document}
%</driver>
%\fi
%
%    \title{The ACM Transactions (\cls{acmtrans}) bibliography style}
%
%    \author{Glenn Paulley \and Andrew W. Appel \and Rebecca L. Davies
%    \and David M. Jones}
%
%    \date{30 October 1997 \\
%    Version 1.5 $\beta$}
%
%    \begin{figure}[!b]
%
%    \footnoterule
%
%    \noindent \copyright 1995, all rights reserved.  Copying of this
%    file is authorized only if either (1) you make absolutely no
%    changes to your copy, including name, or (2) if you do make
%    changes, you name it something other than ``acmtrans.bst''.
%    There are undoubtably bugs in this style.  Please send any bug
%    reports, suggestions for improvements, etc., to
%    \texttt{jacm@theory.lcs.mit.edu}.
%
%    \end{figure}
%
%    \maketitle
%
%    \tableofcontents
%
%
%    \section{Introduction}
%
%    This is the \bst{acmtrans} \BibTeX\ style, which is based Glenn
%    Paulley's \bst{chicago} bibliography style (for further
%    information about the history of this code, please see the
%    comments in \file{chicago.bst}).  The \bst{acmtrans} style was
%    hacked by Andrew W. Appel and Rebecca L. Davies of Princeton
%    University, and further modified and reworked by David M. Jones.
%
%    This style implements an ``author-year'' bibliography format,
%    where in-text citations have the general form ``[author name(s)
%    year]'' and the Reference list is sorted alphabetically by the
%    author or whatever passes for author in the absence of one.  The
%    style also supports abbreviated author lists in citations.
%
%    This style must be supplemented by some \TeX\ macros to process
%    the citation information generated by \bst{acmtrans}.  The
%    citation information deposited in the bibliography has the
%    following format:
%    \begin{quote}
%    \cs{citeauthoryear}\ignorespaces
%    \verb+{+\meta{full-author-info}\verb+}+\ignorespaces
%    \verb+{+\meta{abbrev-author-info}\verb+}+\ignorespaces
%    \verb+{+\meta{year}\verb+}+
%    \end{quote}
%
%    The \LaTeX\ style should define something like the following:
%    \begin{verbatim}
%    \let\@internalcite\cite
%    \def\fullcite{\def\citeauthoryear##1##2##3{##1 ##3}\@internalcite}
%    \def\fullciteA{\def\citeauthoryear##1##2##3{##1}\@internalcite}
%    \def\shortcite{\def\citeauthoryear##1##2##3{##2 ##3}\@internalcite}
%    \def\shortciteA{\def\citeauthoryear##1##2##3{##2}\@internalcite}
%    \def\citeyear{\def\citeauthoryear##1##2##3{##3}\@internalcite}\end{verbatim}
%    Additional commands to manipulate different components of a
%    citation can be defined so that, for example, you can list
%    author's names without parentheses if using a citation as a noun
%    or object in a sentence.  See the \cls{acmart} document class for
%    a more complete example.
%
%    In order to provide more flexibility for use with both
%    \LaTeX~2.09 and \LaTeXe, \bst{acmtrans} uses the following macros
%    for all font changes:
%    \begin{description}
%
%    \item[\cs{bibsc}]
%    for small caps
%
%    \item[\cs{bibemph}]
%    for italics without an italic correction
%
%    \item[\cs{bibemphic}]
%    for italics with an italic correction at the end.
%
%    \end{description}
%
%    For \LaTeXe, the following is appropriate:
%    \begin{verbatim}
%    \let\bibsc\textsc
%    \let\bibemph\emph
%    \let\bibemphic\emph\end{verbatim}
%
%    For \LaTeX~2.09, the following should do:
%    \begin{verbatim}
%    \newcommand{\bibsc}[1]{{\sc#1}}
%    \newcommand{\bibemph}[1]{{\em#1}}
%    \newcommand{\bibemphic}[1]{{\em#1\/}}\end{verbatim}
%
%    One further macro, \cs{bibyear}, is needed, to format the year.
%    The following definition is used in the \cls{acmart} class.  See
%    the comments before \fnc{output.year.check} below and before
%    \cs{bibyear} in \cls{acmart} for more information.
%    \begin{verbatim}
%    \newcommand{\bibyear}[2]{%
%       \unskip\quad\ignorespaces#1\unskip
%       \if#2. .\quad \else \quad#2 \fi
%    }\end{verbatim}
%
%    These \TeX\ macro definitions are found in the \cls{acmart}
%    document class.
%
%
%   \section{Further features of \bst{acmtrans}}
%
%    \begin{itemize}
%
%    \item
%    full names used in citations, but abbreviated citations are
%    available (see above)
%
%    \item
%    if an entry has a ``month'', then the month and year are also
%    printed as part of that bibitem.
%
%    \item
%    all conjunctions use ``and'' instead of ``\&''
%
%    \item
%    major modification from Chicago Manual of Style (13th ed.) is
%    that only the first author in a reference appears last name
%    first- additional authors appear as J. Q. Public.
%
%    \item
%    pages are listed as ``pp. xx-xx'' in all entry types except
%    article entries.
%
%    \item
%    book, inbook, and manual use ``location: publisher'' (or
%    organization) for address and publisher. All other types list
%    publishers separately.
%
%    \item
%    ``pp.'' are used to identify page numbers for all entry types
%    except articles.
%
%    \item
%    organization is used as a citation label if neither author nor
%    editor is present (for manuals).
%
%    \item
%    ``et al.'' is used for long author and editor lists, or when
%    ``others'' is used.
%
%    \end{itemize}
%
%
%    \section{Modifications and bug fixes from newapa.bst}
%
%    \begin{itemize}
%
%    \item
%    added month, year to bib entries if month is present
%
%    \item
%    fixed bug with In proceedings, added necessary comma after title
%
%    \item
%    all conjunctions changed to ``and'' from ``\&''
%
%    \item
%    fixed bug with author labels in my.full.label: ``et al.'' now is
%    generated when ``others'' is an author name
%
%    \item
%    major modification from Chicago Manual of Style (13th ed.) is
%    that only the first author in a reference appears last name
%    first---additional authors appear as J. Q. Public.
%
%    \item
%    pages are listed as ``pp. xx-xx'' in all entry types except
%    article entries. Unnecessary (IMHO) ``()'' around page numbers
%    were removed, and page numbers now don't end with a period.
%
%    \item
%    created chicago.sty for use with this bibstyle (required).
%
%    \item
%    fixed bugs in \fnc{format.vol.num.pages} for missing volume,
%    number, and/or pages. Renamed to \fnc{format.jour.vol}.
%
%    \item
%    fixed bug in formatting booktitles: additional period an error if
%    book has a volume.
%
%    \item
%    fixed bug: editors usually given redundant period before next
%    clause (\fnc{format.editors.dot}) removed.
%
%    \item
%    added label support for organizations, if both author and editor
%    are missing (from alpha.bst). If organization is too long, then
%    the key field is used for abbreviated citations.
%
%    \item
%    In proceedings or books of several volumes, no comma was written
%    between the ``Volume x'' and the page numbers (this was
%    intentional in newapa.bst). Fixed.
%
%    \item
%    Some journals may not have volumes/numbers, only month/year (eg.
%    IEEE Computer). Fixed bug in article style that assumed
%    volume/number was always present.
%
%    \end{itemize}
%
%
%    \section{To-do list}
%
%    Here are a few of the many things that need to be done to finish
%    this style file.
%    \begin{itemize}
%
%    \item
%    Figure out what, if any, copyright notice should cover this file.
%
%    \item
%    Decide what parts of the above documentation to keep.  Sort out
%    credit issues.
%
%    \item
%    Get style rules from ACM and implement them.
%
%    \item
%    Implement \fnc{format.proc.publisher} and sort out other issues
%    mentioned in connection with \fnc{inproceedings}.
%
%    \item
%    Sort out \fnc{format.key} business.
%
%    \item
%    Munge \fld{month} to standardize month abbreviations when authors
%    fail to use the standard strings.
%
%    \item
%    Sort out journal abbreviations.
%
%    \item
%    Make \texttt{.bbl} file look more like what the ACM wants for
%    its automatic conversion programs.  (In fact, we could output
%    SGML code directly for them, if they'd tell us what they want.)
%
%    \item
%    Rewrite \Lenv{macrocode} to index BST macros.
%
%    \end{itemize}
%
%
%    \section{The \texttt{docstrip} modules}
%
%    This code uses the following modules:
%    \begin{center}
%    \begin{tabular}{ll}
%
%    xref   & use crossrefs in the bibliography \\
%
%    driver & generate a driver file for the documentation
%
%    \end{tabular}
%    \end{center}
%
%
%    \section{The macros}
%
%    \subsection{The field names and useful utilities}
%
%    \begin{macrocode}
ENTRY
  { address
    author
    booktitle
    chapter
    edition
    editor
    howpublished
    institution
    journal
    key
    month
    note
    number
    organization
    pages
    publisher
%    \end{macrocode}
%    In addition to the standard fields, we introduce three new
%    fields, \fld{pub-year}, \fld{pub-month} and \fld{pub-address}.
%    These are for use in proceedings, where the \fld{year},
%    \fld{month} and \fld{address} fields normally refer to the date
%    and location of the conference rather than the publisher.  For
%    compatibility with the standard \BibTeX\ styles, we introduce new
%    fields for the publisher information rather than the conference
%    information, although it would probably be more logical for the
%    existing fields always to refer to the publisher and to introduce
%    new fields for the conference information.
%    \begin{macrocode}
    pub-year
    pub-month
    pub-address
    school
    series
    title
    type
    volume
    year
  }
  {}
  { label.year extra.label sort.year sort.label }

INTEGERS {
    output.state
    before.all
    mid.sentence
    after.sentence
    after.block
}
%    \end{macrocode}
%
%    \begin{function}{init.state.consts}
%    \begin{macrocode}
FUNCTION {init.state.consts}
{ #0 'before.all :=
  #1 'mid.sentence :=
  #2 'after.sentence :=
  #3 'after.block :=
}
%    \end{macrocode}
%    \end{function}
%
%    \begin{function}{output.nonnull}
%    \begin{macrocode}
STRINGS { s t u }

FUNCTION {output.nonnull}
{ 's :=
  output.state mid.sentence =
    { ", " * write$ }
    { output.state after.block =
    { add.period$ write$
      newline$
      "\newblock " write$
    }
    { output.state before.all =
        'write$
        { add.period$ " " * write$ }
      if$
    }
      if$
      mid.sentence 'output.state :=
    }
  if$
  s
}
%    \end{macrocode}
%    \end{function}
%
%    \begin{function}{output.nonnull.colon}
%    Use a colon to separate output. Used only for address/publisher
%    combination in book/inbook types, address/institution for
%    manuals, and organization:publisher for proceedings
%    (inproceedings).
%    \begin{macrocode}
FUNCTION {output.nonnull.colon}
{ 's :=
  output.state mid.sentence =
    { ": " * write$ }
    { output.state after.block =
    { add.period$ write$
      newline$
      "\newblock " write$
    }
    { output.state before.all =
        'write$
        { add.period$ " " * write$ }
      if$
    }
      if$
      mid.sentence 'output.state :=
    }
  if$
  s
}
%    \end{macrocode}
%    \end{function}
%
%    \begin{function}{output}
%    \begin{macrocode}
FUNCTION {output}
{ duplicate$ empty$
    'pop$
    'output.nonnull
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{output.colon}
%    \begin{macrocode}
FUNCTION {output.colon}
{ duplicate$ empty$
    'pop$
    'output.nonnull.colon
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{warn.null}
%    Issue a warning message if a field is empty.
%    \begin{macrocode}
FUNCTION {warn.null}
{
    "empty " swap$ * " in " * cite$ * warning$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{output.check}
%    \begin{macrocode}
FUNCTION {output.check}
{
    't :=
    duplicate$
    empty$
        { pop$ t warn.null }
        'output.nonnull
    if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{field.or.null.check}
%    \fnc{field.or.null.check} is similar to \fnc{field.or.null}, but
%    it also issues a warning if the field is null.
%    \begin{macrocode}
FUNCTION {field.or.null.check}
{
    't :=
    duplicate$
    empty$
        { pop$ "" t warn.null }
        'skip$
    if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{output.check.colon}
%    \begin{macrocode}
FUNCTION {output.check.colon}
{ 't :=
  duplicate$ empty$
    { pop$ t warn.null }
    'output.nonnull.colon
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{new.block}
%    \begin{macrocode}
FUNCTION {new.block}
{ output.state before.all =
    'skip$
    { after.block 'output.state := }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{new.sentence}
%    \begin{macrocode}
FUNCTION {new.sentence}
{
    output.state after.block =
        'skip$
        { output.state before.all =
            'skip$
            { after.sentence 'output.state := }
          if$
        }
    if$
}
%    \end{macrocode}
%    \end{function}
%
%    \begin{function}{output.year.check}
%    This is somewhat nasty.  We need to put a quad space around the
%    year.  Unfortunately, the obvious thing (i.e., putting out
%    ``\cs{quad}\meta{year}\cs{quad}'') won't work because the next
%    call to \fnc{output} will add a period \emph{after} the second
%    \cs{quad}.  Because of the way \BibTeX\ writes things, it would
%    be difficult to get the \cs{quad} in the right place, so instead
%    we cheat by wrapping the year in the macro \cs{bibyear}, which is
%    defined in \cls{acmart} to look ahead one character and, if
%    necessary, move the period before the second quad space.
%
%    This should be thought about some more.
%    \begin{macrocode}
FUNCTION {output.year.check}
{ year empty$
     { "year" warn.null }
     { add.period$ write$
       " \bibyear{" year * extra.label * "}" *
       mid.sentence 'output.state :=
     }
  if$
}
%    \end{macrocode}
%    \end{function}


%    \begin{function}{fin.entry}
%    \begin{macrocode}
FUNCTION {fin.entry}
{ add.period$
  write$
  newline$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{not}
%    \begin{macrocode}
FUNCTION {not}
{   { #0 }
    { #1 }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{and}
%    \begin{macrocode}
FUNCTION {and}
{   'skip$
    { pop$ #0 }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{or}
%    \begin{macrocode}
FUNCTION {or}
{   { pop$ #1 }
    'skip$
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{new.block.checka}
%    \begin{macrocode}
FUNCTION {new.block.checka}
{ empty$
    'skip$
    'new.block
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{new.block.checkb}
%    \begin{macrocode}
FUNCTION {new.block.checkb}
{ empty$
  swap$ empty$
  and
    'skip$
    'new.block
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{new.sentence.checka}
%    \begin{macrocode}
FUNCTION {new.sentence.checka}
{ empty$
    'skip$
    'new.sentence
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{new.sentence.checkb}
%    \begin{macrocode}
FUNCTION {new.sentence.checkb}
{ empty$
  swap$ empty$
  and
    'skip$
    'new.sentence
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{field.or.null}
%    \begin{macrocode}
FUNCTION {field.or.null}
{ duplicate$ empty$
    { pop$ "" }
    'skip$
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{parenthesize}
%    Put parentheses around the top string on the stack.
%    \begin{macrocode}
FUNCTION {parenthesize}
{ duplicate$ empty$
    { pop$ "" }
    { " (" swap$ * ")" * }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{emphasize}
%    Emphasize the top string on the stack.
%
%    It's a shame that we can't assume people are using \LaTeXe, since
%    the \cs{emph} command would be much more convenient here.  As it
%    is, we have to try to guess whether or not we will need an italic
%    correction, and use either \fnc{emphasize} or
%    \fnc{emphasize.ic} as appropriate.  As a partial solution, we
%    use the macros \cs{bibemph} and \cs{bibemphic}, which should be
%    defined by the style file.  For \LaTeXe, these can both be
%    equivalent to \cs{emph}; for \LaTeX~2.09, \cs{bibemphic} should
%    add an italic correction and \cs{bibemph} should not.
%    \begin{macrocode}
FUNCTION {emphasize}
{
    duplicate$ empty$
        { pop$ "" }
        { "\bibemph{" swap$ * "}" * }
    if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{emphasize.ic}
%    Emphasize the top string on the stack, and add an italic
%    correction.
%    \begin{macrocode}
FUNCTION {emphasize.ic}
{
    duplicate$ empty$
        { pop$ "" }
        { "\bibemphic{" swap$ * "}" * }
    if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{smallcaps}
%    Put the top string on the stack in small caps.
%    \begin{macrocode}
FUNCTION {smallcaps}
{ duplicate$ empty$
    { pop$ "" }
    { "\bibsc{" swap$ * "}" * }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.names}
%
%    Format bibliographical entries with the last name first.  All
%    names are formatted in this routine.
%
%    Also moved ``Junior'' part to the very end, so that it appears
%    after the first initial instead of before.
%
%    This function no longer automatically puts its output in
%    smallcaps; that's left to the functions that call
%    \fnc{format.names}, so we can use \fnc{format.names} to format
%    ``internal'' editor names. -- dmj, 4/28/96
%    \begin{macrocode}
INTEGERS { nameptr namesleft numnames }

FUNCTION {format.names}
{ 's :=
  #1 'nameptr :=               % nameptr = 1;
  s num.names$ 'numnames :=    % numnames = num.name$(s);
  numnames 'namesleft :=
    { namesleft #0 > }
    {
%      nameptr #1 =
%        {s nameptr "{vv~}{ll}{, f.}{, jj}" format.name$ 't := } {
        s nameptr "{vv~}{ll}{, f.}{, jj}" format.name$ 't :=
%      } if$
      nameptr #1 >
        { namesleft #1 >
              { ", " * t * }
              { numnames #2 >
                  { "," * }
                  'skip$
                if$
                t "others" =
                    { " et~al." * }
                    { " and " * t * } % from Chicago Manual of Style
                  if$
               }
               if$
             }
            't
        if$
        nameptr #1 + 'nameptr :=          % nameptr += 1;
        namesleft #1 - 'namesleft :=      % namesleft =- 1;
    }
  while$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{my.full.label}
%    \begin{macrocode}
FUNCTION {my.full.label}
{ 's :=
  #1 'nameptr :=               % nameptr = 1;
  s num.names$ 'numnames :=    % numnames = num.name$(s);
  numnames 'namesleft :=
    { namesleft #0 > }
    { s nameptr "{vv~}{ll}" format.name$ 't :=  % get the next name
      nameptr #1 >
        { namesleft #1 >
              { ", " * t * }
              { numnames #2 >
                  { "," * }
                  'skip$
                if$
                t "others" =
                    { " et~al." * }
                    { " and " * t * } % from Chicago Manual of Style
                  if$
               }
               if$
             }
            't
        if$
        nameptr #1 + 'nameptr :=          % nameptr += 1;
        namesleft #1 - 'namesleft :=      % namesleft =- 1;
    }
  while$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.names.fml}
%    Format names in ``familiar'' format, with first initial followed
%    by last name. Like format.names, ALL names are formatted.
%
%    Removed small caps from \fnc{format.names.fml} since it's used
%    for ``internal'' editor names, which should be in ordinary text.
%    -- dmj, 4/28/96
%    \begin{macrocode}
FUNCTION {format.names.fml}
{ 's :=
  #1 'nameptr :=               % nameptr = 1;
  s num.names$ 'numnames :=    % numnames = num.name$(s);
  numnames 'namesleft :=
    { namesleft #0 > }
    { s nameptr "{f.~}{vv~}{ll}{, jj}" format.name$ 't :=
      nameptr #1 >
        { namesleft #1 >
              { ", " * t * }
              { numnames #2 >
                  { "," * }
                  'skip$
                if$
                t "others" =
                    { " et~al." * }
                    { " and " * t * }
                  if$
               }
               if$
             }
            't
        if$
        nameptr #1 + 'nameptr :=          % nameptr += 1;
        namesleft #1 - 'namesleft :=      % namesleft =- 1;
    }
  while$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.authors}
%    Added \fnc{smallcaps}, which used to be inside
%    \fnc{format.names}. -- dmj, 4/28/96
%    \begin{macrocode}
FUNCTION {format.authors}
{ author empty$
    { "" }
    { author format.names smallcaps }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.key}
%    \begin{macrocode}
FUNCTION {format.key}
{ empty$
    { key field.or.null }
    { "" }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.editors.fml}
%
%    Format editor names for use in the ``in'' types: inbook,
%    incollection, inproceedings: first initial, then last names. When
%    editors are the LABEL for an entry, then \fnc{format.editor}
%    is used which lists editors by last name first.
%
%    Removed parens around ``Ed.'' since that seems to be desired by
%    the ACM. [8/18/95---dmj]
%    \begin{macrocode}
FUNCTION {format.editors.fml}
{ editor empty$
    { "" }
    { editor format.names.fml
      editor num.names$ #1 >
    { ", Eds." * }
    { ", Ed." * }
      if$
    }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.editors}
%    Format editor names for use in labels, last names first.
%
%    Removed parens around ``Ed.'' since that seems to be desired by
%    the ACM. [8/18/95---dmj]
%    \begin{macrocode}
FUNCTION {format.editors}
{ editor empty$
    { "" }
    { editor format.names smallcaps
      editor num.names$ #1 >
    { ", Eds." * }
    { ", Ed." * }
      if$
    }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.title}
%    \begin{macrocode}
FUNCTION {format.title}
{ title empty$
    { "" }
    { title "t" change.case$ }
  if$
}
%    \end{macrocode}
%    \end{function}
%
%    \begin{function}{n.dashify}
%    \begin{macrocode}
FUNCTION {n.dashify}
{ 't :=
  ""
  { t empty$ not }
    { t #1 #1 substring$ "-" =
    { t #1 #2 substring$ "--" = not
        { "--" *
          t #2 global.max$ substring$ 't :=
        }
        {   { t #1 #1 substring$ "-" = }
        { "-" *
          t #2 global.max$ substring$ 't :=
        }
          while$
        }
      if$
    }
    { t #1 #1 substring$ *
      t #2 global.max$ substring$ 't :=
    }
      if$
    }
  while$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.edition}
%    \begin{macrocode}
FUNCTION {format.edition}
{
    edition empty$
        { "" }
        { edition " ed." * }
    if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.btitle}
%    \begin{macrocode}
FUNCTION {format.btitle}
{
    title emphasize "title" output.check
    new.sentence
    format.edition output
    new.sentence
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.emphasize.booktitle}
%    \begin{macrocode}
FUNCTION {format.emphasize.booktitle}
{
    edition empty$
        { booktitle emphasize.ic }
        { booktitle empty$
%    \end{macrocode}
%    See above.
%    \begin{macrocode}
            { booktitle emphasize }
            { booktitle emphasize.ic edition " ed." * parenthesize * }
        if$
        }
    if$
}
%    \end{macrocode}
%    \end{function}


%    \begin{function}{tie.or.space.connect}
%    Enhanced so that if the first string is empty, no extra space is
%    put in.  It's not clear that this is really useful, but it
%    shouldn't hurt. -- dmj, 4/28/96
%    \begin{macrocode}
FUNCTION {tie.or.space.connect}
{
    swap$
    duplicate$
    empty$
        'pop$
        {
            swap$
            duplicate$
            text.length$ #3 <
                { "~" }
                { " " }
            if$
            swap$ * *
        }
    if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{either.or.check}
%    \begin{macrocode}
FUNCTION {either.or.check}
{ empty$
    'pop$
    { "can't use both " swap$ * " fields in " * cite$ * warning$ }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.bvolume}
%    What should be done if there is both a volume and a number in a
%    book? Currently, the number is silently ignored, and only the
%    volume is used.  Surely there should at least be some sort of
%    warning message.  Besides, if there's both a number and a series,
%    the series gets printed twice.  Must sort this out.
%    \begin{macrocode}
FUNCTION {format.bvolume}
{
    volume empty$
        { "" }
        { series emphasize " vol." volume tie.or.space.connect * }
    if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.number.series}
%    Should this be ``Series no.~5''?  If so, we should be able to
%    merge this with \fnc{format.bvolume}.
%    \begin{macrocode}
FUNCTION {format.number.series}
{
    volume empty$
        { number empty$
            { series field.or.null }
            { series empty$
                { "there's a number but no series in " cite$ * warning$ }
                { series " no." number tie.or.space.connect * }
              if$
            }
          if$
        }
        { "" }
    if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{multi.page.check}
%    \begin{macrocode}
INTEGERS { multiresult }

FUNCTION {multi.page.check}
{ 't :=
  #0 'multiresult :=
    { multiresult not
      t empty$ not
      and
    }
    { t #1 #1 substring$
      duplicate$ "-" =
      swap$ duplicate$ "," =
      swap$ "+" =
      or or
    { #1 'multiresult := }
    { t #2 global.max$ substring$ 't := }
      if$
    }
  while$
  multiresult
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.pages}
%
%    gnp - removed ()
%
%    Removed ``pp.'' since as far as I can tell, it's not wanted. --
%    dmj, 4/28/96
%    \begin{macrocode}
FUNCTION {format.pages}
{
    pages empty$
        { "" }
        { " " pages n.dashify * }
    if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.jour.vol}
%
%    By Young (and Spencer)
%    GNP - fixed bugs with missing volume, number, and/or pages
%
%    Format journal, volume, number, pages for article types.
%
%    \begin{macrocode}
FUNCTION {format.jour.vol}
{
    journal "journal" field.or.null.check
    volume empty$
        { "volume" warn.null }
        { volume tie.or.space.connect }
    if$
    emphasize.ic output
    number field.or.null
    month parenthesize
    *
    output
    format.pages "pages" output.check
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.chapter.pages}
%    \begin{macrocode}
FUNCTION {format.chapter.pages}
{ chapter empty$
    'format.pages
    { type empty$
        { "Chapter" } % gnp - changed to mixed case
        { type "t" change.case$ }
      if$
      chapter tie.or.space.connect
      pages empty$
        {"page numbers missing in " cite$ * warning$}%gnp - added check
        { ", " * format.pages * }
      if$
    }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.in.ed.booktitle}
%    I think this is backwards; editors should come after booktitle.
%    \begin{macrocode}
FUNCTION {format.in.ed.booktitle}
{
    booktitle empty$
        { "" }
        {
            "In " format.emphasize.booktitle *
            editor empty$
                'skip$
                { ", " * format.editors.fml * }
            if$
        }
    if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.thesis.type}
%    \begin{macrocode}
FUNCTION {format.thesis.type}
{ type empty$
    'skip$
    { pop$
      type "t" change.case$
    }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.thesis.title}
%    \begin{macrocode}
%%  FUNCTION {format.thesis.title}
%%  {
%%      title field.or.null "t" change.case$
%%  }
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.tr.number}
%    \begin{macrocode}
FUNCTION {format.tr.number}
{ type empty$
    { "Technical Report" }
    'type
  if$
  number empty$
    { "t" change.case$ }
    { number tie.or.space.connect }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.crossref}
%    Merged \fnc{format.article.crossref} and
%    \fnc{format.incoll.inproc.crossref} into a single macro,
%    \fnc{format.crossref}, which has proper spacing.
%    \begin{macrocode}
%<*xref>
FUNCTION {format.crossref}
{
    "See \citeN{" crossref * "}" *
}
%</xref>
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.crossref.editor}
%    \begin{macrocode}
%<*xref>
FUNCTION {format.crossref.editor}
{ editor #1 "{vv~}{ll}" format.name$
  editor num.names$ duplicate$
  #2 >
    { pop$ " et~al." * }
    { #2 <
    'skip$
    { editor #2 "{ff }{vv }{ll}{ jj}" format.name$ "others" =
        { " et~al." * }
        { " and " * editor #2 "{vv~}{ll}" format.name$ * }
      if$
    }
      if$
    }
  if$
}
%</xref>
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.book.crossref}
%    \begin{macrocode}
%<*xref>
FUNCTION {format.book.crossref}
{ volume empty$
    { "empty volume in " cite$ * "'s crossref of " * crossref * warning$
      "In "
    }
    { "Volume" volume tie.or.space.connect % gnp - changed to mixed case
      " of " *
    }
  if$
  editor empty$
  editor field.or.null author field.or.null =
  or
    { key empty$
    { series empty$
        { "need editor, key, or series for " cite$ * " to crossref " *
          crossref * warning$
          "" *
        }
        { series emphasize.ic }
      if$
    }
    { key * }
      if$
    }
    { format.crossref.editor * }
  if$
  " \citeN{" * crossref * "}" *
}
%</xref>
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.lab.names}
%
%    Determines ``short'' names for the abbreviated author
%    information.  ``Long'' labels are created in \fnc{calc.label},
%    using the routine \fnc{my.full.label} to format author and editor
%    fields.
%
%    There are 4 cases for labels ($n=3$ in the example):
%    \begin{enumerate}
%
%    \item
%    one author:             Foo
%
%    \item
%    one to $n$:               Foo, Bar and Baz
%
%    \item
%    use of ``and others'':    Foo, Bar et al.
%
%    \item
%    more than $n$:            Foo et al.
%
%    \end{enumerate}
%
%    \begin{macrocode}
FUNCTION {format.lab.names}
{ 's :=
  s num.names$ 'numnames :=
  numnames #2 >    % change number to number of others allowed before
                   % forcing "et al".
    { s #1 "{vv~}{ll}" format.name$ " et~al." * }
    {
      numnames #1 - 'namesleft :=
      #2 'nameptr :=
      s #1 "{vv~}{ll}" format.name$
        { namesleft #0 > }
        { nameptr numnames =
            { s nameptr "{ff }{vv }{ll}{ jj}" format.name$ "others" =
                { " et~al." * }
                { " and " * s nameptr "{vv~}{ll}" format.name$ * }
              if$
            }
            { ", " * s nameptr "{vv~}{ll}" format.name$ * }
          if$
          nameptr #1 + 'nameptr :=
          namesleft #1 - 'namesleft :=
        }
      while$
    }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{author.key.label}
%    \begin{macrocode}
FUNCTION {author.key.label}
{ author empty$
    { key empty$
          { "no key, author in " cite$ * warning$
            cite$ #1 #3 substring$ }
         'key
      if$
    }
    { author format.lab.names }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{editor.key.label}
%    \begin{macrocode}
FUNCTION {editor.key.label}
{ editor empty$
    { key empty$
          { "no key, editor in " cite$ * warning$
            cite$ #1 #3 substring$ }
          'key
        if$
     }
     { editor format.lab.names }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{author.key.org.label}
%    added - gnp. Provide label formatting by organization if author
%    is null.
%    \begin{macrocode}
FUNCTION {author.key.org.label}
{ author empty$
    { organization empty$
        { key empty$
            { "no key, author or organization in " cite$ * warning$
              cite$ #1 #3 substring$ }
            'key
          if$
        }
        'organization
      if$
    }
    { author format.lab.names }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{editor.key.org.label}
%    added - gnp. Provide label formatting by organization if editor
%    is null.
%    \begin{macrocode}
FUNCTION {editor.key.org.label}
{ editor empty$
    { organization empty$
        { key empty$
            { "no key, editor or organization in " cite$ * warning$
              cite$ #1 #3 substring$ }
            'key
          if$
        }
        'organization
      if$
    }
    { editor format.lab.names }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{author.editor.key.label}
%    \begin{macrocode}
FUNCTION {author.editor.key.label}
{ author empty$
    { editor empty$
          { key empty$
               { "no key, author, or editor in " cite$ * warning$
                 cite$ #1 #3 substring$ }
             'key
           if$
         }
          { editor format.lab.names }
      if$
    }
    { author format.lab.names }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{calc.label}
%
%    GNP: changed. Form label for \BibTeX\ entry. The classification
%    of which fields are used for which type of entry (book, inbook,
%    etc.) are taken from \bst{alpha.bst}.  The change here from
%    \bst{newapa} is to also include organization as a citation label
%    if author or editor is missing.  See also
%    \fnc{author.organization.sort}, \fnc{editor.organization.sort}.
%
%    \begin{macrocode}
FUNCTION {calc.label}
{ type$ "book" =
  type$ "inbook" =
  or
    'author.editor.key.label
    { type$ "proceedings" =
        'editor.key.org.label
        { type$ "manual" =
            'author.key.org.label
            'author.key.label
          if$
        }
      if$
    }
  if$
  author empty$  % generate the full label citation information.
    { editor empty$
        { organization empty$
           { "no author, editor, or organization in " cite$ * warning$
             "??" }
           'organization
           if$
        }
        { editor my.full.label }
        if$
    }
    { author my.full.label }
  if$
%    \end{macrocode}
%    leave label on the stack, to be popped when required.
%    \begin{macrocode}
  "}{" * swap$ * "}{" *
%  year field.or.null purify$ #-1 #4 substring$ *
%    \end{macrocode}
%    save the year for sort processing afterwards (adding a, b, c, etc.)
%    \begin{macrocode}
  year field.or.null purify$ #-1 #4 substring$
  'label.year :=
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{output.bibitem}
%    \begin{macrocode}
FUNCTION {output.bibitem}
{ newline$
  "\bibitem[\protect\citeauthoryear{" write$
  calc.label write$
  sort.year write$
  "}]{" write$
  cite$ write$
  "}" write$
  newline$
  ""
  before.all 'output.state :=
}
%    \end{macrocode}
%    \end{function}

%    \subsection{The entry types}
%
%    \subsubsection{The \typ{article} type}
%
%    \begin{function}{article}
%    \begin{macrocode}
FUNCTION {article}
{
    output.bibitem
    format.authors "author" output.check
%    \end{macrocode}
%    Some but not all types contain the following line.  It's
%    questionable whether it belongs there at all, but if it does,
%    shouldn't it be in \emph{all} the types?
%    \begin{macrocode}
    author format.key output          % added
    output.year.check                 % added
    new.block
    format.title "title" output.check
    new.block
%<*xref>
    crossref missing$
        {
%</xref>
            format.jour.vol
%<*xref>
        }
        {
            format.crossref output.nonnull
            format.pages output
        }
    if$
%</xref>
  new.block
  note output
  fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{book} type}
%
%    \begin{function}{book}
%    \begin{macrocode}
FUNCTION {book}
{
    output.bibitem
    author empty$
        { format.editors "author and editor" output.check }
        {
          format.authors output.nonnull
%<*xref>
          crossref missing$
              {
%</xref>
%    \end{macrocode}
%    But what if there really is both an author and an editor,
%    \emph{e.g.}, an edited version of someone else's book?
%    \begin{macrocode}
                "author and editor" editor either.or.check
%<*xref>
              }
              'skip$
          if$
%</xref>
        }
    if$
    output.year.check       % added
    new.block
    format.btitle % "title" output.check
%<*xref>
    crossref missing$
        {
%</xref>
            format.bvolume output
            new.block
            format.number.series output
            new.sentence
            publisher "publisher" output.check
            address output
%<*xref>
        }
        {
            new.block
            format.book.crossref output.nonnull
        }
    if$
%</xref>
    new.block
    note output
    fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{booklet} type}
%
%    \begin{function}{booklet}
%    \begin{macrocode}
FUNCTION {booklet}
{ output.bibitem
  format.authors output
  author format.key output          % added
  output.year.check                 % added
  new.block
  format.title "title" output.check
  new.block
  howpublished output
  address output
  new.block
  note output
  fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{inbook} type}
%
%    \begin{function}{inbook}
%    \begin{macrocode}
FUNCTION {inbook}
{
    output.bibitem
    author empty$
        { format.editors "author and editor" output.check }
        { format.authors output.nonnull
          crossref missing$
            { "author and editor" editor either.or.check }
            'skip$
          if$
        }
    if$
    output.year.check                 % added
    new.block
    format.btitle % "title" output.check
%<*xref>
    crossref missing$
        {
%</xref>
            format.bvolume output
            format.chapter.pages "chapter and pages" output.check
            new.block
            format.number.series output
            new.sentence
            publisher "publisher" output.check.colon
            address output
%<*xref>
        }
        {
            format.chapter.pages "chapter and pages" output.check
            new.block
            format.book.crossref output.nonnull
        }
    if$
%</xref>
    new.block
    note output
    fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{incollection} type}
%
%    \begin{function}{incollection}
%    \begin{macrocode}
FUNCTION {incollection}
{ output.bibitem
  format.authors "author" output.check
  author format.key output       % added
  output.year.check              % added
  new.block
  format.title "title" output.check
  new.block
%<*xref>
  crossref missing$
  {
%</xref>
    format.in.ed.booktitle "booktitle" output.check
    format.bvolume output
    format.number.series output
    format.chapter.pages output % gnp - was special.output.nonnull
%                                 left out comma before page numbers
    new.sentence
    address output
    publisher "publisher" output.check.colon
%<*xref>
  }
  {
    format.crossref output.nonnull
    format.chapter.pages output
  }
  if$
%</xref>
  new.block
  note output
  fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{inproceedings} type}
%
%    \begin{function}{format.date}
%    \begin{macrocode}
FUNCTION {format.date}
{ year empty$
    { month empty$
        { "" }
        { "there's a month but no year in " cite$ * warning$
          month
        }
      if$
    }
    { month empty$
        'year
        { month " " * year * }
      if$
    }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{format.proc.loc.date}
%    \begin{macrocode}
FUNCTION {format.proc.loc.date}
{
    address field.or.null
    format.date
    duplicate$ empty$
        'pop$
        { swap$
          duplicate$ empty$
            'pop$
            { ", " * swap$ * }
          if$
        }
    if$
    parenthesize
    *
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{inproceedings}
%    Added \fnc{format.proc.loc.date} to format proceedings location
%    and date.  Should also add something like
%    \fnc{format.proc.publisher} to handle the \fld{pub-year},
%    \fld{pub-month} and \fld{pub-address} fields, but should first
%    find out from ACM how such things should be handled.  In
%    particular, if year of publication is different from year of
%    conference, which should be used in citation (presumably the year
%    of publication).
%
%    A related problem is to decide exactly what the \fld{year} refers
%    to.  In the standard styles, if there is a \fld{year} but no
%    \fld{address}, the year is assumed to refer to the date of
%    publication, while if there is an \fld{address}, the address and
%    date are assumed to refer to the conference.  For true
%    compatibility with the standard styles, we should probably
%    preserve this distinction.
%    \begin{macrocode}
FUNCTION {inproceedings}
{
  output.bibitem
  format.authors "author" output.check
  author format.key output            % added
  output.year.check                   % added
  new.block
  format.title "title" output.check
  new.block
%<*xref>
  crossref missing$
    {
%</xref>
      format.in.ed.booktitle "booktitle" output.check
      format.bvolume output
      format.number.series output
%      address output
      format.proc.loc.date %output
%      format.pages output
      new.sentence
      organization output
      publisher output %.colon
      format.pages output
%<*xref>
     }
    {
      format.crossref output.nonnull
      format.pages output
    }
  if$
%</xref>
  new.block
  note output
  fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{conference} type}
%
%    \begin{function}{conference}
%    \begin{macrocode}
FUNCTION {conference} { inproceedings }
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{manual} type}
%
%    \begin{function}{manual}
%    \begin{macrocode}
FUNCTION {manual}
{ output.bibitem
  author empty$
    { editor empty$
      { organization "organization" output.check
        organization format.key output }  % if all else fails, use key
      { format.editors "author and editor" output.check }
      if$
    }
    { format.authors output.nonnull }
    if$
  output.year.check                 % added
  new.block
  format.btitle % "title" output.check
  organization address new.block.checkb
%    \end{macrocode}
%    Reversed the order of ``address'' and ``organization'', added the
%    ``:''.
%    \begin{macrocode}
  address output
  organization "organization" output.check.colon
%  address output
%  ":" output
%  organization output
  new.block
  note output
  fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{mastersthesis} type}
%
%    \begin{function}{mastersthesis}
%    \begin{macrocode}
FUNCTION {mastersthesis}
{ output.bibitem
  format.authors "author" output.check
  author format.key output          % added
  output.year.check                 % added
  new.block
  format.title "title" output.check
  new.block
  "Master's thesis" format.thesis.type output.nonnull
  school "school" output.check
  address output
  new.block
  note output
  fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{misc} type}
%
%    \begin{function}{misc}
%    \begin{macrocode}
FUNCTION {misc}
{ output.bibitem
  format.authors output
  author format.key output            % added
  output.year.check                   % added
  title howpublished new.block.checkb
  format.title output
  new.block
  howpublished output
  new.block
  note output
  fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{phdthesis} type}
%
%    \begin{function}{phdthesis}
%    \begin{macrocode}
FUNCTION {phdthesis}
{ output.bibitem
  format.authors "author" output.check
  author format.key output            % added
  output.year.check                   % added
  new.block
  format.btitle % "title" output.check
  new.block
  "Ph.\ D. thesis" format.thesis.type output.nonnull
  school "school" output.check
  address output
  new.block
  note output
  fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{proceedings} type}
%
%    \begin{function}{proceedings}
%    \begin{macrocode}
FUNCTION {proceedings}
{ output.bibitem
  editor empty$
    { organization output
      organization format.key output } % gnp - changed from
    { format.editors output.nonnull }  % author format.key
  if$
% author format.key output             % gnp - removed (should be either
%                                        editor or organization
  output.year.check                    % added (newapa)
  new.block
  format.btitle % "title" output.check
  format.bvolume output
  format.number.series output
%  address output
    format.proc.loc.date % output
  new.sentence
  organization output
  publisher output.colon
  new.block
  note output
  fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{techreport} type}
%
%    \begin{function}{techreport}
%    \begin{macrocode}
FUNCTION {techreport}
{ output.bibitem
  format.authors "author" output.check
  author format.key output             % added
  output.year.check                    % added
  new.block
  format.title "title" output.check
  new.block
  format.tr.number
     month empty$
     'skip$
     { month parenthesize * }
     if$
  output.nonnull
  institution "institution" output.check
  address output
  new.block
  note output
  fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{unpublished} type}
%
%    \begin{function}{unpublished}
%    \begin{macrocode}
FUNCTION {unpublished}
{ output.bibitem
  format.authors "author" output.check
  author format.key output              % added
  output.year.check                      % added
  new.block
  format.title "title" output.check
  new.block
  note "note" output.check
  fin.entry
}
%    \end{macrocode}
%    \end{function}

%    \subsubsection{The \typ{default.type} type}
%
%    \begin{function}{default.type}
%    \begin{macrocode}
FUNCTION {default.type} { misc }
%    \end{macrocode}
%    \end{function}

%    \subsection{Standard abbreviations}
%
%    The following abbreviations should all be checked to make sure
%    they correspond to what the ACM wants.
%    \begin{macrocode}
MACRO {jan} {"Jan."}

MACRO {feb} {"Feb."}

MACRO {mar} {"Mar."}

MACRO {apr} {"Apr."}

MACRO {may} {"May"}

MACRO {jun} {"June"}

MACRO {jul} {"July"}

MACRO {aug} {"Aug."}

MACRO {sep} {"Sept."}

MACRO {oct} {"Oct."}

MACRO {nov} {"Nov."}

MACRO {dec} {"Dec."}

MACRO {acmcs}    {"ACM Comput. Surv."}

MACRO {acta}     {"Acta Inf."}

MACRO {ai}       {"Artif. Intell."}

MACRO {cacm}     {"Commun. ACM"}

MACRO {ibmjrd}   {"IBM J. Res. Devel."}

MACRO {ibmsj}    {"IBM Syst. J."}

MACRO {ieeese}   {"IEEE Trans. Softw. Eng."}

MACRO {ieeetc}   {"IEEE Trans. Comput."}

MACRO {ieeetcad} {"IEEE Trans. Comput. Aided Des. Integr. Circuits"}

MACRO {ipl}      {"Inf. Process. Lett."}

MACRO {ic}       {"Inf. Comput."}

MACRO {jacm}     {"J. ACM"}

MACRO {jcss}     {"J. Comput. Syst. Sci."}

MACRO {scp}      {"Sci. Comput. Program."}

MACRO {sicomp}   {"SIAM J. Comput."}

MACRO {tocs}     {"ACM Trans. Comput. Syst."}

MACRO {tods}     {"ACM Trans. Database Syst."}

MACRO {tog}      {"ACM Trans. Graph."}

MACRO {toms}     {"ACM Trans. Math. Softw."}

MACRO {toois}    {"ACM Trans. Off. Inf. Syst"}

MACRO {tois}     {"ACM Trans. Inf. Syst."}

MACRO {toplas}   {"ACM Trans. Program. Lang. Syst."}

MACRO {tcs}      {"Theor. Comput. Sci."}

READ
%    \end{macrocode}

%    \subsection{Sorting the entries}
%
%    \begin{function}{sortify}
%    \begin{macrocode}
FUNCTION {sortify}
{
    purify$
    "l" change.case$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{chop.word}
%    \begin{macrocode}
INTEGERS { len }

FUNCTION {chop.word}
{ 's :=
  'len :=
  s #1 len substring$ =
    { s len #1 + global.max$ substring$ }
    's
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{sort.format.names}
%    \begin{macrocode}
FUNCTION {sort.format.names}
{ 's :=
  #1 'nameptr :=
  ""
  s num.names$ 'numnames :=
  numnames 'namesleft :=
    { namesleft #0 > }
    { nameptr #1 >
          { "   " * }
         'skip$
      if$
      s nameptr "{vv{ } }{ll{ }}{  f{ }}{  jj{ }}" format.name$ 't :=
      nameptr numnames = t "others" = and
          { " et~al" * }
          { t sortify * }
      if$
      nameptr #1 + 'nameptr :=
      namesleft #1 - 'namesleft :=
    }
  while$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{sort.format.title}
%    \begin{macrocode}
FUNCTION {sort.format.title}
{ 't :=
  "A " #2
    "An " #3
      "The " #4 t chop.word
    chop.word
  chop.word
  sortify
  #1 global.max$ substring$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{author.sort}
%    \begin{macrocode}
FUNCTION {author.sort}
{ author empty$
    { key empty$
         { "to sort, need author or key in " cite$ * warning$
           "" }
         { key sortify }
      if$
    }
    { author sort.format.names }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{editor.sort}
%    \begin{macrocode}
FUNCTION {editor.sort}
{ editor empty$
    { key empty$
         { "to sort, need editor or key in " cite$ * warning$
           ""
         }
         { key sortify }
      if$
    }
    { editor sort.format.names }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{author.editor.sort}
%    \begin{macrocode}
FUNCTION {author.editor.sort}
{
    author empty$
%    \end{macrocode}
%    Removed warning generated by lack of author since there's nothing
%    wrong with having an editor and no author. -- dmj, 4/26/96
%    \begin{macrocode}
        { % "missing author in " cite$ * warning$
          editor empty$
            { key empty$
                { "to sort, need author, editor, or key in "
                  cite$ * warning$
                  ""
                }
                {
                  key sortify
                }
              if$
            }
            {
              editor sort.format.names
            }
          if$
        }
        {
          author sort.format.names
        }
    if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{author.organization.sort}
%    added - GNP. Stack author or organization for sorting (from
%    alpha.bst).  Unlike alpha.bst, we need entire names, not
%    abbreviations
%    \begin{macrocode}
FUNCTION {author.organization.sort}
{ author empty$
    { organization empty$
        { key empty$
            { "to sort, need author, organization, or key in "
              cite$ * warning$
              ""
            }
            { key sortify }
          if$
        }
        { organization sortify }
      if$
    }
    { author sort.format.names }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{editor.organization.sort}
%
%    added - GNP. Stack editor or organization for sorting (from
%    alpha.bst).  Unlike alpha.bst, we need entire names, not
%    abbreviations
%
%    \begin{macrocode}
FUNCTION {editor.organization.sort}
{ editor empty$
    { organization empty$
        { key empty$
            { "to sort, need editor, organization, or key in "
              cite$ * warning$
              ""
            }
            { key sortify }
          if$
        }
        { organization sortify }
      if$
    }
    { editor sort.format.names }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{presort}
%
%    Presort creates the bibentry's label via a call to
%    \fnc{calc.label}, and then sorts the entries based on entry
%    type. Chicago.bst adds support for including organizations as the
%    sort key; the following is stolen from alpha.bst.
%
%    \begin{macrocode}
FUNCTION {presort}
{ calc.label sortify % recalculate bibitem label
  year field.or.null purify$ #-1 #4 substring$ * % add year
  "    "
  *
  type$ "book" =
  type$ "inbook" =
  or
    'author.editor.sort
    { type$ "proceedings" =
        'editor.organization.sort
        { type$ "manual" =
            'author.organization.sort
            'author.sort
          if$
        }
      if$
    }
  if$
  #1 entry.max$ substring$        % added for newapa
  'sort.label :=                  % added for newapa
  sort.label                      % added for newapa
  *
  "    "
  *
  title field.or.null
  sort.format.title
  *
  #1 entry.max$ substring$
  'sort.key$ :=
}
%    \end{macrocode}
%    \end{function}

%    \begin{macrocode}
ITERATE {presort}

SORT    % by label, year, author/editor, title

%    \end{macrocode}

%    \begin{function}{init.extra.label.stuff}
%    \begin{macrocode}
STRINGS { last.label next.extra }

INTEGERS { last.extra.num }

FUNCTION {init.extra.label.stuff}
{ #0 int.to.chr$ 'last.label :=
  "" 'next.extra :=
  #0 'last.extra.num :=
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{forward.pass}
%
%    Pass through all entries, comparing current entry to last one.
%    Need to concatenate year to the stack (done by
%    \fnc{calc.label}) to determine if two entries are the same
%    (see \fnc{presort})
%
%    \begin{macrocode}
FUNCTION {forward.pass}
{ last.label
  calc.label year field.or.null purify$ #-1 #4 substring$ * % add year
  #1 entry.max$ substring$ =     % are they equal?
     { last.extra.num #1 + 'last.extra.num :=
       last.extra.num int.to.chr$ 'extra.label :=
     }
     { "a" chr.to.int$ 'last.extra.num :=
       "" 'extra.label :=
       calc.label
       year field.or.null purify$ #-1 #4 substring$ * % add year
       #1 entry.max$ substring$ 'last.label := % assign to last.label
     }
  if$
}
%    \end{macrocode}
%    \end{function}

%    \begin{function}{reverse.pass}
%    \begin{macrocode}
FUNCTION {reverse.pass}
{ next.extra "b" =
    { "a" 'extra.label := }
     'skip$
  if$
  label.year extra.label * 'sort.year :=
  extra.label 'next.extra :=
}
%    \end{macrocode}
%    \end{function}

%    \begin{macrocode}
EXECUTE {init.extra.label.stuff}

ITERATE {forward.pass}

REVERSE {reverse.pass}
%    \end{macrocode}

%    \begin{function}{bib.sort.order}
%    \begin{macrocode}
FUNCTION {bib.sort.order}
{ sort.label
  "    "
  *
  year field.or.null sortify
  *
  "    "
  *
  title field.or.null
  sort.format.title
  *
  #1 entry.max$ substring$
  'sort.key$ :=
}
%    \end{macrocode}
%    \end{function}

%    \begin{macrocode}
ITERATE {bib.sort.order}

SORT % by sort.label, year, title --- giving final bib. order.
%    \end{macrocode}

%    \begin{function}{begin.bib}
%    \begin{macrocode}
FUNCTION {begin.bib}
{ preamble$ empty$
    'skip$
    { preamble$ write$ newline$ }
  if$
  "\begin{thebibliography}{}" write$ newline$
}
%    \end{macrocode}
%    \end{function}

%    \begin{macrocode}
EXECUTE {begin.bib}

EXECUTE {init.state.consts}

ITERATE {call.type$}
%    \end{macrocode}

%    \begin{function}{end.bib}
%    \begin{macrocode}
FUNCTION {end.bib}
{ newline$
  "\end{thebibliography}" write$ newline$
}
%    \end{macrocode}
%    \end{function}

%    \begin{macrocode}
EXECUTE {end.bib}
%    \end{macrocode}

\endinput
