]> git.lyx.org Git - lyx.git/blob - src/lyxtextclass.C
6a5d6fc688905b737601c43169ccc504d8e5cae5
[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_PREAMBLE,
529         CS_END
530 };
531
532
533 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
534 {
535         keyword_item elementTags[] = {
536                 { "end", CS_END },
537                 { "font", CS_FONT },
538                 { "labelfont", CS_LABELFONT },
539                 { "latexname", CS_LATEXNAME },
540                 { "latextype", CS_LATEXTYPE },
541                 { "preamble", CS_PREAMBLE}
542         };
543
544         lexrc.pushTable(elementTags, CS_END);
545
546         string latextype;
547         string latexname;
548         LyXFont font(LyXFont::ALL_INHERIT);
549         LyXFont labelfont(LyXFont::ALL_INHERIT);
550         string preamble;
551         
552         bool getout = false;
553         while (!getout && lexrc.isOK()) {
554                 int le = lexrc.lex();
555                 switch (le) {
556                 case LyXLex::LEX_UNDEF:
557                         lexrc.printError("Unknown ClassOption tag `$$Token'");
558                         continue;
559                 default: break;
560                 }
561                 switch (static_cast<CharStyleTags>(le)) {
562                 case CS_LATEXTYPE:
563                         lexrc.next();
564                         latextype = lexrc.getString();
565                         break;
566                 case CS_LATEXNAME:
567                         lexrc.next();
568                         latexname = lexrc.getString();
569                         break;
570                 case CS_LABELFONT:
571                         labelfont.lyxRead(lexrc);
572                         break;
573                 case CS_FONT:
574                         font.lyxRead(lexrc);
575                         labelfont = font;
576                         break;
577                 case CS_PREAMBLE:
578                         preamble = lexrc.getLongString("EndPreamble");
579                         break;
580                 case CS_END:
581                         getout = true;
582                         break;
583                 }
584         }
585
586         //
587         // Here add element to list if getout == true
588         if (getout) {
589                 CharStyle cs;
590                 cs.name = name;
591                 cs.latextype = latextype;
592                 cs.latexname = latexname;
593                 cs.font = font;
594                 cs.labelfont = labelfont;
595                 cs.preamble = preamble;
596                 charstyles().push_back(cs);
597         }
598
599         lexrc.popTable();
600 }
601
602
603 enum FloatTags {
604         FT_TYPE = 1,
605         FT_NAME,
606         FT_PLACEMENT,
607         FT_EXT,
608         FT_WITHIN,
609         FT_STYLE,
610         FT_LISTNAME,
611         FT_BUILTIN,
612         FT_END
613 };
614
615
616 void LyXTextClass::readFloat(LyXLex & lexrc)
617 {
618         keyword_item floatTags[] = {
619                 { "end", FT_END },
620                 { "extension", FT_EXT },
621                 { "guiname", FT_NAME },
622                 { "latexbuiltin", FT_BUILTIN },
623                 { "listname", FT_LISTNAME },
624                 { "numberwithin", FT_WITHIN },
625                 { "placement", FT_PLACEMENT },
626                 { "style", FT_STYLE },
627                 { "type", FT_TYPE }
628         };
629
630         lexrc.pushTable(floatTags, FT_END);
631
632         string type;
633         string placement;
634         string ext;
635         string within;
636         string style;
637         string name;
638         string listname;
639         bool builtin = false;
640
641         bool getout = false;
642         while (!getout && lexrc.isOK()) {
643                 int le = lexrc.lex();
644                 switch (le) {
645                 case LyXLex::LEX_UNDEF:
646                         lexrc.printError("Unknown ClassOption tag `$$Token'");
647                         continue;
648                 default: break;
649                 }
650                 switch (static_cast<FloatTags>(le)) {
651                 case FT_TYPE:
652                         lexrc.next();
653                         type = lexrc.getString();
654                         // Here we could check if this type is already defined
655                         // and modify it with the rest of the vars instead.
656                         break;
657                 case FT_NAME:
658                         lexrc.next();
659                         name = lexrc.getString();
660                         break;
661                 case FT_PLACEMENT:
662                         lexrc.next();
663                         placement = lexrc.getString();
664                         break;
665                 case FT_EXT:
666                         lexrc.next();
667                         ext = lexrc.getString();
668                         break;
669                 case FT_WITHIN:
670                         lexrc.next();
671                         within = lexrc.getString();
672                         if (within == "none")
673                                 within.erase();
674                         break;
675                 case FT_STYLE:
676                         lexrc.next();
677                         style = lexrc.getString();
678                         break;
679                 case FT_LISTNAME:
680                         lexrc.next();
681                         listname = lexrc.getString();
682                         break;
683                 case FT_BUILTIN:
684                         lexrc.next();
685                         builtin = lexrc.getBool();
686                         break;
687                 case FT_END:
688                         getout = true;
689                         break;
690                 }
691         }
692
693         // Here if have a full float if getout == true
694         if (getout) {
695                 Floating newfloat(type, placement, ext, within,
696                                   style, name, listname, builtin);
697                 floatlist_->newFloat(newfloat);
698         }
699
700         lexrc.popTable();
701 }
702
703
704 enum CounterTags {
705         CT_NAME = 1,
706         CT_WITHIN,
707         CT_END
708 };
709
710 void LyXTextClass::readCounter(LyXLex & lexrc)
711 {
712         keyword_item counterTags[] = {
713                 { "end", CT_END },
714                 { "name", CT_NAME },
715                 { "within", CT_WITHIN }
716         };
717
718         lexrc.pushTable(counterTags, CT_END);
719
720         string name;
721         string within;
722
723         bool getout = false;
724         while (!getout && lexrc.isOK()) {
725                 int le = lexrc.lex();
726                 switch (le) {
727                 case LyXLex::LEX_UNDEF:
728                         lexrc.printError("Unknown ClassOption tag `$$Token'");
729                         continue;
730                 default: break;
731                 }
732                 switch (static_cast<CounterTags>(le)) {
733                 case CT_NAME:
734                         lexrc.next();
735                         name = lexrc.getString();
736                         break;
737                 case CT_WITHIN:
738                         lexrc.next();
739                         within = lexrc.getString();
740                         if (within == "none")
741                                 within.erase();
742                         break;
743                 case CT_END:
744                         getout = true;
745                         break;
746                 }
747         }
748
749         // Here if have a full counter if getout == true
750         if (getout) {
751                 if (within.empty()) {
752                         ctrs_->newCounter(name);
753                 } else {
754                         ctrs_->newCounter(name, within);
755                 }
756         }
757
758         lexrc.popTable();
759 }
760
761
762 LyXFont const & LyXTextClass::defaultfont() const
763 {
764         return defaultfont_;
765 }
766
767
768 string const & LyXTextClass::leftmargin() const
769 {
770         return leftmargin_;
771 }
772
773
774 string const & LyXTextClass::rightmargin() const
775 {
776         return rightmargin_;
777 }
778
779
780 bool LyXTextClass::hasLayout(string const & n) const
781 {
782         string const name = (n.empty() ? defaultLayoutName() : n);
783
784         return find_if(layoutlist_.begin(), layoutlist_.end(),
785                        compare_name(name))
786                 != layoutlist_.end();
787 }
788
789
790
791 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
792 {
793         BOOST_ASSERT(!name.empty());
794
795         LayoutList::const_iterator cit =
796                 find_if(layoutlist_.begin(),
797                         layoutlist_.end(),
798                         compare_name(name));
799
800         if (cit == layoutlist_.end()) {
801                 lyxerr << "We failed to find the layout '" << name
802                        << "' in the layout list. You MUST investigate!"
803                        << endl;
804                 for (LayoutList::const_iterator it = layoutlist_.begin();
805                          it != layoutlist_.end(); ++it)
806                         lyxerr  << " " << it->get()->name() << endl;
807
808                 // we require the name to exist
809                 BOOST_ASSERT(false);
810         }
811
812         return (*cit);
813 }
814
815
816
817 bool LyXTextClass::delete_layout(string const & name)
818 {
819         if (name == defaultLayoutName())
820                 return false;
821
822         LayoutList::iterator it =
823                 remove_if(layoutlist_.begin(), layoutlist_.end(),
824                           compare_name(name));
825
826         LayoutList::iterator end = layoutlist_.end();
827         bool const ret = (it != end);
828         layoutlist_.erase(it, end);
829         return ret;
830 }
831
832
833 // Load textclass info if not loaded yet
834 bool LyXTextClass::load() const
835 {
836         if (loaded)
837                 return true;
838
839         // Read style-file
840         string const real_file = LibFileSearch("layouts", name_, "layout");
841
842         if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
843                 lyxerr << "Error reading `"
844                        << MakeDisplayPath(real_file)
845                        << "'\n(Check `" << name_
846                        << "')\nCheck your installation and "
847                         "try Options/Reconfigure..." << endl;
848                 loaded = false;
849         }
850         loaded = true;
851         return loaded;
852 }
853
854
855 FloatList & LyXTextClass::floats()
856 {
857         return *floatlist_.get();
858 }
859
860
861 FloatList const & LyXTextClass::floats() const
862 {
863         return *floatlist_.get();
864 }
865
866
867 Counters & LyXTextClass::counters() const
868 {
869         return *ctrs_.get();
870 }
871
872
873 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
874 {
875         CharStyles::iterator cs = charstyles().begin();
876         CharStyles::iterator csend = charstyles().end();
877         for (; cs != csend; ++cs) {
878                 if (cs->name == s)
879                         return cs;
880         }
881         return csend;
882 }
883
884
885 string const & LyXTextClass::defaultLayoutName() const
886 {
887         // This really should come from the actual layout... (Lgb)
888         return defaultlayout_;
889 }
890
891
892 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
893 {
894         return operator[](defaultLayoutName());
895 }
896
897
898 string const & LyXTextClass::name() const
899 {
900         return name_;
901 }
902
903
904 string const & LyXTextClass::latexname() const
905 {
906         const_cast<LyXTextClass*>(this)->load();
907         return latexname_;
908 }
909
910
911 string const & LyXTextClass::description() const
912 {
913         return description_;
914 }
915
916
917 string const & LyXTextClass::opt_fontsize() const
918 {
919         return opt_fontsize_;
920 }
921
922
923 string const & LyXTextClass::opt_pagestyle() const
924 {
925         return opt_pagestyle_;
926 }
927
928
929 string const & LyXTextClass::options() const
930 {
931         return options_;
932 }
933
934
935 string const & LyXTextClass::class_header() const
936 {
937         return class_header_;
938 }
939
940
941 string const & LyXTextClass::pagestyle() const
942 {
943         return pagestyle_;
944 }
945
946
947 string const & LyXTextClass::preamble() const
948 {
949         return preamble_;
950 }
951
952
953 LyXTextClass::PageSides LyXTextClass::sides() const
954 {
955         return sides_;
956 }
957
958
959 int LyXTextClass::secnumdepth() const
960 {
961         return secnumdepth_;
962 }
963
964
965 int LyXTextClass::tocdepth() const
966 {
967         return tocdepth_;
968 }
969
970
971 OutputType LyXTextClass::outputType() const
972 {
973         return outputType_;
974 }
975
976
977 bool LyXTextClass::provides(LyXTextClass::Provides p) const
978 {
979         return provides_ & p;
980 }
981
982
983 unsigned int LyXTextClass::columns() const
984 {
985         return columns_;
986 }
987
988
989 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
990 {
991         return titletype_;
992 }
993
994
995 string const & LyXTextClass::titlename() const
996 {
997         return titlename_;
998 }
999
1000
1001 int LyXTextClass::size() const
1002 {
1003         return layoutlist_.size();
1004 }
1005
1006
1007 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1008 {
1009         switch (p) {
1010         case LyXTextClass::OneSide:
1011                 os << '1';
1012                 break;
1013         case LyXTextClass::TwoSides:
1014                 os << '2';
1015                 break;
1016         }
1017         return os;
1018 }