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