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