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