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)));
238 if (defaultlayout_.empty()) {
239 // We do not have a default
240 // layout yet, so we choose
241 // the first layout we
243 defaultlayout_ = name;
248 lexrc.printError("No name given for style: `$$Token'.");
255 string const style = subst(lexrc.getString(),
257 if (!delete_layout(style))
258 lyxerr << "Cannot delete style `"
259 << style << '\'' << endl;
260 // lexrc.printError("Cannot delete style"
267 columns_ = lexrc.getInteger();
272 switch (lexrc.getInteger()) {
273 case 1: sides_ = OneSide; break;
274 case 2: sides_ = TwoSides; break;
276 lyxerr << "Impossible number of page"
277 " sides, setting to one."
287 pagestyle_ = rtrim(lexrc.getString());
291 defaultfont_.lyxRead(lexrc);
292 if (!defaultfont_.resolved()) {
293 lexrc.printError("Warning: defaultfont should "
294 "be fully instantiated!");
295 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
301 secnumdepth_ = lexrc.getInteger();
306 tocdepth_ = lexrc.getInteger();
309 // First step to support options
310 case TC_CLASSOPTIONS:
311 readClassOptions(lexrc);
315 preamble_ = lexrc.getLongString("EndPreamble");
318 case TC_PROVIDESAMSMATH:
319 if (lexrc.next() && lexrc.getInteger())
320 provides_ |= amsmath;
323 case TC_PROVIDESNATBIB:
324 if (lexrc.next() && lexrc.getInteger())
328 case TC_PROVIDESMAKEIDX:
329 if (lexrc.next() && lexrc.getInteger())
330 provides_ |= makeidx;
334 if (lexrc.next() && lexrc.getInteger())
338 case TC_LEFTMARGIN: // left margin type
340 leftmargin_ = lexrc.getString();
343 case TC_RIGHTMARGIN: // right margin type
345 rightmargin_ = lexrc.getString();
353 case TC_TITLELATEXTYPE:
354 readTitleType(lexrc);
356 case TC_TITLELATEXNAME:
358 titlename_ = lexrc.getString();
362 string const nofloat = lexrc.getString();
363 floatlist_->erase(nofloat);
369 if (!merge) { // we are at top level here.
370 lyxerr[Debug::TCLASS] << "Finished reading textclass "
371 << MakeDisplayPath(filename)
373 if (defaultlayout_.empty()) {
374 lyxerr << "Error: Textclass '" << name_
375 << "' is missing a defaultstyle." << endl;
379 lyxerr[Debug::TCLASS] << "Finished reading input file "
380 << MakeDisplayPath(filename)
387 void LyXTextClass::readTitleType(LyXLex & lexrc)
389 keyword_item titleTypeTags[] = {
390 { "commandafter", TITLE_COMMAND_AFTER },
391 { "environment", TITLE_ENVIRONMENT }
394 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
396 int le = lexrc.lex();
398 case LyXLex::LEX_UNDEF:
399 lexrc.printError("Unknown output type `$$Token'");
401 case TITLE_COMMAND_AFTER:
402 case TITLE_ENVIRONMENT:
403 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
406 lyxerr << "Unhandled value " << le
407 << " in LyXTextClass::readTitleType." << endl;
414 void LyXTextClass::readOutputType(LyXLex & lexrc)
416 keyword_item outputTypeTags[] = {
417 { "docbook", DOCBOOK },
419 { "linuxdoc", LINUXDOC },
420 { "literate", LITERATE }
423 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
425 int le = lexrc.lex();
427 case LyXLex::LEX_UNDEF:
428 lexrc.printError("Unknown output type `$$Token'");
434 outputType_ = static_cast<OutputType>(le);
437 lyxerr << "Unhandled value " << le
438 << " in LyXTextClass::readOutputType." << endl;
445 enum MaxCounterTags {
446 MC_COUNTER_CHAPTER = 1,
448 MC_COUNTER_SUBSECTION,
449 MC_COUNTER_SUBSUBSECTION,
450 MC_COUNTER_PARAGRAPH,
451 MC_COUNTER_SUBPARAGRAPH,
459 enum ClassOptionsTags {
467 void LyXTextClass::readClassOptions(LyXLex & lexrc)
469 keyword_item classOptionsTags[] = {
471 {"fontsize", CO_FONTSIZE },
472 {"other", CO_OTHER },
473 {"pagestyle", CO_PAGESTYLE }
476 lexrc.pushTable(classOptionsTags, CO_END);
478 while (!getout && lexrc.isOK()) {
479 int le = lexrc.lex();
481 case LyXLex::LEX_UNDEF:
482 lexrc.printError("Unknown ClassOption tag `$$Token'");
486 switch (static_cast<ClassOptionsTags>(le)) {
489 opt_fontsize_ = rtrim(lexrc.getString());
493 opt_pagestyle_ = rtrim(lexrc.getString());
497 options_ = lexrc.getString();
520 void LyXTextClass::readFloat(LyXLex & lexrc)
522 keyword_item floatTags[] = {
524 { "extension", FT_EXT },
525 { "guiname", FT_NAME },
526 { "latexbuiltin", FT_BUILTIN },
527 { "listname", FT_LISTNAME },
528 { "numberwithin", FT_WITHIN },
529 { "placement", FT_PLACEMENT },
530 { "style", FT_STYLE },
534 lexrc.pushTable(floatTags, FT_END);
543 bool builtin = false;
546 while (!getout && lexrc.isOK()) {
547 int le = lexrc.lex();
549 case LyXLex::LEX_UNDEF:
550 lexrc.printError("Unknown ClassOption tag `$$Token'");
554 switch (static_cast<FloatTags>(le)) {
557 type = lexrc.getString();
558 // Here we could check if this type is already defined
559 // and modify it with the rest of the vars instead.
563 name = lexrc.getString();
567 placement = lexrc.getString();
571 ext = lexrc.getString();
575 within = lexrc.getString();
576 if (within == "none")
581 style = lexrc.getString();
585 listname = lexrc.getString();
589 builtin = lexrc.getBool();
597 // Here if have a full float if getout == true
599 Floating newfloat(type, placement, ext, within,
600 style, name, listname, builtin);
601 floatlist_->newFloat(newfloat);
614 void LyXTextClass::readCounter(LyXLex & lexrc)
616 keyword_item counterTags[] = {
619 { "within", CT_WITHIN }
622 lexrc.pushTable(counterTags, CT_END);
628 while (!getout && lexrc.isOK()) {
629 int le = lexrc.lex();
631 case LyXLex::LEX_UNDEF:
632 lexrc.printError("Unknown ClassOption tag `$$Token'");
636 switch (static_cast<CounterTags>(le)) {
639 name = lexrc.getString();
643 within = lexrc.getString();
644 if (within == "none")
653 // Here if have a full counter if getout == true
655 if (within.empty()) {
656 ctrs_->newCounter(name);
658 ctrs_->newCounter(name, within);
666 LyXFont const & LyXTextClass::defaultfont() const
672 string const & LyXTextClass::leftmargin() const
678 string const & LyXTextClass::rightmargin() const
684 bool LyXTextClass::hasLayout(string const & n) const
686 string const name = (n.empty() ? defaultLayoutName() : n);
688 return find_if(layoutlist_.begin(), layoutlist_.end(),
690 != layoutlist_.end();
695 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
697 BOOST_ASSERT(!name.empty());
699 LayoutList::const_iterator cit =
700 find_if(layoutlist_.begin(),
704 if (cit == layoutlist_.end()) {
705 lyxerr << "We failed to find the layout '" << name
706 << "' in the layout list. You MUST investigate!"
708 for (LayoutList::const_iterator it = layoutlist_.begin();
709 it != layoutlist_.end(); ++it)
710 lyxerr << " " << it->get()->name() << endl;
712 // we require the name to exist
721 bool LyXTextClass::delete_layout(string const & name)
723 if (name == defaultLayoutName())
726 LayoutList::iterator it =
727 remove_if(layoutlist_.begin(), layoutlist_.end(),
730 LayoutList::iterator end = layoutlist_.end();
731 bool const ret = (it != end);
732 layoutlist_.erase(it, end);
737 // Load textclass info if not loaded yet
738 bool LyXTextClass::load() const
744 string const real_file = LibFileSearch("layouts", name_, "layout");
746 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
747 lyxerr << "Error reading `"
748 << MakeDisplayPath(real_file)
749 << "'\n(Check `" << name_
750 << "')\nCheck your installation and "
751 "try Options/Reconfigure..." << endl;
759 FloatList & LyXTextClass::floats()
761 return *floatlist_.get();
765 FloatList const & LyXTextClass::floats() const
767 return *floatlist_.get();
771 Counters & LyXTextClass::counters() const
777 string const & LyXTextClass::defaultLayoutName() const
779 // This really should come from the actual layout... (Lgb)
780 return defaultlayout_;
784 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
786 return operator[](defaultLayoutName());
790 string const & LyXTextClass::name() const
796 string const & LyXTextClass::latexname() const
798 const_cast<LyXTextClass*>(this)->load();
803 string const & LyXTextClass::description() const
809 string const & LyXTextClass::opt_fontsize() const
811 return opt_fontsize_;
815 string const & LyXTextClass::opt_pagestyle() const
817 return opt_pagestyle_;
821 string const & LyXTextClass::options() const
827 string const & LyXTextClass::pagestyle() const
833 string const & LyXTextClass::preamble() const
839 LyXTextClass::PageSides LyXTextClass::sides() const
845 int LyXTextClass::secnumdepth() const
851 int LyXTextClass::tocdepth() const
857 OutputType LyXTextClass::outputType() const
863 bool LyXTextClass::provides(LyXTextClass::Provides p) const
865 return provides_ & p;
869 unsigned int LyXTextClass::columns() const
875 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
881 string const & LyXTextClass::titlename() const
887 int LyXTextClass::size() const
889 return layoutlist_.size();
893 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
896 case LyXTextClass::OneSide:
899 case LyXTextClass::TwoSides: