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"
35 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
37 : name_(fn), latexname_(cln), description_(desc)
44 pagestyle_ = "default";
45 maxcounter_ = LABEL_COUNTER_CHAPTER;
46 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
47 opt_fontsize_ = "10|11|12";
48 opt_pagestyle_ = "empty|plain|headings|fancy";
54 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
56 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
57 if (!lay.Read(lexrc, *this)) {
59 lay.resfont = lay.font;
60 #ifndef INHERIT_LANGUAGE
61 lay.resfont.realize(defaultfont());
62 lay.reslabelfont = lay.labelfont;
63 lay.reslabelfont.realize(defaultfont());
65 lay.resfont.realize(defaultfont(), default_language);
66 lay.reslabelfont = lay.labelfont;
67 lay.reslabelfont.realize(defaultfont(), default_language);
69 return false; // no errors
71 lyxerr << "Error parsing style `" << lay.name() << "'" << endl;
100 // Reads a textclass structure from file.
101 bool LyXTextClass::Read(string const & filename, bool merge)
103 keyword_item textClassTags[] = {
104 { "classoptions", TC_CLASSOPTIONS },
105 { "columns", TC_COLUMNS },
106 { "defaultfont", TC_DEFAULTFONT },
107 { "defaultstyle", TC_DEFAULTSTYLE },
108 { "input", TC_INPUT },
109 { "leftmargin", TC_LEFTMARGIN },
110 { "maxcounter", TC_MAXCOUNTER },
111 { "nostyle", TC_NOSTYLE },
112 { "outputtype", TC_OUTPUTTYPE },
113 { "pagestyle", TC_PAGESTYLE },
114 { "preamble", TC_PREAMBLE },
115 { "providesamsmath", TC_PROVIDESAMSMATH },
116 { "providesmakeidx", TC_PROVIDESMAKEIDX },
117 { "providesnatbib", TC_PROVIDESNATBIB },
118 { "providesurl", TC_PROVIDESURL },
119 { "rightmargin", TC_RIGHTMARGIN },
120 { "secnumdepth", TC_SECNUMDEPTH },
121 { "sides", TC_SIDES },
122 { "style", TC_STYLE },
123 { "tocdepth", TC_TOCDEPTH }
127 lyxerr[Debug::TCLASS] << "Reading textclass "
128 << MakeDisplayPath(filename)
131 lyxerr[Debug::TCLASS] << "Reading input file "
132 << MakeDisplayPath(filename)
135 LyXLex lexrc(textClassTags, TC_RIGHTMARGIN);
138 lexrc.setFile(filename);
139 if (!lexrc.isOK()) error = true;
142 while (lexrc.isOK() && !error) {
143 int le = lexrc.lex();
145 case LyXLex::LEX_FEOF:
148 case LyXLex::LEX_UNDEF:
149 lexrc.printError("Unknown TextClass tag `$$Token'");
154 switch (static_cast<TextClassTags>(le)) {
155 case TC_OUTPUTTYPE: // output type definition
156 readOutputType(lexrc);
159 case TC_INPUT: // Include file
161 string tmp = LibFileSearch("layouts",
165 if (Read(tmp, true)) {
166 lexrc.printError("Error reading input"
173 case TC_DEFAULTSTYLE:
175 string const name = subst(lexrc.getString(),
177 defaultlayout_ = name;
183 string const name = subst(lexrc.getString(),
185 if (hasLayout(name)) {
187 const_cast<LyXLayout &>(operator[](name));
188 error = do_readStyle(lexrc, lay);
192 if (!(error = do_readStyle(lexrc, lay)))
193 layoutlist.push_back(lay);
194 if (defaultlayout_.empty()) {
195 // We do not have a default
196 // layout yet, so we choose
197 // the first layout we
199 defaultlayout_ = name;
204 lexrc.printError("No name given for style: `$$Token'.");
211 string const style = subst(lexrc.getString(),
213 if (!delete_layout(style))
214 lyxerr << "Cannot delete style `" << style << "'" << endl;
215 // lexrc.printError("Cannot delete style"
222 columns_ = lexrc.getInteger();
227 switch (lexrc.getInteger()) {
228 case 1: sides_ = OneSide; break;
229 case 2: sides_ = TwoSides; break;
231 lyxerr << "Impossible number of page"
232 " sides, setting to one."
242 pagestyle_ = strip(lexrc.getString());
246 defaultfont_.lyxRead(lexrc);
247 if (!defaultfont_.resolved()) {
248 lexrc.printError("Warning: defaultfont should "
249 "be fully instantiated!");
250 #ifndef INHERIT_LANGUAGE
251 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
253 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE),
260 readMaxCounter(lexrc);
265 secnumdepth_ = lexrc.getInteger();
270 tocdepth_ = lexrc.getInteger();
273 // First step to support options
274 case TC_CLASSOPTIONS:
275 readClassOptions(lexrc);
279 preamble_ = lexrc.getLongString("EndPreamble");
282 case TC_PROVIDESAMSMATH:
283 if (lexrc.next() && lexrc.getInteger())
284 provides_ |= amsmath;
287 case TC_PROVIDESNATBIB:
288 if (lexrc.next() && lexrc.getInteger())
292 case TC_PROVIDESMAKEIDX:
293 if (lexrc.next() && lexrc.getInteger())
294 provides_ |= makeidx;
298 if (lexrc.next() && lexrc.getInteger())
302 case TC_LEFTMARGIN: // left margin type
304 leftmargin_ = lexrc.getString();
307 case TC_RIGHTMARGIN: // right margin type
309 rightmargin_ = lexrc.getString();
314 if (!merge) { // we are at top level here.
315 lyxerr[Debug::TCLASS] << "Finished reading textclass "
316 << MakeDisplayPath(filename)
318 if (defaultlayout_.empty()) {
319 lyxerr << "Error: Textclass '" << name_
320 << "' is missing a defaultstyle." << endl;
324 lyxerr[Debug::TCLASS] << "Finished reading input file "
325 << MakeDisplayPath(filename)
332 void LyXTextClass::readOutputType(LyXLex & lexrc)
334 keyword_item outputTypeTags[] = {
335 { "docbook", DOCBOOK },
337 { "linuxdoc", LINUXDOC },
338 { "literate", LITERATE }
341 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
343 int le = lexrc.lex();
345 case LyXLex::LEX_UNDEF:
346 lexrc.printError("Unknown output type `$$Token'");
352 outputType_ = static_cast<OutputType>(le);
355 lyxerr << "Unhandled value " << le
356 << " in LyXTextClass::readOutputType." << endl;
363 enum MaxCounterTags {
364 MC_COUNTER_CHAPTER = 1,
366 MC_COUNTER_SUBSECTION,
367 MC_COUNTER_SUBSUBSECTION,
368 MC_COUNTER_PARAGRAPH,
369 MC_COUNTER_SUBPARAGRAPH,
377 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
379 keyword_item maxCounterTags[] = {
380 {"counter_chapter", MC_COUNTER_CHAPTER },
381 {"counter_enumi", MC_COUNTER_ENUMI },
382 {"counter_enumii", MC_COUNTER_ENUMII },
383 {"counter_enumiii", MC_COUNTER_ENUMIII },
384 {"counter_enumiv", MC_COUNTER_ENUMIV },
385 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
386 {"counter_section", MC_COUNTER_SECTION },
387 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
388 {"counter_subsection", MC_COUNTER_SUBSECTION },
389 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
392 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
393 int le = lexrc.lex();
395 case LyXLex::LEX_UNDEF:
396 lexrc.printError("Unknown MaxCounter tag `$$Token'");
400 switch (static_cast<MaxCounterTags>(le)) {
401 case MC_COUNTER_CHAPTER:
402 maxcounter_ = LABEL_COUNTER_CHAPTER;
404 case MC_COUNTER_SECTION:
405 maxcounter_ = LABEL_COUNTER_SECTION;
407 case MC_COUNTER_SUBSECTION:
408 maxcounter_ = LABEL_COUNTER_SUBSECTION;
410 case MC_COUNTER_SUBSUBSECTION:
411 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
413 case MC_COUNTER_PARAGRAPH:
414 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
416 case MC_COUNTER_SUBPARAGRAPH:
417 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
419 case MC_COUNTER_ENUMI:
420 maxcounter_ = LABEL_COUNTER_ENUMI;
422 case MC_COUNTER_ENUMII:
423 maxcounter_ = LABEL_COUNTER_ENUMII;
425 case MC_COUNTER_ENUMIII:
426 maxcounter_ = LABEL_COUNTER_ENUMIII;
428 case MC_COUNTER_ENUMIV:
429 maxcounter_ = LABEL_COUNTER_ENUMIV;
435 enum ClassOptionsTags {
443 void LyXTextClass::readClassOptions(LyXLex & lexrc)
445 keyword_item classOptionsTags[] = {
447 {"fontsize", CO_FONTSIZE },
448 {"other", CO_OTHER },
449 {"pagestyle", CO_PAGESTYLE }
452 lexrc.pushTable(classOptionsTags, CO_END);
454 while (!getout && lexrc.isOK()) {
455 int le = lexrc.lex();
457 case LyXLex::LEX_UNDEF:
458 lexrc.printError("Unknown ClassOption tag `$$Token'");
462 switch (static_cast<ClassOptionsTags>(le)) {
465 opt_fontsize_ = strip(lexrc.getString());
469 opt_pagestyle_ = strip(lexrc.getString());
473 options_ = lexrc.getString();
484 LyXFont const & LyXTextClass::defaultfont() const
490 string const & LyXTextClass::leftmargin() const
496 string const & LyXTextClass::rightmargin() const
502 bool LyXTextClass::hasLayout(string const & n) const
504 string const name = (n.empty() ? defaultLayoutName() : n);
506 return find_if(layoutlist.begin(), layoutlist.end(),
507 lyx::compare_memfun(&LyXLayout::name, name))
512 LyXLayout const & LyXTextClass::operator[](string const & n) const
514 lyx::Assert(!n.empty());
517 lyxerr << "Operator[] called with empty n" << endl;
519 string const name = (n.empty() ? defaultLayoutName() : n);
521 static string lastLayoutName;
522 static LayoutList::difference_type lastLayoutIndex;
524 if (name == lastLayoutName)
525 return layoutlist[lastLayoutIndex];
527 LayoutList::const_iterator cit =
528 find_if(layoutlist.begin(),
530 lyx::compare_memfun(&LyXLayout::name, name));
532 if (cit == layoutlist.end()) {
533 lyxerr << "We failed to find the layout '" << name
534 << "' in the layout list. You MUST investigate!"
537 // we require the name to exist
541 lastLayoutName = name;
542 lastLayoutIndex = std::distance(layoutlist.begin(), cit);
548 bool LyXTextClass::delete_layout(string const & name)
550 if (name == defaultLayoutName())
553 LayoutList::iterator it =
554 remove_if(layoutlist.begin(), layoutlist.end(),
555 lyx::compare_memfun(&LyXLayout::name, name));
556 LayoutList::iterator end = layoutlist.end();
557 bool const ret = (it != end);
558 layoutlist.erase(it, end);
563 // Load textclass info if not loaded yet
564 bool LyXTextClass::load() const
570 string const real_file = LibFileSearch("layouts", name_, "layout");
572 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
573 lyxerr << "Error reading `"
574 << MakeDisplayPath(real_file)
575 << "'\n(Check `" << name_
576 << "')\nCheck your installation and "
577 "try Options/Reconfigure..." << endl;
585 string const LyXTextClass::defaultLayoutName() const
587 // This really should come from the actual layout... (Lgb)
588 return defaultlayout_;
592 LyXLayout const & LyXTextClass::defaultLayout() const
594 return operator[](defaultLayoutName());
598 string const & LyXTextClass::name() const
604 string const & LyXTextClass::latexname() const
606 const_cast<LyXTextClass*>(this)->load();
611 string const & LyXTextClass::description() const
617 string const & LyXTextClass::opt_fontsize() const
619 return opt_fontsize_;
623 string const & LyXTextClass::opt_pagestyle() const
625 return opt_pagestyle_;
629 string const & LyXTextClass::options() const
635 string const & LyXTextClass::pagestyle() const
641 string const & LyXTextClass::preamble() const
647 LyXTextClass::PageSides LyXTextClass::sides() const
653 int LyXTextClass::secnumdepth() const
659 int LyXTextClass::tocdepth() const
665 OutputType LyXTextClass::outputType() const
671 bool LyXTextClass::provides(LyXTextClass::Provides p) const
673 return provides_ & p;
677 unsigned int LyXTextClass::columns() const
683 int LyXTextClass::maxcounter() const
689 int LyXTextClass::size() const
691 return layoutlist.size();
695 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
698 case LyXTextClass::OneSide:
701 case LyXTextClass::TwoSides: