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