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