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