summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/babel.el
blob: a7d67479a8a385f94c1513f26e8f4406da965862 (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
;;; babel.el --- AUCTeX style for `babel.sty' version 3.31.  -*- lexical-binding: t; -*-

;; Copyright (C) 2005-2022  Free Software Foundation, Inc.

;; Author: Ralf Angeli <angeli@iwi.uni-sb.de>
;; Maintainer: auctex-devel@gnu.org
;; Created: 2005-05-29
;; 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 `babel.sty' version 3.31 from 2019/05/04.

;;; Code:

(require 'tex)
(require 'latex)

(eval-when-compile
  (require 'cl-lib))

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

(defvar LaTeX-babel-language-list
  '("afrikaans"
    "azerbaijani"
    "bahasa" "indonesian" "indon" "bahasai" "bahasam" "malay" "meyalu"
    "basque"
    "breton"
    "bulgarian"
    "catalan"
    "croatian"
    "czech"
    "danish"
    "dutch"
    "english" "USenglish" "american" "UKenglish" "british"  "canadian"
    "australian" "newzealand"
    "esperanto"
    "estonian"
    "finnish"
    "french" "francais" "canadien" "acadian"
    "galician"
    "austrian" "german" "germanb" "ngerman" "naustrian"
    "greek" "polutonikogreek"
    "hebrew"
    "icelandic"
    "interlingua"
    "irish"
    "italian"
    "latin"
    "lowersorbian"
    "samin"
    "norsk" "nynorsk"
    "polish"
    "portuges" "portuguese" "brazilian" "brazil"
    "romanian"
    "russian"
    "scottish"
    "spanish"
    "slovak"
    "slovene"
    "swedish"
    "serbian"
    "turkish"
    "ukrainian"
    "uppersorbian"
    "welsh"
    ;; Extra languages mentioned in the `babel' manual.
    "albanian" "hindi" "thai" "thaicjk" "latvian" "turkmen" "hungarian" "magyar"
    "mongolian" "romansh" "lithuanian" "spanglish" "vietnamese" "japanese"
    "pinyin" "arabinc" "farsi" "ibygreek" "bgreek" "serbianic" "frenchle"
    "ethiop" "friulan")
  "List of languages supported by the babel LaTeX package.")

(defvar LaTeX-babel-babelprovide-key-val-options
  `(("import")
    ("captions")
    ("hyphenrules" ,(append '("+") LaTeX-babel-language-list))
    ("main")
    ("script")
    ("language")
    ("mapfont")
    ("intraspace")
    ("intrapenalty"))
  "Key=value options for `\\babelprovide' macro from `babel' package.")

(defun LaTeX-babel-active-languages ()
  "Return a list of languages used in the document."
  (let (main-language active-languages)
    ;; Loop over options provided to class and `babel' package at load time.
    (dolist (elt (append
                  ;; In most cases there is only one element in the alist, if
                  ;; there is more than one element, the first one should
                  ;; contain the class options of the current buffer.  So we can
                  ;; take the car of `LaTeX-provided-class-options'.
                  (cdr (car LaTeX-provided-class-options))
                  (cdr (assoc "babel" LaTeX-provided-package-options))))
      (setq elt (split-string elt "="))
      (if (equal (car elt) "main")
          ;; Starting from version 3.9 of `babel' package, languages can be set
          ;; with the following syntax:
          ;;   \usepackage[latin.medieval,main=danish,spanish.notilde]{babel}
          ;; with `danish' being the default language.  When the default
          ;; language is set with the `main' option, we record it and append to
          ;; the list at the end.
          (setq main-language (car (cdr elt)))
        ;; Get rid of the modifiers (`medieval' and `notilde' in the above
        ;; example).
        (setq elt (car (split-string (car elt) "\\.")))
        (if (member elt LaTeX-babel-language-list)
            ;; Append element to `active-languages' to respect loading order.
            ;; `babel' package uses as default language the last loaded one,
            ;; except if it is set with the `main' option.
            (cl-pushnew elt active-languages :test #'equal))))
    (if main-language
        (cl-pushnew main-language active-languages :test #'equal))
    (nreverse active-languages)))

;; Setup for \babeltags: Note that the macro is \babeltags, we use
;; the version without `s' in order to reduce the hassle with AUCTeX
;; auto-generating the plural form:
(TeX-auto-add-type "babel-babeltag" "LaTeX")

(defvar LaTeX-babel-babeltags-regexp
  '("\\\\babeltags{\\([^}]+\\)}" 1 LaTeX-auto-babel-babeltag)
  "Matches the argument of `\\babeltags' from `babel' package.")

(defun LaTeX-babel-cleanup-babeltags ()
  "Parse defined babel tags and add them to AUCTeX."
  ;; Check if we parsed something at all
  (when (LaTeX-babel-babeltag-list)
    (let (results tag tags cmds)
      ;; Clean up the parsed results from characters we don't want;
      ;; also remove possible comment lines
      (setq results
            (replace-regexp-in-string
             "%.*\\'\\|[ \n\r\t]" ""
             (mapconcat #'car (LaTeX-babel-babeltag-list) ",")))
      ;; Look if \babeltags was issued once with multiple entries or
      ;; more than once in the document:
      (if (string-match-p "," results)
          (progn
            (dolist (elt (split-string results "," t))
              (setq tag (car (split-string elt "=" t)))
              (push tag tags)
              (push (list (concat "text" tag) t) cmds)
              (push (list tag -1) cmds)))
        ;; One \babeltags with one entry only
        (setq tag (car (split-string results "=" t)))
        (push tag tags)
        (push (list (concat "text" tag) t) cmds)
        (push (list tag -1) cmds))
      (mapc #'TeX-add-symbols cmds)
      (mapc #'LaTeX-add-environments tags)
      ;; Fontification
      (when (and (featurep 'font-latex)
                 (eq TeX-install-font-lock 'font-latex-setup))
        (font-latex-add-keywords (mapcar (lambda (x)
                                           (list (concat "text" x)  "{"))
                                         tags)
                                 'textual)
        (font-latex-add-keywords (mapcar (lambda (x)
                                           (list x  ""))
                                         tags)
                                 'type-declaration)))))

;; Setup for \babelfont:
(TeX-auto-add-type "babel-babelfont" "LaTeX")

(defvar LaTeX-babel-babelfont-regexp
  '("\\\\babelfont\\(?:\\[[^]]*\\]\\)?[ \t\n\r%]*{\\([^}]+\\)}"
    1 LaTeX-auto-babel-babelfont)
  "Matches the <font-family> argument of `\\babelfont' from `babel' package.")

(defun LaTeX-babel-cleanup-babelfont ()
  "Parse defined font-families and add them to AUCTeX."
  (when (LaTeX-babel-babelfont-list)
    (dolist (elt (mapcar #'car (LaTeX-babel-babelfont-list)))
      ;; Don't do anything for standard font-families:
      (unless (member elt '("rm" "sf" "tt"))
        ;; Define \<font>family, \<font>default and \text<font>:
        (let ((fam (concat elt "family"))
              (def (concat elt "default"))
              (mac (concat "text" elt)))
          (apply #'TeX-add-symbols
                 `((,fam -1)
                   (,def -1)
                   (,mac t)))
          ;; Cater for fontification:
          (when (and (featurep 'font-latex)
                     (eq TeX-install-font-lock 'font-latex-setup))
            (font-latex-add-keywords `((,fam "")
                                       (,def ""))
                                     'type-declaration)
            (font-latex-add-keywords `((,mac "{"))
                                     'type-command)))))))

(defun LaTeX-babel-auto-prepare ()
  "Clear `LaTeX-auto-babel-babel*' before parsing."
  (setq LaTeX-auto-babel-babeltag  nil
        LaTeX-auto-babel-babelfont nil))

(defun LaTeX-babel-auto-cleanup ()
  "Process parsed elements."
  (LaTeX-babel-cleanup-babeltags)
  (LaTeX-babel-cleanup-babelfont))

(add-hook 'TeX-auto-prepare-hook #'LaTeX-babel-auto-prepare t)
(add-hook 'TeX-auto-cleanup-hook #'LaTeX-babel-auto-cleanup t)
(add-hook 'TeX-update-style-hook #'TeX-auto-parse t)

(defun TeX-arg-babel-lang (optional &optional prompt)
  "Prompt for a language with completion and insert it as an argument."
  (TeX-argument-insert
   (completing-read
    (TeX-argument-prompt optional prompt "Language")
    (LaTeX-babel-active-languages))
   optional))

(defun LaTeX-env-babel-lang (env)
  "Prompt for a language and insert it as an argument of ENV."
  (LaTeX-insert-environment
   env (format "{%s}" (completing-read "Language: "
                                       (LaTeX-babel-active-languages)))))

(defun LaTeX-babel-load-languages ()
  "Load style files of babel active languages."
  ;; Run style hooks for every active language in loading order, so
  ;; `TeX-quote-language' will be correctly set.
  (mapc #'TeX-run-style-hooks (LaTeX-babel-active-languages)))

(TeX-add-style-hook
 "babel"
 (lambda ()
   (LaTeX-babel-load-languages)
   (add-hook 'LaTeX-after-usepackage-hook #'LaTeX-babel-load-languages nil t)

   ;; Add babel to the parser.
   (TeX-auto-add-regexp LaTeX-babel-babeltags-regexp)
   (TeX-auto-add-regexp LaTeX-babel-babelfont-regexp)

   ;; New symbols
   (TeX-add-symbols

    ;; 1.7 Basic language selectors
    '("selectlanguage" TeX-arg-babel-lang)
    '("foreignlanguage" TeX-arg-babel-lang t)

    ;; 1.9 More on selection
    '("babeltags" t)
    '("babelensure" (TeX-arg-key-val
                     (("include") ("exclude")
                      ("fontenc" (;; 128+ glyph encodings (text)
                                  "OT1" "OT2" "OT3" "OT4" "OT6"
                                  ;; 256 glyph encodings (text)
                                  "T1" "T2A" "T2B" "T2C" "T3" "T4" "T5"
                                  ;; 256 glyph encodings (text extended)
                                  "X2"
                                  ;; Other encodings
                                  "LY1" "LV1" "LGR"))))
      TeX-arg-babel-lang)
    ;; 1.10 Shorthands
    '("shorthandon"    "Shorthands list")
    '("shorthandoff"   "Shorthands list")
    '("shorthandoff*"  "Shorthands list")
    '("useshorthands"  "Character")
    '("useshorthands*" "Character")
    '("defineshorthand"
      [TeX-arg-eval mapconcat #'identity
                    (TeX-completing-read-multiple
                     (TeX-argument-prompt t nil "Language(s)")
                     (LaTeX-babel-active-languages))
                    ","]
      t nil)
    '("languageshorthands" TeX-arg-babel-lang)
    '("babelshorthand"   "Short hand")
    '("ifbabelshorthand" "Character" t nil)
    '("aliasshorthand"   "Original" "Alias")

    ;; 1.12 The base option
    '("AfterBabelLanguage"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Language")
                    LaTeX-babel-language-list)
      t)

    ;; 1.14 Selecting fonts
    '("babelfont"
      [TeX-arg-eval mapconcat #'identity
                    (TeX-completing-read-multiple
                     (TeX-argument-prompt t nil "Language(s)")
                     LaTeX-babel-language-list)
                    ","]
      (TeX-arg-eval let ((fontfam (completing-read
                                   (TeX-argument-prompt nil nil "font family")
                                   '("rm" "sf" "tt"))))
                    ;; Run `TeX-check-engine-add-engines' and then
                    ;; load `fontspec.el' if not already loaded and
                    ;; make sure the key-vals are up to date.
                    (unless (member "fontspec" (TeX-style-list))
                      (TeX-check-engine-add-engines 'luatex 'xetex)
                      (TeX-run-style-hooks "fontspec")
                      (LaTeX-fontspec-auto-cleanup))
                    (LaTeX-add-babel-babelfonts fontfam)
                    (LaTeX-babel-cleanup-babelfont)
                    (format "%s" fontfam))
      [TeX-arg-key-val (LaTeX-fontspec-font-features)]
      LaTeX-fontspec-arg-font)

    ;; 1.16 Creating a language
    '("babelprovide"
      [TeX-arg-key-val LaTeX-babel-babelprovide-key-val-options]
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Language")
                    LaTeX-babel-language-list))

    ;; 1.19 Accessing language info
    '("languagename" 0)
    '("iflanguage" TeX-arg-babel-lang t nil)

    ;; 1.20 Hyphenation and line breaking
    '("babelhyphen"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Type/Text")
                    '("soft" "hard" "repeat" "nobreak" "empty")))
    '("babelhyphen*"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Type/Text")
                    '("soft" "hard" "repeat" "nobreak" "empty")))

    '("babelhyphenation"
      [TeX-arg-eval mapconcat #'identity
                    (TeX-completing-read-multiple
                     (TeX-argument-prompt nil nil "Language(s)")
                     LaTeX-babel-language-list)
                    ","]
      t)

    ;; 1.23 Selecting scripts
    '("ensureascii" "Text")

    ;; 1.25 Language attributes
    '("languageattribute" TeX-arg-babel-lang t))

   ;; Don't increase indentation at various \if* macros:
   (let ((exceptions '("ifbabelshorthand"
                       "iflanguage")))
     (dolist (elt exceptions)
       (add-to-list 'LaTeX-indent-begin-exceptions-list elt t))
     (LaTeX-indent-commands-regexp-make))

   ;; New environments: 1.8 Auxiliary language selectors
   (LaTeX-add-environments
    '("otherlanguage" LaTeX-env-babel-lang)
    '("otherlanguage*" LaTeX-env-babel-lang)
    '("hyphenrules" LaTeX-env-babel-lang))

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("selectlanguage"     "{")
                                ("foreignlanguage"    "{{")
                                ("babeltags"          "{")
                                ("babelensure"        "{{")
                                ("shorthandon"        "{")
                                ("shorthandoff"       "*{")
                                ("useshorthands"      "*{")
                                ("languageshorthands" "{")
                                ("babelshorthand"     "{")
                                ("AfterBabelLanguage" "{")
                                ("babelfont"          "[{[{")
                                ("babelprovide"       "[{")
                                ("languagename"       "")
                                ("iflanguage"         "{{{")
                                ("babelhyphen"        "*{")
                                ("babelhyphenation"   "[{")
                                ("ensureascii"        "{"))
                              'function)
     (font-latex-add-keywords '(("defineshorthand"    "[{{")
                                ("aliasshorthand"     "{{")
                                ("languageattribute"  "{{"))
                              'variable)))
 TeX-dialect)

(defun LaTeX-babel-package-options ()
  "Prompt for package options for the babel package."
  (TeX-read-key-val
   t
   (append
    '(("KeepShorthandsActive")
      ("activeacute")
      ("activegrave")
      ("shorthands" ("off"))
      ("safe" ("none" "ref" "bib"))
      ("math" ("active" "normal"))
      ("config")
      ("main" LaTeX-babel-language-list)
      ("headfoot" LaTeX-babel-language-list)
      ("noconfigs")
      ("nocase")
      ("silent")
      ("showlanguages")
      ("nocase")
      ("silent")
      ("strings" ("generic" "unicode" "encoded"
                  "OT1" "OT2" "OT3" "OT4" "OT6"
                  "T1"  "T2A" "T2B" "T2C" "T3" "T4" "T5"
                  "X2"  "LY1" "LV1" "LGR"))
      ("hyphenmap" ("off" "first" "select" "other" "other*"))
      ("bidi" ("default" "basic" "basic-r" "bidi-l" "bidi-r"))
      ("layout" ("sectioning" "counters" "lists" "contents" "footnotes"
                 "captions"  "columns" "graphics" "extras"))
      ("base"))
    (mapcar #'list LaTeX-babel-language-list))))

;;; babel.el ends here