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