Suvayu Ali <fatkasuvayu+li...@gmail.com> writes:

>> +(defun org-latex-guess-polyglossia-language (header info)
>> +  "Set the Polyglossia language according to the LANGUAGE keyword.
>
> I'm in two-minds about this.  On the one had it is a nice thing, but on
> the other hand, it prevents the more fine grained control offered by
> polyglossia.  For example, I used this in my setup before:
>
>   \setdefaultlanguage[variant=british]{english}

The attached supports Polyglossia better.  Note, the last loaded language
is the main one (as babel).  Perhaps org-latex-variant-file-string should
be less annoying (not a file-variable).

Test file:

     #+title: test xetex
     #+language: en-gb
     #+latex_variant: xelatex

     * header
       add polyglossia

     #+BEGIN_SRC emacs-lisp
     (add-to-list 'org-latex-packages-alist
                   '("AUTO, french" "polyglossia" nil ("lualatex" "xelatex")))
     #+END_SRC

Output:

    % -*- LaTeX-command: xelatex -*-
    % Created 2015-07-08 miƩ 20:32
    \documentclass[11pt]{article}
    \usepackage{graphicx}
    \usepackage{grffile}
    \usepackage{longtable}
    \usepackage{wrapfig}
    \usepackage{rotating}
    \usepackage[normalem]{ulem}
    \usepackage{amsmath}
    \usepackage{textcomp}
    \usepackage{amssymb}
    \usepackage{capt-of}
    \usepackage{hyperref}
    \usepackage{polyglossia}
    \setmainlanguage{french}
    \setotherlanguage[variant=uk]{english}
    \author{Rasmus}
    \date{\today}
    \title{test xetex}
    \hypersetup{
     pdfauthor={Rasmus},
     pdftitle={test xetex},
     pdfkeywords={},
     pdfsubject={},
     pdfcreator={Emacs 25.0.50.1 (Org mode 8.3beta)}, 
     pdflang={British}}
    \begin{document}

    \maketitle
    \tableofcontents


    \section{add polyglossia}
    \label{sec:orgheadline1}
    \begin{verbatim}
    (add-to-list 'org-latex-packages-alist
                  '("AUTO, french" "polyglossia" nil ("lualatex" "xelatex")))
    \end{verbatim}
    \end{document}


-- 
Dung makes an excellent fertilizer
>From 96dcb2ab15ddfce1f95560f49076187508b2ef43 Mon Sep 17 00:00:00 2001
From: Rasmus <ras...@gmx.us>
Date: Wed, 8 Jul 2015 14:44:56 +0200
Subject: [PATCH 09/10] ox-latex: Support TeX variants

* org.el (org-latex-default-packages-alist): Only use inputenc
and fontenc in pdftex.
* ox-latex.el (latex): Add :latex-tex-variant.
(org-latex-variant): New defcustom.
(org-latex-variants): New defconst.
(org-latex--remove-packages): New function.
(org-latex-template): Use new function.
(org-latex-pdf-process): Update to use %latex shorthand.
(org-latex-compile): Check and use intended compiler.

Suggested-by: Suvayu Ali <fatkasuvayu+li...@gmail.com>
<http://permalink.gmane.org/gmane.emacs.orgmode/98921>
---
 lisp/org.el      |   4 +-
 lisp/ox-latex.el | 133 ++++++++++++++++++++++++++++++++++++-------------------
 2 files changed, 89 insertions(+), 48 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index f10ccf9..2721646 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -4069,8 +4069,8 @@ header, or they will be appended."
 	  (default-value var)))
 
 (defcustom org-latex-default-packages-alist
-  '(("AUTO" "inputenc"  t)
-    ("T1"   "fontenc"   t)
+  '(("AUTO" "inputenc"  t ("pdflatex"))
+    ("T1"   "fontenc"   t ("pdflatex"))
     (""     "graphicx"  t)
     (""     "grffile"   t)
     (""     "longtable" nil)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 0851066..8c1cd9f 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -144,6 +144,7 @@
     (:latex-text-markup-alist nil nil org-latex-text-markup-alist)
     (:latex-title-command nil nil org-latex-title-command)
     (:latex-toc-command nil nil org-latex-toc-command)
+    (:latex-variant "LATEX_VARIANT" nil org-latex-variant)
     ;; Redefine regular options.
     (:date "DATE" nil "\\today" parse)))
 
@@ -395,6 +396,28 @@ references."
   :version "25.1"
   :package-version '(Org . "8.3"))
 
+(defcustom org-latex-variant "pdflatex"
+  "Tex variant to use."
+  :group 'org-export-latex
+  :type '(choice
+	  (cons :tag "pdfLaTeX" "pdflatex")
+	  (cons :tag "XeLaTeX"  "xelatex")
+	  (cons :tag "luaLaTeX" "lualatex"))
+  :version "25.1"
+  :package-version '(Org . "8.3"))
+
+(defcustom org-latex-variant-file-string "%% -*- LaTeX-command: %s -*-\n"
+  "Variant format-string written to file.
+Default value is a local variable for AUCTeX."
+  :group 'org-export-latex
+  :type 'string
+  :version "25.1"
+  :package-version '(Org . "8.3")
+  )
+
+(defconst org-latex-variants '("pdflatex" "xelatex" "lualatex")
+  "Known LaTeX variants.")
+
 ;;;; Preamble
 
 (defcustom org-latex-default-class "article"
@@ -1032,14 +1055,15 @@ during latex export it will output
 ;;;; Compilation
 
 (defcustom org-latex-pdf-process
-  '("pdflatex -interaction nonstopmode -output-directory %o %f"
-    "pdflatex -interaction nonstopmode -output-directory %o %f"
-    "pdflatex -interaction nonstopmode -output-directory %o %f")
+  '("%latex -interaction nonstopmode -output-directory %o %f"
+    "%latex -interaction nonstopmode -output-directory %o %f"
+    "%latex -interaction nonstopmode -output-directory %o %f")
   "Commands to process a LaTeX file to a PDF file.
 This is a list of strings, each of them will be given to the
 shell as a command.  %f in the command will be replaced by the
 full file name, %b by the file base name (i.e. without directory
-and extension parts) and %o by the base directory of the file.
+and extension parts), %o by the base directory of the file,
+and %latex is the latex variant (see `org-latex-variant').
 
 The reason why this is a list is that it usually takes several
 runs of `pdflatex', maybe mixed with a call to `bibtex'.  Org
@@ -1047,18 +1071,8 @@ does not have a clever mechanism to detect which of these
 commands have to be run to get to a stable result, and it also
 does not do any error checking.
 
-By default, Org uses 3 runs of `pdflatex' to do the processing.
-If you have texi2dvi on your system and if that does not cause
-the infamous egrep/locale bug:
-
-     http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
-
-then `texi2dvi' is the superior choice as it automates the LaTeX
-build process by calling the \"correct\" combinations of
-auxiliary programs.  Org does offer `texi2dvi' as one of the
-customize options.  Alternatively, `rubber' and `latexmk' also
-provide similar functionality.  The latter supports `biber' out
-of the box.
+Consider a smart LaTeX compiler such as `texi2dvi' or `latexmk',
+which calls the \"correct\" combinations of auxiliary programs.
 
 Alternatively, this may be a Lisp function that does the
 processing, so you could use this to apply the machinery of
@@ -1068,36 +1082,24 @@ file name as its single argument."
   :type '(choice
 	  (repeat :tag "Shell command sequence"
 		  (string :tag "Shell command"))
-	  (const :tag "2 runs of pdflatex"
-		 ("pdflatex -interaction nonstopmode -output-directory %o %f"
-		   "pdflatex -interaction nonstopmode -output-directory %o %f"))
-	  (const :tag "3 runs of pdflatex"
-		 ("pdflatex -interaction nonstopmode -output-directory %o %f"
-		   "pdflatex -interaction nonstopmode -output-directory %o %f"
-		   "pdflatex -interaction nonstopmode -output-directory %o %f"))
-	  (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
-		 ("pdflatex -interaction nonstopmode -output-directory %o %f"
-		   "bibtex %b"
-		   "pdflatex -interaction nonstopmode -output-directory %o %f"
-		   "pdflatex -interaction nonstopmode -output-directory %o %f"))
-	  (const :tag "2 runs of xelatex"
-		 ("xelatex -interaction nonstopmode -output-directory %o %f"
-		  "xelatex -interaction nonstopmode -output-directory %o %f"))
-	  (const :tag "3 runs of xelatex"
-		 ("xelatex -interaction nonstopmode -output-directory %o %f"
-		  "xelatex -interaction nonstopmode -output-directory %o %f"
-		  "xelatex -interaction nonstopmode -output-directory %o %f"))
-	  (const :tag "xelatex,bibtex,xelatex,xelatex"
-		 ("xelatex -interaction nonstopmode -output-directory %o %f"
+	  (const :tag "2 runs of latex"
+		 ("%latex -interaction nonstopmode -output-directory %o %f"
+		  "%latex -interaction nonstopmode -output-directory %o %f"))
+	  (const :tag "3 runs of latex"
+		 ("%latex -interaction nonstopmode -output-directory %o %f"
+		  "%latex -interaction nonstopmode -output-directory %o %f"
+		  "%latex -interaction nonstopmode -output-directory %o %f"))
+	  (const :tag "latex,bibtex,latex,latex"
+		 ("%latex -interaction nonstopmode -output-directory %o %f"
 		  "bibtex %b"
-		  "xelatex -interaction nonstopmode -output-directory %o %f"
-		  "xelatex -interaction nonstopmode -output-directory %o %f"))
+		  "%latex -interaction nonstopmode -output-directory %o %f"
+		  "%latex -interaction nonstopmode -output-directory %o %f"))
 	  (const :tag "texi2dvi"
-		 ("texi2dvi -p -b -V %f"))
+		 ("LATEX=%latex texi2dvi -p -b -V %f"))
 	  (const :tag "rubber"
 		 ("rubber -d --into %o %f"))
 	  (const :tag "latexmk"
-		 ("latexmk -g -pdf %f"))
+		 ("latexmk -g -pdflatex=%latex %f"))
 	  (function)))
 
 (defcustom org-latex-logfiles-extensions
@@ -1337,6 +1339,29 @@ Return the new header."
 				       (if (listp lang) (nth 1 lang) lang))))))))
 	 t t header 0)))))
 
+(defun org-latex--remove-packages (pkg-alist info)
+  "Remove packages based on the current TeX variant.
+
+If the fourth argument of an element is set in pkg-alist, and it
+is not a member of the tex variant of the document, the packages
+is removed.  See also `org-latex-variant'.
+
+Return modified pkg-alist."
+  (let ((variant (org-trim (or (plist-get info :latex-variant) ""))))
+    (if (member-ignore-case variant org-latex-variants)
+	(delq nil
+	      (mapcar (lambda (pkg)
+			(unless
+			    (and (listp pkg)
+				 (let ((third (nth 3 pkg)))
+				   (and third
+					(not (member-ignore-case
+					      variant
+					      (if (listp third) third (list third)))))))
+			  pkg))
+		      pkg-alist))
+      pkg-alist)))
+
 (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."
@@ -1476,6 +1501,13 @@ holding export options."
   (let ((title (org-export-data (plist-get info :title) info))
 	(spec (org-latex--format-spec info)))
     (concat
+     ;; LaTeX variant
+     (let ((variant
+	    (car (member-ignore-case
+		  (org-trim (or (plist-get info :latex-variant) ""))
+		  org-latex-variants))))
+       (when variant
+	 (format org-latex-variant-file-string variant)))
      ;; Time-stamp.
      (and (plist-get info :time-stamp-file)
 	  (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
@@ -1497,8 +1529,9 @@ holding export options."
 	    (org-element-normalize-string
 	     (org-splice-latex-header
 	      document-class-string
-	      org-latex-default-packages-alist
-	      org-latex-packages-alist nil
+	      (org-latex--remove-packages org-latex-default-packages-alist info)
+	      (org-latex--remove-packages org-latex-packages-alist info)
+	      nil
 	      (concat (org-element-normalize-string
 		       (plist-get info :latex-header))
 		      (plist-get info :latex-header-extra)))))
@@ -3418,6 +3451,12 @@ create a log buffer and do not bother removing log files.
 Return PDF file name or an error if it couldn't be produced."
   (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
 	 (full-name (file-truename texfile))
+	 (compiler (or (with-temp-buffer
+			 (save-excursion (insert-file-contents full-name))
+			 (when (search-forward-regexp
+				(regexp-opt org-latex-variants) (line-end-position) t)
+			   (match-string 0)))
+		       "pdflatex"))
 	 (out-dir (file-name-directory texfile))
 	 ;; Properly set working directory for compilation.
 	 (default-directory (if (file-name-absolute-p texfile)
@@ -3440,11 +3479,13 @@ Return PDF file name or an error if it couldn't be produced."
 	  (dolist (command org-latex-pdf-process)
 	    (shell-command
 	     (replace-regexp-in-string
-	      "%b" (shell-quote-argument base-name)
+	      "%latex" (shell-quote-argument compiler)
 	      (replace-regexp-in-string
-	       "%f" (shell-quote-argument full-name)
+	       "%b" (shell-quote-argument base-name)
 	       (replace-regexp-in-string
-		"%o" (shell-quote-argument out-dir) command t t) t t) t t)
+		"%f" (shell-quote-argument full-name)
+		(replace-regexp-in-string
+		 "%o" (shell-quote-argument out-dir) command t t) t t) t t) t)
 	     outbuf))
 	  ;; Collect standard errors from output buffer.
 	  (setq warnings (and (not snippet)
-- 
2.4.5

>From 9a91650b444a737b1f627e40761b63dc6552e6f0 Mon Sep 17 00:00:00 2001
From: Rasmus <ras...@gmx.us>
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 <fatkasuvayu+li...@gmail.com>
<http://permalink.gmane.org/gmane.emacs.orgmode/98921>
---
 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

Reply via email to