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