PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/elpa/auctex-11.90.0/style/minted.el

https://gitlab.com/csagedy/prelude
Emacs Lisp | 388 lines | 292 code | 37 blank | 59 comment | 9 complexity | 482a678d1d2c5e09fd6473ac995201da MD5 | raw file
  1. ;;; minted.el --- AUCTeX style for `minted.sty' (v2.4.1)
  2. ;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
  3. ;; Author: Tassilo Horn <tsdh@gnu.org>
  4. ;; Maintainer: auctex-devel@gnu.org
  5. ;; Created: 2014-12-19
  6. ;; Keywords: tex
  7. ;; This file is part of AUCTeX.
  8. ;; AUCTeX is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;; AUCTeX is distributed in the hope that it will be useful, but
  13. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ;; General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with AUCTeX; see the file COPYING. If not, write to the Free
  18. ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. ;; 02110-1301, USA.
  20. ;;; Commentary:
  21. ;; This file adds support for `minted.sty' (v2.4.1) from 2016/10/31.
  22. ;;; Code:
  23. (require 'tex)
  24. (defvar LaTeX-minted-key-val-options
  25. '(("autogobble" ("true" "false"))
  26. ("baselinestretch" ("auto"))
  27. ("breakafter")
  28. ("breakaftergroup" ("true" "false"))
  29. ("breakaftersymbolpre")
  30. ("breakaftersymbolpost")
  31. ("breakbytoken" ("true" "false"))
  32. ("breakbytokenanywhere" ("true" "false"))
  33. ("breakindent")
  34. ("breaklines" ("true" "false"))
  35. ("breaksymbol")
  36. ("breaksymbolleft")
  37. ("breaksymbolright")
  38. ("breaksymbolindent")
  39. ("breaksymbolindentleft")
  40. ("breaksymbolindentright")
  41. ("breaksymbolsep")
  42. ("breaksymbolsepleft")
  43. ("breaksymbolsepright")
  44. ("bgcolor")
  45. ("codetagify")
  46. ("curlyquotes" ("true" "false"))
  47. ("encoding")
  48. ("outencoding")
  49. ("escapeinside")
  50. ("firstline")
  51. ("firstnumber" ("auto" "last" "integer"))
  52. ("fontfamily" ("tt" "courier" "helvetica"))
  53. ("fontseries" ("auto"))
  54. ("fontsize" ("auto" "\\tiny" "\\large" "\\scriptsize" "\\Large"
  55. "\\footnotesize" "\\LARGE" "\\small" "\\huge"
  56. "\\normalsize" "\\Huge"))
  57. ("fontshape" ("auto"))
  58. ("formatcom")
  59. ("frame" ("none" "leftline" "topline" "bottomline" "lines" "single"))
  60. ("framerule")
  61. ("framesep")
  62. ("funcnamehighlighting" ("true" "false"))
  63. ("gobble")
  64. ("highlightcolor")
  65. ("highlightlines")
  66. ("keywordcase" ("lower" "upper" "capitalize"))
  67. ("label")
  68. ("labelposition" ("none" "topline" "bottomline" "all"))
  69. ("lastline")
  70. ("linenos" ("true" "false"))
  71. ("numberfirstline" ("true" "false"))
  72. ("numbers" ("left" "right" "both" "none"))
  73. ("mathescape" ("true" "false"))
  74. ("numberblanklines" ("true" "false"))
  75. ("numbersep")
  76. ("obeytabs" ("true" "false"))
  77. ("python3" ("true" "false"))
  78. ("resetmargins" ("true" "false"))
  79. ("rulecolor")
  80. ("samepage" ("true" "false"))
  81. ("showspaces" ("true" "false"))
  82. ("showtabs" ("true" "false"))
  83. ("space")
  84. ("spacecolor")
  85. ("startinline" ("true" "false"))
  86. ;; FIXME: It would be nice to use the function
  87. ;; `LaTeX-minted-style-list' here, but with a file local var like:
  88. ;; %%% TeX-command-extra-options: "-shell-escape"
  89. ;; in a .tex file, Emacs asks to apply a variable which is not
  90. ;; safe and does not restore the frame; the splitted frame
  91. ;; remains. I couldn't figure out why, so for now, I add the
  92. ;; styles from Pygments version 2.1.3 here.
  93. ("style" ("colorful" "default" "emacs" "friendly" "fruity" "igor"
  94. "lovelace" "manni" "monokai" "murphy" "native"
  95. "paraiso-dark" "paraiso-light" "pastie" "perldoc"
  96. "rrt" "tango" "trac" "vim" "vs" "xcode"))
  97. ("stepnumber")
  98. ("stepnumberfromfirst")
  99. ("stepnumberoffsetvalues" ("true" "false"))
  100. ("stripall" ("true" "false"))
  101. ("stripnl")
  102. ("tab")
  103. ("tabcolor")
  104. ("tabsize")
  105. ("texcl" ("true" "false"))
  106. ("texcomments" ("true" "false"))
  107. ("xleftmargin")
  108. ("xrightmargin"))
  109. "Key=value options for minted macros and environments.")
  110. (defvar LaTeX-minted-key-val-options-local nil
  111. "Buffer-local key=value options for minted macros and environments.")
  112. (make-variable-buffer-local 'LaTeX-minted-key-val-options-local)
  113. (defvar LaTeX-minted-pygmentize-program (executable-find "pygmentize"))
  114. (defvar LaTeX-minted-language-list nil
  115. "List containing languages provided by pymentize program.")
  116. (defun LaTeX-minted-language-list (&rest _ignored)
  117. "Return a list of languages provided by pymentize program.
  118. Update the variable `LaTeX-minted-language-list' if still nil."
  119. (or LaTeX-minted-language-list
  120. (when LaTeX-minted-pygmentize-program
  121. (with-temp-buffer
  122. (shell-command (concat LaTeX-minted-pygmentize-program " -L lexers")
  123. (current-buffer))
  124. (goto-char (point-min))
  125. (let (languages)
  126. (while (re-search-forward "^\\*[[:space:]]\\([^:]+\\):" nil t)
  127. (dolist (lang (split-string (match-string 1) "[[:space:],]" t))
  128. (push lang languages)))
  129. (setq LaTeX-minted-language-list languages))
  130. LaTeX-minted-language-list))))
  131. (defun LaTeX-arg-minted-language (optional &optional prompt)
  132. "Insert a selected pygmentize language as argument for macros from minted.sty.
  133. If OPTIONAL is non-nil, insert it as optional argument in
  134. brackets. PROMPT replaces the standard one."
  135. (TeX-argument-insert
  136. (completing-read (TeX-argument-prompt optional prompt "Language")
  137. (LaTeX-minted-language-list))
  138. optional))
  139. (defvar LaTeX-minted-style-list nil
  140. "List containing styles provided by pymentize program.")
  141. (defun LaTeX-minted-style-list (&rest _ignored)
  142. "Return a list of styles provided by pymentize program.
  143. Update the variable `LaTeX-minted-style-list' if still nil."
  144. (or LaTeX-minted-style-list
  145. (when LaTeX-minted-pygmentize-program
  146. (with-temp-buffer
  147. (shell-command (concat LaTeX-minted-pygmentize-program " -L styles")
  148. (current-buffer))
  149. (goto-char (point-min))
  150. (let (styles)
  151. (while (re-search-forward "^\\*[[:space:]]\\([^:]+\\):" nil t)
  152. (dolist (style (split-string (match-string 1) "[[:space:],]" t))
  153. (push style styles)))
  154. (setq LaTeX-minted-style-list styles))
  155. LaTeX-minted-style-list))))
  156. (defun LaTeX-arg-minted-style (optional &optional prompt)
  157. "Insert a selected pygmentize style as argument for macros from minted.sty.
  158. If OPTIONAL is non-nil, insert it as optional argument in
  159. brackets. PROMPT replaces the standard one."
  160. (TeX-argument-insert
  161. (completing-read (TeX-argument-prompt optional prompt "Style")
  162. (LaTeX-minted-style-list))
  163. optional))
  164. (defun LaTeX-minted-update-key-vals ()
  165. "Update color related key-vals in `LaTeX-minted-key-val-options-local'.
  166. This function checks if one of the packages \"xcolor.sty\" or
  167. \"xcolor.sty\" is loaded and appends defined colors as values to
  168. color related key. \"xcolor.sty\" is preferred if both packages
  169. are loaded."
  170. (when (or (member "xcolor" (TeX-style-list))
  171. (member "color" (TeX-style-list)))
  172. (let* ((colorcmd (if (member "xcolor" (TeX-style-list))
  173. #'LaTeX-xcolor-definecolor-list
  174. #'LaTeX-color-definecolor-list))
  175. (colorkeys '("bgcolor" "highlightcolor"
  176. "rulecolor" "spacecolor" "tabcolor"))
  177. (opts (copy-alist LaTeX-minted-key-val-options-local)))
  178. (dolist (key colorkeys)
  179. (assq-delete-all (car (assoc key opts)) opts)
  180. (push (list key (mapcar #'car (funcall colorcmd)))
  181. opts))
  182. (setq LaTeX-minted-key-val-options-local
  183. (copy-alist opts)))))
  184. (defvar LaTeX-minted-auto-newminted nil)
  185. (defvar LaTeX-minted-newminted-regexp
  186. '("\\\\newminted\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
  187. (1 2) LaTeX-minted-auto-newminted))
  188. (defvar LaTeX-minted-auto-newmint nil)
  189. (defvar LaTeX-minted-newmint-regexp
  190. '("\\\\newmint\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
  191. (1 2) LaTeX-minted-auto-newmint))
  192. (defvar LaTeX-minted-auto-newmintinline nil)
  193. (defvar LaTeX-minted-newmintinline-regexp
  194. '("\\\\newmintinline\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
  195. (1 2) LaTeX-minted-auto-newmintinline))
  196. (defvar LaTeX-minted-auto-newmintedfile nil)
  197. (defvar LaTeX-minted-newmintedfile-regexp
  198. '("\\\\newmintedfile\\(?:\\[\\([^]]+\\)\\]\\)?{\\([^}]+\\)}{[^}]*}"
  199. (1 2) LaTeX-minted-auto-newmintedfile))
  200. (defun LaTeX-minted-auto-prepare ()
  201. (setq LaTeX-minted-auto-newminted nil
  202. LaTeX-minted-auto-newmint nil
  203. LaTeX-minted-auto-newmintinline nil
  204. LaTeX-minted-auto-newmintedfile nil
  205. LaTeX-minted-language-list nil
  206. LaTeX-minted-style-list nil))
  207. (defun LaTeX-minted-auto-cleanup ()
  208. ;; \newminted{lang}{opts} => new langcode and langcode* envs.
  209. ;; \newminted[envname]{lang}{opts} => new envname/envname* envs.
  210. (dolist (name-lang LaTeX-minted-auto-newminted)
  211. (let* ((env (if (> (length (car name-lang)) 0)
  212. (car name-lang)
  213. (concat (cadr name-lang) "code")))
  214. (env* (concat env "*")))
  215. (add-to-list 'LaTeX-auto-environment (list env))
  216. (add-to-list 'LaTeX-auto-environment
  217. (list env* 'LaTeX-env-args
  218. '(TeX-arg-key-val LaTeX-minted-key-val-options-local)))
  219. (add-to-list 'LaTeX-indent-environment-list `(,env current-indentation) t)
  220. (add-to-list 'LaTeX-indent-environment-list `(,env* current-indentation) t)
  221. (add-to-list 'LaTeX-verbatim-environments-local env)
  222. (add-to-list 'LaTeX-verbatim-environments-local env*)))
  223. ;; \newmint{foo}{opts} => \foo|code|
  224. ;; \newmint[macname]{foo}{opts} => \macname|code|
  225. (dolist (name-lang LaTeX-minted-auto-newmint)
  226. (let ((lang (if (> (length (car name-lang)) 0)
  227. (car name-lang)
  228. (cadr name-lang))))
  229. (add-to-list 'TeX-auto-symbol lang)
  230. (add-to-list 'LaTeX-verbatim-macros-with-delims-local lang)))
  231. ;; \newmintinline{foo}{opts} => \fooinline|code|
  232. ;; \newmintinline[macname]{foo}{opts} => \macname|code|
  233. (dolist (name-lang LaTeX-minted-auto-newmintinline)
  234. (let ((lang (if (> (length (car name-lang)) 0)
  235. (car name-lang)
  236. (cadr name-lang))))
  237. (add-to-list 'TeX-auto-symbol lang)
  238. (add-to-list 'LaTeX-verbatim-macros-with-delims-local (concat lang "inline"))))
  239. ;; \newmintedfile{foo}{opts} => \foofile{file-name}
  240. ;; \newmintedfile[macname]{foo}{opts} => \macname{file-name}
  241. (dolist (name-lang LaTeX-minted-auto-newmintedfile)
  242. (let ((lang (if (> (length (car name-lang)) 0)
  243. (car name-lang)
  244. (cadr name-lang))))
  245. (add-to-list 'TeX-auto-symbol (list lang 'TeX-arg-file))))
  246. (when (and (fboundp 'font-latex-update-font-lock)
  247. (eq TeX-install-font-lock 'font-latex-setup))
  248. ;; Refresh font-locking so that the verbatim envs take effect.
  249. (font-latex-update-font-lock t))
  250. ;; Also update the key=vals
  251. (LaTeX-minted-update-key-vals))
  252. (add-hook 'TeX-auto-prepare-hook #'LaTeX-minted-auto-prepare t)
  253. (add-hook 'TeX-auto-cleanup-hook #'LaTeX-minted-auto-cleanup t)
  254. (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
  255. (TeX-add-style-hook
  256. "minted"
  257. (lambda ()
  258. ;; Activate local-version of key=vals
  259. (setq LaTeX-minted-key-val-options-local
  260. (copy-alist LaTeX-minted-key-val-options))
  261. ;; New symbols
  262. (TeX-add-symbols
  263. '("mint" LaTeX-arg-minted-language TeX-arg-verb)
  264. '("mintinline" LaTeX-arg-minted-language TeX-arg-verb)
  265. '("newminted" ["Environment Name"] LaTeX-arg-minted-language
  266. (TeX-arg-key-val LaTeX-minted-key-val-options-local))
  267. '("newmint" ["Macro Name"] LaTeX-arg-minted-language
  268. (TeX-arg-key-val LaTeX-minted-key-val-options-local))
  269. '("newmintinline" ["Macro Name"] LaTeX-arg-minted-language
  270. (TeX-arg-key-val LaTeX-minted-key-val-options-local))
  271. '("newmintedfile" ["Macro Name"] LaTeX-arg-minted-language
  272. (TeX-arg-key-val LaTeX-minted-key-val-options-local))
  273. ;; 3.3 Formatting source code
  274. '("inputminted"
  275. [ TeX-arg-key-val LaTeX-minted-key-val-options-local ]
  276. (LaTeX-arg-minted-language)
  277. TeX-arg-file)
  278. ;; 3.4 Using different styles
  279. '("usemintedstyle"
  280. [ LaTeX-arg-minted-language ] LaTeX-arg-minted-style)
  281. ;; 5.2 Macro option usage
  282. '("setminted"
  283. [ LaTeX-arg-minted-language ]
  284. (TeX-arg-key-val LaTeX-minted-key-val-options-local))
  285. '("setmintedinline"
  286. [ LaTeX-arg-minted-language ]
  287. (TeX-arg-key-val LaTeX-minted-key-val-options-local)))
  288. ;; New environments
  289. (LaTeX-add-environments
  290. '("minted" LaTeX-env-args [TeX-arg-key-val LaTeX-minted-key-val-options-local]
  291. LaTeX-arg-minted-language))
  292. ;; 4 Floating listings: If option "newfloat" is given, run the
  293. ;; style hook and use the interface provided by the style,
  294. ;; otherwise add "listing" manually
  295. (if (or (LaTeX-provided-package-options-member "minted" "newfloat")
  296. (LaTeX-provided-package-options-member "minted" "newfloat=true"))
  297. (progn
  298. (TeX-run-style-hooks "newfloat")
  299. (LaTeX-add-newfloat-DeclareFloatingEnvironments
  300. '("listing" "verbatim")))
  301. (LaTeX-add-environments '("listing" ["Float Position"]))
  302. (TeX-add-symbols '("listoflistings")
  303. '("listingscaption")
  304. '("listoflistingscaption"))
  305. (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
  306. '("listing" current-indentation) t)
  307. (add-to-list 'LaTeX-label-alist '("listing" . "lst:") t)
  308. (when (fboundp 'reftex-add-label-environments)
  309. (reftex-add-label-environments
  310. '(("listing" ?l "lst:" "~\\ref{%s}" caption nil nil)))))
  311. ;; Add to the auto parser
  312. (TeX-auto-add-regexp LaTeX-minted-newminted-regexp)
  313. (TeX-auto-add-regexp LaTeX-minted-newmint-regexp)
  314. (TeX-auto-add-regexp LaTeX-minted-newmintinline-regexp)
  315. (TeX-auto-add-regexp LaTeX-minted-newmintedfile-regexp)
  316. ;; Filling
  317. (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
  318. '("minted" current-indentation) t)
  319. (add-to-list 'LaTeX-verbatim-environments-local "minted")
  320. ;; FIXME: That doesn't work because \mintinline has 2 args and only the
  321. ;; second argument is verbatim.
  322. ;;(add-to-list 'LaTeX-verbatim-macros-with-delims-local "mintinline")
  323. ;; Fontification
  324. (when (and (fboundp 'font-latex-add-keywords)
  325. (fboundp 'font-latex-update-font-lock)
  326. (eq TeX-install-font-lock 'font-latex-setup))
  327. (font-latex-add-keywords '(("usemintedstyle" "[{")
  328. ("setminted" "[{")
  329. ("setmintedinline" "[{")
  330. ("newminted" "[{{")
  331. ("newmint" "[{{")
  332. ("newmintedinline" "[{{")
  333. ("newmintedfile" "[{{")
  334. ;; FIXME: Those have the form \mint{lang}|code|
  335. ;; so ideally the verbatim arg should be
  336. ;; recognized.
  337. "mint" "mintinline")
  338. 'function)
  339. (font-latex-add-keywords '(("inputminted" "[{{"))
  340. 'textual)
  341. ;; Tell font-lock about the update.
  342. (font-latex-update-font-lock t)))
  343. LaTeX-dialect)
  344. (defvar LaTeX-minted-package-options '("chapter" "cache"
  345. "cachedir" "finalizecache"
  346. "frozencache" "draft"
  347. "final" "kpsewhich"
  348. "langlinenos" "newfloat"
  349. "outputdir" "section")
  350. "Package options for the minted package.")
  351. ;;; minted.el ends here