1 ;;; polyglossia.el --- AUCTeX style for `polyglossia.sty' version 1.42.0.
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Maintainer: auctex-devel@gnu.org
6 ;; Author: Mosè Giordano <mose@gnu.org>
9 ;; This file is part of AUCTeX.
11 ;; AUCTeX is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; AUCTeX is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with AUCTeX; see the file COPYING. If not, write to the Free
23 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 ;; This file adds support for `polyglossia.sty' version 1.42.0.
32 ;; -- Create language specific styles with names `gloss-<lang>.el'. They
33 ;; should add `text<lang>' macros, `<lang>' environments (`Arabic' for
34 ;; `arabic' language), and the others language-specific commands.
38 (require 'tex) ;Indispensable when compiling the call to `TeX-auto-add-type'.
40 (TeX-auto-add-type "polyglossia-lang" "LaTeX")
42 ;; Self Parsing -- see (info "(auctex)Hacking the Parser").
43 (defvar LaTeX-polyglossia-lang-regexp
44 (concat "\\\\set\\(defaultlanguage\\|mainlanguage\\|otherlanguages?\\)"
45 "[ \t\n\r]*\\(?:\\[\\(.*\\)\\]\\)?[ \t\n\r]*{\\([A-Za-z, ]+\\)}")
46 "Matches languages set with polyglossia macros.")
48 (defvar LaTeX-polyglossia-setkeys-regexp
50 "[ \t\n\r]*{\\([A-Za-z]+\\)}[ \t\n\r]*{\\([^}]*\\)}")
51 "Matches polyglossia languages options set using \"\setkeys\".")
53 (defvar LaTeX-auto-polyglossia-lang nil
54 "Temporary for parsing polyglossia languages.")
56 (defvar LaTeX-auto-polyglossia-setkeys nil
57 "Temporary for parsing polyglossia language options.")
59 (defun LaTeX-polyglossia-prepare ()
60 "Clear some polyglossia variables before use."
61 (setq LaTeX-auto-polyglossia-lang nil
62 LaTeX-auto-polyglossia-setkeys nil
63 LaTeX-polyglossia-lang-list nil))
65 (defun LaTeX-polyglossia-cleanup ()
66 "Move languages and their options from
67 `LaTeX-auto-polyglossia-lang' to `LaTeX-polyglossia-lang-list'."
68 ;; Example: now the value of `LaTeX-auto-polyglossia-lang' is something like
69 ;; '(("danish" "defaultlanguage" "")
70 ;; ("arabic" "otherlanguage" "locale=tunisia,numerals=maghrib")
71 ;; ("german" "otherlanguage" "spelling=new,script=latin")
72 ;; ("icelandic,brazil,sanskrit" "otherlanguages" ""))
73 ;; We want to end up with a list like
74 ;; '(("danish" "defaultlanguage")
75 ;; ("arabic" "otherlanguage" "locale=tunisia" "numerals=maghrib")
76 ;; ("german" "otherlanguage" "spelling=new" "script=latin")
77 ;; ("icelandic" "otherlanguages")
78 ;; ("brazil" "otherlanguages")
79 ;; ("sanskrit" "otherlanguages" "script=Devanagari"))
80 ;; with "script=Devanagari" option to "sanskrit" language set using
82 ;; In each element of the alist, the key is the language, the second value is
83 ;; the polyglossia command which set the language, the rest of values is the
84 ;; list of options given to the language.
90 ;; `opts' is the string of options for `language', set using
91 ;; "\setdefaultlanguage" or "\setotherlanguage".
92 (setq opts (cdr (cdr elt)))
93 ;; `otheropts' is the string of options for `language' set using
96 (car (cdr (assoc language LaTeX-auto-polyglossia-setkeys))))
98 'LaTeX-polyglossia-lang-list
100 (list language) (list (nth 1 elt))
101 (unless (equal opts '(""))
102 (LaTeX-listify-package-options (car opts)))
103 (if otheropts (LaTeX-listify-package-options otheropts))) t))
104 (LaTeX-listify-package-options (car elt))))
105 LaTeX-auto-polyglossia-lang)))
107 (add-hook 'TeX-auto-prepare-hook #'LaTeX-polyglossia-prepare)
108 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-polyglossia-cleanup)
109 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
110 ;; Run style hooks for every active language. This *has* to be done after
112 (add-hook 'TeX-update-style-hook #'LaTeX-polyglossia-load-languages t)
114 (defvar LaTeX-polyglossia-language-list
115 '("albanian" "amharic" "arabic" "armenian" "asturian" "bahasai" "bahasam"
116 "basque" "bengali" "brazil" "breton" "bulgarian" "catalan" "coptic"
117 "croatian" "czech" "danish" "divehi" "dutch" "english" "esperanto"
118 "estonian" "farsi" "finnish" "french" "friulan" "galician" "german" "greek"
119 "hebrew" "hindi" "icelandic" "interlingua" "irish" "italian" "kannada"
120 "khmer" "korean" "lao" "latin" "latvian" "lithuanian" "lsorbian" "magyar"
121 "malayalam" "marathi" "nko" "norsk" "nynorsk" "occitan" "piedmontese"
122 "polish" "portuges" "romanian" "romansh" "russian" "samin" "sanskrit"
123 "scottish" "serbian" "slovak" "slovenian" "spanish" "swedish" "syriac"
124 "tamil" "telugu" "thai" "tibetan" "turkish" "turkmen" "ukrainian" "urdu"
125 "usorbian" "vietnamese" "welsh")
126 "List of languages supported by the polyglossia LaTeX package.")
128 (defun LaTeX-polyglossia-active-languages ()
129 "Return a list of polyglossia languages used in the document.
130 The last language is the default one."
131 (let (active-languages default)
134 (setq default (or (string-equal "defaultlanguage" (nth 1 elt))
135 (string-equal "mainlanguage" (nth 1 elt))))
136 ;; Append the language to the list if it's the default one.
137 (add-to-list 'active-languages (car elt) default))
138 LaTeX-polyglossia-lang-list)
141 (defun LaTeX-polyglossia-lang-option-member (language option)
142 "Return non-nil if OPTION has been given to polyglossia LANGUAGE.
143 The value is actually the tail of the list of options given to LANGUAGE."
144 (member option (cdr (cdr (assoc language LaTeX-polyglossia-lang-list)))))
146 (defun LaTeX-arg-polyglossia-lang (_optional default multiple setkeys)
147 "Prompt for language and its options with completion and insert them
150 This function is triggered by \"\setdefaultlanguage\",
151 \"\setotherlanguage\", \"\setotherlanguages\", and \"\setkeys\"
152 macros by polyglossia package.
154 OPTIONAL is ignored, if DEFAULT is non-nil treat inserted
155 language as default, if MULTIPLE is non-nil prompt for multiple
156 languages, if SETKEYS is non-nil insert options as second
158 ;; DEFAULT = t , MULTIPLE = nil, SETKEYS = nil: "\setdefaultlanguage".
159 ;; DEFAULT = nil, MULTIPLE = nil, SETKEYS = nil: "\setotherlanguage".
160 ;; DEFAULT = nil, MULTIPLE = t , SETKEYS = nil: "\setotherlanguages".
161 ;; DEFAULT = nil, MULTIPLE = nil, SETKEYS = t : "\setkeys".
162 (let ((language (funcall
164 'TeX-completing-read-multiple
166 (if multiple "Languages: " "Language: ")
168 (LaTeX-polyglossia-active-languages)
169 LaTeX-polyglossia-language-list)))
172 (mapc (lambda (elt) (TeX-run-style-hooks (concat "gloss-" elt)))
174 (TeX-run-style-hooks (concat "gloss-" language)))
175 ;; "\setotherlanguages" doesn't take options, don't prompt for them.
178 (setq var (intern (format "LaTeX-polyglossia-%s-options-list" language)))
179 (if (and (boundp var) (symbol-value var))
180 ;; "\setdefaultlanguage" and "\setotherlanguage" use `options'
181 ;; as first optional argument; "\setkeys" uses `options' as
182 ;; second mandatory argument.
183 (TeX-read-key-val (not setkeys) (symbol-value var))
184 ;; When `LaTeX-polyglossia-<lang>-options-list' is nil or not
185 ;; defined, don't prompt for options.
188 (let ((TeX-arg-opening-brace LaTeX-optop)
189 (TeX-arg-closing-brace LaTeX-optcl))
190 (TeX-argument-insert options t)))
192 (setq language (mapconcat 'identity language ",")))
193 (TeX-argument-insert language nil)
195 (TeX-argument-insert options nil))))
197 (defun LaTeX-arg-polyglossiasetup-options (optional)
198 "Prompt for setup options of polyglossia package.
199 If OPTIONAL is non-nil, insert the resulting value as an optional
200 argument, otherwise as a mandatory one."
201 (TeX-arg-key-val optional
202 '(("language") ;; TODO: add completion in `fontspec.el', see
203 ;; "\newfontlanguage"s in `fontspec-xetex.sty'.
205 ("script") ;; TODO: add completion in `fontspec.el', see
206 ;; "\newfontscript"s in `fontspec-xetex.sty'.
207 ("direction" ("RL" "LR"))
211 ("frenchspacing" ("true" "false"))
212 ("indentfirst" ("true" "false"))
213 ("fontsetup" ("true" "false"))
214 ;; The following options aren't already implemented but are
215 ;; present in `polyglossia.sty' comments.
216 ;; ("nouppercase" ("true" "false"))
221 (defun LaTeX-polyglossia-load-languages ()
222 "Load style files of babel active languages."
223 (mapc (lambda (elt) (TeX-run-style-hooks (concat "gloss-" elt)))
224 (LaTeX-polyglossia-active-languages)))
229 (TeX-check-engine-add-engines 'luatex 'xetex)
231 `(,LaTeX-polyglossia-lang-regexp (3 1 2) LaTeX-auto-polyglossia-lang))
233 `(,LaTeX-polyglossia-setkeys-regexp (1 2) LaTeX-auto-polyglossia-setkeys))
234 (TeX-run-style-hooks "etoolbox" "makecmds" "xkeyval" "fontspec")
236 '("setdefaultlanguage" (LaTeX-arg-polyglossia-lang t nil nil))
237 '("setmainlanguage" (LaTeX-arg-polyglossia-lang t nil nil))
238 '("setotherlanguage" (LaTeX-arg-polyglossia-lang nil nil nil))
239 '("setotherlanguages" (LaTeX-arg-polyglossia-lang nil t nil))
240 '("setkeys" (LaTeX-arg-polyglossia-lang nil nil t ))
241 '("PolyglossiaSetup" (TeX-arg-eval completing-read "Language: "
242 (LaTeX-polyglossia-active-languages))
243 LaTeX-arg-polyglossiasetup-options)
244 "selectbackgroundlanguage"
245 '("resetdefaultlanguage" ["argument"] 1)
253 (TeX-declare-expert-macros
255 "PolyglossiaSetup" "selectbackgroundlanguage" "resetdefaultlanguage"
256 "normalfontlatin" "rmfamilylatin" "sffamilylatin" "ttfamilylatin"
257 "selectlanguage" "foreignlanguage")
260 (when (and (featurep 'font-latex)
261 (eq TeX-install-font-lock 'font-latex-setup))
262 (font-latex-add-keywords '(("setdefaultlanguage" "[{")
263 ("setmainlanguage" "[{")
264 ("setotherlanguage" "[{")
265 ("setotherlanguages" "{")
270 ;; TODO: move each option variable in its specific `gloss-<lang>.el' file.
271 (defvar LaTeX-polyglossia-arabic-options-list
272 '(("calendar" ("gregorian" "islamic"))
273 ("locale" ("default" "mashriq" "libya" "algeria" "tunisia" "morocco" "mauritania"))
274 ("numerals" ("mashriq" "maghrib"))
275 ("abjadjimnotail" ("false" "true")))
276 "Arabic language options for the polyglossia package.")
278 (defvar LaTeX-polyglossia-bengali-options-list
279 '(("numerals" ("Western" "Devanagari"))
280 ("changecounternumbering" ("true" "false")))
281 "Bengali language options for the polyglossia package.")
283 (defvar LaTeX-polyglossia-catalan-options-list
284 '(("babelshorthands" ("true" "false")))
285 "Catalan language options for the polyglossia package.")
287 (defvar LaTeX-polyglossia-dutch-options-list
288 '(("babelshorthands" ("true" "false")))
289 "Dutch language options for the polyglossia package.")
291 (defvar LaTeX-polyglossia-english-options-list
292 '(("variant" ("american" "usmax" "british" "australian" "newzealand"))
293 ("ordinalmonthday" ("true" "false")))
294 "English language options for the polyglossia package.")
296 (defvar LaTeX-polyglossia-farsi-options-list
297 '(("numerals" ("western" "eastern"))
298 ;; ("locale") ;; not yet implemented
299 ;; ("calendar") ;; not yet implemented
301 "Farsi language options for the polyglossia package.")
303 (defvar LaTeX-polyglossia-german-options-list
304 '(("variant" ("german" "austrian" "swiss"))
305 ("spelling" ("new" "old"))
306 ("latesthyphen" ("true" "false"))
307 ("babelshorthands" ("true" "false"))
308 ("script" ("latin" "fraktur")))
309 "German language options for the polyglossia package.")
311 (defvar LaTeX-polyglossia-greek-options-list
312 '(("variant" ("monotonic" "polytonic" "ancient"))
313 ("numerals" ("greek" "arabic"))
314 ("attic" ("true" "false")))
315 "Greek language options for the polyglossia package.")
317 (defvar LaTeX-polyglossia-hebrew-options-list
318 '(("numerals" ("hebrew" "arabic"))
319 ("calendar" ("hebrew" "gregorian")))
320 "Hebrew language options for the polyglossia package.")
322 (defvar LaTeX-polyglossia-hindi-options-list
323 '(("numerals" ("Western" "Devanagari")))
324 "Hindi language options for the polyglossia package.")
326 (defvar LaTeX-polyglossia-lao-options-list
327 '(("numerals" ("lao" "arabic")))
328 "Lao language options for the polyglossia package.")
330 (defvar LaTeX-polyglossia-russian-options-list
331 '(("spelling" ("modern" "old"))
332 ("babelshorthands" ("true" "false")))
333 "Russian language options for the polyglossia package.")
335 (defvar LaTeX-polyglossia-sanskrit-options-list
336 '(("Script" ("Devanagari")))
337 "Sanskrit language options for the polyglossia package.")
339 (defvar LaTeX-polyglossia-serbian-options-list
340 '(("script" ("cyrillic" "latin")))
341 "Serbian language options for the polyglossia package.")
343 (defvar LaTeX-polyglossia-syriac-options-list
344 '(("numerals" ("western" "eastern" "abjad")))
345 "Syriac language options for the polyglossia package.")
347 (defvar LaTeX-polyglossia-thai-options-list
348 '(("numerals" ("thai" "arabic")))
349 "Thai language options for the polyglossia package.")
351 (defvar LaTeX-polyglossia-package-options
352 '("babelshorthands" "localmarks" "nolocalmarks" "quiet")
353 "Package options for the polyglossia package.")
355 ;;; polyglossia.el ends here