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