From da8bcb783b1f5fc99be86617762eca42ab2db6ad Mon Sep 17 00:00:00 2001
From: hseg <gesh@gesh.uni.cx>
Date: Mon, 4 Oct 2021 02:34:59 +0300
Subject: [PATCH] Make babel use more idiomatic

* Use `babel`'s bidi implementation
* Remove global `lang` option -- it broke eg hebrew
* Import babel languages individually instead of as package options --
  was broken for greek, hebrew
* Move `header-includes` to after `babel` setup

Closes #7604
---
 data/templates/default.latex | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)

diff --git a/data/templates/default.latex b/data/templates/default.latex
index 27a3fc877..10377f2cd 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -4,11 +4,6 @@
 $if(colorlinks)$
 \PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
 $endif$
-$if(dir)$
-$if(latex-dir-rtl)$
-\PassOptionsToPackage{RTLdocument}{bidi}
-$endif$
-$endif$
 $if(CJKmainfont)$
 \PassOptionsToPackage{space}{xeCJK}
 $endif$
@@ -17,9 +12,6 @@ $endif$
 $if(fontsize)$
   $fontsize$,
 $endif$
-$if(lang)$
-  $babel-lang$,
-$endif$
 $if(papersize)$
   $papersize$paper,
 $endif$
@@ -370,23 +362,28 @@ $if(csl-refs)$
 \newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{#1}\break}
 \newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
 $endif$
-$for(header-includes)$
-$header-includes$
-$endfor$
 $if(lang)$
-\usepackage[$for(babel-otherlangs)$$babel-otherlangs$,$endfor$main=$babel-lang$]{babel}
+\ifXeTeX
+\usepackage[bidi=default]{babel}
+\fi
+\ifLuaTeX
+\usepackage[bidi=basic]{babel}
+\fi
+\babelprovide[main,import]{$babel-lang$}
+$for(babel-otherlangs)$
+\babelprovide[import]{$babel-otherlangs$}
+$endfor$
 % get rid of language-specific shorthands (see #6817):
 \let\LanguageShortHands\languageshorthands
 \def\languageshorthands#1{}
 $endif$
+$for(header-includes)$
+$header-includes$
+$endfor$
 \ifLuaTeX
   \usepackage{selnolig}  % disable illegal ligatures
 \fi
 $if(dir)$
-\ifXeTeX
-  % Load bidi as late as possible as it modifies e.g. graphicx
-  \usepackage{bidi}
-\fi
 \ifPDFTeX
   \TeXXeTstate=1
   \newcommand{\RL}[1]{\beginR #1\endR}