summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/fontspec.el
blob: 6970b75b581072c4cc8db70dd896d6a6b0e12f21 (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
437
438
439
440
441
442
443
444
445
446
447
448
449
;;; fontspec.el --- AUCTeX style for `fontspec.sty' version 2.7i.  -*- lexical-binding: t; -*-

;; Copyright (C) 2013--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 `fontspec.sty' version 2.7i.  Starting
;; with `fontspec.sty' v2.4, the order of mandatory font names and
;; optional font features in related macros has changed, i.e. optional
;; argument comes after the mandatory one.  This change is now (April
;; 2017) implemented in this file.  Fontification support retains
;; backward compatibility.

;;; Code:

(require 'tex)
(require 'latex)

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

(declare-function LaTeX-color-definecolor-list "color" ())
(declare-function LaTeX-xcolor-definecolor-list "xcolor" ())

(defvar LaTeX-fontspec-font-features
  '(;; I General font selection
    ("Extension" (".otf" ".ttf" ".ttc" ".dfont"))
    ("Path")
    ;; I 4.1 More control over font shape selection
    ("UprightFont")
    ("BoldFont")
    ("ItalicFont")
    ("BoldItalicFont")
    ("SlantedFont")
    ("BoldSlantedFont")
    ("SmallCapsFont")
    ;; I 4.2 Specifically choosing the NFSS family
    ("NFSSFamily")
    ;; I 4.3 Choosing additional NFSS font faces
    ("FontFace")
    ;; III 3. Different features for different font shapes
    ("BoldFeatures")
    ("ItalicFeatures")
    ("BoldItalicFeatures")
    ("SlantedFeatures")
    ("BoldSlantedFeatures")
    ("SmallCapsFeatures")
    ("UprightFeatures")
    ;; III 4. Different features for different font sizes
    ("SizeFeatures")
    ;; III 6. Font independent options
    ("Color")
    ("Scale" ("MatchLowercase" "MatchUppercase"))
    ("WordSpace")
    ("PunctuationSpace")
    ("HyphenChar" ("None"))
    ("OpticalSize")
    ("AutoFakeBold")
    ("AutoFakeSlant")
    ("FakeSlant")
    ("FakeStretch")
    ("FakeBold")
    ("LetterSpace")
    ;; IV.3 OpenType options
    ;; IV.3.1.1 Alternates.  The next 2 are synonyms:
    ("Alternate" ("Random"))
    ("StylisticAlternate" ("Random"))
    ;; IV.3.1.2 Character Variants
    ("CharacterVariant")
    ;; IV 3.1.3 Contextuals
    ("Contextuals" ("Swash"       "SwashOff"       "SwashReset"
                    "Alternate"   "AlternateOff"   "AlternateReset"
                    "WordInitial" "WordInitialOff" "WordInitialReset"
                    "WordFinal"   "WordFinalOff"   "WordFinalReset"
                    "LineFinal"   "LineFinalOff"   "LineFinal"
                    "Inner"       "InnerOff"       "InnerReset"
                    "ResetAll"))
    ;; IV 3.1.4 Diacritics
    ("Diacritics" ("MarkToBase" "MarkToBaseOff" "MarkToBaseReset"
                   "MarkToMark" "MarkToMarkOff" "MarkToMarkReset"
                   "AboveBase"  "AboveBaseOff"  "AboveBaseReset"
                   "BelowBase"  "BelowBaseOff"  "BelowBaseReset"
                   "ResetAll"))
    ;; IV 3.1.5 Fractions
    ("Fractions" ("On" "Off" "Reset" "Alternate"
                  "AlternateOff" "AlternateReset" "ResetAll"))
    ;; IV 3.1.6 Kerning
    ("Kerning" ("On" "Off" "Reset" "ResetAll"
                "Uppercase" "UppercaseOff" "UppercaseReset"))
    ;; IV 3.1.7 Letters
    ("Letters" ("SmallCaps"           "SmallCapsOff"           "SmallCapsReset"
                "PetiteCaps"          "PetiteCapsOff"          "PetiteCapsReset"
                "UppercaseSmallCaps"  "UppercaseSmallCapsOff"  "UppercaseSmallCapsReset"
                "UppercasePetiteCaps" "UppercasePetiteCapsOff" "UppercasePetiteCapsReset"
                "Unicase"             "UnicaseOff"             "UnicaseReset"
                "ResetAll"))
    ;; IV 3.1.8 Ligatures
    ("Ligatures" ("Required"      "RequiredOff"      "RequiredReset"
                  "Common"        "CommonOff"        "CommonReset"
                  "Contextual"    "ContextualOff"    "ContextualReset"
                  "Rare"          "RareOff"          "RareReset"
                  "Discretionary" "DiscretionaryOff" "DiscretionaryReset"
                  "Historic"      "HistoricOff"      "HistoricReset"
                  "TeX"           "TeXOff"           "TeXReset"
                  "ResetAll"))
    ;; IV 3.1.9 Localised Forms
    ("LocalForms" ("On" "Off" "Reset"))
    ;; IV 3.1.10 Numbers
    ("Numbers" ("Uppercase"    "UppercaseOff"    "UppercaseReset"
                "Lowercase"    "LowercaseOff"    "LowercaseReset"
                "Lining"       "LiningOff"       "LiningReset"
                "OldStyle"     "OldStyleOff"     "OldStyleReset"
                "Proportional" "ProportionalOff" "ProportionalReset"
                "Monospaced"   "MonospacedOff"   "MonospacedReset"
                "SlashedZero"  "SlashedZeroOff"  "SlashedZeroReset"
                "Arabic"       "ArabicOff"       "ArabicReset"
                "ResetAll"))
    ;; IV 3.1.11 Ornament
    ("Ornament")
    ;; IV 3.1.12 Style
    ("Style" ("Alternate"      "AlternateOff"      "AlternateReset"
              "Italic"         "ItalicOff"         "ItalicReset"
              "Ruby"           "RubyOff"           "RubyReset"
              "Swash"          "SwashOff"          "SwashReset"
              "Cursive"        "CursiveOff"        "CursiveReset"
              "Historic"       "HistoricOff"       "HistoricReset"
              "Titling"        "TitlingOff"        "TitlingReset"
              "HorizontalKana" "HorizontalKanaOff" "HorizontalKanaReset"
              "VerticalKana"   "VerticalKanaOff"   "VerticalKanaReset"
              "Uppercase"      "UppercaseOff"      "UppercaseReset"
              "ResetAll"))
    ;; IV 3.1.13 Stylistic Set variations.   The next 2 are synonyms:
    ("StylisticSet")
    ("Variant")
    ;; IV 3.1.14 Vertical Position
    ("VerticalPosition" ("Superior"           "SuperiorOff"           "SuperiorReset"
                         "Inferior"           "InferiorOff"           "InferiorReset"
                         "Numerator"          "NumeratorOff"          "NumeratorReset"
                         "Denominator"        "DenominatorOff"        "DenominatorReset"
                         "ScientificInferior" "ScientificInferiorOff" "ScientificInferiorReset"
                         "Ordinal"            "OrdinalOff"            "OrdinalReset"
                         "ResetAll"))
    ;; IV 3.2.1 Annotation
    ("Annotation")
    ;; IV 3.2.2 Character width
    ("CharacterWidth" ("Proportional"          "ProportionalOff"          "ProportionalReset"
                       "Full"                  "FullOff"                  "FullReset"
                       "Half"                  "HalfOff"                  "HalfReset"
                       "Third"                 "ThirdOff"                 "ThirdReset"
                       "Quarter"               "QuarterOff"               "QuarterReset"
                       "AlternateProportional" "AlternateProportionalOff" "AlternateProportionalReset"
                       "AlternateHalf"         "AlternateHalfOff"         "AlternateHalfReset"
                       "ResetAll"))
    ;; IV 3.2.3 CJK shape
    ("CJKShape" ("Traditional"
                 "Simplified"
                 "JIS1978"
                 "JIS1983"
                 "JIS1990"
                 "Expert"
                 "NLC"))
    ;; IV 3.2.4 Vertical typesetting
    ("Vertical" ("RotatedGlyphs"         "RotatedGlyphsOff"         "RotatedGlyphsReset"
                 "AlternatesForRotation" "AlternatesForRotationOff" "AlternatesForRotationReset"
                 "Alternates"            "AlternatesOff"            "AlternatesReset"
                 "KanaAlternates"        "KanaAlternatesOff"        "KanaAlternatesReset"
                 "Kerning"               "KerningOff"               "KerningReset"
                 "AlternateMetrics"      "AlternateMetricsOff"      "AlternateMetricsReset"
                 "HalfMetrics"           "HalfMetricsOff"           "HalfMetricsReset"
                 "ProportionalMetrics"   "ProportionalMetricsOff"   "ProportionalMetricsReset"
                 "ResetAll"))
    ;; VIII 3. Going behind fontspec's back: Offer only an excerpt of
    ;; all possible tags:
    ("RawFeature" ("frac" "lnum" "onum" "pnum" "smcp" "tnum" "zero")))
  "Font features options for macros of the fontspec package.")

(defvar LaTeX-fontspec-font-list nil
  "List of the fonts accessible to fontspec.")

(defun LaTeX-fontspec-arg-font (optional &optional prompt)
  "Prompt for a font name with completion.
If OPTIONAL is non-nil, insert the resulting value as an optional
argument, otherwise as a mandatory one.  Use PROMPT as the prompt
string.

Customize `LaTeX-fontspec-arg-font-search' in order to decide how
to retrieve the list of fonts."
  (unless LaTeX-fontspec-font-list
    (when (if (eq LaTeX-fontspec-arg-font-search 'ask)
              (not (y-or-n-p "Find font yourself? "))
            LaTeX-fontspec-arg-font-search)
      (message "Searching for fonts...")
      (with-temp-buffer
        (shell-command "luaotfload-tool --list=basename" t)
        ;; Search for the font base names and full names, and add them to
        ;; `LaTeX-fontspec-font-list'.  The list is in the form
        ;;     <base name><TAB><full name><TAB><version>
        (while
            (re-search-forward "^\\([^\n\r\t]*\\)\t\\([^\n\r\t]*\\)\t.*$" nil t)
          (add-to-list 'LaTeX-fontspec-font-list (match-string-no-properties 1))
          (add-to-list 'LaTeX-fontspec-font-list
                       (match-string-no-properties 2))))
      (message "Searching for fonts...done")))
  (TeX-argument-insert
   (completing-read
    (TeX-argument-prompt optional prompt "Font name")
    (or LaTeX-fontspec-font-list LaTeX-fontspec-font-list-default))
   optional))

(defun LaTeX-fontspec-font-features ()
  "Return an updated list of font features.
This function retrieves values from various sources and adds them
to appropriate keys which are eventually prepended to
`LaTeX-fontspec-font-features' shadowing the predefined one."
  (append
   ;; Check for color packages, prefer xcolor over color.  Note that
   ;; we run the function `TeX-style-list' only once and after that we
   ;; use the updated `TeX-active-styles'.
   (when (or (member "xcolor" (TeX-style-list))
             (member "color" TeX-active-styles))
     (let ((colorcmd (if (member "xcolor" TeX-active-styles)
                         #'LaTeX-xcolor-definecolor-list
                       #'LaTeX-color-definecolor-list)))
       `(("Color" ,(mapcar #'car (funcall colorcmd))))))
   ;; If `LaTeX-fontspec-font-list' is set, use it for other `*Font'
   ;; related keys:
   (when LaTeX-fontspec-font-list
     `(("UprightFont"     ,LaTeX-fontspec-font-list)
       ("BoldFont"        ,LaTeX-fontspec-font-list)
       ("ItalicFont"      ,LaTeX-fontspec-font-list)
       ("BoldItalicFont"  ,LaTeX-fontspec-font-list)
       ("SlantedFont"     ,LaTeX-fontspec-font-list)
       ("BoldSlantedFont" ,LaTeX-fontspec-font-list)
       ("SmallCapsFont"   ,LaTeX-fontspec-font-list)))
   ;; This is a LuaTeX only feature.  VI. 1 Different font
   ;; technologies and shapers
   (when (eq TeX-engine 'luatex)
     '(("Renderer" ("Harfbuzz" "OpenType" "AAT" "Graphite"))))
   ;; This is a XeTeX only feature.  VII. 1 Different font
   ;; technologies
   (when (eq TeX-engine 'xetex)
     '(("Renderer" ("OpenType" "AAT" "Graphite"))))
   ;; Predefined features:
   LaTeX-fontspec-font-features))

;; Setup for \newfontfamily and \newfontface:
(TeX-auto-add-type "fontspec-newfontcmd" "LaTeX")

(defvar LaTeX-fontspec-newfontcmd-regexp
  '("\\\\newfontfa\\(?:ce\\|mily\\)[ \t\n\r%]*\\\\\\([a-zA-Z]+\\)"
    1 LaTeX-auto-fontspec-newfontcmd)
  "Matches new macros defined with \\newfontface and \\newfontfamily.")

(defun LaTeX-fontspec-auto-prepare ()
  "Clear `LaTeX-auto-fontspec-newfontcmd' before parsing."
  (setq LaTeX-auto-fontspec-newfontcmd nil))

(defun LaTeX-fontspec-auto-cleanup ()
  "Process parsed elements for fontspec package."
  (dolist (mac (mapcar #'car (LaTeX-fontspec-newfontcmd-list)))
    ;; Add macro to list of known macros
    (TeX-add-symbols mac)
    ;; Cater for fontification
    (when (and (featurep 'font-latex)
               (eq TeX-install-font-lock 'font-latex-setup))
      (font-latex-add-keywords `((,mac ""))
                               'type-declaration))))

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

(TeX-add-style-hook
 "fontspec"
 (lambda ()

   (TeX-check-engine-add-engines 'luatex 'xetex)
   (TeX-run-style-hooks "expl3" "xparse")

   ;; Add fontspec to the parser.
   (TeX-auto-add-regexp LaTeX-fontspec-newfontcmd-regexp)

   (TeX-add-symbols
    ;; 4.3 Commands for old-style and lining numbers: \oldstylenums is
    ;; already provided by LaTeX, so just add \liningnums here
    '("liningnums" "Numbers")

    ;; 4.5 Emphasis and nested emphasis
    ;; \emshape seems to be an internal macro
    "emshape"
    '("emfontdeclare" t)
    "emreset"

    ;; 4.6 Strong emphasis
    '("strong" t)
    '("strongfontdeclare" t)
    "strongreset"

    ;; 5 Font selection
    '("fontspec"
      LaTeX-fontspec-arg-font
      [TeX-arg-key-val (LaTeX-fontspec-font-features) "Font features"])

    ;; Default font families
    '("setmainfont"
      (LaTeX-fontspec-arg-font "Main font name")
      [TeX-arg-key-val (LaTeX-fontspec-font-features) "Font features"])
    '("setsansfont"
      (LaTeX-fontspec-arg-font "Sans font name")
      [TeX-arg-key-val (LaTeX-fontspec-font-features) "Font features"])
    '("setmonofont"
      (LaTeX-fontspec-arg-font "Mono font name")
      [TeX-arg-key-val (LaTeX-fontspec-font-features) "Font features"])

    ;; 5.3 Querying whether a font exists
    '("IfFontExistsTF" LaTeX-fontspec-arg-font 2)

    ;; 6 commands to select font families
    '("newfontfamily" TeX-arg-define-macro
      LaTeX-fontspec-arg-font
      [TeX-arg-key-val (LaTeX-fontspec-font-features) "Font features"])

    '("newfontface" TeX-arg-define-macro
      LaTeX-fontspec-arg-font
      [TeX-arg-key-val (LaTeX-fontspec-font-features) "Font features"])

    ;; 6.4 Math(s) fonts
    '("setmathrm" "Font name" [ "Font features" ])
    '("setmathsf" "Font name" [ "Font features" ])
    '("setmathtt" "Font name" [ "Font features" ])
    '("setboldmathrm" "Font name" [ "Font features" ])

    ;; 8 Default settings
    '("defaultfontfeatures" [ LaTeX-fontspec-arg-font ]
      (TeX-arg-key-val (LaTeX-fontspec-font-features) "Font features"))
    '("defaultfontfeatures+" [ LaTeX-fontspec-arg-font ]
      (TeX-arg-key-val (LaTeX-fontspec-font-features) "Font features"))

    ;; 10 Working with the currently selected features
    '("IfFontFeatureActiveTF"
      [TeX-arg-key-val (LaTeX-fontspec-font-features) "Font feature"] 2)

    ;; Changing the currently selected features
    '("addfontfeatures"
      (TeX-arg-key-val (LaTeX-fontspec-font-features) "Font features"))

    ;; 23 Defining new features
    '("newAATfeature"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Existing feature")
                    (LaTeX-fontspec-font-features))
      "New option" 2)

    '("newopentypefeature"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Existing feature")
                    (LaTeX-fontspec-font-features))
      "New option" t)

    '("newfontfeature" "New feature" t)

    ;; 24 Defining new scripts and languages
    '("newfontscript" "Script name" "OpenType tag")
    '("newfontlanguage" "Language name" "OpenType tag")

    ;; 26 Renaming existing features & options
    '("aliasfontfeature"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Existing feature")
                    (LaTeX-fontspec-font-features))
      "New name")

    '("aliasfontfeatureoption"
      (TeX-arg-eval
       (lambda ()
         (let* ((key (completing-read
                      (TeX-argument-prompt nil nil "Feature")
                      (LaTeX-fontspec-font-features)))
                (val (completing-read
                      (TeX-argument-prompt nil nil "Existing name")
                      (cadr (assoc key (LaTeX-fontspec-font-features))))))
           (TeX-argument-insert key nil)
           (format "%s" val))))
      "New name") )

   (LaTeX-add-environments
    ;; 4.6 Strong emphasis
    '("strong"))

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("fontspec"    "[{[")
                                ("setmainfont" "[{[")
                                ("setsansfont" "[{[")
                                ("setmonofont" "[{[")
                                ("newfontfamily" "\\[{[")
                                ("newfontface"   "\\[{[")
                                ("setmathrm" "[{[")
                                ("setmathsf" "[{[")
                                ("setmathtt" "[{[")
                                ("setboldmathrm" "[{[")
                                ("defaultfontfeatures" "+[{")
                                ("addfontfeature"  "{")
                                ("addfontfeatures" "{")
                                ("newfontscript"   "{{")
                                ("newfontlanguage" "{{")
                                ("emfontdeclare"   "{")
                                ("strongfontdeclare"  "{")
                                ("newAATfeature"      "{{{{")
                                ("newopentypefeature" "{{{")
                                ("newfontfeature"     "{{")
                                ("aliasfontfeature"   "{{")
                                ("aliasfontfeatureoption" "{{{"))
                              'function)
     (font-latex-add-keywords '(("liningnums"    "{"))
                              'type-command)
     (font-latex-add-keywords '(("strong"    "{"))
                              'bold-command)))
 TeX-dialect)

(defvar LaTeX-fontspec-package-options
  '("tuenc" "euenc" "math" "no-math" "config" "no-config" "quiet" "silent")
  "Package options for the fontspec package.")

;;; fontspec.el ends here