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)) {
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_PROVIDESMAKEIDX:
288 if (lexrc.next() && lexrc.getInteger())
289 provides_ |= makeidx;
293 if (lexrc.next() && lexrc.getInteger())
297 case TC_LEFTMARGIN: // left margin type
299 leftmargin_ = lexrc.getString();
302 case TC_RIGHTMARGIN: // right margin type
304 rightmargin_ = lexrc.getString();
309 if (!merge) { // we are at top level here.
310 lyxerr[Debug::TCLASS] << "Finished reading textclass "
311 << MakeDisplayPath(filename)
313 if (defaultlayout_.empty()) {
314 lyxerr << "Error: Textclass '" << name_
315 << "' is missing a defaultstyle." << endl;
319 lyxerr[Debug::TCLASS] << "Finished reading input file "
320 << MakeDisplayPath(filename)
327 void LyXTextClass::readOutputType(LyXLex & lexrc)
329 keyword_item outputTypeTags[] = {
330 { "docbook", DOCBOOK },
332 { "linuxdoc", LINUXDOC },
333 { "literate", LITERATE }
336 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
338 int le = lexrc.lex();
340 case LyXLex::LEX_UNDEF:
341 lexrc.printError("Unknown output type `$$Token'");
347 outputType_ = static_cast<OutputType>(le);
350 lyxerr << "Unhandled value " << le
351 << " in LyXTextClass::readOutputType." << endl;
358 enum MaxCounterTags {
359 MC_COUNTER_CHAPTER = 1,
361 MC_COUNTER_SUBSECTION,
362 MC_COUNTER_SUBSUBSECTION,
363 MC_COUNTER_PARAGRAPH,
364 MC_COUNTER_SUBPARAGRAPH,
372 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
374 keyword_item maxCounterTags[] = {
375 {"counter_chapter", MC_COUNTER_CHAPTER },
376 {"counter_enumi", MC_COUNTER_ENUMI },
377 {"counter_enumii", MC_COUNTER_ENUMII },
378 {"counter_enumiii", MC_COUNTER_ENUMIII },
379 {"counter_enumiv", MC_COUNTER_ENUMIV },
380 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
381 {"counter_section", MC_COUNTER_SECTION },
382 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
383 {"counter_subsection", MC_COUNTER_SUBSECTION },
384 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
387 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
388 int le = lexrc.lex();
390 case LyXLex::LEX_UNDEF:
391 lexrc.printError("Unknown MaxCounter tag `$$Token'");
395 switch (static_cast<MaxCounterTags>(le)) {
396 case MC_COUNTER_CHAPTER:
397 maxcounter_ = LABEL_COUNTER_CHAPTER;
399 case MC_COUNTER_SECTION:
400 maxcounter_ = LABEL_COUNTER_SECTION;
402 case MC_COUNTER_SUBSECTION:
403 maxcounter_ = LABEL_COUNTER_SUBSECTION;
405 case MC_COUNTER_SUBSUBSECTION:
406 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
408 case MC_COUNTER_PARAGRAPH:
409 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
411 case MC_COUNTER_SUBPARAGRAPH:
412 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
414 case MC_COUNTER_ENUMI:
415 maxcounter_ = LABEL_COUNTER_ENUMI;
417 case MC_COUNTER_ENUMII:
418 maxcounter_ = LABEL_COUNTER_ENUMII;
420 case MC_COUNTER_ENUMIII:
421 maxcounter_ = LABEL_COUNTER_ENUMIII;
423 case MC_COUNTER_ENUMIV:
424 maxcounter_ = LABEL_COUNTER_ENUMIV;
430 enum ClassOptionsTags {
438 void LyXTextClass::readClassOptions(LyXLex & lexrc)
440 keyword_item classOptionsTags[] = {
442 {"fontsize", CO_FONTSIZE },
443 {"other", CO_OTHER },
444 {"pagestyle", CO_PAGESTYLE }
447 lexrc.pushTable(classOptionsTags, CO_END);
449 while (!getout && lexrc.isOK()) {
450 int le = lexrc.lex();
452 case LyXLex::LEX_UNDEF:
453 lexrc.printError("Unknown ClassOption tag `$$Token'");
457 switch (static_cast<ClassOptionsTags>(le)) {
460 opt_fontsize_ = strip(lexrc.getString());
464 opt_pagestyle_ = strip(lexrc.getString());
468 options_ = lexrc.getString();
479 LyXFont const & LyXTextClass::defaultfont() const
485 string const & LyXTextClass::leftmargin() const
491 string const & LyXTextClass::rightmargin() const
497 bool LyXTextClass::hasLayout(string const & n) const
499 string const name = (n.empty() ? defaultLayoutName() : n);
501 return find_if(layoutlist.begin(), layoutlist.end(),
502 lyx::compare_memfun(&LyXLayout::name, name))
507 LyXLayout const & LyXTextClass::operator[](string const & n) const
509 lyx::Assert(!n.empty());
512 lyxerr << "Operator[] called with empty n" << endl;
514 string const name = (n.empty() ? defaultLayoutName() : n);
516 LayoutList::const_iterator cit =
517 find_if(layoutlist.begin(),
519 lyx::compare_memfun(&LyXLayout::name, name));
521 if (cit == layoutlist.end()) {
522 lyxerr << "We failed to find the layout '" << name
523 << "' in the layout list. You MUST investigate!"
526 // we require the name to exist
534 bool LyXTextClass::delete_layout(string const & name)
536 if (name == defaultLayoutName())
539 LayoutList::iterator it =
540 remove_if(layoutlist.begin(), layoutlist.end(),
541 lyx::compare_memfun(&LyXLayout::name, name));
542 LayoutList::iterator end = layoutlist.end();
543 bool const ret = (it != end);
544 layoutlist.erase(it, end);
549 // Load textclass info if not loaded yet
550 bool LyXTextClass::load() const
556 string const real_file = LibFileSearch("layouts", name_, "layout");
558 if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
559 lyxerr << "Error reading `"
560 << MakeDisplayPath(real_file)
561 << "'\n(Check `" << name_
562 << "')\nCheck your installation and "
563 "try Options/Reconfigure..." << endl;
572 string const LyXTextClass::defaultLayoutName() const
574 // This really should come from the actual layout... (Lgb)
575 return defaultlayout_;
579 LyXLayout const & LyXTextClass::defaultLayout() const
581 return operator[](defaultLayoutName());
585 string const & LyXTextClass::name() const
591 string const & LyXTextClass::latexname() const
593 const_cast<LyXTextClass*>(this)->load();
598 string const & LyXTextClass::description() const
604 string const & LyXTextClass::opt_fontsize() const
606 return opt_fontsize_;
610 string const & LyXTextClass::opt_pagestyle() const
612 return opt_pagestyle_;
616 string const & LyXTextClass::options() const
622 string const & LyXTextClass::pagestyle() const
628 string const & LyXTextClass::preamble() const
634 LyXTextClass::PageSides LyXTextClass::sides() const
640 int LyXTextClass::secnumdepth() const
646 int LyXTextClass::tocdepth() const
652 OutputType LyXTextClass::outputType() const
658 bool LyXTextClass::provides(LyXTextClass::Provides p) const
660 return provides_ & p;
664 unsigned int LyXTextClass::columns() const
670 int LyXTextClass::maxcounter() const
676 int LyXTextClass::size() const
678 return layoutlist.size();
682 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
685 case LyXTextClass::OneSide:
688 case LyXTextClass::TwoSides: