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 * ======================================================
15 #pragma implementation
18 #include "lyxtextclass.h"
23 #include "support/lstrings.h"
24 #include "support/LAssert.h"
25 #include "support/lyxfunctional.h"
26 #include "support/filetools.h"
38 compare_name(string const & name)
41 bool operator()(C & c) {
42 return c->name() == name_;
50 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
52 : name_(fn), latexname_(cln), description_(desc), ctrs_(new Counters)
59 pagestyle_ = "default";
60 maxcounter_ = LABEL_COUNTER_CHAPTER;
61 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
62 opt_fontsize_ = "10|11|12";
63 opt_pagestyle_ = "empty|plain|headings|fancy";
69 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
71 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
72 if (!lay.Read(lexrc, *this)) {
74 lay.resfont = lay.font;
75 lay.resfont.realize(defaultfont());
76 lay.reslabelfont = lay.labelfont;
77 lay.reslabelfont.realize(defaultfont());
78 return false; // no errors
80 lyxerr << "Error parsing style `" << lay.name() << "'" << endl;
111 // Reads a textclass structure from file.
112 bool LyXTextClass::Read(string const & filename, bool merge)
114 keyword_item textClassTags[] = {
115 { "classoptions", TC_CLASSOPTIONS },
116 { "columns", TC_COLUMNS },
117 { "counter", TC_COUNTER },
118 { "defaultfont", TC_DEFAULTFONT },
119 { "defaultstyle", TC_DEFAULTSTYLE },
120 { "float", TC_FLOAT },
121 { "input", TC_INPUT },
122 { "leftmargin", TC_LEFTMARGIN },
123 { "maxcounter", TC_MAXCOUNTER },
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_COUNTER);
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 `" << style << "'" << endl;
228 // lexrc.printError("Cannot delete style"
235 columns_ = lexrc.getInteger();
240 switch (lexrc.getInteger()) {
241 case 1: sides_ = OneSide; break;
242 case 2: sides_ = TwoSides; break;
244 lyxerr << "Impossible number of page"
245 " sides, setting to one."
255 pagestyle_ = rtrim(lexrc.getString());
259 defaultfont_.lyxRead(lexrc);
260 if (!defaultfont_.resolved()) {
261 lexrc.printError("Warning: defaultfont should "
262 "be fully instantiated!");
263 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
268 readMaxCounter(lexrc);
273 secnumdepth_ = lexrc.getInteger();
278 tocdepth_ = lexrc.getInteger();
281 // First step to support options
282 case TC_CLASSOPTIONS:
283 readClassOptions(lexrc);
287 preamble_ = lexrc.getLongString("EndPreamble");
290 case TC_PROVIDESAMSMATH:
291 if (lexrc.next() && lexrc.getInteger())
292 provides_ |= amsmath;
295 case TC_PROVIDESNATBIB:
296 if (lexrc.next() && lexrc.getInteger())
300 case TC_PROVIDESMAKEIDX:
301 if (lexrc.next() && lexrc.getInteger())
302 provides_ |= makeidx;
306 if (lexrc.next() && lexrc.getInteger())
310 case TC_LEFTMARGIN: // left margin type
312 leftmargin_ = lexrc.getString();
315 case TC_RIGHTMARGIN: // right margin type
317 rightmargin_ = lexrc.getString();
328 if (!merge) { // we are at top level here.
329 lyxerr[Debug::TCLASS] << "Finished reading textclass "
330 << MakeDisplayPath(filename)
332 if (defaultlayout_.empty()) {
333 lyxerr << "Error: Textclass '" << name_
334 << "' is missing a defaultstyle." << endl;
338 lyxerr[Debug::TCLASS] << "Finished reading input file "
339 << MakeDisplayPath(filename)
346 void LyXTextClass::readOutputType(LyXLex & lexrc)
348 keyword_item outputTypeTags[] = {
349 { "docbook", DOCBOOK },
351 { "linuxdoc", LINUXDOC },
352 { "literate", LITERATE }
355 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
357 int le = lexrc.lex();
359 case LyXLex::LEX_UNDEF:
360 lexrc.printError("Unknown output type `$$Token'");
366 outputType_ = static_cast<OutputType>(le);
369 lyxerr << "Unhandled value " << le
370 << " in LyXTextClass::readOutputType." << endl;
377 enum MaxCounterTags {
378 MC_COUNTER_CHAPTER = 1,
380 MC_COUNTER_SUBSECTION,
381 MC_COUNTER_SUBSUBSECTION,
382 MC_COUNTER_PARAGRAPH,
383 MC_COUNTER_SUBPARAGRAPH,
391 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
393 keyword_item maxCounterTags[] = {
394 {"counter_chapter", MC_COUNTER_CHAPTER },
395 {"counter_enumi", MC_COUNTER_ENUMI },
396 {"counter_enumii", MC_COUNTER_ENUMII },
397 {"counter_enumiii", MC_COUNTER_ENUMIII },
398 {"counter_enumiv", MC_COUNTER_ENUMIV },
399 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
400 {"counter_section", MC_COUNTER_SECTION },
401 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
402 {"counter_subsection", MC_COUNTER_SUBSECTION },
403 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
406 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
407 int le = lexrc.lex();
409 case LyXLex::LEX_UNDEF:
410 lexrc.printError("Unknown MaxCounter tag `$$Token'");
414 switch (static_cast<MaxCounterTags>(le)) {
415 case MC_COUNTER_CHAPTER:
416 maxcounter_ = LABEL_COUNTER_CHAPTER;
418 case MC_COUNTER_SECTION:
419 maxcounter_ = LABEL_COUNTER_SECTION;
421 case MC_COUNTER_SUBSECTION:
422 maxcounter_ = LABEL_COUNTER_SUBSECTION;
424 case MC_COUNTER_SUBSUBSECTION:
425 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
427 case MC_COUNTER_PARAGRAPH:
428 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
430 case MC_COUNTER_SUBPARAGRAPH:
431 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
433 case MC_COUNTER_ENUMI:
434 maxcounter_ = LABEL_COUNTER_ENUMI;
436 case MC_COUNTER_ENUMII:
437 maxcounter_ = LABEL_COUNTER_ENUMII;
439 case MC_COUNTER_ENUMIII:
440 maxcounter_ = LABEL_COUNTER_ENUMIII;
442 case MC_COUNTER_ENUMIV:
443 maxcounter_ = LABEL_COUNTER_ENUMIV;
449 enum ClassOptionsTags {
457 void LyXTextClass::readClassOptions(LyXLex & lexrc)
459 keyword_item classOptionsTags[] = {
461 {"fontsize", CO_FONTSIZE },
462 {"other", CO_OTHER },
463 {"pagestyle", CO_PAGESTYLE }
466 lexrc.pushTable(classOptionsTags, CO_END);
468 while (!getout && lexrc.isOK()) {
469 int le = lexrc.lex();
471 case LyXLex::LEX_UNDEF:
472 lexrc.printError("Unknown ClassOption tag `$$Token'");
476 switch (static_cast<ClassOptionsTags>(le)) {
479 opt_fontsize_ = rtrim(lexrc.getString());
483 opt_pagestyle_ = rtrim(lexrc.getString());
487 options_ = lexrc.getString();
510 void LyXTextClass::readFloat(LyXLex & lexrc)
512 keyword_item floatTags[] = {
514 { "extension", FT_EXT },
515 { "guiname", FT_NAME },
516 { "latexbuiltin", FT_BUILTIN },
517 { "listname", FT_LISTNAME },
518 { "numberwithin", FT_WITHIN },
519 { "placement", FT_PLACEMENT },
520 { "style", FT_STYLE },
524 lexrc.pushTable(floatTags, FT_END);
533 bool builtin = false;
536 while (!getout && lexrc.isOK()) {
537 int le = lexrc.lex();
539 case LyXLex::LEX_UNDEF:
540 lexrc.printError("Unknown ClassOption tag `$$Token'");
544 switch (static_cast<FloatTags>(le)) {
547 type = lexrc.getString();
548 // Here we could check if this type is already defined
549 // and modify it with the rest of the vars instead.
553 name = lexrc.getString();
557 placement = lexrc.getString();
561 ext = lexrc.getString();
565 within = lexrc.getString();
566 if (within == "none")
571 style = lexrc.getString();
575 listname = lexrc.getString();
579 builtin = lexrc.getBool();
587 // Here if have a full float if getout == true
589 Floating newfloat(type, placement, ext, within,
590 style, name, listname, builtin);
591 floatlist_.newFloat(newfloat);
604 void LyXTextClass::readCounter(LyXLex & lexrc)
606 keyword_item counterTags[] = {
609 { "within", CT_WITHIN }
612 lexrc.pushTable(counterTags, CT_END);
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<CounterTags>(le)) {
629 name = lexrc.getString();
633 within = lexrc.getString();
634 if (within == "none")
643 // Here if have a full float if getout == true
645 if (within.empty()) {
646 ctrs_->newCounter(name);
648 ctrs_->newCounter(name, within);
656 LyXFont const & LyXTextClass::defaultfont() const
662 string const & LyXTextClass::leftmargin() const
668 string const & LyXTextClass::rightmargin() const
674 bool LyXTextClass::hasLayout(string const & n) const
676 string const name = (n.empty() ? defaultLayoutName() : n);
678 return find_if(layoutlist_.begin(), layoutlist_.end(),
680 != layoutlist_.end();
684 LyXLayout_ptr const & LyXTextClass::operator[](string const & n) const
686 lyx::Assert(!n.empty());
689 lyxerr << "Operator[] called with empty n" << endl;
691 string const name = (n.empty() ? defaultLayoutName() : n);
693 static string lastLayoutName;
694 static LayoutList::difference_type lastLayoutIndex;
696 if (name == lastLayoutName)
697 return layoutlist_[lastLayoutIndex];
699 LayoutList::const_iterator cit =
700 find_if(layoutlist_.begin(),
704 if (cit == layoutlist_.end()) {
705 lyxerr << "We failed to find the layout '" << name
706 << "' in the layout list. You MUST investigate!"
709 // we require the name to exist
713 lastLayoutName = name;
714 lastLayoutIndex = std::distance(layoutlist_.begin(), cit);
720 bool LyXTextClass::delete_layout(string const & name)
722 if (name == defaultLayoutName())
725 LayoutList::iterator it =
726 remove_if(layoutlist_.begin(), layoutlist_.end(),
729 LayoutList::iterator end = layoutlist_.end();
730 bool const ret = (it != end);
731 layoutlist_.erase(it, end);
736 // Load textclass info if not loaded yet
737 bool LyXTextClass::load() const
743 string const real_file = LibFileSearch("layouts", name_, "layout");
745 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
746 lyxerr << "Error reading `"
747 << MakeDisplayPath(real_file)
748 << "'\n(Check `" << name_
749 << "')\nCheck your installation and "
750 "try Options/Reconfigure..." << endl;
758 FloatList & LyXTextClass::floats()
764 FloatList const & LyXTextClass::floats() const
770 Counters & LyXTextClass::counters() const
776 string const & LyXTextClass::defaultLayoutName() const
778 // This really should come from the actual layout... (Lgb)
779 return defaultlayout_;
783 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
785 return operator[](defaultLayoutName());
789 string const & LyXTextClass::name() const
795 string const & LyXTextClass::latexname() const
797 const_cast<LyXTextClass*>(this)->load();
802 string const & LyXTextClass::description() const
808 string const & LyXTextClass::opt_fontsize() const
810 return opt_fontsize_;
814 string const & LyXTextClass::opt_pagestyle() const
816 return opt_pagestyle_;
820 string const & LyXTextClass::options() const
826 string const & LyXTextClass::pagestyle() const
832 string const & LyXTextClass::preamble() const
838 LyXTextClass::PageSides LyXTextClass::sides() const
844 int LyXTextClass::secnumdepth() const
850 int LyXTextClass::tocdepth() const
856 OutputType LyXTextClass::outputType() const
862 bool LyXTextClass::provides(LyXTextClass::Provides p) const
864 return provides_ & p;
868 unsigned int LyXTextClass::columns() const
874 int LyXTextClass::maxcounter() const
880 int LyXTextClass::size() const
882 return layoutlist_.size();
886 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
889 case LyXTextClass::OneSide:
892 case LyXTextClass::TwoSides: