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,
53 string const & desc, bool texClassAvail )
54 : name_(fn), latexname_(cln), description_(desc),
55 floatlist_(new FloatList), ctrs_(new Counters), texClassAvail_(texClassAvail)
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::isTeXClassAvailable() const
76 return texClassAvail_;
80 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
82 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
83 if (!lay.Read(lexrc, *this)) {
85 lay.resfont = lay.font;
86 lay.resfont.realize(defaultfont());
87 lay.reslabelfont = lay.labelfont;
88 lay.reslabelfont.realize(defaultfont());
89 return false; // no errors
91 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
125 // Reads a textclass structure from file.
126 bool LyXTextClass::Read(string const & filename, bool merge)
128 keyword_item textClassTags[] = {
129 { "classoptions", TC_CLASSOPTIONS },
130 { "columns", TC_COLUMNS },
131 { "counter", TC_COUNTER },
132 { "defaultfont", TC_DEFAULTFONT },
133 { "defaultstyle", TC_DEFAULTSTYLE },
134 { "environment", TC_ENVIRONMENT },
135 { "float", TC_FLOAT },
136 { "input", TC_INPUT },
137 { "leftmargin", TC_LEFTMARGIN },
138 { "maxcounter", TC_MAXCOUNTER },
139 { "nofloat", TC_NOFLOAT },
140 { "nostyle", TC_NOSTYLE },
141 { "outputtype", TC_OUTPUTTYPE },
142 { "pagestyle", TC_PAGESTYLE },
143 { "preamble", TC_PREAMBLE },
144 { "providesamsmath", TC_PROVIDESAMSMATH },
145 { "providesmakeidx", TC_PROVIDESMAKEIDX },
146 { "providesnatbib", TC_PROVIDESNATBIB },
147 { "providesurl", TC_PROVIDESURL },
148 { "rightmargin", TC_RIGHTMARGIN },
149 { "secnumdepth", TC_SECNUMDEPTH },
150 { "sides", TC_SIDES },
151 { "style", TC_STYLE },
152 { "titlelatexname", TC_TITLELATEXNAME },
153 { "titlelatextype", TC_TITLELATEXTYPE },
154 { "tocdepth", TC_TOCDEPTH }
158 lyxerr[Debug::TCLASS] << "Reading textclass "
159 << MakeDisplayPath(filename)
162 lyxerr[Debug::TCLASS] << "Reading input file "
163 << MakeDisplayPath(filename)
166 LyXLex lexrc(textClassTags,
167 sizeof(textClassTags) / sizeof(textClassTags[0]));
170 lexrc.setFile(filename);
171 if (!lexrc.isOK()) error = true;
174 while (lexrc.isOK() && !error) {
175 int le = lexrc.lex();
178 case LyXLex::LEX_FEOF:
181 case LyXLex::LEX_UNDEF:
182 lexrc.printError("Unknown TextClass tag `$$Token'");
190 switch (static_cast<TextClassTags>(le)) {
192 case TC_OUTPUTTYPE: // output type definition
193 readOutputType(lexrc);
196 case TC_INPUT: // Include file
198 string tmp = LibFileSearch("layouts",
202 if (Read(tmp, true)) {
203 lexrc.printError("Error reading input"
210 case TC_DEFAULTSTYLE:
212 string const name = subst(lexrc.getString(),
214 defaultlayout_ = name;
221 string const name = subst(lexrc.getString(),
223 if (hasLayout(name)) {
224 LyXLayout * lay = operator[](name).get();
225 error = do_readStyle(lexrc, *lay);
229 if (le == TC_ENVIRONMENT)
230 lay.is_environment = true;
231 if (!(error = do_readStyle(lexrc, lay)))
232 layoutlist_.push_back
233 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
234 if (defaultlayout_.empty()) {
235 // We do not have a default
236 // layout yet, so we choose
237 // the first layout we
239 defaultlayout_ = name;
244 lexrc.printError("No name given for style: `$$Token'.");
251 string const style = subst(lexrc.getString(),
253 if (!delete_layout(style))
254 lyxerr << "Cannot delete style `"
255 << style << '\'' << endl;
256 // lexrc.printError("Cannot delete style"
263 columns_ = lexrc.getInteger();
268 switch (lexrc.getInteger()) {
269 case 1: sides_ = OneSide; break;
270 case 2: sides_ = TwoSides; break;
272 lyxerr << "Impossible number of page"
273 " sides, setting to one."
283 pagestyle_ = rtrim(lexrc.getString());
287 defaultfont_.lyxRead(lexrc);
288 if (!defaultfont_.resolved()) {
289 lexrc.printError("Warning: defaultfont should "
290 "be fully instantiated!");
291 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
296 readMaxCounter(lexrc);
301 secnumdepth_ = lexrc.getInteger();
306 tocdepth_ = lexrc.getInteger();
309 // First step to support options
310 case TC_CLASSOPTIONS:
311 readClassOptions(lexrc);
315 preamble_ = lexrc.getLongString("EndPreamble");
318 case TC_PROVIDESAMSMATH:
319 if (lexrc.next() && lexrc.getInteger())
320 provides_ |= amsmath;
323 case TC_PROVIDESNATBIB:
324 if (lexrc.next() && lexrc.getInteger())
328 case TC_PROVIDESMAKEIDX:
329 if (lexrc.next() && lexrc.getInteger())
330 provides_ |= makeidx;
334 if (lexrc.next() && lexrc.getInteger())
338 case TC_LEFTMARGIN: // left margin type
340 leftmargin_ = lexrc.getString();
343 case TC_RIGHTMARGIN: // right margin type
345 rightmargin_ = lexrc.getString();
353 case TC_TITLELATEXTYPE:
354 readTitleType(lexrc);
356 case TC_TITLELATEXNAME:
358 titlename_ = lexrc.getString();
362 string const nofloat = lexrc.getString();
363 floatlist_->erase(nofloat);
369 if (!merge) { // we are at top level here.
370 lyxerr[Debug::TCLASS] << "Finished reading textclass "
371 << MakeDisplayPath(filename)
373 if (defaultlayout_.empty()) {
374 lyxerr << "Error: Textclass '" << name_
375 << "' is missing a defaultstyle." << endl;
379 lyxerr[Debug::TCLASS] << "Finished reading input file "
380 << MakeDisplayPath(filename)
387 void LyXTextClass::readTitleType(LyXLex & lexrc)
389 keyword_item titleTypeTags[] = {
390 { "commandafter", TITLE_COMMAND_AFTER },
391 { "environment", TITLE_ENVIRONMENT }
394 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
396 int le = lexrc.lex();
398 case LyXLex::LEX_UNDEF:
399 lexrc.printError("Unknown output type `$$Token'");
401 case TITLE_COMMAND_AFTER:
402 case TITLE_ENVIRONMENT:
403 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
406 lyxerr << "Unhandled value " << le
407 << " in LyXTextClass::readTitleType." << endl;
414 void LyXTextClass::readOutputType(LyXLex & lexrc)
416 keyword_item outputTypeTags[] = {
417 { "docbook", DOCBOOK },
419 { "linuxdoc", LINUXDOC },
420 { "literate", LITERATE }
423 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
425 int le = lexrc.lex();
427 case LyXLex::LEX_UNDEF:
428 lexrc.printError("Unknown output type `$$Token'");
434 outputType_ = static_cast<OutputType>(le);
437 lyxerr << "Unhandled value " << le
438 << " in LyXTextClass::readOutputType." << endl;
445 enum MaxCounterTags {
446 MC_COUNTER_CHAPTER = 1,
448 MC_COUNTER_SUBSECTION,
449 MC_COUNTER_SUBSUBSECTION,
450 MC_COUNTER_PARAGRAPH,
451 MC_COUNTER_SUBPARAGRAPH,
459 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
461 keyword_item maxCounterTags[] = {
462 {"counter_chapter", MC_COUNTER_CHAPTER },
463 {"counter_enumi", MC_COUNTER_ENUMI },
464 {"counter_enumii", MC_COUNTER_ENUMII },
465 {"counter_enumiii", MC_COUNTER_ENUMIII },
466 {"counter_enumiv", MC_COUNTER_ENUMIV },
467 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
468 {"counter_section", MC_COUNTER_SECTION },
469 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
470 {"counter_subsection", MC_COUNTER_SUBSECTION },
471 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
474 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
476 int le = lexrc.lex();
478 case LyXLex::LEX_UNDEF:
479 lexrc.printError("Unknown MaxCounter tag `$$Token'");
485 switch (static_cast<MaxCounterTags>(le)) {
486 case MC_COUNTER_CHAPTER:
487 maxcounter_ = LABEL_COUNTER_CHAPTER;
489 case MC_COUNTER_SECTION:
490 maxcounter_ = LABEL_COUNTER_SECTION;
492 case MC_COUNTER_SUBSECTION:
493 maxcounter_ = LABEL_COUNTER_SUBSECTION;
495 case MC_COUNTER_SUBSUBSECTION:
496 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
498 case MC_COUNTER_PARAGRAPH:
499 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
501 case MC_COUNTER_SUBPARAGRAPH:
502 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
504 case MC_COUNTER_ENUMI:
505 maxcounter_ = LABEL_COUNTER_ENUMI;
507 case MC_COUNTER_ENUMII:
508 maxcounter_ = LABEL_COUNTER_ENUMII;
510 case MC_COUNTER_ENUMIII:
511 maxcounter_ = LABEL_COUNTER_ENUMIII;
513 case MC_COUNTER_ENUMIV:
514 maxcounter_ = LABEL_COUNTER_ENUMIV;
520 enum ClassOptionsTags {
528 void LyXTextClass::readClassOptions(LyXLex & lexrc)
530 keyword_item classOptionsTags[] = {
532 {"fontsize", CO_FONTSIZE },
533 {"other", CO_OTHER },
534 {"pagestyle", CO_PAGESTYLE }
537 lexrc.pushTable(classOptionsTags, CO_END);
539 while (!getout && lexrc.isOK()) {
540 int le = lexrc.lex();
542 case LyXLex::LEX_UNDEF:
543 lexrc.printError("Unknown ClassOption tag `$$Token'");
547 switch (static_cast<ClassOptionsTags>(le)) {
550 opt_fontsize_ = rtrim(lexrc.getString());
554 opt_pagestyle_ = rtrim(lexrc.getString());
558 options_ = lexrc.getString();
581 void LyXTextClass::readFloat(LyXLex & lexrc)
583 keyword_item floatTags[] = {
585 { "extension", FT_EXT },
586 { "guiname", FT_NAME },
587 { "latexbuiltin", FT_BUILTIN },
588 { "listname", FT_LISTNAME },
589 { "numberwithin", FT_WITHIN },
590 { "placement", FT_PLACEMENT },
591 { "style", FT_STYLE },
595 lexrc.pushTable(floatTags, FT_END);
604 bool builtin = false;
607 while (!getout && lexrc.isOK()) {
608 int le = lexrc.lex();
610 case LyXLex::LEX_UNDEF:
611 lexrc.printError("Unknown ClassOption tag `$$Token'");
615 switch (static_cast<FloatTags>(le)) {
618 type = lexrc.getString();
619 // Here we could check if this type is already defined
620 // and modify it with the rest of the vars instead.
624 name = lexrc.getString();
628 placement = lexrc.getString();
632 ext = lexrc.getString();
636 within = lexrc.getString();
637 if (within == "none")
642 style = lexrc.getString();
646 listname = lexrc.getString();
650 builtin = lexrc.getBool();
658 // Here if have a full float if getout == true
660 Floating newfloat(type, placement, ext, within,
661 style, name, listname, builtin);
662 floatlist_->newFloat(newfloat);
675 void LyXTextClass::readCounter(LyXLex & lexrc)
677 keyword_item counterTags[] = {
680 { "within", CT_WITHIN }
683 lexrc.pushTable(counterTags, CT_END);
689 while (!getout && lexrc.isOK()) {
690 int le = lexrc.lex();
692 case LyXLex::LEX_UNDEF:
693 lexrc.printError("Unknown ClassOption tag `$$Token'");
697 switch (static_cast<CounterTags>(le)) {
700 name = lexrc.getString();
704 within = lexrc.getString();
705 if (within == "none")
714 // Here if have a full float if getout == true
716 if (within.empty()) {
717 ctrs_->newCounter(name);
719 ctrs_->newCounter(name, within);
727 LyXFont const & LyXTextClass::defaultfont() const
733 string const & LyXTextClass::leftmargin() const
739 string const & LyXTextClass::rightmargin() const
745 bool LyXTextClass::hasLayout(string const & n) const
747 string const name = (n.empty() ? defaultLayoutName() : n);
749 return find_if(layoutlist_.begin(), layoutlist_.end(),
751 != layoutlist_.end();
756 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
758 lyx::Assert(!name.empty());
760 LayoutList::const_iterator cit =
761 find_if(layoutlist_.begin(),
765 if (cit == layoutlist_.end()) {
766 lyxerr << "We failed to find the layout '" << name
767 << "' in the layout list. You MUST investigate!"
769 for (LayoutList::const_iterator it = layoutlist_.begin();
770 it != layoutlist_.end(); ++it)
771 lyxerr << " " << it->get()->name() << endl;
773 // we require the name to exist
782 bool LyXTextClass::delete_layout(string const & name)
784 if (name == defaultLayoutName())
787 LayoutList::iterator it =
788 remove_if(layoutlist_.begin(), layoutlist_.end(),
791 LayoutList::iterator end = layoutlist_.end();
792 bool const ret = (it != end);
793 layoutlist_.erase(it, end);
798 // Load textclass info if not loaded yet
799 bool LyXTextClass::load() const
805 string const real_file = LibFileSearch("layouts", name_, "layout");
807 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
808 lyxerr << "Error reading `"
809 << MakeDisplayPath(real_file)
810 << "'\n(Check `" << name_
811 << "')\nCheck your installation and "
812 "try Options/Reconfigure..." << endl;
820 FloatList & LyXTextClass::floats()
822 return *floatlist_.get();
826 FloatList const & LyXTextClass::floats() const
828 return *floatlist_.get();
832 Counters & LyXTextClass::counters() const
838 string const & LyXTextClass::defaultLayoutName() const
840 // This really should come from the actual layout... (Lgb)
841 return defaultlayout_;
845 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
847 return operator[](defaultLayoutName());
851 string const & LyXTextClass::name() const
857 string const & LyXTextClass::latexname() const
859 const_cast<LyXTextClass*>(this)->load();
864 string const & LyXTextClass::description() const
870 string const & LyXTextClass::opt_fontsize() const
872 return opt_fontsize_;
876 string const & LyXTextClass::opt_pagestyle() const
878 return opt_pagestyle_;
882 string const & LyXTextClass::options() const
888 string const & LyXTextClass::pagestyle() const
894 string const & LyXTextClass::preamble() const
900 LyXTextClass::PageSides LyXTextClass::sides() const
906 int LyXTextClass::secnumdepth() const
912 int LyXTextClass::tocdepth() const
918 OutputType LyXTextClass::outputType() const
924 bool LyXTextClass::provides(LyXTextClass::Provides p) const
926 return provides_ & p;
930 unsigned int LyXTextClass::columns() const
936 int LyXTextClass::maxcounter() const
942 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
948 string const & LyXTextClass::titlename() const
957 int LyXTextClass::size() const
959 return layoutlist_.size();
963 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
966 case LyXTextClass::OneSide:
969 case LyXTextClass::TwoSides: