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
8 * \author Angus Leeming
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
17 #include "lyxtextclass.h"
22 #include "FloatList.h"
24 #include "support/lstrings.h"
25 #include "support/filetools.h"
27 using lyx::support::LibFileSearch;
28 using lyx::support::MakeDisplayPath;
29 using lyx::support::rtrim;
30 using lyx::support::subst;
43 compare_name(string const & name)
47 bool operator()(boost::shared_ptr<LyXLayout> const & c)
49 return c->name() == name_;
59 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
60 string const & desc, bool texClassAvail )
61 : name_(fn), latexname_(cln), description_(desc),
62 floatlist_(new FloatList), ctrs_(new Counters), texClassAvail_(texClassAvail)
69 pagestyle_ = "default";
70 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
71 opt_fontsize_ = "10|11|12";
72 opt_pagestyle_ = "empty|plain|headings|fancy";
74 titletype_ = TITLE_COMMAND_AFTER;
75 titlename_ = "maketitle";
80 bool LyXTextClass::isTeXClassAvailable() const
82 return texClassAvail_;
86 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
88 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
89 if (!lay.Read(lexrc, *this)) {
91 lay.resfont = lay.font;
92 lay.resfont.realize(defaultfont());
93 lay.reslabelfont = lay.labelfont;
94 lay.reslabelfont.realize(defaultfont());
95 return false; // no errors
97 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
131 // Reads a textclass structure from file.
132 bool LyXTextClass::Read(string const & filename, bool merge)
134 keyword_item textClassTags[] = {
135 { "charstyle", TC_CHARSTYLE },
136 { "classoptions", TC_CLASSOPTIONS },
137 { "columns", TC_COLUMNS },
138 { "counter", TC_COUNTER },
139 { "defaultfont", TC_DEFAULTFONT },
140 { "defaultstyle", TC_DEFAULTSTYLE },
141 { "environment", TC_ENVIRONMENT },
142 { "float", TC_FLOAT },
143 { "input", TC_INPUT },
144 { "leftmargin", TC_LEFTMARGIN },
145 { "nofloat", TC_NOFLOAT },
146 { "nostyle", TC_NOSTYLE },
147 { "outputtype", TC_OUTPUTTYPE },
148 { "pagestyle", TC_PAGESTYLE },
149 { "preamble", TC_PREAMBLE },
150 { "providesamsmath", TC_PROVIDESAMSMATH },
151 { "providesmakeidx", TC_PROVIDESMAKEIDX },
152 { "providesnatbib", TC_PROVIDESNATBIB },
153 { "providesurl", TC_PROVIDESURL },
154 { "rightmargin", TC_RIGHTMARGIN },
155 { "secnumdepth", TC_SECNUMDEPTH },
156 { "sides", TC_SIDES },
157 { "style", TC_STYLE },
158 { "titlelatexname", TC_TITLELATEXNAME },
159 { "titlelatextype", TC_TITLELATEXTYPE },
160 { "tocdepth", TC_TOCDEPTH }
164 lyxerr[Debug::TCLASS] << "Reading textclass "
165 << MakeDisplayPath(filename)
168 lyxerr[Debug::TCLASS] << "Reading input file "
169 << MakeDisplayPath(filename)
172 LyXLex lexrc(textClassTags,
173 sizeof(textClassTags) / sizeof(textClassTags[0]));
176 lexrc.setFile(filename);
177 if (!lexrc.isOK()) error = true;
180 while (lexrc.isOK() && !error) {
181 int le = lexrc.lex();
184 case LyXLex::LEX_FEOF:
187 case LyXLex::LEX_UNDEF:
188 lexrc.printError("Unknown TextClass tag `$$Token'");
196 switch (static_cast<TextClassTags>(le)) {
198 case TC_OUTPUTTYPE: // output type definition
199 readOutputType(lexrc);
202 case TC_INPUT: // Include file
204 string tmp = LibFileSearch("layouts",
208 if (Read(tmp, true)) {
209 lexrc.printError("Error reading input"
216 case TC_DEFAULTSTYLE:
218 string const name = subst(lexrc.getString(),
220 defaultlayout_ = name;
227 string const name = subst(lexrc.getString(),
229 if (hasLayout(name)) {
230 LyXLayout * lay = operator[](name).get();
231 error = do_readStyle(lexrc, *lay);
235 if (le == TC_ENVIRONMENT)
236 lay.is_environment = true;
237 if (!(error = do_readStyle(lexrc, lay)))
238 layoutlist_.push_back(
239 boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
242 if (defaultlayout_.empty()) {
243 // We do not have a default
244 // layout yet, so we choose
245 // the first layout we
247 defaultlayout_ = name;
252 lexrc.printError("No name given for style: `$$Token'.");
259 string const style = subst(lexrc.getString(),
261 if (!delete_layout(style))
262 lyxerr << "Cannot delete style `"
263 << style << '\'' << endl;
264 // lexrc.printError("Cannot delete style"
271 columns_ = lexrc.getInteger();
276 switch (lexrc.getInteger()) {
277 case 1: sides_ = OneSide; break;
278 case 2: sides_ = TwoSides; break;
280 lyxerr << "Impossible number of page"
281 " sides, setting to one."
291 pagestyle_ = rtrim(lexrc.getString());
295 defaultfont_.lyxRead(lexrc);
296 if (!defaultfont_.resolved()) {
297 lexrc.printError("Warning: defaultfont should "
298 "be fully instantiated!");
299 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
305 secnumdepth_ = lexrc.getInteger();
310 tocdepth_ = lexrc.getInteger();
313 // First step to support options
314 case TC_CLASSOPTIONS:
315 readClassOptions(lexrc);
319 preamble_ = lexrc.getLongString("EndPreamble");
322 case TC_PROVIDESAMSMATH:
323 if (lexrc.next() && lexrc.getInteger())
324 provides_ |= amsmath;
327 case TC_PROVIDESNATBIB:
328 if (lexrc.next() && lexrc.getInteger())
332 case TC_PROVIDESMAKEIDX:
333 if (lexrc.next() && lexrc.getInteger())
334 provides_ |= makeidx;
338 if (lexrc.next() && lexrc.getInteger())
342 case TC_LEFTMARGIN: // left margin type
344 leftmargin_ = lexrc.getString();
347 case TC_RIGHTMARGIN: // right margin type
349 rightmargin_ = lexrc.getString();
353 string const name = subst(lexrc.getString(), '_', ' ');
354 readCharStyle(lexrc, name);
363 case TC_TITLELATEXTYPE:
364 readTitleType(lexrc);
366 case TC_TITLELATEXNAME:
368 titlename_ = lexrc.getString();
372 string const nofloat = lexrc.getString();
373 floatlist_->erase(nofloat);
379 if (!merge) { // we are at top level here.
380 lyxerr[Debug::TCLASS] << "Finished reading textclass "
381 << MakeDisplayPath(filename)
383 if (defaultlayout_.empty()) {
384 lyxerr << "Error: Textclass '" << name_
385 << "' is missing a defaultstyle." << endl;
389 lyxerr[Debug::TCLASS] << "Finished reading input file "
390 << MakeDisplayPath(filename)
397 void LyXTextClass::readTitleType(LyXLex & lexrc)
399 keyword_item titleTypeTags[] = {
400 { "commandafter", TITLE_COMMAND_AFTER },
401 { "environment", TITLE_ENVIRONMENT }
404 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
406 int le = lexrc.lex();
408 case LyXLex::LEX_UNDEF:
409 lexrc.printError("Unknown output type `$$Token'");
411 case TITLE_COMMAND_AFTER:
412 case TITLE_ENVIRONMENT:
413 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
416 lyxerr << "Unhandled value " << le
417 << " in LyXTextClass::readTitleType." << endl;
424 void LyXTextClass::readOutputType(LyXLex & lexrc)
426 keyword_item outputTypeTags[] = {
427 { "docbook", DOCBOOK },
429 { "linuxdoc", LINUXDOC },
430 { "literate", LITERATE }
433 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
435 int le = lexrc.lex();
437 case LyXLex::LEX_UNDEF:
438 lexrc.printError("Unknown output type `$$Token'");
444 outputType_ = static_cast<OutputType>(le);
447 lyxerr << "Unhandled value " << le
448 << " in LyXTextClass::readOutputType." << endl;
455 enum MaxCounterTags {
456 MC_COUNTER_CHAPTER = 1,
458 MC_COUNTER_SUBSECTION,
459 MC_COUNTER_SUBSUBSECTION,
460 MC_COUNTER_PARAGRAPH,
461 MC_COUNTER_SUBPARAGRAPH,
469 enum ClassOptionsTags {
477 void LyXTextClass::readClassOptions(LyXLex & lexrc)
479 keyword_item classOptionsTags[] = {
481 {"fontsize", CO_FONTSIZE },
482 {"other", CO_OTHER },
483 {"pagestyle", CO_PAGESTYLE }
486 lexrc.pushTable(classOptionsTags, CO_END);
488 while (!getout && lexrc.isOK()) {
489 int le = lexrc.lex();
491 case LyXLex::LEX_UNDEF:
492 lexrc.printError("Unknown ClassOption tag `$$Token'");
496 switch (static_cast<ClassOptionsTags>(le)) {
499 opt_fontsize_ = rtrim(lexrc.getString());
503 opt_pagestyle_ = rtrim(lexrc.getString());
507 options_ = lexrc.getString();
527 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
529 keyword_item elementTags[] = {
532 { "labelfont", CS_LABELFONT },
533 { "latexname", CS_LATEXNAME },
534 { "latextype", CS_LATEXTYPE },
535 { "preamble", CS_PREAMBLE}
538 lexrc.pushTable(elementTags, CS_END);
542 LyXFont font(LyXFont::ALL_INHERIT);
543 LyXFont labelfont(LyXFont::ALL_INHERIT);
547 while (!getout && lexrc.isOK()) {
548 int le = lexrc.lex();
550 case LyXLex::LEX_UNDEF:
551 lexrc.printError("Unknown ClassOption tag `$$Token'");
555 switch (static_cast<CharStyleTags>(le)) {
558 latextype = lexrc.getString();
562 latexname = lexrc.getString();
565 labelfont.lyxRead(lexrc);
572 preamble = lexrc.getLongString("EndPreamble");
581 // Here add element to list if getout == true
585 cs.latextype = latextype;
586 cs.latexname = latexname;
588 cs.labelfont = labelfont;
589 cs.preamble = preamble;
590 charstyles().push_back(cs);
610 void LyXTextClass::readFloat(LyXLex & lexrc)
612 keyword_item floatTags[] = {
614 { "extension", FT_EXT },
615 { "guiname", FT_NAME },
616 { "latexbuiltin", FT_BUILTIN },
617 { "listname", FT_LISTNAME },
618 { "numberwithin", FT_WITHIN },
619 { "placement", FT_PLACEMENT },
620 { "style", FT_STYLE },
624 lexrc.pushTable(floatTags, FT_END);
633 bool builtin = false;
636 while (!getout && lexrc.isOK()) {
637 int le = lexrc.lex();
639 case LyXLex::LEX_UNDEF:
640 lexrc.printError("Unknown ClassOption tag `$$Token'");
644 switch (static_cast<FloatTags>(le)) {
647 type = lexrc.getString();
648 // Here we could check if this type is already defined
649 // and modify it with the rest of the vars instead.
653 name = lexrc.getString();
657 placement = lexrc.getString();
661 ext = lexrc.getString();
665 within = lexrc.getString();
666 if (within == "none")
671 style = lexrc.getString();
675 listname = lexrc.getString();
679 builtin = lexrc.getBool();
687 // Here if have a full float if getout == true
689 Floating newfloat(type, placement, ext, within,
690 style, name, listname, builtin);
691 floatlist_->newFloat(newfloat);
704 void LyXTextClass::readCounter(LyXLex & lexrc)
706 keyword_item counterTags[] = {
709 { "within", CT_WITHIN }
712 lexrc.pushTable(counterTags, CT_END);
718 while (!getout && lexrc.isOK()) {
719 int le = lexrc.lex();
721 case LyXLex::LEX_UNDEF:
722 lexrc.printError("Unknown ClassOption tag `$$Token'");
726 switch (static_cast<CounterTags>(le)) {
729 name = lexrc.getString();
733 within = lexrc.getString();
734 if (within == "none")
743 // Here if have a full counter if getout == true
745 if (within.empty()) {
746 ctrs_->newCounter(name);
748 ctrs_->newCounter(name, within);
756 LyXFont const & LyXTextClass::defaultfont() const
762 string const & LyXTextClass::leftmargin() const
768 string const & LyXTextClass::rightmargin() const
774 bool LyXTextClass::hasLayout(string const & n) const
776 string const name = (n.empty() ? defaultLayoutName() : n);
778 return find_if(layoutlist_.begin(), layoutlist_.end(),
780 != layoutlist_.end();
785 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
787 BOOST_ASSERT(!name.empty());
789 LayoutList::const_iterator cit =
790 find_if(layoutlist_.begin(),
794 if (cit == layoutlist_.end()) {
795 lyxerr << "We failed to find the layout '" << name
796 << "' in the layout list. You MUST investigate!"
798 for (LayoutList::const_iterator it = layoutlist_.begin();
799 it != layoutlist_.end(); ++it)
800 lyxerr << " " << it->get()->name() << endl;
802 // we require the name to exist
811 bool LyXTextClass::delete_layout(string const & name)
813 if (name == defaultLayoutName())
816 LayoutList::iterator it =
817 remove_if(layoutlist_.begin(), layoutlist_.end(),
820 LayoutList::iterator end = layoutlist_.end();
821 bool const ret = (it != end);
822 layoutlist_.erase(it, end);
827 // Load textclass info if not loaded yet
828 bool LyXTextClass::load() const
834 string const real_file = LibFileSearch("layouts", name_, "layout");
836 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
837 lyxerr << "Error reading `"
838 << MakeDisplayPath(real_file)
839 << "'\n(Check `" << name_
840 << "')\nCheck your installation and "
841 "try Options/Reconfigure..." << endl;
849 FloatList & LyXTextClass::floats()
851 return *floatlist_.get();
855 FloatList const & LyXTextClass::floats() const
857 return *floatlist_.get();
861 Counters & LyXTextClass::counters() const
867 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
869 CharStyles::iterator cs = charstyles().begin();
870 CharStyles::iterator csend = charstyles().end();
871 for (; cs != csend; ++cs) {
879 string const & LyXTextClass::defaultLayoutName() const
881 // This really should come from the actual layout... (Lgb)
882 return defaultlayout_;
886 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
888 return operator[](defaultLayoutName());
892 string const & LyXTextClass::name() const
898 string const & LyXTextClass::latexname() const
900 const_cast<LyXTextClass*>(this)->load();
905 string const & LyXTextClass::description() const
911 string const & LyXTextClass::opt_fontsize() const
913 return opt_fontsize_;
917 string const & LyXTextClass::opt_pagestyle() const
919 return opt_pagestyle_;
923 string const & LyXTextClass::options() const
929 string const & LyXTextClass::pagestyle() const
935 string const & LyXTextClass::preamble() const
941 LyXTextClass::PageSides LyXTextClass::sides() const
947 int LyXTextClass::secnumdepth() const
953 int LyXTextClass::tocdepth() const
959 OutputType LyXTextClass::outputType() const
965 bool LyXTextClass::provides(LyXTextClass::Provides p) const
967 return provides_ & p;
971 unsigned int LyXTextClass::columns() const
977 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
983 string const & LyXTextClass::titlename() const
989 int LyXTextClass::size() const
991 return layoutlist_.size();
995 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
998 case LyXTextClass::OneSide:
1001 case LyXTextClass::TwoSides: