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