summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/polyglossia.el
blob: cdfb03e093da4a36de39dd7d9d4857c2a8d1fc15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
;;; polyglossia.el --- AUCTeX style for `polyglossia.sty' version 1.42.0.  -*- lexical-binding: t; -*-

;; Copyright (C) 2015-2021  Free Software Foundation, Inc.

;; Maintainer: auctex-devel@gnu.org
;; Author: Mosè Giordano <mose@gnu.org>
;; Keywords: tex

;; This file is part of AUCTeX.

;; AUCTeX is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; AUCTeX is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with AUCTeX; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
;; 02110-1301, USA.

;;; Commentary:

;; This file adds support for `polyglossia.sty' version 1.42.0.

;;; TODO:

;;  -- Create language specific styles with names `gloss-<lang>.el'.  They
;;     should add `text<lang>' macros, `<lang>' environments (`Arabic' for
;;     `arabic' language), and the others language-specific commands.

;;; Code:

(require 'tex) ;Indispensable when compiling the call to `TeX-auto-add-type'.
(require 'latex)

;; Silence the compiler:
(declare-function font-latex-add-keywords
                  "font-latex"
                  (keywords class))

(TeX-auto-add-type "polyglossia-lang" "LaTeX")

;; Self Parsing -- see (info "(auctex)Hacking the Parser").
(defvar LaTeX-polyglossia-lang-regexp
  (concat "\\\\set\\(defaultlanguage\\|mainlanguage\\|otherlanguages?\\)"
          "[ \t\n\r]*\\(?:\\[\\(.*\\)\\]\\)?[ \t\n\r]*{\\([A-Za-z, ]+\\)}")
  "Matches languages set with polyglossia macros.")

(defvar LaTeX-polyglossia-setkeys-regexp
  (concat "\\\\setkeys"
          "[ \t\n\r]*{\\([A-Za-z]+\\)}[ \t\n\r]*{\\([^}]*\\)}")
  "Matches polyglossia languages options set using \"\setkeys\".")

(defvar LaTeX-auto-polyglossia-lang nil
  "Temporary for parsing polyglossia languages.")

(defvar LaTeX-auto-polyglossia-setkeys nil
  "Temporary for parsing polyglossia language options.")

(defun LaTeX-polyglossia-prepare ()
  "Clear some polyglossia variables before use."
  (setq LaTeX-auto-polyglossia-lang nil
        LaTeX-auto-polyglossia-setkeys nil
        LaTeX-polyglossia-lang-list nil))

(defun LaTeX-polyglossia-cleanup ()
  "Move languages and their options from
`LaTeX-auto-polyglossia-lang' to `LaTeX-polyglossia-lang-list'."
  ;; Example: now the value of `LaTeX-auto-polyglossia-lang' is something like
  ;;   '(("danish" "defaultlanguage" "")
  ;;     ("arabic" "otherlanguage" "locale=tunisia,numerals=maghrib")
  ;;     ("german" "otherlanguage" "spelling=new,script=latin")
  ;;     ("icelandic,brazil,sanskrit" "otherlanguages" ""))
  ;; We want to end up with a list like
  ;;   '(("danish" "defaultlanguage")
  ;;     ("arabic" "otherlanguage" "locale=tunisia" "numerals=maghrib")
  ;;     ("german" "otherlanguage" "spelling=new" "script=latin")
  ;;     ("icelandic" "otherlanguages")
  ;;     ("brazil" "otherlanguages")
  ;;     ("sanskrit" "otherlanguages" "script=Devanagari"))
  ;; with "script=Devanagari" option to "sanskrit" language set using
  ;; "\setkeys".
  ;; In each element of the alist, the key is the language, the second value is
  ;; the polyglossia command which set the language, the rest of values is the
  ;; list of options given to the language.
  (let (opts otheropts)
    (mapc
     (lambda (elt)
       (mapc
        (lambda (language)
          ;; `opts' is the string of options for `language', set using
          ;; "\setdefaultlanguage" or "\setotherlanguage".
          (setq opts (cdr (cdr elt)))
          ;; `otheropts' is the string of options for `language' set using
          ;; "\setkeys".
          (setq otheropts
                (car (cdr (assoc language LaTeX-auto-polyglossia-setkeys))))
          (add-to-list
           'LaTeX-polyglossia-lang-list
           (append
            (list language) (list (nth 1 elt))
            (unless (equal opts '(""))
              (LaTeX-listify-package-options (car opts)))
            (if otheropts (LaTeX-listify-package-options otheropts))) t))
        (LaTeX-listify-package-options (car elt))))
     LaTeX-auto-polyglossia-lang)))

(add-hook 'TeX-auto-prepare-hook #'LaTeX-polyglossia-prepare)
(add-hook 'TeX-auto-cleanup-hook #'LaTeX-polyglossia-cleanup)
(add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
;; Run style hooks for every active language.  This *has* to be done after
;; `TeX-auto-parse'.
(add-hook 'TeX-update-style-hook #'LaTeX-polyglossia-load-languages t)

(defvar LaTeX-polyglossia-language-list
  '("albanian" "amharic" "arabic" "armenian" "asturian" "bahasai" "bahasam"
    "basque" "bengali" "brazil" "breton" "bulgarian" "catalan" "coptic"
    "croatian" "czech" "danish" "divehi" "dutch" "english" "esperanto"
    "estonian" "farsi" "finnish" "french" "friulan" "galician" "german" "greek"
    "hebrew" "hindi" "icelandic" "interlingua" "irish" "italian" "kannada"
    "khmer" "korean" "lao" "latin" "latvian" "lithuanian" "lsorbian" "magyar"
    "malayalam" "marathi" "nko" "norsk" "nynorsk" "occitan" "piedmontese"
    "polish" "portuges" "romanian" "romansh" "russian" "samin" "sanskrit"
    "scottish" "serbian" "slovak" "slovenian" "spanish" "swedish" "syriac"
    "tamil" "telugu" "thai" "tibetan" "turkish" "turkmen" "ukrainian" "urdu"
    "usorbian" "vietnamese" "welsh")
  "List of languages supported by the polyglossia LaTeX package.")

(defun LaTeX-polyglossia-active-languages ()
  "Return a list of polyglossia languages used in the document.
The last language is the default one."
  (let (active-languages default)
    (mapc
     (lambda (elt)
       (setq default (or (string-equal "defaultlanguage" (nth 1 elt))
                         (string-equal "mainlanguage" (nth 1 elt))))
       ;; Append the language to the list if it's the default one.
       (if default
           (setq active-languages (append active-languages
                                          (list (car elt))))
         (push active-languages (car elt))))
     LaTeX-polyglossia-lang-list)
    active-languages))

(defun LaTeX-polyglossia-lang-option-member (language option)
  "Return non-nil if OPTION has been given to polyglossia LANGUAGE.
The value is actually the tail of the list of options given to LANGUAGE."
  (member option (cdr (cdr (assoc language LaTeX-polyglossia-lang-list)))))

(defun LaTeX-arg-polyglossia-lang (_optional _default multiple setkeys)
  "Prompt for language and its options with completion and insert them
as arguments.

This function is triggered by \"\setdefaultlanguage\",
\"\setotherlanguage\", \"\setotherlanguages\", and \"\setkeys\"
macros by polyglossia package.

OPTIONAL and DEFAULT are ignored, if MULTIPLE is non-nil prompt
for multiple languages, if SETKEYS is non-nil insert options as
second mandatory argument."
  ;; DEFAULT =  t , MULTIPLE = nil, SETKEYS = nil: "\setdefaultlanguage".
  ;; DEFAULT = nil, MULTIPLE = nil, SETKEYS = nil: "\setotherlanguage".
  ;; DEFAULT = nil, MULTIPLE =  t , SETKEYS = nil: "\setotherlanguages".
  ;; DEFAULT = nil, MULTIPLE = nil, SETKEYS =  t : "\setkeys".

  ;; Note: `DEFAULT' is currently ignored because we don't really have a
  ;; mechanism to identify the default polyglossia language.
  (let ((language (funcall
                   (if multiple
                       #'TeX-completing-read-multiple
                     #'completing-read)
                   (if multiple "Languages: " "Language: ")
                   (if setkeys
                       (LaTeX-polyglossia-active-languages)
                     LaTeX-polyglossia-language-list)))
        var  options)
    (if multiple
        (mapc (lambda (elt) (TeX-run-style-hooks (concat "gloss-" elt)))
              language)
      (TeX-run-style-hooks (concat "gloss-" language)))
    ;; "\setotherlanguages" doesn't take options, don't prompt for them.
    (setq options
          (if multiple ""
            (setq var (intern (format "LaTeX-polyglossia-%s-options-list" language)))
            (if (and (boundp var) (symbol-value var))
                ;; "\setdefaultlanguage" and "\setotherlanguage" use `options'
                ;; as first optional argument; "\setkeys" uses `options' as
                ;; second mandatory argument.
                (TeX-read-key-val (not setkeys) (symbol-value var))
              ;; When `LaTeX-polyglossia-<lang>-options-list' is nil or not
              ;; defined, don't prompt for options.
              "")))
    (unless setkeys
      (let ((TeX-arg-opening-brace LaTeX-optop)
            (TeX-arg-closing-brace LaTeX-optcl))
        (TeX-argument-insert options t)))
    (if multiple
        (setq language (mapconcat #'identity language ",")))
    (TeX-argument-insert language nil)
    (if setkeys
        (TeX-argument-insert options nil))))

(defun LaTeX-arg-polyglossiasetup-options (optional)
  "Prompt for setup options of polyglossia package.
If OPTIONAL is non-nil, insert the resulting value as an optional
argument, otherwise as a mandatory one."
  (TeX-arg-key-val optional
                   '(("language") ;; TODO: add completion in `fontspec.el', see
                                  ;; "\newfontlanguage"s in `fontspec-xetex.sty'.
                     ("hyphennames")
                     ("script") ;; TODO: add completion in `fontspec.el', see
                                ;; "\newfontscript"s in `fontspec-xetex.sty'.
                     ("direction" ("RL" "LR"))
                     ("scripttag")
                     ("langtag")
                     ("hyphenmins")
                     ("frenchspacing" ("true" "false"))
                     ("indentfirst" ("true" "false"))
                     ("fontsetup" ("true" "false"))
                     ;; The following options aren't already implemented but are
                     ;; present in `polyglossia.sty' comments.
                     ;; ("nouppercase" ("true" "false"))
                     ;; ("localalph")
                     ;; ("localnumber")
                     )))

(defun LaTeX-polyglossia-load-languages ()
  "Load style files of babel active languages."
  (mapc (lambda (elt) (TeX-run-style-hooks (concat "gloss-" elt)))
        (LaTeX-polyglossia-active-languages)))

(TeX-add-style-hook
 "polyglossia"
 (lambda ()
   (TeX-check-engine-add-engines 'luatex 'xetex)
   (TeX-auto-add-regexp
    `(,LaTeX-polyglossia-lang-regexp (3 1 2) LaTeX-auto-polyglossia-lang))
   (TeX-auto-add-regexp
    `(,LaTeX-polyglossia-setkeys-regexp (1 2) LaTeX-auto-polyglossia-setkeys))
   (TeX-run-style-hooks "makecmds" "xkeyval" "fontspec")
   (TeX-add-symbols
    '("setdefaultlanguage" (LaTeX-arg-polyglossia-lang  t  nil nil))
    '("setmainlanguage"    (LaTeX-arg-polyglossia-lang  t  nil nil))
    '("setotherlanguage"   (LaTeX-arg-polyglossia-lang nil nil nil))
    '("setotherlanguages"  (LaTeX-arg-polyglossia-lang nil  t  nil))
    '("setkeys"            (LaTeX-arg-polyglossia-lang nil nil  t ))
    '("PolyglossiaSetup"   (TeX-arg-eval completing-read "Language: "
                                         (LaTeX-polyglossia-active-languages))
      LaTeX-arg-polyglossiasetup-options)
    "selectbackgroundlanguage"
    '("resetdefaultlanguage" ["argument"] 1)
    "normalfontlatin"
    "rmfamilylatin"
    "sffamilylatin"
    "ttfamilylatin"
    "selectlanguage"
    "foreignlanguage")

   (TeX-declare-expert-macros
    "polyglossia"
    "PolyglossiaSetup" "selectbackgroundlanguage" "resetdefaultlanguage"
    "normalfontlatin" "rmfamilylatin" "sffamilylatin" "ttfamilylatin"
    "selectlanguage" "foreignlanguage")

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("setdefaultlanguage" "[{")
                                ("setmainlanguage" "[{")
                                ("setotherlanguage" "[{")
                                ("setotherlanguages" "{")
                                ("setkeys" "{{"))
                              'function)))
 TeX-dialect)

;; TODO: move each option variable in its specific `gloss-<lang>.el' file.
(defvar LaTeX-polyglossia-arabic-options-list
  '(("calendar" ("gregorian" "islamic"))
    ("locale" ("default" "mashriq" "libya" "algeria" "tunisia" "morocco" "mauritania"))
    ("numerals" ("mashriq" "maghrib"))
    ("abjadjimnotail" ("false" "true")))
  "Arabic language options for the polyglossia package.")

(defvar LaTeX-polyglossia-bengali-options-list
  '(("numerals" ("Western" "Devanagari"))
    ("changecounternumbering" ("true" "false")))
  "Bengali language options for the polyglossia package.")

(defvar LaTeX-polyglossia-catalan-options-list
  '(("babelshorthands" ("true" "false")))
  "Catalan language options for the polyglossia package.")

(defvar LaTeX-polyglossia-dutch-options-list
  '(("babelshorthands" ("true" "false")))
  "Dutch language options for the polyglossia package.")

(defvar LaTeX-polyglossia-english-options-list
  '(("variant" ("american" "usmax" "british" "australian" "newzealand"))
    ("ordinalmonthday" ("true" "false")))
  "English language options for the polyglossia package.")

(defvar LaTeX-polyglossia-farsi-options-list
  '(("numerals" ("western" "eastern"))
    ;; ("locale") ;; not yet implemented
    ;; ("calendar") ;; not yet implemented
    )
  "Farsi language options for the polyglossia package.")

(defvar LaTeX-polyglossia-german-options-list
  '(("variant" ("german" "austrian" "swiss"))
    ("spelling" ("new" "old"))
    ("latesthyphen" ("true" "false"))
    ("babelshorthands" ("true" "false"))
    ("script" ("latin" "fraktur")))
  "German language options for the polyglossia package.")

(defvar LaTeX-polyglossia-greek-options-list
  '(("variant" ("monotonic" "polytonic" "ancient"))
    ("numerals" ("greek" "arabic"))
    ("attic" ("true" "false")))
  "Greek language options for the polyglossia package.")

(defvar LaTeX-polyglossia-hebrew-options-list
  '(("numerals" ("hebrew" "arabic"))
    ("calendar" ("hebrew" "gregorian")))
  "Hebrew language options for the polyglossia package.")

(defvar LaTeX-polyglossia-hindi-options-list
  '(("numerals" ("Western" "Devanagari")))
  "Hindi language options for the polyglossia package.")

(defvar LaTeX-polyglossia-lao-options-list
  '(("numerals" ("lao" "arabic")))
  "Lao language options for the polyglossia package.")

(defvar LaTeX-polyglossia-russian-options-list
  '(("spelling" ("modern" "old"))
    ("babelshorthands" ("true" "false")))
  "Russian language options for the polyglossia package.")

(defvar LaTeX-polyglossia-sanskrit-options-list
  '(("Script" ("Devanagari")))
  "Sanskrit language options for the polyglossia package.")

(defvar LaTeX-polyglossia-serbian-options-list
  '(("script" ("cyrillic" "latin")))
  "Serbian language options for the polyglossia package.")

(defvar LaTeX-polyglossia-syriac-options-list
  '(("numerals" ("western" "eastern" "abjad")))
  "Syriac language options for the polyglossia package.")

(defvar LaTeX-polyglossia-thai-options-list
  '(("numerals" ("thai" "arabic")))
  "Thai language options for the polyglossia package.")

(defvar LaTeX-polyglossia-package-options
  '("babelshorthands" "localmarks" "nolocalmarks" "quiet")
  "Package options for the polyglossia package.")

;;; polyglossia.el ends here