]> git.lyx.org Git - lyx.git/blob - src/TextClass.cpp
saner borderline between buffer and exporter
[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_END
622 };
623
624
625 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
626 {
627         keyword_item elementTags[] = {
628                 { "bgcolor", IL_BGCOLOR },
629                 { "decoration", IL_DECORATION },
630                 { "end", IL_END },
631                 { "font", IL_FONT },
632                 { "labelfont", IL_LABELFONT },
633                 { "labelstring", IL_LABELSTRING },
634                 { "latexname", IL_LATEXNAME },
635                 { "latexparam", IL_LATEXPARAM },
636                 { "latextype", IL_LATEXTYPE },
637                 { "lyxtype", IL_LYXTYPE },
638                 { "multipar", IL_MULTIPAR },
639                 { "preamble", IL_PREAMBLE }
640         };
641
642         lexrc.pushTable(elementTags, IL_END);
643
644         string lyxtype;
645         docstring labelstring;
646         string latextype;
647         string decoration;
648         string latexname;
649         string latexparam;
650         Font font(defaultfont());
651         Font labelfont(defaultfont());
652         Color::color bgcolor(Color::background);
653         string preamble;
654         bool multipar(false);
655
656         bool getout = false;
657         while (!getout && lexrc.isOK()) {
658                 int le = lexrc.lex();
659                 switch (le) {
660                 case Lexer::LEX_UNDEF:
661                         lexrc.printError("Unknown ClassOption tag `$$Token'");
662                         continue;
663                 default: break;
664                 }
665                 switch (static_cast<InsetLayoutTags>(le)) {
666                 case IL_LYXTYPE:
667                         lexrc.next();
668                         lyxtype = lexrc.getString();
669                         break;
670                 case IL_LATEXTYPE:
671                         lexrc.next();
672                         latextype = lexrc.getString();
673                         break;
674                 case IL_LABELSTRING:
675                         lexrc.next();
676                         labelstring = lexrc.getDocString();
677                         break;
678                 case IL_DECORATION:
679                         lexrc.next();
680                         decoration = lexrc.getString();
681                         break;
682                 case IL_LATEXNAME:
683                         lexrc.next();
684                         latexname = lexrc.getString();
685                         break;
686                 case IL_LATEXPARAM:
687                         lexrc.next();
688                         latexparam = subst(lexrc.getString(), "&quot;", "\"");
689                         break;
690                 case IL_LABELFONT:
691                         labelfont.lyxRead(lexrc);
692                         labelfont.realize(defaultfont());
693                         break;
694                 case IL_MULTIPAR:
695                         lexrc.next();
696                         multipar = lexrc.getBool();
697                         break;
698                 case IL_FONT:
699                         font.lyxRead(lexrc);
700                         font.realize(defaultfont());
701                         // So: define font before labelfont
702                         labelfont = font;
703                         break;
704                 case IL_BGCOLOR: {
705                         lexrc.next();
706                         string const token = lexrc.getString();
707                         bgcolor = lcolor.getFromLyXName(token);
708                         break;
709                 }
710                 case IL_PREAMBLE:
711                         preamble = lexrc.getLongString("EndPreamble");
712                         break;
713                 case IL_END:
714                         getout = true;
715                         break;
716                 }
717         }
718
719         //
720         // Here add element to list if getout == true
721         if (getout) {
722                 InsetLayout il;
723                 il.name = to_ascii(name);
724                 il.lyxtype = lyxtype;
725                 il.labelstring = labelstring;
726                 il.decoration = decoration;
727                 il.latextype = latextype;
728                 il.latexname = latexname;
729                 il.latexparam = latexparam;
730                 il.multipar = multipar;
731                 il.font = font;
732                 il.labelfont = labelfont;
733                 il.bgcolor = bgcolor;           
734                 il.preamble = preamble;
735                 insetlayoutlist_[name] = il;
736         }
737
738         lexrc.popTable();
739 }
740
741
742
743 enum FloatTags {
744         FT_TYPE = 1,
745         FT_NAME,
746         FT_PLACEMENT,
747         FT_EXT,
748         FT_WITHIN,
749         FT_STYLE,
750         FT_LISTNAME,
751         FT_BUILTIN,
752         FT_END
753 };
754
755
756 void TextClass::readFloat(Lexer & lexrc)
757 {
758         keyword_item floatTags[] = {
759                 { "end", FT_END },
760                 { "extension", FT_EXT },
761                 { "guiname", FT_NAME },
762                 { "latexbuiltin", FT_BUILTIN },
763                 { "listname", FT_LISTNAME },
764                 { "numberwithin", FT_WITHIN },
765                 { "placement", FT_PLACEMENT },
766                 { "style", FT_STYLE },
767                 { "type", FT_TYPE }
768         };
769
770         lexrc.pushTable(floatTags, FT_END);
771
772         string type;
773         string placement;
774         string ext;
775         string within;
776         string style;
777         string name;
778         string listName;
779         bool builtin = false;
780
781         bool getout = false;
782         while (!getout && lexrc.isOK()) {
783                 int le = lexrc.lex();
784                 switch (le) {
785                 case Lexer::LEX_UNDEF:
786                         lexrc.printError("Unknown ClassOption tag `$$Token'");
787                         continue;
788                 default: break;
789                 }
790                 switch (static_cast<FloatTags>(le)) {
791                 case FT_TYPE:
792                         lexrc.next();
793                         type = lexrc.getString();
794                         if (floatlist_->typeExist(type)) {
795                                 Floating const & fl = floatlist_->getType(type);
796                                 placement = fl.placement();
797                                 ext = fl.ext();
798                                 within = fl.within();
799                                 style = fl.style();
800                                 name = fl.name();
801                                 listName = fl.listName();
802                                 builtin = fl.builtin();
803                         } 
804                         break;
805                 case FT_NAME:
806                         lexrc.next();
807                         name = lexrc.getString();
808                         break;
809                 case FT_PLACEMENT:
810                         lexrc.next();
811                         placement = lexrc.getString();
812                         break;
813                 case FT_EXT:
814                         lexrc.next();
815                         ext = lexrc.getString();
816                         break;
817                 case FT_WITHIN:
818                         lexrc.next();
819                         within = lexrc.getString();
820                         if (within == "none")
821                                 within.erase();
822                         break;
823                 case FT_STYLE:
824                         lexrc.next();
825                         style = lexrc.getString();
826                         break;
827                 case FT_LISTNAME:
828                         lexrc.next();
829                         listName = lexrc.getString();
830                         break;
831                 case FT_BUILTIN:
832                         lexrc.next();
833                         builtin = lexrc.getBool();
834                         break;
835                 case FT_END:
836                         getout = true;
837                         break;
838                 }
839         }
840
841         // Here if have a full float if getout == true
842         if (getout) {
843                 Floating fl(type, placement, ext, within,
844                             style, name, listName, builtin);
845                 floatlist_->newFloat(fl);
846                 // each float has its own counter
847                 counters_->newCounter(from_ascii(type), from_ascii(within), 
848                                       docstring(), docstring());
849         }
850
851         lexrc.popTable();
852 }
853
854
855 enum CounterTags {
856         CT_NAME = 1,
857         CT_WITHIN,
858         CT_LABELSTRING,
859         CT_LABELSTRING_APPENDIX,
860         CT_END
861 };
862
863 void TextClass::readCounter(Lexer & lexrc)
864 {
865         keyword_item counterTags[] = {
866                 { "end", CT_END },
867                 { "labelstring", CT_LABELSTRING },
868                 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
869                 { "name", CT_NAME },
870                 { "within", CT_WITHIN }
871         };
872
873         lexrc.pushTable(counterTags, CT_END);
874
875         docstring name;
876         docstring within;
877         docstring labelstring;
878         docstring labelstring_appendix;
879
880         bool getout = false;
881         while (!getout && lexrc.isOK()) {
882                 int le = lexrc.lex();
883                 switch (le) {
884                 case Lexer::LEX_UNDEF:
885                         lexrc.printError("Unknown ClassOption tag `$$Token'");
886                         continue;
887                 default: break;
888                 }
889                 switch (static_cast<CounterTags>(le)) {
890                 case CT_NAME:
891                         lexrc.next();
892                         name = lexrc.getDocString();
893                         if (counters_->hasCounter(name))
894                                 LYXERR(Debug::TCLASS) 
895                                         << "Reading existing counter " 
896                                         << to_utf8(name) << endl;
897                         else
898                                 LYXERR(Debug::TCLASS) 
899                                         << "Reading new counter " 
900                                         << to_utf8(name) << endl;
901                         break;
902                 case CT_WITHIN:
903                         lexrc.next();
904                         within = lexrc.getDocString();
905                         if (within == "none")
906                                 within.erase();
907                         break;
908                 case CT_LABELSTRING:
909                         lexrc.next();
910                         labelstring = lexrc.getDocString();
911                         labelstring_appendix = labelstring;
912                         break;
913                 case CT_LABELSTRING_APPENDIX:
914                         lexrc.next();
915                         labelstring_appendix = lexrc.getDocString();
916                         break;
917                 case CT_END:
918                         getout = true;
919                         break;
920                 }
921         }
922
923         // Here if have a full counter if getout == true
924         if (getout)
925                 counters_->newCounter(name, within, 
926                                       labelstring, labelstring_appendix);
927
928         lexrc.popTable();
929 }
930
931
932 Font const & TextClass::defaultfont() const
933 {
934         return defaultfont_;
935 }
936
937
938 docstring const & TextClass::leftmargin() const
939 {
940         return leftmargin_;
941 }
942
943
944 docstring const & TextClass::rightmargin() const
945 {
946         return rightmargin_;
947 }
948
949
950 bool TextClass::hasLayout(docstring const & n) const
951 {
952         docstring const name = n.empty() ? defaultLayoutName() : n;
953
954         return find_if(layoutlist_.begin(), layoutlist_.end(),
955                        LayoutNamesEqual(name))
956                 != layoutlist_.end();
957 }
958
959
960
961 LayoutPtr const & TextClass::operator[](docstring const & name) const
962 {
963         BOOST_ASSERT(!name.empty());
964
965         LayoutList::const_iterator cit =
966                 find_if(layoutlist_.begin(),
967                         layoutlist_.end(),
968                         LayoutNamesEqual(name));
969
970         if (cit == layoutlist_.end()) {
971                 lyxerr << "We failed to find the layout '" << to_utf8(name)
972                        << "' in the layout list. You MUST investigate!"
973                        << endl;
974                 for (LayoutList::const_iterator it = layoutlist_.begin();
975                          it != layoutlist_.end(); ++it)
976                         lyxerr  << " " << to_utf8(it->get()->name()) << endl;
977
978                 // we require the name to exist
979                 BOOST_ASSERT(false);
980         }
981
982         return (*cit);
983 }
984
985
986
987 bool TextClass::delete_layout(docstring const & name)
988 {
989         if (name == defaultLayoutName())
990                 return false;
991
992         LayoutList::iterator it =
993                 remove_if(layoutlist_.begin(), layoutlist_.end(),
994                           LayoutNamesEqual(name));
995
996         LayoutList::iterator end = layoutlist_.end();
997         bool const ret = (it != end);
998         layoutlist_.erase(it, end);
999         return ret;
1000 }
1001
1002
1003 // Load textclass info if not loaded yet
1004 bool TextClass::load(string const & path) const
1005 {
1006         if (loaded_)
1007                 return true;
1008
1009         // Read style-file, provided path is searched before the system ones
1010         FileName layout_file;
1011         if (!path.empty())
1012                 layout_file = FileName(addName(path, name_ + ".layout"));
1013         if (layout_file.empty() || !layout_file.exists())
1014                 layout_file = libFileSearch("layouts", name_, "layout");
1015         loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1016
1017         if (!loaded_) {
1018                 lyxerr << "Error reading `"
1019                        << to_utf8(makeDisplayPath(layout_file.absFilename()))
1020                        << "'\n(Check `" << name_
1021                        << "')\nCheck your installation and "
1022                         "try Options/Reconfigure..." << endl;
1023         }
1024
1025         return loaded_;
1026 }
1027
1028
1029 FloatList & TextClass::floats()
1030 {
1031         return *floatlist_.get();
1032 }
1033
1034
1035 FloatList const & TextClass::floats() const
1036 {
1037         return *floatlist_.get();
1038 }
1039
1040
1041 Counters & TextClass::counters() const
1042 {
1043         return *counters_.get();
1044 }
1045
1046
1047 // Return the layout object of an inset given by name. If the name
1048 // is not found as such, the part after the ':' is stripped off, and
1049 // searched again. In this way, an error fallback can be provided:
1050 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1051 // will invoke the layout object defined by name = 'CharStyle'.
1052 // If that doesn't work either, an empty object returns (shouldn't
1053 // happen).  -- Idea JMarc, comment MV
1054 InsetLayout const & TextClass::insetlayout(docstring const & name) const 
1055 {
1056         docstring n = name;
1057         while (!n.empty()) {
1058                 if (insetlayoutlist_.count(n) > 0)
1059                         return insetlayoutlist_[n];
1060                 docstring::size_type i = n.find(':');
1061                 if (i == string::npos)
1062                         break;
1063                 n = n.substr(0,i);
1064         }
1065         static InsetLayout empty;
1066         empty.labelstring = from_utf8("UNDEFINED");
1067         empty.bgcolor = Color::error;
1068         return empty;
1069 }
1070
1071
1072 docstring const & TextClass::defaultLayoutName() const
1073 {
1074         // This really should come from the actual layout... (Lgb)
1075         return defaultlayout_;
1076 }
1077
1078
1079 LayoutPtr const & TextClass::defaultLayout() const
1080 {
1081         return operator[](defaultLayoutName());
1082 }
1083
1084
1085 string const & TextClass::name() const
1086 {
1087         return name_;
1088 }
1089
1090
1091 string const & TextClass::latexname() const
1092 {
1093         const_cast<TextClass*>(this)->load();
1094         return latexname_;
1095 }
1096
1097
1098 string const & TextClass::description() const
1099 {
1100         return description_;
1101 }
1102
1103
1104 string const & TextClass::opt_fontsize() const
1105 {
1106         return opt_fontsize_;
1107 }
1108
1109
1110 string const & TextClass::opt_pagestyle() const
1111 {
1112         return opt_pagestyle_;
1113 }
1114
1115
1116 string const & TextClass::options() const
1117 {
1118         return options_;
1119 }
1120
1121
1122 string const & TextClass::class_header() const
1123 {
1124         return class_header_;
1125 }
1126
1127
1128 string const & TextClass::pagestyle() const
1129 {
1130         return pagestyle_;
1131 }
1132
1133
1134 docstring const & TextClass::preamble() const
1135 {
1136         return preamble_;
1137 }
1138
1139
1140 TextClass::PageSides TextClass::sides() const
1141 {
1142         return sides_;
1143 }
1144
1145
1146 int TextClass::secnumdepth() const
1147 {
1148         return secnumdepth_;
1149 }
1150
1151
1152 int TextClass::tocdepth() const
1153 {
1154         return tocdepth_;
1155 }
1156
1157
1158 OutputType TextClass::outputType() const
1159 {
1160         return outputType_;
1161 }
1162
1163
1164 bool TextClass::provides(string const & p) const
1165 {
1166         return provides_.find(p) != provides_.end();
1167 }
1168
1169
1170 unsigned int TextClass::columns() const
1171 {
1172         return columns_;
1173 }
1174
1175
1176 TitleLatexType TextClass::titletype() const
1177 {
1178         return titletype_;
1179 }
1180
1181
1182 string const & TextClass::titlename() const
1183 {
1184         return titlename_;
1185 }
1186
1187
1188 int TextClass::size() const
1189 {
1190         return layoutlist_.size();
1191 }
1192
1193
1194 int TextClass::min_toclevel() const
1195 {
1196         return min_toclevel_;
1197 }
1198
1199
1200 int TextClass::max_toclevel() const
1201 {
1202         return max_toclevel_;
1203 }
1204
1205
1206 bool TextClass::hasTocLevels() const
1207 {
1208         return min_toclevel_ != Layout::NOT_IN_TOC;
1209 }
1210
1211
1212 ostream & operator<<(ostream & os, TextClass::PageSides p)
1213 {
1214         switch (p) {
1215         case TextClass::OneSide:
1216                 os << '1';
1217                 break;
1218         case TextClass::TwoSides:
1219                 os << '2';
1220                 break;
1221         }
1222         return os;
1223 }
1224
1225
1226 } // namespace lyx