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 return c->name() == name_;
52 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
54 : name_(fn), latexname_(cln), description_(desc),
55 floatlist_(new FloatList), ctrs_(new Counters)
62 pagestyle_ = "default";
63 maxcounter_ = LABEL_COUNTER_CHAPTER;
64 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
65 opt_fontsize_ = "10|11|12";
66 opt_pagestyle_ = "empty|plain|headings|fancy";
68 titletype_ = TITLE_COMMAND_AFTER;
69 titlename_ = "maketitle";
74 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
76 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
77 if (!lay.Read(lexrc, *this)) {
79 lay.resfont = lay.font;
80 lay.resfont.realize(defaultfont());
81 lay.reslabelfont = lay.labelfont;
82 lay.reslabelfont.realize(defaultfont());
83 return false; // no errors
85 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
119 // Reads a textclass structure from file.
120 bool LyXTextClass::Read(string const & filename, bool merge)
122 keyword_item textClassTags[] = {
123 { "classoptions", TC_CLASSOPTIONS },
124 { "columns", TC_COLUMNS },
125 { "counter", TC_COUNTER },
126 { "defaultfont", TC_DEFAULTFONT },
127 { "defaultstyle", TC_DEFAULTSTYLE },
128 { "float", TC_FLOAT },
129 { "input", TC_INPUT },
130 { "leftmargin", TC_LEFTMARGIN },
131 { "maxcounter", TC_MAXCOUNTER },
132 { "nofloat", TC_NOFLOAT },
133 { "nostyle", TC_NOSTYLE },
134 { "outputtype", TC_OUTPUTTYPE },
135 { "pagestyle", TC_PAGESTYLE },
136 { "preamble", TC_PREAMBLE },
137 { "providesamsmath", TC_PROVIDESAMSMATH },
138 { "providesmakeidx", TC_PROVIDESMAKEIDX },
139 { "providesnatbib", TC_PROVIDESNATBIB },
140 { "providesurl", TC_PROVIDESURL },
141 { "rightmargin", TC_RIGHTMARGIN },
142 { "secnumdepth", TC_SECNUMDEPTH },
143 { "sides", TC_SIDES },
144 { "style", TC_STYLE },
145 { "titlelatexname", TC_TITLELATEXNAME },
146 { "titlelatextype", TC_TITLELATEXTYPE },
147 { "tocdepth", TC_TOCDEPTH }
151 lyxerr[Debug::TCLASS] << "Reading textclass "
152 << MakeDisplayPath(filename)
155 lyxerr[Debug::TCLASS] << "Reading input file "
156 << MakeDisplayPath(filename)
159 LyXLex lexrc(textClassTags, TC_TITLELATEXTYPE);
162 lexrc.setFile(filename);
163 if (!lexrc.isOK()) error = true;
166 while (lexrc.isOK() && !error) {
167 int le = lexrc.lex();
170 case LyXLex::LEX_FEOF:
173 case LyXLex::LEX_UNDEF:
174 lexrc.printError("Unknown TextClass tag `$$Token'");
182 switch (static_cast<TextClassTags>(le)) {
184 case TC_OUTPUTTYPE: // output type definition
185 readOutputType(lexrc);
188 case TC_INPUT: // Include file
190 string tmp = LibFileSearch("layouts",
194 if (Read(tmp, true)) {
195 lexrc.printError("Error reading input"
202 case TC_DEFAULTSTYLE:
204 string const name = subst(lexrc.getString(),
206 defaultlayout_ = name;
212 string const name = subst(lexrc.getString(),
214 if (hasLayout(name)) {
215 LyXLayout * lay = operator[](name).get();
216 error = do_readStyle(lexrc, *lay);
220 if (!(error = do_readStyle(lexrc, lay)))
221 layoutlist_.push_back(boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
222 if (defaultlayout_.empty()) {
223 // We do not have a default
224 // layout yet, so we choose
225 // the first layout we
227 defaultlayout_ = name;
232 lexrc.printError("No name given for style: `$$Token'.");
239 string const name = subst(lexrc.getString(),
241 if (hasLayout(name)) {
242 LyXLayout * lay = operator[](name).get();
243 error = do_readStyle(lexrc, *lay);
247 if (!(error = do_readStyle(lexrc, lay)))
249 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
251 lexrc.printError("Problems reading environment: `$$Token'.");
255 lexrc.printError("No name given for style: `$$Token'.");
262 string const style = subst(lexrc.getString(),
264 if (!delete_layout(style))
265 lyxerr << "Cannot delete style `"
266 << style << '\'' << endl;
267 // lexrc.printError("Cannot delete style"
274 columns_ = lexrc.getInteger();
279 switch (lexrc.getInteger()) {
280 case 1: sides_ = OneSide; break;
281 case 2: sides_ = TwoSides; break;
283 lyxerr << "Impossible number of page"
284 " sides, setting to one."
294 pagestyle_ = rtrim(lexrc.getString());
298 defaultfont_.lyxRead(lexrc);
299 if (!defaultfont_.resolved()) {
300 lexrc.printError("Warning: defaultfont should "
301 "be fully instantiated!");
302 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
307 readMaxCounter(lexrc);
312 secnumdepth_ = lexrc.getInteger();
317 tocdepth_ = lexrc.getInteger();
320 // First step to support options
321 case TC_CLASSOPTIONS:
322 readClassOptions(lexrc);
326 preamble_ = lexrc.getLongString("EndPreamble");
329 case TC_PROVIDESAMSMATH:
330 if (lexrc.next() && lexrc.getInteger())
331 provides_ |= amsmath;
334 case TC_PROVIDESNATBIB:
335 if (lexrc.next() && lexrc.getInteger())
339 case TC_PROVIDESMAKEIDX:
340 if (lexrc.next() && lexrc.getInteger())
341 provides_ |= makeidx;
345 if (lexrc.next() && lexrc.getInteger())
349 case TC_LEFTMARGIN: // left margin type
351 leftmargin_ = lexrc.getString();
354 case TC_RIGHTMARGIN: // right margin type
356 rightmargin_ = lexrc.getString();
364 case TC_TITLELATEXTYPE:
365 readTitleType(lexrc);
367 case TC_TITLELATEXNAME:
369 titlename_ = lexrc.getString();
373 string const nofloat = lexrc.getString();
374 floatlist_->erase(nofloat);
380 if (!merge) { // we are at top level here.
381 lyxerr[Debug::TCLASS] << "Finished reading textclass "
382 << MakeDisplayPath(filename)
384 if (defaultlayout_.empty()) {
385 lyxerr << "Error: Textclass '" << name_
386 << "' is missing a defaultstyle." << endl;
390 lyxerr[Debug::TCLASS] << "Finished reading input file "
391 << MakeDisplayPath(filename)
398 void LyXTextClass::readTitleType(LyXLex & lexrc)
400 keyword_item titleTypeTags[] = {
401 { "commandafter", TITLE_COMMAND_AFTER },
402 { "environment", TITLE_ENVIRONMENT }
405 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
407 int le = lexrc.lex();
409 case LyXLex::LEX_UNDEF:
410 lexrc.printError("Unknown output type `$$Token'");
412 case TITLE_COMMAND_AFTER:
413 case TITLE_ENVIRONMENT:
414 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
417 lyxerr << "Unhandled value " << le
418 << " in LyXTextClass::readTitleType." << endl;
425 void LyXTextClass::readOutputType(LyXLex & lexrc)
427 keyword_item outputTypeTags[] = {
428 { "docbook", DOCBOOK },
430 { "linuxdoc", LINUXDOC },
431 { "literate", LITERATE }
434 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
436 int le = lexrc.lex();
438 case LyXLex::LEX_UNDEF:
439 lexrc.printError("Unknown output type `$$Token'");
445 outputType_ = static_cast<OutputType>(le);
448 lyxerr << "Unhandled value " << le
449 << " in LyXTextClass::readOutputType." << endl;
456 enum MaxCounterTags {
457 MC_COUNTER_CHAPTER = 1,
459 MC_COUNTER_SUBSECTION,
460 MC_COUNTER_SUBSUBSECTION,
461 MC_COUNTER_PARAGRAPH,
462 MC_COUNTER_SUBPARAGRAPH,
470 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
472 keyword_item maxCounterTags[] = {
473 {"counter_chapter", MC_COUNTER_CHAPTER },
474 {"counter_enumi", MC_COUNTER_ENUMI },
475 {"counter_enumii", MC_COUNTER_ENUMII },
476 {"counter_enumiii", MC_COUNTER_ENUMIII },
477 {"counter_enumiv", MC_COUNTER_ENUMIV },
478 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
479 {"counter_section", MC_COUNTER_SECTION },
480 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
481 {"counter_subsection", MC_COUNTER_SUBSECTION },
482 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
485 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
487 int le = lexrc.lex();
489 case LyXLex::LEX_UNDEF:
490 lexrc.printError("Unknown MaxCounter tag `$$Token'");
496 switch (static_cast<MaxCounterTags>(le)) {
497 case MC_COUNTER_CHAPTER:
498 maxcounter_ = LABEL_COUNTER_CHAPTER;
500 case MC_COUNTER_SECTION:
501 maxcounter_ = LABEL_COUNTER_SECTION;
503 case MC_COUNTER_SUBSECTION:
504 maxcounter_ = LABEL_COUNTER_SUBSECTION;
506 case MC_COUNTER_SUBSUBSECTION:
507 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
509 case MC_COUNTER_PARAGRAPH:
510 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
512 case MC_COUNTER_SUBPARAGRAPH:
513 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
515 case MC_COUNTER_ENUMI:
516 maxcounter_ = LABEL_COUNTER_ENUMI;
518 case MC_COUNTER_ENUMII:
519 maxcounter_ = LABEL_COUNTER_ENUMII;
521 case MC_COUNTER_ENUMIII:
522 maxcounter_ = LABEL_COUNTER_ENUMIII;
524 case MC_COUNTER_ENUMIV:
525 maxcounter_ = LABEL_COUNTER_ENUMIV;
531 enum ClassOptionsTags {
539 void LyXTextClass::readClassOptions(LyXLex & lexrc)
541 keyword_item classOptionsTags[] = {
543 {"fontsize", CO_FONTSIZE },
544 {"other", CO_OTHER },
545 {"pagestyle", CO_PAGESTYLE }
548 lexrc.pushTable(classOptionsTags, CO_END);
550 while (!getout && lexrc.isOK()) {
551 int le = lexrc.lex();
553 case LyXLex::LEX_UNDEF:
554 lexrc.printError("Unknown ClassOption tag `$$Token'");
558 switch (static_cast<ClassOptionsTags>(le)) {
561 opt_fontsize_ = rtrim(lexrc.getString());
565 opt_pagestyle_ = rtrim(lexrc.getString());
569 options_ = lexrc.getString();
592 void LyXTextClass::readFloat(LyXLex & lexrc)
594 keyword_item floatTags[] = {
596 { "extension", FT_EXT },
597 { "guiname", FT_NAME },
598 { "latexbuiltin", FT_BUILTIN },
599 { "listname", FT_LISTNAME },
600 { "numberwithin", FT_WITHIN },
601 { "placement", FT_PLACEMENT },
602 { "style", FT_STYLE },
606 lexrc.pushTable(floatTags, FT_END);
615 bool builtin = false;
618 while (!getout && lexrc.isOK()) {
619 int le = lexrc.lex();
621 case LyXLex::LEX_UNDEF:
622 lexrc.printError("Unknown ClassOption tag `$$Token'");
626 switch (static_cast<FloatTags>(le)) {
629 type = lexrc.getString();
630 // Here we could check if this type is already defined
631 // and modify it with the rest of the vars instead.
635 name = lexrc.getString();
639 placement = lexrc.getString();
643 ext = lexrc.getString();
647 within = lexrc.getString();
648 if (within == "none")
653 style = lexrc.getString();
657 listname = lexrc.getString();
661 builtin = lexrc.getBool();
669 // Here if have a full float if getout == true
671 Floating newfloat(type, placement, ext, within,
672 style, name, listname, builtin);
673 floatlist_->newFloat(newfloat);
686 void LyXTextClass::readCounter(LyXLex & lexrc)
688 keyword_item counterTags[] = {
691 { "within", CT_WITHIN }
694 lexrc.pushTable(counterTags, CT_END);
700 while (!getout && lexrc.isOK()) {
701 int le = lexrc.lex();
703 case LyXLex::LEX_UNDEF:
704 lexrc.printError("Unknown ClassOption tag `$$Token'");
708 switch (static_cast<CounterTags>(le)) {
711 name = lexrc.getString();
715 within = lexrc.getString();
716 if (within == "none")
725 // Here if have a full float if getout == true
727 if (within.empty()) {
728 ctrs_->newCounter(name);
730 ctrs_->newCounter(name, within);
738 LyXFont const & LyXTextClass::defaultfont() const
744 string const & LyXTextClass::leftmargin() const
750 string const & LyXTextClass::rightmargin() const
756 bool LyXTextClass::hasLayout(string const & n) const
758 string const name = (n.empty() ? defaultLayoutName() : n);
760 return find_if(layoutlist_.begin(), layoutlist_.end(),
762 != layoutlist_.end();
766 LyXLayout_ptr const & LyXTextClass::operator[](string const & n) const
768 lyx::Assert(!n.empty());
771 lyxerr << "LyXTextClass::operator[] called with empty n" << endl;
773 string const name = (n.empty() ? defaultLayoutName() : n);
775 static string lastLayoutName;
776 static LayoutList::difference_type lastLayoutIndex;
778 if (name == lastLayoutName)
779 return layoutlist_[lastLayoutIndex];
781 LayoutList::const_iterator cit =
782 find_if(layoutlist_.begin(),
786 if (cit == layoutlist_.end()) {
787 lyxerr << "We failed to find the layout '" << name
788 << "' in the layout list. You MUST investigate!"
791 // we require the name to exist
795 lastLayoutName = name;
796 lastLayoutIndex = std::distance(layoutlist_.begin(), cit);
802 LyXLayout_ptr const & LyXTextClass::getEnv(string const & name) const
804 lyx::Assert(!name.empty());
807 lyxerr << "LyXTextClass::getEnv() called with empty n" << endl;
809 LayoutList::const_iterator cit =
810 find_if(envlist_.begin(), envlist_.end(), compare_name(name));
812 if (cit == envlist_.end()) {
813 lyxerr << "We failed to find the environment '" << name
814 << "' in the layout list. You MUST investigate!"
816 // we require the name to exist
824 bool LyXTextClass::delete_layout(string const & name)
826 if (name == defaultLayoutName())
829 LayoutList::iterator it =
830 remove_if(layoutlist_.begin(), layoutlist_.end(),
833 LayoutList::iterator end = layoutlist_.end();
834 bool const ret = (it != end);
835 layoutlist_.erase(it, end);
840 // Load textclass info if not loaded yet
841 bool LyXTextClass::load() const
847 string const real_file = LibFileSearch("layouts", name_, "layout");
849 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
850 lyxerr << "Error reading `"
851 << MakeDisplayPath(real_file)
852 << "'\n(Check `" << name_
853 << "')\nCheck your installation and "
854 "try Options/Reconfigure..." << endl;
862 FloatList & LyXTextClass::floats()
864 return *floatlist_.get();
868 FloatList const & LyXTextClass::floats() const
870 return *floatlist_.get();
874 Counters & LyXTextClass::counters() const
880 string const & LyXTextClass::defaultLayoutName() const
882 // This really should come from the actual layout... (Lgb)
883 return defaultlayout_;
887 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
889 return operator[](defaultLayoutName());
893 string const & LyXTextClass::name() const
899 string const & LyXTextClass::latexname() const
901 const_cast<LyXTextClass*>(this)->load();
906 string const & LyXTextClass::description() const
912 string const & LyXTextClass::opt_fontsize() const
914 return opt_fontsize_;
918 string const & LyXTextClass::opt_pagestyle() const
920 return opt_pagestyle_;
924 string const & LyXTextClass::options() const
930 string const & LyXTextClass::pagestyle() const
936 string const & LyXTextClass::preamble() const
942 LyXTextClass::PageSides LyXTextClass::sides() const
948 int LyXTextClass::secnumdepth() const
954 int LyXTextClass::tocdepth() const
960 OutputType LyXTextClass::outputType() const
966 bool LyXTextClass::provides(LyXTextClass::Provides p) const
968 return provides_ & p;
972 unsigned int LyXTextClass::columns() const
978 int LyXTextClass::maxcounter() const
984 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
990 string const & LyXTextClass::titlename() const
999 int LyXTextClass::size() const
1001 return layoutlist_.size();
1005 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1008 case LyXTextClass::OneSide:
1011 case LyXTextClass::TwoSides: