summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/minted.el
blob: 354213be8a0660701aca67e1ae248ae139c0d766 (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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
;;; minted.el --- AUCTeX style for `minted.sty' (v2.5)  -*- lexical-binding: t; -*-

;; Copyright (C) 2014-2021 Free Software Foundation, Inc.

;; Author: Tassilo Horn <tsdh@gnu.org>
;; Maintainer: auctex-devel@gnu.org
;; Created: 2014-12-19
;; 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 `minted.sty' (v2.5) from 2017/07/19.

;;; Code:

(require 'tex)
(require 'latex)

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

(declare-function font-latex-set-syntactic-keywords
                  "font-latex")

(declare-function LaTeX-color-definecolor-list "color" ())
(declare-function LaTeX-xcolor-definecolor-list "xcolor" ())
(declare-function LaTeX-add-newfloat-DeclareFloatingEnvironments
                  "newfloat" (&rest newfloat-declarefloatingenvironments))

(defvar font-latex-syntactic-keywords-extra)

(defvar LaTeX-minted-key-val-options
  '(("autogobble" ("true" "false"))
    ("baselinestretch" ("auto"))
    ("beameroverlays" ("true" "false"))
    ("breakafter")
    ("breakaftergroup" ("true" "false"))
    ("breakaftersymbolpre")
    ("breakaftersymbolpost")
    ("breakanywhere" ("true" "false"))
    ("breakanywheresymbolpre")
    ("breakanywheresymbolpost")
    ("breakautoindent" ("true" "false"))
    ("breakbefore")
    ("breakbeforegroup" ("true" "false"))
    ("breakbeforesymbolpre")
    ("breakbeforesymbolpost")
    ("breakbytoken" ("true" "false"))
    ("breakbytokenanywhere" ("true" "false"))
    ("breakindent")
    ("breakindentnchars")
    ("breaklines" ("true" "false"))
    ("breaksymbol")
    ("breaksymbolleft")
    ("breaksymbolright")
    ("breaksymbolindent")
    ("breaksymbolindentnchars")
    ("breaksymbolindentleft")
    ("breaksymbolindentleftnchars")
    ("breaksymbolindentright")
    ("breaksymbolseprightnchars")
    ("breaksymbolsep")
    ("breaksymbolsepnchars")
    ("breaksymbolsepleft")
    ("breaksymbolsepleftnchars")
    ("breaksymbolsepright")
    ("breaksymbolseprightnchars")
    ("bgcolor")
    ("codetagify")
    ("curlyquotes" ("true" "false"))
    ("encoding")
    ("escapeinside")
    ("firstline")
    ("firstnumber" ("auto" "last" "integer"))
    ("fontfamily" ("tt" "courier" "helvetica"))
    ("fontseries" ("auto"))
    ("fontsize" ("auto" "\\tiny" "\\large" "\\scriptsize" "\\Large"
                 "\\footnotesize" "\\LARGE" "\\small" "\\huge"
                 "\\normalsize" "\\Huge"))
    ("fontshape" ("auto"))
    ("formatcom")
    ("frame" ("none" "leftline" "topline" "bottomline" "lines" "single"))
    ("framerule")
    ("framesep")
    ("funcnamehighlighting" ("true" "false"))
    ("gobble")
    ("highlightcolor")
    ("highlightlines")
    ("keywordcase" ("lower" "upper" "capitalize"))
    ("label")
    ("labelposition" ("none" "topline" "bottomline" "all"))
    ("lastline")
    ("linenos" ("true" "false"))
    ("numberfirstline" ("true" "false"))
    ("numbers" ("left" "right" "both" "none"))
    ("mathescape" ("true" "false"))
    ("numberblanklines" ("true" "false"))
    ("numbersep")
    ("obeytabs" ("true" "false"))
    ("outencoding")
    ("python3" ("true" "false"))
    ("resetmargins" ("true" "false"))
    ("rulecolor")
    ("samepage" ("true" "false"))
    ("showspaces" ("true" "false"))
    ("showtabs" ("true" "false"))
    ("space")
    ("spacecolor")
    ("startinline" ("true" "false"))
    ;; FIXME: It would be nice to use the function
    ;; `LaTeX-minted-style-list' here, but with a file local var like:
    ;;     %%% TeX-command-extra-options: "-shell-escape"
    ;; in a .tex file, Emacs asks to apply a variable which is not
    ;; safe and does not restore the window; the splitted frame
    ;; remains.  I couldn't figure out why, so for now, I add the
    ;; styles from Pygments version 2.11 here.
    ("style" ("abap" "algol" "algol_nu" "arduino" "autumn"
              "borland" "bw" "colorful" "default" "dracula"
              "emacs" "friendly" "friendly_grayscale" "fruity"
              "gruvbox-dark" "gruvbox-light" "igor" "inkpot"
              "lilypond" "lovelace" "manni" "material"
              "monokai" "murphy" "native" "one-dark"
              "paraiso-dark" "paraiso-light" "pastie" "perldoc"
              "rainbow_dash" "rrt" "sas" "solarized-dark"
              "solarized-light" "stata-dark" "stata-light"
              "stata" "tango" "trac" "vim" "vs" "xcode"
              "zenburn"))
    ("stepnumber")
    ("stepnumberfromfirst")
    ("stepnumberoffsetvalues" ("true" "false"))
    ("stripall" ("true" "false"))
    ("stripnl" ("true" "false"))
    ("tab")
    ("tabcolor")
    ("tabsize")
    ("texcl" ("true" "false"))
    ("texcomments" ("true" "false"))
    ("xleftmargin")
    ("xrightmargin"))
  "Key=value options for minted macros and environments.")

(defun LaTeX-minted-key-val-options ()
  "Return an updated list of key=vals from minted package.
This function retrieves values of (user) defined colors and
prepends them to variable `LaTeX-minted-key-val-options'."
  (append
   (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))
            (colors (mapcar #'car (funcall colorcmd)))
            (keys '("bgcolor" "highlightcolor"
                    "rulecolor" "spacecolor" "tabcolor"))
            result)
       (dolist (key keys result)
         (push (list key colors) result))))
   LaTeX-minted-key-val-options))

(defvar LaTeX-minted-pygmentize-program (executable-find "pygmentize"))

(defvar LaTeX-minted-language-list nil
  "List containing languages provided by pymentize program.")

(defun LaTeX-minted-language-list (&rest _ignored)
  "Return a list of languages provided by pymentize program.
Update the variable `LaTeX-minted-language-list' if still nil."
  (or LaTeX-minted-language-list
      (when LaTeX-minted-pygmentize-program
        (with-temp-buffer
          (shell-command (concat LaTeX-minted-pygmentize-program " -L lexers")
                         (current-buffer))
          (goto-char (point-min))
          (let (languages)
            (while (re-search-forward "^\\*[[:space:]]\\([^:]+\\):" nil t)
              (dolist (lang (split-string (match-string 1) "[[:space:],]" t))
                (push lang languages)))
            (setq LaTeX-minted-language-list languages))
          LaTeX-minted-language-list))))

(defun LaTeX-arg-minted-language (optional &optional prompt)
    "Insert a selected pygmentize language as argument for macros from minted.sty.
If OPTIONAL is non-nil, insert it as optional argument in
brackets.  PROMPT replaces the standard one."
  (TeX-argument-insert
   (completing-read (TeX-argument-prompt optional prompt "Language")
                    (LaTeX-minted-language-list))
   optional))

(defvar LaTeX-minted-style-list nil
  "List containing styles provided by pymentize program.")

(defun LaTeX-minted-style-list (&rest _ignored)
  "Return a list of styles provided by pymentize program.
Update the variable `LaTeX-minted-style-list' if still nil."
  (or LaTeX-minted-style-list
      (when LaTeX-minted-pygmentize-program
        (with-temp-buffer
          (shell-command (concat LaTeX-minted-pygmentize-program " -L styles")
                         (current-buffer))
          (goto-char (point-min))
          (let (styles)
            (while (re-search-forward "^\\*[[:space:]]\\([^:]+\\):" nil t)
              (dolist (style (split-string (match-string 1) "[[:space:],]" t))
                (push style styles)))
            (setq LaTeX-minted-style-list styles))
          LaTeX-minted-style-list))))

(defun LaTeX-arg-minted-style (optional &optional prompt)
  "Insert a selected pygmentize style as argument for macros from minted.sty.
If OPTIONAL is non-nil, insert it as optional argument in
brackets.  PROMPT replaces the standard one."
  (TeX-argument-insert
   (completing-read (TeX-argument-prompt optional prompt "Style")
                    (LaTeX-minted-style-list))
   optional))

(defvar LaTeX-minted-auto-newminted nil)
(defvar LaTeX-minted-newminted-regexp
  '("\\\\newminted\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
    (1 2) LaTeX-minted-auto-newminted))

(defvar LaTeX-minted-auto-newmint nil)
(defvar LaTeX-minted-newmint-regexp
  '("\\\\newmint\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
    (1 2) LaTeX-minted-auto-newmint))

(defvar LaTeX-minted-auto-newmintinline nil)
(defvar LaTeX-minted-newmintinline-regexp
  '("\\\\newmintinline\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
    (1 2) LaTeX-minted-auto-newmintinline))

(defvar LaTeX-minted-auto-newmintedfile nil)
(defvar LaTeX-minted-newmintedfile-regexp
  '("\\\\newmintedfile\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
    (1 2) LaTeX-minted-auto-newmintedfile))

(defun LaTeX-minted-auto-prepare ()
  (setq LaTeX-minted-auto-newminted     nil
        LaTeX-minted-auto-newmint       nil
        LaTeX-minted-auto-newmintinline nil
        LaTeX-minted-auto-newmintedfile nil
        LaTeX-minted-language-list      nil
        LaTeX-minted-style-list         nil))

(defun LaTeX-minted-auto-cleanup ()
  ;; \newminted{lang}{opts} => new langcode and langcode* envs.
  ;; \newminted[envname]{lang}{opts} => new envname/envname* envs.
  (dolist (name-lang LaTeX-minted-auto-newminted)
    (let* ((env (if (> (length (car name-lang)) 0)
                    (car name-lang)
                  (concat (cadr name-lang) "code")))
           (env* (concat env "*")))
      (add-to-list 'LaTeX-auto-environment (list env))
      (add-to-list 'LaTeX-auto-environment
                   (list env* #'LaTeX-env-args
                         '(TeX-arg-key-val (LaTeX-minted-key-val-options))))
      (add-to-list 'LaTeX-indent-environment-list `(,env current-indentation) t)
      (add-to-list 'LaTeX-indent-environment-list `(,env* current-indentation) t)
      (add-to-list 'LaTeX-verbatim-environments-local env)
      (add-to-list 'LaTeX-verbatim-environments-local env*)))
  ;; \newmint{foo}{opts} => \foo[key=vals]|code|
  ;; \newmint[macname]{foo}{opts} => \macname[key=vals]|code|
  (dolist (name-lang LaTeX-minted-auto-newmint)
    (let ((lang (if (> (length (car name-lang)) 0)
                    (car name-lang)
                  (cadr name-lang))))
      (add-to-list 'TeX-auto-symbol
                   `(,lang [TeX-arg-key-val (LaTeX-minted-key-val-options)]
                           TeX-arg-verb))
      (add-to-list 'LaTeX-verbatim-macros-with-delims-local lang)
      (when (and (fboundp 'font-latex-add-keywords)
                 (eq TeX-install-font-lock 'font-latex-setup))
        (font-latex-add-keywords `((,lang "[")) 'textual))))
  ;; \newmintinline{foo}{opts} => \fooinline[key=vals]|code| or
  ;;                              \fooinline[key=vals]{code}
  ;; \newmintinline[macname]{foo}{opts} => \macname[key=vals]|code| or
  ;;                                       \macname[key=vals]{code}
  (dolist (name-lang LaTeX-minted-auto-newmintinline)
    (let ((lang (if (> (length (car name-lang)) 0)
                    (car name-lang)
                  (concat (cadr name-lang) "inline"))))
      (add-to-list 'TeX-auto-symbol
                   `(,lang [TeX-arg-key-val (LaTeX-minted-key-val-options)]
                           TeX-arg-verb-delim-or-brace))
      (add-to-list 'LaTeX-verbatim-macros-with-delims-local lang)
      (add-to-list 'LaTeX-verbatim-macros-with-braces-local lang)
      (when (and (fboundp 'font-latex-add-keywords)
                 (eq TeX-install-font-lock 'font-latex-setup))
        (font-latex-add-keywords `((,lang "[")) 'textual))))
  ;; \newmintedfile{foo}{opts} => \foofile[key=vals]{file-name}
  ;; \newmintedfile[macname]{foo}{opts} => \macname[key=vals]{file-name}
  (dolist (name-lang LaTeX-minted-auto-newmintedfile)
    (let ((lang (if (> (length (car name-lang)) 0)
                    (car name-lang)
                  (concat (cadr name-lang) "file"))))
      (add-to-list 'TeX-auto-symbol
                   `(,lang [TeX-arg-key-val (LaTeX-minted-key-val-options)]
                           TeX-arg-file))))
  (when (and (fboundp 'font-latex-set-syntactic-keywords)
             (eq TeX-install-font-lock 'font-latex-setup))
    ;; Refresh font-locking so that the verbatim envs take effect.
    (font-latex-set-syntactic-keywords)))

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

(defun LaTeX-minted-add-syntactic-keywords-extra (type macro)
  "Add MACRO from minted.sty to `font-latex-syntactic-keywords-extra'.
TYPE is one of the symbols `brace' or `delim' indicating how
verbatim text is enclosed after the macro.  MACRO is a string or
a list of strings."
  (let ((syntax (if (eq type 'brace)
                    '((1 "|") (2 "|"))
                  '((1 "\"") (2 ".") (3 "\""))))
        regexp)
    (when (listp macro)
      (setq macro (regexp-opt macro "\\(?:")))
    (setq regexp `(,(concat
                     ;; The backslash
                     (regexp-quote TeX-esc)
                     ;; Name of the macro(s)
                     macro
                     ;; The optional argument
                     "\\(?:\\[[^][]*\\(?:\\[[^][]*\\][^][]*\\)*\\]\\)?"
                     ;; The first mandatory argument
                     "\\(?:{[^}]+}\\)"
                     ;; With 'brace, allow braced sub-groups otherwise
                     ;; we stop matching too early.  With 'delim, copy
                     ;; font-latex.el:
                     (if (eq type 'brace)
                         (concat "\\({\\)"
                                   "\\(?:[^}{]*"
                                     "\\(?:{[^}{]*"
                                       "\\(?:{[^}{]*"
                                         "\\(?:{[^}{]*}[^}{]*\\)*"
                                       "}[^}{]*\\)*"
                                     "}[^}{]*\\)*"
                                   "\\)"
                                 "\\(}\\)")
                       (concat
                        ;; Opening delimiter
                        "\\([^a-z@*\n\f{]\\).*?"
                        ;; Closing delimiter
                        "\\(" (regexp-quote TeX-esc) "*\\)\\(\\1\\)")))))
    (add-to-list 'font-latex-syntactic-keywords-extra (append regexp syntax))))

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

   ;; New symbols
   (TeX-add-symbols
    '("mint"
      [TeX-arg-key-val (LaTeX-minted-key-val-options)]
      LaTeX-arg-minted-language TeX-arg-verb)
    '("mintinline"
      [TeX-arg-key-val (LaTeX-minted-key-val-options)]
      LaTeX-arg-minted-language TeX-arg-verb-delim-or-brace)
    '("newminted" ["Environment Name"] LaTeX-arg-minted-language
      (TeX-arg-key-val (LaTeX-minted-key-val-options)))
    '("newmint" ["Macro Name"] LaTeX-arg-minted-language
      (TeX-arg-key-val (LaTeX-minted-key-val-options)))
    '("newmintinline" ["Macro Name"] LaTeX-arg-minted-language
      (TeX-arg-key-val (LaTeX-minted-key-val-options)))
    '("newmintedfile" ["Macro Name"] LaTeX-arg-minted-language
      (TeX-arg-key-val (LaTeX-minted-key-val-options)))
    ;; 3.3 Formatting source code
    '("inputminted"
      [TeX-arg-key-val (LaTeX-minted-key-val-options)]
      (LaTeX-arg-minted-language)
      TeX-arg-file)
    ;; 3.4 Using different styles
    '("usemintedstyle"
      [ LaTeX-arg-minted-language ] LaTeX-arg-minted-style)
    ;; 5.2 Macro option usage
    '("setminted"
      [ LaTeX-arg-minted-language ]
      (TeX-arg-key-val (LaTeX-minted-key-val-options)))
    '("setmintedinline"
      [ LaTeX-arg-minted-language ]
      (TeX-arg-key-val (LaTeX-minted-key-val-options))))

   ;; New environments
   (LaTeX-add-environments
    '("minted" LaTeX-env-args [TeX-arg-key-val (LaTeX-minted-key-val-options)]
      LaTeX-arg-minted-language))

   ;; 4 Floating listings: If option "newfloat" is given, run the
   ;; style hook and use the interface provided by the style,
   ;; otherwise add "listing" manually
   (if (or (LaTeX-provided-package-options-member "minted" "newfloat")
           (LaTeX-provided-package-options-member "minted" "newfloat=true"))
       (progn
         (TeX-run-style-hooks "newfloat")
         (LaTeX-add-newfloat-DeclareFloatingEnvironments
          '("listing" "verbatim")))
     (LaTeX-add-environments '("listing" ["Float Position"]))
     (TeX-add-symbols '("listoflistings")
                      '("listingscaption")
                      '("listoflistingscaption"))
     (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
                  '("listing" current-indentation) t)
     (add-to-list 'LaTeX-label-alist '("listing" . LaTeX-listing-label) t)
     (when (fboundp 'reftex-add-label-environments)
       (reftex-add-label-environments
        '(("listing" ?l "lst:" "~\\ref{%s}" caption nil nil)))))

   ;; Add to the auto parser
   (TeX-auto-add-regexp LaTeX-minted-newminted-regexp)
   (TeX-auto-add-regexp LaTeX-minted-newmint-regexp)
   (TeX-auto-add-regexp LaTeX-minted-newmintinline-regexp)
   (TeX-auto-add-regexp LaTeX-minted-newmintedfile-regexp)

   ;; Filling
   (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
                '("minted" current-indentation) t)
   (add-to-list 'LaTeX-verbatim-environments-local "minted")

   ;; Fontification
   (when (and (fboundp 'font-latex-add-keywords)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("usemintedstyle"  "[{")
                                ("setminted"       "[{")
                                ("setmintedinline" "[{")
                                ("newminted"       "[{{")
                                ("newmint"         "[{{")
                                ("newmintinline"   "[{{")
                                ("newmintedfile"   "[{{"))
                              'function)
     (font-latex-add-keywords '(("inputminted" "[{{")
                                ("mint"        "[{")
                                ("mintinline"  "[{"))
                              'textual)
     ;; Add \mint & \mintinline to
     ;; `font-latex-syntactic-keywords-extra' and cater for their
     ;; special syntax: \mint[optional]{lang}{verbatim} or
     ;;                 \mint[optional]{lang}|verbatim|
     (LaTeX-minted-add-syntactic-keywords-extra 'brace
                                                '("mint" "mintinline"))
     (LaTeX-minted-add-syntactic-keywords-extra 'delim
                                                '("mint" "mintinline"))
     ;; Tell font-lock about the update.
     (font-latex-set-syntactic-keywords)))
 TeX-dialect)

(defvar LaTeX-minted-package-options '("chapter"     "cache"
                                       "cachedir"    "finalizecache"
                                       "frozencache" "draft"
                                       "final"       "kpsewhich"
                                       "langlinenos" "newfloat"
                                       "outputdir"   "section")
  "Package options for the minted package.")

;;; minted.el ends here