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