EasyPG 1.07 Released
[packages] / xemacs-packages / auctex / style / listings.el
1 ;;; listings.el --- AUCTeX style for `listings.sty'
2
3 ;; Copyright (C) 2004, 2005, 2009, 2013-2017 Free Software Foundation, Inc.
4
5 ;; Author: Ralf Angeli <angeli@iwi.uni-sb.de>
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Created: 2004-10-17
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 `listings.sty'.
30 ;;
31 ;; May 2015: The style detects new environments defined with
32 ;; `\lstnewenvironment'.  Users need to invoke `C-c C-n' for this.
33 ;;
34 ;; October 2015: The style detects new "styles" defined with
35 ;; `\lstdefinestyle' and offers them during key-value query.
36 ;;
37 ;; January 2017: Put label in opt. argument of environment.
38 ;;
39 ;; FIXME: Please make me more sophisticated!
40
41 ;;; Code:
42
43 ;; Needed for compiling `pushnew':
44 (eval-when-compile (require 'cl))
45
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"))
53     ("aboveskip")
54     ("belowskip")
55     ("lineskip")
56     ("boxpos" ("b" "c" "t"))
57     ;; The printed range
58     ("print" ("true" "false"))
59     ("firstline")
60     ("lastline")
61     ("linerange")
62     ("showlines" ("true" "false"))
63     ("emptylines")
64     ("gobble")
65     ;; Languages and styles
66     ("style")
67     ("language")
68     ("alsolanguage")
69     ("defaultdialect")
70     ("printpod" ("true" "false"))
71     ("usekeywordsintag" ("true" "false"))
72     ("tagstyle")
73     ("markfirstintag")
74     ("makemacrouse" ("true" "false"))
75     ;; Figure out the appearance
76     ("basicstyle")
77     ("identifierstyle")
78     ("commentstyle")
79     ("stringstyle")
80     ("keywordstyle")
81     ("classoffset")
82     ("texcsstyle")
83     ("directivestyle")
84     ("emph")
85     ("moreemph")
86     ("deleteemph")
87     ("emphstyle")
88     ("delim")
89     ("moredelim")
90     ("deletedelim")
91     ;; Getting all characters right
92     ("extendedchars" ("true" "false"))
93     ("inputencoding") ; Could make use of `latex-inputenc-coding-alist'.
94     ("upquote" ("true" "false"))
95     ("tabsize")
96     ("showtabs" ("true" "false"))
97     ("tab")
98     ("showspaces" ("true" "false"))
99     ("showstringspaces" ("true" "false"))
100     ("formfeed")
101     ;; Line numbers
102     ("numbers" ("none" "left" "right"))
103     ("stepnumber")
104     ("numberfirstline" ("true" "false"))
105     ("numberstyle")
106     ("numbersep")
107     ("numberblanklines" ("true" "false"))
108     ("firstnumber" ("auto" "last")) ; Can also take a number.
109     ("name")
110     ;; Captions
111     ("title")
112     ("caption") ; Insert braces?
113     ;; Label is inserted as part of environment insertion; see below
114     ;; for "lstlisting" in style hook
115     ;; ("label")
116     ("nolol" ("true" "false"))
117     ("numberbychapter" ("true" "false"))
118     ("captionpos" ("t" "b")) ; Can be a subset of tb.
119     ("abovecaptionskip")
120     ("belowcaptionskip")
121     ;; Margins and line shape
122     ("linewidth")
123     ("xleftmargin")
124     ("xrightmargin")
125     ("resetmargins" ("true" "false"))
126     ("breaklines" ("true" "false"))
127     ("breakatwhitespace" ("true" "false"))
128     ("prebreak")
129     ("postbreak")
130     ("breakindent")
131     ("breakautoindent" ("true" "false"))
132     ;; Frames
133     ("frame" ("none" "leftline" "topline" "bottomline" "lines" "single"
134               "shadowbox"
135               ;; Alternative to the above values.  A subset of trblTRBL can be
136               ;; given.
137               "t" "r" "b" "l" "T" "R" "B" "L"))
138     ("frameround" ("t" "f")) ; The input actually has to be four times {t,f}.
139     ("framesep")
140     ("rulesep")
141     ("framerule")
142     ("framexleftmargin")
143     ("framexrightmargin")
144     ("framextopmargin")
145     ("framebottommargin")
146     ("backgroundcolor")
147     ("rulecolor")
148     ("fillcolor")
149     ("fulesepcolor")
150     ("frameshape")
151     ;; Indexing
152     ("index")
153     ("moreindex")
154     ("deleteindex")
155     ("indexstyle")
156     ;; Column alignment
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"))
162     ("basewidth")
163     ("fontadjust" ("true" "false"))
164     ;; Escaping to LaTeX
165     ("texcl" ("true" "false"))
166     ("mathescape" ("true" "false"))
167     ("escapechar")
168     ("escapeinside")
169     ("escapebegin")
170     ("escapeend")
171     ;; Interface to fancyvrb
172     ("fancyvrb" ("true" "false"))
173     ("fvcmdparams")
174     ("morefvcmdparams")
175     ;; Language definitions
176     ("keywordsprefix")
177     ("keywords")
178     ("morekeywords")
179     ("deletekeywords")
180     ("texcs")
181     ("moretexcs")
182     ("deletetexcs")
183     ("directives")
184     ("moredirectives")
185     ("deletedirectives")
186     ("sensitive" ("true" "false"))
187     ("alsoletter")
188     ("alsodigit")
189     ("alsoother")
190     ("otherkeywords")
191     ("tag")
192     ("string")
193     ("morestring")
194     ("deletestring")
195     ("comment")
196     ("morecomment")
197     ("deletecomment")
198     ("keywordcomment")
199     ("morekeywordcomment")
200     ("deletekeywordcomment")
201     ("keywordcommentsemicolon")
202     ("podcomment" ("true" "false"))
203     ;; The following are all options from chapter 5, which are
204     ;; experimental
205     ;; Export of identifiers
206     ("procnamekeys")
207     ("moreprocnamekeys")
208     ("deleteprocnamekeys")
209     ("procnamestyle")
210     ("indexprocnames" ("true" "false"))
211     ;; Hyperlink references
212     ("hyperref")
213     ("morehyperref")
214     ("deletehyperref")
215     ("hyperanchor")
216     ("hyperlink")
217     ;; Literate programming
218     ("literate") ;; three arguments: replace,replacement text,length
219     ;; LGrind definitions
220     ("lgrindef")
221     ;; Arbitrary linerange markers
222     ("rangebeginprefix")
223     ("rangebeginsuffix")
224     ("rangeendprefix")
225     ("rangeendsuffix")
226     ("rangeprefix")
227     ("rangesuffix")
228     ("includerangemarker" ("true" "false"))
229     ;; Multicolumn Listing
230     ("multicolumn"))
231   "Key=value options for listings macros and environments.")
232
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)
236
237 ;; Setup for \lstnewenvironment:
238 (defvar LaTeX-auto-listings-lstnewenvironment nil
239   "Temporary for parsing the arguments of `\\lstnewenvironment'
240 from `listings' package.")
241
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'.")
249
250 ;; Setup for \lstdefinestyle:
251 (TeX-auto-add-type "listings-lstdefinestyle" "LaTeX")
252
253 (defvar LaTeX-listings-lstdefinestyle-regexp
254   '("\\\\lstdefinestyle{\\([^}]+\\)}"
255     1 LaTeX-auto-listings-lstdefinestyle)
256   "Matches the argument of \"\\lstdefinestyle\" from
257 \"listings\" package.")
258
259 ;; Setup for parsing the labels inside optional arguments:
260
261 (defvar LaTeX-listings-key-val-label-extract
262   (concat
263    "\\(?:\\[[^][]*"
264      "\\(?:{[^}{]*"
265        "\\(?:{[^}{]*"
266          "\\(?:{[^}{]*}[^}{]*\\)*"
267        "}[^}{]*\\)*"
268      "}[^][]*\\)*"
269    "label[ \t]*=[ \t]*{\\([^}]+\\)}"
270    "\\(?:[^]]*\\)*"
271    "\\]\\)")
272   "Helper regexp to extract the label out of optional argument.")
273
274 (defvar LaTeX-listings-key-val-label-regexp
275   `(,(concat
276       "\\\\begin{lstlisting}" LaTeX-listings-key-val-label-extract)
277     1 LaTeX-auto-label)
278   "Matches the label inside an optional argument after \\begin{lstlisting}.")
279
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))
284          (key (car elt))
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))))
289              opts :test #'equal)
290     (setq LaTeX-listings-key-val-options-local
291           (copy-alist opts))))
292
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))
297
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
309               `(,env
310                 LaTeX-env-args
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))))
319             (t ; No 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)
327                              1 LaTeX-auto-label))
328       ;; Tell RefTeX
329       (when (fboundp 'reftex-add-label-environments)
330         (reftex-add-label-environments
331          `((,env ?l "lst:" "~\\ref{%s}" nil (regexp "[Ll]isting")))))
332       ;; Fontification
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)))
342
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)
346
347 (TeX-add-style-hook
348  "listings"
349  (lambda ()
350
351    ;; Add to parser:
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)
355
356    ;; Local version of key-val options:
357    (setq LaTeX-listings-key-val-options-local
358          (copy-alist LaTeX-listings-key-val-options))
359
360    ;; New symbols
361    (TeX-add-symbols
362     '("lstalias" ["Alias dialect"] "Alias" ["Dialect"] "Language")
363     '("lstdefinestyle"
364       (TeX-arg-eval
365        (lambda ()
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]
373       TeX-arg-file)
374     "lstlistoflistings"
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")
382
383     "lstgrinddeffile" "lstaspectfiles" "lstlanguagefiles"
384     "lstlistingname" "lstlistlistingname")
385
386    ;; New environments
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")))
391
392    ;; Append "lstlisting" to `LaTeX-label-alist':
393    (add-to-list 'LaTeX-label-alist '("lstlisting" . LaTeX-listing-label) t)
394
395    ;; Filling
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")
401    ;; Fontification
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" ""))
409                               'textual)
410      (font-latex-add-keywords '(("lstalias" "{{")
411                                 ("lstdefinestyle" "{{")
412                                 ("lstset" "{"))
413                               'variable)
414      ;; Tell font-lock about the update.
415      (font-latex-update-font-lock t)))
416  LaTeX-dialect)
417
418 (defvar LaTeX-listings-package-options '("draft" "final" "savemem"
419                                          "noaspects"
420                                          ;; procnames is mentioned in
421                                          ;; Section 5.2
422                                          "procnames")
423   "Package options for the listings package.")
424
425 ;;; listings.el ends here