summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/floatrow.el
blob: be8878f02396f2889ad3d2d5dd85b62c50df2232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
;;; floatrow.el --- AUCTeX style for `floatrow.sty' (v0.3b)  -*- lexical-binding: t; -*-

;; Copyright (C) 2017--2021 Free Software Foundation, Inc.

;; Author: Arash Esbati <arash@gnu.org>
;; Maintainer: auctex-devel@gnu.org
;; Created: 2016-11-11
;; Keywords: tex

;; This file is part of AUCTeX.

;; AUCTeX is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; AUCTeX is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with AUCTeX; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
;; 02110-1301, USA.

;;; Commentary:

;; This file adds support for `floatrow.sty' (v0.3b) from 2009/08/02.
;; `floatrow.sty' is part of TeXLive.
;;
;; `floatrow.sty' is a powerful package and the documentation long.
;; It is expected that this style is not feature complete.  One
;; deliberate decision is that this style does not offer any package
;; options: Please use the command `\floatsetup' to set the options
;; you want.
;;
;; This style also alters the way AUCTeX inserts the environments
;; "table" and "figure" (cf.  function `LaTeX-floatrow-env-init').  If
;; you want the original behavior, try `C-c C-e rawfigure' or `C-c C-e
;; rawtable' which include the environments incl.  the macro \RawFloats
;; at the beginning of the environment (cf.  function
;; `LaTeX-floatrow-env-figure-raw').
;;
;; Another deliberate decision is the implementation of commands like
;; `\ffigbox' and `\ttabbox': When invoked with `C-c C-m ffigbox RET',
;; the final result will look like this with cursor being at *:
;;
;;   \ffigbox{*}{%
;;     \caption{query for a caption}%
;;     \label{prefix:is-chosen-acc-to-current-environment}%
;;   }
;;
;; This gives users the freedom to insert any command where point is.
;; The mandatory arguments are not part of the fontification as
;; commands like `\includegraphics' or `tabular' environments have their
;; own fontification.

;;; Code:

;; Needed for compiling `cl-pushnew':
(eval-when-compile
  (require 'cl-lib))

;; Needed for compiling `LaTeX-check-insert-macro-default-style':
(require 'latex)

;; Needed for auto-parsing:
(require 'tex)

;; Silence the compiler:
(declare-function font-latex-add-keywords
                  "font-latex"
                  (keywords class))

(declare-function reftex-compile-variables
                  "reftex"
                  ())

(defvar LaTeX-floatrow-key-val-options
  '(;; 3.1.1 Float Style
    ("style" ("plain" "plaintop" "Plaintop"
              "ruled" "Ruled"
              "boxed" "Boxed" "BOXED"
              "shadowbox" "Shadowbox" "SHADOWBOX"
              "doublebox" "Doublebox" "DOUBLEBOX"
              "wshadowbox" "Wshadowbox" "WSHADOWBOX"))
    ;; 3.1.2 Font Settings
    ("font" ("scriptsize" "footnotesize" "small" "normalsize" "large"
             "Large" "normalfont" "up" "it" "sl" "sc" "md" "bf"
             "rm" "sf" "tt"))
    ("footfont" ("scriptsize" "footnotesize" "small" "normalsize" "large"
                 "Large" "normalfont" "up" "it" "sl" "sc" "md" "bf"
                 "rm" "sf" "tt"))
    ;; 3.1.3 Position of Caption
    ("capposition" ("top" "TOP" "bottom" "beside"))
    ;; 3.1.4 Position of Beside Caption
    ("capbesideposition" ("left" "right" "inside" "outside"
                          "top" "bottom" "center"))
    ;; 3.1.5 Defining The Width of Beside Caption
    ("capbesidewidth" ("none" "sidefil"))
    ;; 3.1.6 Defining Width of Object
    ("floatwidth" ("\\hsize" "\\textwidth" "\\columnwidth" "\\linewidth"))
    ;; 3.1.7 Other Settings for Beside Captions
    ("capbesideframe" ("yes" "no"))
    ;; 3.1.8 Defining Float Foot Position (Legends and Footnotes)
    ("footposition" ("default" "caption" "bottom"))
    ;; 3.1.9 Vertical Alignment of Float Elements
    ("heightadjust" ("all" "caption" "object" "none" "nocaption" "noobject"))
    ("valign" ("t" "c" "b" "s"))
    ;; 3.1.10 Facing Layout
    ("facing" ("yes" "no"))
    ;; 3.1.11 Object Settings
    ("objectset" ("justified" "centering" "raggedright" "RaggedRight" "raggedleft"))
    ;; 3.1.12 Defining Float Margins
    ("margins" ("centering" "raggedright" "raggedleft"
                "hangright" "hanginside" "hangoutside"))
    ;; 3.1.13 Defining Float Separators
    ("floatrowsep" ("columnsep" "quad" "qquad" "hfil" "hfill" "none"))
    ("capbesidesep" ("columnsep" "quad" "qquad" "hfil" "hfill" "none"))
    ;; 3.1.14 Defining Float Rules/Skips
    ("precode" ("none" "thickrule" "rule" "lowrule" "captionskip"))
    ("rowprecode" ("none" "thickrule" "rule" "lowrule" "captionskip"))
    ("midcode" ("none" "thickrule" "rule" "lowrule" "captionskip"))
    ("postcode" ("none" "thickrule" "rule" "lowrule" "captionskip"))
    ("rowpostcode" ("none" "thickrule" "rule" "lowrule" "captionskip"))
    ;; 3.1.15 Defining Float Frames
    ("framestyle" ("fbox" "colorbox" "FRcolorbox" "corners"
                   "doublebox" "shadowbox" "wshadowbox"))
    ("frameset")
    ("framearound" ("none" "object" "all" "row" "none"))
    ("framefit" ("yes" "no"))
    ("rowfill" ("yes" "no"))
    ;; 3.1.16 Settings for Colored Frames
    ("colorframeset")
    ("colorframecorners")
    ;; 3.1.17 Defining Float Skips
    ("captionskip")
    ("footskip")
    ;; 3.1.18 Defining Float Footnote Rule's Style
    ("footnoterule" ("normal" "limited" "fullsize" "none"))
    ;; 3.1.19 Managing Floats with [H] Placement Option
    ("doublefloataswide" ("yes" "no"))
    ("floatHaslist" ("yes" "no"))
    ;; 7.2.1 Additions in The floatrow Package to longtable package
    ("LTcapwidth" ("table" "contents")))
  "Key=value options for floatrow macros and environments.")

(defun LaTeX-floatrow-key-val-options ()
  "Return an updated list of key=vals from floatrow package."
  (append
   (when (LaTeX-floatrow-DeclareNewOption-list)
     (let ((vcode-keys '("precode" "rowprecode" "midcode" "postcode" "rowpostcode"))
           (sep-keys '("floatrowsep" "capbesidesep"))
           result)
       (dolist (keyvals (LaTeX-floatrow-DeclareNewOption-list) result)
         (let* ((key (cond ((string= (nth 1 keyvals) "FloatStyle")
                            "style")
                           ((string= (nth 1 keyvals) "FloatFont")
                            "font")
                           ((string= (nth 1 keyvals) "FloatVCode")
                            "precode")
                           ((string= (nth 1 keyvals) "ColorBox")
                            "colorframeset")
                           ((string= (nth 1 keyvals) "CBoxCorners")
                            "colorframecorners")
                           ((string= (nth 1 keyvals) "ObjectSet")
                            "objectset")
                           ((string= (nth 1 keyvals) "MarginSet")
                            "margins")
                           ((string= (nth 1 keyvals) "FloatSeparators")
                            "floatrowsep")
                           ((string= (nth 1 keyvals) "FloatFootnoterule")
                            "footnoterule")
                           (t nil)))
                (val (nth 2 keyvals))
                (vals-predefined
                 (cadr (assoc key LaTeX-floatrow-key-val-options)))
                (vals-parsed (cadr (assoc key result))))
           ;; Remove entry in `result' if there is one for the `key'.
           ;; `precode' and `floatrowsep' are special here since they
           ;; are placeholders for other keys (see above):
           (cond ((string= key "precode")
                  (dolist (x vcode-keys)
                    (when (assoc x result)
                      (setq result (assq-delete-all (car (assoc x result)) result)))))
                 ((string= key "floatrowsep")
                  (dolist (x sep-keys)
                    (when (assoc x result)
                      (setq result (assq-delete-all (car (assoc x result)) result)))))
                 (t
                  (when (assoc key result)
                    (setq result (assq-delete-all (car (assoc key result)) result)))))
           ;; Add the entrie to `result'.  Again, watch for `precode'
           ;; and `floatrowsep'.  Also delete any dupes:
           (cond ((string= key "precode")
                  (dolist (x vcode-keys)
                    (cl-pushnew (list x (TeX-delete-duplicate-strings
                                         (append vals-parsed
                                                 vals-predefined
                                                 (list val))))
                                result :test #'equal)))
                 ((string= key "floatrowsep")
                  (dolist (x sep-keys)
                    (cl-pushnew (list x (TeX-delete-duplicate-strings
                                         (append vals-parsed
                                                 vals-predefined
                                                 (list val))))
                                result :test #'equal)))
                 (t
                  (cl-pushnew (list key (TeX-delete-duplicate-strings
                                         (append vals-parsed
                                                 vals-predefined
                                                 (list val))))
                              result :test #'equal)))))))
   LaTeX-floatrow-key-val-options))

(defvar LaTeX-floatrow-supported-float-types
  '("figure" "table"                              ; Standard LaTeX
    "widefigure" "widetable" "widefloat"          ; Standard figure* & table*
    "wrapfigure" "wraptable" "wrapfloat"          ; wrapfig.sty
    "rotfigure"  "rottable"  "rotfloat"           ; rotating.sty
    "widerotfigure" "widerottable" "widerotfloat" ; for 2-col & wide
    "figurerow"  "tablerow"  "floatrow"           ; inside floatrow env's
    "capbesidefigure" "capbesidetable"            ; floats with beside captions
    "capbesidefloat"
    "longtable"                                   ; longtable.sty
    "subfigure" "subtable" "sub")                 ; subcaption.sty
  "List of float types supported by floatrow.sty.")

;; Setup for \newfloatcommand
(TeX-auto-add-type "floatrow-newfloatcommand" "LaTeX")

(defvar LaTeX-floatrow-newfloatcommand-regexp
  '("\\\\newfloatcommand{\\([^}]+\\)}" 1 LaTeX-auto-floatrow-newfloatcommand)
  "Matches the arguments of \"\\newfloatcommand\" from floatrow.sty.")

;; Setup for various \Declare* macros:
(TeX-auto-add-type "floatrow-DeclareNewOption" "LaTeX")

(defvar LaTeX-floatrow-DeclareNewOption-regexp
  `(,(concat
      "\\\\Declare"
      "\\("
      (mapconcat #'identity
                 '("FloatStyle"         ; 3.6.1 Float Style Option (style=)
                   "FloatFont"          ; 3.6.2 Float Font Option (font=)
                   "FloatVCode"         ; 3.6.3 Option for Float Rules/Skips (precode= etc.)
                   "ColorBox"           ; 3.6.4 Settings for Colored Frame (colorframeset=)
                   "CBoxCorners"        ;       colorframecorners=
                   "ObjectSet"          ; 3.6.5 Object Justification Option (objectset=)
                   "MarginSet"          ; 3.6.6 Option for Float Box Alignment/Settings (margins=)
                   "FloatSeparators"    ; 3.6.7 Float Separators Options (floatrowsep=, capbesidesep=)
                   "FloatFootnoterule") ; 3.6.8 Option for Footnote Rule's Style (footnoterule=)
                 "\\|")
      "\\)"
      "{\\([^}]+\\)}")
    (0 1 2) LaTeX-auto-floatrow-DeclareNewOption)
  "Matches the arguments of \"\\Declare*\" from floatrow.sty.")

;; Setup for \newseparated(label|ref):
(TeX-auto-add-type "floatrow-newseparatedlabel-ref" "LaTeX")

(defvar LaTeX-floatrow-newseparatedlabel-ref-regexp
  `(,(concat
      "\\\\newseparated"
      "\\(label\\|ref\\)"
      "{?"
      "\\\\"
      "\\([a-zA-Z]+\\)"
      "}?")
    (2 1) LaTeX-auto-floatrow-newseparatedlabel-ref)
  "Matches the arguments \"\\newseparated(label|ref)\" command from floatrow.sty.")

;; Setup for \DeclareNewFloatType:
(TeX-auto-add-type "floatrow-DeclareNewFloatType" "LaTeX")

(defvar LaTeX-floatrow-DeclareNewFloatType-regexp
  '("\\\\DeclareNewFloatType{\\([^}]+\\)}"
    1 LaTeX-auto-floatrow-DeclareNewFloatType)
  "Matches the argument of \"\\DeclareNewFloatType\" command from floatrow.sty.")

;; Plug them into the machinery.
(defun LaTeX-floatrow-auto-prepare ()
  "Clear various \"LaTeX-floatrow\" variables before parsing."
  (setq LaTeX-auto-floatrow-newfloatcommand       nil
        LaTeX-auto-floatrow-DeclareNewOption      nil
        LaTeX-auto-floatrow-newseparatedlabel-ref nil
        LaTeX-auto-floatrow-DeclareNewFloatType   nil))

(defun LaTeX-floatrow-auto-cleanup ()
  "Process parsed results from floatrow package."
  ;; Replace initially the way fig & tab env's are inserted:
  (LaTeX-floatrow-env-init)
  ;;
  ;; Process new float commands like \ffigbox:
  (when (LaTeX-floatrow-newfloatcommand-list)
    (dolist (cmd (mapcar #'car (LaTeX-floatrow-newfloatcommand-list)))
      (TeX-add-symbols `(,cmd LaTeX-floatrow-arg-floatbox))
      (when (and (featurep 'font-latex)
                 (eq TeX-install-font-lock 'font-latex-setup))
        (font-latex-add-keywords `((,cmd "[[["))
                                 'textual))))
  ;;
  ;; Process new label/ref commands:
  (when (LaTeX-floatrow-newseparatedlabel-ref-list)
    (let (floatrow-run-reftex-compile-vars)
      (dolist (elt (LaTeX-floatrow-newseparatedlabel-ref-list))
        (let ((cmd (car elt))
              (type (cadr elt)))
          (if (string= type "ref")
              ;; More fun w/ referencing macros:
              (TeX-add-symbols `(,cmd TeX-arg-ref))
            ;; Less fun w/ label defining macros.  Add cmd to
            ;; TeX-symbol-list:
            (TeX-add-symbols `(,cmd TeX-arg-define-label))
            ;; For AUCTeX, parse the argument of the new macro and add
            ;; it to `LaTeX-auto-label':
            (TeX-auto-add-regexp
             `(,(concat "\\\\" cmd "{\\([^\n\r%\\{}]+\\)}") 1 LaTeX-auto-label))
            ;; For RefTeX, append cmd to `reftex-label-regexps and set
            ;; floatrow-run-reftex-compile-vars to t:
            (when (and (boundp 'reftex-label-regexps)
                       (fboundp 'reftex-compile-variables)
                       (not (string-match
                             cmd
                             (mapconcat #'identity reftex-label-regexps "|"))))
              (add-to-list (make-local-variable 'reftex-label-regexps)
                           (concat "\\\\" cmd "{\\(?1:[^}]*\\)}") t)
              (setq floatrow-run-reftex-compile-vars t)))
          ;; Fontify macros as reference:
          (when (and (featurep 'font-latex)
                     (eq TeX-install-font-lock 'font-latex-setup))
            (font-latex-add-keywords `((,cmd "{"))
                                     'reference))))
      ;; Run `reftex-compile-variables' if needed only once:
      (when floatrow-run-reftex-compile-vars
        (reftex-compile-variables))))
  ;;
  ;; Process new floattypes:
  (when (LaTeX-floatrow-DeclareNewFloatType-list)
    (LaTeX-floatrow-arg-declare-new-floattype nil t))
  ;;
  ;; Add elements from `LaTeX-floatrow-supported-float-types' to
  ;; `LaTeX-caption-supported-float-types':
  (when (boundp 'LaTeX-caption-supported-float-types)
    (make-local-variable 'LaTeX-caption-supported-float-types)
    (dolist (float LaTeX-floatrow-supported-float-types)
      (add-to-list 'LaTeX-caption-supported-float-types float t))) )

(add-hook 'TeX-auto-prepare-hook #'LaTeX-floatrow-auto-prepare t)
(add-hook 'TeX-auto-cleanup-hook #'LaTeX-floatrow-auto-cleanup t)
(add-hook 'TeX-update-style-hook #'TeX-auto-parse t)

(defun LaTeX-floatrow-arg-floatbox (optional)
  "Query and insert arguments of float box commands from floatrow.sty.
If OPTIONAL is non-nil, indicate optional argument during query."
  ;; Query for the optional arguments; ask for "vertpos" only if
  ;; "height" is given.  let-bind `TeX-arg-*-brace' for
  ;; `TeX-argument-insert':
  (let* ((TeX-arg-opening-brace "[")
         (TeX-arg-closing-brace "]")
         (TeX-last-optional-rejected nil)
         (width (LaTeX-check-insert-macro-default-style
                 (completing-read
                  (TeX-argument-prompt t nil "Width")
                  (mapcar (lambda (x) (concat TeX-esc (car x)))
                          (LaTeX-length-list)))))
         (TeX-last-optional-rejected (or (not width)
                                         (and width (string= width ""))))
         (height (LaTeX-check-insert-macro-default-style
                  (completing-read
                   (TeX-argument-prompt t nil "Height")
                   (mapcar (lambda (x) (concat TeX-esc (car x)))
                           (LaTeX-length-list)))))
         (TeX-last-optional-rejected (or (not height)
                                         (and height (string= height ""))))
         (vertpos (LaTeX-check-insert-macro-default-style
                   (if (string= height "")
                       ""
                     (completing-read
                      (TeX-argument-prompt t nil "Vertical alignment")
                      '("t" "c" "b" "s"))))))
    (and width (TeX-argument-insert width t))
    ;; Insert an extra pair of brackets if only `height' is given,
    ;; otherwise it will become `width'
    (when (and width (string= width "")
               height (not (string= height "")))
      (insert "[]"))
    (and height (TeX-argument-insert height t))
    (and vertpos (TeX-argument-insert vertpos t)))
  ;; Now query for the (short-)caption.  Also check for the
  ;; float-type; if we're inside (sub)?floatrow*?, then check for the
  ;; next outer environment:
  (let* ((currenv (if (string-match "floatrow\\*?\\_>" (LaTeX-current-environment))
                      (LaTeX-current-environment 2)
                    (LaTeX-current-environment)))
         (caption (TeX-read-string
                   (TeX-argument-prompt optional nil "Caption")))
         (short-caption
          (when (>= (length caption) LaTeX-short-caption-prompt-length)
            (TeX-read-string
             (TeX-argument-prompt t nil "Short caption")))))
    (indent-according-to-mode)
    ;; The final result will look like this with * being point:
    ;;   \ffigbox{*}{%
    ;;     \caption{text}%
    ;;     \label{fig:foo}%
    ;;   }
    (insert TeX-grop)
    (save-excursion
      ;; We are inside the 1. mandatory arg: Save the pos & insert `}{':
      (insert TeX-grcl TeX-grop)
      (if (and caption (not (string= caption "")))
          (progn
            ;; If caption, move to EOL, delete any spaces and hide the line end
            (end-of-line)
            (delete-horizontal-space)
            (insert "%")
            ;; Add a newline and the caption
            (newline-and-indent)
            (insert (LaTeX-compose-caption-macro caption short-caption))
            ;; If we have a caption, then we probably also want a
            ;; label.  Hide EOL end and proceed to enter a label
            (insert "%")
            (newline-and-indent)
            (when (LaTeX-label currenv 'environment)
              ;; Move point to end of line and hide EOL
              (end-of-line)
              (delete-horizontal-space)
              (insert "%")
              (newline-and-indent))
            ;; Now close the group
            (insert TeX-grcl)
            (indent-according-to-mode)
            (end-of-line))
        ;; Otherwise, only insert a }
        (insert TeX-grcl)))))

(defun LaTeX-floatrow-env-init ()
  "Replace AUCTeX entries in the variable `LaTeX-environment-list'.
After loading the style hook floatrow.el, delete the entries for
figure*? and table*? from variable `LaTeX-environment-list' and
replace them with the ones offered by the style.  Original
entries are available under \"rawfigure*?\" and \"rawtable*?\"."
  (LaTeX-environment-list)
  (dolist (env '("figure" "figure*" "table" "table*"))
          (setq LaTeX-environment-list
                (assq-delete-all (car (assoc env LaTeX-environment-list))
                                 LaTeX-environment-list))
          (LaTeX-add-environments `(,env LaTeX-floatrow-env-figure)
                                  `(,(concat "raw" env) LaTeX-floatrow-env-figure-raw))))

(defun LaTeX-floatrow-env-figure (environment)
  "Create floating ENVIRONMENT suitable for floatrow macros."
  (let ((float (and LaTeX-float
                    (TeX-read-string
                     (TeX-argument-prompt t nil "Float position")
                     LaTeX-float))))
    (LaTeX-insert-environment environment
                              (unless (zerop (length float))
                                (concat LaTeX-optop float LaTeX-optcl)))))

(defun LaTeX-floatrow-env-figure-raw (env)
  "Create raw floating ENV with floatrow.sty.
Also insert the macro \"\\RawFloats\" when finished with user
queries."
  (let ((environment (replace-regexp-in-string "raw" "" env)))
    (LaTeX-env-figure environment)
    (save-excursion
      ;; `LaTeX-find-matching-begin' will not work for us as we don't
      ;; know how user answers queries from AUCTeX, hence we search
      ;; back for `environment':
      (re-search-backward (concat "\\\\begin{" environment "}") nil t)
      (end-of-line)
      (LaTeX-newline)
      (indent-according-to-mode)
      (insert TeX-esc "RawFloats"))))

(defun LaTeX-floatrow-arg-declare-new-options (optional prompt key)
  "Query and insert user-defined values to keys provided by floatrow.sty.
If OPTIONAL is non-nil, ask for an optional argument and insert
it in square brackets.  PROMPT replaces the standard one.  KEY is
a string and corresponds to first parsed element in
`LaTeX-floatrow-DeclareNewOption-regexp'."
  (let ((val (TeX-read-string
              (TeX-argument-prompt optional prompt "New value option"))))
    (LaTeX-add-floatrow-DeclareNewOptions
     (list (concat TeX-esc "Declare" key TeX-grop val TeX-grcl)
           key val))
    (TeX-argument-insert val optional)
    (TeX-argument-insert
     (TeX-read-key-val optional (LaTeX-floatrow-key-val-options)) optional)))

(defun LaTeX-floatrow-arg-newseparatedlabel/ref (optional type)
  "Query and insert user defined label and reference macros from floatrow.sty.
If OPTIONAL is non-nil, insert the argument in brackets.  TYPE is
the string \"label\" or \"ref\"."
  (let ((cmd (TeX-read-string
              (if (string= type "label")
                  (TeX-argument-prompt optional nil "Label command: \\" t)
                (TeX-argument-prompt optional nil "Reference command: \\" t)))))
    (LaTeX-add-floatrow-newseparatedlabel-refs (list cmd type))
    (if (string= type "label")
        (TeX-add-symbols `(,cmd TeX-arg-define-label))
      (TeX-add-symbols `(,cmd TeX-arg-ref)))
    (TeX-argument-insert cmd optional TeX-esc)))

(defun LaTeX-floatrow-arg-declare-new-floattype (optional &optional cleanup)
  "Query and insert the first argument of \"DeclareNewFloatType\" macro.
If OPTIONAL is non-nil, insert the argument in brackets.  If
CLEANUP in non-nil, skip the query and insert process as we are
inside the function `LaTeX-floatrow-auto-cleanup' and process
only the parsed items."
  (let ((type (if cleanup
                  (mapcar #'car (LaTeX-floatrow-DeclareNewFloatType-list))
                (list (TeX-read-string
                       (TeX-argument-prompt optional nil "Environment type"))))))
    ;; If not inside `LaTeX-floatrow-auto-cleanup', add user input to
    ;; list of new floats and insert it
    (unless cleanup
      (LaTeX-add-floatrow-DeclareNewFloatTypes (car type))
      (TeX-argument-insert (car type) optional))
    ;; Make the next variables buffer local
    (make-local-variable 'LaTeX-floatrow-supported-float-types)
    (when (boundp 'LaTeX-caption-supported-float-types)
      (make-local-variable 'LaTeX-caption-supported-float-types))
    ;; Process new float type(s): a) add type to list of known
    ;; environments incl. "raw" version b) add different flavors of
    ;; type to `LaTeX-floatrow-supported-float-types' c) check if
    ;; `LaTeX-caption-supported-float-types' is bound and add
    ;; different flavors of type to it as well
    (dolist (elt type)
      (LaTeX-add-environments `(,elt LaTeX-floatrow-env-figure)
                              `(,(concat "raw" elt) LaTeX-floatrow-env-figure-raw))
      (add-to-list 'LaTeX-floatrow-supported-float-types elt t)
      (add-to-list 'LaTeX-floatrow-supported-float-types (concat "wide" elt) t)
      (add-to-list 'LaTeX-floatrow-supported-float-types (concat elt "row") t)
      (add-to-list 'LaTeX-floatrow-supported-float-types (concat "capbeside" elt) t)
      (when (boundp 'LaTeX-caption-supported-float-types)
        (add-to-list 'LaTeX-caption-supported-float-types elt t)
        (add-to-list 'LaTeX-caption-supported-float-types (concat "wide" elt) t)
        (add-to-list 'LaTeX-caption-supported-float-types (concat elt "row") t)
        (add-to-list 'LaTeX-caption-supported-float-types (concat "capbeside" elt) t)))))

(TeX-add-style-hook
 "floatrow"
 (lambda ()

   ;; Add floatrow to the parser
   (TeX-auto-add-regexp LaTeX-floatrow-DeclareNewOption-regexp)
   (TeX-auto-add-regexp LaTeX-floatrow-newfloatcommand-regexp)
   (TeX-auto-add-regexp LaTeX-floatrow-newseparatedlabel-ref-regexp)
   (TeX-auto-add-regexp LaTeX-floatrow-DeclareNewFloatType-regexp)

   ;; Add pre-defined float commands:
   (LaTeX-add-floatrow-newfloatcommands
    "ffigbox" "fcapside" "ttabbox")

   ;; Macros
   (TeX-add-symbols
    ;; 2.1 The \floatbox Macro
    ;; \floatbox[<preamble>]{<captype>}[<width>][<height>][<vert pos>]{<caption>}{<object>}
    '("floatbox"
      [TeX-arg-eval completing-read
                    (TeX-argument-prompt t nil "Preamble")
                    '("\\capbeside" "\\nocapbeside" "\\captop")]
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Float type")
                    LaTeX-floatrow-supported-float-types)
      LaTeX-floatrow-arg-floatbox)

    ;; 2.2 Creation of Personal Commands for Float Boxes
    '("newfloatcommand"
      (TeX-arg-eval
       (lambda ()
         (let ((cmd (TeX-read-string
                     (TeX-argument-prompt nil nil "Command"))))
           (LaTeX-add-floatrow-newfloatcommands cmd)
           (TeX-add-symbols
            `(,cmd LaTeX-floatrow-arg-floatbox))
           (format "%s" cmd))))
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Float type")
                    '("figure" "table"))
      [ 2 ])

    '("renewfloatcommand"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Command")
                    (LaTeX-floatrow-newfloatcommand-list))
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Float type")
                    '("figure" "table"))
      [ 2 ])


    ;; 2.2.2 Predefined Float Box Commands
    '("ffigbox"
      LaTeX-floatrow-arg-floatbox)

    '("ttabbox"
      LaTeX-floatrow-arg-floatbox)

    '("fcapside"
      LaTeX-floatrow-arg-floatbox)

    ;; 2.3.1 Mixed Row
    '("CenterFloatBoxes" 0)
    '("TopFloatBoxes" 0)
    '("BottomFloatBoxes" 0)
    '("PlainFloatBoxes" 0)
    '("buildFBBOX" 2)

    ;; 3 Float Layout Settings
    '("floatsetup"
      [TeX-arg-eval completing-read
                    (TeX-argument-prompt t nil "Float type")
                    LaTeX-floatrow-supported-float-types]
      (TeX-arg-key-val (LaTeX-floatrow-key-val-options)))

    ;; 3.2 Settings for Current Float Environment
    '("thisfloatsetup"
      (TeX-arg-key-val (LaTeX-floatrow-key-val-options)))

    ;; 3.3 Clearing of Settings for Current Float Type
    '("clearfloatsetup"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Float type")
                    LaTeX-floatrow-supported-float-types))

    ;; 3.4 Temporary Clearing of All Float Settings
    '("killfloatstyle" 0)

    ;; 3.6.1 Float Style Option (style=)
    '("DeclareFloatStyle"
      (LaTeX-floatrow-arg-declare-new-options "New style option"
                                              "FloatStyle"))

    ;; 3.6.2 Float Font Option (font=)
    '("DeclareFloatFont"
      (LaTeX-floatrow-arg-declare-new-options "New font option"
                                              "FloatFont"))

    ;; 3.6.3 Option for Float Rules/Skips (precode= etc.)
    '("DeclareFloatVCode"
      (LaTeX-floatrow-arg-declare-new-options "New rules/skip option"
                                              "FloatVCode"))

    ;; 3.6.4 Settings for Colored Frame (colorframeset=)
    '("DeclareColorBox"
      (LaTeX-floatrow-arg-declare-new-options "New colored frame option"
                                              "ColorBox"))

    ;; (colorframecorners=)
    '("DeclareCBoxCorners"
      (LaTeX-floatrow-arg-declare-new-options "New colored corner option"
                                              "CBoxCorners"))

    ;; 3.6.5 Object Justification Option (objectset=)
    '("DeclareObjectSet"
      (LaTeX-floatrow-arg-declare-new-options "New object justification"
                                              "ObjectSet"))

    ;; 3.6.6 Option for Float Box Alignment/Settings (margins=)
    '("DeclareMarginSet"
      (LaTeX-floatrow-arg-declare-new-options "New alignment option"
                                              "MarginSet"))

    '("setfloatmargins" 2)
    '("setfloatmargins*" 2)
    '("floatfacing" 2)
    '("floatfacing*" 2)
    '("floatboxmargins" 2)
    '("floatrowmargins" 2)
    '("floatcapbesidemargins" 2)

    ;; 3.6.7 Float Separators Options (floatrowsep=, capbesidesep=)
    '("DeclareFloatSeparators"
      (LaTeX-floatrow-arg-declare-new-options "New separator option"
                                              "FloatSeparators"))

    ;; 3.6.8 Option for Footnote Rule's Style (footnoterule=)
    '("DeclareFloatFootnoterule"
      (LaTeX-floatrow-arg-declare-new-options "New footnote rule option"
                                              "FloatFootnoterule"))

    ;; 4 Creation of New Float Types
    '("DeclareNewFloatType"
      (LaTeX-floatrow-arg-declare-new-floattype)
      (TeX-arg-key-val (("placement" ("tbp" "t" "b" "p"))
                        ("name")
                        ("fileext")
                        ("within" ("chapter" "section" "subsection"))
                        ("relatedcapstyle" ("yes" "no")))))

    ;; 6.2 Support of The Label-Sublabel References
    '("newseparatedlabel"
      (LaTeX-floatrow-arg-newseparatedlabel/ref "label") 2)

    '("newseparatedref"
      (LaTeX-floatrow-arg-newseparatedlabel/ref "ref") t)

    '("makelabelseparator" t) ) ;; terminate TeX-add-symbols

   ;; Environments
   (LaTeX-add-environments
    ;; 2.3 Building Float Row
    '("floatrow" [ "Number of beside floats" ])

    ;; 6.1 Managing of Float Parts With the subfloatrow Environment
    '("subfloatrow"  [ "Number of beside floats" ])
    '("subfloatrow*" [ "Number of beside floats" ]))

   ;; 2.1.1 Float Box Width Equals to The Width of Object Contents
   (LaTeX-add-lengths "FBwidth" "FBheight" "Xhsize")

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("floatbox"  "[{[[[")
                                ("ffigbox"   "[[[")
                                ("ttabbox"   "[[[")
                                ("fcapside"  "[[["))
                              'textual)
     (font-latex-add-keywords '(("newfloatcommand"           "{{[[")
                                ("renewfloatcommand"         "{{[[")
                                ("buildFBBOX"                "{{")
                                ("floatsetup"                "[{")
                                ("thisfloatsetup"            "{")
                                ("clearfloatsetup"           "{")
                                ("killfloatstyle"            "")
                                ("DeclareFloatStyle"         "{{")
                                ("DeclareFloatFont"          "{{")
                                ("DeclareFloatVCode"         "{{")
                                ("DeclareColorBox"           "{{")
                                ("DeclareCBoxCorners"        "{{")
                                ("DeclareObjectSet"          "{{")
                                ("DeclareMarginSet"          "{{")
                                ("DeclareFloatSeparators"    "{{")
                                ("DeclareFloatFootnoterule"  "{{")
                                ("newseparatedlabel"         "{{{")
                                ("newseparatedref"           "{{")
                                ("makelabelseparator"        "{")
                                ("DeclareNewFloatType"       "{{")
                                ("RawFloats"                 ""))
                              'function)))
 TeX-dialect)

(defvar LaTeX-floatrow-package-options nil
  "Prompt for package options for the floatrow package.")

;;; floatrow.el ends here