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/lstrings.h"
31 /// Special value of toclevel for layouts that to not belong in a TOC
32 const int Layout::NOT_IN_TOC = -1000;
34 // The order of the LayoutTags enum is no more important. [asierra300396]
47 //LT_ENVIRONMENT_DEFAULT,
64 LT_LABELSTRING_APPENDIX,
92 LT_INTITLE // keep this last!
97 // Constructor for layout
100 margintype = MARGIN_STATIC;
101 latextype = LATEX_PARAGRAPH;
106 font = Font(Font::ALL_INHERIT);
107 labelfont = Font(Font::ALL_INHERIT);
108 resfont = Font(Font::ALL_SANE);
109 reslabelfont = Font(Font::ALL_SANE);
110 nextnoindent = false;
115 labelbottomsep = 0.0;
117 align = LYX_ALIGN_BLOCK;
118 alignpossible = LYX_ALIGN_BLOCK;
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);
125 newline_allowed = true;
126 free_spacing = false;
128 is_environment = false;
129 toclevel = NOT_IN_TOC;
134 // Reads a layout definition from file
135 bool Layout::read(Lexer & lexrc, TextClass const & tclass)
137 // This table is sorted alphabetically [asierra 30March96]
138 keyword_item layoutTags[] = {
139 { "align", LT_ALIGN },
140 { "alignpossible", LT_ALIGNPOSSIBLE },
141 { "bottomsep", LT_BOTTOMSEP },
142 { "commanddepth", LT_COMMANDDEPTH },
143 { "copystyle", LT_COPYSTYLE },
144 { "dependson", LT_DEPENDSON },
146 { "endlabelstring", LT_ENDLABELSTRING },
147 { "endlabeltype", LT_ENDLABELTYPE },
148 { "fill_bottom", LT_FILL_BOTTOM },
149 { "fill_top", LT_FILL_TOP },
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 { "rightmargin", LT_RIGHTMARGIN },
185 { "spacing", LT_SPACING },
186 { "textfont", LT_TEXTFONT },
187 { "toclevel", LT_TOCLEVEL },
188 { "topsep", LT_TOPSEP }
192 bool finished = false;
193 lexrc.pushTable(layoutTags, LT_INTITLE);
194 // parse style section
195 while (!finished && lexrc.isOK() && !error) {
196 int le = lexrc.lex();
197 // See comment in LyXRC.cpp.
199 case Lexer::LEX_FEOF:
202 case Lexer::LEX_UNDEF: // parse error
203 lexrc.printError("Unknown layout tag `$$Token'");
208 switch (static_cast<LayoutTags>(le)) {
209 case LT_END: // end of structure
213 case LT_COPYSTYLE: // initialize with a known style
215 docstring const style = subst(lexrc.getDocString(),
218 if (tclass.hasLayout(style)) {
219 docstring const tmpname = name_;
220 this->operator=(*tclass[style]);
223 lyxerr << "Cannot copy unknown style `"
224 << to_utf8(style) << "'\n"
225 << "All layouts so far:"
227 TextClass::const_iterator it =
229 TextClass::const_iterator end =
231 for (; it != end; ++it) {
232 lyxerr << to_utf8((*it)->name())
236 //lexrc.printError("Cannot copy known "
237 // "style `$$Token'");
242 case LT_OBSOLETEDBY: // replace with a known style
244 docstring const style = lexrc.getDocString();
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 //lexrc.printError("Cannot replace with"
265 depends_on_ = lexrc.getDocString();
268 case LT_MARGIN: // margin style definition.
272 case LT_LATEXTYPE: // LaTeX style definition.
273 readLatexType(lexrc);
276 case LT_LATEXHEADER: // header for environments
278 latexheader = lexrc.getString();
281 case LT_LATEXFOOTER: // footer for environments
283 latexfooter = lexrc.getString();
286 case LT_LATEXPARAGRAPH:
288 latexparagraph = lexrc.getString();
292 intitle = lexrc.next() && lexrc.getInteger();
297 toclevel = lexrc.getInteger();
302 optionalargs = lexrc.getInteger();
305 case LT_NEED_PROTECT:
306 needprotect = lexrc.next() && lexrc.getInteger();
310 keepempty = lexrc.next() && lexrc.getInteger();
323 labelfont.lyxRead(lexrc);
326 case LT_NEXTNOINDENT: // Indent next paragraph?
327 if (lexrc.next() && lexrc.getInteger())
330 nextnoindent = false;
333 case LT_COMMANDDEPTH:
335 commanddepth = lexrc.getInteger();
340 latexname_ = lexrc.getString();
345 latexparam_ = subst(lexrc.getString(), """, "\"");
350 innertag_ = lexrc.getString();
355 labeltag_ = lexrc.getString();
360 itemtag_ = lexrc.getString();
364 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
368 readLabelType(lexrc);
371 case LT_ENDLABELTYPE:
372 readEndLabelType(lexrc);
375 case LT_LEFTMARGIN: // left margin type
377 leftmargin = lexrc.getString();
380 case LT_RIGHTMARGIN: // right margin type
382 rightmargin = lexrc.getString();
385 case LT_LABELINDENT: // label indenting flag
387 labelindent = lexrc.getString();
390 case LT_PARINDENT: // paragraph indent. flag
392 parindent = lexrc.getString();
395 case LT_PARSKIP: // paragraph skip size
397 parskip = lexrc.getFloat();
400 case LT_ITEMSEP: // item separation size
402 itemsep = lexrc.getFloat();
405 case LT_TOPSEP: // top separation size
407 topsep = lexrc.getFloat();
410 case LT_BOTTOMSEP: // bottom separation size
412 bottomsep = lexrc.getFloat();
415 case LT_LABEL_BOTTOMSEP: // label bottom separation size
417 labelbottomsep = lexrc.getFloat();
420 case LT_LABELSEP: // label separator
422 labelsep = subst(lexrc.getString(), 'x', ' ');
426 case LT_PARSEP: // par. separation size
428 parsep = lexrc.getFloat();
431 case LT_FILL_TOP: // fill top flag
433 fill_top = lexrc.getInteger();
436 case LT_FILL_BOTTOM: // fill bottom flag
438 fill_bottom = lexrc.getInteger();
441 case LT_NEWLINE: // newlines allowed?
443 newline_allowed = lexrc.getInteger();
446 case LT_ALIGN: // paragraph align
449 case LT_ALIGNPOSSIBLE: // paragraph allowed align
450 readAlignPossible(lexrc);
453 case LT_LABELSTRING: // label string definition
455 labelstring_ = trim(lexrc.getDocString());
456 labelstring_appendix_ = labelstring_;
460 case LT_ENDLABELSTRING: // endlabel string definition
462 endlabelstring_ = trim(lexrc.getDocString());
465 case LT_LABELSTRING_APPENDIX: // label string appendix definition
467 labelstring_appendix_ = trim(lexrc.getDocString());
470 case LT_LABELCOUNTER: // name of counter to use
472 counter = lyx::from_ascii(trim(lexrc.getString()));
475 case LT_FREE_SPACING: // Allow for free spacing.
477 free_spacing = lexrc.getInteger();
480 case LT_PASS_THRU: // Allow for pass thru.
482 pass_thru = lexrc.getInteger();
485 case LT_SPACING: // setspace.sty
505 void Layout::readAlign(Lexer & lexrc)
507 keyword_item alignTags[] = {
508 { "block", AT_BLOCK },
509 { "center", AT_CENTER },
510 { "layout", AT_LAYOUT },
512 { "right", AT_RIGHT }
515 PushPopHelper pph(lexrc, alignTags, AT_LAYOUT);
516 int le = lexrc.lex();
518 case Lexer::LEX_UNDEF:
519 lexrc.printError("Unknown alignment `$$Token'");
523 switch (static_cast<AlignTags>(le)) {
525 align = LYX_ALIGN_BLOCK;
528 align = LYX_ALIGN_LEFT;
531 align = LYX_ALIGN_RIGHT;
534 align = LYX_ALIGN_CENTER;
537 align = LYX_ALIGN_LAYOUT;
543 void Layout::readAlignPossible(Lexer & lexrc)
545 keyword_item alignTags[] = {
546 { "block", AT_BLOCK },
547 { "center", AT_CENTER },
548 { "layout", AT_LAYOUT },
550 { "right", AT_RIGHT }
553 lexrc.pushTable(alignTags, AT_LAYOUT);
554 alignpossible = LYX_ALIGN_NONE;
555 int lineno = lexrc.getLineNo();
557 int le = lexrc.lex();
559 case Lexer::LEX_UNDEF:
560 lexrc.printError("Unknown alignment `$$Token'");
564 switch (static_cast<AlignTags>(le)) {
566 alignpossible |= LYX_ALIGN_BLOCK;
569 alignpossible |= LYX_ALIGN_LEFT;
572 alignpossible |= LYX_ALIGN_RIGHT;
575 alignpossible |= LYX_ALIGN_CENTER;
578 alignpossible |= LYX_ALIGN_LAYOUT;
581 } while (lineno == lexrc.getLineNo());
590 LA_CENTERED_TOP_ENVIRONMENT,
600 void Layout::readLabelType(Lexer & lexrc)
602 keyword_item labelTypeTags[] = {
603 { "bibliography", LA_BIBLIO },
604 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
605 { "counter", LA_COUNTER },
606 { "enumerate", LA_ENUMERATE },
607 { "itemize", LA_ITEMIZE },
608 { "manual", LA_MANUAL },
609 { "no_label", LA_NO_LABEL },
610 { "sensitive", LA_SENSITIVE },
611 { "static", LA_STATIC },
612 { "top_environment", LA_TOP_ENVIRONMENT }
615 PushPopHelper pph(lexrc, labelTypeTags, LA_BIBLIO);
616 int le = lexrc.lex();
618 case Lexer::LEX_UNDEF:
619 lexrc.printError("Unknown labeltype tag `$$Token'");
623 switch (static_cast<LabelTypeTags>(le)) {
625 labeltype = LABEL_NO_LABEL;
628 labeltype = LABEL_MANUAL;
630 case LA_TOP_ENVIRONMENT:
631 labeltype = LABEL_TOP_ENVIRONMENT;
633 case LA_CENTERED_TOP_ENVIRONMENT:
634 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
637 labeltype = LABEL_STATIC;
640 labeltype = LABEL_SENSITIVE;
643 labeltype = LABEL_COUNTER;
646 labeltype = LABEL_ENUMERATE;
649 labeltype = LABEL_ITEMIZE;
652 labeltype = LABEL_BIBLIO;
660 keyword_item endlabelTypeTags[] = {
661 { "box", END_LABEL_BOX },
662 { "filled_box", END_LABEL_FILLED_BOX },
663 { "no_label", END_LABEL_NO_LABEL },
664 { "static", END_LABEL_STATIC }
670 void Layout::readEndLabelType(Lexer & lexrc)
672 PushPopHelper pph(lexrc, endlabelTypeTags,
673 END_LABEL_ENUM_LAST-END_LABEL_ENUM_FIRST+1);
674 int le = lexrc.lex();
676 case Lexer::LEX_UNDEF:
677 lexrc.printError("Unknown labeltype tag `$$Token'");
679 case END_LABEL_STATIC:
681 case END_LABEL_FILLED_BOX:
682 case END_LABEL_NO_LABEL:
683 endlabeltype = static_cast<LYX_END_LABEL_TYPES>(le);
686 lyxerr << "Unhandled value " << le
687 << " in Layout::readEndLabelType." << endl;
693 void Layout::readMargin(Lexer & lexrc)
695 keyword_item marginTags[] = {
696 { "dynamic", MARGIN_DYNAMIC },
697 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
698 { "manual", MARGIN_MANUAL },
699 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
700 { "static", MARGIN_STATIC }
703 PushPopHelper pph(lexrc, marginTags, MARGIN_RIGHT_ADDRESS_BOX);
705 int le = lexrc.lex();
707 case Lexer::LEX_UNDEF:
708 lexrc.printError("Unknown margin type tag `$$Token'");
713 case MARGIN_FIRST_DYNAMIC:
714 case MARGIN_RIGHT_ADDRESS_BOX:
715 margintype = static_cast<LYX_MARGIN_TYPE>(le);
718 lyxerr << "Unhandled value " << le
719 << " in Layout::readMargin." << endl;
725 void Layout::readLatexType(Lexer & lexrc)
727 keyword_item latexTypeTags[] = {
728 { "bib_environment", LATEX_BIB_ENVIRONMENT },
729 { "command", LATEX_COMMAND },
730 { "environment", LATEX_ENVIRONMENT },
731 { "item_environment", LATEX_ITEM_ENVIRONMENT },
732 { "list_environment", LATEX_LIST_ENVIRONMENT },
733 { "paragraph", LATEX_PARAGRAPH }
736 PushPopHelper pph(lexrc, latexTypeTags, LATEX_LIST_ENVIRONMENT);
737 int le = lexrc.lex();
739 case Lexer::LEX_UNDEF:
740 lexrc.printError("Unknown latextype tag `$$Token'");
742 case LATEX_PARAGRAPH:
744 case LATEX_ENVIRONMENT:
745 case LATEX_ITEM_ENVIRONMENT:
746 case LATEX_BIB_ENVIRONMENT:
747 case LATEX_LIST_ENVIRONMENT:
748 latextype = static_cast<LYX_LATEX_TYPES>(le);
751 lyxerr << "Unhandled value " << le
752 << " in Layout::readLatexType." << endl;
759 ST_SPACING_SINGLE = 1,
766 void Layout::readSpacing(Lexer & lexrc)
768 keyword_item spacingTags[] = {
769 {"double", ST_SPACING_DOUBLE },
770 {"onehalf", ST_SPACING_ONEHALF },
771 {"other", ST_OTHER },
772 {"single", ST_SPACING_SINGLE }
775 PushPopHelper pph(lexrc, spacingTags, ST_OTHER);
776 int le = lexrc.lex();
778 case Lexer::LEX_UNDEF:
779 lexrc.printError("Unknown spacing token `$$Token'");
783 switch (static_cast<SpacingTags>(le)) {
784 case ST_SPACING_SINGLE:
785 spacing.set(Spacing::Single);
787 case ST_SPACING_ONEHALF:
788 spacing.set(Spacing::Onehalf);
790 case ST_SPACING_DOUBLE:
791 spacing.set(Spacing::Double);
795 spacing.set(Spacing::Other, lexrc.getString());
801 docstring const & Layout::name() const
807 void Layout::setName(docstring const & n)
813 docstring const & Layout::obsoleted_by() const
815 return obsoleted_by_;
819 docstring const & Layout::depends_on() const
825 Layout * Layout::forCaption()
827 Layout * lay = new Layout();
828 lay->name_ = from_ascii("Caption");
829 lay->latexname_ = "caption";
830 lay->latextype = LATEX_COMMAND;
831 lay->optionalargs = 1;