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