>From 9a91650b444a737b1f627e40761b63dc6552e6f0 Mon Sep 17 00:00:00 2001
From: Rasmus
Date: Wed, 8 Jul 2015 14:12:21 +0200
Subject: [PATCH 08/10] ox-latex: Add polyglossia support
* ox-latex.el (org-latex-guess-polyglossia-language): New function.
(org-latex-polyglossia-language-alist): New defconst.
(org-latex-template): Apply new function.
Suggested-by: Suvayu Ali
---
lisp/ox-latex.el | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 154 insertions(+), 10 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index ff42843..0851066 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -204,6 +204,96 @@
("uk" . "ukrainian"))
"Alist between language code and corresponding Babel option.")
+(defconst org-latex-polyglossia-language-alist
+ '(("am" . "amharic")
+ ("as" . "asturian") ;; questionable abbreviation
+ ("ar" . "arabic")
+ ("bo" . "tibetan")
+ ("bn" . "bengali")
+ ("bg" . "bulgarian")
+ ("br" . "breton")
+ ("bt-br" . "brazilian")
+ ("ca" . "catalan")
+ ("co" . "coptic") ;; questionable abbreviation
+ ("cs" . "czech")
+ ("cy" . "welsh")
+ ("da" . "danish")
+ ("de" . ("german" "german"))
+ ("de-at" . ("german" "austrian"))
+ ("de-de" . ("german" "german"))
+ ("dv" . "divehi")
+ ("el" . "greek")
+ ("en" . ("english" "usmax"))
+ ("en-au" . ("english" "australian"))
+ ("en-gb" . ("english" "uk"))
+ ("en-nz" . ("english" "newzealand"))
+ ("en-us" . ("english" "usmax"))
+ ("eo" . "esperanto")
+ ("es" . "spanish")
+ ("et" . "estonian")
+ ("eu" . "basque")
+ ("fa" . "farsi")
+ ("fi" . "finnish")
+ ("fr" . "french")
+ ("fu" . "friulan")
+ ("ga" . "irish")
+ ("gd" . "scottish")
+ ("gl" . "galician")
+ ("he" . "hebrew")
+ ("hi" . "hindi")
+ ("hr" . "croatian")
+ ("hu" . "magyar")
+ ("hy" . "armenian")
+ ("id" . "bahasai")
+ ("ia" . "interlingua")
+ ("is" . "icelandic")
+ ("it" . "italian")
+ ("kn" . "kannada")
+ ("la" . ("latin" "modern"))
+ ("la-modern" . ("latin" "modern"))
+ ("la-classic" . ("latin" "classic"))
+ ("la-medieval" . ("latin" "medieval"))
+ ("lo" . "lao")
+ ("lt" . "lithuanian")
+ ("lv" . "latvian")
+ ("mr" . "maranthi")
+ ("ml" . "malayalam")
+ ("nl" . "dutch")
+ ("nb" . "norsk")
+ ("nn" . "nynorsk")
+ ("nk" . "nko") ;; questionable abbreviation
+ ("no" . "norsk")
+ ("oc" . "occitan")
+ ("pl" . "polish")
+ ("pm" . "piedmontese") ;; questionable abbreviation
+ ("pt" . "portuges")
+ ("rm" . "romansh")
+ ("ro" . "romanian")
+ ("ru" . "russian")
+ ("sa" . "sanskrit")
+ ("sb" . "usorbian") ;; questionable abbreviation
+ ("sb-upper" . "usorbian") ;; questionable abbreviation
+ ("sb-lower" . "lsorbian") ;; questionable abbreviation
+ ("sk" . "slovak")
+ ("sl" . "slovenian")
+ ("sm" . "samin") ;; questionable abbreviation
+ ("sq" . "albanian")
+ ("sr" . "serbian")
+ ("sv" . "swedish")
+ ("sy" . "syriac") ;; questionable abbreviation
+ ("ta" . "tamil")
+ ("te" . "telugu")
+ ("th" . "thai")
+ ("tk" . "turkmen")
+ ("tr" . "turkish")
+ ("uk" . "ukrainian")
+ ("ur" . "urdu")
+ ("vi" . "vietnamese")
+ )
+ "Alist between language code and corresponding Polyglossia option.")
+
+
+
(defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
("qbordermatrix" . "\\cr")
("kbordermatrix" . "\\\\"))
@@ -1195,6 +1285,58 @@ Return the new header."
", ")
t nil header 1)))))
+(defun org-latex-guess-polyglossia-language (header info)
+ "Set the Polyglossia language according to the LANGUAGE keyword.
+
+HEADER is the LaTeX header string. INFO is the plist used as
+a communication channel.
+
+Insertion of guessed language only happens when the Polyglossia
+package has been explicitly loaded.
+
+The argument to Polyglossia may be \"AUTO\" which is then
+replaced with the language of the document or
+`org-export-default-language'. Note, the language is really set
+using \setdefaultlanguage and not as an option to the package.
+
+Return the new header."
+ (let ((language (plist-get info :language))
+ result)
+ ;; If no language is set or Polyglossia is not loaded, return
+ ;; HEADER as-is.
+ (if (or (not (stringp language))
+ (not (string-match
+ "\\\\usepackage\\[?\\(.*?\\)?\\]?{polyglossia}\n" header)))
+ header
+ (let* ((options (or (org-string-nw-p (match-string 1 header)) "AUTO"))
+ (languages (save-match-data
+ ;; Reverse as the last loaded language is
+ ;; the main language.
+ (reverse
+ (org-split-string
+ (replace-regexp-in-string
+ "AUTO" language options t)
+ ",[ \t]*"))))
+ (main-language-set (string-match-p "\\\\setmainlanguage{.*?}" header)))
+ (replace-match
+ (concat "\\usepackage{polyglossia}\n"
+ (when languages
+ (dolist (langu languages result)
+ (let ((lang (or (assoc langu org-latex-polyglossia-language-alist)
+ langu)))
+ (setq result
+ (concat
+ result
+ (format (if main-language-set
+ "\\setotherlanguage%s{%s}\n"
+ (prog1 "\\setmainlanguage%s{%s}\n"
+ (setq main-language-set t)))
+ (if (listp lang)
+ (format "[variant=%s]" (nth 2 lang))
+ "")
+ (if (listp lang) (nth 1 lang) lang))))))))
+ t t header 0)))))
+
(defun org-latex--find-verb-separator (s)
"Return a character not used in string S.
This is used to choose a separator for constructs like \\verb."
@@ -1349,16 +1491,18 @@ holding export options."
class-options header t nil 1)))))
(if (not document-class-string)
(user-error "Unknown LaTeX class `%s'" class)
- (org-latex-guess-babel-language
- (org-latex-guess-inputenc
- (org-element-normalize-string
- (org-splice-latex-header
- document-class-string
- org-latex-default-packages-alist
- org-latex-packages-alist nil
- (concat (org-element-normalize-string
- (plist-get info :latex-header))
- (plist-get info :latex-header-extra)))))
+ (org-latex-guess-polyglossia-language
+ (org-latex-guess-babel-language
+ (org-latex-guess-inputenc
+ (org-element-normalize-string
+ (org-splice-latex-header
+ document-class-string
+ org-latex-default-packages-alist
+ org-latex-packages-alist nil
+ (concat (org-element-normalize-string
+ (plist-get info :latex-header))
+ (plist-get info :latex-header-extra)))))
+ info)
info)))
;; Possibly limit depth for headline numbering.
(let ((sec-num (plist-get info :section-numbers)))
--
2.4.5