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