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