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 /* ******************************************************************* */
37 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
39 : name_(fn), latexname_(cln), description_(desc)
46 pagestyle_ = "default";
47 maxcounter_ = LABEL_COUNTER_CHAPTER;
48 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
49 opt_fontsize_ = "10|11|12";
50 opt_pagestyle_ = "empty|plain|headings|fancy";
56 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
58 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
59 if (!lay.Read(lexrc, *this)) {
61 lay.resfont = lay.font;
62 #ifndef INHERIT_LANGUAGE
63 lay.resfont.realize(defaultfont());
64 lay.reslabelfont = lay.labelfont;
65 lay.reslabelfont.realize(defaultfont());
67 lay.resfont.realize(defaultfont(), default_language);
68 lay.reslabelfont = lay.labelfont;
69 lay.reslabelfont.realize(defaultfont(), default_language);
71 return false; // no errors
73 lyxerr << "Error parsing style `" << lay.name() << "'" << endl;
101 // Reads a textclass structure from file.
102 bool LyXTextClass::Read(string const & filename, bool merge)
104 keyword_item textClassTags[] = {
105 { "classoptions", TC_CLASSOPTIONS },
106 { "columns", TC_COLUMNS },
107 { "defaultfont", TC_DEFAULTFONT },
108 { "defaultstyle", TC_DEFAULTSTYLE },
109 { "input", TC_INPUT },
110 { "leftmargin", TC_LEFTMARGIN },
111 { "maxcounter", TC_MAXCOUNTER },
112 { "nostyle", TC_NOSTYLE },
113 { "outputtype", TC_OUTPUTTYPE },
114 { "pagestyle", TC_PAGESTYLE },
115 { "preamble", TC_PREAMBLE },
116 { "providesamsmath", TC_PROVIDESAMSMATH },
117 { "providesmakeidx", TC_PROVIDESMAKEIDX },
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(lowercase(lexrc.getString()), '_', ' ');
176 defaultlayout_ = name;
182 string const name = subst(lowercase(lexrc.getString()),
184 if (hasLayout(name)) {
185 LyXLayout & lay = operator[](name);
186 error = do_readStyle(lexrc, lay);
190 if (!(error = do_readStyle(lexrc, lay)))
191 layoutlist.push_back(lay);
195 lexrc.printError("No name given for style: `$$Token'.");
202 string const style = subst(lowercase(lexrc.getString()),
204 if (!delete_layout(style))
205 lyxerr << "Cannot delete style `" << style << "'" << endl;
206 // lexrc.printError("Cannot delete style"
213 columns_ = lexrc.getInteger();
218 switch (lexrc.getInteger()) {
219 case 1: sides_ = OneSide; break;
220 case 2: sides_ = TwoSides; break;
222 lyxerr << "Impossible number of page"
223 " sides, setting to one."
233 pagestyle_ = strip(lexrc.getString());
237 defaultfont_.lyxRead(lexrc);
238 if (!defaultfont_.resolved()) {
239 lexrc.printError("Warning: defaultfont should "
240 "be fully instantiated!");
241 #ifndef INHERIT_LANGUAGE
242 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
244 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE),
251 readMaxCounter(lexrc);
256 secnumdepth_ = lexrc.getInteger();
261 tocdepth_ = lexrc.getInteger();
264 // First step to support options
265 case TC_CLASSOPTIONS:
266 readClassOptions(lexrc);
270 preamble_ = lexrc.getLongString("EndPreamble");
273 case TC_PROVIDESAMSMATH:
274 if (lexrc.next() && lexrc.getInteger())
275 provides_ |= amsmath;
278 case TC_PROVIDESMAKEIDX:
279 if (lexrc.next() && lexrc.getInteger())
280 provides_ |= makeidx;
284 if (lexrc.next() && lexrc.getInteger())
288 case TC_LEFTMARGIN: // left margin type
290 leftmargin_ = lexrc.getString();
293 case TC_RIGHTMARGIN: // right margin type
295 rightmargin_ = lexrc.getString();
300 if (!merge) { // we are at top level here.
301 lyxerr[Debug::TCLASS] << "Finished reading textclass "
302 << MakeDisplayPath(filename)
304 if (defaultlayout_.empty()) {
305 lyxerr << "Error: Textclass '" << name_
306 << "' is missing a defaultstyle." << endl;
310 lyxerr[Debug::TCLASS] << "Finished reading input file "
311 << MakeDisplayPath(filename)
318 void LyXTextClass::readOutputType(LyXLex & lexrc)
320 keyword_item outputTypeTags[] = {
321 { "docbook", DOCBOOK },
323 { "linuxdoc", LINUXDOC },
324 { "literate", LITERATE }
327 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
329 int le = lexrc.lex();
331 case LyXLex::LEX_UNDEF:
332 lexrc.printError("Unknown output type `$$Token'");
338 outputType_ = static_cast<OutputType>(le);
341 lyxerr << "Unhandled value " << le
342 << " in LyXTextClass::readOutputType." << endl;
349 enum MaxCounterTags {
350 MC_COUNTER_CHAPTER = 1,
352 MC_COUNTER_SUBSECTION,
353 MC_COUNTER_SUBSUBSECTION,
354 MC_COUNTER_PARAGRAPH,
355 MC_COUNTER_SUBPARAGRAPH,
363 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
365 keyword_item maxCounterTags[] = {
366 {"counter_chapter", MC_COUNTER_CHAPTER },
367 {"counter_enumi", MC_COUNTER_ENUMI },
368 {"counter_enumii", MC_COUNTER_ENUMII },
369 {"counter_enumiii", MC_COUNTER_ENUMIII },
370 {"counter_enumiv", MC_COUNTER_ENUMIV },
371 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
372 {"counter_section", MC_COUNTER_SECTION },
373 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
374 {"counter_subsection", MC_COUNTER_SUBSECTION },
375 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
378 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
379 int le = lexrc.lex();
381 case LyXLex::LEX_UNDEF:
382 lexrc.printError("Unknown MaxCounter tag `$$Token'");
386 switch (static_cast<MaxCounterTags>(le)) {
387 case MC_COUNTER_CHAPTER:
388 maxcounter_ = LABEL_COUNTER_CHAPTER;
390 case MC_COUNTER_SECTION:
391 maxcounter_ = LABEL_COUNTER_SECTION;
393 case MC_COUNTER_SUBSECTION:
394 maxcounter_ = LABEL_COUNTER_SUBSECTION;
396 case MC_COUNTER_SUBSUBSECTION:
397 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
399 case MC_COUNTER_PARAGRAPH:
400 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
402 case MC_COUNTER_SUBPARAGRAPH:
403 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
405 case MC_COUNTER_ENUMI:
406 maxcounter_ = LABEL_COUNTER_ENUMI;
408 case MC_COUNTER_ENUMII:
409 maxcounter_ = LABEL_COUNTER_ENUMII;
411 case MC_COUNTER_ENUMIII:
412 maxcounter_ = LABEL_COUNTER_ENUMIII;
414 case MC_COUNTER_ENUMIV:
415 maxcounter_ = LABEL_COUNTER_ENUMIV;
421 enum ClassOptionsTags {
429 void LyXTextClass::readClassOptions(LyXLex & lexrc)
431 keyword_item classOptionsTags[] = {
433 {"fontsize", CO_FONTSIZE },
434 {"other", CO_OTHER },
435 {"pagestyle", CO_PAGESTYLE }
438 lexrc.pushTable(classOptionsTags, CO_END);
440 while (!getout && lexrc.isOK()) {
441 int le = lexrc.lex();
443 case LyXLex::LEX_UNDEF:
444 lexrc.printError("Unknown ClassOption tag `$$Token'");
448 switch (static_cast<ClassOptionsTags>(le)) {
451 opt_fontsize_ = strip(lexrc.getString());
455 opt_pagestyle_ = strip(lexrc.getString());
459 options_ = lexrc.getString();
470 LyXFont const & LyXTextClass::defaultfont() const
476 string const & LyXTextClass::leftmargin() const
482 string const & LyXTextClass::rightmargin() const
488 bool LyXTextClass::hasLayout(string const & n) const
490 string const name = (n.empty() ? defaultLayoutName() : lowercase(n));
492 return find_if(layoutlist.begin(), layoutlist.end(),
493 lyx::compare_memfun(&LyXLayout::name, name))
498 LyXLayout const & LyXTextClass::operator[](string const & n) const
501 lyxerr << "Operator[] called with empty n" << endl;
503 string const name = (n.empty() ? defaultLayoutName() : lowercase(n));
505 LayoutList::const_iterator cit =
506 find_if(layoutlist.begin(),
508 lyx::compare_memfun(&LyXLayout::name, name));
510 if (cit == layoutlist.end()) {
511 lyxerr << "We failed to find the layout '" << name
512 << "' in the layout list. You MUST investigate!"
515 // we require the name to exist
523 LyXLayout & LyXTextClass::operator[](string const & n)
526 lyxerr << "Operator[] called with empty n" << endl;
528 string const name = (n.empty() ? defaultLayoutName() : lowercase(n));
530 LayoutList::iterator it =
531 find_if(layoutlist.begin(),
533 lyx::compare_memfun(&LyXLayout::name, name));
535 if (it == layoutlist.end()) {
536 lyxerr << "We failed to find the layout '" << name
537 << "' in the layout list. You MUST investigate!"
540 // we require the name to exist
548 bool LyXTextClass::delete_layout(string const & n)
550 string const name = lowercase(n);
552 if (name == defaultLayoutName())
555 LayoutList::iterator it =
556 remove_if(layoutlist.begin(), layoutlist.end(),
557 lyx::compare_memfun(&LyXLayout::name, name));
558 LayoutList::iterator end = layoutlist.end();
559 bool const ret = (it != end);
560 layoutlist.erase(it, end);
565 // Load textclass info if not loaded yet
566 bool LyXTextClass::load() const
572 string const real_file = LibFileSearch("layouts", name_, "layout");
574 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
575 lyxerr << "Error reading `"
576 << MakeDisplayPath(real_file)
577 << "'\n(Check `" << name_
578 << "')\nCheck your installation and "
579 "try Options/Reconfigure..." << endl;
588 string const LyXTextClass::defaultLayoutName() const
590 // This really should come from the actual layout... (Lgb)
591 return defaultlayout_;
595 LyXLayout const & LyXTextClass::defaultLayout() const
597 return operator[](defaultLayoutName());
601 LyXLayout & LyXTextClass::defaultLayout()
603 return operator[](defaultLayoutName());
607 string const & LyXTextClass::name() const
613 string const & LyXTextClass::latexname() const
615 const_cast<LyXTextClass*>(this)->load();
620 string const & LyXTextClass::description() const
626 string const & LyXTextClass::opt_fontsize() const
628 return opt_fontsize_;
632 string const & LyXTextClass::opt_pagestyle() const
634 return opt_pagestyle_;
638 string const & LyXTextClass::options() const
644 string const & LyXTextClass::pagestyle() const
650 string const & LyXTextClass::preamble() const
656 LyXTextClass::PageSides LyXTextClass::sides() const
662 int LyXTextClass::secnumdepth() const
668 int LyXTextClass::tocdepth() const
674 OutputType LyXTextClass::outputType() const
680 bool LyXTextClass::provides(LyXTextClass::Provides p) const
682 return provides_ & p;
686 unsigned int LyXTextClass::columns() const
692 int LyXTextClass::maxcounter() const
698 int LyXTextClass::size() const
700 return layoutlist.size();
704 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
707 case LyXTextClass::OneSide:
710 case LyXTextClass::TwoSides: