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