1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ======================================================
14 #include "lyxtextclass.h"
18 #include "FloatList.h"
20 #include "support/lstrings.h"
21 #include "support/LAssert.h"
22 #include "support/lyxfunctional.h"
23 #include "support/filetools.h"
36 compare_name(string const & name)
40 bool operator()(boost::shared_ptr<LyXLayout> const & c)
42 //lyxerr << "comparing '" << name_ << "' to '" << c->name() << "'\n";
43 return c->name() == name_;
53 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
55 : name_(fn), latexname_(cln), description_(desc),
56 floatlist_(new FloatList), ctrs_(new Counters)
63 pagestyle_ = "default";
64 maxcounter_ = LABEL_COUNTER_CHAPTER;
65 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
66 opt_fontsize_ = "10|11|12";
67 opt_pagestyle_ = "empty|plain|headings|fancy";
69 titletype_ = TITLE_COMMAND_AFTER;
70 titlename_ = "maketitle";
75 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
77 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
78 if (!lay.Read(lexrc, *this)) {
80 lay.resfont = lay.font;
81 lay.resfont.realize(defaultfont());
82 lay.reslabelfont = lay.labelfont;
83 lay.reslabelfont.realize(defaultfont());
84 return false; // no errors
86 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
120 // Reads a textclass structure from file.
121 bool LyXTextClass::Read(string const & filename, bool merge)
123 keyword_item textClassTags[] = {
124 { "classoptions", TC_CLASSOPTIONS },
125 { "columns", TC_COLUMNS },
126 { "counter", TC_COUNTER },
127 { "defaultfont", TC_DEFAULTFONT },
128 { "defaultstyle", TC_DEFAULTSTYLE },
129 { "float", TC_FLOAT },
130 { "input", TC_INPUT },
131 { "leftmargin", TC_LEFTMARGIN },
132 { "maxcounter", TC_MAXCOUNTER },
133 { "nofloat", TC_NOFLOAT },
134 { "nostyle", TC_NOSTYLE },
135 { "outputtype", TC_OUTPUTTYPE },
136 { "pagestyle", TC_PAGESTYLE },
137 { "preamble", TC_PREAMBLE },
138 { "providesamsmath", TC_PROVIDESAMSMATH },
139 { "providesmakeidx", TC_PROVIDESMAKEIDX },
140 { "providesnatbib", TC_PROVIDESNATBIB },
141 { "providesurl", TC_PROVIDESURL },
142 { "rightmargin", TC_RIGHTMARGIN },
143 { "secnumdepth", TC_SECNUMDEPTH },
144 { "sides", TC_SIDES },
145 { "style", TC_STYLE },
146 { "titlelatexname", TC_TITLELATEXNAME },
147 { "titlelatextype", TC_TITLELATEXTYPE },
148 { "tocdepth", TC_TOCDEPTH }
152 lyxerr[Debug::TCLASS] << "Reading textclass "
153 << MakeDisplayPath(filename)
156 lyxerr[Debug::TCLASS] << "Reading input file "
157 << MakeDisplayPath(filename)
160 LyXLex lexrc(textClassTags, TC_TITLELATEXTYPE);
163 lexrc.setFile(filename);
164 if (!lexrc.isOK()) error = true;
167 while (lexrc.isOK() && !error) {
168 int le = lexrc.lex();
170 case LyXLex::LEX_FEOF:
173 case LyXLex::LEX_UNDEF:
174 lexrc.printError("Unknown TextClass tag `$$Token'");
179 switch (static_cast<TextClassTags>(le)) {
180 case TC_OUTPUTTYPE: // output type definition
181 readOutputType(lexrc);
184 case TC_INPUT: // Include file
186 string tmp = LibFileSearch("layouts",
190 if (Read(tmp, true)) {
191 lexrc.printError("Error reading input"
198 case TC_DEFAULTSTYLE:
200 string const name = subst(lexrc.getString(),
202 defaultlayout_ = name;
208 string const name = subst(lexrc.getString(),
210 if (hasLayout(name)) {
211 LyXLayout * lay = operator[](name).get();
212 error = do_readStyle(lexrc, *lay);
216 if (!(error = do_readStyle(lexrc, lay)))
217 layoutlist_.push_back(boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
218 if (defaultlayout_.empty()) {
219 // We do not have a default
220 // layout yet, so we choose
221 // the first layout we
223 defaultlayout_ = name;
228 lexrc.printError("No name given for style: `$$Token'.");
235 string const name = subst(lexrc.getString(),
237 if (hasLayout(name)) {
238 LyXLayout * lay = operator[](name).get();
239 error = do_readStyle(lexrc, *lay);
243 if (!(error = do_readStyle(lexrc, lay)))
245 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
247 lexrc.printError("Problems reading environment: `$$Token'.");
251 lexrc.printError("No name given for style: `$$Token'.");
258 string const style = subst(lexrc.getString(),
260 if (!delete_layout(style))
261 lyxerr << "Cannot delete style `"
262 << style << '\'' << endl;
263 // lexrc.printError("Cannot delete style"
270 columns_ = lexrc.getInteger();
275 switch (lexrc.getInteger()) {
276 case 1: sides_ = OneSide; break;
277 case 2: sides_ = TwoSides; break;
279 lyxerr << "Impossible number of page"
280 " sides, setting to one."
290 pagestyle_ = rtrim(lexrc.getString());
294 defaultfont_.lyxRead(lexrc);
295 if (!defaultfont_.resolved()) {
296 lexrc.printError("Warning: defaultfont should "
297 "be fully instantiated!");
298 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
303 readMaxCounter(lexrc);
308 secnumdepth_ = lexrc.getInteger();
313 tocdepth_ = lexrc.getInteger();
316 // First step to support options
317 case TC_CLASSOPTIONS:
318 readClassOptions(lexrc);
322 preamble_ = lexrc.getLongString("EndPreamble");
325 case TC_PROVIDESAMSMATH:
326 if (lexrc.next() && lexrc.getInteger())
327 provides_ |= amsmath;
330 case TC_PROVIDESNATBIB:
331 if (lexrc.next() && lexrc.getInteger())
335 case TC_PROVIDESMAKEIDX:
336 if (lexrc.next() && lexrc.getInteger())
337 provides_ |= makeidx;
341 if (lexrc.next() && lexrc.getInteger())
345 case TC_LEFTMARGIN: // left margin type
347 leftmargin_ = lexrc.getString();
350 case TC_RIGHTMARGIN: // right margin type
352 rightmargin_ = lexrc.getString();
360 case TC_TITLELATEXTYPE:
361 readTitleType(lexrc);
363 case TC_TITLELATEXNAME:
365 titlename_ = lexrc.getString();
369 string const nofloat = lexrc.getString();
370 floatlist_->erase(nofloat);
376 if (!merge) { // we are at top level here.
377 lyxerr[Debug::TCLASS] << "Finished reading textclass "
378 << MakeDisplayPath(filename)
380 if (defaultlayout_.empty()) {
381 lyxerr << "Error: Textclass '" << name_
382 << "' is missing a defaultstyle." << endl;
386 lyxerr[Debug::TCLASS] << "Finished reading input file "
387 << MakeDisplayPath(filename)
394 void LyXTextClass::readTitleType(LyXLex & lexrc)
396 keyword_item titleTypeTags[] = {
397 { "commandafter", TITLE_COMMAND_AFTER },
398 { "environment", TITLE_ENVIRONMENT }
401 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
403 int le = lexrc.lex();
405 case LyXLex::LEX_UNDEF:
406 lexrc.printError("Unknown output type `$$Token'");
408 case TITLE_COMMAND_AFTER:
409 case TITLE_ENVIRONMENT:
410 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
413 lyxerr << "Unhandled value " << le
414 << " in LyXTextClass::readTitleType." << endl;
421 void LyXTextClass::readOutputType(LyXLex & lexrc)
423 keyword_item outputTypeTags[] = {
424 { "docbook", DOCBOOK },
426 { "linuxdoc", LINUXDOC },
427 { "literate", LITERATE }
430 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
432 int le = lexrc.lex();
434 case LyXLex::LEX_UNDEF:
435 lexrc.printError("Unknown output type `$$Token'");
441 outputType_ = static_cast<OutputType>(le);
444 lyxerr << "Unhandled value " << le
445 << " in LyXTextClass::readOutputType." << endl;
452 enum MaxCounterTags {
453 MC_COUNTER_CHAPTER = 1,
455 MC_COUNTER_SUBSECTION,
456 MC_COUNTER_SUBSUBSECTION,
457 MC_COUNTER_PARAGRAPH,
458 MC_COUNTER_SUBPARAGRAPH,
466 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
468 keyword_item maxCounterTags[] = {
469 {"counter_chapter", MC_COUNTER_CHAPTER },
470 {"counter_enumi", MC_COUNTER_ENUMI },
471 {"counter_enumii", MC_COUNTER_ENUMII },
472 {"counter_enumiii", MC_COUNTER_ENUMIII },
473 {"counter_enumiv", MC_COUNTER_ENUMIV },
474 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
475 {"counter_section", MC_COUNTER_SECTION },
476 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
477 {"counter_subsection", MC_COUNTER_SUBSECTION },
478 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
481 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
482 int le = lexrc.lex();
484 case LyXLex::LEX_UNDEF:
485 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();
759 LyXLayout_ptr const & LyXTextClass::operator[](string const & n) const
761 lyx::Assert(!n.empty());
764 lyxerr << "LyXTextClass::operator[] called with empty n" << endl;
766 string const name = (n.empty() ? defaultLayoutName() : n);
768 static string lastLayoutName;
769 static LayoutList::difference_type lastLayoutIndex;
771 if (name == lastLayoutName)
772 return layoutlist_[lastLayoutIndex];
774 LayoutList::const_iterator cit =
775 find_if(layoutlist_.begin(),
779 if (cit == layoutlist_.end()) {
780 lyxerr << "We failed to find the layout '" << name
781 << "' in the layout list. You MUST investigate!"
784 // we require the name to exist
788 lastLayoutName = name;
789 lastLayoutIndex = std::distance(layoutlist_.begin(), cit);
795 LyXLayout_ptr const & LyXTextClass::getEnv(string const & name) const
797 lyx::Assert(!name.empty());
800 lyxerr << "LyXTextClass::getEnv() called with empty n" << endl;
802 LayoutList::const_iterator cit =
803 find_if(envlist_.begin(), envlist_.end(), compare_name(name));
805 if (cit == envlist_.end()) {
806 lyxerr << "We failed to find the environment '" << name
807 << "' in the layout list. You MUST investigate!"
809 // we require the name to exist
817 bool LyXTextClass::delete_layout(string const & name)
819 if (name == defaultLayoutName())
822 LayoutList::iterator it =
823 remove_if(layoutlist_.begin(), layoutlist_.end(),
826 LayoutList::iterator end = layoutlist_.end();
827 bool const ret = (it != end);
828 layoutlist_.erase(it, end);
833 // Load textclass info if not loaded yet
834 bool LyXTextClass::load() const
840 string const real_file = LibFileSearch("layouts", name_, "layout");
842 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
843 lyxerr << "Error reading `"
844 << MakeDisplayPath(real_file)
845 << "'\n(Check `" << name_
846 << "')\nCheck your installation and "
847 "try Options/Reconfigure..." << endl;
855 FloatList & LyXTextClass::floats()
857 return *floatlist_.get();
861 FloatList const & LyXTextClass::floats() const
863 return *floatlist_.get();
867 Counters & LyXTextClass::counters() const
873 string const & LyXTextClass::defaultLayoutName() const
875 // This really should come from the actual layout... (Lgb)
876 return defaultlayout_;
880 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
882 return operator[](defaultLayoutName());
886 string const & LyXTextClass::name() const
892 string const & LyXTextClass::latexname() const
894 const_cast<LyXTextClass*>(this)->load();
899 string const & LyXTextClass::description() const
905 string const & LyXTextClass::opt_fontsize() const
907 return opt_fontsize_;
911 string const & LyXTextClass::opt_pagestyle() const
913 return opt_pagestyle_;
917 string const & LyXTextClass::options() const
923 string const & LyXTextClass::pagestyle() const
929 string const & LyXTextClass::preamble() const
935 LyXTextClass::PageSides LyXTextClass::sides() const
941 int LyXTextClass::secnumdepth() const
947 int LyXTextClass::tocdepth() const
953 OutputType LyXTextClass::outputType() const
959 bool LyXTextClass::provides(LyXTextClass::Provides p) const
961 return provides_ & p;
965 unsigned int LyXTextClass::columns() const
971 int LyXTextClass::maxcounter() const
977 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
983 string const & LyXTextClass::titlename() const
992 int LyXTextClass::size() const
994 return layoutlist_.size();
998 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1001 case LyXTextClass::OneSide:
1004 case LyXTextClass::TwoSides: