3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
16 #include "TextClass.h"
20 #include "support/debug.h"
21 #include "support/lstrings.h"
24 using namespace lyx::support;
28 /// Special value of toclevel for layouts that to not belong in a TOC
29 const int Layout::NOT_IN_TOC = -1000;
31 // The order of the LayoutTags enum is no more important. [asierra300396]
45 //LT_ENVIRONMENT_DEFAULT,
62 LT_LABELSTRING_APPENDIX,
91 LT_INTITLE // keep this last!
98 margintype = MARGIN_STATIC;
99 latextype = LATEX_PARAGRAPH;
105 labelfont = inherit_font;
107 reslabelfont = sane_font;
108 nextnoindent = false;
113 labelbottomsep = 0.0;
115 align = LYX_ALIGN_BLOCK;
116 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
117 labeltype = LABEL_NO_LABEL;
118 endlabeltype = END_LABEL_NO_LABEL;
119 // Should or should not. That is the question.
120 // spacing.set(Spacing::OneHalf);
123 newline_allowed = true;
124 free_spacing = false;
126 is_environment = false;
127 toclevel = NOT_IN_TOC;
132 bool Layout::read(Lexer & lex, TextClass const & tclass)
134 // This table is sorted alphabetically [asierra 30March96]
135 LexerKeyword layoutTags[] = {
136 { "align", LT_ALIGN },
137 { "alignpossible", LT_ALIGNPOSSIBLE },
138 { "bottomsep", LT_BOTTOMSEP },
139 { "category", LT_CATEGORY },
140 { "commanddepth", LT_COMMANDDEPTH },
141 { "copystyle", LT_COPYSTYLE },
142 { "dependson", LT_DEPENDSON },
144 { "endlabelstring", LT_ENDLABELSTRING },
145 { "endlabeltype", LT_ENDLABELTYPE },
146 { "fill_bottom", LT_FILL_BOTTOM },
147 { "fill_top", LT_FILL_TOP },
149 { "freespacing", LT_FREE_SPACING },
150 { "innertag", LT_INNERTAG },
151 { "intitle", LT_INTITLE },
152 { "itemsep", LT_ITEMSEP },
153 { "itemtag", LT_ITEMTAG },
154 { "keepempty", LT_KEEPEMPTY },
155 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
156 { "labelcounter", LT_LABELCOUNTER },
157 { "labelfont", LT_LABELFONT },
158 { "labelindent", LT_LABELINDENT },
159 { "labelsep", LT_LABELSEP },
160 { "labelstring", LT_LABELSTRING },
161 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
162 { "labeltag", LT_LABELTAG },
163 { "labeltype", LT_LABELTYPE },
164 { "latexfooter", LT_LATEXFOOTER },
165 { "latexheader", LT_LATEXHEADER },
166 { "latexname", LT_LATEXNAME },
167 { "latexparagraph", LT_LATEXPARAGRAPH },
168 { "latexparam", LT_LATEXPARAM },
169 { "latextype", LT_LATEXTYPE },
170 { "leftmargin", LT_LEFTMARGIN },
171 { "margin", LT_MARGIN },
172 { "needprotect", LT_NEED_PROTECT },
173 { "newline", LT_NEWLINE },
174 { "nextnoindent", LT_NEXTNOINDENT },
175 { "obsoletedby", LT_OBSOLETEDBY },
176 { "optionalargs", LT_OPTARGS },
177 { "parindent", LT_PARINDENT },
178 { "parsep", LT_PARSEP },
179 { "parskip", LT_PARSKIP },
180 { "passthru", LT_PASS_THRU },
181 { "preamble", LT_PREAMBLE },
182 { "requires", LT_REQUIRES },
183 { "rightmargin", LT_RIGHTMARGIN },
184 { "spacing", LT_SPACING },
185 { "textfont", LT_TEXTFONT },
186 { "toclevel", LT_TOCLEVEL },
187 { "topsep", LT_TOPSEP }
191 bool finished = false;
192 lex.pushTable(layoutTags);
193 // parse style section
194 while (!finished && lex.isOK() && !error) {
196 // See comment in LyXRC.cpp.
198 case Lexer::LEX_FEOF:
201 case Lexer::LEX_UNDEF: // parse error
202 lex.printError("Unknown layout tag `$$Token'");
207 switch (static_cast<LayoutTags>(le)) {
208 case LT_END: // end of structure
216 case LT_COPYSTYLE: { // initialize with a known style
219 style = subst(style, '_', ' ');
221 if (tclass.hasLayout(style)) {
222 docstring const tmpname = name_;
223 this->operator=(tclass[style]);
226 lyxerr << "Cannot copy unknown style `"
227 << to_utf8(style) << "'\n"
228 << "All layouts so far:"
230 DocumentClass::const_iterator lit = tclass.begin();
231 DocumentClass::const_iterator len = tclass.end();
232 for (; lit != len; ++lit)
233 lyxerr << to_utf8(lit->name()) << endl;
235 //lex.printError("Cannot copy known "
236 // "style `$$Token'");
241 case LT_OBSOLETEDBY: { // replace with a known style
244 style = subst(style, '_', ' ');
246 if (tclass.hasLayout(style)) {
247 docstring const tmpname = name_;
248 this->operator=(tclass[style]);
250 if (obsoleted_by().empty())
251 obsoleted_by_ = style;
253 lyxerr << "Cannot replace with unknown style `"
254 << to_utf8(style) << '\'' << endl;
256 //lex.printError("Cannot replace with"
265 depends_on_ = subst(depends_on_, '_', ' ');
268 case LT_MARGIN: // margin style definition.
272 case LT_LATEXTYPE: // LaTeX style definition.
276 case LT_LATEXHEADER: // header for environments
280 case LT_LATEXFOOTER: // footer for environments
284 case LT_LATEXPARAGRAPH:
285 lex >> latexparagraph;
297 lex >> optionalargs ;
300 case LT_NEED_PROTECT:
309 font = lyxRead(lex, font);
314 font = lyxRead(lex, font);
318 labelfont = lyxRead(lex, labelfont);
321 case LT_NEXTNOINDENT: // Indent next paragraph?
325 case LT_COMMANDDEPTH:
335 latexparam_ = subst(latexparam_, """, "\"");
351 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
358 case LT_ENDLABELTYPE:
359 readEndLabelType(lex);
362 case LT_LEFTMARGIN: // left margin type
366 case LT_RIGHTMARGIN: // right margin type
370 case LT_LABELINDENT: // label indenting flag
374 case LT_PARINDENT: // paragraph indent. flag
378 case LT_PARSKIP: // paragraph skip size
382 case LT_ITEMSEP: // item separation size
386 case LT_TOPSEP: // top separation size
390 case LT_BOTTOMSEP: // bottom separation size
394 case LT_LABEL_BOTTOMSEP: // label bottom separation size
395 lex >> labelbottomsep;
398 case LT_LABELSEP: // label separator
400 labelsep = subst(labelsep, 'x', ' ');
403 case LT_PARSEP: // par. separation size
407 case LT_FILL_TOP: // fill top flag
411 case LT_FILL_BOTTOM: // fill bottom flag
415 case LT_NEWLINE: // newlines allowed?
416 lex >> newline_allowed;
419 case LT_ALIGN: // paragraph align
422 case LT_ALIGNPOSSIBLE: // paragraph allowed align
423 readAlignPossible(lex);
426 case LT_LABELSTRING: // label string definition
427 // FIXME: this means LT_ENDLABELSTRING may only
428 // occur after LT_LABELSTRING
430 labelstring_ = trim(labelstring_);
431 labelstring_appendix_ = labelstring_;
434 case LT_ENDLABELSTRING: // endlabel string definition
435 lex >> endlabelstring_;
436 endlabelstring_ = trim(endlabelstring_);
439 case LT_LABELSTRING_APPENDIX: // label string appendix definition
440 lex >> labelstring_appendix_;
441 labelstring_appendix_ = trim(labelstring_appendix_);
444 case LT_LABELCOUNTER: // name of counter to use
446 counter = trim(counter);
449 case LT_FREE_SPACING: // Allow for free spacing.
453 case LT_PASS_THRU: // Allow for pass thru.
457 case LT_SPACING: // setspace.sty
463 vector<string> const req =
464 getVectorFromString(lex.getString());
465 requires_.insert(req.begin(), req.end());
485 LexerKeyword alignTags[] = {
486 { "block", AT_BLOCK },
487 { "center", AT_CENTER },
488 { "layout", AT_LAYOUT },
490 { "right", AT_RIGHT }
494 void Layout::readAlign(Lexer & lex)
496 PushPopHelper pph(lex, alignTags);
499 case Lexer::LEX_UNDEF:
500 lex.printError("Unknown alignment `$$Token'");
506 align = LYX_ALIGN_BLOCK;
509 align = LYX_ALIGN_LEFT;
512 align = LYX_ALIGN_RIGHT;
515 align = LYX_ALIGN_CENTER;
518 align = LYX_ALIGN_LAYOUT;
524 void Layout::readAlignPossible(Lexer & lex)
526 lex.pushTable(alignTags);
527 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
528 int lineno = lex.lineNumber();
532 case Lexer::LEX_UNDEF:
533 lex.printError("Unknown alignment `$$Token'");
539 alignpossible |= LYX_ALIGN_BLOCK;
542 alignpossible |= LYX_ALIGN_LEFT;
545 alignpossible |= LYX_ALIGN_RIGHT;
548 alignpossible |= LYX_ALIGN_CENTER;
551 alignpossible |= LYX_ALIGN_LAYOUT;
554 } while (lineno == lex.lineNumber());
559 void Layout::readLabelType(Lexer & lex)
565 LA_CENTERED_TOP_ENVIRONMENT,
575 LexerKeyword labelTypeTags[] = {
576 { "bibliography", LA_BIBLIO },
577 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
578 { "counter", LA_COUNTER },
579 { "enumerate", LA_ENUMERATE },
580 { "itemize", LA_ITEMIZE },
581 { "manual", LA_MANUAL },
582 { "no_label", LA_NO_LABEL },
583 { "sensitive", LA_SENSITIVE },
584 { "static", LA_STATIC },
585 { "top_environment", LA_TOP_ENVIRONMENT }
588 PushPopHelper pph(lex, labelTypeTags);
591 case Lexer::LEX_UNDEF:
592 lex.printError("Unknown labeltype tag `$$Token'");
598 labeltype = LABEL_NO_LABEL;
601 labeltype = LABEL_MANUAL;
603 case LA_TOP_ENVIRONMENT:
604 labeltype = LABEL_TOP_ENVIRONMENT;
606 case LA_CENTERED_TOP_ENVIRONMENT:
607 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
610 labeltype = LABEL_STATIC;
613 labeltype = LABEL_SENSITIVE;
616 labeltype = LABEL_COUNTER;
619 labeltype = LABEL_ENUMERATE;
622 labeltype = LABEL_ITEMIZE;
625 labeltype = LABEL_BIBLIO;
631 void Layout::readEndLabelType(Lexer & lex)
633 static LexerKeyword endlabelTypeTags[] = {
634 { "box", END_LABEL_BOX },
635 { "filled_box", END_LABEL_FILLED_BOX },
636 { "no_label", END_LABEL_NO_LABEL },
637 { "static", END_LABEL_STATIC }
640 PushPopHelper pph(lex, endlabelTypeTags);
643 case Lexer::LEX_UNDEF:
644 lex.printError("Unknown labeltype tag `$$Token'");
646 case END_LABEL_STATIC:
648 case END_LABEL_FILLED_BOX:
649 case END_LABEL_NO_LABEL:
650 endlabeltype = static_cast<EndLabelType>(le);
653 LYXERR0("Unhandled value " << le
654 << " in Layout::readEndLabelType.");
660 void Layout::readMargin(Lexer & lex)
662 LexerKeyword marginTags[] = {
663 { "dynamic", MARGIN_DYNAMIC },
664 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
665 { "manual", MARGIN_MANUAL },
666 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
667 { "static", MARGIN_STATIC }
670 PushPopHelper pph(lex, marginTags);
674 case Lexer::LEX_UNDEF:
675 lex.printError("Unknown margin type tag `$$Token'");
680 case MARGIN_FIRST_DYNAMIC:
681 case MARGIN_RIGHT_ADDRESS_BOX:
682 margintype = static_cast<MarginType>(le);
685 lyxerr << "Unhandled value " << le
686 << " in Layout::readMargin." << endl;
692 void Layout::readLatexType(Lexer & lex)
694 LexerKeyword latexTypeTags[] = {
695 { "bib_environment", LATEX_BIB_ENVIRONMENT },
696 { "command", LATEX_COMMAND },
697 { "environment", LATEX_ENVIRONMENT },
698 { "item_environment", LATEX_ITEM_ENVIRONMENT },
699 { "list_environment", LATEX_LIST_ENVIRONMENT },
700 { "paragraph", LATEX_PARAGRAPH }
703 PushPopHelper pph(lex, latexTypeTags);
706 case Lexer::LEX_UNDEF:
707 lex.printError("Unknown latextype tag `$$Token'");
709 case LATEX_PARAGRAPH:
711 case LATEX_ENVIRONMENT:
712 case LATEX_ITEM_ENVIRONMENT:
713 case LATEX_BIB_ENVIRONMENT:
714 case LATEX_LIST_ENVIRONMENT:
715 latextype = static_cast<LatexType>(le);
718 lyxerr << "Unhandled value " << le
719 << " in Layout::readLatexType." << endl;
725 void Layout::readSpacing(Lexer & lex)
728 ST_SPACING_SINGLE = 1,
734 LexerKeyword spacingTags[] = {
735 {"double", ST_SPACING_DOUBLE },
736 {"onehalf", ST_SPACING_ONEHALF },
737 {"other", ST_OTHER },
738 {"single", ST_SPACING_SINGLE }
741 PushPopHelper pph(lex, spacingTags);
744 case Lexer::LEX_UNDEF:
745 lex.printError("Unknown spacing token `$$Token'");
750 case ST_SPACING_SINGLE:
751 spacing.set(Spacing::Single);
753 case ST_SPACING_ONEHALF:
754 spacing.set(Spacing::Onehalf);
756 case ST_SPACING_DOUBLE:
757 spacing.set(Spacing::Double);
761 spacing.set(Spacing::Other, lex.getString());
767 docstring const & Layout::name() const
773 void Layout::setName(docstring const & name)
779 docstring const & Layout::obsoleted_by() const
781 return obsoleted_by_;
785 docstring const & Layout::depends_on() const
791 bool Layout::operator==(Layout const & rhs) const
793 // This is enough for the applications we actually make,
794 // at least at the moment. But we could check more.
795 return name() == rhs.name()
796 && latexname() == rhs.latexname()
797 && latextype == rhs.latextype;