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;
102 // Reads a textclass structure from file.
103 bool LyXTextClass::Read(string const & filename, bool merge)
105 keyword_item textClassTags[] = {
106 { "classoptions", TC_CLASSOPTIONS },
107 { "columns", TC_COLUMNS },
108 { "defaultfont", TC_DEFAULTFONT },
109 { "defaultstyle", TC_DEFAULTSTYLE },
110 { "input", TC_INPUT },
111 { "leftmargin", TC_LEFTMARGIN },
112 { "maxcounter", TC_MAXCOUNTER },
113 { "nostyle", TC_NOSTYLE },
114 { "outputtype", TC_OUTPUTTYPE },
115 { "pagestyle", TC_PAGESTYLE },
116 { "preamble", TC_PREAMBLE },
117 { "providesamsmath", TC_PROVIDESAMSMATH },
118 { "providesmakeidx", TC_PROVIDESMAKEIDX },
119 { "providesnatbib", TC_PROVIDESNATBIB },
120 { "providesurl", TC_PROVIDESURL },
121 { "rightmargin", TC_RIGHTMARGIN },
122 { "secnumdepth", TC_SECNUMDEPTH },
123 { "sides", TC_SIDES },
124 { "style", TC_STYLE },
125 { "tocdepth", TC_TOCDEPTH }
129 lyxerr[Debug::TCLASS] << "Reading textclass "
130 << MakeDisplayPath(filename)
133 lyxerr[Debug::TCLASS] << "Reading input file "
134 << MakeDisplayPath(filename)
137 LyXLex lexrc(textClassTags, TC_RIGHTMARGIN);
140 lexrc.setFile(filename);
141 if (!lexrc.isOK()) error = true;
144 while (lexrc.isOK() && !error) {
145 int le = lexrc.lex();
147 case LyXLex::LEX_FEOF:
150 case LyXLex::LEX_UNDEF:
151 lexrc.printError("Unknown TextClass tag `$$Token'");
156 switch (static_cast<TextClassTags>(le)) {
157 case TC_OUTPUTTYPE: // output type definition
158 readOutputType(lexrc);
161 case TC_INPUT: // Include file
163 string tmp = LibFileSearch("layouts",
167 if (Read(tmp, true)) {
168 lexrc.printError("Error reading input"
175 case TC_DEFAULTSTYLE:
177 string const name = subst(lexrc.getString(),
179 defaultlayout_ = name;
185 string const name = subst(lexrc.getString(),
187 if (hasLayout(name)) {
189 const_cast<LyXLayout &>(operator[](name));
190 error = do_readStyle(lexrc, lay);
194 if (!(error = do_readStyle(lexrc, lay)))
195 layoutlist.push_back(lay);
196 if (defaultlayout_.empty()) {
197 // We do not have a default
198 // layout yet, so we choose
199 // the first layout we
201 defaultlayout_ = name;
206 lexrc.printError("No name given for style: `$$Token'.");
213 string const style = subst(lexrc.getString(),
215 if (!delete_layout(style))
216 lyxerr << "Cannot delete style `" << style << "'" << endl;
217 // lexrc.printError("Cannot delete style"
224 columns_ = lexrc.getInteger();
229 switch (lexrc.getInteger()) {
230 case 1: sides_ = OneSide; break;
231 case 2: sides_ = TwoSides; break;
233 lyxerr << "Impossible number of page"
234 " sides, setting to one."
244 pagestyle_ = strip(lexrc.getString());
248 defaultfont_.lyxRead(lexrc);
249 if (!defaultfont_.resolved()) {
250 lexrc.printError("Warning: defaultfont should "
251 "be fully instantiated!");
252 #ifndef INHERIT_LANGUAGE
253 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
255 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE),
262 readMaxCounter(lexrc);
267 secnumdepth_ = lexrc.getInteger();
272 tocdepth_ = lexrc.getInteger();
275 // First step to support options
276 case TC_CLASSOPTIONS:
277 readClassOptions(lexrc);
281 preamble_ = lexrc.getLongString("EndPreamble");
284 case TC_PROVIDESAMSMATH:
285 if (lexrc.next() && lexrc.getInteger())
286 provides_ |= amsmath;
289 case TC_PROVIDESNATBIB:
290 if (lexrc.next() && lexrc.getInteger())
294 case TC_PROVIDESMAKEIDX:
295 if (lexrc.next() && lexrc.getInteger())
296 provides_ |= makeidx;
300 if (lexrc.next() && lexrc.getInteger())
304 case TC_LEFTMARGIN: // left margin type
306 leftmargin_ = lexrc.getString();
309 case TC_RIGHTMARGIN: // right margin type
311 rightmargin_ = lexrc.getString();
316 if (!merge) { // we are at top level here.
317 lyxerr[Debug::TCLASS] << "Finished reading textclass "
318 << MakeDisplayPath(filename)
320 if (defaultlayout_.empty()) {
321 lyxerr << "Error: Textclass '" << name_
322 << "' is missing a defaultstyle." << endl;
326 lyxerr[Debug::TCLASS] << "Finished reading input file "
327 << MakeDisplayPath(filename)
334 void LyXTextClass::readOutputType(LyXLex & lexrc)
336 keyword_item outputTypeTags[] = {
337 { "docbook", DOCBOOK },
339 { "linuxdoc", LINUXDOC },
340 { "literate", LITERATE }
343 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
345 int le = lexrc.lex();
347 case LyXLex::LEX_UNDEF:
348 lexrc.printError("Unknown output type `$$Token'");
354 outputType_ = static_cast<OutputType>(le);
357 lyxerr << "Unhandled value " << le
358 << " in LyXTextClass::readOutputType." << endl;
365 enum MaxCounterTags {
366 MC_COUNTER_CHAPTER = 1,
368 MC_COUNTER_SUBSECTION,
369 MC_COUNTER_SUBSUBSECTION,
370 MC_COUNTER_PARAGRAPH,
371 MC_COUNTER_SUBPARAGRAPH,
379 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
381 keyword_item maxCounterTags[] = {
382 {"counter_chapter", MC_COUNTER_CHAPTER },
383 {"counter_enumi", MC_COUNTER_ENUMI },
384 {"counter_enumii", MC_COUNTER_ENUMII },
385 {"counter_enumiii", MC_COUNTER_ENUMIII },
386 {"counter_enumiv", MC_COUNTER_ENUMIV },
387 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
388 {"counter_section", MC_COUNTER_SECTION },
389 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
390 {"counter_subsection", MC_COUNTER_SUBSECTION },
391 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
394 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
395 int le = lexrc.lex();
397 case LyXLex::LEX_UNDEF:
398 lexrc.printError("Unknown MaxCounter tag `$$Token'");
402 switch (static_cast<MaxCounterTags>(le)) {
403 case MC_COUNTER_CHAPTER:
404 maxcounter_ = LABEL_COUNTER_CHAPTER;
406 case MC_COUNTER_SECTION:
407 maxcounter_ = LABEL_COUNTER_SECTION;
409 case MC_COUNTER_SUBSECTION:
410 maxcounter_ = LABEL_COUNTER_SUBSECTION;
412 case MC_COUNTER_SUBSUBSECTION:
413 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
415 case MC_COUNTER_PARAGRAPH:
416 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
418 case MC_COUNTER_SUBPARAGRAPH:
419 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
421 case MC_COUNTER_ENUMI:
422 maxcounter_ = LABEL_COUNTER_ENUMI;
424 case MC_COUNTER_ENUMII:
425 maxcounter_ = LABEL_COUNTER_ENUMII;
427 case MC_COUNTER_ENUMIII:
428 maxcounter_ = LABEL_COUNTER_ENUMIII;
430 case MC_COUNTER_ENUMIV:
431 maxcounter_ = LABEL_COUNTER_ENUMIV;
437 enum ClassOptionsTags {
445 void LyXTextClass::readClassOptions(LyXLex & lexrc)
447 keyword_item classOptionsTags[] = {
449 {"fontsize", CO_FONTSIZE },
450 {"other", CO_OTHER },
451 {"pagestyle", CO_PAGESTYLE }
454 lexrc.pushTable(classOptionsTags, CO_END);
456 while (!getout && lexrc.isOK()) {
457 int le = lexrc.lex();
459 case LyXLex::LEX_UNDEF:
460 lexrc.printError("Unknown ClassOption tag `$$Token'");
464 switch (static_cast<ClassOptionsTags>(le)) {
467 opt_fontsize_ = strip(lexrc.getString());
471 opt_pagestyle_ = strip(lexrc.getString());
475 options_ = lexrc.getString();
486 LyXFont const & LyXTextClass::defaultfont() const
492 string const & LyXTextClass::leftmargin() const
498 string const & LyXTextClass::rightmargin() const
504 bool LyXTextClass::hasLayout(string const & n) const
506 string const name = (n.empty() ? defaultLayoutName() : n);
508 return find_if(layoutlist.begin(), layoutlist.end(),
509 lyx::compare_memfun(&LyXLayout::name, name))
514 LyXLayout const & LyXTextClass::operator[](string const & n) const
516 lyx::Assert(!n.empty());
519 lyxerr << "Operator[] called with empty n" << endl;
521 string const name = (n.empty() ? defaultLayoutName() : n);
523 static string lastLayoutName;
524 static LayoutList::difference_type lastLayoutIndex;
526 if (name == lastLayoutName)
527 return layoutlist[lastLayoutIndex];
530 LayoutList::const_iterator cit =
531 find_if(layoutlist.begin(),
533 lyx::compare_memfun(&LyXLayout::name, name));
535 if (cit == 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
544 lastLayoutName = name;
545 lastLayoutIndex = std::distance(layoutlist.begin(), cit);
551 bool LyXTextClass::delete_layout(string const & name)
553 if (name == defaultLayoutName())
556 LayoutList::iterator it =
557 remove_if(layoutlist.begin(), layoutlist.end(),
558 lyx::compare_memfun(&LyXLayout::name, name));
559 LayoutList::iterator end = layoutlist.end();
560 bool const ret = (it != end);
561 layoutlist.erase(it, end);
566 // Load textclass info if not loaded yet
567 bool LyXTextClass::load() const
573 string const real_file = LibFileSearch("layouts", name_, "layout");
575 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
576 lyxerr << "Error reading `"
577 << MakeDisplayPath(real_file)
578 << "'\n(Check `" << name_
579 << "')\nCheck your installation and "
580 "try Options/Reconfigure..." << endl;
589 string const LyXTextClass::defaultLayoutName() const
591 // This really should come from the actual layout... (Lgb)
592 return defaultlayout_;
596 LyXLayout const & LyXTextClass::defaultLayout() const
598 return operator[](defaultLayoutName());
602 string const & LyXTextClass::name() const
608 string const & LyXTextClass::latexname() const
610 const_cast<LyXTextClass*>(this)->load();
615 string const & LyXTextClass::description() const
621 string const & LyXTextClass::opt_fontsize() const
623 return opt_fontsize_;
627 string const & LyXTextClass::opt_pagestyle() const
629 return opt_pagestyle_;
633 string const & LyXTextClass::options() const
639 string const & LyXTextClass::pagestyle() const
645 string const & LyXTextClass::preamble() const
651 LyXTextClass::PageSides LyXTextClass::sides() const
657 int LyXTextClass::secnumdepth() const
663 int LyXTextClass::tocdepth() const
669 OutputType LyXTextClass::outputType() const
675 bool LyXTextClass::provides(LyXTextClass::Provides p) const
677 return provides_ & p;
681 unsigned int LyXTextClass::columns() const
687 int LyXTextClass::maxcounter() const
693 int LyXTextClass::size() const
695 return layoutlist.size();
699 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
702 case LyXTextClass::OneSide:
705 case LyXTextClass::TwoSides: