]> git.lyx.org Git - lyx.git/blob - src/TextClass.cpp
* docstream: factorize out some code and introduce odocfstream::reset()
[lyx.git] / src / TextClass.cpp
1 /**
2  * \file TextClass.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author John Levon
10  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "TextClass.h"
18
19 #include "Color.h"
20 #include "Counters.h"
21 #include "support/debug.h"
22 #include "support/gettext.h"
23 #include "Floating.h"
24 #include "FloatList.h"
25 #include "Layout.h"
26 #include "Lexer.h"
27
28 #include "frontends/alert.h"
29
30 #include "support/lstrings.h"
31 #include "support/lyxlib.h"
32 #include "support/filetools.h"
33 #include "support/os.h"
34
35 #include <sstream>
36
37 using std::endl;
38 using std::find_if;
39 using std::remove_if;
40 using std::string;
41 using std::ostream;
42
43 namespace lyx {
44
45 using support::FileName;
46 using support::libFileSearch;
47 using support::makeDisplayPath;
48 using support::quoteName;
49 using support::rtrim;
50 using support::subst;
51 using support::addName;
52
53 extern FontInfo lyxRead(Lexer &, FontInfo const & fi = sane_font);
54
55
56 namespace {
57
58 class LayoutNamesEqual : public std::unary_function<LayoutPtr, bool> {
59 public:
60         LayoutNamesEqual(docstring const & name)
61                 : name_(name)
62         {}
63         bool operator()(LayoutPtr const & c) const
64         {
65                 return c->name() == name_;
66         }
67 private:
68         docstring name_;
69 };
70
71
72 int const FORMAT = 5;
73
74
75 bool layout2layout(FileName const & filename, FileName const & tempfile)
76 {
77         FileName const script = libFileSearch("scripts", "layout2layout.py");
78         if (script.empty()) {
79                 lyxerr << "Could not find layout conversion "
80                           "script layout2layout.py." << endl;
81                 return false;
82         }
83
84         std::ostringstream command;
85         command << support::os::python() << ' ' << quoteName(script.toFilesystemEncoding())
86                 << ' ' << quoteName(filename.toFilesystemEncoding())
87                 << ' ' << quoteName(tempfile.toFilesystemEncoding());
88         string const command_str = command.str();
89
90         LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
91
92         support::cmd_ret const ret =
93                 support::runCommand(command_str);
94         if (ret.first != 0) {
95                 lyxerr << "Could not run layout conversion "
96                           "script layout2layout.py." << endl;
97                 return false;
98         }
99         return true;
100 }
101
102 } // namespace anon
103
104
105 TextClass::TextClass(string const & fn, string const & cln,
106                            string const & desc, bool texClassAvail )
107         : name_(fn), latexname_(cln), description_(desc),
108           floatlist_(new FloatList), counters_(new Counters),
109           texClassAvail_(texClassAvail)
110 {
111         modular_ = false;
112         outputType_ = LATEX;
113         columns_ = 1;
114         sides_ = OneSide;
115         secnumdepth_ = 3;
116         tocdepth_ = 3;
117         pagestyle_ = "default";
118         defaultfont_ = sane_font;
119         opt_fontsize_ = "10|11|12";
120         opt_pagestyle_ = "empty|plain|headings|fancy";
121         titletype_ = TITLE_COMMAND_AFTER;
122         titlename_ = "maketitle";
123         loaded_ = false;
124 }
125
126
127 bool TextClass::isTeXClassAvailable() const
128 {
129         return texClassAvail_;
130 }
131
132
133 bool TextClass::readStyle(Lexer & lexrc, Layout & lay)
134 {
135         LYXERR(Debug::TCLASS, "Reading style " << to_utf8(lay.name()));
136         if (!lay.read(lexrc, *this)) {
137                 // Resolve fonts
138                 lay.resfont = lay.font;
139                 lay.resfont.realize(defaultfont());
140                 lay.reslabelfont = lay.labelfont;
141                 lay.reslabelfont.realize(defaultfont());
142                 return false; // no errors
143         }
144         lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
145         return true;
146 }
147
148
149 enum TextClassTags {
150         TC_OUTPUTTYPE = 1,
151         TC_INPUT,
152         TC_STYLE,
153         TC_DEFAULTSTYLE,
154         TC_INSETLAYOUT,
155         TC_ENVIRONMENT,
156         TC_NOSTYLE,
157         TC_COLUMNS,
158         TC_SIDES,
159         TC_PAGESTYLE,
160         TC_DEFAULTFONT,
161         TC_SECNUMDEPTH,
162         TC_TOCDEPTH,
163         TC_CLASSOPTIONS,
164         TC_PREAMBLE,
165         TC_PROVIDES,
166         TC_LEFTMARGIN,
167         TC_RIGHTMARGIN,
168         TC_FLOAT,
169         TC_COUNTER,
170         TC_NOFLOAT,
171         TC_TITLELATEXNAME,
172         TC_TITLELATEXTYPE,
173         TC_FORMAT
174 };
175
176
177 // Reads a textclass structure from file.
178 bool TextClass::read(FileName const & filename, ReadType rt)
179 {
180         if (!filename.isReadableFile()) {
181                 lyxerr << "Cannot read layout file `" << filename << "'."
182                        << endl;
183                 return true;
184         }
185
186         keyword_item textClassTags[] = {
187                 { "classoptions",    TC_CLASSOPTIONS },
188                 { "columns",         TC_COLUMNS },
189                 { "counter",         TC_COUNTER },
190                 { "defaultfont",     TC_DEFAULTFONT },
191                 { "defaultstyle",    TC_DEFAULTSTYLE },
192                 { "environment",     TC_ENVIRONMENT },
193                 { "float",           TC_FLOAT },
194                 { "format",          TC_FORMAT },
195                 { "input",           TC_INPUT },
196                 { "insetlayout",     TC_INSETLAYOUT },
197                 { "leftmargin",      TC_LEFTMARGIN },
198                 { "nofloat",         TC_NOFLOAT },
199                 { "nostyle",         TC_NOSTYLE },
200                 { "outputtype",      TC_OUTPUTTYPE },
201                 { "pagestyle",       TC_PAGESTYLE },
202                 { "preamble",        TC_PREAMBLE },
203                 { "provides",        TC_PROVIDES },
204                 { "rightmargin",     TC_RIGHTMARGIN },
205                 { "secnumdepth",     TC_SECNUMDEPTH },
206                 { "sides",           TC_SIDES },
207                 { "style",           TC_STYLE },
208                 { "titlelatexname",  TC_TITLELATEXNAME },
209                 { "titlelatextype",  TC_TITLELATEXTYPE },
210                 { "tocdepth",        TC_TOCDEPTH }
211         };
212
213         switch (rt) {
214         case BASECLASS:
215                 LYXERR(Debug::TCLASS, "Reading textclass ");
216                 break;
217         case MERGE:
218                 LYXERR(Debug::TCLASS, "Reading input file ");
219           break;
220         case MODULE:
221                 LYXERR(Debug::TCLASS, "Reading module file ");
222                 break;
223         default:
224                 BOOST_ASSERT(false);
225         }
226         LYXERR(Debug::TCLASS, to_utf8(makeDisplayPath(filename.absFilename())));
227
228         Lexer lexrc(textClassTags,
229                 sizeof(textClassTags) / sizeof(textClassTags[0]));
230
231         lexrc.setFile(filename);
232         bool error = !lexrc.isOK();
233
234         // Format of files before the 'Format' tag was introduced
235         int format = 1;
236
237         // parsing
238         while (lexrc.isOK() && !error) {
239                 int le = lexrc.lex();
240
241                 switch (le) {
242                 case Lexer::LEX_FEOF:
243                         continue;
244
245                 case Lexer::LEX_UNDEF:
246                         lexrc.printError("Unknown TextClass tag `$$Token'");
247                         error = true;
248                         continue;
249
250                 default:
251                         break;
252                 }
253
254                 switch (static_cast<TextClassTags>(le)) {
255
256                 case TC_FORMAT:
257                         if (lexrc.next())
258                                 format = lexrc.getInteger();
259                         break;
260
261                 case TC_OUTPUTTYPE:   // output type definition
262                         readOutputType(lexrc);
263                         break;
264
265                 case TC_INPUT: // Include file
266                         if (lexrc.next()) {
267                                 string const inc = lexrc.getString();
268                                 FileName tmp = libFileSearch("layouts", inc,
269                                                             "layout");
270
271                                 if (tmp.empty()) {
272                                         lexrc.printError("Could not find input"
273                                                          "file: " + inc);
274                                         error = true;
275                                 } else if (read(tmp, MERGE)) {
276                                         lexrc.printError("Error reading input"
277                                                          "file: " + tmp.absFilename());
278                                         error = true;
279                                 }
280                         }
281                         break;
282
283                 case TC_DEFAULTSTYLE:
284                         if (lexrc.next()) {
285                                 docstring const name = from_utf8(subst(lexrc.getString(),
286                                                           '_', ' '));
287                                 defaultlayout_ = name;
288                         }
289                         break;
290
291                 case TC_ENVIRONMENT:
292                 case TC_STYLE:
293                         if (lexrc.next()) {
294                                 docstring const name = from_utf8(subst(lexrc.getString(),
295                                                     '_', ' '));
296                                 if (name.empty()) {
297                                         string s = "Could not read name for style: `$$Token' "
298                                                 + lexrc.getString() + " is probably not valid UTF-8!";
299                                         lexrc.printError(s.c_str());
300                                         Layout lay;
301                                         error = readStyle(lexrc, lay);
302                                 } else if (hasLayout(name)) {
303                                         Layout * lay = operator[](name).get();
304                                         error = readStyle(lexrc, *lay);
305                                 } else {
306                                         Layout lay;
307                                         lay.setName(name);
308                                         if (le == TC_ENVIRONMENT)
309                                                 lay.is_environment = true;
310                                         error = readStyle(lexrc, lay);
311                                         if (!error)
312                                                 layoutlist_.push_back(
313                                                         boost::shared_ptr<Layout>(new Layout(lay))
314                                                         );
315
316                                         if (defaultlayout_.empty()) {
317                                                 // We do not have a default
318                                                 // layout yet, so we choose
319                                                 // the first layout we
320                                                 // encounter.
321                                                 defaultlayout_ = name;
322                                         }
323                                 }
324                         }
325                         else {
326                                 lexrc.printError("No name given for style: `$$Token'.");
327                                 error = true;
328                         }
329                         break;
330
331                 case TC_NOSTYLE:
332                         if (lexrc.next()) {
333                                 docstring const style = from_utf8(subst(lexrc.getString(),
334                                                      '_', ' '));
335                                 if (!deleteLayout(style))
336                                         lyxerr << "Cannot delete style `"
337                                                << to_utf8(style) << '\'' << endl;
338 //                                      lexrc.printError("Cannot delete style"
339 //                                                       " `$$Token'");
340                         }
341                         break;
342
343                 case TC_COLUMNS:
344                         if (lexrc.next())
345                                 columns_ = lexrc.getInteger();
346                         break;
347
348                 case TC_SIDES:
349                         if (lexrc.next()) {
350                                 switch (lexrc.getInteger()) {
351                                 case 1: sides_ = OneSide; break;
352                                 case 2: sides_ = TwoSides; break;
353                                 default:
354                                         lyxerr << "Impossible number of page"
355                                                 " sides, setting to one."
356                                                << endl;
357                                         sides_ = OneSide;
358                                         break;
359                                 }
360                         }
361                         break;
362
363                 case TC_PAGESTYLE:
364                         lexrc.next();
365                         pagestyle_ = rtrim(lexrc.getString());
366                         break;
367
368                 case TC_DEFAULTFONT:
369                         defaultfont_ = lyxRead(lexrc);
370                         if (!defaultfont_.resolved()) {
371                                 lexrc.printError("Warning: defaultfont should "
372                                                  "be fully instantiated!");
373                                 defaultfont_.realize(sane_font);
374                         }
375                         break;
376
377                 case TC_SECNUMDEPTH:
378                         lexrc.next();
379                         secnumdepth_ = lexrc.getInteger();
380                         break;
381
382                 case TC_TOCDEPTH:
383                         lexrc.next();
384                         tocdepth_ = lexrc.getInteger();
385                         break;
386
387                         // First step to support options
388                 case TC_CLASSOPTIONS:
389                         readClassOptions(lexrc);
390                         break;
391
392                 case TC_PREAMBLE:
393                         preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
394                         break;
395
396                 case TC_PROVIDES: {
397                         lexrc.next();
398                         string const feature = lexrc.getString();
399                         lexrc.next();
400                         if (lexrc.getInteger())
401                                 provides_.insert(feature);
402                         else
403                                 provides_.erase(feature);
404                         break;
405                 }
406
407                 case TC_LEFTMARGIN:     // left margin type
408                         if (lexrc.next())
409                                 leftmargin_ = lexrc.getDocString();
410                         break;
411
412                 case TC_RIGHTMARGIN:    // right margin type
413                         if (lexrc.next())
414                                 rightmargin_ = lexrc.getDocString();
415                         break;
416                 case TC_INSETLAYOUT:
417                         if (lexrc.next()) {
418                                 docstring const name = subst(lexrc.getDocString(), '_', ' ');
419                                 readInsetLayout(lexrc, name);
420                         }
421                         break;
422                 case TC_FLOAT:
423                         readFloat(lexrc);
424                         break;
425                 case TC_COUNTER:
426                         readCounter(lexrc);
427                         break;
428                 case TC_TITLELATEXTYPE:
429                         readTitleType(lexrc);
430                         break;
431                 case TC_TITLELATEXNAME:
432                         if (lexrc.next())
433                                 titlename_ = lexrc.getString();
434                         break;
435                 case TC_NOFLOAT:
436                         if (lexrc.next()) {
437                                 string const nofloat = lexrc.getString();
438                                 floatlist_->erase(nofloat);
439                         }
440                         break;
441                 }
442                 if (format != FORMAT)
443                         break;
444         }
445
446         if (format != FORMAT) {
447                 LYXERR(Debug::TCLASS, "Converting layout file from format "
448                                       << format << " to " << FORMAT);
449                 FileName const tempfile(support::tempName());
450                 error = !layout2layout(filename, tempfile);
451                 if (!error)
452                         error = read(tempfile, rt);
453                 tempfile.removeFile();
454                 return error;
455         }
456
457         if (rt == MODULE) 
458                 LYXERR(Debug::TCLASS, "Finished reading module file "
459                                 << to_utf8(makeDisplayPath(filename.absFilename())));
460         else if (rt == MERGE)
461                 LYXERR(Debug::TCLASS, "Finished reading input file "
462                                 << to_utf8(makeDisplayPath(filename.absFilename())));
463         else { // we are at top level here.
464                 LYXERR(Debug::TCLASS, "Finished reading textclass "
465                                       << to_utf8(makeDisplayPath(filename.absFilename())));
466                 if (defaultlayout_.empty()) {
467                         lyxerr << "Error: Textclass '" << name_
468                                << "' is missing a defaultstyle." << endl;
469                         error = true;
470                 }
471
472                 min_toclevel_ = Layout::NOT_IN_TOC;
473                 max_toclevel_ = Layout::NOT_IN_TOC;
474                 const_iterator cit = begin();
475                 const_iterator the_end = end();
476                 for ( ; cit != the_end ; ++cit) {
477                         int const toclevel = (*cit)->toclevel;
478                         if (toclevel != Layout::NOT_IN_TOC) {
479                                 if (min_toclevel_ == Layout::NOT_IN_TOC)
480                                         min_toclevel_ = toclevel;
481                                 else
482                                         min_toclevel_ = std::min(min_toclevel_,
483                                                          toclevel);
484                                 max_toclevel_ = std::max(max_toclevel_,
485                                                          toclevel);
486                         }
487                 }
488                 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
489                         << ", maximum is " << max_toclevel_);
490
491         }
492
493         return error;
494 }
495
496
497 void TextClass::readTitleType(Lexer & lexrc)
498 {
499         keyword_item titleTypeTags[] = {
500                 { "commandafter", TITLE_COMMAND_AFTER },
501                 { "environment", TITLE_ENVIRONMENT }
502         };
503
504         PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
505
506         int le = lexrc.lex();
507         switch (le) {
508         case Lexer::LEX_UNDEF:
509                 lexrc.printError("Unknown output type `$$Token'");
510                 return;
511         case TITLE_COMMAND_AFTER:
512         case TITLE_ENVIRONMENT:
513                 titletype_ = static_cast<TitleLatexType>(le);
514                 break;
515         default:
516                 lyxerr << "Unhandled value " << le
517                        << " in TextClass::readTitleType." << endl;
518
519                 break;
520         }
521 }
522
523
524 void TextClass::readOutputType(Lexer & lexrc)
525 {
526         keyword_item outputTypeTags[] = {
527                 { "docbook", DOCBOOK },
528                 { "latex", LATEX },
529                 { "literate", LITERATE }
530         };
531
532         PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
533
534         int le = lexrc.lex();
535         switch (le) {
536         case Lexer::LEX_UNDEF:
537                 lexrc.printError("Unknown output type `$$Token'");
538                 return;
539         case LATEX:
540         case DOCBOOK:
541         case LITERATE:
542                 outputType_ = static_cast<OutputType>(le);
543                 break;
544         default:
545                 lyxerr << "Unhandled value " << le
546                        << " in TextClass::readOutputType." << endl;
547
548                 break;
549         }
550 }
551
552
553 enum ClassOptionsTags {
554         CO_FONTSIZE = 1,
555         CO_PAGESTYLE,
556         CO_OTHER,
557         CO_HEADER,
558         CO_END
559 };
560
561
562 void TextClass::readClassOptions(Lexer & lexrc)
563 {
564         keyword_item classOptionsTags[] = {
565                 {"end", CO_END },
566                 {"fontsize", CO_FONTSIZE },
567                 {"header", CO_HEADER },
568                 {"other", CO_OTHER },
569                 {"pagestyle", CO_PAGESTYLE }
570         };
571
572         lexrc.pushTable(classOptionsTags, CO_END);
573         bool getout = false;
574         while (!getout && lexrc.isOK()) {
575                 int le = lexrc.lex();
576                 switch (le) {
577                 case Lexer::LEX_UNDEF:
578                         lexrc.printError("Unknown ClassOption tag `$$Token'");
579                         continue;
580                 default: break;
581                 }
582                 switch (static_cast<ClassOptionsTags>(le)) {
583                 case CO_FONTSIZE:
584                         lexrc.next();
585                         opt_fontsize_ = rtrim(lexrc.getString());
586                         break;
587                 case CO_PAGESTYLE:
588                         lexrc.next();
589                         opt_pagestyle_ = rtrim(lexrc.getString());
590                         break;
591                 case CO_OTHER:
592                         lexrc.next();
593                         options_ = lexrc.getString();
594                         break;
595                 case CO_HEADER:
596                         lexrc.next();
597                         class_header_ = subst(lexrc.getString(), "&quot;", "\"");
598                         break;
599                 case CO_END:
600                         getout = true;
601                         break;
602                 }
603         }
604         lexrc.popTable();
605 }
606
607
608 enum InsetLayoutTags {
609         IL_FONT = 1,
610         IL_BGCOLOR,
611         IL_DECORATION,
612         IL_FREESPACING,
613         IL_FORCELTR,
614         IL_LABELFONT,
615         IL_LABELSTRING,
616         IL_LATEXNAME,
617         IL_LATEXPARAM,
618         IL_LATEXTYPE,
619         IL_LYXTYPE,
620         IL_KEEPEMPTY,
621         IL_MULTIPAR,
622         IL_NEEDPROTECT,
623         IL_PASSTHRU,
624         IL_PREAMBLE,
625         IL_END
626 };
627
628
629 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
630 {
631         keyword_item elementTags[] = {
632                 { "bgcolor", IL_BGCOLOR },
633                 { "decoration", IL_DECORATION },
634                 { "end", IL_END },
635                 { "font", IL_FONT },
636                 { "forceltr", IL_FORCELTR },
637                 { "freespacing", IL_FREESPACING },
638                 { "keepempty", IL_KEEPEMPTY },
639                 { "labelfont", IL_LABELFONT },
640                 { "labelstring", IL_LABELSTRING },
641                 { "latexname", IL_LATEXNAME },
642                 { "latexparam", IL_LATEXPARAM },
643                 { "latextype", IL_LATEXTYPE },
644                 { "lyxtype", IL_LYXTYPE },
645                 { "multipar", IL_MULTIPAR },
646                 { "needprotect", IL_NEEDPROTECT },
647                 { "passthru", IL_PASSTHRU },
648                 { "preamble", IL_PREAMBLE }
649         };
650
651         lexrc.pushTable(elementTags, IL_END);
652
653         string lyxtype;
654         docstring labelstring;
655         string latextype;
656         string decoration;
657         string latexname;
658         string latexparam;
659         FontInfo font = inherit_font;
660         FontInfo labelfont = inherit_font;
661         ColorCode bgcolor(Color_background);
662         string preamble;
663         bool multipar = false;
664         bool passthru = false;
665         bool needprotect = false;
666         bool keepempty = false;
667         bool freespacing = false;
668         bool forceltr = false;
669
670         bool getout = false;
671         while (!getout && lexrc.isOK()) {
672                 int le = lexrc.lex();
673                 switch (le) {
674                 case Lexer::LEX_UNDEF:
675                         lexrc.printError("Unknown ClassOption tag `$$Token'");
676                         continue;
677                 default: break;
678                 }
679                 switch (static_cast<InsetLayoutTags>(le)) {
680                 case IL_LYXTYPE:
681                         lexrc.next();
682                         lyxtype = lexrc.getString();
683                         break;
684                 case IL_LATEXTYPE:
685                         lexrc.next();
686                         latextype = lexrc.getString();
687                         break;
688                 case IL_LABELSTRING:
689                         lexrc.next();
690                         labelstring = lexrc.getDocString();
691                         break;
692                 case IL_DECORATION:
693                         lexrc.next();
694                         decoration = lexrc.getString();
695                         break;
696                 case IL_LATEXNAME:
697                         lexrc.next();
698                         latexname = lexrc.getString();
699                         break;
700                 case IL_LATEXPARAM:
701                         lexrc.next();
702                         latexparam = subst(lexrc.getString(), "&quot;", "\"");
703                         break;
704                 case IL_LABELFONT:
705                         labelfont = lyxRead(lexrc, inherit_font);
706                         break;
707                 case IL_FORCELTR:
708                         lexrc.next();
709                         forceltr = lexrc.getBool();
710                         break;
711                 case IL_MULTIPAR:
712                         lexrc.next();
713                         multipar = lexrc.getBool();
714                         break;
715                 case IL_PASSTHRU:
716                         lexrc.next();
717                         passthru = lexrc.getBool();
718                         break;
719                 case IL_KEEPEMPTY:
720                         lexrc.next();
721                         keepempty = lexrc.getBool();
722                         break;
723                 case IL_FREESPACING:
724                         lexrc.next();
725                         freespacing = lexrc.getBool();
726                         break;
727                 case IL_NEEDPROTECT:
728                         lexrc.next();
729                         needprotect = lexrc.getBool();
730                         break;
731                 case IL_FONT:
732                         font = lyxRead(lexrc, inherit_font);
733                         // So: define font before labelfont
734                         labelfont = font;
735                         break;
736                 case IL_BGCOLOR: {
737                         lexrc.next();
738                         string const token = lexrc.getString();
739                         bgcolor = lcolor.getFromLyXName(token);
740                         break;
741                 }
742                 case IL_PREAMBLE:
743                         preamble = lexrc.getLongString("EndPreamble");
744                         break;
745                 case IL_END:
746                         getout = true;
747                         break;
748                 }
749         }
750
751         // Here add element to list if getout == true
752         if (getout) {
753                 InsetLayout il;
754                 il.name = to_ascii(name);
755                 il.lyxtype = lyxtype;
756                 il.labelstring = labelstring;
757                 il.decoration = decoration;
758                 il.latextype = latextype;
759                 il.latexname = latexname;
760                 il.latexparam = latexparam;
761                 il.multipar = multipar;
762                 il.passthru = passthru;
763                 il.needprotect = needprotect;
764                 il.freespacing = freespacing;
765                 il.forceltr = forceltr;
766                 il.keepempty = keepempty;
767                 il.font = font;
768                 // The label font is generally used as-is without
769                 // any realization against a given context.
770                 labelfont.realize(sane_font);
771                 il.labelfont = labelfont;
772                 il.bgcolor = bgcolor;           
773                 il.preamble = preamble;
774                 insetlayoutlist_[name] = il;
775         }
776
777         lexrc.popTable();
778 }
779
780
781
782 enum FloatTags {
783         FT_TYPE = 1,
784         FT_NAME,
785         FT_PLACEMENT,
786         FT_EXT,
787         FT_WITHIN,
788         FT_STYLE,
789         FT_LISTNAME,
790         FT_BUILTIN,
791         FT_END
792 };
793
794
795 void TextClass::readFloat(Lexer & lexrc)
796 {
797         keyword_item floatTags[] = {
798                 { "end", FT_END },
799                 { "extension", FT_EXT },
800                 { "guiname", FT_NAME },
801                 { "latexbuiltin", FT_BUILTIN },
802                 { "listname", FT_LISTNAME },
803                 { "numberwithin", FT_WITHIN },
804                 { "placement", FT_PLACEMENT },
805                 { "style", FT_STYLE },
806                 { "type", FT_TYPE }
807         };
808
809         lexrc.pushTable(floatTags, FT_END);
810
811         string type;
812         string placement;
813         string ext;
814         string within;
815         string style;
816         string name;
817         string listName;
818         bool builtin = false;
819
820         bool getout = false;
821         while (!getout && lexrc.isOK()) {
822                 int le = lexrc.lex();
823                 switch (le) {
824                 case Lexer::LEX_UNDEF:
825                         lexrc.printError("Unknown ClassOption tag `$$Token'");
826                         continue;
827                 default: break;
828                 }
829                 switch (static_cast<FloatTags>(le)) {
830                 case FT_TYPE:
831                         lexrc.next();
832                         type = lexrc.getString();
833                         if (floatlist_->typeExist(type)) {
834                                 Floating const & fl = floatlist_->getType(type);
835                                 placement = fl.placement();
836                                 ext = fl.ext();
837                                 within = fl.within();
838                                 style = fl.style();
839                                 name = fl.name();
840                                 listName = fl.listName();
841                                 builtin = fl.builtin();
842                         } 
843                         break;
844                 case FT_NAME:
845                         lexrc.next();
846                         name = lexrc.getString();
847                         break;
848                 case FT_PLACEMENT:
849                         lexrc.next();
850                         placement = lexrc.getString();
851                         break;
852                 case FT_EXT:
853                         lexrc.next();
854                         ext = lexrc.getString();
855                         break;
856                 case FT_WITHIN:
857                         lexrc.next();
858                         within = lexrc.getString();
859                         if (within == "none")
860                                 within.erase();
861                         break;
862                 case FT_STYLE:
863                         lexrc.next();
864                         style = lexrc.getString();
865                         break;
866                 case FT_LISTNAME:
867                         lexrc.next();
868                         listName = lexrc.getString();
869                         break;
870                 case FT_BUILTIN:
871                         lexrc.next();
872                         builtin = lexrc.getBool();
873                         break;
874                 case FT_END:
875                         getout = true;
876                         break;
877                 }
878         }
879
880         // Here if have a full float if getout == true
881         if (getout) {
882                 Floating fl(type, placement, ext, within,
883                             style, name, listName, builtin);
884                 floatlist_->newFloat(fl);
885                 // each float has its own counter
886                 counters_->newCounter(from_ascii(type), from_ascii(within), 
887                                       docstring(), docstring());
888         }
889
890         lexrc.popTable();
891 }
892
893
894 enum CounterTags {
895         CT_NAME = 1,
896         CT_WITHIN,
897         CT_LABELSTRING,
898         CT_LABELSTRING_APPENDIX,
899         CT_END
900 };
901
902
903 void TextClass::readCounter(Lexer & lexrc)
904 {
905         keyword_item counterTags[] = {
906                 { "end", CT_END },
907                 { "labelstring", CT_LABELSTRING },
908                 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
909                 { "name", CT_NAME },
910                 { "within", CT_WITHIN }
911         };
912
913         lexrc.pushTable(counterTags, CT_END);
914
915         docstring name;
916         docstring within;
917         docstring labelstring;
918         docstring labelstring_appendix;
919
920         bool getout = false;
921         while (!getout && lexrc.isOK()) {
922                 int le = lexrc.lex();
923                 switch (le) {
924                 case Lexer::LEX_UNDEF:
925                         lexrc.printError("Unknown ClassOption tag `$$Token'");
926                         continue;
927                 default: break;
928                 }
929                 switch (static_cast<CounterTags>(le)) {
930                 case CT_NAME:
931                         lexrc.next();
932                         name = lexrc.getDocString();
933                         if (counters_->hasCounter(name))
934                                 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
935                         else
936                                 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
937                         break;
938                 case CT_WITHIN:
939                         lexrc.next();
940                         within = lexrc.getDocString();
941                         if (within == "none")
942                                 within.erase();
943                         break;
944                 case CT_LABELSTRING:
945                         lexrc.next();
946                         labelstring = lexrc.getDocString();
947                         labelstring_appendix = labelstring;
948                         break;
949                 case CT_LABELSTRING_APPENDIX:
950                         lexrc.next();
951                         labelstring_appendix = lexrc.getDocString();
952                         break;
953                 case CT_END:
954                         getout = true;
955                         break;
956                 }
957         }
958
959         // Here if have a full counter if getout == true
960         if (getout)
961                 counters_->newCounter(name, within, 
962                                       labelstring, labelstring_appendix);
963
964         lexrc.popTable();
965 }
966
967
968 FontInfo const & TextClass::defaultfont() const
969 {
970         return defaultfont_;
971 }
972
973
974 docstring const & TextClass::leftmargin() const
975 {
976         return leftmargin_;
977 }
978
979
980 docstring const & TextClass::rightmargin() const
981 {
982         return rightmargin_;
983 }
984
985
986 bool TextClass::hasLayout(docstring const & n) const
987 {
988         docstring const name = n.empty() ? defaultLayoutName() : n;
989
990         return find_if(layoutlist_.begin(), layoutlist_.end(),
991                        LayoutNamesEqual(name))
992                 != layoutlist_.end();
993 }
994
995
996
997 LayoutPtr const & TextClass::operator[](docstring const & name) const
998 {
999         BOOST_ASSERT(!name.empty());
1000
1001         LayoutList::const_iterator cit =
1002                 find_if(layoutlist_.begin(),
1003                         layoutlist_.end(),
1004                         LayoutNamesEqual(name));
1005
1006         if (cit == layoutlist_.end()) {
1007                 lyxerr << "We failed to find the layout '" << to_utf8(name)
1008                        << "' in the layout list. You MUST investigate!"
1009                        << endl;
1010                 for (LayoutList::const_iterator it = layoutlist_.begin();
1011                          it != layoutlist_.end(); ++it)
1012                         lyxerr  << " " << to_utf8(it->get()->name()) << endl;
1013
1014                 // we require the name to exist
1015                 BOOST_ASSERT(false);
1016         }
1017
1018         return *cit;
1019 }
1020
1021
1022 bool TextClass::deleteLayout(docstring const & name)
1023 {
1024         if (name == defaultLayoutName())
1025                 return false;
1026
1027         LayoutList::iterator it =
1028                 remove_if(layoutlist_.begin(), layoutlist_.end(),
1029                           LayoutNamesEqual(name));
1030
1031         LayoutList::iterator end = layoutlist_.end();
1032         bool const ret = (it != end);
1033         layoutlist_.erase(it, end);
1034         return ret;
1035 }
1036
1037
1038 // Load textclass info if not loaded yet
1039 bool TextClass::load(string const & path) const
1040 {
1041         if (loaded_)
1042                 return true;
1043
1044         // Read style-file, provided path is searched before the system ones
1045         FileName layout_file;
1046         if (!path.empty())
1047                 layout_file = FileName(addName(path, name_ + ".layout"));
1048         if (layout_file.empty() || !layout_file.exists())
1049                 layout_file = libFileSearch("layouts", name_, "layout");
1050         loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1051
1052         if (!loaded_) {
1053                 lyxerr << "Error reading `"
1054                        << to_utf8(makeDisplayPath(layout_file.absFilename()))
1055                        << "'\n(Check `" << name_
1056                        << "')\nCheck your installation and "
1057                         "try Options/Reconfigure..." << endl;
1058         }
1059
1060         return loaded_;
1061 }
1062
1063
1064 FloatList & TextClass::floats()
1065 {
1066         return *floatlist_.get();
1067 }
1068
1069
1070 FloatList const & TextClass::floats() const
1071 {
1072         return *floatlist_.get();
1073 }
1074
1075
1076 Counters & TextClass::counters() const
1077 {
1078         return *counters_.get();
1079 }
1080
1081
1082 // Return the layout object of an inset given by name. If the name
1083 // is not found as such, the part after the ':' is stripped off, and
1084 // searched again. In this way, an error fallback can be provided:
1085 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1086 // will invoke the layout object defined by name = 'CharStyle'.
1087 // If that doesn't work either, an empty object returns (shouldn't
1088 // happen).  -- Idea JMarc, comment MV
1089 InsetLayout const & TextClass::insetlayout(docstring const & name) const 
1090 {
1091         docstring n = name;
1092         while (!n.empty()) {
1093                 if (insetlayoutlist_.count(n) > 0)
1094                         return insetlayoutlist_[n];
1095                 docstring::size_type i = n.find(':');
1096                 if (i == string::npos)
1097                         break;
1098                 n = n.substr(0,i);
1099         }
1100         static InsetLayout empty;
1101         empty.labelstring = from_utf8("UNDEFINED");
1102         empty.labelfont = sane_font;
1103         empty.labelfont.setColor(Color_error);
1104         empty.bgcolor = Color_error;
1105         return empty;
1106 }
1107
1108
1109 docstring const & TextClass::defaultLayoutName() const
1110 {
1111         // This really should come from the actual layout... (Lgb)
1112         return defaultlayout_;
1113 }
1114
1115
1116 LayoutPtr const & TextClass::defaultLayout() const
1117 {
1118         return operator[](defaultLayoutName());
1119 }
1120
1121
1122 string const & TextClass::name() const
1123 {
1124         return name_;
1125 }
1126
1127
1128 string const & TextClass::latexname() const
1129 {
1130         const_cast<TextClass*>(this)->load();
1131         return latexname_;
1132 }
1133
1134
1135 string const & TextClass::description() const
1136 {
1137         return description_;
1138 }
1139
1140
1141 string const & TextClass::opt_fontsize() const
1142 {
1143         return opt_fontsize_;
1144 }
1145
1146
1147 string const & TextClass::opt_pagestyle() const
1148 {
1149         return opt_pagestyle_;
1150 }
1151
1152
1153 string const & TextClass::options() const
1154 {
1155         return options_;
1156 }
1157
1158
1159 string const & TextClass::class_header() const
1160 {
1161         return class_header_;
1162 }
1163
1164
1165 string const & TextClass::pagestyle() const
1166 {
1167         return pagestyle_;
1168 }
1169
1170
1171 docstring const & TextClass::preamble() const
1172 {
1173         return preamble_;
1174 }
1175
1176
1177 PageSides TextClass::sides() const
1178 {
1179         return sides_;
1180 }
1181
1182
1183 int TextClass::secnumdepth() const
1184 {
1185         return secnumdepth_;
1186 }
1187
1188
1189 int TextClass::tocdepth() const
1190 {
1191         return tocdepth_;
1192 }
1193
1194
1195 OutputType TextClass::outputType() const
1196 {
1197         return outputType_;
1198 }
1199
1200
1201 bool TextClass::provides(string const & p) const
1202 {
1203         return provides_.find(p) != provides_.end();
1204 }
1205
1206
1207 unsigned int TextClass::columns() const
1208 {
1209         return columns_;
1210 }
1211
1212
1213 TitleLatexType TextClass::titletype() const
1214 {
1215         return titletype_;
1216 }
1217
1218
1219 string const & TextClass::titlename() const
1220 {
1221         return titlename_;
1222 }
1223
1224
1225 int TextClass::size() const
1226 {
1227         return layoutlist_.size();
1228 }
1229
1230
1231 int TextClass::min_toclevel() const
1232 {
1233         return min_toclevel_;
1234 }
1235
1236
1237 int TextClass::max_toclevel() const
1238 {
1239         return max_toclevel_;
1240 }
1241
1242
1243 bool TextClass::hasTocLevels() const
1244 {
1245         return min_toclevel_ != Layout::NOT_IN_TOC;
1246 }
1247
1248
1249 ostream & operator<<(ostream & os, PageSides p)
1250 {
1251         switch (p) {
1252         case OneSide:
1253                 os << '1';
1254                 break;
1255         case TwoSides:
1256                 os << '2';
1257                 break;
1258         }
1259         return os;
1260 }
1261
1262
1263 } // namespace lyx