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