AUCTeX Sync -- New Style Files
[packages] / xemacs-packages / auctex / style / polyglossia.el
1 ;;; polyglossia.el --- AUCTeX style for `polyglossia.sty' version 1.42.0.
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Maintainer: auctex-devel@gnu.org
6 ;; Author: Mosè Giordano <mose@gnu.org>
7 ;; Keywords: tex
8
9 ;; This file is part of AUCTeX.
10
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)
14 ;; any later version.
15
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.
20
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
24 ;; 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This file adds support for `polyglossia.sty' version 1.42.0.
29
30 ;;; TODO:
31
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.
35
36 ;;; Code:
37
38 (require 'tex) ;Indispensable when compiling the call to `TeX-auto-add-type'.
39
40 (TeX-auto-add-type "polyglossia-lang" "LaTeX")
41
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.")
47
48 (defvar LaTeX-polyglossia-setkeys-regexp
49   (concat "\\\\setkeys"
50           "[ \t\n\r]*{\\([A-Za-z]+\\)}[ \t\n\r]*{\\([^}]*\\)}")
51   "Matches polyglossia languages options set using \"\setkeys\".")
52
53 (defvar LaTeX-auto-polyglossia-lang nil
54   "Temporary for parsing polyglossia languages.")
55
56 (defvar LaTeX-auto-polyglossia-setkeys nil
57   "Temporary for parsing polyglossia language options.")
58
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))
64
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
81   ;; "\setkeys".
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.
85   (let (opts otheropts)
86     (mapc
87      (lambda (elt)
88        (mapc
89         (lambda (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
94           ;; "\setkeys".
95           (setq otheropts
96                 (car (cdr (assoc language LaTeX-auto-polyglossia-setkeys))))
97           (add-to-list
98            'LaTeX-polyglossia-lang-list
99            (append
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)))
106
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
111 ;; `TeX-auto-parse'.
112 (add-hook 'TeX-update-style-hook #'LaTeX-polyglossia-load-languages t)
113
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.")
127
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)
132     (mapc
133      (lambda (elt)
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)
139     active-languages))
140
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)))))
145
146 (defun LaTeX-arg-polyglossia-lang (_optional default multiple setkeys)
147   "Prompt for language and its options with completion and insert them
148 as arguments.
149
150 This function is triggered by \"\setdefaultlanguage\",
151 \"\setotherlanguage\", \"\setotherlanguages\", and \"\setkeys\"
152 macros by polyglossia package.
153
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
157 mandatory argument."
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
163                    (if multiple
164                        'TeX-completing-read-multiple
165                      'completing-read)
166                    (if multiple "Languages: " "Language: ")
167                    (if setkeys
168                        (LaTeX-polyglossia-active-languages)
169                      LaTeX-polyglossia-language-list)))
170         var  options)
171     (if multiple
172         (mapc (lambda (elt) (TeX-run-style-hooks (concat "gloss-" elt)))
173               language)
174       (TeX-run-style-hooks (concat "gloss-" language)))
175     ;; "\setotherlanguages" doesn't take options, don't prompt for them.
176     (setq options
177           (if multiple ""
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.
186               "")))
187     (unless setkeys
188       (let ((TeX-arg-opening-brace LaTeX-optop)
189             (TeX-arg-closing-brace LaTeX-optcl))
190         (TeX-argument-insert options t)))
191     (if multiple
192         (setq language (mapconcat 'identity language ",")))
193     (TeX-argument-insert language nil)
194     (if setkeys
195         (TeX-argument-insert options nil))))
196
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'.
204                      ("hyphennames")
205                      ("script") ;; TODO: add completion in `fontspec.el', see
206                                 ;; "\newfontscript"s in `fontspec-xetex.sty'.
207                      ("direction" ("RL" "LR"))
208                      ("scripttag")
209                      ("langtag")
210                      ("hyphenmins")
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"))
217                      ;; ("localalph")
218                      ;; ("localnumber")
219                      )))
220
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)))
225
226 (TeX-add-style-hook
227  "polyglossia"
228  (lambda ()
229    (TeX-check-engine-add-engines 'luatex 'xetex)
230    (TeX-auto-add-regexp
231     `(,LaTeX-polyglossia-lang-regexp (3 1 2) LaTeX-auto-polyglossia-lang))
232    (TeX-auto-add-regexp
233     `(,LaTeX-polyglossia-setkeys-regexp (1 2) LaTeX-auto-polyglossia-setkeys))
234    (TeX-run-style-hooks "etoolbox" "makecmds" "xkeyval" "fontspec")
235    (TeX-add-symbols
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)
246     "normalfontlatin"
247     "rmfamilylatin"
248     "sffamilylatin"
249     "ttfamilylatin"
250     "selectlanguage"
251     "foreignlanguage")
252
253    (TeX-declare-expert-macros
254     "polyglossia"
255     "PolyglossiaSetup" "selectbackgroundlanguage" "resetdefaultlanguage"
256     "normalfontlatin" "rmfamilylatin" "sffamilylatin" "ttfamilylatin"
257     "selectlanguage" "foreignlanguage")
258
259    ;; Fontification
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" "{")
266                                 ("setkeys" "{{"))
267                               'function)))
268  LaTeX-dialect)
269
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.")
277
278 (defvar LaTeX-polyglossia-bengali-options-list
279   '(("numerals" ("Western" "Devanagari"))
280     ("changecounternumbering" ("true" "false")))
281   "Bengali language options for the polyglossia package.")
282
283 (defvar LaTeX-polyglossia-catalan-options-list
284   '(("babelshorthands" ("true" "false")))
285   "Catalan language options for the polyglossia package.")
286
287 (defvar LaTeX-polyglossia-dutch-options-list
288   '(("babelshorthands" ("true" "false")))
289   "Dutch language options for the polyglossia package.")
290
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.")
295
296 (defvar LaTeX-polyglossia-farsi-options-list
297   '(("numerals" ("western" "eastern"))
298     ;; ("locale") ;; not yet implemented
299     ;; ("calendar") ;; not yet implemented
300     )
301   "Farsi language options for the polyglossia package.")
302
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.")
310
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.")
316
317 (defvar LaTeX-polyglossia-hebrew-options-list
318   '(("numerals" ("hebrew" "arabic"))
319     ("calendar" ("hebrew" "gregorian")))
320   "Hebrew language options for the polyglossia package.")
321
322 (defvar LaTeX-polyglossia-hindi-options-list
323   '(("numerals" ("Western" "Devanagari")))
324   "Hindi language options for the polyglossia package.")
325
326 (defvar LaTeX-polyglossia-lao-options-list
327   '(("numerals" ("lao" "arabic")))
328   "Lao language options for the polyglossia package.")
329
330 (defvar LaTeX-polyglossia-russian-options-list
331   '(("spelling" ("modern" "old"))
332     ("babelshorthands" ("true" "false")))
333   "Russian language options for the polyglossia package.")
334
335 (defvar LaTeX-polyglossia-sanskrit-options-list
336   '(("Script" ("Devanagari")))
337   "Sanskrit language options for the polyglossia package.")
338
339 (defvar LaTeX-polyglossia-serbian-options-list
340   '(("script" ("cyrillic" "latin")))
341   "Serbian language options for the polyglossia package.")
342
343 (defvar LaTeX-polyglossia-syriac-options-list
344   '(("numerals" ("western" "eastern" "abjad")))
345   "Syriac language options for the polyglossia package.")
346
347 (defvar LaTeX-polyglossia-thai-options-list
348   '(("numerals" ("thai" "arabic")))
349   "Thai language options for the polyglossia package.")
350
351 (defvar LaTeX-polyglossia-package-options
352   '("babelshorthands" "localmarks" "nolocalmarks" "quiet")
353   "Package options for the polyglossia package.")
354
355 ;;; polyglossia.el ends here