\documentclass[11pt]{article}

\usepackage{geometry}
\geometry{a4paper}

\usepackage{fontspec}
\setmainfont{TeX Gyre Termes}
\setmonofont{TeX Gyre Cursor}

\usepackage{csquotes}

\usepackage{polyglossia}
\setdefaultlanguage[variant=british]{english}

\usepackage{setspace}
\doublespacing


\usepackage[hidelinks]{hyperref}


\setlength{\parindent}{0em}
\setlength{\parskip}{1em}

\usepackage[style=authoryear-ibid,alldates=long,giveninits=true,dashed=false,datelabel=year,datecirca=true,mergedate=maximum,maxbibnames=10,ibidpage=true]{biblatex}

\renewbibmacro*{volume+number+eid}{ % puts issue number in brackets
  \printfield{volume}
  \printfield{number}
  \setunit{\addcomma\space}
  \printfield{eid}}
\DeclareFieldFormat[article]{number}{\mkbibparens{#1}}

\renewbibmacro{in:}{ % gets rid of In for journal articles
  \ifentrytype{article}
    {}
    {\printtext{\bibstring{in}\intitlepunct}}}

\DefineBibliographyStrings{english}{ % changes visited on to accessed
urlseen={Accessed},
}

\DeclareFieldFormat[article]{pages}{#1} % gets rid of pp. for journal articles
\DeclareNameAlias{sortname}{family-given} % stops 2nd author being listed first last
\renewcommand*{\intitlepunct}{\space} % deletes colon after In
\AtEveryBibitem{\uspunctuation} % fixes full stop placement

\renewcommand*{\postnotedelim}{\addcolon} % replaces p. in text with colons
\DeclareFieldFormat{postnote}{#1}
\DeclareFieldFormat{multipostnote}{#1}

\DeclareFieldFormat{origdate}{\mkbibbrackets{#1}} % year first published
\renewbibmacro*{cite:labeldate+extradate}{%
  \iffieldundef{origyear}
    {}
    {\printorigdate
     \setunit{\addspace}}%
  \iffieldundef{labelyear}
    {}
    {\printtext[bibhyperref]{\printlabeldateextra}}}

\DeclareCiteCommand{\citeorigyear}
  {\boolfalse{citetracker}%
   \boolfalse{pagetracker}%
   \usebibmacro{prenote}}
  {\printfield{origyear}}
  {\multicitedelim}
  {\usebibmacro{postnote}}

\renewbibmacro*{date+extradate}{%
  \iffieldundef{origyear}
    {}
    {\printorigdate
     \setunit{\addspace}}%
  \iffieldundef{labelyear}
    {}
    {\printtext[parens]{%
       \iflabeldateisdate
         {\printdateextra}
         {\printlabeldateextra}}}}
         
\DeclareSourcemap{
  \maps[datatype=bibtex]{
      \map[overwrite=false]{
          \step[fieldsource=year]
          \step[fieldset=sortyear, origfieldval, final]
          \step[fieldsource=sortyear, match={c. }, replace={}]
    }
  }
}

\bibliography{bib}
\nocite{*}

\begin{document}

\section*{Draft bibliography for Cornish}

\tableofcontents

\printbibliography[heading=bibintoc,title={Introductions and general histories},keyword=corn:gen]

\printbibliography[heading=bibintoc,title={Old Cornish writing and commentary},keyword=corn:oc]

\printbibliography[heading=bibintoc,title={Middle Cornish writing and commentary},keyword=corn:mc]

\printbibliography[heading=bibintoc,title={Late Cornish writing and commentary},keyword=corn:lc]

\printbibliography[heading=bibintoc,title={Revived Cornish writing and commentary},keyword=corn:revc]

\printbibliography[heading=bibintoc,title={Linguistic works on traditional
  Cornish},keyword=corn:tradling]

\printbibliography[heading=bibintoc,title={Linguistic works on revived Cornish},keyword=corn:revling]

\printbibliography[heading=bibintoc,title={The decline of Cornish},keyword=corn:decl]

\printbibliography[heading=bibintoc,title={Textbooks, dictionaries, grammars: Unified Cornish/Unified Cornish Revised/Kernowek Standard},keyword=corn:ucmat]

\printbibliography[heading=bibintoc,title={Textbooks, dictionaries, grammars: Kernewek Kemmyn},keyword=corn:kkmat]

\printbibliography[heading=bibintoc,title={Textbooks, dictionaries, grammars: Modern Cornish},keyword=corn:rlcmat]

\printbibliography[heading=bibintoc,title={Textbooks, dictionaries, grammars: SWF},keyword=corn:swfmat]

\printbibliography[heading=bibintoc,title={Textbooks, dictionaries, grammars: Early and other varieties},keyword=corn:altmat]

\printbibliography[heading=bibintoc,title={Spelling/varieties debate 1: Pre-SWF},keyword=corn:orth2]

\printbibliography[heading=bibintoc,title={Spelling/varieties debate 2: The SWF era},keyword=corn:orth3]

\printbibliography[heading=bibintoc,title={Accounts of antiquarian work and of the early revival},keyword=corn:factrev]

\printbibliography[heading=bibintoc,title={(Critical) accounts of the later revival and
  contemporary use of Cornish},keyword=corn:critrev2]

\printbibliography[heading=bibintoc,title={Methodological},keyword=corn:meth]

\printbibliography[heading=bibintoc,title={Language policy},keyword=corn:pol]

\printbibliography[heading=bibintoc,title={Language technology},keyword=corn:tech]

\printbibliography[heading=bibintoc,title={Phrasebooks and specialist dictionaries},keyword=corn:pop]

\printbibliography[heading=bibintoc,title={Cornwall and Brittany},keyword=corn:bret]

\printbibliography[heading=bibintoc,title={A selection of non-language-focused
  works},keyword=corn:nonling]

\printbibliography[heading=bibintoc,title={Works I have not seen, but which are cited in earlier issues of \textit{The
  year's work in modern language studies}},keyword=corn:unsorted]

\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
