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;
41 class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
43 LayoutNamesEqual(string const & name)
46 bool operator()(LyXLayout_ptr const & c) const
48 return c->name() == name_;
57 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
58 string const & desc, bool texClassAvail )
59 : name_(fn), latexname_(cln), description_(desc),
60 floatlist_(new FloatList), ctrs_(new Counters),
61 texClassAvail_(texClassAvail)
68 pagestyle_ = "default";
69 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
70 opt_fontsize_ = "10|11|12";
71 opt_pagestyle_ = "empty|plain|headings|fancy";
73 titletype_ = TITLE_COMMAND_AFTER;
74 titlename_ = "maketitle";
79 bool LyXTextClass::isTeXClassAvailable() const
81 return texClassAvail_;
85 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
87 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
88 if (!lay.Read(lexrc, *this)) {
90 lay.resfont = lay.font;
91 lay.resfont.realize(defaultfont());
92 lay.reslabelfont = lay.labelfont;
93 lay.reslabelfont.realize(defaultfont());
94 return false; // no errors
96 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 { "charstyle", TC_CHARSTYLE },
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 { "nofloat", TC_NOFLOAT },
145 { "nostyle", TC_NOSTYLE },
146 { "outputtype", TC_OUTPUTTYPE },
147 { "pagestyle", TC_PAGESTYLE },
148 { "preamble", TC_PREAMBLE },
149 { "providesamsmath", TC_PROVIDESAMSMATH },
150 { "providesmakeidx", TC_PROVIDESMAKEIDX },
151 { "providesnatbib", TC_PROVIDESNATBIB },
152 { "providesurl", TC_PROVIDESURL },
153 { "rightmargin", TC_RIGHTMARGIN },
154 { "secnumdepth", TC_SECNUMDEPTH },
155 { "sides", TC_SIDES },
156 { "style", TC_STYLE },
157 { "titlelatexname", TC_TITLELATEXNAME },
158 { "titlelatextype", TC_TITLELATEXTYPE },
159 { "tocdepth", TC_TOCDEPTH }
163 lyxerr[Debug::TCLASS] << "Reading textclass "
164 << MakeDisplayPath(filename)
167 lyxerr[Debug::TCLASS] << "Reading input file "
168 << MakeDisplayPath(filename)
171 LyXLex lexrc(textClassTags,
172 sizeof(textClassTags) / sizeof(textClassTags[0]));
175 lexrc.setFile(filename);
176 if (!lexrc.isOK()) error = true;
179 while (lexrc.isOK() && !error) {
180 int le = lexrc.lex();
183 case LyXLex::LEX_FEOF:
186 case LyXLex::LEX_UNDEF:
187 lexrc.printError("Unknown TextClass tag `$$Token'");
195 switch (static_cast<TextClassTags>(le)) {
197 case TC_OUTPUTTYPE: // output type definition
198 readOutputType(lexrc);
201 case TC_INPUT: // Include file
203 string tmp = LibFileSearch("layouts",
207 if (Read(tmp, true)) {
208 lexrc.printError("Error reading input"
215 case TC_DEFAULTSTYLE:
217 string const name = subst(lexrc.getString(),
219 defaultlayout_ = name;
226 string const name = subst(lexrc.getString(),
228 if (hasLayout(name)) {
229 LyXLayout * lay = operator[](name).get();
230 error = do_readStyle(lexrc, *lay);
234 if (le == TC_ENVIRONMENT)
235 lay.is_environment = true;
236 if (!(error = do_readStyle(lexrc, lay)))
237 layoutlist_.push_back(
238 boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
241 if (defaultlayout_.empty()) {
242 // We do not have a default
243 // layout yet, so we choose
244 // the first layout we
246 defaultlayout_ = name;
251 lexrc.printError("No name given for style: `$$Token'.");
258 string const style = subst(lexrc.getString(),
260 if (!delete_layout(style))
261 lyxerr << "Cannot delete style `"
262 << style << '\'' << endl;
263 // lexrc.printError("Cannot delete style"
270 columns_ = lexrc.getInteger();
275 switch (lexrc.getInteger()) {
276 case 1: sides_ = OneSide; break;
277 case 2: sides_ = TwoSides; break;
279 lyxerr << "Impossible number of page"
280 " sides, setting to one."
290 pagestyle_ = rtrim(lexrc.getString());
294 defaultfont_.lyxRead(lexrc);
295 if (!defaultfont_.resolved()) {
296 lexrc.printError("Warning: defaultfont should "
297 "be fully instantiated!");
298 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
304 secnumdepth_ = lexrc.getInteger();
309 tocdepth_ = lexrc.getInteger();
312 // First step to support options
313 case TC_CLASSOPTIONS:
314 readClassOptions(lexrc);
318 preamble_ = lexrc.getLongString("EndPreamble");
321 case TC_PROVIDESAMSMATH:
322 if (lexrc.next() && lexrc.getInteger())
323 provides_ |= amsmath;
326 case TC_PROVIDESNATBIB:
327 if (lexrc.next() && lexrc.getInteger())
331 case TC_PROVIDESMAKEIDX:
332 if (lexrc.next() && lexrc.getInteger())
333 provides_ |= makeidx;
337 if (lexrc.next() && lexrc.getInteger())
341 case TC_LEFTMARGIN: // left margin type
343 leftmargin_ = lexrc.getString();
346 case TC_RIGHTMARGIN: // right margin type
348 rightmargin_ = lexrc.getString();
352 string const name = subst(lexrc.getString(), '_', ' ');
353 readCharStyle(lexrc, name);
362 case TC_TITLELATEXTYPE:
363 readTitleType(lexrc);
365 case TC_TITLELATEXNAME:
367 titlename_ = lexrc.getString();
371 string const nofloat = lexrc.getString();
372 floatlist_->erase(nofloat);
378 if (!merge) { // we are at top level here.
379 lyxerr[Debug::TCLASS] << "Finished reading textclass "
380 << MakeDisplayPath(filename)
382 if (defaultlayout_.empty()) {
383 lyxerr << "Error: Textclass '" << name_
384 << "' is missing a defaultstyle." << endl;
388 min_toclevel_ = LyXLayout::NOT_IN_TOC;
389 max_toclevel_ = LyXLayout::NOT_IN_TOC;
390 const_iterator cit = begin();
391 const_iterator the_end = end();
392 for ( ; cit != the_end ; ++cit) {
393 int const toclevel = (*cit)->toclevel;
394 if (toclevel != LyXLayout::NOT_IN_TOC) {
395 if (min_toclevel_ == LyXLayout::NOT_IN_TOC)
396 min_toclevel_ = toclevel;
398 min_toclevel_ = std::min(min_toclevel_,
400 max_toclevel_ = std::max(max_toclevel_,
404 lyxerr[Debug::TCLASS]
405 << "Minimum TocLevel is " << min_toclevel_
406 << ", maximum is " << max_toclevel_ <<endl;
409 lyxerr[Debug::TCLASS] << "Finished reading input file "
410 << MakeDisplayPath(filename)
417 void LyXTextClass::readTitleType(LyXLex & lexrc)
419 keyword_item titleTypeTags[] = {
420 { "commandafter", TITLE_COMMAND_AFTER },
421 { "environment", TITLE_ENVIRONMENT }
424 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
426 int le = lexrc.lex();
428 case LyXLex::LEX_UNDEF:
429 lexrc.printError("Unknown output type `$$Token'");
431 case TITLE_COMMAND_AFTER:
432 case TITLE_ENVIRONMENT:
433 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
436 lyxerr << "Unhandled value " << le
437 << " in LyXTextClass::readTitleType." << endl;
444 void LyXTextClass::readOutputType(LyXLex & lexrc)
446 keyword_item outputTypeTags[] = {
447 { "docbook", DOCBOOK },
449 { "linuxdoc", LINUXDOC },
450 { "literate", LITERATE }
453 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
455 int le = lexrc.lex();
457 case LyXLex::LEX_UNDEF:
458 lexrc.printError("Unknown output type `$$Token'");
464 outputType_ = static_cast<OutputType>(le);
467 lyxerr << "Unhandled value " << le
468 << " in LyXTextClass::readOutputType." << endl;
475 enum ClassOptionsTags {
484 void LyXTextClass::readClassOptions(LyXLex & lexrc)
486 keyword_item classOptionsTags[] = {
488 {"fontsize", CO_FONTSIZE },
489 {"header", CO_HEADER },
490 {"other", CO_OTHER },
491 {"pagestyle", CO_PAGESTYLE }
494 lexrc.pushTable(classOptionsTags, CO_END);
496 while (!getout && lexrc.isOK()) {
497 int le = lexrc.lex();
499 case LyXLex::LEX_UNDEF:
500 lexrc.printError("Unknown ClassOption tag `$$Token'");
504 switch (static_cast<ClassOptionsTags>(le)) {
507 opt_fontsize_ = rtrim(lexrc.getString());
511 opt_pagestyle_ = rtrim(lexrc.getString());
515 options_ = lexrc.getString();
519 class_header_ = subst(lexrc.getString(), """, "\"");
540 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
542 keyword_item elementTags[] = {
545 { "labelfont", CS_LABELFONT },
546 { "latexname", CS_LATEXNAME },
547 { "latexparam", CS_LATEXPARAM },
548 { "latextype", CS_LATEXTYPE },
549 { "preamble", CS_PREAMBLE}
552 lexrc.pushTable(elementTags, CS_END);
557 LyXFont font(LyXFont::ALL_INHERIT);
558 LyXFont labelfont(LyXFont::ALL_INHERIT);
562 while (!getout && lexrc.isOK()) {
563 int le = lexrc.lex();
565 case LyXLex::LEX_UNDEF:
566 lexrc.printError("Unknown ClassOption tag `$$Token'");
570 switch (static_cast<CharStyleTags>(le)) {
573 latextype = lexrc.getString();
577 latexname = lexrc.getString();
581 latexparam = subst(lexrc.getString(), """, "\"");
584 labelfont.lyxRead(lexrc);
591 preamble = lexrc.getLongString("EndPreamble");
600 // Here add element to list if getout == true
604 cs.latextype = latextype;
605 cs.latexname = latexname;
606 cs.latexparam = latexparam;
608 cs.labelfont = labelfont;
609 cs.preamble = preamble;
610 charstyles().push_back(cs);
630 void LyXTextClass::readFloat(LyXLex & lexrc)
632 keyword_item floatTags[] = {
634 { "extension", FT_EXT },
635 { "guiname", FT_NAME },
636 { "latexbuiltin", FT_BUILTIN },
637 { "listname", FT_LISTNAME },
638 { "numberwithin", FT_WITHIN },
639 { "placement", FT_PLACEMENT },
640 { "style", FT_STYLE },
644 lexrc.pushTable(floatTags, FT_END);
653 bool builtin = false;
656 while (!getout && lexrc.isOK()) {
657 int le = lexrc.lex();
659 case LyXLex::LEX_UNDEF:
660 lexrc.printError("Unknown ClassOption tag `$$Token'");
664 switch (static_cast<FloatTags>(le)) {
667 type = lexrc.getString();
668 // Here we could check if this type is already defined
669 // and modify it with the rest of the vars instead.
673 name = lexrc.getString();
677 placement = lexrc.getString();
681 ext = lexrc.getString();
685 within = lexrc.getString();
686 if (within == "none")
691 style = lexrc.getString();
695 listname = lexrc.getString();
699 builtin = lexrc.getBool();
707 // Here if have a full float if getout == true
709 Floating newfloat(type, placement, ext, within,
710 style, name, listname, builtin);
711 floatlist_->newFloat(newfloat);
724 void LyXTextClass::readCounter(LyXLex & lexrc)
726 keyword_item counterTags[] = {
729 { "within", CT_WITHIN }
732 lexrc.pushTable(counterTags, CT_END);
738 while (!getout && lexrc.isOK()) {
739 int le = lexrc.lex();
741 case LyXLex::LEX_UNDEF:
742 lexrc.printError("Unknown ClassOption tag `$$Token'");
746 switch (static_cast<CounterTags>(le)) {
749 name = lexrc.getString();
753 within = lexrc.getString();
754 if (within == "none")
763 // Here if have a full counter if getout == true
765 if (within.empty()) {
766 ctrs_->newCounter(name);
768 ctrs_->newCounter(name, within);
776 LyXFont const & LyXTextClass::defaultfont() const
782 string const & LyXTextClass::leftmargin() const
788 string const & LyXTextClass::rightmargin() const
794 bool LyXTextClass::hasLayout(string const & n) const
796 string const name = (n.empty() ? defaultLayoutName() : n);
798 return find_if(layoutlist_.begin(), layoutlist_.end(),
799 LayoutNamesEqual(name))
800 != layoutlist_.end();
805 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
807 BOOST_ASSERT(!name.empty());
809 LayoutList::const_iterator cit =
810 find_if(layoutlist_.begin(),
812 LayoutNamesEqual(name));
814 if (cit == layoutlist_.end()) {
815 lyxerr << "We failed to find the layout '" << name
816 << "' in the layout list. You MUST investigate!"
818 for (LayoutList::const_iterator it = layoutlist_.begin();
819 it != layoutlist_.end(); ++it)
820 lyxerr << " " << it->get()->name() << endl;
822 // we require the name to exist
831 bool LyXTextClass::delete_layout(string const & name)
833 if (name == defaultLayoutName())
836 LayoutList::iterator it =
837 remove_if(layoutlist_.begin(), layoutlist_.end(),
838 LayoutNamesEqual(name));
840 LayoutList::iterator end = layoutlist_.end();
841 bool const ret = (it != end);
842 layoutlist_.erase(it, end);
847 // Load textclass info if not loaded yet
848 bool LyXTextClass::load() const
854 string const real_file = LibFileSearch("layouts", name_, "layout");
855 loaded_ = const_cast<LyXTextClass*>(this)->Read(real_file) == 0;
858 lyxerr << "Error reading `"
859 << MakeDisplayPath(real_file)
860 << "'\n(Check `" << name_
861 << "')\nCheck your installation and "
862 "try Options/Reconfigure..." << endl;
869 FloatList & LyXTextClass::floats()
871 return *floatlist_.get();
875 FloatList const & LyXTextClass::floats() const
877 return *floatlist_.get();
881 Counters & LyXTextClass::counters() const
887 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
889 CharStyles::iterator cs = charstyles().begin();
890 CharStyles::iterator csend = charstyles().end();
891 for (; cs != csend; ++cs) {
899 string const & LyXTextClass::defaultLayoutName() const
901 // This really should come from the actual layout... (Lgb)
902 return defaultlayout_;
906 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
908 return operator[](defaultLayoutName());
912 string const & LyXTextClass::name() const
918 string const & LyXTextClass::latexname() const
920 const_cast<LyXTextClass*>(this)->load();
925 string const & LyXTextClass::description() const
931 string const & LyXTextClass::opt_fontsize() const
933 return opt_fontsize_;
937 string const & LyXTextClass::opt_pagestyle() const
939 return opt_pagestyle_;
943 string const & LyXTextClass::options() const
949 string const & LyXTextClass::class_header() const
951 return class_header_;
955 string const & LyXTextClass::pagestyle() const
961 string const & LyXTextClass::preamble() const
967 LyXTextClass::PageSides LyXTextClass::sides() const
973 int LyXTextClass::secnumdepth() const
979 int LyXTextClass::tocdepth() const
985 OutputType LyXTextClass::outputType() const
991 bool LyXTextClass::provides(LyXTextClass::Provides p) const
993 return provides_ & p;
997 unsigned int LyXTextClass::columns() const
1003 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1009 string const & LyXTextClass::titlename() const
1015 int LyXTextClass::size() const
1017 return layoutlist_.size();
1021 int LyXTextClass::min_toclevel() const
1023 return min_toclevel_;
1027 int LyXTextClass::max_toclevel() const
1029 return max_toclevel_;
1033 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1036 case LyXTextClass::OneSide:
1039 case LyXTextClass::TwoSides: