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