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"
35 compare_name(string const & name)
38 bool operator()(C & c) {
39 return c->name() == name_;
47 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
49 : name_(fn), latexname_(cln), description_(desc),
50 floatlist_(new FloatList), ctrs_(new Counters)
57 pagestyle_ = "default";
58 maxcounter_ = LABEL_COUNTER_CHAPTER;
59 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
60 opt_fontsize_ = "10|11|12";
61 opt_pagestyle_ = "empty|plain|headings|fancy";
67 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
69 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
70 if (!lay.Read(lexrc, *this)) {
72 lay.resfont = lay.font;
73 lay.resfont.realize(defaultfont());
74 lay.reslabelfont = lay.labelfont;
75 lay.reslabelfont.realize(defaultfont());
76 return false; // no errors
78 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
110 // Reads a textclass structure from file.
111 bool LyXTextClass::Read(string const & filename, bool merge)
113 keyword_item textClassTags[] = {
114 { "classoptions", TC_CLASSOPTIONS },
115 { "columns", TC_COLUMNS },
116 { "counter", TC_COUNTER },
117 { "defaultfont", TC_DEFAULTFONT },
118 { "defaultstyle", TC_DEFAULTSTYLE },
119 { "float", TC_FLOAT },
120 { "input", TC_INPUT },
121 { "leftmargin", TC_LEFTMARGIN },
122 { "maxcounter", TC_MAXCOUNTER },
123 { "nofloat", TC_NOFLOAT },
124 { "nostyle", TC_NOSTYLE },
125 { "outputtype", TC_OUTPUTTYPE },
126 { "pagestyle", TC_PAGESTYLE },
127 { "preamble", TC_PREAMBLE },
128 { "providesamsmath", TC_PROVIDESAMSMATH },
129 { "providesmakeidx", TC_PROVIDESMAKEIDX },
130 { "providesnatbib", TC_PROVIDESNATBIB },
131 { "providesurl", TC_PROVIDESURL },
132 { "rightmargin", TC_RIGHTMARGIN },
133 { "secnumdepth", TC_SECNUMDEPTH },
134 { "sides", TC_SIDES },
135 { "style", TC_STYLE },
136 { "tocdepth", TC_TOCDEPTH }
140 lyxerr[Debug::TCLASS] << "Reading textclass "
141 << MakeDisplayPath(filename)
144 lyxerr[Debug::TCLASS] << "Reading input file "
145 << MakeDisplayPath(filename)
148 LyXLex lexrc(textClassTags, TC_NOFLOAT);
151 lexrc.setFile(filename);
152 if (!lexrc.isOK()) error = true;
155 while (lexrc.isOK() && !error) {
156 int le = lexrc.lex();
158 case LyXLex::LEX_FEOF:
161 case LyXLex::LEX_UNDEF:
162 lexrc.printError("Unknown TextClass tag `$$Token'");
167 switch (static_cast<TextClassTags>(le)) {
168 case TC_OUTPUTTYPE: // output type definition
169 readOutputType(lexrc);
172 case TC_INPUT: // Include file
174 string tmp = LibFileSearch("layouts",
178 if (Read(tmp, true)) {
179 lexrc.printError("Error reading input"
186 case TC_DEFAULTSTYLE:
188 string const name = subst(lexrc.getString(),
190 defaultlayout_ = name;
196 string const name = subst(lexrc.getString(),
198 if (hasLayout(name)) {
200 operator[](name).get();
201 error = do_readStyle(lexrc, *lay);
205 if (!(error = do_readStyle(lexrc, lay)))
206 layoutlist_.push_back(boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
207 if (defaultlayout_.empty()) {
208 // We do not have a default
209 // layout yet, so we choose
210 // the first layout we
212 defaultlayout_ = name;
217 lexrc.printError("No name given for style: `$$Token'.");
224 string const style = subst(lexrc.getString(),
226 if (!delete_layout(style))
227 lyxerr << "Cannot delete style `"
228 << style << '\'' << endl;
229 // lexrc.printError("Cannot delete style"
236 columns_ = lexrc.getInteger();
241 switch (lexrc.getInteger()) {
242 case 1: sides_ = OneSide; break;
243 case 2: sides_ = TwoSides; break;
245 lyxerr << "Impossible number of page"
246 " sides, setting to one."
256 pagestyle_ = rtrim(lexrc.getString());
260 defaultfont_.lyxRead(lexrc);
261 if (!defaultfont_.resolved()) {
262 lexrc.printError("Warning: defaultfont should "
263 "be fully instantiated!");
264 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
269 readMaxCounter(lexrc);
274 secnumdepth_ = lexrc.getInteger();
279 tocdepth_ = lexrc.getInteger();
282 // First step to support options
283 case TC_CLASSOPTIONS:
284 readClassOptions(lexrc);
288 preamble_ = lexrc.getLongString("EndPreamble");
291 case TC_PROVIDESAMSMATH:
292 if (lexrc.next() && lexrc.getInteger())
293 provides_ |= amsmath;
296 case TC_PROVIDESNATBIB:
297 if (lexrc.next() && lexrc.getInteger())
301 case TC_PROVIDESMAKEIDX:
302 if (lexrc.next() && lexrc.getInteger())
303 provides_ |= makeidx;
307 if (lexrc.next() && lexrc.getInteger())
311 case TC_LEFTMARGIN: // left margin type
313 leftmargin_ = lexrc.getString();
316 case TC_RIGHTMARGIN: // right margin type
318 rightmargin_ = lexrc.getString();
328 string const nofloat = lexrc.getString();
329 floatlist_->erase(nofloat);
336 if (!merge) { // we are at top level here.
337 lyxerr[Debug::TCLASS] << "Finished reading textclass "
338 << MakeDisplayPath(filename)
340 if (defaultlayout_.empty()) {
341 lyxerr << "Error: Textclass '" << name_
342 << "' is missing a defaultstyle." << endl;
346 lyxerr[Debug::TCLASS] << "Finished reading input file "
347 << MakeDisplayPath(filename)
354 void LyXTextClass::readOutputType(LyXLex & lexrc)
356 keyword_item outputTypeTags[] = {
357 { "docbook", DOCBOOK },
359 { "linuxdoc", LINUXDOC },
360 { "literate", LITERATE }
363 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
365 int le = lexrc.lex();
367 case LyXLex::LEX_UNDEF:
368 lexrc.printError("Unknown output type `$$Token'");
374 outputType_ = static_cast<OutputType>(le);
377 lyxerr << "Unhandled value " << le
378 << " in LyXTextClass::readOutputType." << endl;
385 enum MaxCounterTags {
386 MC_COUNTER_CHAPTER = 1,
388 MC_COUNTER_SUBSECTION,
389 MC_COUNTER_SUBSUBSECTION,
390 MC_COUNTER_PARAGRAPH,
391 MC_COUNTER_SUBPARAGRAPH,
399 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
401 keyword_item maxCounterTags[] = {
402 {"counter_chapter", MC_COUNTER_CHAPTER },
403 {"counter_enumi", MC_COUNTER_ENUMI },
404 {"counter_enumii", MC_COUNTER_ENUMII },
405 {"counter_enumiii", MC_COUNTER_ENUMIII },
406 {"counter_enumiv", MC_COUNTER_ENUMIV },
407 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
408 {"counter_section", MC_COUNTER_SECTION },
409 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
410 {"counter_subsection", MC_COUNTER_SUBSECTION },
411 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
414 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
415 int le = lexrc.lex();
417 case LyXLex::LEX_UNDEF:
418 lexrc.printError("Unknown MaxCounter tag `$$Token'");
422 switch (static_cast<MaxCounterTags>(le)) {
423 case MC_COUNTER_CHAPTER:
424 maxcounter_ = LABEL_COUNTER_CHAPTER;
426 case MC_COUNTER_SECTION:
427 maxcounter_ = LABEL_COUNTER_SECTION;
429 case MC_COUNTER_SUBSECTION:
430 maxcounter_ = LABEL_COUNTER_SUBSECTION;
432 case MC_COUNTER_SUBSUBSECTION:
433 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
435 case MC_COUNTER_PARAGRAPH:
436 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
438 case MC_COUNTER_SUBPARAGRAPH:
439 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
441 case MC_COUNTER_ENUMI:
442 maxcounter_ = LABEL_COUNTER_ENUMI;
444 case MC_COUNTER_ENUMII:
445 maxcounter_ = LABEL_COUNTER_ENUMII;
447 case MC_COUNTER_ENUMIII:
448 maxcounter_ = LABEL_COUNTER_ENUMIII;
450 case MC_COUNTER_ENUMIV:
451 maxcounter_ = LABEL_COUNTER_ENUMIV;
457 enum ClassOptionsTags {
465 void LyXTextClass::readClassOptions(LyXLex & lexrc)
467 keyword_item classOptionsTags[] = {
469 {"fontsize", CO_FONTSIZE },
470 {"other", CO_OTHER },
471 {"pagestyle", CO_PAGESTYLE }
474 lexrc.pushTable(classOptionsTags, CO_END);
476 while (!getout && lexrc.isOK()) {
477 int le = lexrc.lex();
479 case LyXLex::LEX_UNDEF:
480 lexrc.printError("Unknown ClassOption tag `$$Token'");
484 switch (static_cast<ClassOptionsTags>(le)) {
487 opt_fontsize_ = rtrim(lexrc.getString());
491 opt_pagestyle_ = rtrim(lexrc.getString());
495 options_ = lexrc.getString();
518 void LyXTextClass::readFloat(LyXLex & lexrc)
520 keyword_item floatTags[] = {
522 { "extension", FT_EXT },
523 { "guiname", FT_NAME },
524 { "latexbuiltin", FT_BUILTIN },
525 { "listname", FT_LISTNAME },
526 { "numberwithin", FT_WITHIN },
527 { "placement", FT_PLACEMENT },
528 { "style", FT_STYLE },
532 lexrc.pushTable(floatTags, FT_END);
541 bool builtin = false;
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<FloatTags>(le)) {
555 type = lexrc.getString();
556 // Here we could check if this type is already defined
557 // and modify it with the rest of the vars instead.
561 name = lexrc.getString();
565 placement = lexrc.getString();
569 ext = lexrc.getString();
573 within = lexrc.getString();
574 if (within == "none")
579 style = lexrc.getString();
583 listname = lexrc.getString();
587 builtin = lexrc.getBool();
595 // Here if have a full float if getout == true
597 Floating newfloat(type, placement, ext, within,
598 style, name, listname, builtin);
599 floatlist_->newFloat(newfloat);
612 void LyXTextClass::readCounter(LyXLex & lexrc)
614 keyword_item counterTags[] = {
617 { "within", CT_WITHIN }
620 lexrc.pushTable(counterTags, CT_END);
626 while (!getout && lexrc.isOK()) {
627 int le = lexrc.lex();
629 case LyXLex::LEX_UNDEF:
630 lexrc.printError("Unknown ClassOption tag `$$Token'");
634 switch (static_cast<CounterTags>(le)) {
637 name = lexrc.getString();
641 within = lexrc.getString();
642 if (within == "none")
651 // Here if have a full float if getout == true
653 if (within.empty()) {
654 ctrs_->newCounter(name);
656 ctrs_->newCounter(name, within);
664 LyXFont const & LyXTextClass::defaultfont() const
670 string const & LyXTextClass::leftmargin() const
676 string const & LyXTextClass::rightmargin() const
682 bool LyXTextClass::hasLayout(string const & n) const
684 string const name = (n.empty() ? defaultLayoutName() : n);
686 return find_if(layoutlist_.begin(), layoutlist_.end(),
688 != layoutlist_.end();
692 LyXLayout_ptr const & LyXTextClass::operator[](string const & n) const
694 lyx::Assert(!n.empty());
697 lyxerr << "Operator[] called with empty n" << endl;
699 string const name = (n.empty() ? defaultLayoutName() : n);
701 static string lastLayoutName;
702 static LayoutList::difference_type lastLayoutIndex;
704 if (name == lastLayoutName)
705 return layoutlist_[lastLayoutIndex];
707 LayoutList::const_iterator cit =
708 find_if(layoutlist_.begin(),
712 if (cit == layoutlist_.end()) {
713 lyxerr << "We failed to find the layout '" << name
714 << "' in the layout list. You MUST investigate!"
717 // we require the name to exist
721 lastLayoutName = name;
722 lastLayoutIndex = std::distance(layoutlist_.begin(), cit);
728 bool LyXTextClass::delete_layout(string const & name)
730 if (name == defaultLayoutName())
733 LayoutList::iterator it =
734 remove_if(layoutlist_.begin(), layoutlist_.end(),
737 LayoutList::iterator end = layoutlist_.end();
738 bool const ret = (it != end);
739 layoutlist_.erase(it, end);
744 // Load textclass info if not loaded yet
745 bool LyXTextClass::load() const
751 string const real_file = LibFileSearch("layouts", name_, "layout");
753 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
754 lyxerr << "Error reading `"
755 << MakeDisplayPath(real_file)
756 << "'\n(Check `" << name_
757 << "')\nCheck your installation and "
758 "try Options/Reconfigure..." << endl;
766 FloatList & LyXTextClass::floats()
768 return *floatlist_.get();
772 FloatList const & LyXTextClass::floats() const
774 return *floatlist_.get();
778 Counters & LyXTextClass::counters() const
784 string const & LyXTextClass::defaultLayoutName() const
786 // This really should come from the actual layout... (Lgb)
787 return defaultlayout_;
791 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
793 return operator[](defaultLayoutName());
797 string const & LyXTextClass::name() const
803 string const & LyXTextClass::latexname() const
805 const_cast<LyXTextClass*>(this)->load();
810 string const & LyXTextClass::description() const
816 string const & LyXTextClass::opt_fontsize() const
818 return opt_fontsize_;
822 string const & LyXTextClass::opt_pagestyle() const
824 return opt_pagestyle_;
828 string const & LyXTextClass::options() const
834 string const & LyXTextClass::pagestyle() const
840 string const & LyXTextClass::preamble() const
846 LyXTextClass::PageSides LyXTextClass::sides() const
852 int LyXTextClass::secnumdepth() const
858 int LyXTextClass::tocdepth() const
864 OutputType LyXTextClass::outputType() const
870 bool LyXTextClass::provides(LyXTextClass::Provides p) const
872 return provides_ & p;
876 unsigned int LyXTextClass::columns() const
882 int LyXTextClass::maxcounter() const
888 int LyXTextClass::size() const
890 return layoutlist_.size();
894 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
897 case LyXTextClass::OneSide:
900 case LyXTextClass::TwoSides: