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"
21 #include "FloatList.h"
23 #include "support/lstrings.h"
24 #include "support/LAssert.h"
25 #include "support/lyxfunctional.h"
26 #include "support/filetools.h"
30 using namespace lyx::support;
41 compare_name(string const & name)
45 bool operator()(boost::shared_ptr<LyXLayout> const & c)
47 return c->name() == name_;
57 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
58 string const & desc, bool texClassAvail )
59 : name_(fn), latexname_(cln), description_(desc),
60 floatlist_(new FloatList), ctrs_(new Counters), texClassAvail_(texClassAvail)
67 pagestyle_ = "default";
68 maxcounter_ = LABEL_COUNTER_CHAPTER;
69 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
70 opt_fontsize_ = "10|11|12";
71 opt_pagestyle_ = "empty|plain|headings|fancy";
73 titletype_ = TITLE_COMMAND_AFTER;
74 titlename_ = "maketitle";
79 bool LyXTextClass::isTeXClassAvailable() const
81 return texClassAvail_;
85 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
87 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
88 if (!lay.Read(lexrc, *this)) {
90 lay.resfont = lay.font;
91 lay.resfont.realize(defaultfont());
92 lay.reslabelfont = lay.labelfont;
93 lay.reslabelfont.realize(defaultfont());
94 return false; // no errors
96 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
130 // Reads a textclass structure from file.
131 bool LyXTextClass::Read(string const & filename, bool merge)
133 keyword_item textClassTags[] = {
134 { "classoptions", TC_CLASSOPTIONS },
135 { "columns", TC_COLUMNS },
136 { "counter", TC_COUNTER },
137 { "defaultfont", TC_DEFAULTFONT },
138 { "defaultstyle", TC_DEFAULTSTYLE },
139 { "environment", TC_ENVIRONMENT },
140 { "float", TC_FLOAT },
141 { "input", TC_INPUT },
142 { "leftmargin", TC_LEFTMARGIN },
143 { "maxcounter", TC_MAXCOUNTER },
144 { "nofloat", TC_NOFLOAT },
145 { "nostyle", TC_NOSTYLE },
146 { "outputtype", TC_OUTPUTTYPE },
147 { "pagestyle", TC_PAGESTYLE },
148 { "preamble", TC_PREAMBLE },
149 { "providesamsmath", TC_PROVIDESAMSMATH },
150 { "providesmakeidx", TC_PROVIDESMAKEIDX },
151 { "providesnatbib", TC_PROVIDESNATBIB },
152 { "providesurl", TC_PROVIDESURL },
153 { "rightmargin", TC_RIGHTMARGIN },
154 { "secnumdepth", TC_SECNUMDEPTH },
155 { "sides", TC_SIDES },
156 { "style", TC_STYLE },
157 { "titlelatexname", TC_TITLELATEXNAME },
158 { "titlelatextype", TC_TITLELATEXTYPE },
159 { "tocdepth", TC_TOCDEPTH }
163 lyxerr[Debug::TCLASS] << "Reading textclass "
164 << MakeDisplayPath(filename)
167 lyxerr[Debug::TCLASS] << "Reading input file "
168 << MakeDisplayPath(filename)
171 LyXLex lexrc(textClassTags,
172 sizeof(textClassTags) / sizeof(textClassTags[0]));
175 lexrc.setFile(filename);
176 if (!lexrc.isOK()) error = true;
179 while (lexrc.isOK() && !error) {
180 int le = lexrc.lex();
183 case LyXLex::LEX_FEOF:
186 case LyXLex::LEX_UNDEF:
187 lexrc.printError("Unknown TextClass tag `$$Token'");
195 switch (static_cast<TextClassTags>(le)) {
197 case TC_OUTPUTTYPE: // output type definition
198 readOutputType(lexrc);
201 case TC_INPUT: // Include file
203 string tmp = LibFileSearch("layouts",
207 if (Read(tmp, true)) {
208 lexrc.printError("Error reading input"
215 case TC_DEFAULTSTYLE:
217 string const name = subst(lexrc.getString(),
219 defaultlayout_ = name;
226 string const name = subst(lexrc.getString(),
228 if (hasLayout(name)) {
229 LyXLayout * lay = operator[](name).get();
230 error = do_readStyle(lexrc, *lay);
234 if (le == TC_ENVIRONMENT)
235 lay.is_environment = true;
236 if (!(error = do_readStyle(lexrc, lay)))
237 layoutlist_.push_back
238 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
239 if (defaultlayout_.empty()) {
240 // We do not have a default
241 // layout yet, so we choose
242 // the first layout we
244 defaultlayout_ = name;
249 lexrc.printError("No name given for style: `$$Token'.");
256 string const style = subst(lexrc.getString(),
258 if (!delete_layout(style))
259 lyxerr << "Cannot delete style `"
260 << style << '\'' << endl;
261 // lexrc.printError("Cannot delete style"
268 columns_ = lexrc.getInteger();
273 switch (lexrc.getInteger()) {
274 case 1: sides_ = OneSide; break;
275 case 2: sides_ = TwoSides; break;
277 lyxerr << "Impossible number of page"
278 " sides, setting to one."
288 pagestyle_ = rtrim(lexrc.getString());
292 defaultfont_.lyxRead(lexrc);
293 if (!defaultfont_.resolved()) {
294 lexrc.printError("Warning: defaultfont should "
295 "be fully instantiated!");
296 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
301 readMaxCounter(lexrc);
306 secnumdepth_ = lexrc.getInteger();
311 tocdepth_ = lexrc.getInteger();
314 // First step to support options
315 case TC_CLASSOPTIONS:
316 readClassOptions(lexrc);
320 preamble_ = lexrc.getLongString("EndPreamble");
323 case TC_PROVIDESAMSMATH:
324 if (lexrc.next() && lexrc.getInteger())
325 provides_ |= amsmath;
328 case TC_PROVIDESNATBIB:
329 if (lexrc.next() && lexrc.getInteger())
333 case TC_PROVIDESMAKEIDX:
334 if (lexrc.next() && lexrc.getInteger())
335 provides_ |= makeidx;
339 if (lexrc.next() && lexrc.getInteger())
343 case TC_LEFTMARGIN: // left margin type
345 leftmargin_ = lexrc.getString();
348 case TC_RIGHTMARGIN: // right margin type
350 rightmargin_ = lexrc.getString();
358 case TC_TITLELATEXTYPE:
359 readTitleType(lexrc);
361 case TC_TITLELATEXNAME:
363 titlename_ = lexrc.getString();
367 string const nofloat = lexrc.getString();
368 floatlist_->erase(nofloat);
374 if (!merge) { // we are at top level here.
375 lyxerr[Debug::TCLASS] << "Finished reading textclass "
376 << MakeDisplayPath(filename)
378 if (defaultlayout_.empty()) {
379 lyxerr << "Error: Textclass '" << name_
380 << "' is missing a defaultstyle." << endl;
384 lyxerr[Debug::TCLASS] << "Finished reading input file "
385 << MakeDisplayPath(filename)
392 void LyXTextClass::readTitleType(LyXLex & lexrc)
394 keyword_item titleTypeTags[] = {
395 { "commandafter", TITLE_COMMAND_AFTER },
396 { "environment", TITLE_ENVIRONMENT }
399 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
401 int le = lexrc.lex();
403 case LyXLex::LEX_UNDEF:
404 lexrc.printError("Unknown output type `$$Token'");
406 case TITLE_COMMAND_AFTER:
407 case TITLE_ENVIRONMENT:
408 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
411 lyxerr << "Unhandled value " << le
412 << " in LyXTextClass::readTitleType." << endl;
419 void LyXTextClass::readOutputType(LyXLex & lexrc)
421 keyword_item outputTypeTags[] = {
422 { "docbook", DOCBOOK },
424 { "linuxdoc", LINUXDOC },
425 { "literate", LITERATE }
428 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
430 int le = lexrc.lex();
432 case LyXLex::LEX_UNDEF:
433 lexrc.printError("Unknown output type `$$Token'");
439 outputType_ = static_cast<OutputType>(le);
442 lyxerr << "Unhandled value " << le
443 << " in LyXTextClass::readOutputType." << endl;
450 enum MaxCounterTags {
451 MC_COUNTER_CHAPTER = 1,
453 MC_COUNTER_SUBSECTION,
454 MC_COUNTER_SUBSUBSECTION,
455 MC_COUNTER_PARAGRAPH,
456 MC_COUNTER_SUBPARAGRAPH,
464 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
466 keyword_item maxCounterTags[] = {
467 {"counter_chapter", MC_COUNTER_CHAPTER },
468 {"counter_enumi", MC_COUNTER_ENUMI },
469 {"counter_enumii", MC_COUNTER_ENUMII },
470 {"counter_enumiii", MC_COUNTER_ENUMIII },
471 {"counter_enumiv", MC_COUNTER_ENUMIV },
472 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
473 {"counter_section", MC_COUNTER_SECTION },
474 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
475 {"counter_subsection", MC_COUNTER_SUBSECTION },
476 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
479 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
481 int le = lexrc.lex();
483 case LyXLex::LEX_UNDEF:
484 lexrc.printError("Unknown MaxCounter tag `$$Token'");
490 switch (static_cast<MaxCounterTags>(le)) {
491 case MC_COUNTER_CHAPTER:
492 maxcounter_ = LABEL_COUNTER_CHAPTER;
494 case MC_COUNTER_SECTION:
495 maxcounter_ = LABEL_COUNTER_SECTION;
497 case MC_COUNTER_SUBSECTION:
498 maxcounter_ = LABEL_COUNTER_SUBSECTION;
500 case MC_COUNTER_SUBSUBSECTION:
501 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
503 case MC_COUNTER_PARAGRAPH:
504 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
506 case MC_COUNTER_SUBPARAGRAPH:
507 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
509 case MC_COUNTER_ENUMI:
510 maxcounter_ = LABEL_COUNTER_ENUMI;
512 case MC_COUNTER_ENUMII:
513 maxcounter_ = LABEL_COUNTER_ENUMII;
515 case MC_COUNTER_ENUMIII:
516 maxcounter_ = LABEL_COUNTER_ENUMIII;
518 case MC_COUNTER_ENUMIV:
519 maxcounter_ = LABEL_COUNTER_ENUMIV;
525 enum ClassOptionsTags {
533 void LyXTextClass::readClassOptions(LyXLex & lexrc)
535 keyword_item classOptionsTags[] = {
537 {"fontsize", CO_FONTSIZE },
538 {"other", CO_OTHER },
539 {"pagestyle", CO_PAGESTYLE }
542 lexrc.pushTable(classOptionsTags, CO_END);
544 while (!getout && lexrc.isOK()) {
545 int le = lexrc.lex();
547 case LyXLex::LEX_UNDEF:
548 lexrc.printError("Unknown ClassOption tag `$$Token'");
552 switch (static_cast<ClassOptionsTags>(le)) {
555 opt_fontsize_ = rtrim(lexrc.getString());
559 opt_pagestyle_ = rtrim(lexrc.getString());
563 options_ = lexrc.getString();
586 void LyXTextClass::readFloat(LyXLex & lexrc)
588 keyword_item floatTags[] = {
590 { "extension", FT_EXT },
591 { "guiname", FT_NAME },
592 { "latexbuiltin", FT_BUILTIN },
593 { "listname", FT_LISTNAME },
594 { "numberwithin", FT_WITHIN },
595 { "placement", FT_PLACEMENT },
596 { "style", FT_STYLE },
600 lexrc.pushTable(floatTags, FT_END);
609 bool builtin = false;
612 while (!getout && lexrc.isOK()) {
613 int le = lexrc.lex();
615 case LyXLex::LEX_UNDEF:
616 lexrc.printError("Unknown ClassOption tag `$$Token'");
620 switch (static_cast<FloatTags>(le)) {
623 type = lexrc.getString();
624 // Here we could check if this type is already defined
625 // and modify it with the rest of the vars instead.
629 name = lexrc.getString();
633 placement = lexrc.getString();
637 ext = lexrc.getString();
641 within = lexrc.getString();
642 if (within == "none")
647 style = lexrc.getString();
651 listname = lexrc.getString();
655 builtin = lexrc.getBool();
663 // Here if have a full float if getout == true
665 Floating newfloat(type, placement, ext, within,
666 style, name, listname, builtin);
667 floatlist_->newFloat(newfloat);
680 void LyXTextClass::readCounter(LyXLex & lexrc)
682 keyword_item counterTags[] = {
685 { "within", CT_WITHIN }
688 lexrc.pushTable(counterTags, CT_END);
694 while (!getout && lexrc.isOK()) {
695 int le = lexrc.lex();
697 case LyXLex::LEX_UNDEF:
698 lexrc.printError("Unknown ClassOption tag `$$Token'");
702 switch (static_cast<CounterTags>(le)) {
705 name = lexrc.getString();
709 within = lexrc.getString();
710 if (within == "none")
719 // Here if have a full float if getout == true
721 if (within.empty()) {
722 ctrs_->newCounter(name);
724 ctrs_->newCounter(name, within);
732 LyXFont const & LyXTextClass::defaultfont() const
738 string const & LyXTextClass::leftmargin() const
744 string const & LyXTextClass::rightmargin() const
750 bool LyXTextClass::hasLayout(string const & n) const
752 string const name = (n.empty() ? defaultLayoutName() : n);
754 return find_if(layoutlist_.begin(), layoutlist_.end(),
756 != layoutlist_.end();
761 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
763 Assert(!name.empty());
765 LayoutList::const_iterator cit =
766 find_if(layoutlist_.begin(),
770 if (cit == layoutlist_.end()) {
771 lyxerr << "We failed to find the layout '" << name
772 << "' in the layout list. You MUST investigate!"
774 for (LayoutList::const_iterator it = layoutlist_.begin();
775 it != layoutlist_.end(); ++it)
776 lyxerr << " " << it->get()->name() << endl;
778 // we require the name to exist
787 bool LyXTextClass::delete_layout(string const & name)
789 if (name == defaultLayoutName())
792 LayoutList::iterator it =
793 remove_if(layoutlist_.begin(), layoutlist_.end(),
796 LayoutList::iterator end = layoutlist_.end();
797 bool const ret = (it != end);
798 layoutlist_.erase(it, end);
803 // Load textclass info if not loaded yet
804 bool LyXTextClass::load() const
810 string const real_file = LibFileSearch("layouts", name_, "layout");
812 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
813 lyxerr << "Error reading `"
814 << MakeDisplayPath(real_file)
815 << "'\n(Check `" << name_
816 << "')\nCheck your installation and "
817 "try Options/Reconfigure..." << endl;
825 FloatList & LyXTextClass::floats()
827 return *floatlist_.get();
831 FloatList const & LyXTextClass::floats() const
833 return *floatlist_.get();
837 Counters & LyXTextClass::counters() const
843 string const & LyXTextClass::defaultLayoutName() const
845 // This really should come from the actual layout... (Lgb)
846 return defaultlayout_;
850 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
852 return operator[](defaultLayoutName());
856 string const & LyXTextClass::name() const
862 string const & LyXTextClass::latexname() const
864 const_cast<LyXTextClass*>(this)->load();
869 string const & LyXTextClass::description() const
875 string const & LyXTextClass::opt_fontsize() const
877 return opt_fontsize_;
881 string const & LyXTextClass::opt_pagestyle() const
883 return opt_pagestyle_;
887 string const & LyXTextClass::options() const
893 string const & LyXTextClass::pagestyle() const
899 string const & LyXTextClass::preamble() const
905 LyXTextClass::PageSides LyXTextClass::sides() const
911 int LyXTextClass::secnumdepth() const
917 int LyXTextClass::tocdepth() const
923 OutputType LyXTextClass::outputType() const
929 bool LyXTextClass::provides(LyXTextClass::Provides p) const
931 return provides_ & p;
935 unsigned int LyXTextClass::columns() const
941 int LyXTextClass::maxcounter() const
947 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
953 string const & LyXTextClass::titlename() const
962 int LyXTextClass::size() const
964 return layoutlist_.size();
968 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
971 case LyXTextClass::OneSide:
974 case LyXTextClass::TwoSides: