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"
28 using namespace lyx::support;
40 compare_name(string const & name)
44 bool operator()(boost::shared_ptr<LyXLayout> const & c)
46 return c->name() == name_;
56 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
57 string const & desc, bool texClassAvail )
58 : name_(fn), latexname_(cln), description_(desc),
59 floatlist_(new FloatList), ctrs_(new Counters), texClassAvail_(texClassAvail)
66 pagestyle_ = "default";
67 maxcounter_ = LABEL_COUNTER_CHAPTER;
68 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
69 opt_fontsize_ = "10|11|12";
70 opt_pagestyle_ = "empty|plain|headings|fancy";
72 titletype_ = TITLE_COMMAND_AFTER;
73 titlename_ = "maketitle";
78 bool LyXTextClass::isTeXClassAvailable() const
80 return texClassAvail_;
84 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
86 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
87 if (!lay.Read(lexrc, *this)) {
89 lay.resfont = lay.font;
90 lay.resfont.realize(defaultfont());
91 lay.reslabelfont = lay.labelfont;
92 lay.reslabelfont.realize(defaultfont());
93 return false; // no errors
95 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
129 // Reads a textclass structure from file.
130 bool LyXTextClass::Read(string const & filename, bool merge)
132 keyword_item textClassTags[] = {
133 { "classoptions", TC_CLASSOPTIONS },
134 { "columns", TC_COLUMNS },
135 { "counter", TC_COUNTER },
136 { "defaultfont", TC_DEFAULTFONT },
137 { "defaultstyle", TC_DEFAULTSTYLE },
138 { "environment", TC_ENVIRONMENT },
139 { "float", TC_FLOAT },
140 { "input", TC_INPUT },
141 { "leftmargin", TC_LEFTMARGIN },
142 { "maxcounter", TC_MAXCOUNTER },
143 { "nofloat", TC_NOFLOAT },
144 { "nostyle", TC_NOSTYLE },
145 { "outputtype", TC_OUTPUTTYPE },
146 { "pagestyle", TC_PAGESTYLE },
147 { "preamble", TC_PREAMBLE },
148 { "providesamsmath", TC_PROVIDESAMSMATH },
149 { "providesmakeidx", TC_PROVIDESMAKEIDX },
150 { "providesnatbib", TC_PROVIDESNATBIB },
151 { "providesurl", TC_PROVIDESURL },
152 { "rightmargin", TC_RIGHTMARGIN },
153 { "secnumdepth", TC_SECNUMDEPTH },
154 { "sides", TC_SIDES },
155 { "style", TC_STYLE },
156 { "titlelatexname", TC_TITLELATEXNAME },
157 { "titlelatextype", TC_TITLELATEXTYPE },
158 { "tocdepth", TC_TOCDEPTH }
162 lyxerr[Debug::TCLASS] << "Reading textclass "
163 << MakeDisplayPath(filename)
166 lyxerr[Debug::TCLASS] << "Reading input file "
167 << MakeDisplayPath(filename)
170 LyXLex lexrc(textClassTags,
171 sizeof(textClassTags) / sizeof(textClassTags[0]));
174 lexrc.setFile(filename);
175 if (!lexrc.isOK()) error = true;
178 while (lexrc.isOK() && !error) {
179 int le = lexrc.lex();
182 case LyXLex::LEX_FEOF:
185 case LyXLex::LEX_UNDEF:
186 lexrc.printError("Unknown TextClass tag `$$Token'");
194 switch (static_cast<TextClassTags>(le)) {
196 case TC_OUTPUTTYPE: // output type definition
197 readOutputType(lexrc);
200 case TC_INPUT: // Include file
202 string tmp = LibFileSearch("layouts",
206 if (Read(tmp, true)) {
207 lexrc.printError("Error reading input"
214 case TC_DEFAULTSTYLE:
216 string const name = subst(lexrc.getString(),
218 defaultlayout_ = name;
225 string const name = subst(lexrc.getString(),
227 if (hasLayout(name)) {
228 LyXLayout * lay = operator[](name).get();
229 error = do_readStyle(lexrc, *lay);
233 if (le == TC_ENVIRONMENT)
234 lay.is_environment = true;
235 if (!(error = do_readStyle(lexrc, lay)))
236 layoutlist_.push_back
237 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
238 if (defaultlayout_.empty()) {
239 // We do not have a default
240 // layout yet, so we choose
241 // the first layout we
243 defaultlayout_ = name;
248 lexrc.printError("No name given for style: `$$Token'.");
255 string const style = subst(lexrc.getString(),
257 if (!delete_layout(style))
258 lyxerr << "Cannot delete style `"
259 << style << '\'' << endl;
260 // lexrc.printError("Cannot delete style"
267 columns_ = lexrc.getInteger();
272 switch (lexrc.getInteger()) {
273 case 1: sides_ = OneSide; break;
274 case 2: sides_ = TwoSides; break;
276 lyxerr << "Impossible number of page"
277 " sides, setting to one."
287 pagestyle_ = rtrim(lexrc.getString());
291 defaultfont_.lyxRead(lexrc);
292 if (!defaultfont_.resolved()) {
293 lexrc.printError("Warning: defaultfont should "
294 "be fully instantiated!");
295 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
300 readMaxCounter(lexrc);
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();
357 case TC_TITLELATEXTYPE:
358 readTitleType(lexrc);
360 case TC_TITLELATEXNAME:
362 titlename_ = lexrc.getString();
366 string const nofloat = lexrc.getString();
367 floatlist_->erase(nofloat);
373 if (!merge) { // we are at top level here.
374 lyxerr[Debug::TCLASS] << "Finished reading textclass "
375 << MakeDisplayPath(filename)
377 if (defaultlayout_.empty()) {
378 lyxerr << "Error: Textclass '" << name_
379 << "' is missing a defaultstyle." << endl;
383 lyxerr[Debug::TCLASS] << "Finished reading input file "
384 << MakeDisplayPath(filename)
391 void LyXTextClass::readTitleType(LyXLex & lexrc)
393 keyword_item titleTypeTags[] = {
394 { "commandafter", TITLE_COMMAND_AFTER },
395 { "environment", TITLE_ENVIRONMENT }
398 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
400 int le = lexrc.lex();
402 case LyXLex::LEX_UNDEF:
403 lexrc.printError("Unknown output type `$$Token'");
405 case TITLE_COMMAND_AFTER:
406 case TITLE_ENVIRONMENT:
407 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
410 lyxerr << "Unhandled value " << le
411 << " in LyXTextClass::readTitleType." << endl;
418 void LyXTextClass::readOutputType(LyXLex & lexrc)
420 keyword_item outputTypeTags[] = {
421 { "docbook", DOCBOOK },
423 { "linuxdoc", LINUXDOC },
424 { "literate", LITERATE }
427 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
429 int le = lexrc.lex();
431 case LyXLex::LEX_UNDEF:
432 lexrc.printError("Unknown output type `$$Token'");
438 outputType_ = static_cast<OutputType>(le);
441 lyxerr << "Unhandled value " << le
442 << " in LyXTextClass::readOutputType." << endl;
449 enum MaxCounterTags {
450 MC_COUNTER_CHAPTER = 1,
452 MC_COUNTER_SUBSECTION,
453 MC_COUNTER_SUBSUBSECTION,
454 MC_COUNTER_PARAGRAPH,
455 MC_COUNTER_SUBPARAGRAPH,
463 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
465 keyword_item maxCounterTags[] = {
466 {"counter_chapter", MC_COUNTER_CHAPTER },
467 {"counter_enumi", MC_COUNTER_ENUMI },
468 {"counter_enumii", MC_COUNTER_ENUMII },
469 {"counter_enumiii", MC_COUNTER_ENUMIII },
470 {"counter_enumiv", MC_COUNTER_ENUMIV },
471 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
472 {"counter_section", MC_COUNTER_SECTION },
473 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
474 {"counter_subsection", MC_COUNTER_SUBSECTION },
475 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
478 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
480 int le = lexrc.lex();
482 case LyXLex::LEX_UNDEF:
483 lexrc.printError("Unknown MaxCounter tag `$$Token'");
489 switch (static_cast<MaxCounterTags>(le)) {
490 case MC_COUNTER_CHAPTER:
491 maxcounter_ = LABEL_COUNTER_CHAPTER;
493 case MC_COUNTER_SECTION:
494 maxcounter_ = LABEL_COUNTER_SECTION;
496 case MC_COUNTER_SUBSECTION:
497 maxcounter_ = LABEL_COUNTER_SUBSECTION;
499 case MC_COUNTER_SUBSUBSECTION:
500 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
502 case MC_COUNTER_PARAGRAPH:
503 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
505 case MC_COUNTER_SUBPARAGRAPH:
506 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
508 case MC_COUNTER_ENUMI:
509 maxcounter_ = LABEL_COUNTER_ENUMI;
511 case MC_COUNTER_ENUMII:
512 maxcounter_ = LABEL_COUNTER_ENUMII;
514 case MC_COUNTER_ENUMIII:
515 maxcounter_ = LABEL_COUNTER_ENUMIII;
517 case MC_COUNTER_ENUMIV:
518 maxcounter_ = LABEL_COUNTER_ENUMIV;
524 enum ClassOptionsTags {
532 void LyXTextClass::readClassOptions(LyXLex & lexrc)
534 keyword_item classOptionsTags[] = {
536 {"fontsize", CO_FONTSIZE },
537 {"other", CO_OTHER },
538 {"pagestyle", CO_PAGESTYLE }
541 lexrc.pushTable(classOptionsTags, CO_END);
543 while (!getout && lexrc.isOK()) {
544 int le = lexrc.lex();
546 case LyXLex::LEX_UNDEF:
547 lexrc.printError("Unknown ClassOption tag `$$Token'");
551 switch (static_cast<ClassOptionsTags>(le)) {
554 opt_fontsize_ = rtrim(lexrc.getString());
558 opt_pagestyle_ = rtrim(lexrc.getString());
562 options_ = lexrc.getString();
585 void LyXTextClass::readFloat(LyXLex & lexrc)
587 keyword_item floatTags[] = {
589 { "extension", FT_EXT },
590 { "guiname", FT_NAME },
591 { "latexbuiltin", FT_BUILTIN },
592 { "listname", FT_LISTNAME },
593 { "numberwithin", FT_WITHIN },
594 { "placement", FT_PLACEMENT },
595 { "style", FT_STYLE },
599 lexrc.pushTable(floatTags, FT_END);
608 bool builtin = false;
611 while (!getout && lexrc.isOK()) {
612 int le = lexrc.lex();
614 case LyXLex::LEX_UNDEF:
615 lexrc.printError("Unknown ClassOption tag `$$Token'");
619 switch (static_cast<FloatTags>(le)) {
622 type = lexrc.getString();
623 // Here we could check if this type is already defined
624 // and modify it with the rest of the vars instead.
628 name = lexrc.getString();
632 placement = lexrc.getString();
636 ext = lexrc.getString();
640 within = lexrc.getString();
641 if (within == "none")
646 style = lexrc.getString();
650 listname = lexrc.getString();
654 builtin = lexrc.getBool();
662 // Here if have a full float if getout == true
664 Floating newfloat(type, placement, ext, within,
665 style, name, listname, builtin);
666 floatlist_->newFloat(newfloat);
679 void LyXTextClass::readCounter(LyXLex & lexrc)
681 keyword_item counterTags[] = {
684 { "within", CT_WITHIN }
687 lexrc.pushTable(counterTags, CT_END);
693 while (!getout && lexrc.isOK()) {
694 int le = lexrc.lex();
696 case LyXLex::LEX_UNDEF:
697 lexrc.printError("Unknown ClassOption tag `$$Token'");
701 switch (static_cast<CounterTags>(le)) {
704 name = lexrc.getString();
708 within = lexrc.getString();
709 if (within == "none")
718 // Here if have a full float if getout == true
720 if (within.empty()) {
721 ctrs_->newCounter(name);
723 ctrs_->newCounter(name, within);
731 LyXFont const & LyXTextClass::defaultfont() const
737 string const & LyXTextClass::leftmargin() const
743 string const & LyXTextClass::rightmargin() const
749 bool LyXTextClass::hasLayout(string const & n) const
751 string const name = (n.empty() ? defaultLayoutName() : n);
753 return find_if(layoutlist_.begin(), layoutlist_.end(),
755 != layoutlist_.end();
760 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
762 BOOST_ASSERT(!name.empty());
764 LayoutList::const_iterator cit =
765 find_if(layoutlist_.begin(),
769 if (cit == layoutlist_.end()) {
770 lyxerr << "We failed to find the layout '" << name
771 << "' in the layout list. You MUST investigate!"
773 for (LayoutList::const_iterator it = layoutlist_.begin();
774 it != layoutlist_.end(); ++it)
775 lyxerr << " " << it->get()->name() << endl;
777 // we require the name to exist
786 bool LyXTextClass::delete_layout(string const & name)
788 if (name == defaultLayoutName())
791 LayoutList::iterator it =
792 remove_if(layoutlist_.begin(), layoutlist_.end(),
795 LayoutList::iterator end = layoutlist_.end();
796 bool const ret = (it != end);
797 layoutlist_.erase(it, end);
802 // Load textclass info if not loaded yet
803 bool LyXTextClass::load() const
809 string const real_file = LibFileSearch("layouts", name_, "layout");
811 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
812 lyxerr << "Error reading `"
813 << MakeDisplayPath(real_file)
814 << "'\n(Check `" << name_
815 << "')\nCheck your installation and "
816 "try Options/Reconfigure..." << endl;
824 FloatList & LyXTextClass::floats()
826 return *floatlist_.get();
830 FloatList const & LyXTextClass::floats() const
832 return *floatlist_.get();
836 Counters & LyXTextClass::counters() const
842 string const & LyXTextClass::defaultLayoutName() const
844 // This really should come from the actual layout... (Lgb)
845 return defaultlayout_;
849 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
851 return operator[](defaultLayoutName());
855 string const & LyXTextClass::name() const
861 string const & LyXTextClass::latexname() const
863 const_cast<LyXTextClass*>(this)->load();
868 string const & LyXTextClass::description() const
874 string const & LyXTextClass::opt_fontsize() const
876 return opt_fontsize_;
880 string const & LyXTextClass::opt_pagestyle() const
882 return opt_pagestyle_;
886 string const & LyXTextClass::options() const
892 string const & LyXTextClass::pagestyle() const
898 string const & LyXTextClass::preamble() const
904 LyXTextClass::PageSides LyXTextClass::sides() const
910 int LyXTextClass::secnumdepth() const
916 int LyXTextClass::tocdepth() const
922 OutputType LyXTextClass::outputType() const
928 bool LyXTextClass::provides(LyXTextClass::Provides p) const
930 return provides_ & p;
934 unsigned int LyXTextClass::columns() const
940 int LyXTextClass::maxcounter() const
946 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
952 string const & LyXTextClass::titlename() const
961 int LyXTextClass::size() const
963 return layoutlist_.size();
967 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
970 case LyXTextClass::OneSide:
973 case LyXTextClass::TwoSides: