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"
27 using namespace lyx::support;
38 compare_name(string const & name)
42 bool operator()(boost::shared_ptr<LyXLayout> const & c)
44 return c->name() == name_;
54 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
55 string const & desc, bool texClassAvail )
56 : name_(fn), latexname_(cln), description_(desc),
57 floatlist_(new FloatList), ctrs_(new Counters), texClassAvail_(texClassAvail)
64 pagestyle_ = "default";
65 maxcounter_ = LABEL_COUNTER_CHAPTER;
66 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
67 opt_fontsize_ = "10|11|12";
68 opt_pagestyle_ = "empty|plain|headings|fancy";
70 titletype_ = TITLE_COMMAND_AFTER;
71 titlename_ = "maketitle";
76 bool LyXTextClass::isTeXClassAvailable() const
78 return texClassAvail_;
82 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
84 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
85 if (!lay.Read(lexrc, *this)) {
87 lay.resfont = lay.font;
88 lay.resfont.realize(defaultfont());
89 lay.reslabelfont = lay.labelfont;
90 lay.reslabelfont.realize(defaultfont());
91 return false; // no errors
93 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
127 // Reads a textclass structure from file.
128 bool LyXTextClass::Read(string const & filename, bool merge)
130 keyword_item textClassTags[] = {
131 { "classoptions", TC_CLASSOPTIONS },
132 { "columns", TC_COLUMNS },
133 { "counter", TC_COUNTER },
134 { "defaultfont", TC_DEFAULTFONT },
135 { "defaultstyle", TC_DEFAULTSTYLE },
136 { "environment", TC_ENVIRONMENT },
137 { "float", TC_FLOAT },
138 { "input", TC_INPUT },
139 { "leftmargin", TC_LEFTMARGIN },
140 { "maxcounter", TC_MAXCOUNTER },
141 { "nofloat", TC_NOFLOAT },
142 { "nostyle", TC_NOSTYLE },
143 { "outputtype", TC_OUTPUTTYPE },
144 { "pagestyle", TC_PAGESTYLE },
145 { "preamble", TC_PREAMBLE },
146 { "providesamsmath", TC_PROVIDESAMSMATH },
147 { "providesmakeidx", TC_PROVIDESMAKEIDX },
148 { "providesnatbib", TC_PROVIDESNATBIB },
149 { "providesurl", TC_PROVIDESURL },
150 { "rightmargin", TC_RIGHTMARGIN },
151 { "secnumdepth", TC_SECNUMDEPTH },
152 { "sides", TC_SIDES },
153 { "style", TC_STYLE },
154 { "titlelatexname", TC_TITLELATEXNAME },
155 { "titlelatextype", TC_TITLELATEXTYPE },
156 { "tocdepth", TC_TOCDEPTH }
160 lyxerr[Debug::TCLASS] << "Reading textclass "
161 << MakeDisplayPath(filename)
164 lyxerr[Debug::TCLASS] << "Reading input file "
165 << MakeDisplayPath(filename)
168 LyXLex lexrc(textClassTags,
169 sizeof(textClassTags) / sizeof(textClassTags[0]));
172 lexrc.setFile(filename);
173 if (!lexrc.isOK()) error = true;
176 while (lexrc.isOK() && !error) {
177 int le = lexrc.lex();
180 case LyXLex::LEX_FEOF:
183 case LyXLex::LEX_UNDEF:
184 lexrc.printError("Unknown TextClass tag `$$Token'");
192 switch (static_cast<TextClassTags>(le)) {
194 case TC_OUTPUTTYPE: // output type definition
195 readOutputType(lexrc);
198 case TC_INPUT: // Include file
200 string tmp = LibFileSearch("layouts",
204 if (Read(tmp, true)) {
205 lexrc.printError("Error reading input"
212 case TC_DEFAULTSTYLE:
214 string const name = subst(lexrc.getString(),
216 defaultlayout_ = name;
223 string const name = subst(lexrc.getString(),
225 if (hasLayout(name)) {
226 LyXLayout * lay = operator[](name).get();
227 error = do_readStyle(lexrc, *lay);
231 if (le == TC_ENVIRONMENT)
232 lay.is_environment = true;
233 if (!(error = do_readStyle(lexrc, lay)))
234 layoutlist_.push_back
235 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
236 if (defaultlayout_.empty()) {
237 // We do not have a default
238 // layout yet, so we choose
239 // the first layout we
241 defaultlayout_ = name;
246 lexrc.printError("No name given for style: `$$Token'.");
253 string const style = subst(lexrc.getString(),
255 if (!delete_layout(style))
256 lyxerr << "Cannot delete style `"
257 << style << '\'' << endl;
258 // lexrc.printError("Cannot delete style"
265 columns_ = lexrc.getInteger();
270 switch (lexrc.getInteger()) {
271 case 1: sides_ = OneSide; break;
272 case 2: sides_ = TwoSides; break;
274 lyxerr << "Impossible number of page"
275 " sides, setting to one."
285 pagestyle_ = rtrim(lexrc.getString());
289 defaultfont_.lyxRead(lexrc);
290 if (!defaultfont_.resolved()) {
291 lexrc.printError("Warning: defaultfont should "
292 "be fully instantiated!");
293 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
298 readMaxCounter(lexrc);
303 secnumdepth_ = lexrc.getInteger();
308 tocdepth_ = lexrc.getInteger();
311 // First step to support options
312 case TC_CLASSOPTIONS:
313 readClassOptions(lexrc);
317 preamble_ = lexrc.getLongString("EndPreamble");
320 case TC_PROVIDESAMSMATH:
321 if (lexrc.next() && lexrc.getInteger())
322 provides_ |= amsmath;
325 case TC_PROVIDESNATBIB:
326 if (lexrc.next() && lexrc.getInteger())
330 case TC_PROVIDESMAKEIDX:
331 if (lexrc.next() && lexrc.getInteger())
332 provides_ |= makeidx;
336 if (lexrc.next() && lexrc.getInteger())
340 case TC_LEFTMARGIN: // left margin type
342 leftmargin_ = lexrc.getString();
345 case TC_RIGHTMARGIN: // right margin type
347 rightmargin_ = lexrc.getString();
355 case TC_TITLELATEXTYPE:
356 readTitleType(lexrc);
358 case TC_TITLELATEXNAME:
360 titlename_ = lexrc.getString();
364 string const nofloat = lexrc.getString();
365 floatlist_->erase(nofloat);
371 if (!merge) { // we are at top level here.
372 lyxerr[Debug::TCLASS] << "Finished reading textclass "
373 << MakeDisplayPath(filename)
375 if (defaultlayout_.empty()) {
376 lyxerr << "Error: Textclass '" << name_
377 << "' is missing a defaultstyle." << endl;
381 lyxerr[Debug::TCLASS] << "Finished reading input file "
382 << MakeDisplayPath(filename)
389 void LyXTextClass::readTitleType(LyXLex & lexrc)
391 keyword_item titleTypeTags[] = {
392 { "commandafter", TITLE_COMMAND_AFTER },
393 { "environment", TITLE_ENVIRONMENT }
396 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
398 int le = lexrc.lex();
400 case LyXLex::LEX_UNDEF:
401 lexrc.printError("Unknown output type `$$Token'");
403 case TITLE_COMMAND_AFTER:
404 case TITLE_ENVIRONMENT:
405 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
408 lyxerr << "Unhandled value " << le
409 << " in LyXTextClass::readTitleType." << endl;
416 void LyXTextClass::readOutputType(LyXLex & lexrc)
418 keyword_item outputTypeTags[] = {
419 { "docbook", DOCBOOK },
421 { "linuxdoc", LINUXDOC },
422 { "literate", LITERATE }
425 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
427 int le = lexrc.lex();
429 case LyXLex::LEX_UNDEF:
430 lexrc.printError("Unknown output type `$$Token'");
436 outputType_ = static_cast<OutputType>(le);
439 lyxerr << "Unhandled value " << le
440 << " in LyXTextClass::readOutputType." << endl;
447 enum MaxCounterTags {
448 MC_COUNTER_CHAPTER = 1,
450 MC_COUNTER_SUBSECTION,
451 MC_COUNTER_SUBSUBSECTION,
452 MC_COUNTER_PARAGRAPH,
453 MC_COUNTER_SUBPARAGRAPH,
461 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
463 keyword_item maxCounterTags[] = {
464 {"counter_chapter", MC_COUNTER_CHAPTER },
465 {"counter_enumi", MC_COUNTER_ENUMI },
466 {"counter_enumii", MC_COUNTER_ENUMII },
467 {"counter_enumiii", MC_COUNTER_ENUMIII },
468 {"counter_enumiv", MC_COUNTER_ENUMIV },
469 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
470 {"counter_section", MC_COUNTER_SECTION },
471 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
472 {"counter_subsection", MC_COUNTER_SUBSECTION },
473 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
476 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
478 int le = lexrc.lex();
480 case LyXLex::LEX_UNDEF:
481 lexrc.printError("Unknown MaxCounter tag `$$Token'");
487 switch (static_cast<MaxCounterTags>(le)) {
488 case MC_COUNTER_CHAPTER:
489 maxcounter_ = LABEL_COUNTER_CHAPTER;
491 case MC_COUNTER_SECTION:
492 maxcounter_ = LABEL_COUNTER_SECTION;
494 case MC_COUNTER_SUBSECTION:
495 maxcounter_ = LABEL_COUNTER_SUBSECTION;
497 case MC_COUNTER_SUBSUBSECTION:
498 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
500 case MC_COUNTER_PARAGRAPH:
501 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
503 case MC_COUNTER_SUBPARAGRAPH:
504 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
506 case MC_COUNTER_ENUMI:
507 maxcounter_ = LABEL_COUNTER_ENUMI;
509 case MC_COUNTER_ENUMII:
510 maxcounter_ = LABEL_COUNTER_ENUMII;
512 case MC_COUNTER_ENUMIII:
513 maxcounter_ = LABEL_COUNTER_ENUMIII;
515 case MC_COUNTER_ENUMIV:
516 maxcounter_ = LABEL_COUNTER_ENUMIV;
522 enum ClassOptionsTags {
530 void LyXTextClass::readClassOptions(LyXLex & lexrc)
532 keyword_item classOptionsTags[] = {
534 {"fontsize", CO_FONTSIZE },
535 {"other", CO_OTHER },
536 {"pagestyle", CO_PAGESTYLE }
539 lexrc.pushTable(classOptionsTags, CO_END);
541 while (!getout && lexrc.isOK()) {
542 int le = lexrc.lex();
544 case LyXLex::LEX_UNDEF:
545 lexrc.printError("Unknown ClassOption tag `$$Token'");
549 switch (static_cast<ClassOptionsTags>(le)) {
552 opt_fontsize_ = rtrim(lexrc.getString());
556 opt_pagestyle_ = rtrim(lexrc.getString());
560 options_ = lexrc.getString();
583 void LyXTextClass::readFloat(LyXLex & lexrc)
585 keyword_item floatTags[] = {
587 { "extension", FT_EXT },
588 { "guiname", FT_NAME },
589 { "latexbuiltin", FT_BUILTIN },
590 { "listname", FT_LISTNAME },
591 { "numberwithin", FT_WITHIN },
592 { "placement", FT_PLACEMENT },
593 { "style", FT_STYLE },
597 lexrc.pushTable(floatTags, FT_END);
606 bool builtin = false;
609 while (!getout && lexrc.isOK()) {
610 int le = lexrc.lex();
612 case LyXLex::LEX_UNDEF:
613 lexrc.printError("Unknown ClassOption tag `$$Token'");
617 switch (static_cast<FloatTags>(le)) {
620 type = lexrc.getString();
621 // Here we could check if this type is already defined
622 // and modify it with the rest of the vars instead.
626 name = lexrc.getString();
630 placement = lexrc.getString();
634 ext = lexrc.getString();
638 within = lexrc.getString();
639 if (within == "none")
644 style = lexrc.getString();
648 listname = lexrc.getString();
652 builtin = lexrc.getBool();
660 // Here if have a full float if getout == true
662 Floating newfloat(type, placement, ext, within,
663 style, name, listname, builtin);
664 floatlist_->newFloat(newfloat);
677 void LyXTextClass::readCounter(LyXLex & lexrc)
679 keyword_item counterTags[] = {
682 { "within", CT_WITHIN }
685 lexrc.pushTable(counterTags, CT_END);
691 while (!getout && lexrc.isOK()) {
692 int le = lexrc.lex();
694 case LyXLex::LEX_UNDEF:
695 lexrc.printError("Unknown ClassOption tag `$$Token'");
699 switch (static_cast<CounterTags>(le)) {
702 name = lexrc.getString();
706 within = lexrc.getString();
707 if (within == "none")
716 // Here if have a full float if getout == true
718 if (within.empty()) {
719 ctrs_->newCounter(name);
721 ctrs_->newCounter(name, within);
729 LyXFont const & LyXTextClass::defaultfont() const
735 string const & LyXTextClass::leftmargin() const
741 string const & LyXTextClass::rightmargin() const
747 bool LyXTextClass::hasLayout(string const & n) const
749 string const name = (n.empty() ? defaultLayoutName() : n);
751 return find_if(layoutlist_.begin(), layoutlist_.end(),
753 != layoutlist_.end();
758 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
760 Assert(!name.empty());
762 LayoutList::const_iterator cit =
763 find_if(layoutlist_.begin(),
767 if (cit == layoutlist_.end()) {
768 lyxerr << "We failed to find the layout '" << name
769 << "' in the layout list. You MUST investigate!"
771 for (LayoutList::const_iterator it = layoutlist_.begin();
772 it != layoutlist_.end(); ++it)
773 lyxerr << " " << it->get()->name() << endl;
775 // we require the name to exist
784 bool LyXTextClass::delete_layout(string const & name)
786 if (name == defaultLayoutName())
789 LayoutList::iterator it =
790 remove_if(layoutlist_.begin(), layoutlist_.end(),
793 LayoutList::iterator end = layoutlist_.end();
794 bool const ret = (it != end);
795 layoutlist_.erase(it, end);
800 // Load textclass info if not loaded yet
801 bool LyXTextClass::load() const
807 string const real_file = LibFileSearch("layouts", name_, "layout");
809 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
810 lyxerr << "Error reading `"
811 << MakeDisplayPath(real_file)
812 << "'\n(Check `" << name_
813 << "')\nCheck your installation and "
814 "try Options/Reconfigure..." << endl;
822 FloatList & LyXTextClass::floats()
824 return *floatlist_.get();
828 FloatList const & LyXTextClass::floats() const
830 return *floatlist_.get();
834 Counters & LyXTextClass::counters() const
840 string const & LyXTextClass::defaultLayoutName() const
842 // This really should come from the actual layout... (Lgb)
843 return defaultlayout_;
847 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
849 return operator[](defaultLayoutName());
853 string const & LyXTextClass::name() const
859 string const & LyXTextClass::latexname() const
861 const_cast<LyXTextClass*>(this)->load();
866 string const & LyXTextClass::description() const
872 string const & LyXTextClass::opt_fontsize() const
874 return opt_fontsize_;
878 string const & LyXTextClass::opt_pagestyle() const
880 return opt_pagestyle_;
884 string const & LyXTextClass::options() const
890 string const & LyXTextClass::pagestyle() const
896 string const & LyXTextClass::preamble() const
902 LyXTextClass::PageSides LyXTextClass::sides() const
908 int LyXTextClass::secnumdepth() const
914 int LyXTextClass::tocdepth() const
920 OutputType LyXTextClass::outputType() const
926 bool LyXTextClass::provides(LyXTextClass::Provides p) const
928 return provides_ & p;
932 unsigned int LyXTextClass::columns() const
938 int LyXTextClass::maxcounter() const
944 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
950 string const & LyXTextClass::titlename() const
959 int LyXTextClass::size() const
961 return layoutlist_.size();
965 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
968 case LyXTextClass::OneSide:
971 case LyXTextClass::TwoSides: