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