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,
54 : name_(fn), latexname_(cln), description_(desc),
55 floatlist_(new FloatList), ctrs_(new Counters)
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::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
76 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
77 if (!lay.Read(lexrc, *this)) {
79 lay.resfont = lay.font;
80 lay.resfont.realize(defaultfont());
81 lay.reslabelfont = lay.labelfont;
82 lay.reslabelfont.realize(defaultfont());
83 return false; // no errors
85 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
119 // Reads a textclass structure from file.
120 bool LyXTextClass::Read(string const & filename, bool merge)
122 keyword_item textClassTags[] = {
123 { "classoptions", TC_CLASSOPTIONS },
124 { "columns", TC_COLUMNS },
125 { "counter", TC_COUNTER },
126 { "defaultfont", TC_DEFAULTFONT },
127 { "defaultstyle", TC_DEFAULTSTYLE },
128 { "environment", TC_ENVIRONMENT },
129 { "float", TC_FLOAT },
130 { "input", TC_INPUT },
131 { "leftmargin", TC_LEFTMARGIN },
132 { "maxcounter", TC_MAXCOUNTER },
133 { "nofloat", TC_NOFLOAT },
134 { "nostyle", TC_NOSTYLE },
135 { "outputtype", TC_OUTPUTTYPE },
136 { "pagestyle", TC_PAGESTYLE },
137 { "preamble", TC_PREAMBLE },
138 { "providesamsmath", TC_PROVIDESAMSMATH },
139 { "providesmakeidx", TC_PROVIDESMAKEIDX },
140 { "providesnatbib", TC_PROVIDESNATBIB },
141 { "providesurl", TC_PROVIDESURL },
142 { "rightmargin", TC_RIGHTMARGIN },
143 { "secnumdepth", TC_SECNUMDEPTH },
144 { "sides", TC_SIDES },
145 { "style", TC_STYLE },
146 { "titlelatexname", TC_TITLELATEXNAME },
147 { "titlelatextype", TC_TITLELATEXTYPE },
148 { "tocdepth", TC_TOCDEPTH }
152 lyxerr[Debug::TCLASS] << "Reading textclass "
153 << MakeDisplayPath(filename)
156 lyxerr[Debug::TCLASS] << "Reading input file "
157 << MakeDisplayPath(filename)
160 LyXLex lexrc(textClassTags,
161 sizeof(textClassTags) / sizeof(textClassTags[0]));
164 lexrc.setFile(filename);
165 if (!lexrc.isOK()) error = true;
168 while (lexrc.isOK() && !error) {
169 int le = lexrc.lex();
172 case LyXLex::LEX_FEOF:
175 case LyXLex::LEX_UNDEF:
176 lexrc.printError("Unknown TextClass tag `$$Token'");
184 switch (static_cast<TextClassTags>(le)) {
186 case TC_OUTPUTTYPE: // output type definition
187 readOutputType(lexrc);
190 case TC_INPUT: // Include file
192 string tmp = LibFileSearch("layouts",
196 if (Read(tmp, true)) {
197 lexrc.printError("Error reading input"
204 case TC_DEFAULTSTYLE:
206 string const name = subst(lexrc.getString(),
208 defaultlayout_ = name;
214 string const name = subst(lexrc.getString(),
216 if (hasLayout(name)) {
217 LyXLayout * lay = operator[](name).get();
218 error = do_readStyle(lexrc, *lay);
222 if (!(error = do_readStyle(lexrc, lay)))
223 layoutlist_.push_back
224 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
225 if (defaultlayout_.empty()) {
226 // We do not have a default
227 // layout yet, so we choose
228 // the first layout we
230 defaultlayout_ = name;
235 lexrc.printError("No name given for style: `$$Token'.");
242 string const name = subst(lexrc.getString(),
244 if (hasLayout(name)) {
245 LyXLayout * lay = operator[](name).get();
246 error = do_readStyle(lexrc, *lay);
250 if (!(error = do_readStyle(lexrc, lay)))
252 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
254 lexrc.printError("Problems reading environment: `$$Token'.");
258 lexrc.printError("No name given for style: `$$Token'.");
265 string const style = subst(lexrc.getString(),
267 if (!delete_layout(style))
268 lyxerr << "Cannot delete style `"
269 << style << '\'' << endl;
270 // lexrc.printError("Cannot delete style"
277 columns_ = lexrc.getInteger();
282 switch (lexrc.getInteger()) {
283 case 1: sides_ = OneSide; break;
284 case 2: sides_ = TwoSides; break;
286 lyxerr << "Impossible number of page"
287 " sides, setting to one."
297 pagestyle_ = rtrim(lexrc.getString());
301 defaultfont_.lyxRead(lexrc);
302 if (!defaultfont_.resolved()) {
303 lexrc.printError("Warning: defaultfont should "
304 "be fully instantiated!");
305 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
310 readMaxCounter(lexrc);
315 secnumdepth_ = lexrc.getInteger();
320 tocdepth_ = lexrc.getInteger();
323 // First step to support options
324 case TC_CLASSOPTIONS:
325 readClassOptions(lexrc);
329 preamble_ = lexrc.getLongString("EndPreamble");
332 case TC_PROVIDESAMSMATH:
333 if (lexrc.next() && lexrc.getInteger())
334 provides_ |= amsmath;
337 case TC_PROVIDESNATBIB:
338 if (lexrc.next() && lexrc.getInteger())
342 case TC_PROVIDESMAKEIDX:
343 if (lexrc.next() && lexrc.getInteger())
344 provides_ |= makeidx;
348 if (lexrc.next() && lexrc.getInteger())
352 case TC_LEFTMARGIN: // left margin type
354 leftmargin_ = lexrc.getString();
357 case TC_RIGHTMARGIN: // right margin type
359 rightmargin_ = lexrc.getString();
367 case TC_TITLELATEXTYPE:
368 readTitleType(lexrc);
370 case TC_TITLELATEXNAME:
372 titlename_ = lexrc.getString();
376 string const nofloat = lexrc.getString();
377 floatlist_->erase(nofloat);
383 if (!merge) { // we are at top level here.
384 lyxerr[Debug::TCLASS] << "Finished reading textclass "
385 << MakeDisplayPath(filename)
387 if (defaultlayout_.empty()) {
388 lyxerr << "Error: Textclass '" << name_
389 << "' is missing a defaultstyle." << endl;
393 lyxerr[Debug::TCLASS] << "Finished reading input file "
394 << MakeDisplayPath(filename)
401 void LyXTextClass::readTitleType(LyXLex & lexrc)
403 keyword_item titleTypeTags[] = {
404 { "commandafter", TITLE_COMMAND_AFTER },
405 { "environment", TITLE_ENVIRONMENT }
408 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
410 int le = lexrc.lex();
412 case LyXLex::LEX_UNDEF:
413 lexrc.printError("Unknown output type `$$Token'");
415 case TITLE_COMMAND_AFTER:
416 case TITLE_ENVIRONMENT:
417 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
420 lyxerr << "Unhandled value " << le
421 << " in LyXTextClass::readTitleType." << endl;
428 void LyXTextClass::readOutputType(LyXLex & lexrc)
430 keyword_item outputTypeTags[] = {
431 { "docbook", DOCBOOK },
433 { "linuxdoc", LINUXDOC },
434 { "literate", LITERATE }
437 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
439 int le = lexrc.lex();
441 case LyXLex::LEX_UNDEF:
442 lexrc.printError("Unknown output type `$$Token'");
448 outputType_ = static_cast<OutputType>(le);
451 lyxerr << "Unhandled value " << le
452 << " in LyXTextClass::readOutputType." << endl;
459 enum MaxCounterTags {
460 MC_COUNTER_CHAPTER = 1,
462 MC_COUNTER_SUBSECTION,
463 MC_COUNTER_SUBSUBSECTION,
464 MC_COUNTER_PARAGRAPH,
465 MC_COUNTER_SUBPARAGRAPH,
473 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
475 keyword_item maxCounterTags[] = {
476 {"counter_chapter", MC_COUNTER_CHAPTER },
477 {"counter_enumi", MC_COUNTER_ENUMI },
478 {"counter_enumii", MC_COUNTER_ENUMII },
479 {"counter_enumiii", MC_COUNTER_ENUMIII },
480 {"counter_enumiv", MC_COUNTER_ENUMIV },
481 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
482 {"counter_section", MC_COUNTER_SECTION },
483 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
484 {"counter_subsection", MC_COUNTER_SUBSECTION },
485 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
488 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
490 int le = lexrc.lex();
492 case LyXLex::LEX_UNDEF:
493 lexrc.printError("Unknown MaxCounter tag `$$Token'");
499 switch (static_cast<MaxCounterTags>(le)) {
500 case MC_COUNTER_CHAPTER:
501 maxcounter_ = LABEL_COUNTER_CHAPTER;
503 case MC_COUNTER_SECTION:
504 maxcounter_ = LABEL_COUNTER_SECTION;
506 case MC_COUNTER_SUBSECTION:
507 maxcounter_ = LABEL_COUNTER_SUBSECTION;
509 case MC_COUNTER_SUBSUBSECTION:
510 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
512 case MC_COUNTER_PARAGRAPH:
513 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
515 case MC_COUNTER_SUBPARAGRAPH:
516 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
518 case MC_COUNTER_ENUMI:
519 maxcounter_ = LABEL_COUNTER_ENUMI;
521 case MC_COUNTER_ENUMII:
522 maxcounter_ = LABEL_COUNTER_ENUMII;
524 case MC_COUNTER_ENUMIII:
525 maxcounter_ = LABEL_COUNTER_ENUMIII;
527 case MC_COUNTER_ENUMIV:
528 maxcounter_ = LABEL_COUNTER_ENUMIV;
534 enum ClassOptionsTags {
542 void LyXTextClass::readClassOptions(LyXLex & lexrc)
544 keyword_item classOptionsTags[] = {
546 {"fontsize", CO_FONTSIZE },
547 {"other", CO_OTHER },
548 {"pagestyle", CO_PAGESTYLE }
551 lexrc.pushTable(classOptionsTags, CO_END);
553 while (!getout && lexrc.isOK()) {
554 int le = lexrc.lex();
556 case LyXLex::LEX_UNDEF:
557 lexrc.printError("Unknown ClassOption tag `$$Token'");
561 switch (static_cast<ClassOptionsTags>(le)) {
564 opt_fontsize_ = rtrim(lexrc.getString());
568 opt_pagestyle_ = rtrim(lexrc.getString());
572 options_ = lexrc.getString();
595 void LyXTextClass::readFloat(LyXLex & lexrc)
597 keyword_item floatTags[] = {
599 { "extension", FT_EXT },
600 { "guiname", FT_NAME },
601 { "latexbuiltin", FT_BUILTIN },
602 { "listname", FT_LISTNAME },
603 { "numberwithin", FT_WITHIN },
604 { "placement", FT_PLACEMENT },
605 { "style", FT_STYLE },
609 lexrc.pushTable(floatTags, FT_END);
618 bool builtin = false;
621 while (!getout && lexrc.isOK()) {
622 int le = lexrc.lex();
624 case LyXLex::LEX_UNDEF:
625 lexrc.printError("Unknown ClassOption tag `$$Token'");
629 switch (static_cast<FloatTags>(le)) {
632 type = lexrc.getString();
633 // Here we could check if this type is already defined
634 // and modify it with the rest of the vars instead.
638 name = lexrc.getString();
642 placement = lexrc.getString();
646 ext = lexrc.getString();
650 within = lexrc.getString();
651 if (within == "none")
656 style = lexrc.getString();
660 listname = lexrc.getString();
664 builtin = lexrc.getBool();
672 // Here if have a full float if getout == true
674 Floating newfloat(type, placement, ext, within,
675 style, name, listname, builtin);
676 floatlist_->newFloat(newfloat);
689 void LyXTextClass::readCounter(LyXLex & lexrc)
691 keyword_item counterTags[] = {
694 { "within", CT_WITHIN }
697 lexrc.pushTable(counterTags, CT_END);
703 while (!getout && lexrc.isOK()) {
704 int le = lexrc.lex();
706 case LyXLex::LEX_UNDEF:
707 lexrc.printError("Unknown ClassOption tag `$$Token'");
711 switch (static_cast<CounterTags>(le)) {
714 name = lexrc.getString();
718 within = lexrc.getString();
719 if (within == "none")
728 // Here if have a full float if getout == true
730 if (within.empty()) {
731 ctrs_->newCounter(name);
733 ctrs_->newCounter(name, within);
741 LyXFont const & LyXTextClass::defaultfont() const
747 string const & LyXTextClass::leftmargin() const
753 string const & LyXTextClass::rightmargin() const
759 bool LyXTextClass::hasLayout(string const & n) const
761 string const name = (n.empty() ? defaultLayoutName() : n);
763 return find_if(layoutlist_.begin(), layoutlist_.end(),
765 != layoutlist_.end();
769 LyXLayout_ptr const & LyXTextClass::operator[](string const & n) const
771 lyx::Assert(!n.empty());
774 lyxerr << "LyXTextClass::operator[] called with empty n" << endl;
776 string const name = (n.empty() ? defaultLayoutName() : n);
778 static string lastLayoutName;
779 static LayoutList::difference_type lastLayoutIndex;
781 if (name == lastLayoutName)
782 return layoutlist_[lastLayoutIndex];
784 LayoutList::const_iterator cit =
785 find_if(layoutlist_.begin(),
789 if (cit == layoutlist_.end()) {
790 lyxerr << "We failed to find the layout '" << name
791 << "' in the layout list. You MUST investigate!"
794 // we require the name to exist
798 lastLayoutName = name;
799 lastLayoutIndex = std::distance(layoutlist_.begin(), cit);
805 LyXLayout_ptr const & LyXTextClass::getEnv(string const & name) const
807 lyx::Assert(!name.empty());
810 lyxerr << "LyXTextClass::getEnv() called with empty n" << endl;
812 LayoutList::const_iterator cit =
813 find_if(envlist_.begin(), envlist_.end(), compare_name(name));
815 if (cit == envlist_.end()) {
816 lyxerr << "We failed to find the environment '" << name
817 << "' in the layout list. You MUST investigate!"
819 // we require the name to exist
827 bool LyXTextClass::delete_layout(string const & name)
829 if (name == defaultLayoutName())
832 LayoutList::iterator it =
833 remove_if(layoutlist_.begin(), layoutlist_.end(),
836 LayoutList::iterator end = layoutlist_.end();
837 bool const ret = (it != end);
838 layoutlist_.erase(it, end);
843 // Load textclass info if not loaded yet
844 bool LyXTextClass::load() const
850 string const real_file = LibFileSearch("layouts", name_, "layout");
852 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
853 lyxerr << "Error reading `"
854 << MakeDisplayPath(real_file)
855 << "'\n(Check `" << name_
856 << "')\nCheck your installation and "
857 "try Options/Reconfigure..." << endl;
865 FloatList & LyXTextClass::floats()
867 return *floatlist_.get();
871 FloatList const & LyXTextClass::floats() const
873 return *floatlist_.get();
877 Counters & LyXTextClass::counters() const
883 string const & LyXTextClass::defaultLayoutName() const
885 // This really should come from the actual layout... (Lgb)
886 return defaultlayout_;
890 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
892 return operator[](defaultLayoutName());
896 string const & LyXTextClass::name() const
902 string const & LyXTextClass::latexname() const
904 const_cast<LyXTextClass*>(this)->load();
909 string const & LyXTextClass::description() const
915 string const & LyXTextClass::opt_fontsize() const
917 return opt_fontsize_;
921 string const & LyXTextClass::opt_pagestyle() const
923 return opt_pagestyle_;
927 string const & LyXTextClass::options() const
933 string const & LyXTextClass::pagestyle() const
939 string const & LyXTextClass::preamble() const
945 LyXTextClass::PageSides LyXTextClass::sides() const
951 int LyXTextClass::secnumdepth() const
957 int LyXTextClass::tocdepth() const
963 OutputType LyXTextClass::outputType() const
969 bool LyXTextClass::provides(LyXTextClass::Provides p) const
971 return provides_ & p;
975 unsigned int LyXTextClass::columns() const
981 int LyXTextClass::maxcounter() const
987 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
993 string const & LyXTextClass::titlename() const
1002 int LyXTextClass::size() const
1004 return layoutlist_.size();
1008 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1011 case LyXTextClass::OneSide:
1014 case LyXTextClass::TwoSides: