1 ;;; fontspec.el --- AUCTeX style for `fontspec.sty' version 2.6a.
3 ;; Copyright (C) 2013, 2017 Free Software Foundation, Inc.
5 ;; Maintainer: auctex-devel@gnu.org
6 ;; Author: Mosè Giordano <mose@gnu.org>
9 ;; This file is part of AUCTeX.
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)
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.
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
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.
37 (defvar LaTeX-fontspec-font-features
39 ("Extension" (".otf" ".ttf" ".ttc" ".dfont"))
41 ;; 6.1 More control over font shape selection
48 ;; 6.2 Specifically choosing the NFSS family
51 ;; 11 Different features for different font shapes
55 ("BoldItalicFeatures")
57 ("BoldSlantedFeatures")
59 ;; 13 Different features for different font sizes
61 ;; 14 Font independent options
63 ("Scale" ("MatchLowercase" "MatchUppercase"))
74 ;; 16 OpenType options
75 ("Ligatures" ("Required" "RequiredOff"
77 "Contextual" "ContextualOff"
79 "Discretionary" "DiscretionaryOff"
80 "Historic" "HistoricOff"
83 ("Letters" ("Uppercase" "UppercaseOff"
84 "SmallCaps" "SmallCapsOff"
85 "PetiteCaps" "PetiteCapsOff"
86 "UppercaseSmallCaps" "UppercaseSmallCapsOff"
87 "UppercasePetiteCaps" "UppercasePetiteCapsOff"
88 "Unicase" "UnicaseOff"
90 ("Numbers" ("Uppercase" "UppercaseOff"
91 "Lowercase" "LowercaseOff"
93 "OldStyle" "OldStyleOff"
94 "Proportional" "ProportionalOff"
95 "Monospaced" "MonospacedOff"
96 "SlashedZero" "SlashedZeroOff"
99 ("Contextuals" ("Swash" "SwashOff"
100 "Alternate" "AlternateOff"
101 "WordInitial" "WordInitialOff"
102 "WordFinal" "WordFinalOff"
103 "LineFinal" "LineFinalOff"
106 ("VerticalPosition" ("Superior" "SuperiorOff"
107 "Inferior" "InferiorOff"
108 "Numerator" "NumeratorOff"
109 "Denominator" "DenominatorOff"
110 "ScientificInferior" "ScientificInferiorOff"
111 "Ordinal" "OrdinalOff"
113 ("Fraction" ("On" "Off" "Reset" "Alternate" "AlternateOff" "ResetAll"))
116 ("Alternate" ("Random"))
117 ("Style" ("Alternate" "AlternateOff"
121 "Cursive" "CursiveOff"
122 "Historic" "HistoricOff"
123 "TitlingCaps" "TitlingCapsOff"
124 "HorizontalKana" "HorizontalKanaOff"
125 "VerticalKana" "VerticalKanaOff"
127 ("Diacritics" ("MarkToBase" "MarkToBaseOff"
128 "MarkToMark" "MarkToMarkOff"
129 "AboveBase" "AboveBaseOff"
130 "BelowBase" "BelowBaseOff"
132 ("Kerning" ("Uppercase" "UppercaseOff" "On" "Off" "Reset" "ResetAll"))
133 ("CharacterWidth" ("Proportional" "ProportionalOff"
137 "Quarter" "QuarterOff"
138 "AlternateProportional" "AlternateProportionalOff"
139 "AlternateHalf" "AlternateHalfOff"
142 ("CJKShape" ("Traditional"
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"
158 ;; 25 Going behind fontspec's back: Offer only an excerpt of all
160 ("RawFeature" ("frac" "lnum" "onum" "pnum" "smcp" "tnum" "zero")))
161 "Font features options for macros of the fontspec package.")
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)
167 (defvar LaTeX-fontspec-font-list nil
168 "List of the fonts accessible to fontspec.")
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
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...")
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>
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")))
196 (TeX-argument-prompt optional prompt "Font name")
197 (or LaTeX-fontspec-font-list LaTeX-fontspec-font-list-default))
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
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
216 ;; Setup for \newfontfamily and \newfontface:
217 (TeX-auto-add-type "fontspec-newfontcmd" "LaTeX")
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.")
224 (defun LaTeX-fontspec-auto-prepare ()
225 "Clear `LaTeX-auto-fontspec-newfontcmd' before parsing."
226 (setq LaTeX-auto-fontspec-newfontcmd nil))
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 ""))
238 ;; Update values of Color key:
239 (LaTeX-fontspec-update-font-features))
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)
248 (TeX-check-engine-add-engines 'luatex 'xetex)
249 (TeX-run-style-hooks "expl3" "xparse")
251 ;; Add fontspec to the parser.
252 (TeX-auto-add-regexp LaTeX-fontspec-newfontcmd-regexp)
254 ;; Activate the buffer local version of font features:
255 (setq LaTeX-fontspec-font-features-local
256 (copy-alist LaTeX-fontspec-font-features))
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")
263 ;; 4.5 Emphasis and nested emphasis
264 ;; \emshape seems to be an internal macro
269 ;; 4.6 Strong emphasis
271 '("strongfontdeclare" t)
276 LaTeX-fontspec-arg-font
277 [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"])
279 ;; Default font families
281 (LaTeX-fontspec-arg-font "Main font name")
282 [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"])
284 (LaTeX-fontspec-arg-font "Sans font name")
285 [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"])
287 (LaTeX-fontspec-arg-font "Mono font name")
288 [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"])
290 ;; 5.3 Querying whether a font exists
291 '("IfFontExistsTF" LaTeX-fontspec-arg-font 2)
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"])
298 '("newfontface" TeX-arg-define-macro
299 LaTeX-fontspec-arg-font
300 [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"])
303 '("setmathrm" "Font name" [ "Font features" ])
304 '("setmathsf" "Font name" [ "Font features" ])
305 '("setmathtt" "Font name" [ "Font features" ])
306 '("setboldmathrm" "Font name" [ "Font features" ])
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"))
314 ;; 10 Working with the currently selected features
315 '("IfFontFeatureActiveTF"
316 [TeX-arg-key-val LaTeX-fontspec-font-features-local "Font feature"] 2)
318 ;; Changing the currently selected features
320 (TeX-arg-key-val LaTeX-fontspec-font-features-local "Font features"))
322 ;; 23 Defining new features
324 (TeX-arg-eval completing-read
325 (TeX-argument-prompt optional nil "Existing feature")
326 LaTeX-fontspec-font-features-local)
329 '("newopentypefeature"
330 (TeX-arg-eval completing-read
331 (TeX-argument-prompt optional nil "Existing feature")
332 LaTeX-fontspec-font-features-local)
335 '("newfontfeature" "New feature" t)
337 ;; 24 Defining new scripts and languages
338 '("newfontscript" "Script name" "OpenType tag")
339 '("newfontlanguage" "Language name" "OpenType tag")
341 ;; 26 Renaming existing features & options
343 (TeX-arg-eval completing-read
344 (TeX-argument-prompt optional nil "Existing feature")
345 LaTeX-fontspec-font-features-local)
348 '("aliasfontfeatureoption"
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)
361 (LaTeX-add-environments
362 ;; 4.6 Strong emphasis
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" "\\[{[")
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" "{{{"))
391 (font-latex-add-keywords '(("liningnums" "{"))
393 (font-latex-add-keywords '(("strong" "{"))
397 (defvar LaTeX-fontspec-package-options
398 '("tuenc" "euenc" "math" "no-math" "config" "no-config" "quiet" "silent")
399 "Package options for the fontspec package.")
401 ;;; fontspec.el ends here