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 maxcounter_ = LABEL_COUNTER_CHAPTER;
71 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
72 opt_fontsize_ = "10|11|12";
73 opt_pagestyle_ = "empty|plain|headings|fancy";
75 titletype_ = TITLE_COMMAND_AFTER;
76 titlename_ = "maketitle";
81 bool LyXTextClass::isTeXClassAvailable() const
83 return texClassAvail_;
87 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
89 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
90 if (!lay.Read(lexrc, *this)) {
92 lay.resfont = lay.font;
93 lay.resfont.realize(defaultfont());
94 lay.reslabelfont = lay.labelfont;
95 lay.reslabelfont.realize(defaultfont());
96 return false; // no errors
98 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
132 // Reads a textclass structure from file.
133 bool LyXTextClass::Read(string const & filename, bool merge)
135 keyword_item textClassTags[] = {
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 { "maxcounter", TC_MAXCOUNTER },
146 { "nofloat", TC_NOFLOAT },
147 { "nostyle", TC_NOSTYLE },
148 { "outputtype", TC_OUTPUTTYPE },
149 { "pagestyle", TC_PAGESTYLE },
150 { "preamble", TC_PREAMBLE },
151 { "providesamsmath", TC_PROVIDESAMSMATH },
152 { "providesmakeidx", TC_PROVIDESMAKEIDX },
153 { "providesnatbib", TC_PROVIDESNATBIB },
154 { "providesurl", TC_PROVIDESURL },
155 { "rightmargin", TC_RIGHTMARGIN },
156 { "secnumdepth", TC_SECNUMDEPTH },
157 { "sides", TC_SIDES },
158 { "style", TC_STYLE },
159 { "titlelatexname", TC_TITLELATEXNAME },
160 { "titlelatextype", TC_TITLELATEXTYPE },
161 { "tocdepth", TC_TOCDEPTH }
165 lyxerr[Debug::TCLASS] << "Reading textclass "
166 << MakeDisplayPath(filename)
169 lyxerr[Debug::TCLASS] << "Reading input file "
170 << MakeDisplayPath(filename)
173 LyXLex lexrc(textClassTags,
174 sizeof(textClassTags) / sizeof(textClassTags[0]));
177 lexrc.setFile(filename);
178 if (!lexrc.isOK()) error = true;
181 while (lexrc.isOK() && !error) {
182 int le = lexrc.lex();
185 case LyXLex::LEX_FEOF:
188 case LyXLex::LEX_UNDEF:
189 lexrc.printError("Unknown TextClass tag `$$Token'");
197 switch (static_cast<TextClassTags>(le)) {
199 case TC_OUTPUTTYPE: // output type definition
200 readOutputType(lexrc);
203 case TC_INPUT: // Include file
205 string tmp = LibFileSearch("layouts",
209 if (Read(tmp, true)) {
210 lexrc.printError("Error reading input"
217 case TC_DEFAULTSTYLE:
219 string const name = subst(lexrc.getString(),
221 defaultlayout_ = name;
228 string const name = subst(lexrc.getString(),
230 if (hasLayout(name)) {
231 LyXLayout * lay = operator[](name).get();
232 error = do_readStyle(lexrc, *lay);
236 if (le == TC_ENVIRONMENT)
237 lay.is_environment = true;
238 if (!(error = do_readStyle(lexrc, lay)))
239 layoutlist_.push_back
240 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
241 if (defaultlayout_.empty()) {
242 // We do not have a default
243 // layout yet, so we choose
244 // the first layout we
246 defaultlayout_ = name;
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);
483 int le = lexrc.lex();
485 case LyXLex::LEX_UNDEF:
486 lexrc.printError("Unknown MaxCounter tag `$$Token'");
492 switch (static_cast<MaxCounterTags>(le)) {
493 case MC_COUNTER_CHAPTER:
494 maxcounter_ = LABEL_COUNTER_CHAPTER;
496 case MC_COUNTER_SECTION:
497 maxcounter_ = LABEL_COUNTER_SECTION;
499 case MC_COUNTER_SUBSECTION:
500 maxcounter_ = LABEL_COUNTER_SUBSECTION;
502 case MC_COUNTER_SUBSUBSECTION:
503 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
505 case MC_COUNTER_PARAGRAPH:
506 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
508 case MC_COUNTER_SUBPARAGRAPH:
509 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
511 case MC_COUNTER_ENUMI:
512 maxcounter_ = LABEL_COUNTER_ENUMI;
514 case MC_COUNTER_ENUMII:
515 maxcounter_ = LABEL_COUNTER_ENUMII;
517 case MC_COUNTER_ENUMIII:
518 maxcounter_ = LABEL_COUNTER_ENUMIII;
520 case MC_COUNTER_ENUMIV:
521 maxcounter_ = LABEL_COUNTER_ENUMIV;
527 enum ClassOptionsTags {
535 void LyXTextClass::readClassOptions(LyXLex & lexrc)
537 keyword_item classOptionsTags[] = {
539 {"fontsize", CO_FONTSIZE },
540 {"other", CO_OTHER },
541 {"pagestyle", CO_PAGESTYLE }
544 lexrc.pushTable(classOptionsTags, CO_END);
546 while (!getout && lexrc.isOK()) {
547 int le = lexrc.lex();
549 case LyXLex::LEX_UNDEF:
550 lexrc.printError("Unknown ClassOption tag `$$Token'");
554 switch (static_cast<ClassOptionsTags>(le)) {
557 opt_fontsize_ = rtrim(lexrc.getString());
561 opt_pagestyle_ = rtrim(lexrc.getString());
565 options_ = lexrc.getString();
588 void LyXTextClass::readFloat(LyXLex & lexrc)
590 keyword_item floatTags[] = {
592 { "extension", FT_EXT },
593 { "guiname", FT_NAME },
594 { "latexbuiltin", FT_BUILTIN },
595 { "listname", FT_LISTNAME },
596 { "numberwithin", FT_WITHIN },
597 { "placement", FT_PLACEMENT },
598 { "style", FT_STYLE },
602 lexrc.pushTable(floatTags, FT_END);
611 bool builtin = false;
614 while (!getout && lexrc.isOK()) {
615 int le = lexrc.lex();
617 case LyXLex::LEX_UNDEF:
618 lexrc.printError("Unknown ClassOption tag `$$Token'");
622 switch (static_cast<FloatTags>(le)) {
625 type = lexrc.getString();
626 // Here we could check if this type is already defined
627 // and modify it with the rest of the vars instead.
631 name = lexrc.getString();
635 placement = lexrc.getString();
639 ext = lexrc.getString();
643 within = lexrc.getString();
644 if (within == "none")
649 style = lexrc.getString();
653 listname = lexrc.getString();
657 builtin = lexrc.getBool();
665 // Here if have a full float if getout == true
667 Floating newfloat(type, placement, ext, within,
668 style, name, listname, builtin);
669 floatlist_->newFloat(newfloat);
682 void LyXTextClass::readCounter(LyXLex & lexrc)
684 keyword_item counterTags[] = {
687 { "within", CT_WITHIN }
690 lexrc.pushTable(counterTags, CT_END);
696 while (!getout && lexrc.isOK()) {
697 int le = lexrc.lex();
699 case LyXLex::LEX_UNDEF:
700 lexrc.printError("Unknown ClassOption tag `$$Token'");
704 switch (static_cast<CounterTags>(le)) {
707 name = lexrc.getString();
711 within = lexrc.getString();
712 if (within == "none")
721 // Here if have a full float if getout == true
723 if (within.empty()) {
724 ctrs_->newCounter(name);
726 ctrs_->newCounter(name, within);
734 LyXFont const & LyXTextClass::defaultfont() const
740 string const & LyXTextClass::leftmargin() const
746 string const & LyXTextClass::rightmargin() const
752 bool LyXTextClass::hasLayout(string const & n) const
754 string const name = (n.empty() ? defaultLayoutName() : n);
756 return find_if(layoutlist_.begin(), layoutlist_.end(),
758 != layoutlist_.end();
763 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
765 BOOST_ASSERT(!name.empty());
767 LayoutList::const_iterator cit =
768 find_if(layoutlist_.begin(),
772 if (cit == layoutlist_.end()) {
773 lyxerr << "We failed to find the layout '" << name
774 << "' in the layout list. You MUST investigate!"
776 for (LayoutList::const_iterator it = layoutlist_.begin();
777 it != layoutlist_.end(); ++it)
778 lyxerr << " " << it->get()->name() << endl;
780 // we require the name to exist
789 bool LyXTextClass::delete_layout(string const & name)
791 if (name == defaultLayoutName())
794 LayoutList::iterator it =
795 remove_if(layoutlist_.begin(), layoutlist_.end(),
798 LayoutList::iterator end = layoutlist_.end();
799 bool const ret = (it != end);
800 layoutlist_.erase(it, end);
805 // Load textclass info if not loaded yet
806 bool LyXTextClass::load() const
812 string const real_file = LibFileSearch("layouts", name_, "layout");
814 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
815 lyxerr << "Error reading `"
816 << MakeDisplayPath(real_file)
817 << "'\n(Check `" << name_
818 << "')\nCheck your installation and "
819 "try Options/Reconfigure..." << endl;
827 FloatList & LyXTextClass::floats()
829 return *floatlist_.get();
833 FloatList const & LyXTextClass::floats() const
835 return *floatlist_.get();
839 Counters & LyXTextClass::counters() const
845 string const & LyXTextClass::defaultLayoutName() const
847 // This really should come from the actual layout... (Lgb)
848 return defaultlayout_;
852 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
854 return operator[](defaultLayoutName());
858 string const & LyXTextClass::name() const
864 string const & LyXTextClass::latexname() const
866 const_cast<LyXTextClass*>(this)->load();
871 string const & LyXTextClass::description() const
877 string const & LyXTextClass::opt_fontsize() const
879 return opt_fontsize_;
883 string const & LyXTextClass::opt_pagestyle() const
885 return opt_pagestyle_;
889 string const & LyXTextClass::options() const
895 string const & LyXTextClass::pagestyle() const
901 string const & LyXTextClass::preamble() const
907 LyXTextClass::PageSides LyXTextClass::sides() const
913 int LyXTextClass::secnumdepth() const
919 int LyXTextClass::tocdepth() const
925 OutputType LyXTextClass::outputType() const
931 bool LyXTextClass::provides(LyXTextClass::Provides p) const
933 return provides_ & p;
937 unsigned int LyXTextClass::columns() const
943 int LyXTextClass::maxcounter() const
949 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
955 string const & LyXTextClass::titlename() const
964 int LyXTextClass::size() const
966 return layoutlist_.size();
970 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
973 case LyXTextClass::OneSide:
976 case LyXTextClass::TwoSides: