1 ;;; listings.el --- AUCTeX style for `listings.sty'
3 ;; Copyright (C) 2004, 2005, 2009, 2013-2017 Free Software Foundation, Inc.
5 ;; Author: Ralf Angeli <angeli@iwi.uni-sb.de>
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 `listings.sty'.
31 ;; May 2015: The style detects new environments defined with
32 ;; `\lstnewenvironment'. Users need to invoke `C-c C-n' for this.
34 ;; October 2015: The style detects new "styles" defined with
35 ;; `\lstdefinestyle' and offers them during key-value query.
37 ;; January 2017: Put label in opt. argument of environment.
39 ;; FIXME: Please make me more sophisticated!
43 ;; Needed for compiling `pushnew':
44 (eval-when-compile (require 'cl))
46 ;; The following are options taken from chapter 4 of the listings
47 ;; manual (2007/02/22 Version 1.4).
48 (defvar LaTeX-listings-key-val-options
49 '(;; Space and placement
50 ("float" ("t" "b" "p" "h")) ; Support [*] as an optional prefix and that
51 ; tbph are not exclusive.
52 ("floatplacement" ("t" "b" "p" "h"))
56 ("boxpos" ("b" "c" "t"))
58 ("print" ("true" "false"))
62 ("showlines" ("true" "false"))
65 ;; Languages and styles
70 ("printpod" ("true" "false"))
71 ("usekeywordsintag" ("true" "false"))
74 ("makemacrouse" ("true" "false"))
75 ;; Figure out the appearance
91 ;; Getting all characters right
92 ("extendedchars" ("true" "false"))
93 ("inputencoding") ; Could make use of `latex-inputenc-coding-alist'.
94 ("upquote" ("true" "false"))
96 ("showtabs" ("true" "false"))
98 ("showspaces" ("true" "false"))
99 ("showstringspaces" ("true" "false"))
102 ("numbers" ("none" "left" "right"))
104 ("numberfirstline" ("true" "false"))
107 ("numberblanklines" ("true" "false"))
108 ("firstnumber" ("auto" "last")) ; Can also take a number.
112 ("caption") ; Insert braces?
113 ;; Label is inserted as part of environment insertion; see below
114 ;; for "lstlisting" in style hook
116 ("nolol" ("true" "false"))
117 ("numberbychapter" ("true" "false"))
118 ("captionpos" ("t" "b")) ; Can be a subset of tb.
121 ;; Margins and line shape
125 ("resetmargins" ("true" "false"))
126 ("breaklines" ("true" "false"))
127 ("breakatwhitespace" ("true" "false"))
131 ("breakautoindent" ("true" "false"))
133 ("frame" ("none" "leftline" "topline" "bottomline" "lines" "single"
135 ;; Alternative to the above values. A subset of trblTRBL can be
137 "t" "r" "b" "l" "T" "R" "B" "L"))
138 ("frameround" ("t" "f")) ; The input actually has to be four times {t,f}.
143 ("framexrightmargin")
145 ("framebottommargin")
157 ("columns" ("fixed" "flexible" "fullflexible" "spaceflexible")) ;
158 ; Also supports an optional
159 ; argument with {c,l,r}.
160 ("flexiblecolumns" ("true" "false"))
161 ("keepspaces" ("true" "false"))
163 ("fontadjust" ("true" "false"))
165 ("texcl" ("true" "false"))
166 ("mathescape" ("true" "false"))
171 ;; Interface to fancyvrb
172 ("fancyvrb" ("true" "false"))
175 ;; Language definitions
186 ("sensitive" ("true" "false"))
199 ("morekeywordcomment")
200 ("deletekeywordcomment")
201 ("keywordcommentsemicolon")
202 ("podcomment" ("true" "false"))
203 ;; The following are all options from chapter 5, which are
205 ;; Export of identifiers
208 ("deleteprocnamekeys")
210 ("indexprocnames" ("true" "false"))
211 ;; Hyperlink references
217 ;; Literate programming
218 ("literate") ;; three arguments: replace,replacement text,length
219 ;; LGrind definitions
221 ;; Arbitrary linerange markers
228 ("includerangemarker" ("true" "false"))
229 ;; Multicolumn Listing
231 "Key=value options for listings macros and environments.")
233 (defvar LaTeX-listings-key-val-options-local nil
234 "Buffer-local Key=value options for listings macros and environments.")
235 (make-variable-buffer-local 'LaTeX-listings-key-val-options-local)
237 ;; Setup for \lstnewenvironment:
238 (defvar LaTeX-auto-listings-lstnewenvironment nil
239 "Temporary for parsing the arguments of `\\lstnewenvironment'
240 from `listings' package.")
242 (defvar LaTeX-listings-lstnewenvironment-regexp
243 `(,(concat "\\\\lstnewenvironment"
244 "[ \t\n\r]*{\\([A-Za-z0-9]+\\)}%?"
245 "[ \t\n\r]*\\[?\\([0-9]?\\)\\]?%?"
246 "[ \t\n\r]*\\(\\[\\)?")
247 (1 2 3) LaTeX-auto-listings-lstnewenvironment)
248 "Matches the argument of `\\lstnewenvironment' from `listings.sty'.")
250 ;; Setup for \lstdefinestyle:
251 (TeX-auto-add-type "listings-lstdefinestyle" "LaTeX")
253 (defvar LaTeX-listings-lstdefinestyle-regexp
254 '("\\\\lstdefinestyle{\\([^}]+\\)}"
255 1 LaTeX-auto-listings-lstdefinestyle)
256 "Matches the argument of \"\\lstdefinestyle\" from
257 \"listings\" package.")
259 ;; Setup for parsing the labels inside optional arguments:
261 (defvar LaTeX-listings-key-val-label-extract
266 "\\(?:{[^}{]*}[^}{]*\\)*"
269 "label[ \t]*=[ \t]*{\\([^}]+\\)}"
272 "Helper regexp to extract the label out of optional argument.")
274 (defvar LaTeX-listings-key-val-label-regexp
276 "\\\\begin{lstlisting}" LaTeX-listings-key-val-label-extract)
278 "Matches the label inside an optional argument after \\begin{lstlisting}.")
280 (defun LaTeX-listings-update-style-key ()
281 "Update the \"style\" key from `LaTeX-listings-key-val-options-local'
282 with user-defined values via the \"lstdefinestyle\" macro."
283 (let* ((elt (assoc "style" LaTeX-listings-key-val-options-local))
285 (temp (copy-alist LaTeX-listings-key-val-options-local))
286 (opts (assq-delete-all (car (assoc key temp)) temp)))
287 (pushnew (list key (TeX-delete-duplicate-strings
288 (mapcar #'car (LaTeX-listings-lstdefinestyle-list))))
290 (setq LaTeX-listings-key-val-options-local
293 (defun LaTeX-listings-auto-prepare ()
294 "Clear temporary variable from `listings.sty' before parsing."
295 (setq LaTeX-auto-listings-lstnewenvironment nil)
296 (setq LaTeX-auto-listings-lstdefinestyle nil))
298 (defun LaTeX-listings-auto-cleanup ()
299 "Process the parsed results of \"\\lstnewenvironment\" and
300 \"\\lstdefinestyle\"."
301 (dolist (env-args LaTeX-auto-listings-lstnewenvironment)
302 (let ((env (car env-args))
303 (args (cadr env-args))
304 (opt (nth 2 env-args)))
305 (cond (;; opt. 1st argument and mandatory argument(s)
306 (and args (not (string-equal args ""))
307 opt (not (string-equal opt "")))
308 (LaTeX-add-environments
311 [TeX-arg-key-val LaTeX-listings-key-val-options-local]
312 (LaTeX-env-label-as-keyval "caption")
313 ,(1- (string-to-number args)))))
314 (;; mandatory argument(s) only
315 (and args (not (string-equal args ""))
316 (string-equal opt ""))
317 (LaTeX-add-environments
318 (list env (string-to-number args))))
320 (LaTeX-add-environments (list env))))
321 (add-to-list 'LaTeX-indent-environment-list `(,env current-indentation) t)
322 (add-to-list 'LaTeX-verbatim-environments-local env t)
323 (add-to-list 'LaTeX-label-alist `(,env . LaTeX-listing-label) t)
324 ;; Add new env to parser for labels in opt. argument:
325 (TeX-auto-add-regexp `(,(concat "\\\\begin{" env "}"
326 LaTeX-listings-key-val-label-extract)
329 (when (fboundp 'reftex-add-label-environments)
330 (reftex-add-label-environments
331 `((,env ?l "lst:" "~\\ref{%s}" nil (regexp "[Ll]isting")))))
333 (when (and (fboundp 'font-latex-add-keywords)
334 (fboundp 'font-latex-update-font-lock)
335 (eq TeX-install-font-lock 'font-latex-setup))
336 ;; Tell font-lock about the update.
337 (font-latex-update-font-lock t))
338 ;; Add new env's to `ispell-tex-skip-alist': skip the entire env
339 (TeX-ispell-skip-setcdr `(,(cons env (concat "\\\\end{" env "}"))))))
340 (when (LaTeX-listings-lstdefinestyle-list)
341 (LaTeX-listings-update-style-key)))
343 (add-hook 'TeX-auto-prepare-hook #'LaTeX-listings-auto-prepare t)
344 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-listings-auto-cleanup t)
345 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
352 (TeX-auto-add-regexp LaTeX-listings-lstnewenvironment-regexp)
353 (TeX-auto-add-regexp LaTeX-listings-lstdefinestyle-regexp)
354 (TeX-auto-add-regexp LaTeX-listings-key-val-label-regexp)
356 ;; Local version of key-val options:
357 (setq LaTeX-listings-key-val-options-local
358 (copy-alist LaTeX-listings-key-val-options))
362 '("lstalias" ["Alias dialect"] "Alias" ["Dialect"] "Language")
366 (let ((name (TeX-read-string "Style name: ")))
367 (LaTeX-add-listings-lstdefinestyles name)
368 (LaTeX-listings-update-style-key)
369 (format "%s" name))))
370 (TeX-arg-key-val LaTeX-listings-key-val-options-local))
371 '("lstinline" TeX-arg-verb)
372 '("lstinputlisting" [TeX-arg-key-val LaTeX-listings-key-val-options-local]
375 '("lstnewenvironment" "Name" ["Number or arguments"] ["Default argument"]
376 "Starting code" "Ending code")
377 '("lstset" (TeX-arg-key-val LaTeX-listings-key-val-options-local))
378 '("lstloadlanguages" t)
379 ;; 4.17 Short Inline Listing Commands
380 '("lstMakeShortInline" [ "Options" ] "Character")
381 '("lstDeleteShortInline" "Character")
383 "lstgrinddeffile" "lstaspectfiles" "lstlanguagefiles"
384 "lstlistingname" "lstlistlistingname")
387 (LaTeX-add-environments
388 '("lstlisting" LaTeX-env-args
389 [TeX-arg-key-val LaTeX-listings-key-val-options-local]
390 (LaTeX-env-label-as-keyval "caption")))
392 ;; Append "lstlisting" to `LaTeX-label-alist':
393 (add-to-list 'LaTeX-label-alist '("lstlisting" . LaTeX-listing-label) t)
396 (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
397 '("lstlisting" current-indentation) t)
398 (add-to-list 'LaTeX-verbatim-environments-local "lstlisting")
399 (add-to-list 'LaTeX-verbatim-macros-with-delims-local "lstinline")
400 (add-to-list 'LaTeX-verbatim-macros-with-braces-local "lstinline")
402 (when (and (fboundp 'font-latex-add-keywords)
403 (fboundp 'font-latex-update-font-lock)
404 (eq TeX-install-font-lock 'font-latex-setup))
405 (font-latex-add-keywords '(("lstnewenvironment" "{[[{{")) 'function)
406 (font-latex-add-keywords '(("lstinputlisting" "[{")) 'reference)
407 (font-latex-add-keywords '(("lstinline" "[")
408 ("lstlistoflistings" ""))
410 (font-latex-add-keywords '(("lstalias" "{{")
411 ("lstdefinestyle" "{{")
414 ;; Tell font-lock about the update.
415 (font-latex-update-font-lock t)))
418 (defvar LaTeX-listings-package-options '("draft" "final" "savemem"
420 ;; procnames is mentioned in
423 "Package options for the listings package.")
425 ;;; listings.el ends here