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