]> git.lyx.org Git - lyx.git/blob - src/Layout.cpp
simplify Lexer handling a bit more
[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 "TextClass.h"
17 #include "Lexer.h"
18 #include "Font.h"
19
20 #include "support/debug.h"
21 #include "support/lstrings.h"
22
23 #include <ostream>
24
25 using namespace std;
26 using namespace lyx::support;
27
28 namespace lyx {
29
30 /// Special value of toclevel for layouts that to not belong in a TOC
31 const int Layout::NOT_IN_TOC = -1000;
32
33 //  The order of the LayoutTags enum is no more important. [asierra300396]
34 // Tags indexes.
35 enum LayoutTags {
36         LT_ALIGN = 1,
37         LT_ALIGNPOSSIBLE,
38         LT_MARGIN,
39         LT_BOTTOMSEP,
40         LT_CATEGORY,
41         LT_COMMANDDEPTH,
42         LT_COPYSTYLE,
43         LT_DEPENDSON,
44         LT_OBSOLETEDBY,
45         //LT_EMPTY,
46         LT_END,
47         //LT_ENVIRONMENT_DEFAULT,
48         //LT_FANCYHDR,
49         LT_FILL_BOTTOM,
50         LT_FILL_TOP,
51         //LT_FIRST_COUNTER,
52         LT_FONT,
53         LT_FREE_SPACING,
54         LT_PASS_THRU,
55         //LT_HEADINGS,
56         LT_ITEMSEP,
57         LT_KEEPEMPTY,
58         LT_LABEL_BOTTOMSEP,
59         LT_LABELFONT,
60         LT_TEXTFONT,
61         LT_LABELINDENT,
62         LT_LABELSEP,
63         LT_LABELSTRING,
64         LT_LABELSTRING_APPENDIX,
65         LT_LABELCOUNTER,
66         LT_LABELTYPE,
67         LT_ENDLABELSTRING,
68         LT_ENDLABELTYPE,
69         LT_LATEXNAME,
70         LT_LATEXPARAM,
71         LT_OPTARGS,
72         LT_LATEXTYPE,
73         LT_LATEXHEADER,
74         LT_LATEXFOOTER,
75         LT_LATEXPARAGRAPH,
76         LT_LEFTMARGIN,
77         LT_NEED_PROTECT,
78         LT_NEWLINE,
79         LT_NEXTNOINDENT,
80         LT_PARINDENT,
81         LT_PARSEP,
82         LT_PARSKIP,
83         //LT_PLAIN,
84         LT_PREAMBLE,
85         LT_REQUIRES,
86         LT_RIGHTMARGIN,
87         LT_SPACING,
88         LT_TOPSEP,
89         LT_TOCLEVEL,
90         LT_INNERTAG,
91         LT_LABELTAG,
92         LT_ITEMTAG,
93         LT_INTITLE // keep this last!
94 };
95
96 /////////////////////
97
98 Layout::Layout()
99 {
100         margintype = MARGIN_STATIC;
101         latextype = LATEX_PARAGRAPH;
102         intitle = false;
103         optionalargs = 0;
104         needprotect = false;
105         keepempty = false;
106         font = inherit_font;
107         labelfont = inherit_font;
108         resfont = sane_font;
109         reslabelfont = sane_font;
110         nextnoindent = false;
111         parskip = 0.0;
112         itemsep = 0;
113         topsep = 0.0;
114         bottomsep = 0.0;
115         labelbottomsep = 0.0;
116         parsep = 0;
117         align = LYX_ALIGN_BLOCK;
118         alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
119         labeltype = LABEL_NO_LABEL;
120         endlabeltype = END_LABEL_NO_LABEL;
121         // Should or should not. That is the question.
122         // spacing.set(Spacing::OneHalf);
123         fill_top = false;
124         fill_bottom = false;
125         newline_allowed = true;
126         free_spacing = false;
127         pass_thru = false;
128         is_environment = false;
129         toclevel = NOT_IN_TOC;
130         commanddepth = 0;
131 }
132
133
134 bool Layout::read(Lexer & lexrc, TextClass const & tclass)
135 {
136         // This table is sorted alphabetically [asierra 30March96]
137         LexerKeyword layoutTags[] = {
138                 { "align",          LT_ALIGN },
139                 { "alignpossible",  LT_ALIGNPOSSIBLE },
140                 { "bottomsep",      LT_BOTTOMSEP },
141                 { "category",       LT_CATEGORY },
142                 { "commanddepth",   LT_COMMANDDEPTH },
143                 { "copystyle",      LT_COPYSTYLE },
144                 { "dependson",      LT_DEPENDSON },
145                 { "end",            LT_END },
146                 { "endlabelstring", LT_ENDLABELSTRING },
147                 { "endlabeltype",   LT_ENDLABELTYPE },
148                 { "fill_bottom",    LT_FILL_BOTTOM },
149                 { "fill_top",       LT_FILL_TOP },
150                 { "font",           LT_FONT },
151                 { "freespacing",    LT_FREE_SPACING },
152                 { "innertag",       LT_INNERTAG },
153                 { "intitle",        LT_INTITLE },
154                 { "itemsep",        LT_ITEMSEP },
155                 { "itemtag",        LT_ITEMTAG },
156                 { "keepempty",      LT_KEEPEMPTY },
157                 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
158                 { "labelcounter",   LT_LABELCOUNTER },
159                 { "labelfont",      LT_LABELFONT },
160                 { "labelindent",    LT_LABELINDENT },
161                 { "labelsep",       LT_LABELSEP },
162                 { "labelstring",    LT_LABELSTRING },
163                 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
164                 { "labeltag",       LT_LABELTAG },
165                 { "labeltype",      LT_LABELTYPE },
166                 { "latexfooter",    LT_LATEXFOOTER },
167                 { "latexheader",    LT_LATEXHEADER },
168                 { "latexname",      LT_LATEXNAME },
169                 { "latexparagraph", LT_LATEXPARAGRAPH },
170                 { "latexparam",     LT_LATEXPARAM },
171                 { "latextype",      LT_LATEXTYPE },
172                 { "leftmargin",     LT_LEFTMARGIN },
173                 { "margin",         LT_MARGIN },
174                 { "needprotect",    LT_NEED_PROTECT },
175                 { "newline",        LT_NEWLINE },
176                 { "nextnoindent",   LT_NEXTNOINDENT },
177                 { "obsoletedby",    LT_OBSOLETEDBY },
178                 { "optionalargs",   LT_OPTARGS },
179                 { "parindent",      LT_PARINDENT },
180                 { "parsep",         LT_PARSEP },
181                 { "parskip",        LT_PARSKIP },
182                 { "passthru",       LT_PASS_THRU },
183                 { "preamble",       LT_PREAMBLE },
184                 { "requires",       LT_REQUIRES },
185                 { "rightmargin",    LT_RIGHTMARGIN },
186                 { "spacing",        LT_SPACING },
187                 { "textfont",       LT_TEXTFONT },
188                 { "toclevel",       LT_TOCLEVEL },
189                 { "topsep",         LT_TOPSEP }
190         };
191
192         bool error = false;
193         bool finished = false;
194         lexrc.pushTable(layoutTags);
195         // parse style section
196         while (!finished && lexrc.isOK() && !error) {
197                 int le = lexrc.lex();
198                 // See comment in LyXRC.cpp.
199                 switch (le) {
200                 case Lexer::LEX_FEOF:
201                         continue;
202
203                 case Lexer::LEX_UNDEF:          // parse error
204                         lexrc.printError("Unknown layout tag `$$Token'");
205                         error = true;
206                         continue;
207                 default: break;
208                 }
209                 switch (static_cast<LayoutTags>(le)) {
210                 case LT_END:            // end of structure
211                         finished = true;
212                         break;
213
214                 case LT_CATEGORY:
215                         if (lexrc.next())
216                                 category_ = lexrc.getDocString();
217                         break;
218
219                 case LT_COPYSTYLE:     // initialize with a known style
220                         if (lexrc.next()) {
221                                 docstring const style = subst(lexrc.getDocString(),
222                                                                 '_', ' ');
223
224                                 if (tclass.hasLayout(style)) {
225                                         docstring const tmpname = name_;
226                                         this->operator=(tclass[style]);
227                                         name_ = tmpname;
228                                 } else {
229                                         lyxerr << "Cannot copy unknown style `"
230                                                << to_utf8(style) << "'\n"
231                                                << "All layouts so far:"
232                                                << endl;
233                                         DocumentClass::const_iterator lit = tclass.begin();
234                                         DocumentClass::const_iterator len = tclass.end();
235                                         for (; lit != len; ++lit)
236                                                 lyxerr << to_utf8(lit->name()) << endl;
237
238                                         //lexrc.printError("Cannot copy known "
239                                         //               "style `$$Token'");
240                                 }
241                         }
242                         break;
243
244                 case LT_OBSOLETEDBY:     // replace with a known style
245                         if (lexrc.next()) {
246                                 docstring const style = 
247                                         subst(lexrc.getDocString(), '_', ' ');
248
249                                 if (tclass.hasLayout(style)) {
250                                         docstring const tmpname = name_;
251                                         this->operator=(tclass[style]);
252                                         name_ = tmpname;
253                                         if (obsoleted_by().empty())
254                                                 obsoleted_by_ = style;
255                                 } else {
256                                         lyxerr << "Cannot replace with unknown style `" 
257                                                 << to_utf8(style) << '\'' << endl;
258
259                                         //lexrc.printError("Cannot replace with"
260                                         //               " unknown style "
261                                         //               "`$$Token'");
262                                 }
263                         }
264                         break;
265
266                 case LT_DEPENDSON:
267                         if (lexrc.next())
268                         depends_on_ = subst(lexrc.getDocString(), '_', ' ');
269                         break;
270
271                 case LT_MARGIN:         // margin style definition.
272                         readMargin(lexrc);
273                         break;
274
275                 case LT_LATEXTYPE:      // LaTeX style definition.
276                         readLatexType(lexrc);
277                         break;
278
279                 case LT_LATEXHEADER:    // header for environments
280                         lexrc.next();
281                         latexheader = lexrc.getString();
282                         break;
283
284                 case LT_LATEXFOOTER:    // footer for environments
285                         lexrc.next();
286                         latexfooter = lexrc.getString();
287                         break;
288
289                 case LT_LATEXPARAGRAPH:
290                         lexrc.next();
291                         latexparagraph = lexrc.getString();
292                         break;
293
294                 case LT_INTITLE:
295                         intitle = lexrc.next() && lexrc.getInteger();
296                         break;
297
298                 case LT_TOCLEVEL:
299                         lexrc.next();
300                         toclevel = lexrc.getInteger();
301                         break;
302
303                 case LT_OPTARGS:
304                         if (lexrc.next())
305                                 optionalargs = lexrc.getInteger();
306                         break;
307
308                 case LT_NEED_PROTECT:
309                         needprotect = lexrc.next() && lexrc.getInteger();
310                         break;
311
312                 case LT_KEEPEMPTY:
313                         keepempty = lexrc.next() && lexrc.getInteger();
314                         break;
315
316                 case LT_FONT:
317                         font = lyxRead(lexrc, font);
318                         labelfont = font;
319                         break;
320
321                 case LT_TEXTFONT:
322                         font = lyxRead(lexrc, font);
323                         break;
324
325                 case LT_LABELFONT:
326                         labelfont = lyxRead(lexrc, labelfont);
327                         break;
328
329                 case LT_NEXTNOINDENT:   // Indent next paragraph?
330                         if (lexrc.next() && lexrc.getInteger())
331                                 nextnoindent = true;
332                         else
333                                 nextnoindent = false;
334                         break;
335
336                 case LT_COMMANDDEPTH:
337                         lexrc.next();
338                         commanddepth = lexrc.getInteger();
339                         break;
340
341                 case LT_LATEXNAME:
342                         if (lexrc.next())
343                                 latexname_ = lexrc.getString();
344                         break;
345
346                 case LT_LATEXPARAM:
347                         if (lexrc.next())
348                                 latexparam_ = subst(lexrc.getString(), "&quot;", "\"");
349                         break;
350
351                 case LT_INNERTAG:
352                         if (lexrc.next())
353                                 innertag_ = lexrc.getString();
354                         break;
355
356                 case LT_LABELTAG:
357                         if (lexrc.next())
358                                 labeltag_ = lexrc.getString();
359                         break;
360
361                 case LT_ITEMTAG:
362                         if (lexrc.next())
363                                 itemtag_ = lexrc.getString();
364                         break;
365
366                 case LT_PREAMBLE:
367                         preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
368                         break;
369
370                 case LT_LABELTYPE:
371                         readLabelType(lexrc);
372                         break;
373
374                 case LT_ENDLABELTYPE:
375                         readEndLabelType(lexrc);
376                         break;
377
378                 case LT_LEFTMARGIN:     // left margin type
379                         if (lexrc.next())
380                                 leftmargin = lexrc.getDocString();
381                         break;
382
383                 case LT_RIGHTMARGIN:    // right margin type
384                         if (lexrc.next())
385                                 rightmargin = lexrc.getDocString();
386                         break;
387
388                 case LT_LABELINDENT:    // label indenting flag
389                         if (lexrc.next())
390                                 labelindent = lexrc.getDocString();
391                         break;
392
393                 case LT_PARINDENT:      // paragraph indent. flag
394                         if (lexrc.next())
395                                 parindent = lexrc.getDocString();
396                         break;
397
398                 case LT_PARSKIP:        // paragraph skip size
399                         if (lexrc.next())
400                                 parskip = lexrc.getFloat();
401                         break;
402
403                 case LT_ITEMSEP:        // item separation size
404                         if (lexrc.next())
405                                 itemsep = lexrc.getFloat();
406                         break;
407
408                 case LT_TOPSEP:         // top separation size
409                         if (lexrc.next())
410                                 topsep = lexrc.getFloat();
411                         break;
412
413                 case LT_BOTTOMSEP:      // bottom separation size
414                         if (lexrc.next())
415                                 bottomsep = lexrc.getFloat();
416                         break;
417
418                 case LT_LABEL_BOTTOMSEP: // label bottom separation size
419                         if (lexrc.next())
420                                 labelbottomsep = lexrc.getFloat();
421                         break;
422
423                 case LT_LABELSEP:       // label separator
424                         if (lexrc.next()) {
425                                 labelsep = from_utf8(subst(lexrc.getString(), 'x', ' '));
426                         }
427                         break;
428
429                 case LT_PARSEP:         // par. separation size
430                         if (lexrc.next())
431                                 parsep = lexrc.getFloat();
432                         break;
433
434                 case LT_FILL_TOP:       // fill top flag
435                         if (lexrc.next())
436                                 fill_top = lexrc.getInteger();
437                         break;
438
439                 case LT_FILL_BOTTOM:    // fill bottom flag
440                         if (lexrc.next())
441                                 fill_bottom = lexrc.getInteger();
442                         break;
443
444                 case LT_NEWLINE:        // newlines allowed?
445                         if (lexrc.next())
446                                 newline_allowed = lexrc.getInteger();
447                         break;
448
449                 case LT_ALIGN:          // paragraph align
450                         readAlign(lexrc);
451                         break;
452                 case LT_ALIGNPOSSIBLE:  // paragraph allowed align
453                         readAlignPossible(lexrc);
454                         break;
455
456                 case LT_LABELSTRING:    // label string definition
457                         if (lexrc.next()) {
458                                 labelstring_ = trim(lexrc.getDocString());
459                                 labelstring_appendix_ = labelstring_;
460                         }
461                         break;
462
463                 case LT_ENDLABELSTRING: // endlabel string definition
464                         if (lexrc.next())
465                                 endlabelstring_ = trim(lexrc.getDocString());
466                         break;
467
468                 case LT_LABELSTRING_APPENDIX: // label string appendix definition
469                         if (lexrc.next())
470                                 labelstring_appendix_ = trim(lexrc.getDocString());
471                         break;
472
473                 case LT_LABELCOUNTER: // name of counter to use
474                         if (lexrc.next())
475                                 counter = lyx::from_ascii(trim(lexrc.getString()));
476                         break;
477
478                 case LT_FREE_SPACING:   // Allow for free spacing.
479                         if (lexrc.next())
480                                 free_spacing = lexrc.getInteger();
481                         break;
482
483                 case LT_PASS_THRU:      // Allow for pass thru.
484                         if (lexrc.next())
485                                 pass_thru = lexrc.getInteger();
486                         break;
487
488                 case LT_SPACING: // setspace.sty
489                         readSpacing(lexrc);
490                         break;
491
492                 case LT_REQUIRES:
493                         lexrc.eatLine();
494                         vector<string> const req = 
495                                 getVectorFromString(lexrc.getString());
496                         requires_.insert(req.begin(), req.end());
497                         break;
498
499                 }
500         }
501         lexrc.popTable();
502
503         return !error;
504 }
505
506
507 enum {
508         AT_BLOCK = 1,
509         AT_LEFT,
510         AT_RIGHT,
511         AT_CENTER,
512         AT_LAYOUT
513 };
514
515 void Layout::readAlign(Lexer & lexrc)
516 {
517         LexerKeyword alignTags[] = {
518                 { "block",  AT_BLOCK },
519                 { "center", AT_CENTER },
520                 { "layout", AT_LAYOUT },
521                 { "left",   AT_LEFT },
522                 { "right",  AT_RIGHT }
523         };
524
525         PushPopHelper pph(lexrc, alignTags);
526         int le = lexrc.lex();
527         switch (le) {
528         case Lexer::LEX_UNDEF:
529                 lexrc.printError("Unknown alignment `$$Token'");
530                 return;
531         default: break;
532         };
533         switch (le) {
534         case AT_BLOCK:
535                 align = LYX_ALIGN_BLOCK;
536                 break;
537         case AT_LEFT:
538                 align = LYX_ALIGN_LEFT;
539                 break;
540         case AT_RIGHT:
541                 align = LYX_ALIGN_RIGHT;
542                 break;
543         case AT_CENTER:
544                 align = LYX_ALIGN_CENTER;
545                 break;
546         case AT_LAYOUT:
547                 align = LYX_ALIGN_LAYOUT;
548                 break;
549         }
550 }
551
552
553 void Layout::readAlignPossible(Lexer & lexrc)
554 {
555         LexerKeyword alignTags[] = {
556                 { "block",  AT_BLOCK },
557                 { "center", AT_CENTER },
558                 { "layout", AT_LAYOUT },
559                 { "left",   AT_LEFT },
560                 { "right",  AT_RIGHT }
561         };
562
563         lexrc.pushTable(alignTags);
564         alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
565         int lineno = lexrc.lineNumber();
566         do {
567                 int le = lexrc.lex();
568                 switch (le) {
569                 case Lexer::LEX_UNDEF:
570                         lexrc.printError("Unknown alignment `$$Token'");
571                         continue;
572                 default: break;
573                 };
574                 switch (le) {
575                 case AT_BLOCK:
576                         alignpossible |= LYX_ALIGN_BLOCK;
577                         break;
578                 case AT_LEFT:
579                         alignpossible |= LYX_ALIGN_LEFT;
580                         break;
581                 case AT_RIGHT:
582                         alignpossible |= LYX_ALIGN_RIGHT;
583                         break;
584                 case AT_CENTER:
585                         alignpossible |= LYX_ALIGN_CENTER;
586                         break;
587                 case AT_LAYOUT:
588                         alignpossible |= LYX_ALIGN_LAYOUT;
589                         break;
590                 }
591         } while (lineno == lexrc.lineNumber());
592         lexrc.popTable();
593 }
594
595
596 void Layout::readLabelType(Lexer & lexrc)
597 {
598         enum {
599                 LA_NO_LABEL = 1,
600                 LA_MANUAL,
601                 LA_TOP_ENVIRONMENT,
602                 LA_CENTERED_TOP_ENVIRONMENT,
603                 LA_STATIC,
604                 LA_SENSITIVE,
605                 LA_COUNTER,
606                 LA_ENUMERATE,
607                 LA_ITEMIZE,
608                 LA_BIBLIO
609         };
610
611
612         LexerKeyword labelTypeTags[] = {
613                 { "bibliography",             LA_BIBLIO },
614                 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
615                 { "counter",                  LA_COUNTER },
616                 { "enumerate",                LA_ENUMERATE },
617                 { "itemize",                  LA_ITEMIZE },
618                 { "manual",                   LA_MANUAL },
619                 { "no_label",                 LA_NO_LABEL },
620                 { "sensitive",                LA_SENSITIVE },
621                 { "static",                   LA_STATIC },
622                 { "top_environment",          LA_TOP_ENVIRONMENT }
623         };
624
625         PushPopHelper pph(lexrc, labelTypeTags);
626         int le = lexrc.lex();
627         switch (le) {
628         case Lexer::LEX_UNDEF:
629                 lexrc.printError("Unknown labeltype tag `$$Token'");
630                 return;
631         default: break;
632         }
633         switch (le) {
634         case LA_NO_LABEL:
635                 labeltype = LABEL_NO_LABEL;
636                 break;
637         case LA_MANUAL:
638                 labeltype = LABEL_MANUAL;
639                 break;
640         case LA_TOP_ENVIRONMENT:
641                 labeltype = LABEL_TOP_ENVIRONMENT;
642                 break;
643         case LA_CENTERED_TOP_ENVIRONMENT:
644                 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
645                 break;
646         case LA_STATIC:
647                 labeltype = LABEL_STATIC;
648                 break;
649         case LA_SENSITIVE:
650                 labeltype = LABEL_SENSITIVE;
651                 break;
652         case LA_COUNTER:
653                 labeltype = LABEL_COUNTER;
654                 break;
655         case LA_ENUMERATE:
656                 labeltype = LABEL_ENUMERATE;
657                 break;
658         case LA_ITEMIZE:
659                 labeltype = LABEL_ITEMIZE;
660                 break;
661         case LA_BIBLIO:
662                 labeltype = LABEL_BIBLIO;
663                 break;
664         }
665 }
666
667
668 void Layout::readEndLabelType(Lexer & lexrc)
669 {
670         static LexerKeyword endlabelTypeTags[] = {
671                 { "box",              END_LABEL_BOX },
672                 { "filled_box", END_LABEL_FILLED_BOX },
673                 { "no_label",     END_LABEL_NO_LABEL },
674                 { "static",     END_LABEL_STATIC }
675         };
676
677         PushPopHelper pph(lexrc, endlabelTypeTags);
678         int le = lexrc.lex();
679         switch (le) {
680         case Lexer::LEX_UNDEF:
681                 lexrc.printError("Unknown labeltype tag `$$Token'");
682                 break;
683         case END_LABEL_STATIC:
684         case END_LABEL_BOX:
685         case END_LABEL_FILLED_BOX:
686         case END_LABEL_NO_LABEL:
687                 endlabeltype = static_cast<EndLabelType>(le);
688                 break;
689         default:
690                 lyxerr << "Unhandled value " << le
691                        << " in Layout::readEndLabelType." << endl;
692                 break;
693         }
694 }
695
696
697 void Layout::readMargin(Lexer & lexrc)
698 {
699         LexerKeyword marginTags[] = {
700                 { "dynamic",           MARGIN_DYNAMIC },
701                 { "first_dynamic",     MARGIN_FIRST_DYNAMIC },
702                 { "manual",            MARGIN_MANUAL },
703                 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
704                 { "static",            MARGIN_STATIC }
705         };
706
707         PushPopHelper pph(lexrc, marginTags);
708
709         int le = lexrc.lex();
710         switch (le) {
711         case Lexer::LEX_UNDEF:
712                 lexrc.printError("Unknown margin type tag `$$Token'");
713                 return;
714         case MARGIN_STATIC:
715         case MARGIN_MANUAL:
716         case MARGIN_DYNAMIC:
717         case MARGIN_FIRST_DYNAMIC:
718         case MARGIN_RIGHT_ADDRESS_BOX:
719                 margintype = static_cast<MarginType>(le);
720                 break;
721         default:
722                 lyxerr << "Unhandled value " << le
723                        << " in Layout::readMargin." << endl;
724                 break;
725         }
726 }
727
728
729 void Layout::readLatexType(Lexer & lexrc)
730 {
731         LexerKeyword latexTypeTags[] = {
732                 { "bib_environment",  LATEX_BIB_ENVIRONMENT },
733                 { "command",          LATEX_COMMAND },
734                 { "environment",      LATEX_ENVIRONMENT },
735                 { "item_environment", LATEX_ITEM_ENVIRONMENT },
736                 { "list_environment", LATEX_LIST_ENVIRONMENT },
737                 { "paragraph",        LATEX_PARAGRAPH }
738         };
739
740         PushPopHelper pph(lexrc, latexTypeTags);
741         int le = lexrc.lex();
742         switch (le) {
743         case Lexer::LEX_UNDEF:
744                 lexrc.printError("Unknown latextype tag `$$Token'");
745                 return;
746         case LATEX_PARAGRAPH:
747         case LATEX_COMMAND:
748         case LATEX_ENVIRONMENT:
749         case LATEX_ITEM_ENVIRONMENT:
750         case LATEX_BIB_ENVIRONMENT:
751         case LATEX_LIST_ENVIRONMENT:
752                 latextype = static_cast<LatexType>(le);
753                 break;
754         default:
755                 lyxerr << "Unhandled value " << le
756                        << " in Layout::readLatexType." << endl;
757                 break;
758         }
759 }
760
761
762 void Layout::readSpacing(Lexer & lexrc)
763 {
764         enum {
765                 ST_SPACING_SINGLE = 1,
766                 ST_SPACING_ONEHALF,
767                 ST_SPACING_DOUBLE,
768                 ST_OTHER
769         };
770
771         LexerKeyword spacingTags[] = {
772                 {"double",  ST_SPACING_DOUBLE },
773                 {"onehalf", ST_SPACING_ONEHALF },
774                 {"other",   ST_OTHER },
775                 {"single",  ST_SPACING_SINGLE }
776         };
777
778         PushPopHelper pph(lexrc, spacingTags);
779         int le = lexrc.lex();
780         switch (le) {
781         case Lexer::LEX_UNDEF:
782                 lexrc.printError("Unknown spacing token `$$Token'");
783                 return;
784         default: break;
785         }
786         switch (le) {
787         case ST_SPACING_SINGLE:
788                 spacing.set(Spacing::Single);
789                 break;
790         case ST_SPACING_ONEHALF:
791                 spacing.set(Spacing::Onehalf);
792                 break;
793         case ST_SPACING_DOUBLE:
794                 spacing.set(Spacing::Double);
795                 break;
796         case ST_OTHER:
797                 lexrc.next();
798                 spacing.set(Spacing::Other, lexrc.getString());
799                 break;
800         }
801 }
802
803
804 docstring const & Layout::name() const
805 {
806         return name_;
807 }
808
809
810 void Layout::setName(docstring const & name)
811 {
812         name_ = name;
813 }
814
815
816 docstring const & Layout::obsoleted_by() const
817 {
818         return obsoleted_by_;
819 }
820
821
822 docstring const & Layout::depends_on() const
823 {
824         return depends_on_;
825 }
826
827
828 bool Layout::operator==(Layout const & rhs) const
829 {
830         // This is enough for the applications we actually make,
831         // at least at the moment. But we could check more.
832         return name() == rhs.name()
833                 && latexname() == rhs.latexname()
834                 && latextype == rhs.latextype;
835 }
836
837
838 Layout * Layout::forCaption()
839 {
840         Layout * lay = new Layout();
841         lay->name_ = from_ascii("Caption");
842         lay->latexname_ = "caption";
843         lay->latextype = LATEX_COMMAND;
844         lay->optionalargs = 1;
845         return lay;
846 }
847
848 } // namespace lyx