1 ;;; newfloat.el --- AUCTeX style for `newfloat.sty' (v1.1-109)
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Arash Esbati <arash@gnu.org>
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 `newfloat.sty' (v1.1-109) from 2015/09/17.
30 ;; `newfloat.sty' is part of TeXLive.
32 ;; This style has some facilities to process the newly defined
33 ;; floating environments within AUCTeX, e.g. indentation, label
34 ;; addition etc. Currently it makes provision for `figure', `table'
35 ;; and `verbatim' floating types. To make things work, the style
36 ;; needs some help from the user. When a new floating environment is
37 ;; defined, the user should pass the floating type as a comment to
40 ;; \DeclareFloatingEnvironment[
42 ;; listname={List of Codes},
43 ;; fileext=lol]{code} % {verbatim}
45 ;; Note the `{verbatim}' as a comment after the name of the float env.
46 ;; Due to parsing reasons, this key-word must be in the same line as
47 ;; the name of the float and enclosed in braces `{}'.
49 ;; Before the opening brace, only spaces and comment chars `%' are
50 ;; allowed. Anything following the closing brace `}' is ignored.
52 ;; If things do not work or when in doubt, press `C-c C-n'. Comments
53 ;; for improvement are welcome.
57 ;; Needed for auto-parsing.
60 (defvar LaTeX-newfloat-key-val-options
64 ("placement" ("t" "b" "p" "tbp" "htbp"))
65 ("chapterlistsgaps" ("on" "off")))
66 "Key=value options for newfloat macros.")
68 (defvar LaTeX-newfloat-key-val-options-local nil
69 "Buffer-local Key=value options for newfloat macros.")
70 (make-variable-buffer-local 'LaTeX-newfloat-key-val-options-local)
72 ;; Setup parsing for \DeclareFloatingEnvironment:
73 (TeX-auto-add-type "newfloat-DeclareFloatingEnvironment" "LaTeX")
75 (defvar LaTeX-newfloat-DeclareFloatingEnvironment-regex
76 `(,(concat "\\\\DeclareFloatingEnvironment"
79 "[ \t\n\r%{}a-zA-Z0-9=,-]*"
83 "\\(?:[ %]*{\\([^}]*\\)}\\)?")
84 (1 2) LaTeX-auto-newfloat-DeclareFloatingEnvironment)
85 "Matches the argument of `\\DeclareFloatingEnvironment' from
88 (defun LaTeX-newfloat-auto-prepare ()
89 "Clear `LaTeX-auto-newfloat-DeclareFloatingEnvironment' before parsing."
90 (setq LaTeX-auto-newfloat-DeclareFloatingEnvironment nil))
92 (defun LaTeX-newfloat-auto-cleanup ()
93 "Process definded floats with \\DeclareFloatingEnvironment.
94 Depending on floating type passed as a comment to
95 AUCTeX (\"figure\", \"table\" or \"verbatim\"), update
96 `LaTeX-figure-label' and `LaTeX-table-label'. If RefTeX is
97 loaded, add the new floating environment via
98 `reftex-add-label-environments'. For \"verbatim\" environments,
99 update `LaTeX-indent-environment-list' to suppress indentation.
100 If `caption.el' is loaded, add the new floating environment to
101 `LaTeX-caption-supported-float-types'. Also define the macros
102 \"listofENVs\" and \"listofENVes\"."
103 (dolist (flt-type (LaTeX-newfloat-DeclareFloatingEnvironment-list))
104 (let ((flt (car flt-type))
105 (type (cadr flt-type)))
106 (cond ((string-equal type "figure")
107 (LaTeX-add-environments `(,flt LaTeX-env-figure))
108 (add-to-list 'LaTeX-label-alist `(,flt . LaTeX-figure-label) t)
109 (when (fboundp 'reftex-add-label-environments)
110 (reftex-add-label-environments
111 `((,flt ?f ,LaTeX-figure-label "~\\ref{%s}" caption nil nil)))))
112 ((string-equal type "table")
113 (LaTeX-add-environments `(,flt LaTeX-env-figure))
114 (add-to-list 'LaTeX-label-alist `(,flt . LaTeX-table-label) t)
115 (when (fboundp 'reftex-add-label-environments)
116 (reftex-add-label-environments
117 `((,flt ?t ,LaTeX-table-label "~\\ref{%s}" caption nil nil)))))
118 ((string-equal type "verbatim")
119 (LaTeX-add-environments `(,flt ["Float Position"]))
120 (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
121 `(,flt current-indentation) t)
122 (add-to-list 'LaTeX-label-alist `(,flt . LaTeX-listing-label) t)
123 (when (fboundp 'reftex-add-label-environments)
124 (reftex-add-label-environments
125 `((,flt ?l "lst:" "~\\ref{%s}" caption nil nil)))))
127 (LaTeX-add-environments `(,flt ["Float Position"]))))
128 (when (boundp 'LaTeX-caption-supported-float-types)
129 (add-to-list (make-local-variable 'LaTeX-caption-supported-float-types)
131 (if (string-equal "e" (substring flt -1))
132 (TeX-add-symbols (concat "listof" flt "s"))
134 (concat "listof" flt "s")
135 (concat "listof" flt "es"))))))
137 (add-hook 'TeX-auto-prepare-hook #'LaTeX-newfloat-auto-prepare t)
138 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-newfloat-auto-cleanup t)
139 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
145 ;; Add newfloat to the parser.
146 (TeX-auto-add-regexp LaTeX-newfloat-DeclareFloatingEnvironment-regex)
148 ;; Depending on class, add "within" key to the local options list
150 (setq LaTeX-newfloat-key-val-options-local
151 (copy-alist LaTeX-newfloat-key-val-options))
153 (if (< (LaTeX-largest-level) 2)
154 (add-to-list 'LaTeX-newfloat-key-val-options-local
155 '("within" ("chapter" "section" "none")))
156 (add-to-list 'LaTeX-newfloat-key-val-options-local
157 '("within" ("section" "none"))))
161 '("DeclareFloatingEnvironment"
162 [TeX-arg-key-val LaTeX-newfloat-key-val-options-local]
165 (let ((newfloat (TeX-read-string
166 (TeX-argument-prompt nil nil "Floating environment"))))
167 (LaTeX-add-newfloat-DeclareFloatingEnvironments newfloat)
168 (format "%s" newfloat)))))
170 '("SetupFloatingEnvironment"
171 (TeX-arg-eval completing-read
172 (TeX-argument-prompt nil nil "Floating environment")
173 (mapcar 'car (LaTeX-newfloat-DeclareFloatingEnvironment-list)))
174 (TeX-arg-key-val LaTeX-newfloat-key-val-options-local))
176 '("ForEachFloatingEnvironment" t)
177 '("ForEachFloatingEnvironment*" t)
180 (TeX-arg-eval completing-read
181 (TeX-argument-prompt nil nil "Floating environment")
182 (mapcar 'car (LaTeX-newfloat-DeclareFloatingEnvironment-list)))
188 (let ((keyvals (TeX-read-key-val
190 (append '(("chapterlistsgap"))
191 (if (< (LaTeX-largest-level) 2)
192 '(("within" ("chapter" "section" "none")))
193 '(("within" ("section" "none"))))))))
194 (format "%s" keyvals))))))
197 (when (and (featurep 'font-latex)
198 (eq TeX-install-font-lock 'font-latex-setup))
199 (font-latex-add-keywords '(("DeclareFloatingEnvironment" "[{")
200 ("SetupFloatingEnvironment" "{{")
201 ("ForEachFloatingEnvironment" "*{")
202 ("PrepareListOf" "{{")
203 ("newfloatsetup" "{"))
207 (defun LaTeX-newfloat-package-options ()
208 "Prompt for package options for the newfloat package."
212 '(("chapterlistsgap"))
213 (if (< (LaTeX-largest-level) 2)
214 '(("within" ("chapter" "section" "none")))
215 '(("within" ("section" "none")))))))
217 ;;; newfloat.el ends here