\ProvidesFile{biblatex-gost.def}
[2014/11/28\space v1.1\space biblatex-gost styles]

% these declarations should be here, not in lbx, to be initialized for all languages
\NewBibliographyString{
  involumes,
  geneditor,
  geneditors,
  bygeneditor,
  bygeneditortr,
  bygeneditorco,
  bygeneditoran,
  bygeneditorin,
  bygeneditorfo,
  bygeneditoraf,
  bygeneditortrco,
  bygeneditortran,
  bygeneditortrin,
  bygeneditortrfo,
  bygeneditortraf,
  bygeneditorcoin,
  bygeneditorcofo,
  bygeneditorcoaf,
  bygeneditoranin,
  bygeneditoranfo,
  bygeneditoranaf,
  bygeneditortrcoin,
  bygeneditortrcofo,
  bygeneditortrcoaf,
  bygeneditortranin,
  bygeneditortranfo,
  bygeneditortranaf,
  bycompilertr,
  bycompilerco,
  bycompileran,
  bycompilerin,
  bycompilerfo,
  bycompileraf,
  bycompilertrco,
  bycompilertran,
  bycompilertrin,
  bycompilertrfo,
  bycompilertraf,
  bycompilercoin,
  bycompilercofo,
  bycompilercoaf,
  bycompileranin,
  bycompileranfo,
  bycompileranaf,
  bycompilertrcoin,
  bycompilertrcofo,
  bycompilertrcoaf,
  bycompilertranin,
  bycompilertranfo,
  bycompilertranaf,
  gecompiler,
  gecompilers,
  bygecompiler,
  bygecompilertr,
  bygecompilerco,
  bygecompileran,
  bygecompilerin,
  bygecompilerfo,
  bygecompileraf,
  bygecompilertrco,
  bygecompilertran,
  bygecompilertrin,
  bygecompilertrfo,
  bygecompilertraf,
  bygecompilercoin,
  bygecompilercofo,
  bygecompilercoaf,
  bygecompileranin,
  bygecompileranfo,
  bygecompileranaf,
  bygecompilertrcoin,
  bygecompilertrcofo,
  bygecompilertrcoaf,
  bygecompilertranin,
  bygecompilertranfo,
  bygecompilertranaf,
  books,
  parts,
  issues,
  mediavideorecording,
  mediasoundrecording,
  mediagraphic,
  mediacartographic,
  mediakit,
  mediamotionpicture,
  mediamicroform,
  mediamultimedia,
  mediamusic,
  mediaobject,
  mediamanuscript,
  mediatext,
  mediabraille,
  mediaeresource,
  updated,
  systemreq,
  countryru,countryussr,patentru,patentussr,patreqru,patrequssr,invcert,
  requested,published,priority,
  docthesis,phdautoref,docautoref,
}
\DeclareLanguageMapping{russian}{russian-gost}
%
%   Sorting Schemes
%
\DeclareSortingScheme{nty}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{heading}
    \name{sortname}
    \name{author}
    \name{editor}
    \name{translator}
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field{sortyear}
    \field{year}
  }
  \sort{
    \field{sortvolume}
    \field[padside=left,padwidth=4,padchar=0]{volume}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{book}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{part}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{issue}
    \literal{0000}
  }
}

\DeclareSortingScheme{nyvt}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{heading}
    \name{sortname}
    \name{author}
    \name{editor}
    \name{translator}
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field{sortyear}
    \field{year}
  }
  \sort{
    \field{sortvolume}
    \field[padside=left,padwidth=4,padchar=0]{volume}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{book}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{part}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{issue}
    \literal{0000}
  }
  \sort{
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
}

\DeclareSortingScheme{ynt}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{heading}
  }
  \sort{
    \field{sortyear}
    \field{year}
    \literal{9999}
  }
  \sort{
    \name{sortname}
    \name{author}
    \name{editor}
    \name{translator}
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
}

\DeclareSortingScheme{ydnt}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{heading}
  }
  \sort[direction=descending]{
    \field[strside=left,strwidth=4]{sortyear}
    \field[strside=left,strwidth=4]{year}
    \literal{9999}
  }
  \sort{
    \name{sortname}
    \name{author}
    \name{editor}
    \name{translator}
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
}

\DeclareSortingScheme{nyt}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{heading}
    \name{sortname}
    \name{author}
    \name{editor}
    \name{translator}
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field{sortyear}
    \field{year}
  }
  \sort{
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field{sortvolume}
    \field[padside=left,padwidth=4,padchar=0]{volume}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{book}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{part}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{issue}
    \literal{0000}
  }
}

\DeclareSortingScheme{ntvy}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{heading}
    \name{sortname}
    \name{author}
    \name{editor}
    \name{translator}
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field{sortvolume}
    \field[padside=left,padwidth=4,padchar=0]{volume}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{book}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{part}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{issue}
    \literal{0000}
  }
  \sort{
    \field{sortyear}
    \field{year}
  }
}

\DeclareSortingScheme{anyt}{
  \sort{
    \field{presort}
  }
  \sort{
    \field{labelalpha}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{heading}
    \field{sortname}
    \field{author}
    \field{editor}
    \field{translator}
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field{sortyear}
    \field{year}
  }
  \sort{
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{volume}
    \literal{0000}
  }
}

\DeclareSortingScheme{anyvt}{
  \sort{
    \field{presort}
  }
  \sort{
    \field{labelalpha}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{heading}
    \field{sortname}
    \field{author}
    \field{editor}
    \field{translator}
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
  \sort{
    \field{sortyear}
    \field{year}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{volume}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{book}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{part}
    \literal{0000}
  }
  \sort{
    \field[padside=left,padwidth=4,padchar=0]{issue}
    \literal{0000}
  }
  \sort{
    \field{sorttitle}
    \field{maintitle}
    \field{title}
  }
}

\DeclareBibliographyOption{movenames}[true]{%
  \gdef\blx@opt@movenames{#1}%
  \ifstrequal{#1}{true}
    {\DeclareStyleSourcemap{
      \maps[datatype=bibtex]{
        \map[overwrite]{
          \step[fieldsource=author,
            match=\regexp{(.+\s+and\s+){3,}|and\s+others\s*$},
            final]
          \step[fieldsource=options,
            match=\regexp{(.+)},
            replace=\regexp{useauthor=false,$1}]
          \step[fieldsource=options,
            match=\regexp{^$},
            replace=\regexp{useauthor=false}]
        }
        \map{
          \step[fieldsource=author,
            match=\regexp{(.+\s+and\s+){3,}|and\s+others\s*$},
            final]
          \step[fieldset=options, fieldvalue={useauthor=false}]
        }
        \map[overwrite]{
          \step[fieldsource=editor,
            match=\regexp{(.+\s+and\s+){3,}|and\s+others\s*$},
            final]
          \step[fieldsource=options,
            match=\regexp{(.+)},
            replace=\regexp{useeditor=false,$1}]
          \step[fieldsource=options,
            match=\regexp{^$},
            replace=\regexp{useeditor=false}]
        }
        \map{
          \step[fieldsource=editor,
            match=\regexp{(.+\s+and\s+){3,}|and\s+others\s*$},
            final]
          \step[fieldset=options, fieldvalue={useeditor=false}]
        }
        \map[overwrite]{
          \pertype{patent}
          \step[fieldsource=options, final]
          \step[fieldset=options, fieldvalue={useauthor=false,}]
          \step[fieldset=options, origfieldval, append]
        }
        \map{
          \pertype{patent}
          \step[fieldset=options, fieldvalue={useauthor=false}]
        }
      }
     }
    }
   {\DeclareStyleSourcemap{
      \maps[datatype=bibtex]{
        \map[overwrite]{
          \pertype{patent}
          \step[fieldsource=options, final]
          \step[fieldset=options, fieldvalue={useauthor=false,}]
          \step[fieldset=options, origfieldval, append]
        }
        \map{
          \pertype{patent}
          \step[fieldset=options, fieldvalue={useauthor=false}]
        }
      }
    }
   }
}

\DeclareSortExclusion{inbook,incollection,inproceeding,bookinbook,suppbook,suppcollection,inreference}
  {editor,volume,maintitle}

\DeclareLabelname{%
  \field{shortauthor}
  \field{author}
  \field{shorteditor}
  \field{editor}
  \field{translator}
}

\DeclareLabeltitle[book,collection,reference,proceedings]{%
  \field{shorttitle}
  \field{maintitle}
  \field{title}}

\DeclareDataInheritance{*}{*}{%
  \noinherit{heading}}
\DeclareDataInheritance{periodical}{article}{%
  \noinherit{endyear}\noinherit{endmonth}\noinherit{endday}
  \inherit{credits}{journalcredits}}
\DeclareDataInheritance{mvbook,mvcollection,mvproceedings}
  {book,collection,inbook,incollection,inproceedings}{%
  \noinherit{endyear}\noinherit{endmonth}\noinherit{endday}}
\DeclareDataInheritance{mvbook,mvcollection,mvproceedings,mvreference}
  {book,collection,proceedings,reference}{%
  \inherit{shorttitle}{shorttitle}}

\DeclareDataInheritance{book}{inbook,bookinbook,suppbook}{%
  \inherit{translator}{booktranslator}
  \inherit{introduction}{bookintroduction}
  \inherit{commentator}{bookcommentator}
  \inherit{annotator}{bookannotator}
  \inherit{foreword}{bookforeword}
  \inherit{afterword}{bookafterword}
  \inherit{origlanguage}{bookoriglanguage}
  \inherit{credits}{bookcredits}
  \noinherit{pagetotal}}
\DeclareDataInheritance{collection,reference}{incollection,inreference,suppcollection}{%
  \inherit{translator}{booktranslator}
  \inherit{introduction}{bookintroduction}
  \inherit{commentator}{bookcommentator}
  \inherit{annotator}{bookannotator}
  \inherit{foreword}{bookforeword}
  \inherit{afterword}{bookafterword}
  \inherit{origlanguage}{bookoriglanguage}
  \inherit{credits}{bookcredits}
  \noinherit{pagetotal}}
\DeclareDataInheritance{proceedings}{inproceedings}{%
  \inherit{translator}{booktranslator}
  \inherit{introduction}{bookintroduction}
  \inherit{commentator}{bookcommentator}
  \inherit{annotator}{bookannotator}
  \inherit{foreword}{bookforeword}
  \inherit{afterword}{bookafterword}
  \inherit{origlanguage}{bookoriglanguage}
  \inherit{credits}{bookcredits}
  \noinherit{pagetotal}}

% Related

\renewbibmacro*{related:bytranslator}[1]{%
  \entrydata{#1}{%
    \renewbibmacro*{name:hook}[1]{%
      \ifnumequal{\value{listcount}}{1}
        {\begingroup
         \mkrelatedstring%
         \lbx@initnamehook{#1}%
         \endgroup}
        {}}%
    \printnames[bytranslator]{translator}%
    \setunit*{\addspace\bibstring[\mkrelatedstring]{astitle}\space}%
    \usebibmacro{author}%
    \setunit*{\labelnamepunct}%
    \usebibmacro{title}%
    \setunit{\addspace}%
    \printtext[parens]{%
      \printlist{location}%
      \iflistundef{publisher}
        {\setunit*{\addcomma\space}}
        {\setunit*{\addcolon\space}}%
      \printlist{publisher}%
      \setunit*{\addcomma\space}%
      \printdate}}}

\renewbibmacro*{related:multivolume}[1]{%
  \entrydata*{#1}{%
    \usebibmacro{volume+parts}{\newunit}%
    \setunit{\addcolondelim}%
    \ifboolexpr{
      test {\ifnamesequal{author}{savedauthor}}
      or
      test {\ifnameundef{author}}
    }
      {}
      {\usebibmacro{author}%
       \setunit*{\labelnamepunct}}%
    \usebibmacro{title}%
    \setunit{\respdelim}%
    \iflistsequal{credits}{savedcredits}
      {}
      {\usebibmacro{credits}}%
    \setunit*{\resppunct}%
    \ifnamesequal{editor}{savededitor}
      {}
      {\usebibmacro{byeditor}}%
    \setunit*{\resppunct}%
    \ifnamesequal{translator}{savedtranslator}
      {}
      {\usebibmacro{bytranslator+others}}%
    \newunit\newblock
    \usebibmacro{date}%
    \newunit\newblock
    \printfield{pagetotal}%
    \usebibmacro{finentry}}}

\endinput
