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