% $Id: mlastyle.bst,v 1.20 2005-02-09 03:53:11 tpope Exp $
% BibTeX bibliography style `mlastyle'
% Based on BibTeX standard bibliography style `plain'
	% version 0.99a for BibTeX versions 0.99a or later, LaTeX version 2.09.
	% Copyright (C) 2003 Tim Pope, 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
	% btxbst.doc, plain.bst, unsrt.bst, alpha.bst, and abbrv.bst.
	% This restriction helps ensure that all standard styles are identical.
	% The file mlastyle.dvi has the documentation for this style.

ENTRY
  { address
    author
    booktitle
    chapter
    edition
    editor
    howpublished
    institution
    journal
    key
    month
    note
    number
    organization
    pages
    publisher
    school
    series
    title
    type
    volume
    year
    % New fields
    url
    urldate
    accessdate
    urlpath
    database
    version
    % eid isbn issn doi
  }
  { author.duplicate author.shared }
  { label }

INTEGERS { output.state before.all mid.phrase mid.sentence after.sentence after.block author.different author.last author.flast citealp.used url.used }

STRINGS { s t u empty.title }

% Rearranged such that true values require capital letters
FUNCTION {init.consts}
{ #-1 'mid.phrase :=
  #0 'mid.sentence :=
  #1 'before.all :=
  #2 'after.sentence :=
  #3 'after.block :=

  #0 'author.different :=
  #1 'author.last :=
  #2 'author.flast :=
  %#3 'author.firstlast :=

  #0 'citealp.used :=
  #0 'url.used :=
  " \ \ \ \ \unskip\unskip\unskip\unskip\unskip " 'empty.title :=
  "" 'empty.title :=
}

% I do this a lot
FUNCTION {duplicate}
{
  duplicate$
  "duplicate should be duplicate$" warning$
}

FUNCTION {not}
{   { #0 }
    { #1 }
  if$
}

FUNCTION {and}
{   'skip$
    { pop$ #0 }
  if$
}

FUNCTION {or}
{   { pop$ #1 }
    'skip$
  if$
}

INTEGERS {chars i j}

% text.length$ does not include braces
% This function is probably a bad idea
FUNCTION {num.chars}
{
  #0 'chars :=
    { duplicate$ duplicate$ #1 chars substring$ = not }
    { duplicate$ duplicate$ text.length$ swap$ #1 chars
    substring$ text.length$ - chars + #1 + 'chars := }
  while$
    { duplicate$ duplicate$ #1 chars #1 - substring$ = }
    { chars #1 - 'chars := }
  while$
  pop$ chars
}

% Puts the period inside any quotes
FUNCTION {do.period}
{
  duplicate$ #-1 #2 substring$ "''" =
    { #2 'i :=
	{ duplicate$ #-1 i - #11 substring$ "'\thinspace" = }
	{ i #11 + 'i := }
      while$
      duplicate$ #-1 i - global.max$ substring$
      add.period$ swap$ #-1 i substring$ *
    }
    {
      duplicate$ #-1 #3 substring$ "''}" =
	{ #3 'i :=
	    { duplicate$ #-1 i - #11 substring$ "'\thinspace" = }
	    { i #11 + 'i := }
	  while$
	  duplicate$ #-1 i - global.max$ substring$
	  add.period$ swap$ #-1 i substring$ *
	}
	{ add.period$ }
      if$
    }
  if$
}

FUNCTION {do.space}
{ duplicate$ duplicate$ do.period =
    { "\ " * }
    { " " * }
  if$
}

% Switched to do.period and added mid.phrase
FUNCTION {output.nonnull}
{ 's :=
  output.state mid.sentence =
    { ", " * write$ }
    { output.state after.block =
	{ do.period write$
	  newline$
	  "\newblock " write$
	}
	{ output.state before.all =
	    'write$
	    { output.state mid.phrase =
		{ do.space write$ }
		{ do.period " " * write$ }
	      if$
	    }
	  if$
	}
      if$
      mid.sentence 'output.state :=
    }
  if$
  s
}

FUNCTION {output}
{ duplicate$ empty$
    'pop$
    'output.nonnull
  if$
}

FUNCTION {output.check}
{ 't :=
  duplicate$ empty$
    { pop$ "empty " t * " in " * cite$ * warning$ }
    'output.nonnull
  if$
}

% Switched to do.period
FUNCTION {fin.entry}
{ do.period
  write$
  newline$
}

FUNCTION {new.block}
{ output.state before.all =
    'skip$
    { after.block 'output.state := }
  if$
}

FUNCTION {new.sentence}
{ output.state after.block =
    'skip$
    { output.state before.all =
	'skip$
	{ after.sentence 'output.state := }
      if$
    }
  if$
}

FUNCTION {continue.sentence}
{ output.state after.block =
    'skip$
    { output.state before.all =
	'skip$
	{ mid.phrase 'output.state := }
      if$
    }
  if$
}

FUNCTION {new.block.checka}
{ empty$
    'skip$
    'new.block
  if$
}

FUNCTION {new.block.checkb}
{ empty$
  swap$ empty$
  and
    'skip$
    'new.block
  if$
}

FUNCTION {new.sentence.checka}
{ empty$
    'skip$
    'new.sentence
  if$
}

FUNCTION {new.sentence.checkb}
{ empty$
  swap$ empty$
  and
    'skip$
    'new.sentence
  if$
}

FUNCTION {field.or.null}
{ duplicate$ empty$
    { pop$ "" }
    'skip$
  if$
}

FUNCTION {sortify}
{ purify$
  "l" change.case$
}

FUNCTION {emphasize}
{ duplicate$ empty$
    { pop$ "" }
    { "\emph{" swap$ * "}" * }
  if$
}

FUNCTION {enquote}
{ duplicate$ empty$
    { pop$ "" }
    {
      #1 'i :=
      #0 'j :=
	{ duplicate$ i #1 substring$ "" = not }
	{ duplicate$ duplicate$ i #1 substring$ quote$ =
	  swap$ i #1 - #1 substring$ "\" = not and %"
	    { duplicate$ #1 i #1 - substring$ swap$
	      i #1 + global.max$ substring$
	      j
		{ swap$ duplicate$ #-1 #1 substring$ "'" =
		    { swap$ "\thinspace'" }
		    { swap$ "'" }
		  if$
		   #0 'j :=
		}
		{ duplicate$ #1 #1 substring$ "`" =
		    { "`\thinspace" }
		    { "`" }
		  if$
		#1 'j :=
		}
	      if$
	      swap$ * *
	    }
	    'skip$
	  if$
	  i #1 + 'i :=
	}
      while$
      j #1 =
	{ "odd number of quotation marks in " cite$ * warning$ }
	'skip$
      if$
      duplicate$ #1 #1 substring$ "`" =
	{ "``\thinspace" swap$ * }
	{ "``" swap$ *}
      if$
      duplicate$ #-1 #1 substring$ "'" =
	{ "\thinspace''" * }
	{ "''" *}
      if$
    }
  if$
}

FUNCTION {selective.emphasize}
{ field.or.null duplicate$ sortify 's :=
  s #1 #8 substring$ duplicate$ "rev. of " = swap$ "rev.~of " = or
  s #1 #9 substring$ "rev.\ of " = or
  s "home page" = or
  s "america online" = or
    'skip$
    'emphasize
  if$
}

FUNCTION {selective.enquote}
{ field.or.null duplicate$ sortify 's :=
  s #1 #8 substring$ duplicate$ "rev. of " = swap$ "rev.~of " = or
  s #1 #9 substring$ "rev.\ of " = or
  s "introduction" = or
  s "preface" = or
  s "forward" = or
  s "afterward" = or
  s "home page" = or
    'skip$
    'enquote
  if$
}

INTEGERS { nameptr namesleft numnames }

FUNCTION {format.names.custom}
{ 'u :=
  's :=
  #1 'nameptr :=
  s num.names$ 'numnames :=
  numnames 'namesleft :=
    { namesleft #0 > }
    { s nameptr u 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 :=
      namesleft #1 - 'namesleft :=
    }
  while$
}

FUNCTION {format.names}
{ "{ff~}{vv~}{ll}{, jj}" format.names.custom }

% Changed first author to last name first
FUNCTION {format.names.beginning}
{ 's :=
  #1 'nameptr :=
  s num.names$ 'numnames :=
  numnames 'namesleft :=
    { namesleft #0 > }
    { nameptr #1 =
	{ s nameptr "{vv~}{ll}{, ff}{, jj}" format.name$
	  duplicate$ #1 #1 substring$ duplicate$ "{" =
	    'skip$
	    { "u" change.case$ }
	  if$
	  swap$ #2 global.max$ substring$ * 't :=
	}
	{ s nameptr "{ff~}{vv~}{ll}{, jj}" format.name$ 't := }
      if$
      nameptr #1 >
	{ namesleft #1 >
	    { ", " * t * }
	    { numnames #2 >
		{ "," * }
		'skip$
	      if$
	      t "others" =
		{ " et~al." * }
		{ " and " * t * }
	      if$
	    }
	  if$
	}
	't
      if$
      nameptr #1 + 'nameptr :=
      namesleft #1 - 'namesleft :=
    }
  while$
}

% Added check for duplicate authors
FUNCTION {format.authors}
{ author empty$
    { "" }
    { author.duplicate #1 =
	{ "-{}-{}-" }
	{ author format.names.beginning }
      if$
    }
  if$
}

% Changed to MLA editor abbreviations
% author.duplicate also applies
FUNCTION {format.editors}
{ editor empty$
    { "" }
    { author empty$ author.duplicate #1 = and
	{ "-{}-{}-" }
	{ editor format.names.beginning }
      if$
      editor num.names$ #1 >
	{ ", eds." * }
	{ ", ed." * }
      if$
    }
  if$
}

FUNCTION {format.editors.mid}
{ editor empty$
    { "" }
    {
      editor num.names$ #1 >
	{ "Eds.\ " }
	{ "Ed.\ " }
      if$
      editor format.names *
    }
  if$
}

% Formats the address and publisher
FUNCTION {format.publisher}
{ address empty$
    { publisher }
    { address ": " * publisher * }
  if$
}

% Changed to preserve case and use quotes
FUNCTION {format.title}
{ title selective.enquote
}

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$
}

FUNCTION {format.year}
{ year empty$
    { "" }
    { year n.dashify
      duplicate$ duplicate$ num.chars #10 =
      swap$ #5 #2 substring$ "--" = and
	{ duplicate$ duplicate$ #1 #2 substring$ swap$ #7 #2 substring$ =
	    { duplicate$ #1 #6 substring$ swap$ #9 #2 substring$ * }
	    {}
	  if$
	}
	{}
      if$
    }
  if$
}

FUNCTION {format.month}
{
  month #-1 #1 substring$ chr.to.int$ duplicate$ #47 > swap$ #58 < and
    { 
      month #-2 #1 substring$ chr.to.int$ duplicate$ #47 > swap$ #58 < and
        { #2 'i := }
	{ #1 'i := }
      if$
      month #-2 i - #2 substring$ "\ " =
        { month #0 i - i substring$
	  month #-2 i - #2 substring$
	  month #-3 i - global.max$ i - #2 - substring$
	  * *
	}
	{
	  month #-1 i - #1 substring$ duplicate$ " " = swap$ "~" = or
	    { month #0 i - i substring$
	      month #-1 i - #1 substring$
	      month #-2 i - global.max$ i - #1 - substring$
	      * *
	    }
	    { month }
	  if$
	}
      if$
    }
    { month }
  if$
}

FUNCTION {format.date}
{ year empty$
    { month empty$
	{ "" }
	{ "there's a month but no year in " cite$ * warning$
	  format.month
	}
      if$
    }
    { month empty$
	'format.year
	{ format.month do.space format.year * }
      if$
    }
  if$
}

%INTEGERS {xx}
FUNCTION {format.btitle}
{ title selective.emphasize
}

FUNCTION {tie.or.space.connect}
{ duplicate$ text.length$ #3 <
    { "~" swap$ * * }
    { swap$ do.space swap$ * }
  if$
}

FUNCTION {either.or.check}
{ empty$
    'pop$
    { "can't use both " swap$ * " fields in " * cite$ * warning$ }
  if$
}

FUNCTION {abbreviate}
{ duplicate$ sortify 's := 't :=
  ""
  "10th" "tenth" "9th" "ninth" "8th" "eighth" "7th" "seventh" "6th" "sixth"
  "5th" "fifth" "4th" "fourth" "3rd" "third" "2nd" "second" "1st" "first"
  "Rev." "revised" "Abr." "abridged"
  {duplicate$ empty$ { pop$ #0 }{ #1 } if$ }{ s = { 't := }{ pop$ } if$ }
  while$
  t
}

FUNCTION {format.bvolume.or.num.series}
{ volume empty$
    { number empty$
	{ series field.or.null }
	{ 
	  series empty$
	    { number "there's a number but no series in " cite$ * warning$ }
	    { series
	  %output.state mid.sentence =
	    %{ " Sers." * }
	    %{ "Sers." * }
	  %if$
	  number tie.or.space.connect
	  }
	  if$
	}
      if$
    }
    { "Vol." volume tie.or.space.connect
      series empty$
	'skip$
	{ " of " * series selective.emphasize * add.period$ }
      if$
      number empty$
	'skip$
	{ number * " vols." * }
      if$
      %"volume and number" number either.or.check
    }
  if$
}

% Changed edition to MLA abbreviation
FUNCTION {format.edition}
{ edition empty$
    { version empty$
	{ "" }
	{ "Vers.~" version *}
      if$
    }
    { edition abbreviate
      duplicate$ sortify "revised" =
	{ pop$ "Rev." }
	'skip$
      if$
      duplicate$ sortify "abridged" =
	{ pop$ "Abr." }
	'skip$
      if$
      do.space "ed." *
      "edition and version" version either.or.check
    }
  if$
}

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
}

FUNCTION {format.pages}
{ pages empty$
    { "" }
    {
% j is positive if pages contains a comma
% (not fully implemented)
      #0 'j :=
      pages n.dashify duplicate$ 't :=
	{ duplicate$ empty$ not }
	{
	  duplicate$ #1 #1 substring$ duplicate$
	  "{" =
	    { j #1 + 'j := }
	    'skip$
	  if$
	  "}" =
	    { j #1 - 'j := }
	    'skip$
	  if$
	  duplicate$ #1 #2 substring$ ", " =
	    { j j #0 = + 'j := }
	    'skip$
	  if$
	  #2 global.max$ substring$
	}
      while$
	{ t empty$ not }
	{ t #1 #2 substring$ "--" = j not and
	    { t #3 global.max$ substring$ 't :=
	      duplicate$ num.chars 'i :=
	      "--" *
	      i t num.chars =
		{   { i #2 > }
		    { duplicate$ #1 i #2 - substring$
		      t #1 i #2 - substring$ =
			{ t i #1 - global.max$ substring$ * "" 't :=  #2 'i := }
			{ i #1 - 'i := }
		      if$
		    }
		  while$
		}
		'skip$
	      if$
	    }
	    { t #1 #1 substring$ duplicate$ "," = swap$ "-" = or j and
		{ "+" * "" 't := }
		{ t #1 #1 substring$ *
		  t #2 global.max$ substring$ 't :=
		}
	      if$
	    }
	  if$
	}
      while$
    }
  if$
}

FUNCTION {format.vol.num.year.pages}
{ volume empty$
    { number empty$
	'skip$
	{ number n.dashify
	  "there's a number but no volume in " cite$ * warning$ }
      if$
      format.date
    }
    { volume
      number empty$
	'skip$
	{ "." * number n.dashify * }
      if$
      " (" * format.date * ")" *
    }
  if$
  edition empty$
    'skip$
    { ", " * format.edition * }
  if$
  type field.or.null sortify duplicate$ "sec." = swap$ "section" = or
    { chapter empty$
	  { "section type requires chapter in " cite$ warning$ }
	  { ", sec." chapter n.dashify tie.or.space.connect }
	if$
      }
    'skip$
  if$
  type field.or.null sortify "review" = chapter empty$ not and
    { ", sec." chapter n.dashify tie.or.space.connect }
    'skip$
  if$
  pages empty$
    'skip$
    { ": " * format.pages * }
  if$
}

% chapter might be useful if type is set to "pars."
FUNCTION {format.chapter.pages}
{ chapter empty$
  type field.or.null sortify duplicate$ "def." = swap$ "definition" = or
  or
    'format.pages
    { type empty$
	{ "chapter ignored in " cite$ * warning$ }
	{ type "t" change.case$
	  chapter n.dashify tie.or.space.connect
	  pages empty$
	    'skip$
	    { ", " * }
	  if$
	}
      if$
      format.pages *
    }
  if$
}

FUNCTION {empty.misc.check}
{ author empty$ title empty$ howpublished empty$
  month empty$ year empty$ note empty$
  and and and and and
  key empty$ not and
    { "all relevant fields are empty in " cite$ * warning$ }
    'skip$
  if$
}

FUNCTION {format.thesis.type}
{ type empty$
    'skip$
    { pop$
      type "t" change.case$
    }
  if$
}

FUNCTION {format.tr.number}
{ type empty$
    { "Technical Report" }
    'type
  if$
  number empty$
    { "t" change.case$ }
    { number tie.or.space.connect }
  if$
}

FUNCTION {format.urldate}
{ database empty$
    'skip$
    { database selective.emphasize output.nonnull new.block }
  if$
  urldate missing$
  { accessdate empty$
      { cite$ " has no urldate field" * warning$
	"\theurldate{}"
      }
      { "accessdate in " cite$ * " is depreciated; use urldate instead" *
	warning$ accessdate
      }
    if$
  }
  { urldate }
  if$
}
FUNCTION {output.url}
{
  url empty$
    { urlpath empty$ 
	'skip$
	{ format.urldate output }
      if$
    }
    { format.urldate
      " $<$\url{" * url * "}$>$" *
      output.nonnull
    }
  if$
  urlpath empty$
    'skip$
    { "Path: " urlpath * output.nonnull }
  if$
}

FUNCTION {format.crossref}
{
  pages empty$ { "\citealp" } { "\citealp[" format.chapter.pages * "]" * } if$
  "{" * crossref * "}" *
}


FUNCTION {cite.title}
{ title empty$ journal empty$ not and
    { key empty$ { journal }{ key } if$ selective.emphasize }
    { title empty$ key empty$ and
	{ "empty title and key in " cite$ * warning$ "" }
	{ key empty$ {title}{key} if$
	  type$ "book" =
	  type$ "booklet" =
	  type$ "inbook" =
	  type$ "manual" =
	  type$ "phdthesis" =
	  type$ "proceedings" =
	  or or or or or
	    { selective.emphasize }
	    { type$ "article" =
	      %type$ "booklet" =
	      type$ "incollection" =
	      type$ "inproceedings" =
	      type$ "mastersthesis" =
	      type$ "misc" =
	      type$ "techreport" =
	      type$ "unpublished" =
	      or or or or or or
		{ selective.enquote }
		{}
	      if$
	    }
	  if$
	}
      if$
    }
  if$
}

FUNCTION {cite.label}
{
  %author empty$
  %  { editor empty$ { organization field.or.null 'a := }{ editor 'a :=} if$}
  %  { author 'a := }
  %if$
  %abbr empty$ {
  author empty$
    { editor empty$
	{ "" }
	{ editor }
      if$
    }
    { author }
  if$
  duplicate$ empty$
    { cite.title *
    "(" * empty.title * ")" * }
    { duplicate$
      author.shared author.different =
	{ "{vv~}{ll}" format.names.custom 's := }
	{ author.shared author.last =
	    { "{f.~}{vv~}{ll}{, jj}" format.names.custom 's := }
	    { author.shared author.flast =
		{ "{ff~}{vv~}{ll}{, jj}" format.names.custom 's := }
		{ "{vv~}{ll}" format.names.custom ", " *
		  cite.title * 's := "WTF" warning$ }
	      if$
	    }
	  if$
	}
      if$
      s
      "(" * empty.title *
      author.duplicate { cite.title * }{} if$
      "){" * swap$ "{ff~}{vv~}{ll}{, jj}" format.names.custom * "}" *
    }
  if$
      %}
      %if$
    %}
    %{}% abbr }
 % if$
}

% TODO
FUNCTION {output.bibitem}
{ newline$
  "\bibitem[" write$
  cite.label write$
  "]{" write$
  cite$ write$
  "}" write$
  newline$
  ""
  before.all 'output.state :=
}

FUNCTION {article}
{ output.bibitem
  format.authors "author" output.check
  new.block
  format.title "title" output.check
  new.block
  crossref missing$
    { type empty$
	'skip$
	{ type sortify "review" =
	    { "Rev.\ of " booktitle selective.emphasize * 
	    "booktitle" output.check new.block
	    }
	    { "Ignoring invalid type in " cite$ warning$ }
	  if$
	}
      if$
      journal selective.emphasize "journal" output.check
      continue.sentence
      address empty$
	'skip$
	{ "[" address * "]" * output
	  continue.sentence
	}
      if$
      series empty$
	'skip$
	{ series sortify
	  duplicate$ "new" =
	    { "ns" }
	    { duplicate$ "old" =
		{ "os" }
		{ series abbreviate do.space "ser." * "l" change.case$ }
	      if$
	    }
	  if$
	  output.nonnull
	  continue.sentence
	}
      if$
      format.vol.num.year.pages output
    }
    { format.crossref output.nonnull
      %format.pages output
    }
  if$
  new.block
  output.url
  new.block
  note output
  fin.entry
}

FUNCTION {book}
{ output.bibitem
  author empty$
    { format.editors "author and editor" output.check }
    { format.authors output.nonnull }
  if$
  new.block
  format.btitle "title" output.check
  new.block
  crossref missing$
    { author empty$ 'skip$ { format.editors.mid output new.block } if$
      format.edition output
      new.sentence
      format.bvolume.or.num.series output
      new.block
      format.publisher "publisher" output.check
      format.date "year" output.check
    }
    {% format.date "year" output
      new.block
      format.crossref output.nonnull
    }
  if$
  new.block
  output.url
  new.block
  note output
  fin.entry
}

FUNCTION {booklet}
{ output.bibitem
  format.authors output
  new.block
  format.btitle "title" output.check
  howpublished address new.block.checkb
  howpublished output
  address output
  format.date output
  new.block
  note output
  fin.entry
}

FUNCTION {inbook}
{ output.bibitem
  author empty$
    { format.editors "author and editor" output.check }
    { format.authors output.nonnull }
  if$
  new.block
  format.btitle "title" output.check
  new.block
  crossref missing$
    { author empty$ 'skip$ { format.editors.mid output new.block } if$
      format.edition output
      new.sentence
      format.bvolume.or.num.series output
      new.block
      format.publisher "publisher" output.check
      format.date "year" output.check
      new.sentence
      format.chapter.pages "chapter and pages" output.check
      %address output
    }
    {
      new.block
      format.crossref output.nonnull
    }
  if$
  new.block
  output.url
  note output
  fin.entry
}

FUNCTION {incollection}
{ output.bibitem
  format.authors "author" output.check
  chapter empty$
    { new.block type output }
    'skip$
  if$
  new.block
  format.title "title" output.check
  type field.or.null sortify duplicate$ "definition" = swap$ "def." = or
    { new.block "Def.~" chapter field.or.null * "chapter" output.check } 
    'skip$
  if$
  new.block
  crossref missing$
    { 
      booktitle selective.emphasize "booktitle" output.check
      new.block
      format.editors.mid "editor" output.check
      new.block
      format.edition output
      new.sentence
      format.bvolume.or.num.series output
      new.block
      format.publisher "publisher" output.check
      %address output
      format.date "year" output.check
      new.block
      format.chapter.pages output
    }
    { format.crossref output.nonnull
      %format.chapter.pages output
    }
  if$
  new.block
  output.url
  new.block
  note output
  fin.entry
}

FUNCTION {inproceedings}
{ output.bibitem
  format.authors "author" output.check
  new.block
  format.title "title" output.check
  new.block
  crossref missing$
    {
      booktitle "booktitle" output.check
      new.sentence
      format.editors.mid output
      new.block
      format.edition output
      new.sentence
      format.bvolume.or.num.series output
      new.block
      format.edition output
      new.sentence
      format.publisher output
      format.date "year" output.check
      %address empty$
	%{ organization publisher new.sentence.checkb
	 % organization output
	 % publisher output
	 % format.date "year" output.check
	%}
	%{ address output.nonnull
	 % format.date "year" output.check
	 % new.sentence
	 % organization output
	 % publisher output
	%}
      %if$
      new.sentence
      format.pages output
    }
    { format.crossref output.nonnull
    }
  if$
  new.block
  output.url
  new.block
  note output
  fin.entry
}

FUNCTION {conference} { inproceedings }

FUNCTION {manual}
{ output.bibitem
  author empty$
    { organization empty$
	'skip$
	{ organization output.nonnull
	  address output
	}
      if$
    }
    { format.authors output.nonnull }
  if$
  new.block
  format.btitle "title" output.check
  new.block
  format.edition output
  new.sentence
  author empty$
    { organization empty$
	{ address new.block.checka
	  address output
	}
	'skip$
      if$
    }
    { organization address new.block.checkb
      organization output
      address output
    }
  if$
  format.date output
  new.block
  output.url
  new.block
  note output
  fin.entry
}

FUNCTION {mastersthesis}
{ output.bibitem
  format.authors "author" output.check
  new.block
  format.title "title" output.check
  new.block
  "Diss." format.thesis.type output.nonnull
  new.block
  school "school" output.check
  address output
  format.date "year" output.check
  new.block
  output.url
  new.block
  note output
  fin.entry
}

FUNCTION {misc}
{ output.bibitem
  format.authors output
  title howpublished new.block.checkb
  format.title output
  new.block
  %howpublished new.block.checka
  howpublished output
  format.date output
  new.block
  output.url
  new.block
  note output
  fin.entry
  empty.misc.check
}

FUNCTION {phdthesis}
{ output.bibitem
  format.authors "author" output.check
  new.block
  format.btitle "title" output.check
  new.block
  "Diss." format.thesis.type output.nonnull
  new.block
  school "school" output.check
  address output
  format.date "year" output.check
  new.block
  output.url
  new.block
  note output
  fin.entry
}

FUNCTION {proceedings}
{ output.bibitem
  editor empty$
    { organization output }
    { format.editors output.nonnull }
  if$
  new.block
  format.btitle "title" output.check
  new.block
  format.bvolume.or.num.series output
  address empty$
    { editor empty$
	{ publisher new.sentence.checka }
	{ organization publisher new.sentence.checkb
	  organization output
	}
      if$
      publisher output
      format.date "year" output.check
    }
    { address output.nonnull
      format.date "year" output.check
      new.sentence
      editor empty$
	'skip$
	{ organization output }
      if$
      publisher output
    }
  if$
  new.block
  output.url
  new.block
  note output
  fin.entry
}

FUNCTION {techreport}
{ output.bibitem
  format.authors "author" output.check
  new.block
  format.title "title" output.check
  new.block
  format.tr.number output.nonnull
  new.block
  institution "institution" output.check
  address output
  format.date "year" output.check
  new.block
  note output
  fin.entry
}

FUNCTION {unpublished}
{ output.bibitem
  format.authors "author" output.check
  new.block
  format.title "title" output.check
  new.block
  output.url
  new.block
  note "note" output.check
  format.date output
  fin.entry
}

FUNCTION {default.type} { misc }

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."}

% Currently left in to help with compatibility

MACRO {acmcs} {"ACM Computing Surveys"}

MACRO {acta} {"Acta Informatica"}

MACRO {cacm} {"Communications of the ACM"}

MACRO {ibmjrd} {"IBM Journal of Research and Development"}

MACRO {ibmsj} {"IBM Systems Journal"}

MACRO {ieeese} {"IEEE Transactions on Software Engineering"}

MACRO {ieeetc} {"IEEE Transactions on Computers"}

MACRO {ieeetcad}
 {"IEEE Transactions on Computer-Aided Design of Integrated Circuits"}

MACRO {ipl} {"Information Processing Letters"}

MACRO {jacm} {"Journal of the ACM"}

MACRO {jcss} {"Journal of Computer and System Sciences"}

MACRO {scp} {"Science of Computer Programming"}

MACRO {sicomp} {"SIAM Journal on Computing"}

MACRO {tocs} {"ACM Transactions on Computer Systems"}

MACRO {tods} {"ACM Transactions on Database Systems"}

MACRO {tog} {"ACM Transactions on Graphics"}

MACRO {toms} {"ACM Transactions on Mathematical Software"}

MACRO {toois} {"ACM Transactions on Office Information Systems"}

MACRO {toplas} {"ACM Transactions on Programming Languages and Systems"}

MACRO {tcs} {"Theoretical Computer Science"}

READ

INTEGERS { len }

FUNCTION {chop.word}
{ 's :=
  'len :=
  s #1 len substring$ =
    { s len #1 + global.max$ substring$ }
    's
  if$
}

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

FUNCTION {sort.format.title}
{ 't :=
  "A " #2
    "An " #3
      "The " #4
	"Rev. of " #8
	  "Rev.~of " #8
	    "Rev.\ of " #9
	    t chop.word
	  chop.word
	chop.word
      chop.word
    chop.word
  chop.word
  sortify
  #1 global.max$ substring$
}

FUNCTION {author.sort}
{ author empty$
    { key empty$
	{ "to sort, need author or key in " cite$ * warning$
	  ""
	}
	{ key sortify }
      if$
    }
    { author sort.format.names }
  if$
}

FUNCTION {author.editor.sort}
{ author empty$
    { 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$
}

FUNCTION {author.organization.sort}
{ author empty$
    { organization empty$
	{ key empty$
	    { "to sort, need author, organization, or key in " cite$ * warning$
	      ""
	    }
	    { key sortify }
	  if$
	}
	{ "The " #4 organization chop.word sortify }
      if$
    }
    { author sort.format.names }
  if$
}

FUNCTION {editor.organization.sort}
{ editor empty$
    { organization empty$
	{ key empty$
	    { "to sort, need editor, organization, or key in " cite$ * warning$
	      ""
	    }
	    { key sortify }
	  if$
	}
	{ "The " #4 organization chop.word sortify }
      if$
    }
    { editor sort.format.names }
  if$
}

FUNCTION {presort}
{ type$ "book" =
  type$ "inbook" =
  or
    'author.editor.sort
    { type$ "proceedings" =
	'editor.organization.sort
	{ type$ "manual" =
	    'author.organization.sort
	    'author.sort
	  if$
	}
      if$
    }
  if$
  duplicate$ empty$ 'skip$ { "    " * } if$
  key empty$
    { title field.or.null sort.format.title * "    " * booktitle field.or.null sort.format.title * }
    { key sortify * }
  if$
  "    "
  *
  year field.or.null sortify
  *
  #1 entry.max$ substring$
  'sort.key$ :=
}

ITERATE {presort}

SORT

STRINGS {a b}

FUNCTION {init.vars}
{ #0 int.to.chr$ 'a :=
  #0 int.to.chr$ 'b :=
  #0 'i :=
  #0 'j :=
}

% Function name is not strictly accurate
FUNCTION {forward.author.dup.check}
{
  url empty$ urlpath empty$ urldate empty$ and and
    'skip$
    { url.used #1 + 'url.used := }
  if$
  crossref empty$ 'skip$ { citealp.used #1 + 'citealp.used := } if$
  author empty$
    { editor empty$ { organization field.or.null 'a := }{ editor 'a :=} if$}
    { author 'a := }
  if$
  %'a :=
  a empty$
    'skip$
    { b "{ff~}{vv~}{ll}{, jj}" format.names.custom purify$
      a "{ff~}{vv~}{ll}{, jj}" format.names.custom purify$ =
	{ #1 'author.duplicate := }
	{ #0 'author.duplicate := }
      if$
      a 'b :=
    }
  if$
}

FUNCTION {forward.author.share.check}
%
% Pass through all entries, comparing current author to last one.
%
{ author.duplicate author empty$ or
  { author.duplicate { i 'author.shared := }{} if$}
  { b "{f.~}{vv~}{ll}{, jj}" format.names.custom purify$
    author field.or.null "{f.~}{vv~}{ll}{, jj}" format.names.custom purify$ =
    { author.flast 'author.shared := }
    { b "{vv~}{ll}" format.names.custom purify$
      author field.or.null "{vv~}{ll}" format.names.custom purify$ =
      { author.last 'author.shared := }
      { author.different 'author.shared := }
      if$
    }
    if$
    author.shared 'i :=
    author field.or.null 'b :=
  }
  if$
}

FUNCTION {reverse.author}
{ 
  author.duplicate
  { author.shared i <
      { i 'author.shared := }
      {}
    if$
  }
  { author.shared i <
      { author.shared i 'author.shared := 'i := }
      { author.shared 'i := }
    if$
  }
  if$
  j #1 = author.duplicate not and { #2 'author.duplicate := } {} if$
  author.duplicate 'j :=
}

EXECUTE {init.consts}

EXECUTE {init.vars}
ITERATE {forward.author.dup.check}

EXECUTE {init.vars}
ITERATE {forward.author.share.check}

EXECUTE {init.vars}
REVERSE {reverse.author}

%INTEGERS { number.label longest.label.width }

%FUNCTION {initialize.longest.label}
%{ "" 'longest.label :=
%  #1 'number.label :=
%  #0 'longest.label.width :=
%}

%FUNCTION {longest.label.pass}
%{ number.label int.to.str$ 'label :=
%  number.label #1 + 'number.label :=
%  label width$ longest.label.width >
%    { label 'longest.label :=
%      label width$ 'longest.label.width :=
%    }
%    'skip$
%  if$
%}

%EXECUTE {initialize.longest.label}

%ITERATE {longest.label.pass}

FUNCTION {begin.bib}
{
  citealp.used
    { "\expandafter\ifx\csname citealp\endcsname\relax\let\citealp\cite\fi"
      write$ newline$
    }
    'skip$
  if$
  url.used
    { "\providecommand\url[1]{\texttt{#1}}"
      write$ newline$
      "\providecommand\theurldate{\today}"
      write$ newline$
    }
    'skip$
  if$
  preamble$ empty$
    'skip$
    { preamble$ write$ newline$ }
  if$
  "\begin{thebibliography}{\rule{.5in}{0pt}}" write$ newline$
}

EXECUTE {begin.bib}

ITERATE {call.type$}

FUNCTION {end.bib}
{ newline$
  "\end{thebibliography}" write$ newline$
}

EXECUTE {end.bib}
