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;
220 string const name = subst(lexrc.getString(),
222 if (hasLayout(name)) {
223 LyXLayout * lay = operator[](name).get();
224 error = do_readStyle(lexrc, *lay);
228 if (!(error = do_readStyle(lexrc, lay)))
229 layoutlist_.push_back
230 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
231 if (defaultlayout_.empty()) {
232 // We do not have a default
233 // layout yet, so we choose
234 // the first layout we
236 defaultlayout_ = name;
241 lexrc.printError("No name given for style: `$$Token'.");
248 string const name = subst(lexrc.getString(),
250 if (hasLayout(name)) {
251 LyXLayout * lay = operator[](name).get();
252 error = do_readStyle(lexrc, *lay);
256 if (!(error = do_readStyle(lexrc, lay)))
258 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
260 lexrc.printError("Problems reading environment: `$$Token'.");
264 lexrc.printError("No name given for style: `$$Token'.");
271 string const style = subst(lexrc.getString(),
273 if (!delete_layout(style))
274 lyxerr << "Cannot delete style `"
275 << style << '\'' << endl;
276 // lexrc.printError("Cannot delete style"
283 columns_ = lexrc.getInteger();
288 switch (lexrc.getInteger()) {
289 case 1: sides_ = OneSide; break;
290 case 2: sides_ = TwoSides; break;
292 lyxerr << "Impossible number of page"
293 " sides, setting to one."
303 pagestyle_ = rtrim(lexrc.getString());
307 defaultfont_.lyxRead(lexrc);
308 if (!defaultfont_.resolved()) {
309 lexrc.printError("Warning: defaultfont should "
310 "be fully instantiated!");
311 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
316 readMaxCounter(lexrc);
321 secnumdepth_ = lexrc.getInteger();
326 tocdepth_ = lexrc.getInteger();
329 // First step to support options
330 case TC_CLASSOPTIONS:
331 readClassOptions(lexrc);
335 preamble_ = lexrc.getLongString("EndPreamble");
338 case TC_PROVIDESAMSMATH:
339 if (lexrc.next() && lexrc.getInteger())
340 provides_ |= amsmath;
343 case TC_PROVIDESNATBIB:
344 if (lexrc.next() && lexrc.getInteger())
348 case TC_PROVIDESMAKEIDX:
349 if (lexrc.next() && lexrc.getInteger())
350 provides_ |= makeidx;
354 if (lexrc.next() && lexrc.getInteger())
358 case TC_LEFTMARGIN: // left margin type
360 leftmargin_ = lexrc.getString();
363 case TC_RIGHTMARGIN: // right margin type
365 rightmargin_ = lexrc.getString();
373 case TC_TITLELATEXTYPE:
374 readTitleType(lexrc);
376 case TC_TITLELATEXNAME:
378 titlename_ = lexrc.getString();
382 string const nofloat = lexrc.getString();
383 floatlist_->erase(nofloat);
389 if (!merge) { // we are at top level here.
390 lyxerr[Debug::TCLASS] << "Finished reading textclass "
391 << MakeDisplayPath(filename)
393 if (defaultlayout_.empty()) {
394 lyxerr << "Error: Textclass '" << name_
395 << "' is missing a defaultstyle." << endl;
399 lyxerr[Debug::TCLASS] << "Finished reading input file "
400 << MakeDisplayPath(filename)
407 void LyXTextClass::readTitleType(LyXLex & lexrc)
409 keyword_item titleTypeTags[] = {
410 { "commandafter", TITLE_COMMAND_AFTER },
411 { "environment", TITLE_ENVIRONMENT }
414 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
416 int le = lexrc.lex();
418 case LyXLex::LEX_UNDEF:
419 lexrc.printError("Unknown output type `$$Token'");
421 case TITLE_COMMAND_AFTER:
422 case TITLE_ENVIRONMENT:
423 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
426 lyxerr << "Unhandled value " << le
427 << " in LyXTextClass::readTitleType." << endl;
434 void LyXTextClass::readOutputType(LyXLex & lexrc)
436 keyword_item outputTypeTags[] = {
437 { "docbook", DOCBOOK },
439 { "linuxdoc", LINUXDOC },
440 { "literate", LITERATE }
443 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
445 int le = lexrc.lex();
447 case LyXLex::LEX_UNDEF:
448 lexrc.printError("Unknown output type `$$Token'");
454 outputType_ = static_cast<OutputType>(le);
457 lyxerr << "Unhandled value " << le
458 << " in LyXTextClass::readOutputType." << endl;
465 enum MaxCounterTags {
466 MC_COUNTER_CHAPTER = 1,
468 MC_COUNTER_SUBSECTION,
469 MC_COUNTER_SUBSUBSECTION,
470 MC_COUNTER_PARAGRAPH,
471 MC_COUNTER_SUBPARAGRAPH,
479 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
481 keyword_item maxCounterTags[] = {
482 {"counter_chapter", MC_COUNTER_CHAPTER },
483 {"counter_enumi", MC_COUNTER_ENUMI },
484 {"counter_enumii", MC_COUNTER_ENUMII },
485 {"counter_enumiii", MC_COUNTER_ENUMIII },
486 {"counter_enumiv", MC_COUNTER_ENUMIV },
487 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
488 {"counter_section", MC_COUNTER_SECTION },
489 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
490 {"counter_subsection", MC_COUNTER_SUBSECTION },
491 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
494 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
496 int le = lexrc.lex();
498 case LyXLex::LEX_UNDEF:
499 lexrc.printError("Unknown MaxCounter tag `$$Token'");
505 switch (static_cast<MaxCounterTags>(le)) {
506 case MC_COUNTER_CHAPTER:
507 maxcounter_ = LABEL_COUNTER_CHAPTER;
509 case MC_COUNTER_SECTION:
510 maxcounter_ = LABEL_COUNTER_SECTION;
512 case MC_COUNTER_SUBSECTION:
513 maxcounter_ = LABEL_COUNTER_SUBSECTION;
515 case MC_COUNTER_SUBSUBSECTION:
516 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
518 case MC_COUNTER_PARAGRAPH:
519 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
521 case MC_COUNTER_SUBPARAGRAPH:
522 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
524 case MC_COUNTER_ENUMI:
525 maxcounter_ = LABEL_COUNTER_ENUMI;
527 case MC_COUNTER_ENUMII:
528 maxcounter_ = LABEL_COUNTER_ENUMII;
530 case MC_COUNTER_ENUMIII:
531 maxcounter_ = LABEL_COUNTER_ENUMIII;
533 case MC_COUNTER_ENUMIV:
534 maxcounter_ = LABEL_COUNTER_ENUMIV;
540 enum ClassOptionsTags {
548 void LyXTextClass::readClassOptions(LyXLex & lexrc)
550 keyword_item classOptionsTags[] = {
552 {"fontsize", CO_FONTSIZE },
553 {"other", CO_OTHER },
554 {"pagestyle", CO_PAGESTYLE }
557 lexrc.pushTable(classOptionsTags, CO_END);
559 while (!getout && lexrc.isOK()) {
560 int le = lexrc.lex();
562 case LyXLex::LEX_UNDEF:
563 lexrc.printError("Unknown ClassOption tag `$$Token'");
567 switch (static_cast<ClassOptionsTags>(le)) {
570 opt_fontsize_ = rtrim(lexrc.getString());
574 opt_pagestyle_ = rtrim(lexrc.getString());
578 options_ = lexrc.getString();
601 void LyXTextClass::readFloat(LyXLex & lexrc)
603 keyword_item floatTags[] = {
605 { "extension", FT_EXT },
606 { "guiname", FT_NAME },
607 { "latexbuiltin", FT_BUILTIN },
608 { "listname", FT_LISTNAME },
609 { "numberwithin", FT_WITHIN },
610 { "placement", FT_PLACEMENT },
611 { "style", FT_STYLE },
615 lexrc.pushTable(floatTags, FT_END);
624 bool builtin = false;
627 while (!getout && lexrc.isOK()) {
628 int le = lexrc.lex();
630 case LyXLex::LEX_UNDEF:
631 lexrc.printError("Unknown ClassOption tag `$$Token'");
635 switch (static_cast<FloatTags>(le)) {
638 type = lexrc.getString();
639 // Here we could check if this type is already defined
640 // and modify it with the rest of the vars instead.
644 name = lexrc.getString();
648 placement = lexrc.getString();
652 ext = lexrc.getString();
656 within = lexrc.getString();
657 if (within == "none")
662 style = lexrc.getString();
666 listname = lexrc.getString();
670 builtin = lexrc.getBool();
678 // Here if have a full float if getout == true
680 Floating newfloat(type, placement, ext, within,
681 style, name, listname, builtin);
682 floatlist_->newFloat(newfloat);
695 void LyXTextClass::readCounter(LyXLex & lexrc)
697 keyword_item counterTags[] = {
700 { "within", CT_WITHIN }
703 lexrc.pushTable(counterTags, CT_END);
709 while (!getout && lexrc.isOK()) {
710 int le = lexrc.lex();
712 case LyXLex::LEX_UNDEF:
713 lexrc.printError("Unknown ClassOption tag `$$Token'");
717 switch (static_cast<CounterTags>(le)) {
720 name = lexrc.getString();
724 within = lexrc.getString();
725 if (within == "none")
734 // Here if have a full float if getout == true
736 if (within.empty()) {
737 ctrs_->newCounter(name);
739 ctrs_->newCounter(name, within);
747 LyXFont const & LyXTextClass::defaultfont() const
753 string const & LyXTextClass::leftmargin() const
759 string const & LyXTextClass::rightmargin() const
765 bool LyXTextClass::hasLayout(string const & n) const
767 string const name = (n.empty() ? defaultLayoutName() : n);
769 return find_if(layoutlist_.begin(), layoutlist_.end(),
771 != layoutlist_.end();
775 LyXLayout_ptr const & LyXTextClass::operator[](string const & n) const
777 lyx::Assert(!n.empty());
780 lyxerr << "LyXTextClass::operator[] called with empty n" << endl;
782 string const name = (n.empty() ? defaultLayoutName() : n);
784 static string lastLayoutName;
785 static LayoutList::difference_type lastLayoutIndex;
787 if (name == lastLayoutName)
788 return layoutlist_[lastLayoutIndex];
790 LayoutList::const_iterator cit =
791 find_if(layoutlist_.begin(),
795 if (cit == layoutlist_.end()) {
796 lyxerr << "We failed to find the layout '" << name
797 << "' in the layout list. You MUST investigate!"
800 // we require the name to exist
804 lastLayoutName = name;
805 lastLayoutIndex = std::distance(layoutlist_.begin(), cit);
811 LyXLayout_ptr const & LyXTextClass::getEnv(string const & name) const
813 lyx::Assert(!name.empty());
816 lyxerr << "LyXTextClass::getEnv() called with empty n" << endl;
818 LayoutList::const_iterator cit =
819 find_if(envlist_.begin(), envlist_.end(), compare_name(name));
821 if (cit == envlist_.end()) {
822 lyxerr << "We failed to find the environment '" << name
823 << "' in the layout list. You MUST investigate!"
825 // we require the name to exist
833 bool LyXTextClass::delete_layout(string const & name)
835 if (name == defaultLayoutName())
838 LayoutList::iterator it =
839 remove_if(layoutlist_.begin(), layoutlist_.end(),
842 LayoutList::iterator end = layoutlist_.end();
843 bool const ret = (it != end);
844 layoutlist_.erase(it, end);
849 // Load textclass info if not loaded yet
850 bool LyXTextClass::load() const
856 string const real_file = LibFileSearch("layouts", name_, "layout");
858 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
859 lyxerr << "Error reading `"
860 << MakeDisplayPath(real_file)
861 << "'\n(Check `" << name_
862 << "')\nCheck your installation and "
863 "try Options/Reconfigure..." << endl;
871 FloatList & LyXTextClass::floats()
873 return *floatlist_.get();
877 FloatList const & LyXTextClass::floats() const
879 return *floatlist_.get();
883 Counters & LyXTextClass::counters() const
889 string const & LyXTextClass::defaultLayoutName() const
891 // This really should come from the actual layout... (Lgb)
892 return defaultlayout_;
896 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
898 return operator[](defaultLayoutName());
902 string const & LyXTextClass::name() const
908 string const & LyXTextClass::latexname() const
910 const_cast<LyXTextClass*>(this)->load();
915 string const & LyXTextClass::description() const
921 string const & LyXTextClass::opt_fontsize() const
923 return opt_fontsize_;
927 string const & LyXTextClass::opt_pagestyle() const
929 return opt_pagestyle_;
933 string const & LyXTextClass::options() const
939 string const & LyXTextClass::pagestyle() const
945 string const & LyXTextClass::preamble() const
951 LyXTextClass::PageSides LyXTextClass::sides() const
957 int LyXTextClass::secnumdepth() const
963 int LyXTextClass::tocdepth() const
969 OutputType LyXTextClass::outputType() const
975 bool LyXTextClass::provides(LyXTextClass::Provides p) const
977 return provides_ & p;
981 unsigned int LyXTextClass::columns() const
987 int LyXTextClass::maxcounter() const
993 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
999 string const & LyXTextClass::titlename() const
1008 int LyXTextClass::size() const
1010 return layoutlist_.size();
1014 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1017 case LyXTextClass::OneSide:
1020 case LyXTextClass::TwoSides: