AUCTeX Sync -- New Style Files
[packages] / xemacs-packages / auctex / style / newfloat.el
1 ;;; newfloat.el --- AUCTeX style for `newfloat.sty' (v1.1-109)
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Arash Esbati <arash@gnu.org>
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Created: 2015-09-19
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 `newfloat.sty' (v1.1-109) from 2015/09/17.
30 ;; `newfloat.sty' is part of TeXLive.
31
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
38 ;; AUCTeX, e.g.
39 ;;
40 ;;     \DeclareFloatingEnvironment[
41 ;;       name=Code,
42 ;;       listname={List of Codes},
43 ;;       fileext=lol]{code}  %  {verbatim}
44 ;;
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 `{}'.
48 ;;
49 ;; Before the opening brace, only spaces and comment chars `%' are
50 ;; allowed.  Anything following the closing brace `}' is ignored.
51
52 ;; If things do not work or when in doubt, press `C-c C-n'.  Comments
53 ;; for improvement are welcome.
54
55 ;;; Code:
56
57 ;; Needed for auto-parsing.
58 (require 'tex)
59
60 (defvar LaTeX-newfloat-key-val-options
61   '(("fileext")
62     ("listname")
63     ("name")
64     ("placement" ("t" "b" "p" "tbp" "htbp"))
65     ("chapterlistsgaps" ("on" "off")))
66   "Key=value options for newfloat macros.")
67
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)
71
72 ;; Setup parsing for \DeclareFloatingEnvironment:
73 (TeX-auto-add-type "newfloat-DeclareFloatingEnvironment" "LaTeX")
74
75 (defvar LaTeX-newfloat-DeclareFloatingEnvironment-regex
76   `(,(concat "\\\\DeclareFloatingEnvironment"
77              "[ \t\n\r%]*"
78              "\\["
79              "[ \t\n\r%{}a-zA-Z0-9=,-]*"
80              "\\]"
81              "[ \t\n\r%]*"
82              "{\\([^}]+\\)}"
83              "\\(?:[ %]*{\\([^}]*\\)}\\)?")
84     (1 2) LaTeX-auto-newfloat-DeclareFloatingEnvironment)
85   "Matches the argument of `\\DeclareFloatingEnvironment' from
86 `newfloat.sty'.")
87
88 (defun LaTeX-newfloat-auto-prepare ()
89   "Clear `LaTeX-auto-newfloat-DeclareFloatingEnvironment' before parsing."
90   (setq LaTeX-auto-newfloat-DeclareFloatingEnvironment nil))
91
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)))))
126             (t
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)
130                      flt))
131       (if (string-equal "e" (substring flt -1))
132           (TeX-add-symbols (concat "listof" flt "s"))
133         (TeX-add-symbols
134          (concat "listof" flt "s")
135          (concat "listof" flt "es"))))))
136
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)
140
141 (TeX-add-style-hook
142  "newfloat"
143  (lambda ()
144
145    ;; Add newfloat to the parser.
146    (TeX-auto-add-regexp LaTeX-newfloat-DeclareFloatingEnvironment-regex)
147
148    ;; Depending on class, add "within" key to the local options list
149    ;; and use it.
150    (setq LaTeX-newfloat-key-val-options-local
151          (copy-alist LaTeX-newfloat-key-val-options))
152
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"))))
158
159    ;; Commands:
160    (TeX-add-symbols
161     '("DeclareFloatingEnvironment"
162       [TeX-arg-key-val LaTeX-newfloat-key-val-options-local]
163       (TeX-arg-eval
164        (lambda ()
165          (let ((newfloat (TeX-read-string
166                          (TeX-argument-prompt nil nil "Floating environment"))))
167            (LaTeX-add-newfloat-DeclareFloatingEnvironments newfloat)
168            (format "%s" newfloat)))))
169
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))
175
176     '("ForEachFloatingEnvironment" t)
177     '("ForEachFloatingEnvironment*" t)
178
179     '("PrepareListOf"
180       (TeX-arg-eval completing-read
181                     (TeX-argument-prompt nil nil "Floating environment")
182                     (mapcar 'car (LaTeX-newfloat-DeclareFloatingEnvironment-list)))
183       t)
184
185     '("newfloatsetup"
186       (TeX-arg-eval
187        (lambda ()
188          (let ((keyvals (TeX-read-key-val
189                          nil
190                          (append '(("chapterlistsgap"))
191                                  (if (< (LaTeX-largest-level) 2)
192                                      '(("within" ("chapter" "section" "none")))
193                                    '(("within" ("section" "none"))))))))
194            (format "%s" keyvals))))))
195
196    ;; Fontification
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"               "{"))
204                               'function)))
205  LaTeX-dialect)
206
207 (defun LaTeX-newfloat-package-options ()
208   "Prompt for package options for the newfloat package."
209   (TeX-read-key-val
210    t
211    (append
212     '(("chapterlistsgap"))
213     (if (< (LaTeX-largest-level) 2)
214         '(("within" ("chapter" "section" "none")))
215       '(("within" ("section" "none")))))))
216
217 ;;; newfloat.el ends here