1 ;;; minted.el --- AUCTeX style for `minted.sty' (v2.5)
3 ;; Copyright (C) 2014-2017 Free Software Foundation, Inc.
5 ;; Author: Tassilo Horn <tsdh@gnu.org>
6 ;; Maintainer: auctex-devel@gnu.org
10 ;; This file is part of AUCTeX.
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)
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.
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
29 ;; This file adds support for `minted.sty' (v2.5) from 2017/07/19.
35 (defvar LaTeX-minted-key-val-options
36 '(("autogobble" ("true" "false"))
37 ("baselinestretch" ("auto"))
38 ("beameroverlays" ("true" "false"))
40 ("breakaftergroup" ("true" "false"))
41 ("breakaftersymbolpre")
42 ("breakaftersymbolpost")
43 ("breakanywhere" ("true" "false"))
44 ("breakanywheresymbolpre")
45 ("breakanywheresymbolpost")
46 ("breakautoindent" ("true" "false"))
48 ("breakbeforegroup" ("true" "false"))
49 ("breakbeforesymbolpre")
50 ("breakbeforesymbolpost")
51 ("breakbytoken" ("true" "false"))
52 ("breakbytokenanywhere" ("true" "false"))
55 ("breaklines" ("true" "false"))
60 ("breaksymbolindentnchars")
61 ("breaksymbolindentleft")
62 ("breaksymbolindentleftnchars")
63 ("breaksymbolindentright")
64 ("breaksymbolseprightnchars")
66 ("breaksymbolsepnchars")
67 ("breaksymbolsepleft")
68 ("breaksymbolsepleftnchars")
69 ("breaksymbolsepright")
70 ("breaksymbolseprightnchars")
73 ("curlyquotes" ("true" "false"))
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"))
85 ("frame" ("none" "leftline" "topline" "bottomline" "lines" "single"))
88 ("funcnamehighlighting" ("true" "false"))
92 ("keywordcase" ("lower" "upper" "capitalize"))
94 ("labelposition" ("none" "topline" "bottomline" "all"))
96 ("linenos" ("true" "false"))
97 ("numberfirstline" ("true" "false"))
98 ("numbers" ("left" "right" "both" "none"))
99 ("mathescape" ("true" "false"))
100 ("numberblanklines" ("true" "false"))
102 ("obeytabs" ("true" "false"))
104 ("python3" ("true" "false"))
105 ("resetmargins" ("true" "false"))
107 ("samepage" ("true" "false"))
108 ("showspaces" ("true" "false"))
109 ("showtabs" ("true" "false"))
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"))
125 ("stepnumberfromfirst")
126 ("stepnumberoffsetvalues" ("true" "false"))
127 ("stripall" ("true" "false"))
128 ("stripnl" ("true" "false"))
132 ("texcl" ("true" "false"))
133 ("texcomments" ("true" "false"))
136 "Key=value options for minted macros and environments.")
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)
142 (defvar LaTeX-minted-pygmentize-program (executable-find "pygmentize"))
144 (defvar LaTeX-minted-language-list nil
145 "List containing languages provided by pymentize program.")
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
153 (shell-command (concat LaTeX-minted-pygmentize-program " -L lexers")
155 (goto-char (point-min))
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))))
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."
168 (completing-read (TeX-argument-prompt optional prompt "Language")
169 (LaTeX-minted-language-list))
172 (defvar LaTeX-minted-style-list nil
173 "List containing styles provided by pymentize program.")
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
181 (shell-command (concat LaTeX-minted-pygmentize-program " -L styles")
183 (goto-char (point-min))
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))))
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."
196 (completing-read (TeX-argument-prompt optional prompt "Style")
197 (LaTeX-minted-style-list))
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
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)))
218 (setq LaTeX-minted-key-val-options-local
219 (copy-alist opts)))))
221 (defvar LaTeX-minted-auto-newminted nil)
222 (defvar LaTeX-minted-newminted-regexp
223 '("\\\\newminted\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
224 (1 2) LaTeX-minted-auto-newminted))
226 (defvar LaTeX-minted-auto-newmint nil)
227 (defvar LaTeX-minted-newmint-regexp
228 '("\\\\newmint\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
229 (1 2) LaTeX-minted-auto-newmint))
231 (defvar LaTeX-minted-auto-newmintinline nil)
232 (defvar LaTeX-minted-newmintinline-regexp
233 '("\\\\newmintinline\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
234 (1 2) LaTeX-minted-auto-newmintinline))
236 (defvar LaTeX-minted-auto-newmintedfile nil)
237 (defvar LaTeX-minted-newmintedfile-regexp
238 '("\\\\newmintedfile\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
239 (1 2) LaTeX-minted-auto-newmintedfile))
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))
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)
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)
271 (add-to-list 'TeX-auto-symbol
272 `(,lang [ TeX-arg-key-val LaTeX-minted-key-val-options-local ]
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)
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 ]
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)
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 ]
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))
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)
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
321 (let ((syntax (if (eq type 'brace)
323 '((1 "\"") (2 ".") (3 "\""))))
326 (setq macro (regexp-opt macro "\\(?:")))
327 (setq regexp `(,(concat
329 (regexp-quote TeX-esc)
330 ;; Name of the macro(s)
332 ;; The optional argument
333 "\\(?:\\[[^][]*\\(?:\\[[^][]*\\][^][]*\\)*\\]\\)?"
334 ;; The first mandatory argument
336 ;; With 'brace, allow braced sub-groups otherwise
337 ;; we stop matching too early. With 'delim, copy
344 "\\(?:{[^}{]*}[^}{]*\\)*"
351 "\\([^a-z@*\n\f{]\\).*?"
353 "\\(" (regexp-quote TeX-esc) "*\\)\\(\\1\\)")))))
354 (add-to-list 'font-latex-syntactic-keywords-extra (append regexp syntax))))
360 ;; Activate local-version of key=vals
361 (setq LaTeX-minted-key-val-options-local
362 (copy-alist LaTeX-minted-key-val-options))
367 [ TeX-arg-key-val LaTeX-minted-key-val-options-local ]
368 LaTeX-arg-minted-language TeX-arg-verb)
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
382 [ TeX-arg-key-val LaTeX-minted-key-val-options-local ]
383 (LaTeX-arg-minted-language)
385 ;; 3.4 Using different styles
387 [ LaTeX-arg-minted-language ] LaTeX-arg-minted-style)
388 ;; 5.2 Macro option usage
390 [ LaTeX-arg-minted-language ]
391 (TeX-arg-key-val LaTeX-minted-key-val-options-local))
393 [ LaTeX-arg-minted-language ]
394 (TeX-arg-key-val LaTeX-minted-key-val-options-local)))
397 (LaTeX-add-environments
398 '("minted" LaTeX-env-args [TeX-arg-key-val LaTeX-minted-key-val-options-local]
399 LaTeX-arg-minted-language))
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"))
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")
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)))))
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)
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")
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" "[{")
438 ("setmintedinline" "[{")
441 ("newmintinline" "[{{")
442 ("newmintedfile" "[{{"))
444 (font-latex-add-keywords '(("inputminted" "[{{")
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)))
460 (defvar LaTeX-minted-package-options '("chapter" "cache"
461 "cachedir" "finalizecache"
462 "frozencache" "draft"
464 "langlinenos" "newfloat"
465 "outputdir" "section")
466 "Package options for the minted package.")
468 ;;; minted.el ends here