EasyPG 1.07 Released
[packages] / xemacs-packages / auctex / style / fontspec.el
1 ;;; fontspec.el --- AUCTeX style for `fontspec.sty' version 2.6a.
2
3 ;; Copyright (C) 2013, 2017 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 `fontspec.sty' version 2.6a.  Starting
29 ;; with `fontspec.sty' v2.4, the order of mandatory font names and
30 ;; optional font features in related macros has changed, i.e. optional
31 ;; argument comes after the mandatory one.  This change is now (April
32 ;; 2017) implemented in this file.  Fontification support retains
33 ;; backward compatibilty.
34
35 ;;; Code:
36
37 (defvar LaTeX-fontspec-font-features
38   '(;; 5 Font selection
39     ("Extension" (".otf" ".ttf" ".ttc" ".dfont"))
40     ("Path")
41     ;; 6.1 More control over font shape selection
42     ("BoldFont")
43     ("ItalicFont")
44     ("BoldItalicFont")
45     ("SlantedFont")
46     ("BoldSlantedFont")
47     ("SmallCapsFont")
48     ;; 6.2 Specifically choosing the NFSS family
49     ("NFSSFamily")
50     ("FontFace")
51     ;; 11 Different features for different font shapes
52     ("UprightFeatures")
53     ("BoldFeatures")
54     ("ItalicFeatures")
55     ("BoldItalicFeatures")
56     ("SlantedFeatures")
57     ("BoldSlantedFeatures")
58     ("SmallCapsFeatures")
59     ;; 13 Different features for different font sizes
60     ("SizeFeatures")
61     ;; 14 Font independent options
62     ("Color")
63     ("Scale" ("MatchLowercase" "MatchUppercase"))
64     ("WordSpace")
65     ("PunctuationSpace")
66     ("HyphenChar")
67     ("OpticalSize")
68     ("AutoFakeBold")
69     ("AutoFakeSlant")
70     ("FakeSlant")
71     ("FakeStretch")
72     ("FakeBold")
73     ("LetterSpace")
74     ;; 16 OpenType options
75     ("Ligatures" ("Required"      "RequiredOff"
76                   "Common"        "CommonOff"
77                   "Contextual"    "ContextualOff"
78                   "Rare"          "RareOff"
79                   "Discretionary" "DiscretionaryOff"
80                   "Historic"      "HistoricOff"
81                   "TeX"
82                   "ResetAll"))
83     ("Letters" ("Uppercase"           "UppercaseOff"
84                 "SmallCaps"           "SmallCapsOff"
85                 "PetiteCaps"          "PetiteCapsOff"
86                 "UppercaseSmallCaps"  "UppercaseSmallCapsOff"
87                 "UppercasePetiteCaps" "UppercasePetiteCapsOff"
88                 "Unicase"             "UnicaseOff"
89                 "ResetAll"))
90     ("Numbers" ("Uppercase"    "UppercaseOff"
91                 "Lowercase"    "LowercaseOff"
92                 "Lining"       "LiningOff"
93                 "OldStyle"     "OldStyleOff"
94                 "Proportional" "ProportionalOff"
95                 "Monospaced"   "MonospacedOff"
96                 "SlashedZero"  "SlashedZeroOff"
97                 "Arabic"       "ArabicOff"
98                 "ResetAll"))
99     ("Contextuals" ("Swash"       "SwashOff"
100                     "Alternate"   "AlternateOff"
101                     "WordInitial" "WordInitialOff"
102                     "WordFinal"   "WordFinalOff"
103                     "LineFinal"   "LineFinalOff"
104                     "Inner"       "InnerOff"
105                     "ResetAll"))
106     ("VerticalPosition" ("Superior"           "SuperiorOff"
107                          "Inferior"           "InferiorOff"
108                          "Numerator"          "NumeratorOff"
109                          "Denominator"        "DenominatorOff"
110                          "ScientificInferior" "ScientificInferiorOff"
111                          "Ordinal"            "OrdinalOff"
112                          "ResetAll"))
113     ("Fraction" ("On" "Off" "Reset" "Alternate" "AlternateOff" "ResetAll"))
114     ("StylisticSet")
115     ("CharacterVariant")
116     ("Alternate" ("Random"))
117     ("Style" ("Alternate"      "AlternateOff"
118               "Italic"         "ItalicOff"
119               "Ruby"           "RubyOff"
120               "Swash"          "SwashOff"
121               "Cursive"        "CursiveOff"
122               "Historic"       "HistoricOff"
123               "TitlingCaps"    "TitlingCapsOff"
124               "HorizontalKana" "HorizontalKanaOff"
125               "VerticalKana"   "VerticalKanaOff"
126               "ResetAll"))
127     ("Diacritics" ("MarkToBase" "MarkToBaseOff"
128                    "MarkToMark" "MarkToMarkOff"
129                    "AboveBase"  "AboveBaseOff"
130                    "BelowBase"  "BelowBaseOff"
131                    "ResetAll"))
132     ("Kerning" ("Uppercase" "UppercaseOff" "On" "Off" "Reset" "ResetAll"))
133     ("CharacterWidth" ("Proportional"          "ProportionalOff"
134                        "Full"                  "FullOff"
135                        "Half"                  "HalfOff"
136                        "Third"                 "ThirdOff"
137                        "Quarter"               "QuarterOff"
138                        "AlternateProportional" "AlternateProportionalOff"
139                        "AlternateHalf"         "AlternateHalfOff"
140                        "ResetAll"))
141     ("Annotation")
142     ("CJKShape" ("Traditional"
143                  "Simplified"
144                  "JIS1978"
145                  "JIS1983"
146                  "JIS1990"
147                  "Expert"
148                  "NLC"))
149     ("Vertical" ("RotatedGlyphs"         "RotatedGlyphsOff"
150                  "AlternatesForRotation" "AlternatesForRotationOff"
151                  "Alternates"            "AlternatesOff"
152                  "KanaAlternates"        "KanaAlternatesOff"
153                  "Kerning"               "KerningOff"
154                  "AlternateMetrics"      "AlternateMetricsOff"
155                  "HalfMetrics"           "HalfMetricsOff"
156                  "ProportionalMetrics"   "ProportionalMetricsOff"
157                  "ResetAll"))
158     ;; 25 Going behind fontspec's back: Offer only an excerpt of all
159     ;; possible tags:
160     ("RawFeature" ("frac" "lnum" "onum" "pnum" "smcp" "tnum" "zero")))
161   "Font features options for macros of the fontspec package.")
162
163 (defvar LaTeX-fontspec-font-features-local nil
164   "Buffer-local font features options for macros of the fontspec package.")
165 (make-variable-buffer-local 'LaTeX-fontspec-font-features-local)
166
167 (defvar LaTeX-fontspec-font-list nil
168   "List of the fonts accessible to fontspec.")
169
170 (defun LaTeX-fontspec-arg-font (optional &optional prompt)
171   "Prompt for a font name with completion.
172 If OPTIONAL is non-nil, insert the resulting value as an optional
173 argument, otherwise as a mandatory one.  Use PROMPT as the prompt
174 string.
175
176 Customize `LaTeX-fontspec-arg-font-search' in order to decide how
177 to retrieve the list of fonts."
178   (unless LaTeX-fontspec-font-list
179     (when (if (eq LaTeX-fontspec-arg-font-search 'ask)
180               (not (y-or-n-p "Find font yourself? "))
181             LaTeX-fontspec-arg-font-search)
182       (message "Searching for fonts...")
183       (with-temp-buffer
184         (shell-command "luaotfload-tool --list=basename" t)
185         ;; Search for the font base names and full names, and add them to
186         ;; `LaTeX-fontspec-font-list'.  The list is in the form
187         ;;     <base name><TAB><full name><TAB><version>
188         (while
189             (re-search-forward "^\\([^\n\r\t]*\\)\t\\([^\n\r\t]*\\)\t.*$" nil t)
190           (add-to-list 'LaTeX-fontspec-font-list (match-string-no-properties 1))
191           (add-to-list 'LaTeX-fontspec-font-list
192                        (match-string-no-properties 2))))
193       (message "Searching for fonts...done")))
194   (TeX-argument-insert
195    (completing-read
196     (TeX-argument-prompt optional prompt "Font name")
197     (or LaTeX-fontspec-font-list LaTeX-fontspec-font-list-default))
198    optional))
199
200 (defun LaTeX-fontspec-update-font-features ()
201   "Update Color key=values in `LaTeX-fontspec-font-features-local'."
202   ;; Check if any color defininig package is loaded and update the
203   ;; key=values for coloring.  Prefer xcolor.sty if both packages are
204   ;; loaded.
205   (when (or (member "xcolor" (TeX-style-list))
206             (member "color" (TeX-style-list)))
207     (let* ((colorcmd (if (member "xcolor" (TeX-style-list))
208                          #'LaTeX-xcolor-definecolor-list
209                        #'LaTeX-color-definecolor-list))
210            (tmp (copy-alist LaTeX-fontspec-font-features-local)))
211       (setq tmp (assq-delete-all (car (assoc "Color" tmp)) tmp))
212       (push (list "Color" (mapcar #'car (funcall colorcmd))) tmp)
213       (setq LaTeX-fontspec-font-features-local
214             (copy-alist tmp)))))
215
216 ;; Setup for \newfontfamily and \newfontface:
217 (TeX-auto-add-type "fontspec-newfontcmd" "LaTeX")
218
219 (defvar LaTeX-fontspec-newfontcmd-regexp
220   '("\\\\newfontfa\\(?:ce\\|mily\\)[ \t\n\r%]*\\\\\\([a-zA-Z]+\\)"
221     1 LaTeX-auto-fontspec-newfontcmd)
222   "Matches new macros defined with \\newfontface and \\newfontfamily.")
223
224 (defun LaTeX-fontspec-auto-prepare ()
225   "Clear `LaTeX-auto-fontspec-newfontcmd' before parsing."
226   (setq LaTeX-auto-fontspec-newfontcmd nil))
227
228 (defun LaTeX-fontspec-auto-cleanup ()
229   "Process parsed elements for fontspec package."
230   (dolist (mac (mapcar #'car (LaTeX-fontspec-newfontcmd-list)))
231     ;; Add macro to list of known macros
232     (TeX-add-symbols mac)
233     ;; Cater for fontification
234     (when (and (featurep 'font-latex)
235                (eq TeX-install-font-lock 'font-latex-setup))
236       (font-latex-add-keywords `((,mac ""))
237                                'type-declaration)))
238   ;; Update values of Color key:
239   (LaTeX-fontspec-update-font-features))
240
241 (add-hook 'TeX-auto-prepare-hook #'LaTeX-fontspec-auto-prepare t)
242 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-fontspec-auto-cleanup t)
243 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
244
245 (TeX-add-style-hook
246  "fontspec"
247  (lambda ()
248    (TeX-check-engine-add-engines 'luatex 'xetex)
249    (TeX-run-style-hooks "expl3" "xparse")
250
251    ;; Add fontspec to the parser.
252    (TeX-auto-add-regexp LaTeX-fontspec-newfontcmd-regexp)
253
254    ;; Activate the buffer local version of font features:
255    (setq LaTeX-fontspec-font-features-local
256          (copy-alist LaTeX-fontspec-font-features))
257
258    (TeX-add-symbols
259     ;; 4.3 Commands for old-style and lining numbers: \oldstylenums is
260     ;; already provided by LaTeX, so just add \liningnums here
261     '("liningnums" "Numbers")
262
263     ;; 4.5 Emphasis and nested emphasis
264     ;; \emshape seems to be an internal macro
265     "emshape"
266     '("emfontdeclare" t)
267     "emreset"
268
269     ;; 4.6 Strong emphasis
270     '("strong" t)
271     '("strongfontdeclare" t)
272     "strongreset"
273
274     ;; 5 Font selection
275     '("fontspec"
276       LaTeX-fontspec-arg-font
277       [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"])
278
279     ;; Default font families
280     '("setmainfont"
281       (LaTeX-fontspec-arg-font "Main font name")
282       [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"])
283     '("setsansfont"
284       (LaTeX-fontspec-arg-font "Sans font name")
285       [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"])
286     '("setmonofont"
287       (LaTeX-fontspec-arg-font "Mono font name")
288       [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"])
289
290     ;; 5.3 Querying whether a font exists
291     '("IfFontExistsTF" LaTeX-fontspec-arg-font 2)
292
293     ;; 6 commands to select font families
294     '("newfontfamily" TeX-arg-define-macro
295       LaTeX-fontspec-arg-font
296       [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"])
297
298     '("newfontface" TeX-arg-define-macro
299       LaTeX-fontspec-arg-font
300       [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"])
301
302     ;; 6.4 Math(s) fonts
303     '("setmathrm" "Font name" [ "Font features" ])
304     '("setmathsf" "Font name" [ "Font features" ])
305     '("setmathtt" "Font name" [ "Font features" ])
306     '("setboldmathrm" "Font name" [ "Font features" ])
307
308     ;; 8 Default settings
309     '("defaultfontfeatures" [ LaTeX-fontspec-arg-font ]
310       (TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"))
311     '("defaultfontfeatures+" [ LaTeX-fontspec-arg-font ]
312       (TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"))
313
314     ;; 10 Working with the currently selected features
315     '("IfFontFeatureActiveTF"
316       [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font feature"] 2)
317
318     ;; Changing the currently selected features
319     '("addfontfeatures"
320       (TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"))
321
322     ;; 23 Defining new features
323     '("newAATfeature"
324       (TeX-arg-eval completing-read
325                     (TeX-argument-prompt optional nil "Existing feature")
326                     LaTeX-fontspec-font-features-local)
327       "New option" 2)
328
329     '("newopentypefeature"
330       (TeX-arg-eval completing-read
331                     (TeX-argument-prompt optional nil "Existing feature")
332                     LaTeX-fontspec-font-features-local)
333       "New option" t)
334
335     '("newfontfeature" "New feature" t)
336
337     ;; 24 Defining new scripts and languages
338     '("newfontscript" "Script name" "OpenType tag")
339     '("newfontlanguage" "Language name" "OpenType tag")
340
341     ;; 26 Renaming existing features & options
342     '("aliasfontfeature"
343       (TeX-arg-eval completing-read
344                     (TeX-argument-prompt optional nil "Existing feature")
345                     LaTeX-fontspec-font-features-local)
346       "New name")
347
348     '("aliasfontfeatureoption"
349       (TeX-arg-eval
350        (lambda ()
351          (let* ((key (completing-read
352                       (TeX-argument-prompt optional nil "Feature")
353                       LaTeX-fontspec-font-features-local))
354                 (val (completing-read
355                       (TeX-argument-prompt optional nil "Existing name")
356                       (cadr (assoc key LaTeX-fontspec-font-features-local)))))
357            (TeX-argument-insert key optional)
358            (format "%s" val))))
359       "New name") )
360
361    (LaTeX-add-environments
362     ;; 4.6 Strong emphasis
363     '("strong"))
364
365    ;; Fontification
366    (when (and (featurep 'font-latex)
367               (eq TeX-install-font-lock 'font-latex-setup))
368      (font-latex-add-keywords '(("fontspec"    "[{[")
369                                 ("setmainfont" "[{[")
370                                 ("setsansfont" "[{[")
371                                 ("setmonofont" "[{[")
372                                 ("newfontfamily" "\\[{[")
373                                 ("newfontface"   "\\[{[")
374                                 ("setmathrm" "[{[")
375                                 ("setmathsf" "[{[")
376                                 ("setmathtt" "[{[")
377                                 ("setboldmathrm" "[{[")
378                                 ("defaultfontfeatures" "+[{")
379                                 ("addfontfeature"  "{")
380                                 ("addfontfeatures" "{")
381                                 ("newfontscript"   "{{")
382                                 ("newfontlanguage" "{{")
383                                 ("emfontdeclare"   "{")
384                                 ("strongfontdeclare"  "{")
385                                 ("newAATfeature"      "{{{{")
386                                 ("newopentypefeature" "{{{")
387                                 ("newfontfeature"     "{{")
388                                 ("aliasfontfeature"   "{{")
389                                 ("aliasfontfeatureoption" "{{{"))
390                               'function)
391      (font-latex-add-keywords '(("liningnums"    "{"))
392                               'type-command)
393      (font-latex-add-keywords '(("strong"    "{"))
394                               'bold-command)))
395  LaTeX-dialect)
396
397 (defvar LaTeX-fontspec-package-options
398   '("tuenc" "euenc" "math" "no-math" "config" "no-config" "quiet" "silent")
399   "Package options for the fontspec package.")
400
401 ;;; fontspec.el ends here