Initial Commit
[packages] / xemacs-packages / auctex / style / babel.el
1 ;;; babel.el --- AUCTeX style for `babel.sty' version 3.9h.
2
3 ;; Copyright (C) 2005, 2007, 2013-2014 Free Software Foundation, Inc.
4
5 ;; Author: Ralf Angeli <angeli@iwi.uni-sb.de>
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Created: 2005-05-29
8 ;; Keywords: tex
9
10 ;; This file is part of AUCTeX.
11
12 ;; AUCTeX is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
16
17 ;; AUCTeX is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with AUCTeX; see the file COPYING.  If not, write to the Free
24 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 ;; 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This file adds support for `babel.sty' version 3.9h.
30
31 ;;; Code:
32
33 (defvar LaTeX-babel-language-list
34   '("afrikaans"
35     "bahasa" "indonesian" "indon" "bahasai" "bahasam" "malay" "meyalu"
36     "basque"
37     "breton"
38     "bulgarian"
39     "catalan"
40     "croatian"
41     "czech"
42     "danish"
43     "dutch"
44     "english" "USenglish" "american" "UKenglish" "british"  "canadian"
45     "australian" "newzealand"
46     "esperanto"
47     "estonian"
48     "finnish"
49     "french" "francais" "canadien" "acadian"
50     "galician"
51     "austrian" "german" "germanb" "ngerman" "naustrian"
52     "greek" "polutonikogreek"
53     "hebrew"
54     "icelandic"
55     "interlingua"
56     "irish"
57     "italian"
58     "latin"
59     "lowersorbian"
60     "samin"
61     "norsk" "nynorsk"
62     "polish"
63     "portuges" "portuguese" "brazilian" "brazil"
64     "romanian"
65     "russian"
66     "scottish"
67     "spanish"
68     "slovak"
69     "slovene"
70     "swedish"
71     "serbian"
72     "turkish"
73     "ukrainian"
74     "uppersorbian"
75     "welsh"
76     ;; Extra languages mentioned in the `babel' manual.
77     "albanian" "hindi" "thai" "thaicjk" "latvian" "turkmen" "hungarian" "magyar"
78     "mongolian" "romansh" "lithuanian" "spanglish" "vietnamese" "japanese"
79     "pinyin" "arabinc" "farsi" "ibygreek" "bgreek" "serbianic" "frenchle"
80     "ethiop" "friulan" "frenchb")
81   "List of languages supported by the babel LaTeX package.")
82
83 (defun LaTeX-babel-active-languages ()
84   "Return a list of languages used in the document."
85   (let (main-language active-languages)
86     ;; Loop over options provided to class and `babel' package at load time.
87     (dolist (elt (append
88                   ;; In most cases there is only one element in the alist, if
89                   ;; there is more than one element, the first one should
90                   ;; contain the class options of the current buffer.  So we can
91                   ;; take the car of `LaTeX-provided-class-options'.
92                   (cdr (car LaTeX-provided-class-options))
93                   (cdr (assoc "babel" LaTeX-provided-package-options))))
94       (setq elt (TeX-split-string "=" elt))
95       (if (equal (car elt) "main")
96           ;; Starting from version 3.9 of `babel' package, languages can be set
97           ;; with the following syntax:
98           ;;   \usepackage[latin.medieval,main=danish,spanish.notilde]{babel}
99           ;; with `danish' being the default language.  When the default
100           ;; language is set with the `main' option, we record it and append to
101           ;; the list at the end.
102           (setq main-language (car (cdr elt)))
103         ;; Get rid of the modifiers (`medieval' and `notilde' in the above
104         ;; example).
105         (setq elt (car (TeX-split-string "\\." (car elt))))
106         (if (member elt LaTeX-babel-language-list)
107             ;; Append element to `active-languages' to respect loading order.
108             ;; `babel' package uses as default language the last loaded one,
109             ;; except if it is set with the `main' option.
110             (add-to-list 'active-languages elt t))))
111     (if main-language
112         (add-to-list 'active-languages main-language t))
113     active-languages))
114
115 (defun TeX-arg-babel-lang (optional &optional prompt)
116   "Prompt for a language with completion and insert it as an argument."
117   (TeX-argument-insert
118    (completing-read "Language: " (LaTeX-babel-active-languages)) nil))
119
120 (defun LaTeX-env-babel-lang (env)
121   "Prompt for a language and insert it as an argument of ENV."
122   (LaTeX-insert-environment
123    env (format "{%s}" (completing-read "Language: "
124                                        (LaTeX-babel-active-languages)))))
125
126 (defun LaTeX-babel-load-languages ()
127   "Load style files of babel active languages."
128   ;; Run style hooks for every active language in loading order, so
129   ;; `TeX-quote-language' will be correctly set.
130   (mapc 'TeX-run-style-hooks (LaTeX-babel-active-languages)))
131
132 (TeX-add-style-hook
133  "babel"
134  (lambda ()
135    (LaTeX-babel-load-languages)
136    (add-hook 'LaTeX-after-usepackage-hook 'LaTeX-babel-load-languages nil t)
137    ;; New symbols
138    (TeX-add-symbols
139     '("selectlanguage" TeX-arg-babel-lang)
140     '("foreignlanguage" TeX-arg-babel-lang t)
141     "languagename"
142     '("iflanguage" TeX-arg-babel-lang t nil)
143     '("useshorthands" t)
144     '("defineshorthand" t nil)
145     '("aliasshorthand" t nil)
146     '("languageshorthands" TeX-arg-babel-lang)
147     '("shorthandon" t)
148     '("shorthandoff" t)
149     '("languageattribute" TeX-arg-babel-lang t))
150    ;; New environments
151    (LaTeX-add-environments
152     '("otherlanguage" LaTeX-env-babel-lang)
153     '("otherlanguage*" LaTeX-env-babel-lang)
154     '("hyphenrules" LaTeX-env-babel-lang))
155    ;; Fontification
156    (when (and (featurep 'font-latex)
157               (eq TeX-install-font-lock 'font-latex-setup))
158      (font-latex-add-keywords '(("selectlanguage" "{")
159                                 ("foreignlanguage" "{{")
160                                 ("iflanguage" "{{{")
161                                 ("languagename" "")
162                                 ("useshorthands" "{")
163                                 ("languageshorthands" "{")
164                                 ("shorthandon" "{")
165                                 ("shorthandoff" "{"))
166                               'function)
167      (font-latex-add-keywords '(("defineshorthand" "{{")
168                                 ("aliasshorthand" "{{")
169                                 ("languageattribute" "{{"))
170                               'variable)))
171  LaTeX-dialect)
172
173 (defun LaTeX-babel-package-options ()
174   "Prompt for package options for the babel package."
175   (TeX-read-key-val
176    t
177    (append
178     '(("KeepShorthandsActive")
179       ("activeacute")
180       ("activegrave")
181       ("shorthands")
182       ("safe" ("none" "ref" "bib"))
183       ("math" ("active" "normal"))
184       ("config")
185       ("main" LaTeX-babel-language-list)
186       ("headfoot" LaTeX-babel-language-list)
187       ("noconfigs")
188       ("showlanguages")
189       ("strings" ("generic" "unicode" "encoded"))
190       ("hyphenmap" ("off" "main" "select" "other" "other*"))
191       ("base"))
192     (mapcar 'list LaTeX-babel-language-list))))
193
194 ;;; babel.el ends here