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