]> git.lyx.org Git - lyx.git/blob - src/Layout.cpp
Introduce listpreamble argument type
[lyx.git] / src / Layout.cpp
1 /**
2  * \file Layout.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "Layout.h"
16 #include "FontInfo.h"
17 #include "Language.h"
18 #include "Lexer.h"
19 #include "output_xhtml.h"
20 #include "TextClass.h"
21
22 #include "support/debug.h"
23 #include "support/lassert.h"
24 #include "support/lstrings.h"
25 #include "support/Messages.h"
26 #include "support/textutils.h"
27
28
29 using namespace std;
30 using namespace lyx::support;
31
32 namespace lyx {
33
34 /// Special value of toclevel for layouts that to not belong in a TOC
35 const int Layout::NOT_IN_TOC = -1000;
36
37 //  The order of the LayoutTags enum is no more important. [asierra300396]
38 // Tags indexes.
39 enum LayoutTags {
40         LT_ALIGN = 1,
41         LT_ALIGNPOSSIBLE,
42         LT_ARGUMENT,
43         LT_AUTONESTS,
44         LT_AUTONESTEDBY,
45         LT_MARGIN,
46         LT_BOTTOMSEP,
47         LT_CATEGORY,
48         LT_COMMANDDEPTH,
49         LT_COPYSTYLE,
50         LT_DEPENDSON,
51         LT_OBSOLETEDBY,
52         LT_END,
53         LT_FONT,
54         LT_FREE_SPACING,
55         LT_PASS_THRU,
56         LT_PASS_THRU_CHARS,
57         LT_PARBREAK_IS_NEWLINE,
58         LT_ITEMCOMMAND,
59         LT_ITEMSEP,
60         LT_KEEPEMPTY,
61         LT_LABEL_BOTTOMSEP,
62         LT_LABELFONT,
63         LT_TEXTFONT,
64         LT_LABELINDENT,
65         LT_LABELSEP,
66         LT_LABELSTRING,
67         LT_LABELSTRING_APPENDIX,
68         LT_LABELCOUNTER,
69         LT_LABELTYPE,
70         LT_ENDLABELSTRING,
71         LT_ENDLABELTYPE,
72         LT_LATEXNAME,
73         LT_LATEXPARAM,
74         LT_LATEXTYPE,
75         LT_LEFTDELIM,
76         LT_LEFTMARGIN,
77         LT_NEED_CPROTECT,
78         LT_NEED_PROTECT,
79         LT_NEWLINE,
80         LT_NEXTNOINDENT,
81         LT_PAR_GROUP,
82         LT_PARINDENT,
83         LT_PARSEP,
84         LT_PARSKIP,
85         LT_PREAMBLE,
86         LT_LANGPREAMBLE,
87         LT_BABELPREAMBLE,
88         LT_REQUIRES,
89         LT_RIGHTMARGIN,
90         LT_SPACING,
91         LT_TOPSEP,
92         LT_TOCLEVEL,
93         LT_INNERTAG,
94         LT_LABELTAG,
95         LT_ITEMTAG,
96         LT_HTMLTAG,
97         LT_HTMLATTR,
98         LT_HTMLITEM,
99         LT_HTMLITEMATTR,
100         LT_HTMLLABEL,
101         LT_HTMLLABELATTR,
102         LT_HTMLLABELFIRST,
103         LT_HTMLPREAMBLE,
104         LT_HTMLSTYLE,
105         LT_HTMLFORCECSS,
106         LT_INPREAMBLE,
107         LT_HTMLTITLE,
108         LT_SPELLCHECK,
109         LT_REFPREFIX,
110         LT_RESETARGS,
111         LT_RESUMECOUNTER,
112         LT_STEPMASTERCOUNTER,
113         LT_RIGHTDELIM,
114         LT_FORCELOCAL,
115         LT_TOGGLE_INDENT,
116         LT_ADDTOTOC,
117         LT_ISTOCCAPTION,
118         LT_INTITLE // keep this last!
119 };
120
121 /////////////////////
122
123 Layout::Layout()
124         : add_to_toc_(false), is_toc_caption_(true)
125 {
126         unknown_ = false;
127         margintype = MARGIN_STATIC;
128         latextype = LATEX_PARAGRAPH;
129         resumecounter = false;
130         stepmastercounter = false;
131         intitle = false;
132         inpreamble = false;
133         needprotect = false;
134         needcprotect = false;
135         keepempty = false;
136         font = inherit_font;
137         labelfont = inherit_font;
138         resfont = sane_font;
139         reslabelfont = sane_font;
140         nextnoindent = false;
141         parskip = 0.0;
142         itemsep = 0;
143         topsep = 0.0;
144         bottomsep = 0.0;
145         labelbottomsep = 0.0;
146         parsep = 0;
147         align = LYX_ALIGN_BLOCK;
148         alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
149         labeltype = LABEL_NO_LABEL;
150         endlabeltype = END_LABEL_NO_LABEL;
151         // Should or should not. That is the question.
152         // spacing.set(Spacing::OneHalf);
153         newline_allowed = true;
154         free_spacing = false;
155         pass_thru = false;
156         parbreak_is_newline = false;
157         toclevel = NOT_IN_TOC;
158         commanddepth = 0;
159         htmllabelfirst_ = false;
160         htmlforcecss_ = false;
161         htmltitle_ = false;
162         spellcheck = true;
163         forcelocal = 0;
164         itemcommand_ = "item";
165         toggle_indent = ITOGGLE_DOCUMENT_DEFAULT;
166         par_group_ = false;
167 }
168
169
170 bool Layout::read(Lexer & lex, TextClass const & tclass)
171 {
172         // If this is an empty layout, or if no force local version is set,
173         // we know that we will not discard the stuff to read
174         if (forcelocal == 0)
175                 return readIgnoreForcelocal(lex, tclass);
176         Layout tmp(*this);
177         tmp.forcelocal = 0;
178         bool const ret = tmp.readIgnoreForcelocal(lex, tclass);
179         // Keep the stuff if
180         // - the read version is higher
181         // - both versions are infinity (arbitrary decision)
182         // - the file did not contain any local version (needed for not
183         //   skipping user defined local layouts)
184         if (tmp.forcelocal <= 0 || tmp.forcelocal > forcelocal)
185                 *this = tmp;
186         return ret;
187 }
188
189
190 bool Layout::readIgnoreForcelocal(Lexer & lex, TextClass const & tclass)
191 {
192         // This table is sorted alphabetically [asierra 30March96]
193         LexerKeyword layoutTags[] = {
194                 { "addtotoc",       LT_ADDTOTOC },
195                 { "align",          LT_ALIGN },
196                 { "alignpossible",  LT_ALIGNPOSSIBLE },
197                 { "argument",       LT_ARGUMENT },
198                 { "autonests",      LT_AUTONESTS },
199                 { "babelpreamble",  LT_BABELPREAMBLE },
200                 { "bottomsep",      LT_BOTTOMSEP },
201                 { "category",       LT_CATEGORY },
202                 { "commanddepth",   LT_COMMANDDEPTH },
203                 { "copystyle",      LT_COPYSTYLE },
204                 { "dependson",      LT_DEPENDSON },
205                 { "end",            LT_END },
206                 { "endlabelstring", LT_ENDLABELSTRING },
207                 { "endlabeltype",   LT_ENDLABELTYPE },
208                 { "font",           LT_FONT },
209                 { "forcelocal",     LT_FORCELOCAL },
210                 { "freespacing",    LT_FREE_SPACING },
211                 { "htmlattr",       LT_HTMLATTR },
212                 { "htmlforcecss",   LT_HTMLFORCECSS },
213                 { "htmlitem",       LT_HTMLITEM },
214                 { "htmlitemattr",   LT_HTMLITEMATTR },
215                 { "htmllabel",      LT_HTMLLABEL },
216                 { "htmllabelattr",  LT_HTMLLABELATTR },
217                 { "htmllabelfirst", LT_HTMLLABELFIRST },
218                 { "htmlpreamble",   LT_HTMLPREAMBLE },
219                 { "htmlstyle",      LT_HTMLSTYLE },
220                 { "htmltag",        LT_HTMLTAG },
221                 { "htmltitle",      LT_HTMLTITLE },
222                 { "innertag",       LT_INNERTAG },
223                 { "inpreamble",     LT_INPREAMBLE },
224                 { "intitle",        LT_INTITLE },
225                 { "isautonestedby", LT_AUTONESTEDBY },
226                 { "istoccaption",   LT_ISTOCCAPTION },
227                 { "itemcommand",    LT_ITEMCOMMAND },
228                 { "itemsep",        LT_ITEMSEP },
229                 { "itemtag",        LT_ITEMTAG },
230                 { "keepempty",      LT_KEEPEMPTY },
231                 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
232                 { "labelcounter",   LT_LABELCOUNTER },
233                 { "labelfont",      LT_LABELFONT },
234                 { "labelindent",    LT_LABELINDENT },
235                 { "labelsep",       LT_LABELSEP },
236                 { "labelstring",    LT_LABELSTRING },
237                 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
238                 { "labeltag",       LT_LABELTAG },
239                 { "labeltype",      LT_LABELTYPE },
240                 { "langpreamble",   LT_LANGPREAMBLE },
241                 { "latexname",      LT_LATEXNAME },
242                 { "latexparam",     LT_LATEXPARAM },
243                 { "latextype",      LT_LATEXTYPE },
244                 { "leftdelim",      LT_LEFTDELIM },
245                 { "leftmargin",     LT_LEFTMARGIN },
246                 { "margin",         LT_MARGIN },
247                 { "needcprotect",    LT_NEED_CPROTECT },
248                 { "needprotect",    LT_NEED_PROTECT },
249                 { "newline",        LT_NEWLINE },
250                 { "nextnoindent",   LT_NEXTNOINDENT },
251                 { "obsoletedby",    LT_OBSOLETEDBY },
252                 { "paragraphgroup", LT_PAR_GROUP },
253                 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
254                 { "parindent",      LT_PARINDENT },
255                 { "parsep",         LT_PARSEP },
256                 { "parskip",        LT_PARSKIP },
257                 { "passthru",       LT_PASS_THRU },
258                 { "passthruchars",  LT_PASS_THRU_CHARS },
259                 { "preamble",       LT_PREAMBLE },
260                 { "refprefix",      LT_REFPREFIX },
261                 { "requires",       LT_REQUIRES },
262                 { "resetargs",      LT_RESETARGS },
263                 { "resumecounter",  LT_RESUMECOUNTER },
264                 { "rightdelim",     LT_RIGHTDELIM },
265                 { "rightmargin",    LT_RIGHTMARGIN },
266                 { "spacing",        LT_SPACING },
267                 { "spellcheck",     LT_SPELLCHECK },
268                 { "stepmastercounter",  LT_STEPMASTERCOUNTER },
269                 { "textfont",       LT_TEXTFONT },
270                 { "toclevel",       LT_TOCLEVEL },
271                 { "toggleindent",   LT_TOGGLE_INDENT },
272                 { "topsep",         LT_TOPSEP }
273         };
274
275         bool error = false;
276         bool finished = false;
277         lex.pushTable(layoutTags);
278
279         // parse style section
280         while (!finished && lex.isOK() && !error) {
281                 int le = lex.lex();
282                 // See comment in LyXRC.cpp.
283                 switch (le) {
284                 case Lexer::LEX_FEOF:
285                         continue;
286
287                 case Lexer::LEX_UNDEF:
288                         // parse error
289                         lex.printError("Unknown layout tag `$$Token'");
290                         error = true;
291                         continue;
292
293                 default:
294                         break;
295                 }
296                 switch (static_cast<LayoutTags>(le)) {
297                 case LT_END:
298                         finished = true;
299                         break;
300
301                 case LT_CATEGORY:
302                         lex >> category_;
303                         break;
304
305                 case LT_COPYSTYLE: {
306                         docstring style;
307                         lex >> style;
308                         style = subst(style, '_', ' ');
309
310                         if (tclass.hasLayout(style)) {
311                                 docstring const tmpname = name_;
312                                 this->operator=(tclass[style]);
313                                 name_ = tmpname;
314                         } else {
315                                 LYXERR0("Cannot copy unknown style `"
316                                         << style << "'\n"
317                                         << "All layouts so far:");
318                                 DocumentClass::const_iterator lit = tclass.begin();
319                                 DocumentClass::const_iterator len = tclass.end();
320                                 for (; lit != len; ++lit)
321                                         LYXERR0(lit->name());
322                         }
323                         break;
324                         }
325
326                 case LT_OBSOLETEDBY: {
327                         docstring style;
328                         lex >> style;
329                         style = subst(style, '_', ' ');
330
331                         if (tclass.hasLayout(style)) {
332                                 docstring const tmpname = name_;
333                                 this->operator=(tclass[style]);
334                                 name_ = tmpname;
335                                 if (obsoleted_by().empty())
336                                         obsoleted_by_ = style;
337                         } else {
338                                 LYXERR0("Cannot replace with unknown style `"
339                                         << style << '\'');
340
341                                 //lex.printError("Cannot replace with"
342                                 //               " unknown style "
343                                 //               "`$$Token'");
344                         }
345                         break;
346                 }
347
348                 case LT_DEPENDSON:
349                         lex >> depends_on_;
350                         depends_on_ = subst(depends_on_, '_', ' ');
351                         break;
352
353                 case LT_MARGIN:
354                         readMargin(lex);
355                         break;
356
357                 case LT_LATEXTYPE:
358                         readLatexType(lex);
359                         break;
360
361                 case LT_INTITLE:
362                         lex >> intitle;
363                         break;
364
365                 case LT_INPREAMBLE:
366                         lex >> inpreamble;
367                         break;
368
369                 case LT_TOCLEVEL:
370                         lex >> toclevel;
371                         break;
372
373                 case LT_RESETARGS:
374                         bool reset;
375                         lex >> reset;
376                         if (reset) {
377                                 latexargs_.clear();
378                                 itemargs_.clear();
379                                 postcommandargs_.clear();
380                                 listpreamble_.clear();
381                         }
382                         break;
383
384                 case LT_RESUMECOUNTER:
385                         lex >> resumecounter;
386                         break;
387
388                 case LT_STEPMASTERCOUNTER:
389                         lex >> stepmastercounter;
390                         break;
391
392                 case LT_ARGUMENT:
393                         readArgument(lex);
394                         break;
395
396                 case LT_NEED_PROTECT:
397                         lex >> needprotect;
398                         break;
399
400                 case LT_NEED_CPROTECT:
401                         lex >> needcprotect;
402                         break;
403
404                 case LT_KEEPEMPTY:
405                         lex >> keepempty;
406                         break;
407
408                 case LT_FONT:
409                         font = lyxRead(lex, font);
410                         labelfont = font;
411                         break;
412
413                 case LT_TEXTFONT:
414                         font = lyxRead(lex, font);
415                         break;
416
417                 case LT_LABELFONT:
418                         labelfont = lyxRead(lex, labelfont);
419                         break;
420
421                 case LT_NEXTNOINDENT:
422                         lex >> nextnoindent;
423                         break;
424
425                 case LT_TOGGLE_INDENT: {
426                         string tog;
427                         lex >> tog;
428                         tog = support::ascii_lowercase(tog);
429                         if (tog == "always")
430                                 toggle_indent = ITOGGLE_ALWAYS;
431                         else if (tog == "never")
432                                 toggle_indent = ITOGGLE_NEVER;
433                         else
434                                 toggle_indent = ITOGGLE_DOCUMENT_DEFAULT;
435                         break;
436                 }
437
438                 case LT_COMMANDDEPTH:
439                         lex >> commanddepth;
440                         break;
441
442                 case LT_LATEXNAME:
443                         lex >> latexname_;
444                         break;
445
446                 case LT_LATEXPARAM:
447                         lex >> latexparam_;
448                         latexparam_ = subst(latexparam_, "&quot;", "\"");
449                         break;
450
451                 case LT_LEFTDELIM:
452                         lex >> leftdelim_;
453                         leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
454                                                     from_ascii("\n"));
455                         break;
456
457                 case LT_RIGHTDELIM:
458                         lex >> rightdelim_;
459                         rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
460                                                      from_ascii("\n"));
461                         break;
462
463                 case LT_INNERTAG:
464                         lex >> innertag_;
465                         break;
466
467                 case LT_LABELTAG:
468                         lex >> labeltag_;
469                         break;
470
471                 case LT_ITEMTAG:
472                         lex >> itemtag_;
473                         break;
474
475                 case LT_ITEMCOMMAND:
476                         lex >> itemcommand_;
477                         break;
478
479                 case LT_PREAMBLE:
480                         preamble_ = lex.getLongString(from_ascii("EndPreamble"));
481                         break;
482
483                 case LT_LANGPREAMBLE:
484                         langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
485                         break;
486
487                 case LT_BABELPREAMBLE:
488                         babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
489                         break;
490
491                 case LT_LABELTYPE:
492                         readLabelType(lex);
493                         break;
494
495                 case LT_ENDLABELTYPE:
496                         readEndLabelType(lex);
497                         break;
498
499                 case LT_LEFTMARGIN:
500                         lex >> leftmargin;
501                         break;
502
503                 case LT_RIGHTMARGIN:
504                         lex >> rightmargin;
505                         break;
506
507                 case LT_LABELINDENT:
508                         lex >> labelindent;
509                         break;
510
511                 case LT_PARINDENT:
512                         lex >> parindent;
513                         break;
514
515                 case LT_PARSKIP:
516                         lex >> parskip;
517                         break;
518
519                 case LT_ITEMSEP:
520                         lex >> itemsep;
521                         break;
522
523                 case LT_TOPSEP:
524                         lex >> topsep;
525                         break;
526
527                 case LT_BOTTOMSEP:
528                         lex >> bottomsep;
529                         break;
530
531                 case LT_LABEL_BOTTOMSEP:
532                         lex >> labelbottomsep;
533                         break;
534
535                 case LT_LABELSEP:
536                         lex >> labelsep;
537                         labelsep = subst(labelsep, 'x', ' ');
538                         break;
539
540                 case LT_PARSEP:
541                         lex >> parsep;
542                         break;
543
544                 case LT_NEWLINE:
545                         lex >> newline_allowed;
546                         break;
547
548                 case LT_ALIGN:
549                         readAlign(lex);
550                         break;
551
552                 case LT_ALIGNPOSSIBLE:
553                         readAlignPossible(lex);
554                         break;
555
556                 case LT_LABELSTRING:
557                         // FIXME: this means LT_LABELSTRING_APPENDIX may only
558                         // occur after LT_LABELSTRING
559                         lex >> labelstring_;
560                         labelstring_appendix_ = labelstring_;
561                         break;
562
563                 case LT_ENDLABELSTRING:
564                         lex >> endlabelstring_;
565                         break;
566
567                 case LT_LABELSTRING_APPENDIX:
568                         lex >> labelstring_appendix_;
569                         break;
570
571                 case LT_LABELCOUNTER:
572                         lex >> counter;
573                         break;
574
575                 case LT_PAR_GROUP:
576                         lex >> par_group_;
577                         break;
578
579                 case LT_FREE_SPACING:
580                         lex >> free_spacing;
581                         break;
582
583                 case LT_PASS_THRU:
584                         lex >> pass_thru;
585                         break;
586
587                 case LT_PASS_THRU_CHARS:
588                         lex >> pass_thru_chars;
589                         break;
590
591                 case LT_PARBREAK_IS_NEWLINE:
592                         lex >> parbreak_is_newline;
593                         break;
594
595                 case LT_SPACING:
596                         readSpacing(lex);
597                         break;
598
599                 case LT_REQUIRES: {
600                         lex.eatLine();
601                         vector<string> const req =
602                                 getVectorFromString(lex.getString(true));
603                         requires_.insert(req.begin(), req.end());
604                         break;
605                 }
606
607                 case LT_AUTONESTS: {
608                         docstring const autonest =
609                                 subst(subst(subst(lex.getLongString(from_ascii("EndAutoNests")),
610                                                   from_ascii("\n"), docstring()),
611                                             from_ascii(" "), docstring()),
612                                       from_ascii("\t"), docstring());
613                         vector<docstring> const autonests =
614                                 getVectorFromString(autonest);
615                         autonests_.insert(autonests.begin(), autonests.end());
616                         break;
617                 }
618
619                 case LT_AUTONESTEDBY: {
620                         docstring const autonest =
621                                 subst(subst(subst(lex.getLongString(from_ascii("EndIsAutoNestedBy")),
622                                                   from_ascii("\n"), docstring()),
623                                             from_ascii(" "), docstring()),
624                                       from_ascii("\t"), docstring());
625                         vector<docstring> const autonests =
626                                 getVectorFromString(autonest);
627                         autonested_by_.insert(autonests.begin(), autonests.end());
628                         break;
629                 }
630
631                 case LT_REFPREFIX: {
632                         docstring arg;
633                         lex >> arg;
634                         if (arg == "OFF")
635                                 refprefix.clear();
636                         else
637                                 refprefix = arg;
638                         break;
639                 }
640
641                 case LT_HTMLTAG:
642                         lex >> htmltag_;
643                         break;
644
645                 case LT_HTMLATTR:
646                         lex >> htmlattr_;
647                         break;
648
649                 case LT_HTMLITEM:
650                         lex >> htmlitemtag_;
651                         break;
652
653                 case LT_HTMLITEMATTR:
654                         lex >> htmlitemattr_;
655                         break;
656
657                 case LT_HTMLLABEL:
658                         lex >> htmllabeltag_;
659                         break;
660
661                 case LT_HTMLLABELATTR:
662                         lex >> htmllabelattr_;
663                         break;
664
665                 case LT_HTMLLABELFIRST:
666                         lex >> htmllabelfirst_;
667                         break;
668
669                 case LT_HTMLSTYLE:
670                         htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
671                         break;
672
673                 case LT_HTMLFORCECSS:
674                         lex >> htmlforcecss_;
675                         break;
676
677                 case LT_HTMLPREAMBLE:
678                         htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
679                         break;
680
681                 case LT_HTMLTITLE:
682                         lex >> htmltitle_;
683                         break;
684
685                 case LT_SPELLCHECK:
686                         lex >> spellcheck;
687                         break;
688
689                 case LT_FORCELOCAL:
690                         lex >> forcelocal;
691                         break;
692
693                 case LT_ADDTOTOC:
694                         lex >> toc_type_;
695                         add_to_toc_ = !toc_type_.empty();
696                         break;
697
698                 case LT_ISTOCCAPTION:
699                         lex >> is_toc_caption_;
700                         break;
701
702                 }
703         }
704         lex.popTable();
705         // make sure we only have inpreamble = true for commands
706         if (inpreamble && latextype != LATEX_COMMAND && latextype != LATEX_PARAGRAPH) {
707                 LYXERR0("InPreamble not permitted except with command and paragraph layouts.");
708                 LYXERR0("Layout name: " << name());
709                 inpreamble = false;
710         }
711
712         return finished && !error;
713 }
714
715
716 enum {
717         AT_BLOCK = 1,
718         AT_LEFT,
719         AT_RIGHT,
720         AT_CENTER,
721         AT_LAYOUT
722 };
723
724
725 LexerKeyword alignTags[] = {
726         { "block",  AT_BLOCK },
727         { "center", AT_CENTER },
728         { "layout", AT_LAYOUT },
729         { "left",   AT_LEFT },
730         { "right",  AT_RIGHT }
731 };
732
733
734 void Layout::readAlign(Lexer & lex)
735 {
736         PushPopHelper pph(lex, alignTags);
737         int le = lex.lex();
738         switch (le) {
739         case Lexer::LEX_UNDEF:
740                 lex.printError("Unknown alignment `$$Token'");
741                 return;
742         default: break;
743         };
744         switch (le) {
745         case AT_BLOCK:
746                 align = LYX_ALIGN_BLOCK;
747                 break;
748         case AT_LEFT:
749                 align = LYX_ALIGN_LEFT;
750                 break;
751         case AT_RIGHT:
752                 align = LYX_ALIGN_RIGHT;
753                 break;
754         case AT_CENTER:
755                 align = LYX_ALIGN_CENTER;
756                 break;
757         case AT_LAYOUT:
758                 align = LYX_ALIGN_LAYOUT;
759                 break;
760         }
761 }
762
763
764 void Layout::readAlignPossible(Lexer & lex)
765 {
766         lex.pushTable(alignTags);
767         alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
768         int lineno = lex.lineNumber();
769         do {
770                 int le = lex.lex();
771                 switch (le) {
772                 case Lexer::LEX_UNDEF:
773                         lex.printError("Unknown alignment `$$Token'");
774                         continue;
775                 default: break;
776                 };
777                 switch (le) {
778                 case AT_BLOCK:
779                         alignpossible |= LYX_ALIGN_BLOCK;
780                         break;
781                 case AT_LEFT:
782                         alignpossible |= LYX_ALIGN_LEFT;
783                         break;
784                 case AT_RIGHT:
785                         alignpossible |= LYX_ALIGN_RIGHT;
786                         break;
787                 case AT_CENTER:
788                         alignpossible |= LYX_ALIGN_CENTER;
789                         break;
790                 case AT_LAYOUT:
791                         alignpossible |= LYX_ALIGN_LAYOUT;
792                         break;
793                 }
794         } while (lineno == lex.lineNumber());
795         lex.popTable();
796 }
797
798
799 void Layout::readLabelType(Lexer & lex)
800 {
801         enum {
802                 LA_NO_LABEL = 1,
803                 LA_MANUAL,
804                 LA_ABOVE,
805                 LA_CENTERED,
806                 LA_STATIC,
807                 LA_SENSITIVE,
808                 LA_ENUMERATE,
809                 LA_ITEMIZE,
810                 LA_BIBLIO
811         };
812
813
814         LexerKeyword labelTypeTags[] = {
815           { "above",        LA_ABOVE },
816                 { "bibliography", LA_BIBLIO },
817                 { "centered",     LA_CENTERED },
818                 { "enumerate",    LA_ENUMERATE },
819                 { "itemize",      LA_ITEMIZE },
820                 { "manual",       LA_MANUAL },
821                 { "no_label",     LA_NO_LABEL },
822                 { "sensitive",    LA_SENSITIVE },
823                 { "static",       LA_STATIC }
824         };
825
826         PushPopHelper pph(lex, labelTypeTags);
827         int le = lex.lex();
828         switch (le) {
829         case Lexer::LEX_UNDEF:
830                 lex.printError("Unknown labeltype tag `$$Token'");
831                 return;
832         default: break;
833         }
834         switch (le) {
835         case LA_NO_LABEL:
836                 labeltype = LABEL_NO_LABEL;
837                 break;
838         case LA_MANUAL:
839                 labeltype = LABEL_MANUAL;
840                 break;
841         case LA_ABOVE:
842                 labeltype = LABEL_ABOVE;
843                 break;
844         case LA_CENTERED:
845                 labeltype = LABEL_CENTERED;
846                 break;
847         case LA_STATIC:
848                 labeltype = LABEL_STATIC;
849                 break;
850         case LA_SENSITIVE:
851                 labeltype = LABEL_SENSITIVE;
852                 break;
853         case LA_ENUMERATE:
854                 labeltype = LABEL_ENUMERATE;
855                 break;
856         case LA_ITEMIZE:
857                 labeltype = LABEL_ITEMIZE;
858                 break;
859         case LA_BIBLIO:
860                 labeltype = LABEL_BIBLIO;
861                 break;
862         }
863 }
864
865
866 void Layout::readEndLabelType(Lexer & lex)
867 {
868         // this should be const, but can't be because
869         // of PushPopHelper.
870         static LexerKeyword endlabelTypeTags[] = {
871                 { "box",              END_LABEL_BOX },
872                 { "filled_box", END_LABEL_FILLED_BOX },
873                 { "no_label",     END_LABEL_NO_LABEL },
874                 { "static",     END_LABEL_STATIC }
875         };
876
877         PushPopHelper pph(lex, endlabelTypeTags);
878         int le = lex.lex();
879         switch (le) {
880         case Lexer::LEX_UNDEF:
881                 lex.printError("Unknown labeltype tag `$$Token'");
882                 break;
883         case END_LABEL_STATIC:
884         case END_LABEL_BOX:
885         case END_LABEL_FILLED_BOX:
886         case END_LABEL_NO_LABEL:
887                 endlabeltype = static_cast<EndLabelType>(le);
888                 break;
889         default:
890                 LYXERR0("Unhandled value " << le);
891                 break;
892         }
893 }
894
895
896 void Layout::readMargin(Lexer & lex)
897 {
898         LexerKeyword marginTags[] = {
899                 { "dynamic",           MARGIN_DYNAMIC },
900                 { "first_dynamic",     MARGIN_FIRST_DYNAMIC },
901                 { "manual",            MARGIN_MANUAL },
902                 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
903                 { "static",            MARGIN_STATIC }
904         };
905
906         PushPopHelper pph(lex, marginTags);
907
908         int le = lex.lex();
909         switch (le) {
910         case Lexer::LEX_UNDEF:
911                 lex.printError("Unknown margin type tag `$$Token'");
912                 return;
913         case MARGIN_STATIC:
914         case MARGIN_MANUAL:
915         case MARGIN_DYNAMIC:
916         case MARGIN_FIRST_DYNAMIC:
917         case MARGIN_RIGHT_ADDRESS_BOX:
918                 margintype = static_cast<MarginType>(le);
919                 break;
920         default:
921                 LYXERR0("Unhandled value " << le);
922                 break;
923         }
924 }
925
926
927 void Layout::readLatexType(Lexer & lex)
928 {
929         LexerKeyword latexTypeTags[] = {
930                 { "bib_environment",  LATEX_BIB_ENVIRONMENT },
931                 { "command",          LATEX_COMMAND },
932                 { "environment",      LATEX_ENVIRONMENT },
933                 { "item_environment", LATEX_ITEM_ENVIRONMENT },
934                 { "list_environment", LATEX_LIST_ENVIRONMENT },
935                 { "paragraph",        LATEX_PARAGRAPH }
936         };
937
938         PushPopHelper pph(lex, latexTypeTags);
939         int le = lex.lex();
940         switch (le) {
941         case Lexer::LEX_UNDEF:
942                 lex.printError("Unknown latextype tag `$$Token'");
943                 return;
944         case LATEX_PARAGRAPH:
945         case LATEX_COMMAND:
946         case LATEX_ITEM_ENVIRONMENT:
947         case LATEX_LIST_ENVIRONMENT:
948                 latextype = static_cast<LatexType>(le);
949                 break;
950         case LATEX_ENVIRONMENT:
951         case LATEX_BIB_ENVIRONMENT:
952                 latextype = static_cast<LatexType>(le);
953                 par_group_ = true;
954                 break;
955         default:
956                 LYXERR0("Unhandled value " << le);
957                 break;
958         }
959 }
960
961
962 void Layout::readSpacing(Lexer & lex)
963 {
964         enum {
965                 ST_SPACING_SINGLE = 1,
966                 ST_SPACING_ONEHALF,
967                 ST_SPACING_DOUBLE,
968                 ST_OTHER
969         };
970
971         LexerKeyword spacingTags[] = {
972                 {"double",  ST_SPACING_DOUBLE },
973                 {"onehalf", ST_SPACING_ONEHALF },
974                 {"other",   ST_OTHER },
975                 {"single",  ST_SPACING_SINGLE }
976         };
977
978         PushPopHelper pph(lex, spacingTags);
979         int le = lex.lex();
980         switch (le) {
981         case Lexer::LEX_UNDEF:
982                 lex.printError("Unknown spacing token `$$Token'");
983                 return;
984         default: break;
985         }
986         switch (le) {
987         case ST_SPACING_SINGLE:
988                 spacing.set(Spacing::Single);
989                 break;
990         case ST_SPACING_ONEHALF:
991                 spacing.set(Spacing::Onehalf);
992                 break;
993         case ST_SPACING_DOUBLE:
994                 spacing.set(Spacing::Double);
995                 break;
996         case ST_OTHER:
997                 lex.next();
998                 spacing.set(Spacing::Other, lex.getString());
999                 break;
1000         }
1001 }
1002
1003
1004 void Layout::readArgument(Lexer & lex)
1005 {
1006         latexarg arg;
1007         // writeArgument() makes use of these default values
1008         arg.mandatory = false;
1009         arg.nodelims = false;
1010         arg.autoinsert = false;
1011         arg.insertcotext = false;
1012         bool error = false;
1013         bool finished = false;
1014         arg.font = inherit_font;
1015         arg.labelfont = inherit_font;
1016         arg.is_toc_caption = false;
1017         arg.passthru = PT_INHERITED;
1018         string id;
1019         lex >> id;
1020         bool const itemarg = prefixIs(id, "item:");
1021         bool const postcmd = prefixIs(id, "post:");
1022         bool const listpreamble = prefixIs(id, "listpreamble:");
1023
1024         while (!finished && lex.isOK() && !error) {
1025                 lex.next();
1026                 string const tok = ascii_lowercase(lex.getString());
1027
1028                 if (tok.empty()) {
1029                         continue;
1030                 } else if (tok == "endargument") {
1031                         finished = true;
1032                 } else if (tok == "labelstring") {
1033                         lex.next();
1034                         arg.labelstring = lex.getDocString();
1035                 } else if (tok == "menustring") {
1036                         lex.next();
1037                         arg.menustring = lex.getDocString();
1038                 } else if (tok == "mandatory") {
1039                         lex.next();
1040                         arg.mandatory = lex.getBool();
1041                 } else if (tok == "autoinsert") {
1042                         lex.next();
1043                         arg.autoinsert = lex.getBool();
1044                 } else if (tok == "insertcotext") {
1045                         lex.next();
1046                         arg.insertcotext = lex.getBool();
1047                 } else if (tok == "leftdelim") {
1048                         lex.next();
1049                         arg.ldelim = lex.getDocString();
1050                         arg.ldelim = support::subst(arg.ldelim, from_ascii("<br/>"),
1051                                                     from_ascii("\n"));
1052                 } else if (tok == "rightdelim") {
1053                         lex.next();
1054                         arg.rdelim = lex.getDocString();
1055                         arg.rdelim = support::subst(arg.rdelim, from_ascii("<br/>"),
1056                                                     from_ascii("\n"));
1057                 } else if (tok == "defaultarg") {
1058                         lex.next();
1059                         arg.defaultarg = lex.getDocString();
1060                 } else if (tok == "presetarg") {
1061                         lex.next();
1062                         arg.presetarg = lex.getDocString();
1063                 } else if (tok == "tooltip") {
1064                         lex.next();
1065                         arg.tooltip = lex.getDocString();
1066                 } else if (tok == "requires") {
1067                         lex.next();
1068                         arg.requires = lex.getString();
1069                 } else if (tok == "decoration") {
1070                         lex.next();
1071                         arg.decoration = lex.getString();
1072                 } else if (tok == "font") {
1073                         arg.font = lyxRead(lex, arg.font);
1074                 } else if (tok == "labelfont") {
1075                         arg.labelfont = lyxRead(lex, arg.labelfont);
1076                 } else if (tok == "passthruchars") {
1077                         lex.next();
1078                         arg.pass_thru_chars = lex.getDocString();
1079                 } else if (tok == "passthru") {
1080                         lex.next();
1081                         docstring value = lex.getDocString();
1082                         if (value == "true" || value == "1")
1083                                 arg.passthru = PT_TRUE;
1084                         else if (value == "false" || value == "0")
1085                                 arg.passthru = PT_FALSE;
1086                         else
1087                                 arg.passthru = PT_INHERITED;
1088                 } else if (tok == "istoccaption") {
1089                         lex.next();
1090                         arg.is_toc_caption = lex.getBool();
1091                 } else {
1092                         lex.printError("Unknown tag");
1093                         error = true;
1094                 }
1095         }
1096         if (arg.labelstring.empty())
1097                 LYXERR0("Incomplete Argument definition!");
1098         else if (itemarg)
1099                 itemargs_[id] = arg;
1100         else if (postcmd)
1101                 postcommandargs_[id] = arg;
1102         else if (listpreamble) {
1103                 // list preamble has no delimiters by default
1104                 arg.nodelims = true;
1105                 listpreamble_[id] = arg;
1106         } else
1107                 latexargs_[id] = arg;
1108 }
1109
1110
1111 void writeArgument(ostream & os, string const & id, Layout::latexarg const & arg)
1112 {
1113         os << "\tArgument " << id << '\n';
1114         if (!arg.labelstring.empty())
1115                 os << "\t\tLabelString \"" << to_utf8(arg.labelstring) << "\"\n";
1116         if (!arg.menustring.empty())
1117                 os << "\t\tMenuString \"" << to_utf8(arg.menustring) << "\"\n";
1118         if (arg.mandatory)
1119                 os << "\t\tMandatory " << arg.mandatory << '\n';
1120         if (arg.autoinsert)
1121                 os << "\t\tAutoinsert " << arg.autoinsert << '\n';
1122         if (arg.insertcotext)
1123                 os << "\t\tInsertCotext " << arg.insertcotext << '\n';
1124         if (!arg.ldelim.empty())
1125                 os << "\t\tLeftDelim \""
1126                    << to_utf8(subst(arg.ldelim, from_ascii("\n"), from_ascii("<br/>")))
1127                    << "\"\n";
1128         if (!arg.rdelim.empty())
1129                 os << "\t\tRightDelim \""
1130                    << to_utf8(subst(arg.rdelim, from_ascii("\n"), from_ascii("<br/>")))
1131                    << "\"\n";
1132         if (!arg.defaultarg.empty())
1133                 os << "\t\tDefaultArg \"" << to_utf8(arg.defaultarg) << "\"\n";
1134         if (!arg.presetarg.empty())
1135                 os << "\t\tPresetArg \"" << to_utf8(arg.presetarg) << "\"\n";
1136         if (!arg.tooltip.empty())
1137                 os << "\t\tToolTip \"" << to_utf8(arg.tooltip) << "\"\n";
1138         if (!arg.requires.empty())
1139                 os << "\t\tRequires \"" << arg.requires << "\"\n";
1140         if (!arg.decoration.empty())
1141                 os << "\t\tDecoration \"" << arg.decoration << "\"\n";
1142         if (arg.font != inherit_font)
1143                 lyxWrite(os, arg.font, "Font", 2);
1144         if (arg.labelfont != inherit_font)
1145                 lyxWrite(os, arg.labelfont, "LabelFont", 2);
1146         switch (arg.passthru) {
1147                 case PT_TRUE:
1148                         os << "\t\tPassThru true\n";
1149                         break;
1150                 case PT_FALSE:
1151                         os << "\t\tPassThru false\n";
1152                         break;
1153                 case PT_INHERITED:
1154                         os << "\t\tPassThru inherited\n";
1155                         break;
1156         }
1157         if (!arg.pass_thru_chars.empty())
1158                 os << "\t\tPassThruChars \"" << to_utf8(arg.pass_thru_chars) << "\"\n";
1159         os << "\tEndArgument\n";
1160 }
1161
1162
1163 void Layout::write(ostream & os) const
1164 {
1165         os << "Style \"" << to_utf8(name_) << "\"\n";
1166         if (!category_.empty() && obsoleted_by_.empty())
1167                 os << "\tCategory \"" << to_utf8(category_) << "\"\n";
1168         // Can't deduce Copystyle here :-(
1169         if (!obsoleted_by_.empty()) {
1170                 os << "\tObsoletedBy \"" << to_utf8(obsoleted_by_)
1171                    << "\"\nEnd\n";
1172                 return;
1173         }
1174         if (!depends_on_.empty())
1175                 os << "\tDependsOn " << to_utf8(depends_on_) << '\n';
1176         switch (margintype) {
1177                 case MARGIN_DYNAMIC:
1178                         os << "\tMargin Dynamic\n";
1179                         break;
1180                 case MARGIN_FIRST_DYNAMIC:
1181                         os << "\tMargin First_Dynamic\n";
1182                         break;
1183                 case MARGIN_MANUAL:
1184                         os << "\tMargin Manual\n";
1185                         break;
1186                 case MARGIN_RIGHT_ADDRESS_BOX:
1187                         os << "\tMargin Right_Address_Box\n";
1188                         break;
1189                 case MARGIN_STATIC:
1190                         os << "\tMargin Static\n";
1191                         break;
1192         }
1193         switch (latextype) {
1194                 case LATEX_BIB_ENVIRONMENT:
1195                         os << "\tLatexType Bib_Environment\n";
1196                         break;
1197                 case LATEX_COMMAND:
1198                         os << "\tLatexType Command\n";
1199                         break;
1200                 case LATEX_ENVIRONMENT:
1201                         os << "\tLatexType Environment\n";
1202                         break;
1203                 case LATEX_ITEM_ENVIRONMENT:
1204                         os << "\tLatexType Item_Environment\n";
1205                         break;
1206                 case LATEX_LIST_ENVIRONMENT:
1207                         os << "\tLatexType List_Environment\n";
1208                         break;
1209                 case LATEX_PARAGRAPH:
1210                         os << "\tLatexType Paragraph\n";
1211                         break;
1212         }
1213         os << "\tInTitle " << intitle << "\n"
1214               "\tInPreamble " << inpreamble << "\n"
1215               "\tTocLevel " << toclevel << "\n"
1216               "\tResumeCounter " << resumecounter << "\n"
1217              "\tStepMasterCounter " << stepmastercounter << '\n';
1218         // ResetArgs does not make sense here
1219         for (LaTeXArgMap::const_iterator it = latexargs_.begin();
1220              it != latexargs_.end(); ++it)
1221                 writeArgument(os, it->first, it->second);
1222         for (LaTeXArgMap::const_iterator it = itemargs_.begin();
1223              it != itemargs_.end(); ++it)
1224                 writeArgument(os, it->first, it->second);
1225         for (LaTeXArgMap::const_iterator it = postcommandargs_.begin();
1226              it != postcommandargs_.end(); ++it)
1227                 writeArgument(os, it->first, it->second);
1228         for (LaTeXArgMap::const_iterator it = listpreamble_.begin();
1229              it != listpreamble_.end(); ++it)
1230                 writeArgument(os, it->first, it->second);
1231         os << "\tNeedProtect " << needprotect << "\n"
1232               "\tNeedCProtect " << needcprotect << "\n"
1233               "\tKeepEmpty " << keepempty << '\n';
1234         if (labelfont == font)
1235                 lyxWrite(os, font, "Font", 1);
1236         else {
1237                 lyxWrite(os, font, "TextFont", 1);
1238                 lyxWrite(os, labelfont, "LabelFont", 1);
1239         }
1240         os << "\tNextNoIndent " << nextnoindent << "\n"
1241               "\tCommandDepth " << commanddepth << '\n';
1242         if (!latexname_.empty())
1243                 os << "\tLatexName \"" << latexname_ << "\"\n";
1244         if (!latexparam_.empty())
1245                 os << "\tLatexParam \"" << subst(latexparam_, "\"", "&quot;")
1246                    << "\"\n";
1247         if (!leftdelim_.empty())
1248                 os << "\tLeftDelim "
1249                    << to_utf8(subst(leftdelim_, from_ascii("\n"), from_ascii("<br/>")))
1250                    << '\n';
1251         if (!rightdelim_.empty())
1252                 os << "\tRightDelim "
1253                    << to_utf8(subst(rightdelim_, from_ascii("\n"), from_ascii("<br/>")))
1254                    << '\n';
1255         if (!innertag_.empty())
1256                 os << "\tInnerTag \"" << innertag_ << "\"\n";
1257         if (!labeltag_.empty())
1258                 os << "\tLabelTag \"" << labeltag_ << "\"\n";
1259         if (!itemtag_.empty())
1260                 os << "\tItemTag \"" << itemtag_ << "\"\n";
1261         if (!itemcommand_.empty())
1262                 os << "\tItemCommand " << itemcommand_ << '\n';
1263         if (!preamble_.empty())
1264                 os << "\tPreamble\n\t"
1265                    << to_utf8(subst(rtrim(preamble_, "\n"),
1266                                     from_ascii("\n"), from_ascii("\n\t")))
1267                    << "\n\tEndPreamble\n";
1268         if (!langpreamble_.empty())
1269                 os << "\tLangPreamble\n\t"
1270                    << to_utf8(subst(rtrim(langpreamble_, "\n"),
1271                                     from_ascii("\n"), from_ascii("\n\t")))
1272                    << "\n\tEndLangPreamble\n";
1273         if (!babelpreamble_.empty())
1274                 os << "\tBabelPreamble\n\t"
1275                    << to_utf8(subst(rtrim(babelpreamble_, "\n"),
1276                                     from_ascii("\n"), from_ascii("\n\t")))
1277                    << "\n\tEndBabelPreamble\n";
1278         switch (labeltype) {
1279         case LABEL_ABOVE:
1280                 os << "\tLabelType Above\n";
1281                 break;
1282         case LABEL_BIBLIO:
1283                 os << "\tLabelType Bibliography\n";
1284                 break;
1285         case LABEL_CENTERED:
1286                 os << "\tLabelType Centered\n";
1287                 break;
1288         case LABEL_ENUMERATE:
1289                 os << "\tLabelType Enumerate\n";
1290                 break;
1291         case LABEL_ITEMIZE:
1292                 os << "\tLabelType Itemize\n";
1293                 break;
1294         case LABEL_MANUAL:
1295                 os << "\tLabelType Manual\n";
1296                 break;
1297         case LABEL_NO_LABEL:
1298                 os << "\tLabelType No_Label\n";
1299                 break;
1300         case LABEL_SENSITIVE:
1301                 os << "\tLabelType Sensitive\n";
1302                 break;
1303         case LABEL_STATIC:
1304                 os << "\tLabelType Static\n";
1305                 break;
1306         }
1307         switch (endlabeltype) {
1308         case END_LABEL_BOX:
1309                 os << "\tEndLabelType Box\n";
1310                 break;
1311         case END_LABEL_FILLED_BOX:
1312                 os << "\tEndLabelType Filled_Box\n";
1313                 break;
1314         case END_LABEL_NO_LABEL:
1315                 os << "\tEndLabelType No_Label\n";
1316                 break;
1317         case END_LABEL_STATIC:
1318                 os << "\tEndLabelType Static\n";
1319                 break;
1320         }
1321         os << "\tParagraphGroup \"" << par_group_ << "\"\n";
1322         if (!leftmargin.empty())
1323                 os << "\tLeftMargin \"" << to_utf8(leftmargin) << "\"\n";
1324         if (!rightmargin.empty())
1325                 os << "\tRightMargin \"" << to_utf8(rightmargin) << "\"\n";
1326         if (!labelindent.empty())
1327                 os << "\tLabelIndent " << to_utf8(labelindent) << '\n';
1328         if (!parindent.empty())
1329                 os << "\tParIndent " << to_utf8(parindent) << '\n';
1330         os << "\tParSkip " << parskip << "\n"
1331               "\tItemSep " << itemsep << "\n"
1332               "\tTopSep " << topsep << "\n"
1333               "\tBottomSep " << bottomsep << "\n"
1334               "\tLabelBottomSep " << labelbottomsep << '\n';
1335         if (!labelsep.empty())
1336                 os << "\tLabelSep " << to_utf8(subst(labelsep, ' ', 'x'))
1337                    << '\n';
1338         os << "\tParSep " << parsep << "\n"
1339               "\tNewLine " << newline_allowed << '\n';
1340         switch (align) {
1341         case LYX_ALIGN_BLOCK:
1342                 os << "\tAlign Block\n";
1343                 break;
1344         case LYX_ALIGN_CENTER:
1345                 os << "\tAlign Center\n";
1346                 break;
1347         case LYX_ALIGN_LAYOUT:
1348                 os << "\tAlign Layout\n";
1349                 break;
1350         case LYX_ALIGN_LEFT:
1351                 os << "\tAlign Left\n";
1352                 break;
1353         case LYX_ALIGN_RIGHT:
1354                 os << "\tAlign Right\n";
1355                 break;
1356         case LYX_ALIGN_DECIMAL:
1357         case LYX_ALIGN_SPECIAL:
1358         case LYX_ALIGN_NONE:
1359                 break;
1360         }
1361         if (alignpossible & (LYX_ALIGN_BLOCK | LYX_ALIGN_CENTER |
1362                              LYX_ALIGN_LAYOUT | LYX_ALIGN_LEFT | LYX_ALIGN_RIGHT)) {
1363                 bool first = true;
1364                 os << "\tAlignPossible";
1365                 if (alignpossible & LYX_ALIGN_BLOCK) {
1366                         os << " Block";
1367                         first = false;
1368                 }
1369                 if (alignpossible & LYX_ALIGN_CENTER) {
1370                         if (!first)
1371                                 os << ',';
1372                         os << " Center";
1373                         first = false;
1374                 }
1375                 if (alignpossible & LYX_ALIGN_LAYOUT) {
1376                         if (!first)
1377                                 os << ',';
1378                         os << " Layout";
1379                         first = false;
1380                 }
1381                 if (alignpossible & LYX_ALIGN_LEFT) {
1382                         if (!first)
1383                                 os << ',';
1384                         os << " Left";
1385                         first = false;
1386                 }
1387                 if (alignpossible & LYX_ALIGN_RIGHT) {
1388                         if (!first)
1389                                 os << ',';
1390                         os << " Right";
1391                         first = false;
1392                 }
1393                 os << '\n';
1394         }
1395         // LabelString must come before LabelStringAppendix
1396         if (!labelstring_.empty())
1397                 os << "\tLabelString \"" << to_utf8(labelstring_) << "\"\n";
1398         if (!endlabelstring_.empty())
1399                 os << "\tEndLabelString \"" << to_utf8(endlabelstring_) << "\"\n";
1400         if (!labelstring_appendix_.empty() && labelstring_appendix_ != labelstring_)
1401                 os << "\tLabelStringAppendix \""
1402                    << to_utf8(labelstring_appendix_) << "\"\n";
1403         if (!counter.empty())
1404                 os << "\tLabelCounter \"" << to_utf8(counter) << "\"\n";
1405         os << "\tFreeSpacing " << free_spacing << '\n';
1406         os << "\tPassThru " << pass_thru << '\n';
1407         if (!pass_thru_chars.empty())
1408                 os << "\tPassThruChars " << to_utf8(pass_thru_chars) << '\n';
1409         os << "\tParbreakIsNewline " << parbreak_is_newline << '\n';
1410         switch (spacing.getSpace()) {
1411         case Spacing::Double:
1412                 os << "\tSpacing Double\n";
1413                 break;
1414         case Spacing::Onehalf:
1415                 os << "\tSpacing Onehalf\n";
1416                 break;
1417         case Spacing::Other:
1418                 os << "\tSpacing Other " << spacing.getValueAsString() << '\n';
1419                 break;
1420         case Spacing::Single:
1421                 os << "\tSpacing Single\n";
1422                 break;
1423         case Spacing::Default:
1424                 break;
1425         }
1426         if (!requires_.empty()) {
1427                 os << "\tRequires ";
1428                 for (set<string>::const_iterator it = requires_.begin();
1429                      it != requires_.end(); ++it) {
1430                         if (it != requires_.begin())
1431                                 os << ',';
1432                         os << *it;
1433                 }
1434                 os << '\n';
1435         }
1436         if (!autonests_.empty()) {
1437                 os << "\tAutoNests\n\t";
1438                 for (set<docstring>::const_iterator it = autonests_.begin();
1439                      it != autonests_.end(); ++it) {
1440                         if (it != autonests_.begin())
1441                                 os << ',';
1442                         os << to_utf8(*it);
1443                 }
1444                 os << "\n\tEndAutoNests\n";
1445         }
1446         if (!autonested_by_.empty()) {
1447                 os << "\tIsAutoNestedBy\n\t\t";
1448                 for (set<docstring>::const_iterator it = autonested_by_.begin();
1449                      it != autonested_by_.end(); ++it) {
1450                         if (it != autonested_by_.begin())
1451                                 os << ',';
1452                         os << to_utf8(*it);
1453                 }
1454                 os << "\n\tEndIsAutoNestedBy\n";
1455         }
1456         if (refprefix.empty())
1457                 os << "\tRefPrefix OFF\n";
1458         else
1459                 os << "\tRefPrefix " << to_utf8(refprefix) << '\n';
1460         if (!htmltag_.empty())
1461                 os << "\tHTMLTag " << htmltag_ << '\n';
1462         if (!htmlattr_.empty())
1463                 os << "\tHTMLAttr " << htmlattr_ << '\n';
1464         if (!htmlitemtag_.empty())
1465                 os << "\tHTMLItem " << htmlitemtag_ << '\n';
1466         if (!htmlitemattr_.empty())
1467                 os << "\tHTMLItemAttr " << htmlitemattr_ << '\n';
1468         if (!htmllabeltag_.empty())
1469                 os << "\tHTMLLabel " << htmllabeltag_ << '\n';
1470         if (!htmllabelattr_.empty())
1471                 os << "\tHTMLLabelAttr " << htmllabelattr_ << '\n';
1472         os << "\tHTMLLabelFirst " << htmllabelfirst_ << '\n';
1473         if (!htmlstyle_.empty())
1474                 os << "\tHTMLStyle\n"
1475                    << to_utf8(rtrim(htmlstyle_, "\n"))
1476                    << "\n\tEndHTMLStyle\n";
1477         os << "\tHTMLForceCSS " << htmlforcecss_ << '\n';
1478         if (!htmlpreamble_.empty())
1479                 os << "\tHTMLPreamble\n"
1480                    << to_utf8(rtrim(htmlpreamble_, "\n"))
1481                    << "\n\tEndPreamble\n";
1482         os << "\tHTMLTitle " << htmltitle_ << "\n"
1483               "\tSpellcheck " << spellcheck << "\n"
1484               "\tForceLocal " << forcelocal << "\n"
1485               "End\n";
1486 }
1487
1488
1489 bool Layout::hasArgs() const
1490 {
1491         return !latexargs_.empty() || !postcommandargs_.empty() ||
1492                 !itemargs_.empty() || !listpreamble_.empty();
1493 }
1494
1495
1496 Layout::LaTeXArgMap Layout::args() const
1497 {
1498         LaTeXArgMap args = latexargs_;
1499         if (!postcommandargs_.empty())
1500                 args.insert(postcommandargs_.begin(), postcommandargs_.end());
1501         if (!listpreamble_.empty())
1502                 args.insert(listpreamble_.begin(), listpreamble_.end());
1503         if (!itemargs_.empty())
1504                 args.insert(itemargs_.begin(), itemargs_.end());
1505         return args;
1506 }
1507
1508
1509 int Layout::optArgs() const
1510 {
1511         int nr = 0;
1512         LaTeXArgMap::const_iterator it = latexargs_.begin();
1513         for (; it != latexargs_.end(); ++it) {
1514                 if (!(*it).second.mandatory)
1515                         ++nr;
1516         }
1517         LaTeXArgMap::const_iterator iit = postcommandargs_.begin();
1518         for (; iit != postcommandargs_.end(); ++iit) {
1519                 if (!(*iit).second.mandatory)
1520                         ++nr;
1521         }
1522         return nr;
1523 }
1524
1525
1526 int Layout::requiredArgs() const
1527 {
1528         int nr = 0;
1529         LaTeXArgMap::const_iterator it = latexargs_.begin();
1530         for (; it != latexargs_.end(); ++it) {
1531                 if ((*it).second.mandatory)
1532                         ++nr;
1533         }
1534         LaTeXArgMap::const_iterator iit = postcommandargs_.begin();
1535         for (; iit != postcommandargs_.end(); ++iit) {
1536                 if (!(*iit).second.mandatory)
1537                         ++nr;
1538         }
1539         return nr;
1540 }
1541
1542
1543 string const & Layout::htmltag() const
1544 {
1545         if (htmltag_.empty())
1546                 htmltag_ =  "div";
1547         return htmltag_;
1548 }
1549
1550
1551 string const & Layout::htmlattr() const
1552 {
1553         if (htmlattr_.empty())
1554                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
1555         return htmlattr_;
1556 }
1557
1558
1559 string const & Layout::htmlitemtag() const
1560 {
1561         if (htmlitemtag_.empty())
1562                 htmlitemtag_ = "div";
1563         return htmlitemtag_;
1564 }
1565
1566
1567 string const & Layout::htmlitemattr() const
1568 {
1569         if (htmlitemattr_.empty())
1570                 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
1571         return htmlitemattr_;
1572 }
1573
1574
1575 string const & Layout::htmllabeltag() const
1576 {
1577         if (htmllabeltag_.empty())
1578                         htmllabeltag_ = "span";
1579         return htmllabeltag_;
1580 }
1581
1582
1583 string const & Layout::htmllabelattr() const
1584 {
1585         if (htmllabelattr_.empty())
1586                 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
1587         return htmllabelattr_;
1588 }
1589
1590
1591 docstring Layout::htmlstyle() const
1592 {
1593         if (!htmlstyle_.empty() && !htmlforcecss_)
1594                 return htmlstyle_;
1595         if (htmldefaultstyle_.empty())
1596                 makeDefaultCSS();
1597         docstring retval = htmldefaultstyle_;
1598         if (!htmlstyle_.empty())
1599                 retval += '\n' + htmlstyle_;
1600         return retval;
1601 }
1602
1603
1604 string Layout::defaultCSSClass() const
1605 {
1606         if (!defaultcssclass_.empty())
1607                 return defaultcssclass_;
1608         docstring d;
1609         docstring::const_iterator it = name().begin();
1610         docstring::const_iterator en = name().end();
1611         for (; it != en; ++it) {
1612                 char_type const c = *it;
1613                 if (!isAlphaASCII(c)) {
1614                         if (d.empty())
1615                                 // make sure we don't start with an underscore,
1616                                 // as that sometimes causes problems.
1617                                 d = from_ascii("lyx_");
1618                         else
1619                                 d += '_';
1620                 } else if (isLower(c))
1621                         d += c;
1622                 else
1623                         // this is slow, so do it only if necessary
1624                         d += lowercase(c);
1625         }
1626         defaultcssclass_ = to_utf8(d);
1627         return defaultcssclass_;
1628 }
1629
1630
1631 namespace {
1632
1633 string makeMarginValue(char const * side, double d)
1634 {
1635         ostringstream os;
1636         os << "margin-" << side << ": " << d << "ex;\n";
1637         return os.str();
1638 }
1639
1640 } // namespace
1641
1642
1643 void Layout::makeDefaultCSS() const
1644 {
1645         // this never needs to be redone, since reloading layouts will
1646         // wipe out what we did before.
1647         if (!htmldefaultstyle_.empty())
1648                 return;
1649
1650         // main font
1651         htmldefaultstyle_ = font.asCSS();
1652
1653         // bottom margins
1654         string tmp;
1655         if (topsep > 0)
1656                 tmp += makeMarginValue("top", topsep);
1657         if (bottomsep > 0)
1658                 tmp += makeMarginValue("bottom", bottomsep);
1659         if (!leftmargin.empty()) {
1660                 // we can't really do what LyX does with the margin, so
1661                 // we'll just figure out how many characters it is
1662                 int const len = leftmargin.length();
1663                 tmp += makeMarginValue("left", len);
1664         }
1665         if (!rightmargin.empty()) {
1666                 int const len = rightmargin.length();
1667                 tmp += makeMarginValue("right", len);
1668         }
1669
1670         if (!tmp.empty()) {
1671                 if (!htmldefaultstyle_.empty())
1672                         htmldefaultstyle_ += from_ascii("\n");
1673                 htmldefaultstyle_ += from_ascii(tmp);
1674         }
1675
1676         // alignment
1677         string where = alignmentToCSS(align);
1678         if (!where.empty()) {
1679                 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1680         }
1681
1682         // wrap up what we have, if anything
1683         if (!htmldefaultstyle_.empty())
1684                 htmldefaultstyle_ =
1685                         from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1686                         htmldefaultstyle_ + from_ascii("\n}\n");
1687
1688         if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1689                 return;
1690
1691         docstring labelCSS;
1692
1693         // label font
1694         if (labelfont != font)
1695                 labelCSS = labelfont.asCSS() + from_ascii("\n");
1696         if (labeltype == LABEL_CENTERED)
1697                 labelCSS += from_ascii("text-align: center;\n");
1698
1699         if (!labelCSS.empty())
1700                 htmldefaultstyle_ +=
1701                         from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1702                         labelCSS + from_ascii("\n}\n");
1703 }
1704
1705
1706 bool Layout::operator==(Layout const & rhs) const
1707 {
1708         // This is enough for the applications we actually make,
1709         // at least at the moment. But we could check more.
1710         return name() == rhs.name()
1711                 && latexname() == rhs.latexname()
1712                 && latextype == rhs.latextype;
1713 }
1714
1715
1716 } // namespace lyx