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