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