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"
26 using namespace lyx::support;
30 /// Special value of toclevel for layouts that to not belong in a TOC
31 const int Layout::NOT_IN_TOC = -1000;
33 // The order of the LayoutTags enum is no more important. [asierra300396]
47 //LT_ENVIRONMENT_DEFAULT,
64 LT_LABELSTRING_APPENDIX,
93 LT_INTITLE // keep this last!
100 margintype = MARGIN_STATIC;
101 latextype = LATEX_PARAGRAPH;
107 labelfont = inherit_font;
109 reslabelfont = sane_font;
110 nextnoindent = false;
115 labelbottomsep = 0.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);
125 newline_allowed = true;
126 free_spacing = false;
128 is_environment = false;
129 toclevel = NOT_IN_TOC;
134 bool Layout::read(Lexer & lexrc, TextClass const & tclass)
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 },
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 { "requires", LT_REQUIRES },
185 { "rightmargin", LT_RIGHTMARGIN },
186 { "spacing", LT_SPACING },
187 { "textfont", LT_TEXTFONT },
188 { "toclevel", LT_TOCLEVEL },
189 { "topsep", LT_TOPSEP }
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.
200 case Lexer::LEX_FEOF:
203 case Lexer::LEX_UNDEF: // parse error
204 lexrc.printError("Unknown layout tag `$$Token'");
209 switch (static_cast<LayoutTags>(le)) {
210 case LT_END: // end of structure
216 category_ = lexrc.getDocString();
219 case LT_COPYSTYLE: // initialize with a known style
221 docstring const style = subst(lexrc.getDocString(),
224 if (tclass.hasLayout(style)) {
225 docstring const tmpname = name_;
226 this->operator=(tclass[style]);
229 lyxerr << "Cannot copy unknown style `"
230 << to_utf8(style) << "'\n"
231 << "All layouts so far:"
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;
238 //lexrc.printError("Cannot copy known "
239 // "style `$$Token'");
244 case LT_OBSOLETEDBY: // replace with a known style
246 docstring const style =
247 subst(lexrc.getDocString(), '_', ' ');
249 if (tclass.hasLayout(style)) {
250 docstring const tmpname = name_;
251 this->operator=(tclass[style]);
253 if (obsoleted_by().empty())
254 obsoleted_by_ = style;
256 lyxerr << "Cannot replace with unknown style `"
257 << to_utf8(style) << '\'' << endl;
259 //lexrc.printError("Cannot replace with"
268 depends_on_ = subst(lexrc.getDocString(), '_', ' ');
271 case LT_MARGIN: // margin style definition.
275 case LT_LATEXTYPE: // LaTeX style definition.
276 readLatexType(lexrc);
279 case LT_LATEXHEADER: // header for environments
281 latexheader = lexrc.getString();
284 case LT_LATEXFOOTER: // footer for environments
286 latexfooter = lexrc.getString();
289 case LT_LATEXPARAGRAPH:
291 latexparagraph = lexrc.getString();
295 intitle = lexrc.next() && lexrc.getInteger();
300 toclevel = lexrc.getInteger();
305 optionalargs = lexrc.getInteger();
308 case LT_NEED_PROTECT:
309 needprotect = lexrc.next() && lexrc.getInteger();
313 keepempty = lexrc.next() && lexrc.getInteger();
317 font = lyxRead(lexrc, font);
322 font = lyxRead(lexrc, font);
326 labelfont = lyxRead(lexrc, labelfont);
329 case LT_NEXTNOINDENT: // Indent next paragraph?
330 if (lexrc.next() && lexrc.getInteger())
333 nextnoindent = false;
336 case LT_COMMANDDEPTH:
338 commanddepth = lexrc.getInteger();
343 latexname_ = lexrc.getString();
348 latexparam_ = subst(lexrc.getString(), """, "\"");
353 innertag_ = lexrc.getString();
358 labeltag_ = lexrc.getString();
363 itemtag_ = lexrc.getString();
367 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
371 readLabelType(lexrc);
374 case LT_ENDLABELTYPE:
375 readEndLabelType(lexrc);
378 case LT_LEFTMARGIN: // left margin type
380 leftmargin = lexrc.getDocString();
383 case LT_RIGHTMARGIN: // right margin type
385 rightmargin = lexrc.getDocString();
388 case LT_LABELINDENT: // label indenting flag
390 labelindent = lexrc.getDocString();
393 case LT_PARINDENT: // paragraph indent. flag
395 parindent = lexrc.getDocString();
398 case LT_PARSKIP: // paragraph skip size
400 parskip = lexrc.getFloat();
403 case LT_ITEMSEP: // item separation size
405 itemsep = lexrc.getFloat();
408 case LT_TOPSEP: // top separation size
410 topsep = lexrc.getFloat();
413 case LT_BOTTOMSEP: // bottom separation size
415 bottomsep = lexrc.getFloat();
418 case LT_LABEL_BOTTOMSEP: // label bottom separation size
420 labelbottomsep = lexrc.getFloat();
423 case LT_LABELSEP: // label separator
425 labelsep = from_utf8(subst(lexrc.getString(), 'x', ' '));
429 case LT_PARSEP: // par. separation size
431 parsep = lexrc.getFloat();
434 case LT_FILL_TOP: // fill top flag
436 fill_top = lexrc.getInteger();
439 case LT_FILL_BOTTOM: // fill bottom flag
441 fill_bottom = lexrc.getInteger();
444 case LT_NEWLINE: // newlines allowed?
446 newline_allowed = lexrc.getInteger();
449 case LT_ALIGN: // paragraph align
452 case LT_ALIGNPOSSIBLE: // paragraph allowed align
453 readAlignPossible(lexrc);
456 case LT_LABELSTRING: // label string definition
458 labelstring_ = trim(lexrc.getDocString());
459 labelstring_appendix_ = labelstring_;
463 case LT_ENDLABELSTRING: // endlabel string definition
465 endlabelstring_ = trim(lexrc.getDocString());
468 case LT_LABELSTRING_APPENDIX: // label string appendix definition
470 labelstring_appendix_ = trim(lexrc.getDocString());
473 case LT_LABELCOUNTER: // name of counter to use
475 counter = lyx::from_ascii(trim(lexrc.getString()));
478 case LT_FREE_SPACING: // Allow for free spacing.
480 free_spacing = lexrc.getInteger();
483 case LT_PASS_THRU: // Allow for pass thru.
485 pass_thru = lexrc.getInteger();
488 case LT_SPACING: // setspace.sty
494 vector<string> const req =
495 getVectorFromString(lexrc.getString());
496 requires_.insert(req.begin(), req.end());
515 void Layout::readAlign(Lexer & lexrc)
517 LexerKeyword alignTags[] = {
518 { "block", AT_BLOCK },
519 { "center", AT_CENTER },
520 { "layout", AT_LAYOUT },
522 { "right", AT_RIGHT }
525 PushPopHelper pph(lexrc, alignTags);
526 int le = lexrc.lex();
528 case Lexer::LEX_UNDEF:
529 lexrc.printError("Unknown alignment `$$Token'");
535 align = LYX_ALIGN_BLOCK;
538 align = LYX_ALIGN_LEFT;
541 align = LYX_ALIGN_RIGHT;
544 align = LYX_ALIGN_CENTER;
547 align = LYX_ALIGN_LAYOUT;
553 void Layout::readAlignPossible(Lexer & lexrc)
555 LexerKeyword alignTags[] = {
556 { "block", AT_BLOCK },
557 { "center", AT_CENTER },
558 { "layout", AT_LAYOUT },
560 { "right", AT_RIGHT }
563 lexrc.pushTable(alignTags);
564 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
565 int lineno = lexrc.lineNumber();
567 int le = lexrc.lex();
569 case Lexer::LEX_UNDEF:
570 lexrc.printError("Unknown alignment `$$Token'");
576 alignpossible |= LYX_ALIGN_BLOCK;
579 alignpossible |= LYX_ALIGN_LEFT;
582 alignpossible |= LYX_ALIGN_RIGHT;
585 alignpossible |= LYX_ALIGN_CENTER;
588 alignpossible |= LYX_ALIGN_LAYOUT;
591 } while (lineno == lexrc.lineNumber());
596 void Layout::readLabelType(Lexer & lexrc)
602 LA_CENTERED_TOP_ENVIRONMENT,
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 }
625 PushPopHelper pph(lexrc, labelTypeTags);
626 int le = lexrc.lex();
628 case Lexer::LEX_UNDEF:
629 lexrc.printError("Unknown labeltype tag `$$Token'");
635 labeltype = LABEL_NO_LABEL;
638 labeltype = LABEL_MANUAL;
640 case LA_TOP_ENVIRONMENT:
641 labeltype = LABEL_TOP_ENVIRONMENT;
643 case LA_CENTERED_TOP_ENVIRONMENT:
644 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
647 labeltype = LABEL_STATIC;
650 labeltype = LABEL_SENSITIVE;
653 labeltype = LABEL_COUNTER;
656 labeltype = LABEL_ENUMERATE;
659 labeltype = LABEL_ITEMIZE;
662 labeltype = LABEL_BIBLIO;
668 void Layout::readEndLabelType(Lexer & lexrc)
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 }
677 PushPopHelper pph(lexrc, endlabelTypeTags);
678 int le = lexrc.lex();
680 case Lexer::LEX_UNDEF:
681 lexrc.printError("Unknown labeltype tag `$$Token'");
683 case END_LABEL_STATIC:
685 case END_LABEL_FILLED_BOX:
686 case END_LABEL_NO_LABEL:
687 endlabeltype = static_cast<EndLabelType>(le);
690 lyxerr << "Unhandled value " << le
691 << " in Layout::readEndLabelType." << endl;
697 void Layout::readMargin(Lexer & lexrc)
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 }
707 PushPopHelper pph(lexrc, marginTags);
709 int le = lexrc.lex();
711 case Lexer::LEX_UNDEF:
712 lexrc.printError("Unknown margin type tag `$$Token'");
717 case MARGIN_FIRST_DYNAMIC:
718 case MARGIN_RIGHT_ADDRESS_BOX:
719 margintype = static_cast<MarginType>(le);
722 lyxerr << "Unhandled value " << le
723 << " in Layout::readMargin." << endl;
729 void Layout::readLatexType(Lexer & lexrc)
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 }
740 PushPopHelper pph(lexrc, latexTypeTags);
741 int le = lexrc.lex();
743 case Lexer::LEX_UNDEF:
744 lexrc.printError("Unknown latextype tag `$$Token'");
746 case LATEX_PARAGRAPH:
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);
755 lyxerr << "Unhandled value " << le
756 << " in Layout::readLatexType." << endl;
762 void Layout::readSpacing(Lexer & lexrc)
765 ST_SPACING_SINGLE = 1,
771 LexerKeyword spacingTags[] = {
772 {"double", ST_SPACING_DOUBLE },
773 {"onehalf", ST_SPACING_ONEHALF },
774 {"other", ST_OTHER },
775 {"single", ST_SPACING_SINGLE }
778 PushPopHelper pph(lexrc, spacingTags);
779 int le = lexrc.lex();
781 case Lexer::LEX_UNDEF:
782 lexrc.printError("Unknown spacing token `$$Token'");
787 case ST_SPACING_SINGLE:
788 spacing.set(Spacing::Single);
790 case ST_SPACING_ONEHALF:
791 spacing.set(Spacing::Onehalf);
793 case ST_SPACING_DOUBLE:
794 spacing.set(Spacing::Double);
798 spacing.set(Spacing::Other, lexrc.getString());
804 docstring const & Layout::name() const
810 void Layout::setName(docstring const & name)
816 docstring const & Layout::obsoleted_by() const
818 return obsoleted_by_;
822 docstring const & Layout::depends_on() const
828 bool Layout::operator==(Layout const & rhs) const
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;
838 Layout * Layout::forCaption()
840 Layout * lay = new Layout();
841 lay->name_ = from_ascii("Caption");
842 lay->latexname_ = "caption";
843 lay->latextype = LATEX_COMMAND;
844 lay->optionalargs = 1;