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