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(), """, "\"");
534 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
536 keyword_item elementTags[] = {
539 { "labelfont", CS_LABELFONT },
540 { "latexname", CS_LATEXNAME },
541 { "latexparam", CS_LATEXPARAM },
542 { "latextype", CS_LATEXTYPE },
543 { "preamble", CS_PREAMBLE}
546 lexrc.pushTable(elementTags, CS_END);
551 LyXFont font(LyXFont::ALL_INHERIT);
552 LyXFont labelfont(LyXFont::ALL_INHERIT);
556 while (!getout && lexrc.isOK()) {
557 int le = lexrc.lex();
559 case LyXLex::LEX_UNDEF:
560 lexrc.printError("Unknown ClassOption tag `$$Token'");
564 switch (static_cast<CharStyleTags>(le)) {
567 latextype = lexrc.getString();
571 latexname = lexrc.getString();
575 latexparam = subst(lexrc.getString(), """, "\"");
578 labelfont.lyxRead(lexrc);
585 preamble = lexrc.getLongString("EndPreamble");
594 // Here add element to list if getout == true
598 cs.latextype = latextype;
599 cs.latexname = latexname;
600 cs.latexparam = latexparam;
602 cs.labelfont = labelfont;
603 cs.preamble = preamble;
604 charstyles().push_back(cs);
624 void LyXTextClass::readFloat(LyXLex & lexrc)
626 keyword_item floatTags[] = {
628 { "extension", FT_EXT },
629 { "guiname", FT_NAME },
630 { "latexbuiltin", FT_BUILTIN },
631 { "listname", FT_LISTNAME },
632 { "numberwithin", FT_WITHIN },
633 { "placement", FT_PLACEMENT },
634 { "style", FT_STYLE },
638 lexrc.pushTable(floatTags, FT_END);
647 bool builtin = false;
650 while (!getout && lexrc.isOK()) {
651 int le = lexrc.lex();
653 case LyXLex::LEX_UNDEF:
654 lexrc.printError("Unknown ClassOption tag `$$Token'");
658 switch (static_cast<FloatTags>(le)) {
661 type = lexrc.getString();
662 // Here we could check if this type is already defined
663 // and modify it with the rest of the vars instead.
667 name = lexrc.getString();
671 placement = lexrc.getString();
675 ext = lexrc.getString();
679 within = lexrc.getString();
680 if (within == "none")
685 style = lexrc.getString();
689 listname = lexrc.getString();
693 builtin = lexrc.getBool();
701 // Here if have a full float if getout == true
703 Floating newfloat(type, placement, ext, within,
704 style, name, listname, builtin);
705 floatlist_->newFloat(newfloat);
718 void LyXTextClass::readCounter(LyXLex & lexrc)
720 keyword_item counterTags[] = {
723 { "within", CT_WITHIN }
726 lexrc.pushTable(counterTags, CT_END);
732 while (!getout && lexrc.isOK()) {
733 int le = lexrc.lex();
735 case LyXLex::LEX_UNDEF:
736 lexrc.printError("Unknown ClassOption tag `$$Token'");
740 switch (static_cast<CounterTags>(le)) {
743 name = lexrc.getString();
747 within = lexrc.getString();
748 if (within == "none")
757 // Here if have a full counter if getout == true
759 if (within.empty()) {
760 ctrs_->newCounter(name);
762 ctrs_->newCounter(name, within);
770 LyXFont const & LyXTextClass::defaultfont() const
776 string const & LyXTextClass::leftmargin() const
782 string const & LyXTextClass::rightmargin() const
788 bool LyXTextClass::hasLayout(string const & n) const
790 string const name = (n.empty() ? defaultLayoutName() : n);
792 return find_if(layoutlist_.begin(), layoutlist_.end(),
794 != layoutlist_.end();
799 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
801 BOOST_ASSERT(!name.empty());
803 LayoutList::const_iterator cit =
804 find_if(layoutlist_.begin(),
808 if (cit == layoutlist_.end()) {
809 lyxerr << "We failed to find the layout '" << name
810 << "' in the layout list. You MUST investigate!"
812 for (LayoutList::const_iterator it = layoutlist_.begin();
813 it != layoutlist_.end(); ++it)
814 lyxerr << " " << it->get()->name() << endl;
816 // we require the name to exist
825 bool LyXTextClass::delete_layout(string const & name)
827 if (name == defaultLayoutName())
830 LayoutList::iterator it =
831 remove_if(layoutlist_.begin(), layoutlist_.end(),
834 LayoutList::iterator end = layoutlist_.end();
835 bool const ret = (it != end);
836 layoutlist_.erase(it, end);
841 // Load textclass info if not loaded yet
842 bool LyXTextClass::load() const
848 string const real_file = LibFileSearch("layouts", name_, "layout");
850 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
851 lyxerr << "Error reading `"
852 << MakeDisplayPath(real_file)
853 << "'\n(Check `" << name_
854 << "')\nCheck your installation and "
855 "try Options/Reconfigure..." << endl;
863 FloatList & LyXTextClass::floats()
865 return *floatlist_.get();
869 FloatList const & LyXTextClass::floats() const
871 return *floatlist_.get();
875 Counters & LyXTextClass::counters() const
881 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
883 CharStyles::iterator cs = charstyles().begin();
884 CharStyles::iterator csend = charstyles().end();
885 for (; cs != csend; ++cs) {
893 string const & LyXTextClass::defaultLayoutName() const
895 // This really should come from the actual layout... (Lgb)
896 return defaultlayout_;
900 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
902 return operator[](defaultLayoutName());
906 string const & LyXTextClass::name() const
912 string const & LyXTextClass::latexname() const
914 const_cast<LyXTextClass*>(this)->load();
919 string const & LyXTextClass::description() const
925 string const & LyXTextClass::opt_fontsize() const
927 return opt_fontsize_;
931 string const & LyXTextClass::opt_pagestyle() const
933 return opt_pagestyle_;
937 string const & LyXTextClass::options() const
943 string const & LyXTextClass::class_header() const
945 return class_header_;
949 string const & LyXTextClass::pagestyle() const
955 string const & LyXTextClass::preamble() const
961 LyXTextClass::PageSides LyXTextClass::sides() const
967 int LyXTextClass::secnumdepth() const
973 int LyXTextClass::tocdepth() const
979 OutputType LyXTextClass::outputType() const
985 bool LyXTextClass::provides(LyXTextClass::Provides p) const
987 return provides_ & p;
991 unsigned int LyXTextClass::columns() const
997 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1003 string const & LyXTextClass::titlename() const
1009 int LyXTextClass::size() const
1011 return layoutlist_.size();
1015 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1018 case LyXTextClass::OneSide:
1021 case LyXTextClass::TwoSides: