1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ======================================================
15 #pragma implementation
18 #include "lyxtextclass.h"
22 #include "support/lstrings.h"
23 #include "support/LAssert.h"
24 #include "support/lyxfunctional.h"
25 #include "support/filetools.h"
37 compare_name(string const & name)
40 bool operator()(C & c) {
41 return c->name() == name_;
49 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
51 : name_(fn), latexname_(cln), description_(desc)
58 pagestyle_ = "default";
59 maxcounter_ = LABEL_COUNTER_CHAPTER;
60 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
61 opt_fontsize_ = "10|11|12";
62 opt_pagestyle_ = "empty|plain|headings|fancy";
68 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
70 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
71 if (!lay.Read(lexrc, *this)) {
73 lay.resfont = lay.font;
74 #ifndef INHERIT_LANGUAGE
75 lay.resfont.realize(defaultfont());
76 lay.reslabelfont = lay.labelfont;
77 lay.reslabelfont.realize(defaultfont());
79 lay.resfont.realize(defaultfont(), default_language);
80 lay.reslabelfont = lay.labelfont;
81 lay.reslabelfont.realize(defaultfont(), default_language);
83 return false; // no errors
85 lyxerr << "Error parsing style `" << lay.name() << "'" << endl;
114 // Reads a textclass structure from file.
115 bool LyXTextClass::Read(string const & filename, bool merge)
117 keyword_item textClassTags[] = {
118 { "classoptions", TC_CLASSOPTIONS },
119 { "columns", TC_COLUMNS },
120 { "defaultfont", TC_DEFAULTFONT },
121 { "defaultstyle", TC_DEFAULTSTYLE },
122 { "input", TC_INPUT },
123 { "leftmargin", TC_LEFTMARGIN },
124 { "maxcounter", TC_MAXCOUNTER },
125 { "nostyle", TC_NOSTYLE },
126 { "outputtype", TC_OUTPUTTYPE },
127 { "pagestyle", TC_PAGESTYLE },
128 { "preamble", TC_PREAMBLE },
129 { "providesamsmath", TC_PROVIDESAMSMATH },
130 { "providesmakeidx", TC_PROVIDESMAKEIDX },
131 { "providesnatbib", TC_PROVIDESNATBIB },
132 { "providesurl", TC_PROVIDESURL },
133 { "rightmargin", TC_RIGHTMARGIN },
134 { "secnumdepth", TC_SECNUMDEPTH },
135 { "sides", TC_SIDES },
136 { "style", TC_STYLE },
137 { "tocdepth", TC_TOCDEPTH }
141 lyxerr[Debug::TCLASS] << "Reading textclass "
142 << MakeDisplayPath(filename)
145 lyxerr[Debug::TCLASS] << "Reading input file "
146 << MakeDisplayPath(filename)
149 LyXLex lexrc(textClassTags, TC_RIGHTMARGIN);
152 lexrc.setFile(filename);
153 if (!lexrc.isOK()) error = true;
156 while (lexrc.isOK() && !error) {
157 int le = lexrc.lex();
159 case LyXLex::LEX_FEOF:
162 case LyXLex::LEX_UNDEF:
163 lexrc.printError("Unknown TextClass tag `$$Token'");
168 switch (static_cast<TextClassTags>(le)) {
169 case TC_OUTPUTTYPE: // output type definition
170 readOutputType(lexrc);
173 case TC_INPUT: // Include file
175 string tmp = LibFileSearch("layouts",
179 if (Read(tmp, true)) {
180 lexrc.printError("Error reading input"
187 case TC_DEFAULTSTYLE:
189 string const name = subst(lexrc.getString(),
191 defaultlayout_ = name;
197 string const name = subst(lexrc.getString(),
199 if (hasLayout(name)) {
201 operator[](name).get();
202 error = do_readStyle(lexrc, *lay);
206 if (!(error = do_readStyle(lexrc, lay)))
207 layoutlist.push_back(boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
208 if (defaultlayout_.empty()) {
209 // We do not have a default
210 // layout yet, so we choose
211 // the first layout we
213 defaultlayout_ = name;
218 lexrc.printError("No name given for style: `$$Token'.");
225 string const style = subst(lexrc.getString(),
227 if (!delete_layout(style))
228 lyxerr << "Cannot delete style `" << style << "'" << endl;
229 // lexrc.printError("Cannot delete style"
236 columns_ = lexrc.getInteger();
241 switch (lexrc.getInteger()) {
242 case 1: sides_ = OneSide; break;
243 case 2: sides_ = TwoSides; break;
245 lyxerr << "Impossible number of page"
246 " sides, setting to one."
256 pagestyle_ = rtrim(lexrc.getString());
260 defaultfont_.lyxRead(lexrc);
261 if (!defaultfont_.resolved()) {
262 lexrc.printError("Warning: defaultfont should "
263 "be fully instantiated!");
264 #ifndef INHERIT_LANGUAGE
265 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
267 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE),
274 readMaxCounter(lexrc);
279 secnumdepth_ = lexrc.getInteger();
284 tocdepth_ = lexrc.getInteger();
287 // First step to support options
288 case TC_CLASSOPTIONS:
289 readClassOptions(lexrc);
293 preamble_ = lexrc.getLongString("EndPreamble");
296 case TC_PROVIDESAMSMATH:
297 if (lexrc.next() && lexrc.getInteger())
298 provides_ |= amsmath;
301 case TC_PROVIDESNATBIB:
302 if (lexrc.next() && lexrc.getInteger())
306 case TC_PROVIDESMAKEIDX:
307 if (lexrc.next() && lexrc.getInteger())
308 provides_ |= makeidx;
312 if (lexrc.next() && lexrc.getInteger())
316 case TC_LEFTMARGIN: // left margin type
318 leftmargin_ = lexrc.getString();
321 case TC_RIGHTMARGIN: // right margin type
323 rightmargin_ = lexrc.getString();
328 if (!merge) { // we are at top level here.
329 lyxerr[Debug::TCLASS] << "Finished reading textclass "
330 << MakeDisplayPath(filename)
332 if (defaultlayout_.empty()) {
333 lyxerr << "Error: Textclass '" << name_
334 << "' is missing a defaultstyle." << endl;
338 lyxerr[Debug::TCLASS] << "Finished reading input file "
339 << MakeDisplayPath(filename)
346 void LyXTextClass::readOutputType(LyXLex & lexrc)
348 keyword_item outputTypeTags[] = {
349 { "docbook", DOCBOOK },
351 { "linuxdoc", LINUXDOC },
352 { "literate", LITERATE }
355 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
357 int le = lexrc.lex();
359 case LyXLex::LEX_UNDEF:
360 lexrc.printError("Unknown output type `$$Token'");
366 outputType_ = static_cast<OutputType>(le);
369 lyxerr << "Unhandled value " << le
370 << " in LyXTextClass::readOutputType." << endl;
377 enum MaxCounterTags {
378 MC_COUNTER_CHAPTER = 1,
380 MC_COUNTER_SUBSECTION,
381 MC_COUNTER_SUBSUBSECTION,
382 MC_COUNTER_PARAGRAPH,
383 MC_COUNTER_SUBPARAGRAPH,
391 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
393 keyword_item maxCounterTags[] = {
394 {"counter_chapter", MC_COUNTER_CHAPTER },
395 {"counter_enumi", MC_COUNTER_ENUMI },
396 {"counter_enumii", MC_COUNTER_ENUMII },
397 {"counter_enumiii", MC_COUNTER_ENUMIII },
398 {"counter_enumiv", MC_COUNTER_ENUMIV },
399 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
400 {"counter_section", MC_COUNTER_SECTION },
401 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
402 {"counter_subsection", MC_COUNTER_SUBSECTION },
403 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
406 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
407 int le = lexrc.lex();
409 case LyXLex::LEX_UNDEF:
410 lexrc.printError("Unknown MaxCounter tag `$$Token'");
414 switch (static_cast<MaxCounterTags>(le)) {
415 case MC_COUNTER_CHAPTER:
416 maxcounter_ = LABEL_COUNTER_CHAPTER;
418 case MC_COUNTER_SECTION:
419 maxcounter_ = LABEL_COUNTER_SECTION;
421 case MC_COUNTER_SUBSECTION:
422 maxcounter_ = LABEL_COUNTER_SUBSECTION;
424 case MC_COUNTER_SUBSUBSECTION:
425 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
427 case MC_COUNTER_PARAGRAPH:
428 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
430 case MC_COUNTER_SUBPARAGRAPH:
431 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
433 case MC_COUNTER_ENUMI:
434 maxcounter_ = LABEL_COUNTER_ENUMI;
436 case MC_COUNTER_ENUMII:
437 maxcounter_ = LABEL_COUNTER_ENUMII;
439 case MC_COUNTER_ENUMIII:
440 maxcounter_ = LABEL_COUNTER_ENUMIII;
442 case MC_COUNTER_ENUMIV:
443 maxcounter_ = LABEL_COUNTER_ENUMIV;
449 enum ClassOptionsTags {
457 void LyXTextClass::readClassOptions(LyXLex & lexrc)
459 keyword_item classOptionsTags[] = {
461 {"fontsize", CO_FONTSIZE },
462 {"other", CO_OTHER },
463 {"pagestyle", CO_PAGESTYLE }
466 lexrc.pushTable(classOptionsTags, CO_END);
468 while (!getout && lexrc.isOK()) {
469 int le = lexrc.lex();
471 case LyXLex::LEX_UNDEF:
472 lexrc.printError("Unknown ClassOption tag `$$Token'");
476 switch (static_cast<ClassOptionsTags>(le)) {
479 opt_fontsize_ = rtrim(lexrc.getString());
483 opt_pagestyle_ = rtrim(lexrc.getString());
487 options_ = lexrc.getString();
498 LyXFont const & LyXTextClass::defaultfont() const
504 string const & LyXTextClass::leftmargin() const
510 string const & LyXTextClass::rightmargin() const
516 bool LyXTextClass::hasLayout(string const & n) const
518 string const name = (n.empty() ? defaultLayoutName() : n);
520 return find_if(layoutlist.begin(), layoutlist.end(),
526 LyXLayout_ptr const & LyXTextClass::operator[](string const & n) const
528 lyx::Assert(!n.empty());
531 lyxerr << "Operator[] called with empty n" << endl;
533 string const name = (n.empty() ? defaultLayoutName() : n);
535 static string lastLayoutName;
536 static LayoutList::difference_type lastLayoutIndex;
538 if (name == lastLayoutName)
539 return layoutlist[lastLayoutIndex];
541 LayoutList::const_iterator cit =
542 find_if(layoutlist.begin(),
546 if (cit == layoutlist.end()) {
547 lyxerr << "We failed to find the layout '" << name
548 << "' in the layout list. You MUST investigate!"
551 // we require the name to exist
555 lastLayoutName = name;
556 lastLayoutIndex = std::distance(layoutlist.begin(), cit);
562 bool LyXTextClass::delete_layout(string const & name)
564 if (name == defaultLayoutName())
567 LayoutList::iterator it =
568 remove_if(layoutlist.begin(), layoutlist.end(),
571 LayoutList::iterator end = layoutlist.end();
572 bool const ret = (it != end);
573 layoutlist.erase(it, end);
578 // Load textclass info if not loaded yet
579 bool LyXTextClass::load() const
585 string const real_file = LibFileSearch("layouts", name_, "layout");
587 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
588 lyxerr << "Error reading `"
589 << MakeDisplayPath(real_file)
590 << "'\n(Check `" << name_
591 << "')\nCheck your installation and "
592 "try Options/Reconfigure..." << endl;
600 string const LyXTextClass::defaultLayoutName() const
602 // This really should come from the actual layout... (Lgb)
603 return defaultlayout_;
607 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
609 return operator[](defaultLayoutName());
613 string const & LyXTextClass::name() const
619 string const & LyXTextClass::latexname() const
621 const_cast<LyXTextClass*>(this)->load();
626 string const & LyXTextClass::description() const
632 string const & LyXTextClass::opt_fontsize() const
634 return opt_fontsize_;
638 string const & LyXTextClass::opt_pagestyle() const
640 return opt_pagestyle_;
644 string const & LyXTextClass::options() const
650 string const & LyXTextClass::pagestyle() const
656 string const & LyXTextClass::preamble() const
662 LyXTextClass::PageSides LyXTextClass::sides() const
668 int LyXTextClass::secnumdepth() const
674 int LyXTextClass::tocdepth() const
680 OutputType LyXTextClass::outputType() const
686 bool LyXTextClass::provides(LyXTextClass::Provides p) const
688 return provides_ & p;
692 unsigned int LyXTextClass::columns() const
698 int LyXTextClass::maxcounter() const
704 int LyXTextClass::size() const
706 return layoutlist.size();
710 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
713 case LyXTextClass::OneSide:
716 case LyXTextClass::TwoSides: