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