AUCTeX Sync -- New Style Files
[packages] / xemacs-packages / auctex / style / minted.el
1 ;;; minted.el --- AUCTeX style for `minted.sty' (v2.5)
2
3 ;; Copyright (C) 2014-2017 Free Software Foundation, Inc.
4
5 ;; Author: Tassilo Horn <tsdh@gnu.org>
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Created: 2014-12-19
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 `minted.sty' (v2.5) from 2017/07/19.
30
31 ;;; Code:
32
33 (require 'tex)
34
35 (defvar LaTeX-minted-key-val-options
36   '(("autogobble" ("true" "false"))
37     ("baselinestretch" ("auto"))
38     ("beameroverlays" ("true" "false"))
39     ("breakafter")
40     ("breakaftergroup" ("true" "false"))
41     ("breakaftersymbolpre")
42     ("breakaftersymbolpost")
43     ("breakanywhere" ("true" "false"))
44     ("breakanywheresymbolpre")
45     ("breakanywheresymbolpost")
46     ("breakautoindent" ("true" "false"))
47     ("breakbefore")
48     ("breakbeforegroup" ("true" "false"))
49     ("breakbeforesymbolpre")
50     ("breakbeforesymbolpost")
51     ("breakbytoken" ("true" "false"))
52     ("breakbytokenanywhere" ("true" "false"))
53     ("breakindent")
54     ("breakindentnchars")
55     ("breaklines" ("true" "false"))
56     ("breaksymbol")
57     ("breaksymbolleft")
58     ("breaksymbolright")
59     ("breaksymbolindent")
60     ("breaksymbolindentnchars")
61     ("breaksymbolindentleft")
62     ("breaksymbolindentleftnchars")
63     ("breaksymbolindentright")
64     ("breaksymbolseprightnchars")
65     ("breaksymbolsep")
66     ("breaksymbolsepnchars")
67     ("breaksymbolsepleft")
68     ("breaksymbolsepleftnchars")
69     ("breaksymbolsepright")
70     ("breaksymbolseprightnchars")
71     ("bgcolor")
72     ("codetagify")
73     ("curlyquotes" ("true" "false"))
74     ("encoding")
75     ("escapeinside")
76     ("firstline")
77     ("firstnumber" ("auto" "last" "integer"))
78     ("fontfamily" ("tt" "courier" "helvetica"))
79     ("fontseries" ("auto"))
80     ("fontsize" ("auto" "\\tiny" "\\large" "\\scriptsize" "\\Large"
81                  "\\footnotesize" "\\LARGE" "\\small" "\\huge"
82                  "\\normalsize" "\\Huge"))
83     ("fontshape" ("auto"))
84     ("formatcom")
85     ("frame" ("none" "leftline" "topline" "bottomline" "lines" "single"))
86     ("framerule")
87     ("framesep")
88     ("funcnamehighlighting" ("true" "false"))
89     ("gobble")
90     ("highlightcolor")
91     ("highlightlines")
92     ("keywordcase" ("lower" "upper" "capitalize"))
93     ("label")
94     ("labelposition" ("none" "topline" "bottomline" "all"))
95     ("lastline")
96     ("linenos" ("true" "false"))
97     ("numberfirstline" ("true" "false"))
98     ("numbers" ("left" "right" "both" "none"))
99     ("mathescape" ("true" "false"))
100     ("numberblanklines" ("true" "false"))
101     ("numbersep")
102     ("obeytabs" ("true" "false"))
103     ("outencoding")
104     ("python3" ("true" "false"))
105     ("resetmargins" ("true" "false"))
106     ("rulecolor")
107     ("samepage" ("true" "false"))
108     ("showspaces" ("true" "false"))
109     ("showtabs" ("true" "false"))
110     ("space")
111     ("spacecolor")
112     ("startinline" ("true" "false"))
113     ;; FIXME: It would be nice to use the function
114     ;; `LaTeX-minted-style-list' here, but with a file local var like:
115     ;;     %%% TeX-command-extra-options: "-shell-escape"
116     ;; in a .tex file, Emacs asks to apply a variable which is not
117     ;; safe and does not restore the frame; the splitted frame
118     ;; remains.  I couldn't figure out why, so for now, I add the
119     ;; styles from Pygments version 2.1.3 here.
120     ("style" ("colorful" "default" "emacs" "friendly" "fruity" "igor"
121               "lovelace" "manni" "monokai" "murphy" "native"
122               "paraiso-dark" "paraiso-light" "pastie" "perldoc"
123               "rrt" "tango" "trac" "vim" "vs" "xcode"))
124     ("stepnumber")
125     ("stepnumberfromfirst")
126     ("stepnumberoffsetvalues" ("true" "false"))
127     ("stripall" ("true" "false"))
128     ("stripnl" ("true" "false"))
129     ("tab")
130     ("tabcolor")
131     ("tabsize")
132     ("texcl" ("true" "false"))
133     ("texcomments" ("true" "false"))
134     ("xleftmargin")
135     ("xrightmargin"))
136   "Key=value options for minted macros and environments.")
137
138 (defvar LaTeX-minted-key-val-options-local nil
139   "Buffer-local key=value options for minted macros and environments.")
140 (make-variable-buffer-local 'LaTeX-minted-key-val-options-local)
141
142 (defvar LaTeX-minted-pygmentize-program (executable-find "pygmentize"))
143
144 (defvar LaTeX-minted-language-list nil
145   "List containing languages provided by pymentize program.")
146
147 (defun LaTeX-minted-language-list (&rest _ignored)
148   "Return a list of languages provided by pymentize program.
149 Update the variable `LaTeX-minted-language-list' if still nil."
150   (or LaTeX-minted-language-list
151       (when LaTeX-minted-pygmentize-program
152         (with-temp-buffer
153           (shell-command (concat LaTeX-minted-pygmentize-program " -L lexers")
154                          (current-buffer))
155           (goto-char (point-min))
156           (let (languages)
157             (while (re-search-forward "^\\*[[:space:]]\\([^:]+\\):" nil t)
158               (dolist (lang (split-string (match-string 1) "[[:space:],]" t))
159                 (push lang languages)))
160             (setq LaTeX-minted-language-list languages))
161           LaTeX-minted-language-list))))
162
163 (defun LaTeX-arg-minted-language (optional &optional prompt)
164     "Insert a selected pygmentize language as argument for macros from minted.sty.
165 If OPTIONAL is non-nil, insert it as optional argument in
166 brackets.  PROMPT replaces the standard one."
167   (TeX-argument-insert
168    (completing-read (TeX-argument-prompt optional prompt "Language")
169                     (LaTeX-minted-language-list))
170    optional))
171
172 (defvar LaTeX-minted-style-list nil
173   "List containing styles provided by pymentize program.")
174
175 (defun LaTeX-minted-style-list (&rest _ignored)
176   "Return a list of styles provided by pymentize program.
177 Update the variable `LaTeX-minted-style-list' if still nil."
178   (or LaTeX-minted-style-list
179       (when LaTeX-minted-pygmentize-program
180         (with-temp-buffer
181           (shell-command (concat LaTeX-minted-pygmentize-program " -L styles")
182                          (current-buffer))
183           (goto-char (point-min))
184           (let (styles)
185             (while (re-search-forward "^\\*[[:space:]]\\([^:]+\\):" nil t)
186               (dolist (style (split-string (match-string 1) "[[:space:],]" t))
187                 (push style styles)))
188             (setq LaTeX-minted-style-list styles))
189           LaTeX-minted-style-list))))
190
191 (defun LaTeX-arg-minted-style (optional &optional prompt)
192   "Insert a selected pygmentize style as argument for macros from minted.sty.
193 If OPTIONAL is non-nil, insert it as optional argument in
194 brackets.  PROMPT replaces the standard one."
195   (TeX-argument-insert
196    (completing-read (TeX-argument-prompt optional prompt "Style")
197                     (LaTeX-minted-style-list))
198    optional))
199
200 (defun LaTeX-minted-update-key-vals ()
201   "Update color related key-vals in `LaTeX-minted-key-val-options-local'.
202 This function checks if one of the packages \"xcolor.sty\" or
203 \"xcolor.sty\" is loaded and appends defined colors as values to
204 color related key.  \"xcolor.sty\" is preferred if both packages
205 are loaded."
206   (when (or (member "xcolor" (TeX-style-list))
207             (member "color" (TeX-style-list)))
208     (let* ((colorcmd (if (member "xcolor" (TeX-style-list))
209                          #'LaTeX-xcolor-definecolor-list
210                        #'LaTeX-color-definecolor-list))
211            (colorkeys '("bgcolor" "highlightcolor"
212                         "rulecolor" "spacecolor" "tabcolor"))
213            (opts (copy-alist LaTeX-minted-key-val-options-local)))
214       (dolist (key colorkeys)
215         (setq opts (assq-delete-all (car (assoc key opts)) opts))
216         (push (list key (mapcar #'car (funcall colorcmd)))
217               opts))
218       (setq LaTeX-minted-key-val-options-local
219             (copy-alist opts)))))
220
221 (defvar LaTeX-minted-auto-newminted nil)
222 (defvar LaTeX-minted-newminted-regexp
223   '("\\\\newminted\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
224     (1 2) LaTeX-minted-auto-newminted))
225
226 (defvar LaTeX-minted-auto-newmint nil)
227 (defvar LaTeX-minted-newmint-regexp
228   '("\\\\newmint\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
229     (1 2) LaTeX-minted-auto-newmint))
230
231 (defvar LaTeX-minted-auto-newmintinline nil)
232 (defvar LaTeX-minted-newmintinline-regexp
233   '("\\\\newmintinline\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
234     (1 2) LaTeX-minted-auto-newmintinline))
235
236 (defvar LaTeX-minted-auto-newmintedfile nil)
237 (defvar LaTeX-minted-newmintedfile-regexp
238   '("\\\\newmintedfile\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
239     (1 2) LaTeX-minted-auto-newmintedfile))
240
241 (defun LaTeX-minted-auto-prepare ()
242   (setq LaTeX-minted-auto-newminted     nil
243         LaTeX-minted-auto-newmint       nil
244         LaTeX-minted-auto-newmintinline nil
245         LaTeX-minted-auto-newmintedfile nil
246         LaTeX-minted-language-list      nil
247         LaTeX-minted-style-list         nil))
248
249 (defun LaTeX-minted-auto-cleanup ()
250   ;; \newminted{lang}{opts} => new langcode and langcode* envs.
251   ;; \newminted[envname]{lang}{opts} => new envname/envname* envs.
252   (dolist (name-lang LaTeX-minted-auto-newminted)
253     (let* ((env (if (> (length (car name-lang)) 0)
254                     (car name-lang)
255                   (concat (cadr name-lang) "code")))
256            (env* (concat env "*")))
257       (add-to-list 'LaTeX-auto-environment (list env))
258       (add-to-list 'LaTeX-auto-environment
259                    (list env* 'LaTeX-env-args
260                          '(TeX-arg-key-val LaTeX-minted-key-val-options-local)))
261       (add-to-list 'LaTeX-indent-environment-list `(,env current-indentation) t)
262       (add-to-list 'LaTeX-indent-environment-list `(,env* current-indentation) t)
263       (add-to-list 'LaTeX-verbatim-environments-local env)
264       (add-to-list 'LaTeX-verbatim-environments-local env*)))
265   ;; \newmint{foo}{opts} => \foo[key=vals]|code|
266   ;; \newmint[macname]{foo}{opts} => \macname[key=vals]|code|
267   (dolist (name-lang LaTeX-minted-auto-newmint)
268     (let ((lang (if (> (length (car name-lang)) 0)
269                     (car name-lang)
270                   (cadr name-lang))))
271       (add-to-list 'TeX-auto-symbol
272                    `(,lang [ TeX-arg-key-val LaTeX-minted-key-val-options-local ]
273                            TeX-arg-verb))
274       (add-to-list 'LaTeX-verbatim-macros-with-delims-local lang)
275       (when (and (fboundp 'font-latex-add-keywords)
276                  (fboundp 'font-latex-update-font-lock)
277                  (eq TeX-install-font-lock 'font-latex-setup))
278         (font-latex-add-keywords `((,lang "[")) 'textual))))
279   ;; \newmintinline{foo}{opts} => \fooinline[key=vals]|code| or
280   ;;                              \fooinline[key=vals]{code}
281   ;; \newmintinline[macname]{foo}{opts} => \macname[key=vals]|code| or
282   ;;                                       \macname[key=vals]{code}
283   (dolist (name-lang LaTeX-minted-auto-newmintinline)
284     (let ((lang (if (> (length (car name-lang)) 0)
285                     (car name-lang)
286                   (concat (cadr name-lang) "inline"))))
287       (add-to-list 'TeX-auto-symbol
288                    `(,lang [ TeX-arg-key-val LaTeX-minted-key-val-options-local ]
289                           TeX-arg-verb))
290       (add-to-list 'LaTeX-verbatim-macros-with-delims-local lang)
291       (add-to-list 'LaTeX-verbatim-macros-with-braces-local lang)
292       (when (and (fboundp 'font-latex-add-keywords)
293                  (fboundp 'font-latex-update-font-lock)
294                  (eq TeX-install-font-lock 'font-latex-setup))
295         (font-latex-add-keywords `((,lang "[")) 'textual))))
296   ;; \newmintedfile{foo}{opts} => \foofile[key=vals]{file-name}
297   ;; \newmintedfile[macname]{foo}{opts} => \macname[key=vals]{file-name}
298   (dolist (name-lang LaTeX-minted-auto-newmintedfile)
299     (let ((lang (if (> (length (car name-lang)) 0)
300                     (car name-lang)
301                   (concat (cadr name-lang) "file"))))
302       (add-to-list 'TeX-auto-symbol
303                    `(,lang [ TeX-arg-key-val LaTeX-minted-key-val-options-local ]
304                            TeX-arg-file))))
305   (when (and (fboundp 'font-latex-update-font-lock)
306              (eq TeX-install-font-lock 'font-latex-setup))
307     ;; Refresh font-locking so that the verbatim envs take effect.
308     (font-latex-update-font-lock t))
309   ;; Also update the key=vals
310   (LaTeX-minted-update-key-vals))
311
312 (add-hook 'TeX-auto-prepare-hook #'LaTeX-minted-auto-prepare t)
313 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-minted-auto-cleanup t)
314 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
315
316 (defun LaTeX-minted-add-syntactic-keywords-extra (type macro)
317   "Add MACRO from minted.sty to `font-latex-syntactic-keywords-extra'.
318 TYPE is one of the symbols `brace' or `delim' indicating how
319 verbatim text is enclosed after the macro.  MACRO is a string or
320 a list of strings."
321   (let ((syntax (if (eq type 'brace)
322                     '((1 "|") (2 "|"))
323                   '((1 "\"") (2 ".") (3 "\""))))
324         regexp)
325     (when (listp macro)
326       (setq macro (regexp-opt macro "\\(?:")))
327     (setq regexp `(,(concat
328                      ;; The backslash
329                      (regexp-quote TeX-esc)
330                      ;; Name of the macro(s)
331                      macro
332                      ;; The optional argument
333                      "\\(?:\\[[^][]*\\(?:\\[[^][]*\\][^][]*\\)*\\]\\)?"
334                      ;; The first mandatory argument
335                      "\\(?:{[^}]+}\\)"
336                      ;; With 'brace, allow braced sub-groups otherwise
337                      ;; we stop matching too early.  With 'delim, copy
338                      ;; font-latex.el:
339                      (if (eq type 'brace)
340                          (concat "\\({\\)"
341                                    "\\(?:[^}{]*"
342                                      "\\(?:{[^}{]*"
343                                        "\\(?:{[^}{]*"
344                                          "\\(?:{[^}{]*}[^}{]*\\)*"
345                                        "}[^}{]*\\)*"
346                                      "}[^}{]*\\)*"
347                                    "\\)"
348                                  "\\(}\\)")
349                        (concat
350                         ;; Opening delimiter
351                         "\\([^a-z@*\n\f{]\\).*?"
352                         ;; Closing delimiter
353                         "\\(" (regexp-quote TeX-esc) "*\\)\\(\\1\\)")))))
354     (add-to-list 'font-latex-syntactic-keywords-extra (append regexp syntax))))
355
356 (TeX-add-style-hook
357  "minted"
358  (lambda ()
359
360    ;; Activate local-version of key=vals
361    (setq LaTeX-minted-key-val-options-local
362          (copy-alist LaTeX-minted-key-val-options))
363
364    ;; New symbols
365    (TeX-add-symbols
366     '("mint"
367       [ TeX-arg-key-val LaTeX-minted-key-val-options-local ]
368       LaTeX-arg-minted-language TeX-arg-verb)
369     '("mintinline"
370       [ TeX-arg-key-val LaTeX-minted-key-val-options-local ]
371       LaTeX-arg-minted-language TeX-arg-verb)
372     '("newminted" ["Environment Name"] LaTeX-arg-minted-language
373       (TeX-arg-key-val LaTeX-minted-key-val-options-local))
374     '("newmint" ["Macro Name"] LaTeX-arg-minted-language
375       (TeX-arg-key-val LaTeX-minted-key-val-options-local))
376     '("newmintinline" ["Macro Name"] LaTeX-arg-minted-language
377       (TeX-arg-key-val LaTeX-minted-key-val-options-local))
378     '("newmintedfile" ["Macro Name"] LaTeX-arg-minted-language
379       (TeX-arg-key-val LaTeX-minted-key-val-options-local))
380     ;; 3.3 Formatting source code
381     '("inputminted"
382       [ TeX-arg-key-val LaTeX-minted-key-val-options-local ]
383       (LaTeX-arg-minted-language)
384       TeX-arg-file)
385     ;; 3.4 Using different styles
386     '("usemintedstyle"
387       [ LaTeX-arg-minted-language ] LaTeX-arg-minted-style)
388     ;; 5.2 Macro option usage
389     '("setminted"
390       [ LaTeX-arg-minted-language ]
391       (TeX-arg-key-val LaTeX-minted-key-val-options-local))
392     '("setmintedinline"
393       [ LaTeX-arg-minted-language ]
394       (TeX-arg-key-val LaTeX-minted-key-val-options-local)))
395
396    ;; New environments
397    (LaTeX-add-environments
398     '("minted" LaTeX-env-args [TeX-arg-key-val LaTeX-minted-key-val-options-local]
399       LaTeX-arg-minted-language))
400
401    ;; 4 Floating listings: If option "newfloat" is given, run the
402    ;; style hook and use the interface provided by the style,
403    ;; otherwise add "listing" manually
404    (if (or (LaTeX-provided-package-options-member "minted" "newfloat")
405            (LaTeX-provided-package-options-member "minted" "newfloat=true"))
406        (progn
407          (TeX-run-style-hooks "newfloat")
408          (LaTeX-add-newfloat-DeclareFloatingEnvironments
409           '("listing" "verbatim")))
410      (LaTeX-add-environments '("listing" ["Float Position"]))
411      (TeX-add-symbols '("listoflistings")
412                       '("listingscaption")
413                       '("listoflistingscaption"))
414      (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
415                   '("listing" current-indentation) t)
416      (add-to-list 'LaTeX-label-alist '("listing" . LaTeX-listing-label) t)
417      (when (fboundp 'reftex-add-label-environments)
418        (reftex-add-label-environments
419         '(("listing" ?l "lst:" "~\\ref{%s}" caption nil nil)))))
420
421    ;; Add to the auto parser
422    (TeX-auto-add-regexp LaTeX-minted-newminted-regexp)
423    (TeX-auto-add-regexp LaTeX-minted-newmint-regexp)
424    (TeX-auto-add-regexp LaTeX-minted-newmintinline-regexp)
425    (TeX-auto-add-regexp LaTeX-minted-newmintedfile-regexp)
426
427    ;; Filling
428    (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
429                 '("minted" current-indentation) t)
430    (add-to-list 'LaTeX-verbatim-environments-local "minted")
431
432    ;; Fontification
433    (when (and (fboundp 'font-latex-add-keywords)
434               (fboundp 'font-latex-update-font-lock)
435               (eq TeX-install-font-lock 'font-latex-setup))
436      (font-latex-add-keywords '(("usemintedstyle"  "[{")
437                                 ("setminted"       "[{")
438                                 ("setmintedinline" "[{")
439                                 ("newminted"       "[{{")
440                                 ("newmint"         "[{{")
441                                 ("newmintinline"   "[{{")
442                                 ("newmintedfile"   "[{{"))
443                               'function)
444      (font-latex-add-keywords '(("inputminted" "[{{")
445                                 ("mint"        "[{")
446                                 ("mintinline"  "[{"))
447                               'textual)
448      ;; Add \mint & \mintinline to
449      ;; `font-latex-syntactic-keywords-extra' and cater for their
450      ;; special syntax: \mint[optional]{lang}{verbatim} or
451      ;;                 \mint[optional]{lang}|verbatim|
452      (LaTeX-minted-add-syntactic-keywords-extra 'brace
453                                                 '("mint" "mintinline"))
454      (LaTeX-minted-add-syntactic-keywords-extra 'delim
455                                                 '("mint" "mintinline"))
456      ;; Tell font-lock about the update.
457      (font-latex-update-font-lock t)))
458  LaTeX-dialect)
459
460 (defvar LaTeX-minted-package-options '("chapter"     "cache"
461                                        "cachedir"    "finalizecache"
462                                        "frozencache" "draft"
463                                        "final"       "kpsewhich"
464                                        "langlinenos" "newfloat"
465                                        "outputdir"   "section")
466   "Package options for the minted package.")
467
468 ;;; minted.el ends here