1 ;;; fancyvrb.el --- AUCTeX style for `fancyvrb.sty' version 2.8.
3 ;; Copyright (C) 2013, 2014, 2016, 2017 Free Software Foundation, Inc.
5 ;; Maintainer: auctex-devel@gnu.org
6 ;; Author: Mosè Giordano <mose@gnu.org>
9 ;; This file is part of AUCTeX.
11 ;; AUCTeX is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; AUCTeX is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with AUCTeX; see the file COPYING. If not, write to the Free
23 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 ;; This file adds support for `fancyvrb.sty' version 2.8.
30 ;; This style has some capabilities to parse user defined macros,
31 ;; environments and saved blocks with `SaveVerbatim' environments and
32 ;; offer them for completion. After defining a new macro or
33 ;; environment, hit `C-c C-n' to parse the document. In case of
34 ;; `SaveVerbatim', a second `C-c C-n' might be necessary.
36 ;; The command `\CustomVerbatimEnvironment' is not mentioned in the
37 ;; documenation; hence this command is removed from this style. Use
38 ;; `\DefineVerbatimEnvironment' for new environments and customize the
39 ;; standard ones with `\RecustomVerbatimEnvironment'.
41 ;; The entries in `LaTeX-fancyvrb-key-val-options' cover also some
42 ;; keys which are not mentioned in the manual of the package.
46 ;; Needed for auto-parsing.
49 (defvar LaTeX-fancyvrb-key-val-options
50 '(("commentchar" ("none"))
55 ("fontfamily" ("tt" "courier" "helvetica"))
56 ("fontsize" ("auto" "\\tiny" "\\scriptsize"
57 "\\footnotesize" "\\small" "\\normalsize"
58 "\\large" "\\Large" "\\LARGE" "\\huge" "\\Huge"))
59 ("fontshape" ("auto" "n" "it" "sl" "sc" "scit"))
60 ("fontseries" ("auto" "m" "b" "bx" "sb" "c" "l" "lc"))
61 ("frame" ("none" "leftline" "topline" "bottomline" "lines" "single"))
64 ("rulecolor" ("none"))
65 ("fillcolor" ("none"))
67 ("labelposition" ("none" "topline" "bottomline" "all"))
68 ("numbers" ("none" "left" "right"))
70 ("firstnumber" ("auto" "last" "integer"))
72 ("numberblanklines" ("true" "false"))
75 ("showspaces" ("true" "false"))
76 ("showtabs" ("true" "false"))
77 ("obeytabs" ("true" "false"))
79 ("baselinestretch" ("auto" "dimension"))
80 ("commandchars" ("none"))
83 ("resetmargins" ("true" "false"))
85 ("samepage" ("true" "false"))
92 ;; Actually, the following options are used only by the `BVerbatim'
94 ("boxwidth" ("auto" "dimension"))
95 ("baseline" ("b" "c" "t"))
96 ;; The next key applies only to `\SaveVerb' macro.
98 "Key=value options for fancyvrb macros and environments.")
100 (defvar LaTeX-fancyvrb-key-val-options-local nil
101 "Buffer-local key=value options for fancyvrb macros and environments.
102 This variable is intended for packages like \"fvextra\" which
103 provide new key=values for fancyvrb environments. New key=values
104 should be appended to this variable.")
105 (make-variable-buffer-local 'LaTeX-fancyvrb-key-val-options-local)
107 (defvar LaTeX-fancyvrb-base-macros
108 '("VerbatimInput" "BVerbatimInput" "LVerbatimInput"
109 "SaveVerb" "UseVerb" "Verb")
110 "List of base macros available with fancyvrb package.
111 Starred versions are not included in this list.")
113 (defvar LaTeX-fancyvrb-base-environments
114 '("Verbatim" "BVerbatim" "LVerbatim" "SaveVerbatim" "VerbatimOut")
115 "List of base environments available with fancyvrb package.
116 Starred versions are not included in this list.")
118 (defvar LaTeX-fancyvrb-key-val-skip-regexp
123 "\\(?:{[^}{]*}[^}{]*\\)*"
127 "Helper regexp to skip over an optional argument.")
129 ;; Setup for defining new Verbatim commands:
131 (TeX-auto-add-type "fancyvrb-macro" "LaTeX")
133 (defvar LaTeX-fancyvrb-macro-regexp
134 `(,(concat "\\\\\\(Rec\\|C\\)ustomVerbatimCommand"
135 "[ \t\n\r]*{?[ \t\n\r]*\\\\\\([A-Za-z]+\\)[ \t\n\r]*}?"
136 "[ \t\n\r]*{[ \t\n\r]*\\([A-Za-z]+\\)[ \t\n\r]*}")
137 (2 3 1) LaTeX-auto-fancyvrb-macro)
138 "Matches macros by fancyvrb package.")
140 ;; Setup for defining new Verbatim environments:
142 (TeX-auto-add-type "fancyvrb-environment" "LaTeX")
144 (defvar LaTeX-fancyvrb-environment-regexp
145 `(,(concat "\\\\DefineVerbatimEnvironment"
146 "[ \t\n\r]*{[ \t\n\r]*\\([A-Za-z0-9]+\\)[ \t\n\r]*}"
147 "[ \t\n\r]*{[ \t\n\r]*\\([A-Za-z]+\\)[ \t\n\r]*}")
148 (1 2) LaTeX-auto-fancyvrb-environment)
149 "Matches new environments defined by fancyvrb package.")
151 ;; Setup for names in SaveVerb macros:
153 (TeX-auto-add-type "fancyvrb-saveverb" "LaTeX")
155 (defvar LaTeX-fancyvrb-saveverb-regexp
156 `(,(concat "\\\\SaveVerb"
157 LaTeX-fancyvrb-key-val-skip-regexp
159 1 LaTeX-auto-fancyvrb-saveverb)
160 "Match the name under which verbatim text is saved by SaveVerb macro.")
162 ;; Setup for names in SaveVerbatim environments:
164 (TeX-auto-add-type "fancyvrb-saveverbatim" "LaTeX")
166 (defvar LaTeX-fancyvrb-saveverbatim-regexp
167 `(,(concat "\\\\begin{SaveVerbatim}"
168 LaTeX-fancyvrb-key-val-skip-regexp
170 1 LaTeX-auto-fancyvrb-saveverbatim)
171 "Match the name under which verbatim text is saved by SaveVerbatim environment.")
173 (defun LaTeX-fancyvrb-auto-prepare ()
174 "Clear various LaTeX-auto-fancyvrb-* variables before parsing."
175 (setq LaTeX-auto-fancyvrb-macro nil
176 LaTeX-auto-fancyvrb-environment nil
177 LaTeX-auto-fancyvrb-saveverb nil
178 LaTeX-auto-fancyvrb-saveverbatim nil))
180 (defun LaTeX-fancyvrb-auto-cleanup ()
181 "Process parsed elements for fancyvrb package."
182 (LaTeX-fancyvrb-arg-define-macro nil t)
183 (LaTeX-fancyvrb-arg-define-environment nil t))
185 (add-hook 'TeX-auto-prepare-hook #'LaTeX-fancyvrb-auto-prepare t)
186 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-fancyvrb-auto-cleanup t)
187 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
189 (defun LaTeX-fancyvrb-arg-define-macro (optional &optional cleanup recustom)
190 "Query and insert a new verbatim macro with fancyvrb package.
191 If OPTIONAL is non-nil, insert the arguments in brackets. If
192 CLEANUP is non-nil, do not insert any arguments in the buffer and
193 update only various AUCTeX variables for verbatim macros. If
194 RECUSTOM is non-nil, delete macros from the variable
195 `TeX-symbol-list' before adding the new ones."
196 (let ((new-mac (unless cleanup
199 (TeX-argument-prompt optional nil "Verbatim macro: \\" t)
200 (mapcar #'car (apply #'append LaTeX-fancyvrb-macro-list)))
202 (TeX-argument-prompt optional nil "New verbatim macro: \\" t)))))
203 (base-mac (unless cleanup
204 (completing-read (TeX-argument-prompt optional nil "Based on macro")
205 LaTeX-fancyvrb-base-macros)))
206 (rec-flag (if recustom "Rec" "C")))
207 ;; We are (re-)defining a macro: Insert user queried input and use
208 ;; `LaTeX-add-fancyvrb-macros' on the input
210 (TeX-argument-insert new-mac optional TeX-esc)
211 (TeX-argument-insert base-mac optional)
213 (TeX-read-key-val optional LaTeX-fancyvrb-key-val-options-local) optional)
214 (LaTeX-add-fancyvrb-macros `(,new-mac ,base-mac ,rec-flag))))
216 ;; Now run the procdure: Do not use the function
217 ;; `LaTeX-fancyvrb-macro-list' here which will remove dupes from the
218 ;; variable `LaTeX-fancyvrb-macro-list' depending on car; we need
219 ;; the variable as is, hence (apply #'append ...);
220 (dolist (elt (apply #'append LaTeX-fancyvrb-macro-list))
221 (let ((mac-name (nth 0 elt))
222 (base-mac (nth 1 elt))
224 ;; If we're Rec-ustomizing, delete the entry first from
225 ;; `TeX-symbol-list':
226 (when (string= flag "Rec")
227 (setq TeX-symbol-list
228 (assq-delete-all (car (assoc mac-name (TeX-symbol-list))) TeX-symbol-list)))
229 ;; Now add the new item: Start with new macros for loading
231 (cond ((member base-mac '("VerbatimInput" "BVerbatimInput" "LVerbatimInput"))
234 [ TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local ]
235 LaTeX-fancyvrb-arg-file-relative))
236 (when (and (fboundp 'font-latex-add-keywords)
237 (fboundp 'font-latex-update-font-lock))
238 (font-latex-add-keywords `((,mac-name "[{"))
240 ;; New macros for saving verbatim text:
241 ((string= base-mac "SaveVerb")
244 [ TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local ]
247 (let ((name (TeX-read-string
248 (TeX-argument-prompt optional nil "Save name"))))
249 (LaTeX-add-fancyvrb-saveverbs name)
250 (format "%s" name))))
252 (when (and (fboundp 'font-latex-add-keywords)
253 (fboundp 'font-latex-update-font-lock))
254 (font-latex-add-keywords `((,mac-name "[{"))
256 ;; New macros for using previously saved text:
257 ((string= base-mac "UseVerb")
262 (TeX-argument-prompt optional nil "Saved name")
263 (LaTeX-fancyvrb-saveverb-list))))
264 (when (and (fboundp 'font-latex-add-keywords)
265 (fboundp 'font-latex-update-font-lock))
266 (font-latex-add-keywords `((,mac-name "{"))
268 ;; Anything else is considered as verbatim typesetting macro:
272 [ TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local ]
274 (add-to-list 'LaTeX-verbatim-macros-with-delims-local mac-name t)
275 (when (and (fboundp 'font-latex-add-keywords)
276 (fboundp 'font-latex-update-font-lock))
277 (font-latex-add-keywords `((,mac-name "["))
280 (when (and (fboundp 'font-latex-add-keywords)
281 (fboundp 'font-latex-update-font-lock)
282 (eq TeX-install-font-lock 'font-latex-setup))
283 (font-latex-update-font-lock t)))
285 (defun LaTeX-fancyvrb-arg-define-environment (optional &optional cleanup)
286 "Query and insert a new verbatim environment with fancyvrb package.
287 If OPTIONAL is non-nil, insert the arguments in brackets. If
288 CLEANUP is non-nil, do not insert any arguments in the buffer and
289 update only various AUCTeX variables for verbatim environments."
290 (let ((new-env (unless cleanup
292 (TeX-argument-prompt optional nil "New verbatim environment"))))
293 (base-env (unless cleanup
294 (completing-read (TeX-argument-prompt optional nil "Based on environment")
295 LaTeX-fancyvrb-base-environments))))
296 ;; We are defining a new env: First insert the arguments and the
297 ;; run `LaTeX-add-fancyvrb-environments' on '(new-env base-env).
298 ;; If base-env is SaveVerbatim, run
299 ;; `LaTeX-add-fancyvrb-saveverbatims' on new-env as well.
301 (TeX-argument-insert (car new-env) optional)
302 (TeX-argument-insert base-env optional)
304 (TeX-read-key-val optional LaTeX-fancyvrb-key-val-options-local) optional)
305 (LaTeX-add-fancyvrb-environments `(,new-env ,base-env))
306 (when (string= base-env "SaveVerbatim")
307 (LaTeX-add-fancyvrb-saveverbatims new-env)))
309 ;; Now run the procdure:
310 (dolist (elt (LaTeX-fancyvrb-environment-list))
311 (let ((env (car elt))
313 (cond ((string= type "VerbatimOut")
314 (LaTeX-add-environments
316 (let ((options (TeX-read-key-val t LaTeX-fancyvrb-key-val-options-local))
317 (file (TeX-read-string "Output file: ")))
318 (LaTeX-insert-environment
321 (unless (zerop (length options))
322 (concat LaTeX-optop options LaTeX-optcl))
323 (concat TeX-grop file TeX-grcl))))))))
324 ((string= type "SaveVerbatim")
325 (TeX-auto-add-regexp `(,(concat "\\\\begin{"
328 LaTeX-fancyvrb-key-val-skip-regexp
330 1 LaTeX-auto-fancyvrb-saveverbatim)))
332 ;; Regular verbatim environments have a starred
333 ;; version; so add them here; the non-starred additions
334 ;; to `LaTeX-verbatim-environments-local' and
335 ;; `LaTeX-indent-environment-list' are done outside
337 (LaTeX-add-environments
338 `(,env LaTeX-env-args
339 [ TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local ]))
340 (LaTeX-add-environments
341 `(,(concat env "*") LaTeX-env-args
342 [ TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local ]))
343 (add-to-list 'LaTeX-verbatim-environments-local (concat env "*"))
344 (add-to-list 'LaTeX-indent-environment-list
345 `(,(concat env "*") current-indentation) t)))
346 ;; These apply for all environments defined:
347 (add-to-list 'LaTeX-verbatim-environments-local env)
348 (add-to-list 'LaTeX-indent-environment-list `(,env current-indentation) t)))
350 (when (and (fboundp 'font-latex-add-keywords)
351 (fboundp 'font-latex-update-font-lock)
352 (eq TeX-install-font-lock 'font-latex-setup))
353 (font-latex-update-font-lock t))))
355 (defun LaTeX-fancyvrb-arg-file-relative (optional)
356 "Query and insert a file name relative to current master file.
357 If OPTIONAL is non-nil, insert the file name in brackets."
360 (read-file-name (TeX-argument-prompt optional nil "File"))
361 (TeX-master-directory))
367 (TeX-auto-add-regexp LaTeX-fancyvrb-macro-regexp)
368 (TeX-auto-add-regexp LaTeX-fancyvrb-environment-regexp)
369 (TeX-auto-add-regexp LaTeX-fancyvrb-saveverb-regexp)
370 (TeX-auto-add-regexp LaTeX-fancyvrb-saveverbatim-regexp)
374 ;; Activate the buffer-local version of key-vals.
375 (setq LaTeX-fancyvrb-key-val-options-local
376 (copy-alist LaTeX-fancyvrb-key-val-options))
379 ;; Verbatim material in footnotes
381 ;; Improved verbatim commands
382 '("Verb" [TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local] TeX-arg-verb)
383 '("DefineShortVerb" (TeX-arg-eval
385 (TeX-argument-prompt optional nil "Character")
387 '("UndefineShortVerb" (TeX-arg-eval
389 (TeX-argument-prompt optional nil "Character")
391 ;; Verbatim environments
392 '("fvset" (TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local))
393 ;; Changing individual line formatting
394 "FancyVerbFormatLine"
397 ;; Selection of lines to print
398 "FancyVerbStartString"
399 "FancyVerbStopString"
401 ;; Personalized environments
402 '("DefineVerbatimEnvironment"
403 LaTeX-fancyvrb-arg-define-environment)
404 '("RecustomVerbatimEnvironment"
405 (TeX-arg-eval completing-read
406 (TeX-argument-prompt optional nil "Verbatim environment")
407 LaTeX-fancyvrb-base-environments)
408 (TeX-arg-eval completing-read
409 (TeX-argument-prompt optional nil "Based on environment")
410 LaTeX-fancyvrb-base-environments)
411 (TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local))
413 '("CustomVerbatimCommand"
414 LaTeX-fancyvrb-arg-define-macro)
415 '("RecustomVerbatimCommand"
416 (LaTeX-fancyvrb-arg-define-macro nil t))
418 ;; Saving and restoring verbatim text and environments
420 [TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local]
423 (let ((name (TeX-read-string
424 (TeX-argument-prompt optional nil "Save name"))))
425 (LaTeX-add-fancyvrb-saveverbs name)
426 (format "%s" name))))
428 '("UseVerb" (TeX-arg-eval
430 (TeX-argument-prompt optional nil "Saved name")
431 (LaTeX-fancyvrb-saveverb-list)))
432 '("UseVerbatim" (TeX-arg-eval completing-read
433 (TeX-argument-prompt optional nil "Saved name")
434 (LaTeX-fancyvrb-saveverbatim-list)))
435 '("LUseVerbatim" (TeX-arg-eval completing-read
436 (TeX-argument-prompt optional nil "Saved name")
437 (LaTeX-fancyvrb-saveverbatim-list)))
438 '("BUseVerbatim" (TeX-arg-eval completing-read
439 (TeX-argument-prompt optional nil "Saved name")
440 (LaTeX-fancyvrb-saveverbatim-list)))
442 ;; Writing and reading verbatim files
443 '("VerbatimInput" [TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local]
444 LaTeX-fancyvrb-arg-file-relative)
445 '("BVerbatimInput" [TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local]
446 LaTeX-fancyvrb-arg-file-relative)
447 '("LVerbatimInput" [TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local]
448 LaTeX-fancyvrb-arg-file-relative))
450 (LaTeX-add-environments
451 '("Verbatim" LaTeX-env-args
452 [TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local])
453 '("Verbatim*" LaTeX-env-args
454 [TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local])
455 '("BVerbatim" LaTeX-env-args
456 [TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local])
457 '("BVerbatim*" LaTeX-env-args
458 [TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local])
459 '("LVerbatim" LaTeX-env-args
460 [TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local])
461 '("LVerbatim*" LaTeX-env-args
462 [TeX-arg-key-val LaTeX-fancyvrb-key-val-options-local])
465 (let ((options (TeX-read-key-val t LaTeX-fancyvrb-key-val-options-local))
466 (name (TeX-read-string "Save name: ")))
467 (LaTeX-insert-environment
470 (unless (zerop (length options))
471 (concat LaTeX-optop options LaTeX-optcl))
472 (concat TeX-grop name TeX-grcl)))
473 (LaTeX-add-fancyvrb-saveverbatims name))))
476 (let ((options (TeX-read-key-val t LaTeX-fancyvrb-key-val-options-local))
477 (file (TeX-read-string "Output file: ")))
478 (LaTeX-insert-environment
480 (concat (unless (zerop (length options))
481 (concat LaTeX-optop options LaTeX-optcl))
482 (concat TeX-grop file TeX-grcl)))))))
488 (add-to-list 'LaTeX-verbatim-environments-local "Verbatim")
489 (add-to-list 'LaTeX-verbatim-environments-local "Verbatim*")
490 (add-to-list 'LaTeX-verbatim-environments-local "BVerbatim")
491 (add-to-list 'LaTeX-verbatim-environments-local "BVerbatim*")
492 (add-to-list 'LaTeX-verbatim-environments-local "LVerbatim")
493 (add-to-list 'LaTeX-verbatim-environments-local "LVerbatim*")
494 (add-to-list 'LaTeX-verbatim-environments-local "SaveVerbatim")
495 (add-to-list 'LaTeX-verbatim-environments-local "VerbatimOut")
496 (make-local-variable 'LaTeX-indent-environment-list)
497 (add-to-list 'LaTeX-indent-environment-list '("Verbatim" current-indentation) t)
498 (add-to-list 'LaTeX-indent-environment-list '("Verbatim*" current-indentation) t)
499 (add-to-list 'LaTeX-indent-environment-list '("BVerbatim" current-indentation) t)
500 (add-to-list 'LaTeX-indent-environment-list '("BVerbatim*" current-indentation) t)
501 (add-to-list 'LaTeX-indent-environment-list '("LVerbatim" current-indentation) t)
502 (add-to-list 'LaTeX-indent-environment-list '("LVerbatim*" current-indentation) t)
503 (add-to-list 'LaTeX-indent-environment-list '("SaveVerbatim" current-indentation) t)
504 (add-to-list 'LaTeX-indent-environment-list '("VerbatimOut" current-indentation) t)
505 (add-to-list 'LaTeX-verbatim-macros-with-delims-local "Verb")
508 (when (and (fboundp 'font-latex-add-keywords)
509 (fboundp 'font-latex-update-font-lock)
510 (eq TeX-install-font-lock 'font-latex-setup))
511 (font-latex-add-keywords '(("CustomVerbatimCommand" "{{{")
512 ("RecustomVerbatimCommand" "{{{")
513 ("DefineVerbatimEnvironment" "{{{")
514 ("RecustomVerbatimEnvironment" "{{{")
515 ("DefineShortVerb" "{")
516 ("UndefineShortVerb" "{")
519 (font-latex-add-keywords '(("VerbatimInput" "[{")
520 ("BVerbatimInput" "[{")
521 ("LVerbatimInput" "[{"))
523 (font-latex-add-keywords '(("Verb" "[") ; The second argument should
524 ; actually be verbatim.
529 ("BUseVerbatim" "{"))
531 ;; Tell font-lock about the update.
532 (font-latex-update-font-lock t)))
535 (defvar LaTeX-fancyvrb-package-options nil
536 "Package options for the fancyvrb package.")
538 ;;; fancyvrb.el ends here