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