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