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