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