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