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;
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 { "input", TC_INPUT },
108 { "leftmargin", TC_LEFTMARGIN },
109 { "maxcounter", TC_MAXCOUNTER },
110 { "nostyle", TC_NOSTYLE },
111 { "outputtype", TC_OUTPUTTYPE },
112 { "pagestyle", TC_PAGESTYLE },
113 { "preamble", TC_PREAMBLE },
114 { "providesamsmath", TC_PROVIDESAMSMATH },
115 { "providesmakeidx", TC_PROVIDESMAKEIDX },
116 { "providesurl", TC_PROVIDESURL },
117 { "rightmargin", TC_RIGHTMARGIN },
118 { "secnumdepth", TC_SECNUMDEPTH },
119 { "sides", TC_SIDES },
120 { "style", TC_STYLE },
121 { "tocdepth", TC_TOCDEPTH }
125 lyxerr[Debug::TCLASS] << "Reading textclass "
126 << MakeDisplayPath(filename)
129 lyxerr[Debug::TCLASS] << "Reading input file "
130 << MakeDisplayPath(filename)
133 LyXLex lexrc(textClassTags, TC_RIGHTMARGIN);
136 lexrc.setFile(filename);
137 if (!lexrc.isOK()) error = true;
140 while (lexrc.isOK() && !error) {
141 int le = lexrc.lex();
143 case LyXLex::LEX_FEOF:
146 case LyXLex::LEX_UNDEF:
147 lexrc.printError("Unknown TextClass tag `$$Token'");
152 switch (static_cast<TextClassTags>(le)) {
153 case TC_OUTPUTTYPE: // output type definition
154 readOutputType(lexrc);
157 case TC_INPUT: // Include file
159 string tmp = LibFileSearch("layouts",
163 if (Read(tmp, true)) {
164 lexrc.printError("Error reading input"
173 string name = subst(lexrc.getString(),
175 if (hasLayout(name)) {
176 LyXLayout & lay = GetLayout(name);
177 error = do_readStyle(lexrc, lay);
181 if (!(error = do_readStyle(lexrc, lay)))
182 layoutlist.push_back(lay);
186 lexrc.printError("No name given for style: `$$Token'.");
193 string const style = subst(lexrc.getString(),
195 if (!delete_layout(style))
196 lexrc.printError("Cannot delete style"
203 columns_ = lexrc.getInteger();
208 switch (lexrc.getInteger()) {
209 case 1: sides_ = OneSide; break;
210 case 2: sides_ = TwoSides; break;
212 lyxerr << "Impossible number of page"
213 " sides, setting to one."
223 pagestyle_ = strip(lexrc.getString());
227 defaultfont_.lyxRead(lexrc);
228 if (!defaultfont_.resolved()) {
229 lexrc.printError("Warning: defaultfont should "
230 "be fully instantiated!");
231 #ifndef INHERIT_LANGUAGE
232 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
234 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE),
241 readMaxCounter(lexrc);
246 secnumdepth_ = lexrc.getInteger();
251 tocdepth_ = lexrc.getInteger();
254 // First step to support options
255 case TC_CLASSOPTIONS:
256 readClassOptions(lexrc);
260 preamble_ = lexrc.getLongString("EndPreamble");
263 case TC_PROVIDESAMSMATH:
264 if (lexrc.next() && lexrc.getInteger())
265 provides_ |= amsmath;
268 case TC_PROVIDESMAKEIDX:
269 if (lexrc.next() && lexrc.getInteger())
270 provides_ |= makeidx;
274 if (lexrc.next() && lexrc.getInteger())
278 case TC_LEFTMARGIN: // left margin type
280 leftmargin_ = lexrc.getString();
283 case TC_RIGHTMARGIN: // right margin type
285 rightmargin_ = lexrc.getString();
290 if (!merge) { // we are at top level here.
291 lyxerr[Debug::TCLASS] << "Finished reading textclass "
292 << MakeDisplayPath(filename)
295 lyxerr[Debug::TCLASS] << "Finished reading input file "
296 << MakeDisplayPath(filename)
303 void LyXTextClass::readOutputType(LyXLex & lexrc)
305 keyword_item outputTypeTags[] = {
306 { "docbook", DOCBOOK },
308 { "linuxdoc", LINUXDOC },
309 { "literate", LITERATE }
312 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
314 int le = lexrc.lex();
316 case LyXLex::LEX_UNDEF:
317 lexrc.printError("Unknown output type `$$Token'");
323 outputType_ = static_cast<OutputType>(le);
326 lyxerr << "Unhandled value " << le
327 << " in LyXTextClass::readOutputType." << endl;
334 enum MaxCounterTags {
335 MC_COUNTER_CHAPTER = 1,
337 MC_COUNTER_SUBSECTION,
338 MC_COUNTER_SUBSUBSECTION,
339 MC_COUNTER_PARAGRAPH,
340 MC_COUNTER_SUBPARAGRAPH,
348 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
350 keyword_item maxCounterTags[] = {
351 {"counter_chapter", MC_COUNTER_CHAPTER },
352 {"counter_enumi", MC_COUNTER_ENUMI },
353 {"counter_enumii", MC_COUNTER_ENUMII },
354 {"counter_enumiii", MC_COUNTER_ENUMIII },
355 {"counter_enumiv", MC_COUNTER_ENUMIV },
356 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
357 {"counter_section", MC_COUNTER_SECTION },
358 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
359 {"counter_subsection", MC_COUNTER_SUBSECTION },
360 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
363 pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
364 int le = lexrc.lex();
366 case LyXLex::LEX_UNDEF:
367 lexrc.printError("Unknown MaxCounter tag `$$Token'");
371 switch (static_cast<MaxCounterTags>(le)) {
372 case MC_COUNTER_CHAPTER:
373 maxcounter_ = LABEL_COUNTER_CHAPTER;
375 case MC_COUNTER_SECTION:
376 maxcounter_ = LABEL_COUNTER_SECTION;
378 case MC_COUNTER_SUBSECTION:
379 maxcounter_ = LABEL_COUNTER_SUBSECTION;
381 case MC_COUNTER_SUBSUBSECTION:
382 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
384 case MC_COUNTER_PARAGRAPH:
385 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
387 case MC_COUNTER_SUBPARAGRAPH:
388 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
390 case MC_COUNTER_ENUMI:
391 maxcounter_ = LABEL_COUNTER_ENUMI;
393 case MC_COUNTER_ENUMII:
394 maxcounter_ = LABEL_COUNTER_ENUMII;
396 case MC_COUNTER_ENUMIII:
397 maxcounter_ = LABEL_COUNTER_ENUMIII;
399 case MC_COUNTER_ENUMIV:
400 maxcounter_ = LABEL_COUNTER_ENUMIV;
406 enum ClassOptionsTags {
414 void LyXTextClass::readClassOptions(LyXLex & lexrc)
416 keyword_item classOptionsTags[] = {
418 {"fontsize", CO_FONTSIZE },
419 {"other", CO_OTHER },
420 {"pagestyle", CO_PAGESTYLE }
423 lexrc.pushTable(classOptionsTags, CO_END);
425 while (!getout && lexrc.isOK()) {
426 int le = lexrc.lex();
428 case LyXLex::LEX_UNDEF:
429 lexrc.printError("Unknown ClassOption tag `$$Token'");
433 switch (static_cast<ClassOptionsTags>(le)) {
436 opt_fontsize_ = strip(lexrc.getString());
440 opt_pagestyle_ = strip(lexrc.getString());
444 options_ = lexrc.getString();
455 LyXFont const & LyXTextClass::defaultfont() const
461 string const & LyXTextClass::leftmargin() const
467 string const & LyXTextClass::rightmargin() const
473 bool LyXTextClass::hasLayout(string const & name) const
475 return find_if(layoutlist.begin(), layoutlist.end(),
476 lyx::compare_memfun(&LyXLayout::name, name))
481 LyXLayout const & LyXTextClass::GetLayout (string const & name) const
483 LayoutList::const_iterator cit =
484 find_if(layoutlist.begin(),
486 lyx::compare_memfun(&LyXLayout::name, name));
487 lyx::Assert(cit != layoutlist.end()); // we require the name to exist
492 LyXLayout & LyXTextClass::GetLayout(string const & name)
494 LayoutList::iterator it =
495 find_if(layoutlist.begin(),
497 lyx::compare_memfun(&LyXLayout::name, name));
498 lyx::Assert(it != layoutlist.end()); // we require the name to exist
503 bool LyXTextClass::delete_layout(string const & name)
505 LayoutList::iterator it =
506 remove_if(layoutlist.begin(), layoutlist.end(),
507 lyx::compare_memfun(&LyXLayout::name, name));
508 LayoutList::iterator end = layoutlist.end();
509 bool const ret = (it != end);
510 layoutlist.erase(it, end);
515 // Load textclass info if not loaded yet
516 void LyXTextClass::load()
521 string const real_file = LibFileSearch("layouts", name_, "layout");
523 if (Read(real_file)) {
524 lyxerr << "Error reading `"
525 << MakeDisplayPath(real_file)
526 << "'\n(Check `" << name_
527 << "')\nCheck your installation and "
528 "try Options/Reconfigure..." << endl;
534 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
537 case LyXTextClass::OneSide:
540 case LyXTextClass::TwoSides: