3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
8 * \author Angus Leeming
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
17 #include "lyxtextclass.h"
22 #include "FloatList.h"
24 #include "support/lstrings.h"
25 #include "support/LAssert.h"
26 #include "support/lyxfunctional.h"
27 #include "support/filetools.h"
31 using namespace lyx::support;
42 compare_name(string const & name)
46 bool operator()(boost::shared_ptr<LyXLayout> const & c)
48 return c->name() == name_;
58 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
59 string const & desc, bool texClassAvail )
60 : name_(fn), latexname_(cln), description_(desc),
61 floatlist_(new FloatList), ctrs_(new Counters), texClassAvail_(texClassAvail)
68 pagestyle_ = "default";
69 maxcounter_ = LABEL_COUNTER_CHAPTER;
70 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
71 opt_fontsize_ = "10|11|12";
72 opt_pagestyle_ = "empty|plain|headings|fancy";
74 titletype_ = TITLE_COMMAND_AFTER;
75 titlename_ = "maketitle";
80 bool LyXTextClass::isTeXClassAvailable() const
82 return texClassAvail_;
86 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
88 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
89 if (!lay.Read(lexrc, *this)) {
91 lay.resfont = lay.font;
92 lay.resfont.realize(defaultfont());
93 lay.reslabelfont = lay.labelfont;
94 lay.reslabelfont.realize(defaultfont());
95 return false; // no errors
97 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
131 // Reads a textclass structure from file.
132 bool LyXTextClass::Read(string const & filename, bool merge)
134 keyword_item textClassTags[] = {
135 { "classoptions", TC_CLASSOPTIONS },
136 { "columns", TC_COLUMNS },
137 { "counter", TC_COUNTER },
138 { "defaultfont", TC_DEFAULTFONT },
139 { "defaultstyle", TC_DEFAULTSTYLE },
140 { "environment", TC_ENVIRONMENT },
141 { "float", TC_FLOAT },
142 { "input", TC_INPUT },
143 { "leftmargin", TC_LEFTMARGIN },
144 { "maxcounter", TC_MAXCOUNTER },
145 { "nofloat", TC_NOFLOAT },
146 { "nostyle", TC_NOSTYLE },
147 { "outputtype", TC_OUTPUTTYPE },
148 { "pagestyle", TC_PAGESTYLE },
149 { "preamble", TC_PREAMBLE },
150 { "providesamsmath", TC_PROVIDESAMSMATH },
151 { "providesmakeidx", TC_PROVIDESMAKEIDX },
152 { "providesnatbib", TC_PROVIDESNATBIB },
153 { "providesurl", TC_PROVIDESURL },
154 { "rightmargin", TC_RIGHTMARGIN },
155 { "secnumdepth", TC_SECNUMDEPTH },
156 { "sides", TC_SIDES },
157 { "style", TC_STYLE },
158 { "titlelatexname", TC_TITLELATEXNAME },
159 { "titlelatextype", TC_TITLELATEXTYPE },
160 { "tocdepth", TC_TOCDEPTH }
164 lyxerr[Debug::TCLASS] << "Reading textclass "
165 << MakeDisplayPath(filename)
168 lyxerr[Debug::TCLASS] << "Reading input file "
169 << MakeDisplayPath(filename)
172 LyXLex lexrc(textClassTags,
173 sizeof(textClassTags) / sizeof(textClassTags[0]));
176 lexrc.setFile(filename);
177 if (!lexrc.isOK()) error = true;
180 while (lexrc.isOK() && !error) {
181 int le = lexrc.lex();
184 case LyXLex::LEX_FEOF:
187 case LyXLex::LEX_UNDEF:
188 lexrc.printError("Unknown TextClass tag `$$Token'");
196 switch (static_cast<TextClassTags>(le)) {
198 case TC_OUTPUTTYPE: // output type definition
199 readOutputType(lexrc);
202 case TC_INPUT: // Include file
204 string tmp = LibFileSearch("layouts",
208 if (Read(tmp, true)) {
209 lexrc.printError("Error reading input"
216 case TC_DEFAULTSTYLE:
218 string const name = subst(lexrc.getString(),
220 defaultlayout_ = name;
227 string const name = subst(lexrc.getString(),
229 if (hasLayout(name)) {
230 LyXLayout * lay = operator[](name).get();
231 error = do_readStyle(lexrc, *lay);
235 if (le == TC_ENVIRONMENT)
236 lay.is_environment = true;
237 if (!(error = do_readStyle(lexrc, lay)))
238 layoutlist_.push_back
239 (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
240 if (defaultlayout_.empty()) {
241 // We do not have a default
242 // layout yet, so we choose
243 // the first layout we
245 defaultlayout_ = name;
250 lexrc.printError("No name given for style: `$$Token'.");
257 string const style = subst(lexrc.getString(),
259 if (!delete_layout(style))
260 lyxerr << "Cannot delete style `"
261 << style << '\'' << endl;
262 // lexrc.printError("Cannot delete style"
269 columns_ = lexrc.getInteger();
274 switch (lexrc.getInteger()) {
275 case 1: sides_ = OneSide; break;
276 case 2: sides_ = TwoSides; break;
278 lyxerr << "Impossible number of page"
279 " sides, setting to one."
289 pagestyle_ = rtrim(lexrc.getString());
293 defaultfont_.lyxRead(lexrc);
294 if (!defaultfont_.resolved()) {
295 lexrc.printError("Warning: defaultfont should "
296 "be fully instantiated!");
297 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
302 readMaxCounter(lexrc);
307 secnumdepth_ = lexrc.getInteger();
312 tocdepth_ = lexrc.getInteger();
315 // First step to support options
316 case TC_CLASSOPTIONS:
317 readClassOptions(lexrc);
321 preamble_ = lexrc.getLongString("EndPreamble");
324 case TC_PROVIDESAMSMATH:
325 if (lexrc.next() && lexrc.getInteger())
326 provides_ |= amsmath;
329 case TC_PROVIDESNATBIB:
330 if (lexrc.next() && lexrc.getInteger())
334 case TC_PROVIDESMAKEIDX:
335 if (lexrc.next() && lexrc.getInteger())
336 provides_ |= makeidx;
340 if (lexrc.next() && lexrc.getInteger())
344 case TC_LEFTMARGIN: // left margin type
346 leftmargin_ = lexrc.getString();
349 case TC_RIGHTMARGIN: // right margin type
351 rightmargin_ = lexrc.getString();
359 case TC_TITLELATEXTYPE:
360 readTitleType(lexrc);
362 case TC_TITLELATEXNAME:
364 titlename_ = lexrc.getString();
368 string const nofloat = lexrc.getString();
369 floatlist_->erase(nofloat);
375 if (!merge) { // we are at top level here.
376 lyxerr[Debug::TCLASS] << "Finished reading textclass "
377 << MakeDisplayPath(filename)
379 if (defaultlayout_.empty()) {
380 lyxerr << "Error: Textclass '" << name_
381 << "' is missing a defaultstyle." << endl;
385 lyxerr[Debug::TCLASS] << "Finished reading input file "
386 << MakeDisplayPath(filename)
393 void LyXTextClass::readTitleType(LyXLex & lexrc)
395 keyword_item titleTypeTags[] = {
396 { "commandafter", TITLE_COMMAND_AFTER },
397 { "environment", TITLE_ENVIRONMENT }
400 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
402 int le = lexrc.lex();
404 case LyXLex::LEX_UNDEF:
405 lexrc.printError("Unknown output type `$$Token'");
407 case TITLE_COMMAND_AFTER:
408 case TITLE_ENVIRONMENT:
409 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
412 lyxerr << "Unhandled value " << le
413 << " in LyXTextClass::readTitleType." << endl;
420 void LyXTextClass::readOutputType(LyXLex & lexrc)
422 keyword_item outputTypeTags[] = {
423 { "docbook", DOCBOOK },
425 { "linuxdoc", LINUXDOC },
426 { "literate", LITERATE }
429 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
431 int le = lexrc.lex();
433 case LyXLex::LEX_UNDEF:
434 lexrc.printError("Unknown output type `$$Token'");
440 outputType_ = static_cast<OutputType>(le);
443 lyxerr << "Unhandled value " << le
444 << " in LyXTextClass::readOutputType." << endl;
451 enum MaxCounterTags {
452 MC_COUNTER_CHAPTER = 1,
454 MC_COUNTER_SUBSECTION,
455 MC_COUNTER_SUBSUBSECTION,
456 MC_COUNTER_PARAGRAPH,
457 MC_COUNTER_SUBPARAGRAPH,
465 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
467 keyword_item maxCounterTags[] = {
468 {"counter_chapter", MC_COUNTER_CHAPTER },
469 {"counter_enumi", MC_COUNTER_ENUMI },
470 {"counter_enumii", MC_COUNTER_ENUMII },
471 {"counter_enumiii", MC_COUNTER_ENUMIII },
472 {"counter_enumiv", MC_COUNTER_ENUMIV },
473 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
474 {"counter_section", MC_COUNTER_SECTION },
475 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
476 {"counter_subsection", MC_COUNTER_SUBSECTION },
477 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
480 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
482 int le = lexrc.lex();
484 case LyXLex::LEX_UNDEF:
485 lexrc.printError("Unknown MaxCounter tag `$$Token'");
491 switch (static_cast<MaxCounterTags>(le)) {
492 case MC_COUNTER_CHAPTER:
493 maxcounter_ = LABEL_COUNTER_CHAPTER;
495 case MC_COUNTER_SECTION:
496 maxcounter_ = LABEL_COUNTER_SECTION;
498 case MC_COUNTER_SUBSECTION:
499 maxcounter_ = LABEL_COUNTER_SUBSECTION;
501 case MC_COUNTER_SUBSUBSECTION:
502 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
504 case MC_COUNTER_PARAGRAPH:
505 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
507 case MC_COUNTER_SUBPARAGRAPH:
508 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
510 case MC_COUNTER_ENUMI:
511 maxcounter_ = LABEL_COUNTER_ENUMI;
513 case MC_COUNTER_ENUMII:
514 maxcounter_ = LABEL_COUNTER_ENUMII;
516 case MC_COUNTER_ENUMIII:
517 maxcounter_ = LABEL_COUNTER_ENUMIII;
519 case MC_COUNTER_ENUMIV:
520 maxcounter_ = LABEL_COUNTER_ENUMIV;
526 enum ClassOptionsTags {
534 void LyXTextClass::readClassOptions(LyXLex & lexrc)
536 keyword_item classOptionsTags[] = {
538 {"fontsize", CO_FONTSIZE },
539 {"other", CO_OTHER },
540 {"pagestyle", CO_PAGESTYLE }
543 lexrc.pushTable(classOptionsTags, CO_END);
545 while (!getout && lexrc.isOK()) {
546 int le = lexrc.lex();
548 case LyXLex::LEX_UNDEF:
549 lexrc.printError("Unknown ClassOption tag `$$Token'");
553 switch (static_cast<ClassOptionsTags>(le)) {
556 opt_fontsize_ = rtrim(lexrc.getString());
560 opt_pagestyle_ = rtrim(lexrc.getString());
564 options_ = lexrc.getString();
587 void LyXTextClass::readFloat(LyXLex & lexrc)
589 keyword_item floatTags[] = {
591 { "extension", FT_EXT },
592 { "guiname", FT_NAME },
593 { "latexbuiltin", FT_BUILTIN },
594 { "listname", FT_LISTNAME },
595 { "numberwithin", FT_WITHIN },
596 { "placement", FT_PLACEMENT },
597 { "style", FT_STYLE },
601 lexrc.pushTable(floatTags, FT_END);
610 bool builtin = false;
613 while (!getout && lexrc.isOK()) {
614 int le = lexrc.lex();
616 case LyXLex::LEX_UNDEF:
617 lexrc.printError("Unknown ClassOption tag `$$Token'");
621 switch (static_cast<FloatTags>(le)) {
624 type = lexrc.getString();
625 // Here we could check if this type is already defined
626 // and modify it with the rest of the vars instead.
630 name = lexrc.getString();
634 placement = lexrc.getString();
638 ext = lexrc.getString();
642 within = lexrc.getString();
643 if (within == "none")
648 style = lexrc.getString();
652 listname = lexrc.getString();
656 builtin = lexrc.getBool();
664 // Here if have a full float if getout == true
666 Floating newfloat(type, placement, ext, within,
667 style, name, listname, builtin);
668 floatlist_->newFloat(newfloat);
681 void LyXTextClass::readCounter(LyXLex & lexrc)
683 keyword_item counterTags[] = {
686 { "within", CT_WITHIN }
689 lexrc.pushTable(counterTags, CT_END);
695 while (!getout && lexrc.isOK()) {
696 int le = lexrc.lex();
698 case LyXLex::LEX_UNDEF:
699 lexrc.printError("Unknown ClassOption tag `$$Token'");
703 switch (static_cast<CounterTags>(le)) {
706 name = lexrc.getString();
710 within = lexrc.getString();
711 if (within == "none")
720 // Here if have a full float if getout == true
722 if (within.empty()) {
723 ctrs_->newCounter(name);
725 ctrs_->newCounter(name, within);
733 LyXFont const & LyXTextClass::defaultfont() const
739 string const & LyXTextClass::leftmargin() const
745 string const & LyXTextClass::rightmargin() const
751 bool LyXTextClass::hasLayout(string const & n) const
753 string const name = (n.empty() ? defaultLayoutName() : n);
755 return find_if(layoutlist_.begin(), layoutlist_.end(),
757 != layoutlist_.end();
762 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
764 Assert(!name.empty());
766 LayoutList::const_iterator cit =
767 find_if(layoutlist_.begin(),
771 if (cit == layoutlist_.end()) {
772 lyxerr << "We failed to find the layout '" << name
773 << "' in the layout list. You MUST investigate!"
775 for (LayoutList::const_iterator it = layoutlist_.begin();
776 it != layoutlist_.end(); ++it)
777 lyxerr << " " << it->get()->name() << endl;
779 // we require the name to exist
788 bool LyXTextClass::delete_layout(string const & name)
790 if (name == defaultLayoutName())
793 LayoutList::iterator it =
794 remove_if(layoutlist_.begin(), layoutlist_.end(),
797 LayoutList::iterator end = layoutlist_.end();
798 bool const ret = (it != end);
799 layoutlist_.erase(it, end);
804 // Load textclass info if not loaded yet
805 bool LyXTextClass::load() const
811 string const real_file = LibFileSearch("layouts", name_, "layout");
813 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
814 lyxerr << "Error reading `"
815 << MakeDisplayPath(real_file)
816 << "'\n(Check `" << name_
817 << "')\nCheck your installation and "
818 "try Options/Reconfigure..." << endl;
826 FloatList & LyXTextClass::floats()
828 return *floatlist_.get();
832 FloatList const & LyXTextClass::floats() const
834 return *floatlist_.get();
838 Counters & LyXTextClass::counters() const
844 string const & LyXTextClass::defaultLayoutName() const
846 // This really should come from the actual layout... (Lgb)
847 return defaultlayout_;
851 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
853 return operator[](defaultLayoutName());
857 string const & LyXTextClass::name() const
863 string const & LyXTextClass::latexname() const
865 const_cast<LyXTextClass*>(this)->load();
870 string const & LyXTextClass::description() const
876 string const & LyXTextClass::opt_fontsize() const
878 return opt_fontsize_;
882 string const & LyXTextClass::opt_pagestyle() const
884 return opt_pagestyle_;
888 string const & LyXTextClass::options() const
894 string const & LyXTextClass::pagestyle() const
900 string const & LyXTextClass::preamble() const
906 LyXTextClass::PageSides LyXTextClass::sides() const
912 int LyXTextClass::secnumdepth() const
918 int LyXTextClass::tocdepth() const
924 OutputType LyXTextClass::outputType() const
930 bool LyXTextClass::provides(LyXTextClass::Provides p) const
932 return provides_ & p;
936 unsigned int LyXTextClass::columns() const
942 int LyXTextClass::maxcounter() const
948 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
954 string const & LyXTextClass::titlename() const
963 int LyXTextClass::size() const
965 return layoutlist_.size();
969 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
972 case LyXTextClass::OneSide:
975 case LyXTextClass::TwoSides: