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;
131 // Reads a textclass structure from file.
132 bool LyXTextClass::Read(string const & filename, bool merge)
134 keyword_item textClassTags[] = {
135 { "charstyle", TC_CHARSTYLE },
136 { "classoptions", TC_CLASSOPTIONS },
137 { "columns", TC_COLUMNS },
138 { "counter", TC_COUNTER },
139 { "defaultfont", TC_DEFAULTFONT },
140 { "defaultstyle", TC_DEFAULTSTYLE },
141 { "environment", TC_ENVIRONMENT },
142 { "float", TC_FLOAT },
143 { "input", TC_INPUT },
144 { "leftmargin", TC_LEFTMARGIN },
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))
242 if (defaultlayout_.empty()) {
243 // We do not have a default
244 // layout yet, so we choose
245 // the first layout we
247 defaultlayout_ = name;
252 lexrc.printError("No name given for style: `$$Token'.");
259 string const style = subst(lexrc.getString(),
261 if (!delete_layout(style))
262 lyxerr << "Cannot delete style `"
263 << style << '\'' << endl;
264 // lexrc.printError("Cannot delete style"
271 columns_ = lexrc.getInteger();
276 switch (lexrc.getInteger()) {
277 case 1: sides_ = OneSide; break;
278 case 2: sides_ = TwoSides; break;
280 lyxerr << "Impossible number of page"
281 " sides, setting to one."
291 pagestyle_ = rtrim(lexrc.getString());
295 defaultfont_.lyxRead(lexrc);
296 if (!defaultfont_.resolved()) {
297 lexrc.printError("Warning: defaultfont should "
298 "be fully instantiated!");
299 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
305 secnumdepth_ = lexrc.getInteger();
310 tocdepth_ = lexrc.getInteger();
313 // First step to support options
314 case TC_CLASSOPTIONS:
315 readClassOptions(lexrc);
319 preamble_ = lexrc.getLongString("EndPreamble");
322 case TC_PROVIDESAMSMATH:
323 if (lexrc.next() && lexrc.getInteger())
324 provides_ |= amsmath;
327 case TC_PROVIDESNATBIB:
328 if (lexrc.next() && lexrc.getInteger())
332 case TC_PROVIDESMAKEIDX:
333 if (lexrc.next() && lexrc.getInteger())
334 provides_ |= makeidx;
338 if (lexrc.next() && lexrc.getInteger())
342 case TC_LEFTMARGIN: // left margin type
344 leftmargin_ = lexrc.getString();
347 case TC_RIGHTMARGIN: // right margin type
349 rightmargin_ = lexrc.getString();
353 string const name = subst(lexrc.getString(), '_', ' ');
354 readCharStyle(lexrc, name);
363 case TC_TITLELATEXTYPE:
364 readTitleType(lexrc);
366 case TC_TITLELATEXNAME:
368 titlename_ = lexrc.getString();
372 string const nofloat = lexrc.getString();
373 floatlist_->erase(nofloat);
379 if (!merge) { // we are at top level here.
380 lyxerr[Debug::TCLASS] << "Finished reading textclass "
381 << MakeDisplayPath(filename)
383 if (defaultlayout_.empty()) {
384 lyxerr << "Error: Textclass '" << name_
385 << "' is missing a defaultstyle." << endl;
389 lyxerr[Debug::TCLASS] << "Finished reading input file "
390 << MakeDisplayPath(filename)
397 void LyXTextClass::readTitleType(LyXLex & lexrc)
399 keyword_item titleTypeTags[] = {
400 { "commandafter", TITLE_COMMAND_AFTER },
401 { "environment", TITLE_ENVIRONMENT }
404 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
406 int le = lexrc.lex();
408 case LyXLex::LEX_UNDEF:
409 lexrc.printError("Unknown output type `$$Token'");
411 case TITLE_COMMAND_AFTER:
412 case TITLE_ENVIRONMENT:
413 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
416 lyxerr << "Unhandled value " << le
417 << " in LyXTextClass::readTitleType." << endl;
424 void LyXTextClass::readOutputType(LyXLex & lexrc)
426 keyword_item outputTypeTags[] = {
427 { "docbook", DOCBOOK },
429 { "linuxdoc", LINUXDOC },
430 { "literate", LITERATE }
433 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
435 int le = lexrc.lex();
437 case LyXLex::LEX_UNDEF:
438 lexrc.printError("Unknown output type `$$Token'");
444 outputType_ = static_cast<OutputType>(le);
447 lyxerr << "Unhandled value " << le
448 << " in LyXTextClass::readOutputType." << endl;
455 enum MaxCounterTags {
456 MC_COUNTER_CHAPTER = 1,
458 MC_COUNTER_SUBSECTION,
459 MC_COUNTER_SUBSUBSECTION,
460 MC_COUNTER_PARAGRAPH,
461 MC_COUNTER_SUBPARAGRAPH,
469 enum ClassOptionsTags {
478 void LyXTextClass::readClassOptions(LyXLex & lexrc)
480 keyword_item classOptionsTags[] = {
482 {"fontsize", CO_FONTSIZE },
483 {"header", CO_HEADER },
484 {"other", CO_OTHER },
485 {"pagestyle", CO_PAGESTYLE }
488 lexrc.pushTable(classOptionsTags, CO_END);
490 while (!getout && lexrc.isOK()) {
491 int le = lexrc.lex();
493 case LyXLex::LEX_UNDEF:
494 lexrc.printError("Unknown ClassOption tag `$$Token'");
498 switch (static_cast<ClassOptionsTags>(le)) {
501 opt_fontsize_ = rtrim(lexrc.getString());
505 opt_pagestyle_ = rtrim(lexrc.getString());
509 options_ = lexrc.getString();
513 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 { "latextype", CS_LATEXTYPE },
541 { "preamble", CS_PREAMBLE}
544 lexrc.pushTable(elementTags, CS_END);
548 LyXFont font(LyXFont::ALL_INHERIT);
549 LyXFont labelfont(LyXFont::ALL_INHERIT);
553 while (!getout && lexrc.isOK()) {
554 int le = lexrc.lex();
556 case LyXLex::LEX_UNDEF:
557 lexrc.printError("Unknown ClassOption tag `$$Token'");
561 switch (static_cast<CharStyleTags>(le)) {
564 latextype = lexrc.getString();
568 latexname = lexrc.getString();
571 labelfont.lyxRead(lexrc);
578 preamble = lexrc.getLongString("EndPreamble");
587 // Here add element to list if getout == true
591 cs.latextype = latextype;
592 cs.latexname = latexname;
594 cs.labelfont = labelfont;
595 cs.preamble = preamble;
596 charstyles().push_back(cs);
616 void LyXTextClass::readFloat(LyXLex & lexrc)
618 keyword_item floatTags[] = {
620 { "extension", FT_EXT },
621 { "guiname", FT_NAME },
622 { "latexbuiltin", FT_BUILTIN },
623 { "listname", FT_LISTNAME },
624 { "numberwithin", FT_WITHIN },
625 { "placement", FT_PLACEMENT },
626 { "style", FT_STYLE },
630 lexrc.pushTable(floatTags, FT_END);
639 bool builtin = false;
642 while (!getout && lexrc.isOK()) {
643 int le = lexrc.lex();
645 case LyXLex::LEX_UNDEF:
646 lexrc.printError("Unknown ClassOption tag `$$Token'");
650 switch (static_cast<FloatTags>(le)) {
653 type = lexrc.getString();
654 // Here we could check if this type is already defined
655 // and modify it with the rest of the vars instead.
659 name = lexrc.getString();
663 placement = lexrc.getString();
667 ext = lexrc.getString();
671 within = lexrc.getString();
672 if (within == "none")
677 style = lexrc.getString();
681 listname = lexrc.getString();
685 builtin = lexrc.getBool();
693 // Here if have a full float if getout == true
695 Floating newfloat(type, placement, ext, within,
696 style, name, listname, builtin);
697 floatlist_->newFloat(newfloat);
710 void LyXTextClass::readCounter(LyXLex & lexrc)
712 keyword_item counterTags[] = {
715 { "within", CT_WITHIN }
718 lexrc.pushTable(counterTags, CT_END);
724 while (!getout && lexrc.isOK()) {
725 int le = lexrc.lex();
727 case LyXLex::LEX_UNDEF:
728 lexrc.printError("Unknown ClassOption tag `$$Token'");
732 switch (static_cast<CounterTags>(le)) {
735 name = lexrc.getString();
739 within = lexrc.getString();
740 if (within == "none")
749 // Here if have a full counter if getout == true
751 if (within.empty()) {
752 ctrs_->newCounter(name);
754 ctrs_->newCounter(name, within);
762 LyXFont const & LyXTextClass::defaultfont() const
768 string const & LyXTextClass::leftmargin() const
774 string const & LyXTextClass::rightmargin() const
780 bool LyXTextClass::hasLayout(string const & n) const
782 string const name = (n.empty() ? defaultLayoutName() : n);
784 return find_if(layoutlist_.begin(), layoutlist_.end(),
786 != layoutlist_.end();
791 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
793 BOOST_ASSERT(!name.empty());
795 LayoutList::const_iterator cit =
796 find_if(layoutlist_.begin(),
800 if (cit == layoutlist_.end()) {
801 lyxerr << "We failed to find the layout '" << name
802 << "' in the layout list. You MUST investigate!"
804 for (LayoutList::const_iterator it = layoutlist_.begin();
805 it != layoutlist_.end(); ++it)
806 lyxerr << " " << it->get()->name() << endl;
808 // we require the name to exist
817 bool LyXTextClass::delete_layout(string const & name)
819 if (name == defaultLayoutName())
822 LayoutList::iterator it =
823 remove_if(layoutlist_.begin(), layoutlist_.end(),
826 LayoutList::iterator end = layoutlist_.end();
827 bool const ret = (it != end);
828 layoutlist_.erase(it, end);
833 // Load textclass info if not loaded yet
834 bool LyXTextClass::load() const
840 string const real_file = LibFileSearch("layouts", name_, "layout");
842 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
843 lyxerr << "Error reading `"
844 << MakeDisplayPath(real_file)
845 << "'\n(Check `" << name_
846 << "')\nCheck your installation and "
847 "try Options/Reconfigure..." << endl;
855 FloatList & LyXTextClass::floats()
857 return *floatlist_.get();
861 FloatList const & LyXTextClass::floats() const
863 return *floatlist_.get();
867 Counters & LyXTextClass::counters() const
873 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
875 CharStyles::iterator cs = charstyles().begin();
876 CharStyles::iterator csend = charstyles().end();
877 for (; cs != csend; ++cs) {
885 string const & LyXTextClass::defaultLayoutName() const
887 // This really should come from the actual layout... (Lgb)
888 return defaultlayout_;
892 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
894 return operator[](defaultLayoutName());
898 string const & LyXTextClass::name() const
904 string const & LyXTextClass::latexname() const
906 const_cast<LyXTextClass*>(this)->load();
911 string const & LyXTextClass::description() const
917 string const & LyXTextClass::opt_fontsize() const
919 return opt_fontsize_;
923 string const & LyXTextClass::opt_pagestyle() const
925 return opt_pagestyle_;
929 string const & LyXTextClass::options() const
935 string const & LyXTextClass::class_header() const
937 return class_header_;
941 string const & LyXTextClass::pagestyle() const
947 string const & LyXTextClass::preamble() const
953 LyXTextClass::PageSides LyXTextClass::sides() const
959 int LyXTextClass::secnumdepth() const
965 int LyXTextClass::tocdepth() const
971 OutputType LyXTextClass::outputType() const
977 bool LyXTextClass::provides(LyXTextClass::Provides p) const
979 return provides_ & p;
983 unsigned int LyXTextClass::columns() const
989 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
995 string const & LyXTextClass::titlename() const
1001 int LyXTextClass::size() const
1003 return layoutlist_.size();
1007 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1010 case LyXTextClass::OneSide:
1013 case LyXTextClass::TwoSides: