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