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