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