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 lyxerr[Debug::TCLASS] << "Finished reading input file "
389 << MakeDisplayPath(filename)
396 void LyXTextClass::readTitleType(LyXLex & lexrc)
398 keyword_item titleTypeTags[] = {
399 { "commandafter", TITLE_COMMAND_AFTER },
400 { "environment", TITLE_ENVIRONMENT }
403 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
405 int le = lexrc.lex();
407 case LyXLex::LEX_UNDEF:
408 lexrc.printError("Unknown output type `$$Token'");
410 case TITLE_COMMAND_AFTER:
411 case TITLE_ENVIRONMENT:
412 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
415 lyxerr << "Unhandled value " << le
416 << " in LyXTextClass::readTitleType." << endl;
423 void LyXTextClass::readOutputType(LyXLex & lexrc)
425 keyword_item outputTypeTags[] = {
426 { "docbook", DOCBOOK },
428 { "linuxdoc", LINUXDOC },
429 { "literate", LITERATE }
432 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
434 int le = lexrc.lex();
436 case LyXLex::LEX_UNDEF:
437 lexrc.printError("Unknown output type `$$Token'");
443 outputType_ = static_cast<OutputType>(le);
446 lyxerr << "Unhandled value " << le
447 << " in LyXTextClass::readOutputType." << endl;
454 enum MaxCounterTags {
455 MC_COUNTER_CHAPTER = 1,
457 MC_COUNTER_SUBSECTION,
458 MC_COUNTER_SUBSUBSECTION,
459 MC_COUNTER_PARAGRAPH,
460 MC_COUNTER_SUBPARAGRAPH,
468 enum ClassOptionsTags {
477 void LyXTextClass::readClassOptions(LyXLex & lexrc)
479 keyword_item classOptionsTags[] = {
481 {"fontsize", CO_FONTSIZE },
482 {"header", CO_HEADER },
483 {"other", CO_OTHER },
484 {"pagestyle", CO_PAGESTYLE }
487 lexrc.pushTable(classOptionsTags, CO_END);
489 while (!getout && lexrc.isOK()) {
490 int le = lexrc.lex();
492 case LyXLex::LEX_UNDEF:
493 lexrc.printError("Unknown ClassOption tag `$$Token'");
497 switch (static_cast<ClassOptionsTags>(le)) {
500 opt_fontsize_ = rtrim(lexrc.getString());
504 opt_pagestyle_ = rtrim(lexrc.getString());
508 options_ = lexrc.getString();
512 class_header_ = subst(lexrc.getString(), """, "\"");
533 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
535 keyword_item elementTags[] = {
538 { "labelfont", CS_LABELFONT },
539 { "latexname", CS_LATEXNAME },
540 { "latexparam", CS_LATEXPARAM },
541 { "latextype", CS_LATEXTYPE },
542 { "preamble", CS_PREAMBLE}
545 lexrc.pushTable(elementTags, CS_END);
550 LyXFont font(LyXFont::ALL_INHERIT);
551 LyXFont labelfont(LyXFont::ALL_INHERIT);
555 while (!getout && lexrc.isOK()) {
556 int le = lexrc.lex();
558 case LyXLex::LEX_UNDEF:
559 lexrc.printError("Unknown ClassOption tag `$$Token'");
563 switch (static_cast<CharStyleTags>(le)) {
566 latextype = lexrc.getString();
570 latexname = lexrc.getString();
574 latexparam = subst(lexrc.getString(), """, "\"");
577 labelfont.lyxRead(lexrc);
584 preamble = lexrc.getLongString("EndPreamble");
593 // Here add element to list if getout == true
597 cs.latextype = latextype;
598 cs.latexname = latexname;
599 cs.latexparam = latexparam;
601 cs.labelfont = labelfont;
602 cs.preamble = preamble;
603 charstyles().push_back(cs);
623 void LyXTextClass::readFloat(LyXLex & lexrc)
625 keyword_item floatTags[] = {
627 { "extension", FT_EXT },
628 { "guiname", FT_NAME },
629 { "latexbuiltin", FT_BUILTIN },
630 { "listname", FT_LISTNAME },
631 { "numberwithin", FT_WITHIN },
632 { "placement", FT_PLACEMENT },
633 { "style", FT_STYLE },
637 lexrc.pushTable(floatTags, FT_END);
646 bool builtin = false;
649 while (!getout && lexrc.isOK()) {
650 int le = lexrc.lex();
652 case LyXLex::LEX_UNDEF:
653 lexrc.printError("Unknown ClassOption tag `$$Token'");
657 switch (static_cast<FloatTags>(le)) {
660 type = lexrc.getString();
661 // Here we could check if this type is already defined
662 // and modify it with the rest of the vars instead.
666 name = lexrc.getString();
670 placement = lexrc.getString();
674 ext = lexrc.getString();
678 within = lexrc.getString();
679 if (within == "none")
684 style = lexrc.getString();
688 listname = lexrc.getString();
692 builtin = lexrc.getBool();
700 // Here if have a full float if getout == true
702 Floating newfloat(type, placement, ext, within,
703 style, name, listname, builtin);
704 floatlist_->newFloat(newfloat);
717 void LyXTextClass::readCounter(LyXLex & lexrc)
719 keyword_item counterTags[] = {
722 { "within", CT_WITHIN }
725 lexrc.pushTable(counterTags, CT_END);
731 while (!getout && lexrc.isOK()) {
732 int le = lexrc.lex();
734 case LyXLex::LEX_UNDEF:
735 lexrc.printError("Unknown ClassOption tag `$$Token'");
739 switch (static_cast<CounterTags>(le)) {
742 name = lexrc.getString();
746 within = lexrc.getString();
747 if (within == "none")
756 // Here if have a full counter if getout == true
758 if (within.empty()) {
759 ctrs_->newCounter(name);
761 ctrs_->newCounter(name, within);
769 LyXFont const & LyXTextClass::defaultfont() const
775 string const & LyXTextClass::leftmargin() const
781 string const & LyXTextClass::rightmargin() const
787 bool LyXTextClass::hasLayout(string const & n) const
789 string const name = (n.empty() ? defaultLayoutName() : n);
791 return find_if(layoutlist_.begin(), layoutlist_.end(),
792 LayoutNamesEqual(name))
793 != layoutlist_.end();
798 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
800 BOOST_ASSERT(!name.empty());
802 LayoutList::const_iterator cit =
803 find_if(layoutlist_.begin(),
805 LayoutNamesEqual(name));
807 if (cit == layoutlist_.end()) {
808 lyxerr << "We failed to find the layout '" << name
809 << "' in the layout list. You MUST investigate!"
811 for (LayoutList::const_iterator it = layoutlist_.begin();
812 it != layoutlist_.end(); ++it)
813 lyxerr << " " << it->get()->name() << endl;
815 // we require the name to exist
824 bool LyXTextClass::delete_layout(string const & name)
826 if (name == defaultLayoutName())
829 LayoutList::iterator it =
830 remove_if(layoutlist_.begin(), layoutlist_.end(),
831 LayoutNamesEqual(name));
833 LayoutList::iterator end = layoutlist_.end();
834 bool const ret = (it != end);
835 layoutlist_.erase(it, end);
840 // Load textclass info if not loaded yet
841 bool LyXTextClass::load() const
847 string const real_file = LibFileSearch("layouts", name_, "layout");
848 loaded_ = const_cast<LyXTextClass*>(this)->Read(real_file) == 0;
851 lyxerr << "Error reading `"
852 << MakeDisplayPath(real_file)
853 << "'\n(Check `" << name_
854 << "')\nCheck your installation and "
855 "try Options/Reconfigure..." << endl;
862 FloatList & LyXTextClass::floats()
864 return *floatlist_.get();
868 FloatList const & LyXTextClass::floats() const
870 return *floatlist_.get();
874 Counters & LyXTextClass::counters() const
880 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
882 CharStyles::iterator cs = charstyles().begin();
883 CharStyles::iterator csend = charstyles().end();
884 for (; cs != csend; ++cs) {
892 string const & LyXTextClass::defaultLayoutName() const
894 // This really should come from the actual layout... (Lgb)
895 return defaultlayout_;
899 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
901 return operator[](defaultLayoutName());
905 string const & LyXTextClass::name() const
911 string const & LyXTextClass::latexname() const
913 const_cast<LyXTextClass*>(this)->load();
918 string const & LyXTextClass::description() const
924 string const & LyXTextClass::opt_fontsize() const
926 return opt_fontsize_;
930 string const & LyXTextClass::opt_pagestyle() const
932 return opt_pagestyle_;
936 string const & LyXTextClass::options() const
942 string const & LyXTextClass::class_header() const
944 return class_header_;
948 string const & LyXTextClass::pagestyle() const
954 string const & LyXTextClass::preamble() const
960 LyXTextClass::PageSides LyXTextClass::sides() const
966 int LyXTextClass::secnumdepth() const
972 int LyXTextClass::tocdepth() const
978 OutputType LyXTextClass::outputType() const
984 bool LyXTextClass::provides(LyXTextClass::Provides p) const
986 return provides_ & p;
990 unsigned int LyXTextClass::columns() const
996 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1002 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: