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";
63 titletype_ = TITLE_COMMAND_AFTER;
64 titlename_ = "maketitle";
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;
113 // Reads a textclass structure from file.
114 bool LyXTextClass::Read(string const & filename, bool merge)
116 keyword_item textClassTags[] = {
117 { "classoptions", TC_CLASSOPTIONS },
118 { "columns", TC_COLUMNS },
119 { "counter", TC_COUNTER },
120 { "defaultfont", TC_DEFAULTFONT },
121 { "defaultstyle", TC_DEFAULTSTYLE },
122 { "float", TC_FLOAT },
123 { "input", TC_INPUT },
124 { "leftmargin", TC_LEFTMARGIN },
125 { "maxcounter", TC_MAXCOUNTER },
126 { "nofloat", TC_NOFLOAT },
127 { "nostyle", TC_NOSTYLE },
128 { "outputtype", TC_OUTPUTTYPE },
129 { "pagestyle", TC_PAGESTYLE },
130 { "preamble", TC_PREAMBLE },
131 { "providesamsmath", TC_PROVIDESAMSMATH },
132 { "providesmakeidx", TC_PROVIDESMAKEIDX },
133 { "providesnatbib", TC_PROVIDESNATBIB },
134 { "providesurl", TC_PROVIDESURL },
135 { "rightmargin", TC_RIGHTMARGIN },
136 { "secnumdepth", TC_SECNUMDEPTH },
137 { "sides", TC_SIDES },
138 { "style", TC_STYLE },
139 { "titlelatexname", TC_TITLELATEXNAME },
140 { "titlelatextype", TC_TITLELATEXTYPE },
141 { "tocdepth", TC_TOCDEPTH }
145 lyxerr[Debug::TCLASS] << "Reading textclass "
146 << MakeDisplayPath(filename)
149 lyxerr[Debug::TCLASS] << "Reading input file "
150 << MakeDisplayPath(filename)
153 LyXLex lexrc(textClassTags, TC_TITLELATEXTYPE);
156 lexrc.setFile(filename);
157 if (!lexrc.isOK()) error = true;
160 while (lexrc.isOK() && !error) {
161 int le = lexrc.lex();
163 case LyXLex::LEX_FEOF:
166 case LyXLex::LEX_UNDEF:
167 lexrc.printError("Unknown TextClass tag `$$Token'");
172 switch (static_cast<TextClassTags>(le)) {
173 case TC_OUTPUTTYPE: // output type definition
174 readOutputType(lexrc);
177 case TC_INPUT: // Include file
179 string tmp = LibFileSearch("layouts",
183 if (Read(tmp, true)) {
184 lexrc.printError("Error reading input"
191 case TC_DEFAULTSTYLE:
193 string const name = subst(lexrc.getString(),
195 defaultlayout_ = name;
201 string const name = subst(lexrc.getString(),
203 if (hasLayout(name)) {
205 operator[](name).get();
206 error = do_readStyle(lexrc, *lay);
210 if (!(error = do_readStyle(lexrc, lay)))
211 layoutlist_.push_back(boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
212 if (defaultlayout_.empty()) {
213 // We do not have a default
214 // layout yet, so we choose
215 // the first layout we
217 defaultlayout_ = name;
222 lexrc.printError("No name given for style: `$$Token'.");
229 string const style = subst(lexrc.getString(),
231 if (!delete_layout(style))
232 lyxerr << "Cannot delete style `"
233 << style << '\'' << endl;
234 // lexrc.printError("Cannot delete style"
241 columns_ = lexrc.getInteger();
246 switch (lexrc.getInteger()) {
247 case 1: sides_ = OneSide; break;
248 case 2: sides_ = TwoSides; break;
250 lyxerr << "Impossible number of page"
251 " sides, setting to one."
261 pagestyle_ = rtrim(lexrc.getString());
265 defaultfont_.lyxRead(lexrc);
266 if (!defaultfont_.resolved()) {
267 lexrc.printError("Warning: defaultfont should "
268 "be fully instantiated!");
269 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
274 readMaxCounter(lexrc);
279 secnumdepth_ = lexrc.getInteger();
284 tocdepth_ = lexrc.getInteger();
287 // First step to support options
288 case TC_CLASSOPTIONS:
289 readClassOptions(lexrc);
293 preamble_ = lexrc.getLongString("EndPreamble");
296 case TC_PROVIDESAMSMATH:
297 if (lexrc.next() && lexrc.getInteger())
298 provides_ |= amsmath;
301 case TC_PROVIDESNATBIB:
302 if (lexrc.next() && lexrc.getInteger())
306 case TC_PROVIDESMAKEIDX:
307 if (lexrc.next() && lexrc.getInteger())
308 provides_ |= makeidx;
312 if (lexrc.next() && lexrc.getInteger())
316 case TC_LEFTMARGIN: // left margin type
318 leftmargin_ = lexrc.getString();
321 case TC_RIGHTMARGIN: // right margin type
323 rightmargin_ = lexrc.getString();
331 case TC_TITLELATEXTYPE:
332 readTitleType(lexrc);
334 case TC_TITLELATEXNAME:
336 titlename_ = lexrc.getString();
340 string const nofloat = lexrc.getString();
341 floatlist_->erase(nofloat);
347 if (!merge) { // we are at top level here.
348 lyxerr[Debug::TCLASS] << "Finished reading textclass "
349 << MakeDisplayPath(filename)
351 if (defaultlayout_.empty()) {
352 lyxerr << "Error: Textclass '" << name_
353 << "' is missing a defaultstyle." << endl;
357 lyxerr[Debug::TCLASS] << "Finished reading input file "
358 << MakeDisplayPath(filename)
365 void LyXTextClass::readTitleType(LyXLex & lexrc)
367 keyword_item titleTypeTags[] = {
368 { "commandafter", TITLE_COMMAND_AFTER },
369 { "environment", TITLE_ENVIRONMENT }
372 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
374 int le = lexrc.lex();
376 case LyXLex::LEX_UNDEF:
377 lexrc.printError("Unknown output type `$$Token'");
379 case TITLE_COMMAND_AFTER:
380 case TITLE_ENVIRONMENT:
381 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
384 lyxerr << "Unhandled value " << le
385 << " in LyXTextClass::readTitleType." << endl;
392 void LyXTextClass::readOutputType(LyXLex & lexrc)
394 keyword_item outputTypeTags[] = {
395 { "docbook", DOCBOOK },
397 { "linuxdoc", LINUXDOC },
398 { "literate", LITERATE }
401 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
403 int le = lexrc.lex();
405 case LyXLex::LEX_UNDEF:
406 lexrc.printError("Unknown output type `$$Token'");
412 outputType_ = static_cast<OutputType>(le);
415 lyxerr << "Unhandled value " << le
416 << " in LyXTextClass::readOutputType." << endl;
423 enum MaxCounterTags {
424 MC_COUNTER_CHAPTER = 1,
426 MC_COUNTER_SUBSECTION,
427 MC_COUNTER_SUBSUBSECTION,
428 MC_COUNTER_PARAGRAPH,
429 MC_COUNTER_SUBPARAGRAPH,
437 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
439 keyword_item maxCounterTags[] = {
440 {"counter_chapter", MC_COUNTER_CHAPTER },
441 {"counter_enumi", MC_COUNTER_ENUMI },
442 {"counter_enumii", MC_COUNTER_ENUMII },
443 {"counter_enumiii", MC_COUNTER_ENUMIII },
444 {"counter_enumiv", MC_COUNTER_ENUMIV },
445 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
446 {"counter_section", MC_COUNTER_SECTION },
447 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
448 {"counter_subsection", MC_COUNTER_SUBSECTION },
449 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
452 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
453 int le = lexrc.lex();
455 case LyXLex::LEX_UNDEF:
456 lexrc.printError("Unknown MaxCounter tag `$$Token'");
460 switch (static_cast<MaxCounterTags>(le)) {
461 case MC_COUNTER_CHAPTER:
462 maxcounter_ = LABEL_COUNTER_CHAPTER;
464 case MC_COUNTER_SECTION:
465 maxcounter_ = LABEL_COUNTER_SECTION;
467 case MC_COUNTER_SUBSECTION:
468 maxcounter_ = LABEL_COUNTER_SUBSECTION;
470 case MC_COUNTER_SUBSUBSECTION:
471 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
473 case MC_COUNTER_PARAGRAPH:
474 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
476 case MC_COUNTER_SUBPARAGRAPH:
477 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
479 case MC_COUNTER_ENUMI:
480 maxcounter_ = LABEL_COUNTER_ENUMI;
482 case MC_COUNTER_ENUMII:
483 maxcounter_ = LABEL_COUNTER_ENUMII;
485 case MC_COUNTER_ENUMIII:
486 maxcounter_ = LABEL_COUNTER_ENUMIII;
488 case MC_COUNTER_ENUMIV:
489 maxcounter_ = LABEL_COUNTER_ENUMIV;
495 enum ClassOptionsTags {
503 void LyXTextClass::readClassOptions(LyXLex & lexrc)
505 keyword_item classOptionsTags[] = {
507 {"fontsize", CO_FONTSIZE },
508 {"other", CO_OTHER },
509 {"pagestyle", CO_PAGESTYLE }
512 lexrc.pushTable(classOptionsTags, CO_END);
514 while (!getout && lexrc.isOK()) {
515 int le = lexrc.lex();
517 case LyXLex::LEX_UNDEF:
518 lexrc.printError("Unknown ClassOption tag `$$Token'");
522 switch (static_cast<ClassOptionsTags>(le)) {
525 opt_fontsize_ = rtrim(lexrc.getString());
529 opt_pagestyle_ = rtrim(lexrc.getString());
533 options_ = lexrc.getString();
556 void LyXTextClass::readFloat(LyXLex & lexrc)
558 keyword_item floatTags[] = {
560 { "extension", FT_EXT },
561 { "guiname", FT_NAME },
562 { "latexbuiltin", FT_BUILTIN },
563 { "listname", FT_LISTNAME },
564 { "numberwithin", FT_WITHIN },
565 { "placement", FT_PLACEMENT },
566 { "style", FT_STYLE },
570 lexrc.pushTable(floatTags, FT_END);
579 bool builtin = false;
582 while (!getout && lexrc.isOK()) {
583 int le = lexrc.lex();
585 case LyXLex::LEX_UNDEF:
586 lexrc.printError("Unknown ClassOption tag `$$Token'");
590 switch (static_cast<FloatTags>(le)) {
593 type = lexrc.getString();
594 // Here we could check if this type is already defined
595 // and modify it with the rest of the vars instead.
599 name = lexrc.getString();
603 placement = lexrc.getString();
607 ext = lexrc.getString();
611 within = lexrc.getString();
612 if (within == "none")
617 style = lexrc.getString();
621 listname = lexrc.getString();
625 builtin = lexrc.getBool();
633 // Here if have a full float if getout == true
635 Floating newfloat(type, placement, ext, within,
636 style, name, listname, builtin);
637 floatlist_->newFloat(newfloat);
650 void LyXTextClass::readCounter(LyXLex & lexrc)
652 keyword_item counterTags[] = {
655 { "within", CT_WITHIN }
658 lexrc.pushTable(counterTags, CT_END);
664 while (!getout && lexrc.isOK()) {
665 int le = lexrc.lex();
667 case LyXLex::LEX_UNDEF:
668 lexrc.printError("Unknown ClassOption tag `$$Token'");
672 switch (static_cast<CounterTags>(le)) {
675 name = lexrc.getString();
679 within = lexrc.getString();
680 if (within == "none")
689 // Here if have a full float if getout == true
691 if (within.empty()) {
692 ctrs_->newCounter(name);
694 ctrs_->newCounter(name, within);
702 LyXFont const & LyXTextClass::defaultfont() const
708 string const & LyXTextClass::leftmargin() const
714 string const & LyXTextClass::rightmargin() const
720 bool LyXTextClass::hasLayout(string const & n) const
722 string const name = (n.empty() ? defaultLayoutName() : n);
724 return find_if(layoutlist_.begin(), layoutlist_.end(),
726 != layoutlist_.end();
730 LyXLayout_ptr const & LyXTextClass::operator[](string const & n) const
732 lyx::Assert(!n.empty());
735 lyxerr << "Operator[] called with empty n" << endl;
737 string const name = (n.empty() ? defaultLayoutName() : n);
739 static string lastLayoutName;
740 static LayoutList::difference_type lastLayoutIndex;
742 if (name == lastLayoutName)
743 return layoutlist_[lastLayoutIndex];
745 LayoutList::const_iterator cit =
746 find_if(layoutlist_.begin(),
750 if (cit == layoutlist_.end()) {
751 lyxerr << "We failed to find the layout '" << name
752 << "' in the layout list. You MUST investigate!"
755 // we require the name to exist
759 lastLayoutName = name;
760 lastLayoutIndex = std::distance(layoutlist_.begin(), cit);
766 bool LyXTextClass::delete_layout(string const & name)
768 if (name == defaultLayoutName())
771 LayoutList::iterator it =
772 remove_if(layoutlist_.begin(), layoutlist_.end(),
775 LayoutList::iterator end = layoutlist_.end();
776 bool const ret = (it != end);
777 layoutlist_.erase(it, end);
782 // Load textclass info if not loaded yet
783 bool LyXTextClass::load() const
789 string const real_file = LibFileSearch("layouts", name_, "layout");
791 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
792 lyxerr << "Error reading `"
793 << MakeDisplayPath(real_file)
794 << "'\n(Check `" << name_
795 << "')\nCheck your installation and "
796 "try Options/Reconfigure..." << endl;
804 FloatList & LyXTextClass::floats()
806 return *floatlist_.get();
810 FloatList const & LyXTextClass::floats() const
812 return *floatlist_.get();
816 Counters & LyXTextClass::counters() const
822 string const & LyXTextClass::defaultLayoutName() const
824 // This really should come from the actual layout... (Lgb)
825 return defaultlayout_;
829 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
831 return operator[](defaultLayoutName());
835 string const & LyXTextClass::name() const
841 string const & LyXTextClass::latexname() const
843 const_cast<LyXTextClass*>(this)->load();
848 string const & LyXTextClass::description() const
854 string const & LyXTextClass::opt_fontsize() const
856 return opt_fontsize_;
860 string const & LyXTextClass::opt_pagestyle() const
862 return opt_pagestyle_;
866 string const & LyXTextClass::options() const
872 string const & LyXTextClass::pagestyle() const
878 string const & LyXTextClass::preamble() const
884 LyXTextClass::PageSides LyXTextClass::sides() const
890 int LyXTextClass::secnumdepth() const
896 int LyXTextClass::tocdepth() const
902 OutputType LyXTextClass::outputType() const
908 bool LyXTextClass::provides(LyXTextClass::Provides p) const
910 return provides_ & p;
914 unsigned int LyXTextClass::columns() const
920 int LyXTextClass::maxcounter() const
926 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
932 string const & LyXTextClass::titlename() const
941 int LyXTextClass::size() const
943 return layoutlist_.size();
947 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
950 case LyXTextClass::OneSide:
953 case LyXTextClass::TwoSides: