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(lexrc.getString(),
177 defaultlayout_ = name;
183 string const name = subst(lexrc.getString(),
185 if (hasLayout(name)) {
186 LyXLayout & lay = operator[](name);
187 error = do_readStyle(lexrc, lay);
191 if (!(error = do_readStyle(lexrc, lay)))
192 layoutlist.push_back(lay);
193 if (defaultlayout_.empty()) {
194 // We do not have a default
195 // layout yet, so we choose
196 // the first layout we
198 defaultlayout_ = name;
203 lexrc.printError("No name given for style: `$$Token'.");
210 string const style = subst(lexrc.getString(),
212 if (!delete_layout(style))
213 lyxerr << "Cannot delete style `" << style << "'" << endl;
214 // lexrc.printError("Cannot delete style"
221 columns_ = lexrc.getInteger();
226 switch (lexrc.getInteger()) {
227 case 1: sides_ = OneSide; break;
228 case 2: sides_ = TwoSides; break;
230 lyxerr << "Impossible number of page"
231 " sides, setting to one."
241 pagestyle_ = strip(lexrc.getString());
245 defaultfont_.lyxRead(lexrc);
246 if (!defaultfont_.resolved()) {
247 lexrc.printError("Warning: defaultfont should "
248 "be fully instantiated!");
249 #ifndef INHERIT_LANGUAGE
250 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
252 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE),
259 readMaxCounter(lexrc);
264 secnumdepth_ = lexrc.getInteger();
269 tocdepth_ = lexrc.getInteger();
272 // First step to support options
273 case TC_CLASSOPTIONS:
274 readClassOptions(lexrc);
278 preamble_ = lexrc.getLongString("EndPreamble");
281 case TC_PROVIDESAMSMATH:
282 if (lexrc.next() && lexrc.getInteger())
283 provides_ |= amsmath;
286 case TC_PROVIDESMAKEIDX:
287 if (lexrc.next() && lexrc.getInteger())
288 provides_ |= makeidx;
292 if (lexrc.next() && lexrc.getInteger())
296 case TC_LEFTMARGIN: // left margin type
298 leftmargin_ = lexrc.getString();
301 case TC_RIGHTMARGIN: // right margin type
303 rightmargin_ = lexrc.getString();
308 if (!merge) { // we are at top level here.
309 lyxerr[Debug::TCLASS] << "Finished reading textclass "
310 << MakeDisplayPath(filename)
312 if (defaultlayout_.empty()) {
313 lyxerr << "Error: Textclass '" << name_
314 << "' is missing a defaultstyle." << endl;
318 lyxerr[Debug::TCLASS] << "Finished reading input file "
319 << MakeDisplayPath(filename)
326 void LyXTextClass::readOutputType(LyXLex & lexrc)
328 keyword_item outputTypeTags[] = {
329 { "docbook", DOCBOOK },
331 { "linuxdoc", LINUXDOC },
332 { "literate", LITERATE }
335 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
337 int le = lexrc.lex();
339 case LyXLex::LEX_UNDEF:
340 lexrc.printError("Unknown output type `$$Token'");
346 outputType_ = static_cast<OutputType>(le);
349 lyxerr << "Unhandled value " << le
350 << " in LyXTextClass::readOutputType." << endl;
357 enum MaxCounterTags {
358 MC_COUNTER_CHAPTER = 1,
360 MC_COUNTER_SUBSECTION,
361 MC_COUNTER_SUBSUBSECTION,
362 MC_COUNTER_PARAGRAPH,
363 MC_COUNTER_SUBPARAGRAPH,
371 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
373 keyword_item maxCounterTags[] = {
374 {"counter_chapter", MC_COUNTER_CHAPTER },
375 {"counter_enumi", MC_COUNTER_ENUMI },
376 {"counter_enumii", MC_COUNTER_ENUMII },
377 {"counter_enumiii", MC_COUNTER_ENUMIII },
378 {"counter_enumiv", MC_COUNTER_ENUMIV },
379 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
380 {"counter_section", MC_COUNTER_SECTION },
381 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
382 {"counter_subsection", MC_COUNTER_SUBSECTION },
383 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
386 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
387 int le = lexrc.lex();
389 case LyXLex::LEX_UNDEF:
390 lexrc.printError("Unknown MaxCounter tag `$$Token'");
394 switch (static_cast<MaxCounterTags>(le)) {
395 case MC_COUNTER_CHAPTER:
396 maxcounter_ = LABEL_COUNTER_CHAPTER;
398 case MC_COUNTER_SECTION:
399 maxcounter_ = LABEL_COUNTER_SECTION;
401 case MC_COUNTER_SUBSECTION:
402 maxcounter_ = LABEL_COUNTER_SUBSECTION;
404 case MC_COUNTER_SUBSUBSECTION:
405 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
407 case MC_COUNTER_PARAGRAPH:
408 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
410 case MC_COUNTER_SUBPARAGRAPH:
411 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
413 case MC_COUNTER_ENUMI:
414 maxcounter_ = LABEL_COUNTER_ENUMI;
416 case MC_COUNTER_ENUMII:
417 maxcounter_ = LABEL_COUNTER_ENUMII;
419 case MC_COUNTER_ENUMIII:
420 maxcounter_ = LABEL_COUNTER_ENUMIII;
422 case MC_COUNTER_ENUMIV:
423 maxcounter_ = LABEL_COUNTER_ENUMIV;
429 enum ClassOptionsTags {
437 void LyXTextClass::readClassOptions(LyXLex & lexrc)
439 keyword_item classOptionsTags[] = {
441 {"fontsize", CO_FONTSIZE },
442 {"other", CO_OTHER },
443 {"pagestyle", CO_PAGESTYLE }
446 lexrc.pushTable(classOptionsTags, CO_END);
448 while (!getout && lexrc.isOK()) {
449 int le = lexrc.lex();
451 case LyXLex::LEX_UNDEF:
452 lexrc.printError("Unknown ClassOption tag `$$Token'");
456 switch (static_cast<ClassOptionsTags>(le)) {
459 opt_fontsize_ = strip(lexrc.getString());
463 opt_pagestyle_ = strip(lexrc.getString());
467 options_ = lexrc.getString();
478 LyXFont const & LyXTextClass::defaultfont() const
484 string const & LyXTextClass::leftmargin() const
490 string const & LyXTextClass::rightmargin() const
496 bool LyXTextClass::hasLayout(string const & n) const
498 string const name = (n.empty() ? defaultLayoutName() : n);
500 return find_if(layoutlist.begin(), layoutlist.end(),
501 lyx::compare_memfun(&LyXLayout::name, name))
506 LyXLayout const & LyXTextClass::operator[](string const & n) const
508 lyx::Assert(!n.empty());
511 lyxerr << "Operator[] called with empty n" << endl;
513 string const name = (n.empty() ? defaultLayoutName() : n);
515 LayoutList::const_iterator cit =
516 find_if(layoutlist.begin(),
518 lyx::compare_memfun(&LyXLayout::name, name));
520 if (cit == layoutlist.end()) {
521 lyxerr << "We failed to find the layout '" << name
522 << "' in the layout list. You MUST investigate!"
525 // we require the name to exist
533 LyXLayout & LyXTextClass::operator[](string const & n)
535 lyx::Assert(!n.empty());
538 lyxerr << "Operator[] called with empty n" << endl;
540 string const name = (n.empty() ? defaultLayoutName() : n);
542 LayoutList::iterator it =
543 find_if(layoutlist.begin(),
545 lyx::compare_memfun(&LyXLayout::name, name));
547 if (it == layoutlist.end()) {
548 lyxerr << "We failed to find the layout '" << name
549 << "' in the layout list. You MUST investigate!"
552 // we require the name to exist
560 bool LyXTextClass::delete_layout(string const & name)
562 if (name == defaultLayoutName())
565 LayoutList::iterator it =
566 remove_if(layoutlist.begin(), layoutlist.end(),
567 lyx::compare_memfun(&LyXLayout::name, name));
568 LayoutList::iterator end = layoutlist.end();
569 bool const ret = (it != end);
570 layoutlist.erase(it, end);
575 // Load textclass info if not loaded yet
576 bool LyXTextClass::load() const
582 string const real_file = LibFileSearch("layouts", name_, "layout");
584 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
585 lyxerr << "Error reading `"
586 << MakeDisplayPath(real_file)
587 << "'\n(Check `" << name_
588 << "')\nCheck your installation and "
589 "try Options/Reconfigure..." << endl;
598 string const LyXTextClass::defaultLayoutName() const
600 // This really should come from the actual layout... (Lgb)
601 return defaultlayout_;
605 LyXLayout const & LyXTextClass::defaultLayout() const
607 return operator[](defaultLayoutName());
611 LyXLayout & LyXTextClass::defaultLayout()
613 return operator[](defaultLayoutName());
617 string const & LyXTextClass::name() const
623 string const & LyXTextClass::latexname() const
625 const_cast<LyXTextClass*>(this)->load();
630 string const & LyXTextClass::description() const
636 string const & LyXTextClass::opt_fontsize() const
638 return opt_fontsize_;
642 string const & LyXTextClass::opt_pagestyle() const
644 return opt_pagestyle_;
648 string const & LyXTextClass::options() const
654 string const & LyXTextClass::pagestyle() const
660 string const & LyXTextClass::preamble() const
666 LyXTextClass::PageSides LyXTextClass::sides() const
672 int LyXTextClass::secnumdepth() const
678 int LyXTextClass::tocdepth() const
684 OutputType LyXTextClass::outputType() const
690 bool LyXTextClass::provides(LyXTextClass::Provides p) const
692 return provides_ & p;
696 unsigned int LyXTextClass::columns() const
702 int LyXTextClass::maxcounter() const
708 int LyXTextClass::size() const
710 return layoutlist.size();
714 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
717 case LyXTextClass::OneSide:
720 case LyXTextClass::TwoSides: