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