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/filetools.h"
27 using lyx::support::LibFileSearch;
28 using lyx::support::MakeDisplayPath;
29 using lyx::support::rtrim;
30 using lyx::support::subst;
43 compare_name(string const & name)
47 bool operator()(boost::shared_ptr<LyXLayout> const & c)
49 return c->name() == name_;
59 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
60 string const & desc, bool texClassAvail )
61 : name_(fn), latexname_(cln), description_(desc),
62 floatlist_(new FloatList), ctrs_(new Counters), texClassAvail_(texClassAvail)
69 pagestyle_ = "default";
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;
130 // Reads a textclass structure from file.
131 bool LyXTextClass::Read(string const & filename, bool merge)
133 keyword_item textClassTags[] = {
134 { "classoptions", TC_CLASSOPTIONS },
135 { "columns", TC_COLUMNS },
136 { "counter", TC_COUNTER },
137 { "defaultfont", TC_DEFAULTFONT },
138 { "defaultstyle", TC_DEFAULTSTYLE },
139 { "environment", TC_ENVIRONMENT },
140 { "float", TC_FLOAT },
141 { "input", TC_INPUT },
142 { "leftmargin", TC_LEFTMARGIN },
143 { "nofloat", TC_NOFLOAT },
144 { "nostyle", TC_NOSTYLE },
145 { "outputtype", TC_OUTPUTTYPE },
146 { "pagestyle", TC_PAGESTYLE },
147 { "preamble", TC_PREAMBLE },
148 { "providesamsmath", TC_PROVIDESAMSMATH },
149 { "providesmakeidx", TC_PROVIDESMAKEIDX },
150 { "providesnatbib", TC_PROVIDESNATBIB },
151 { "providesurl", TC_PROVIDESURL },
152 { "rightmargin", TC_RIGHTMARGIN },
153 { "secnumdepth", TC_SECNUMDEPTH },
154 { "sides", TC_SIDES },
155 { "style", TC_STYLE },
156 { "titlelatexname", TC_TITLELATEXNAME },
157 { "titlelatextype", TC_TITLELATEXTYPE },
158 { "tocdepth", TC_TOCDEPTH }
162 lyxerr[Debug::TCLASS] << "Reading textclass "
163 << MakeDisplayPath(filename)
166 lyxerr[Debug::TCLASS] << "Reading input file "
167 << MakeDisplayPath(filename)
170 LyXLex lexrc(textClassTags,
171 sizeof(textClassTags) / sizeof(textClassTags[0]));
174 lexrc.setFile(filename);
175 if (!lexrc.isOK()) error = true;
178 while (lexrc.isOK() && !error) {
179 int le = lexrc.lex();
182 case LyXLex::LEX_FEOF:
185 case LyXLex::LEX_UNDEF:
186 lexrc.printError("Unknown TextClass tag `$$Token'");
194 switch (static_cast<TextClassTags>(le)) {
196 case TC_OUTPUTTYPE: // output type definition
197 readOutputType(lexrc);
200 case TC_INPUT: // Include file
202 string tmp = LibFileSearch("layouts",
206 if (Read(tmp, true)) {
207 lexrc.printError("Error reading input"
214 case TC_DEFAULTSTYLE:
216 string const name = subst(lexrc.getString(),
218 defaultlayout_ = name;
225 string const name = subst(lexrc.getString(),
227 if (hasLayout(name)) {
228 LyXLayout * lay = operator[](name).get();
229 error = do_readStyle(lexrc, *lay);
233 if (le == TC_ENVIRONMENT)
234 lay.is_environment = true;
235 if (!(error = do_readStyle(lexrc, lay)))
236 layoutlist_.push_back(
237 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));
303 secnumdepth_ = lexrc.getInteger();
308 tocdepth_ = lexrc.getInteger();
311 // First step to support options
312 case TC_CLASSOPTIONS:
313 readClassOptions(lexrc);
317 preamble_ = lexrc.getLongString("EndPreamble");
320 case TC_PROVIDESAMSMATH:
321 if (lexrc.next() && lexrc.getInteger())
322 provides_ |= amsmath;
325 case TC_PROVIDESNATBIB:
326 if (lexrc.next() && lexrc.getInteger())
330 case TC_PROVIDESMAKEIDX:
331 if (lexrc.next() && lexrc.getInteger())
332 provides_ |= makeidx;
336 if (lexrc.next() && lexrc.getInteger())
340 case TC_LEFTMARGIN: // left margin type
342 leftmargin_ = lexrc.getString();
345 case TC_RIGHTMARGIN: // right margin type
347 rightmargin_ = lexrc.getString();
355 case TC_TITLELATEXTYPE:
356 readTitleType(lexrc);
358 case TC_TITLELATEXNAME:
360 titlename_ = lexrc.getString();
364 string const nofloat = lexrc.getString();
365 floatlist_->erase(nofloat);
371 if (!merge) { // we are at top level here.
372 lyxerr[Debug::TCLASS] << "Finished reading textclass "
373 << MakeDisplayPath(filename)
375 if (defaultlayout_.empty()) {
376 lyxerr << "Error: Textclass '" << name_
377 << "' is missing a defaultstyle." << endl;
381 lyxerr[Debug::TCLASS] << "Finished reading input file "
382 << MakeDisplayPath(filename)
389 void LyXTextClass::readTitleType(LyXLex & lexrc)
391 keyword_item titleTypeTags[] = {
392 { "commandafter", TITLE_COMMAND_AFTER },
393 { "environment", TITLE_ENVIRONMENT }
396 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
398 int le = lexrc.lex();
400 case LyXLex::LEX_UNDEF:
401 lexrc.printError("Unknown output type `$$Token'");
403 case TITLE_COMMAND_AFTER:
404 case TITLE_ENVIRONMENT:
405 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
408 lyxerr << "Unhandled value " << le
409 << " in LyXTextClass::readTitleType." << endl;
416 void LyXTextClass::readOutputType(LyXLex & lexrc)
418 keyword_item outputTypeTags[] = {
419 { "docbook", DOCBOOK },
421 { "linuxdoc", LINUXDOC },
422 { "literate", LITERATE }
425 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
427 int le = lexrc.lex();
429 case LyXLex::LEX_UNDEF:
430 lexrc.printError("Unknown output type `$$Token'");
436 outputType_ = static_cast<OutputType>(le);
439 lyxerr << "Unhandled value " << le
440 << " in LyXTextClass::readOutputType." << endl;
447 enum MaxCounterTags {
448 MC_COUNTER_CHAPTER = 1,
450 MC_COUNTER_SUBSECTION,
451 MC_COUNTER_SUBSUBSECTION,
452 MC_COUNTER_PARAGRAPH,
453 MC_COUNTER_SUBPARAGRAPH,
461 enum ClassOptionsTags {
469 void LyXTextClass::readClassOptions(LyXLex & lexrc)
471 keyword_item classOptionsTags[] = {
473 {"fontsize", CO_FONTSIZE },
474 {"other", CO_OTHER },
475 {"pagestyle", CO_PAGESTYLE }
478 lexrc.pushTable(classOptionsTags, CO_END);
480 while (!getout && lexrc.isOK()) {
481 int le = lexrc.lex();
483 case LyXLex::LEX_UNDEF:
484 lexrc.printError("Unknown ClassOption tag `$$Token'");
488 switch (static_cast<ClassOptionsTags>(le)) {
491 opt_fontsize_ = rtrim(lexrc.getString());
495 opt_pagestyle_ = rtrim(lexrc.getString());
499 options_ = lexrc.getString();
522 void LyXTextClass::readFloat(LyXLex & lexrc)
524 keyword_item floatTags[] = {
526 { "extension", FT_EXT },
527 { "guiname", FT_NAME },
528 { "latexbuiltin", FT_BUILTIN },
529 { "listname", FT_LISTNAME },
530 { "numberwithin", FT_WITHIN },
531 { "placement", FT_PLACEMENT },
532 { "style", FT_STYLE },
536 lexrc.pushTable(floatTags, FT_END);
545 bool builtin = false;
548 while (!getout && lexrc.isOK()) {
549 int le = lexrc.lex();
551 case LyXLex::LEX_UNDEF:
552 lexrc.printError("Unknown ClassOption tag `$$Token'");
556 switch (static_cast<FloatTags>(le)) {
559 type = lexrc.getString();
560 // Here we could check if this type is already defined
561 // and modify it with the rest of the vars instead.
565 name = lexrc.getString();
569 placement = lexrc.getString();
573 ext = lexrc.getString();
577 within = lexrc.getString();
578 if (within == "none")
583 style = lexrc.getString();
587 listname = lexrc.getString();
591 builtin = lexrc.getBool();
599 // Here if have a full float if getout == true
601 Floating newfloat(type, placement, ext, within,
602 style, name, listname, builtin);
603 floatlist_->newFloat(newfloat);
616 void LyXTextClass::readCounter(LyXLex & lexrc)
618 keyword_item counterTags[] = {
621 { "within", CT_WITHIN }
624 lexrc.pushTable(counterTags, CT_END);
630 while (!getout && lexrc.isOK()) {
631 int le = lexrc.lex();
633 case LyXLex::LEX_UNDEF:
634 lexrc.printError("Unknown ClassOption tag `$$Token'");
638 switch (static_cast<CounterTags>(le)) {
641 name = lexrc.getString();
645 within = lexrc.getString();
646 if (within == "none")
655 // Here if have a full counter if getout == true
657 if (within.empty()) {
658 ctrs_->newCounter(name);
660 ctrs_->newCounter(name, within);
668 LyXFont const & LyXTextClass::defaultfont() const
674 string const & LyXTextClass::leftmargin() const
680 string const & LyXTextClass::rightmargin() const
686 bool LyXTextClass::hasLayout(string const & n) const
688 string const name = (n.empty() ? defaultLayoutName() : n);
690 return find_if(layoutlist_.begin(), layoutlist_.end(),
692 != layoutlist_.end();
697 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
699 BOOST_ASSERT(!name.empty());
701 LayoutList::const_iterator cit =
702 find_if(layoutlist_.begin(),
706 if (cit == layoutlist_.end()) {
707 lyxerr << "We failed to find the layout '" << name
708 << "' in the layout list. You MUST investigate!"
710 for (LayoutList::const_iterator it = layoutlist_.begin();
711 it != layoutlist_.end(); ++it)
712 lyxerr << " " << it->get()->name() << endl;
714 // we require the name to exist
723 bool LyXTextClass::delete_layout(string const & name)
725 if (name == defaultLayoutName())
728 LayoutList::iterator it =
729 remove_if(layoutlist_.begin(), layoutlist_.end(),
732 LayoutList::iterator end = layoutlist_.end();
733 bool const ret = (it != end);
734 layoutlist_.erase(it, end);
739 // Load textclass info if not loaded yet
740 bool LyXTextClass::load() const
746 string const real_file = LibFileSearch("layouts", name_, "layout");
748 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
749 lyxerr << "Error reading `"
750 << MakeDisplayPath(real_file)
751 << "'\n(Check `" << name_
752 << "')\nCheck your installation and "
753 "try Options/Reconfigure..." << endl;
761 FloatList & LyXTextClass::floats()
763 return *floatlist_.get();
767 FloatList const & LyXTextClass::floats() const
769 return *floatlist_.get();
773 Counters & LyXTextClass::counters() const
779 string const & LyXTextClass::defaultLayoutName() const
781 // This really should come from the actual layout... (Lgb)
782 return defaultlayout_;
786 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
788 return operator[](defaultLayoutName());
792 string const & LyXTextClass::name() const
798 string const & LyXTextClass::latexname() const
800 const_cast<LyXTextClass*>(this)->load();
805 string const & LyXTextClass::description() const
811 string const & LyXTextClass::opt_fontsize() const
813 return opt_fontsize_;
817 string const & LyXTextClass::opt_pagestyle() const
819 return opt_pagestyle_;
823 string const & LyXTextClass::options() const
829 string const & LyXTextClass::pagestyle() const
835 string const & LyXTextClass::preamble() const
841 LyXTextClass::PageSides LyXTextClass::sides() const
847 int LyXTextClass::secnumdepth() const
853 int LyXTextClass::tocdepth() const
859 OutputType LyXTextClass::outputType() const
865 bool LyXTextClass::provides(LyXTextClass::Provides p) const
867 return provides_ & p;
871 unsigned int LyXTextClass::columns() const
877 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
883 string const & LyXTextClass::titlename() const
889 int LyXTextClass::size() const
891 return layoutlist_.size();
895 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
898 case LyXTextClass::OneSide:
901 case LyXTextClass::TwoSides: