]> git.lyx.org Git - lyx.git/blob - src/TextClass.cpp
Minor adjustment to previous commit.
[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), ctrs_(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                         // Here we could check if this type is already defined
845                         // and modify it with the rest of the vars instead.
846                         break;
847                 case FT_NAME:
848                         lexrc.next();
849                         name = lexrc.getString();
850                         break;
851                 case FT_PLACEMENT:
852                         lexrc.next();
853                         placement = lexrc.getString();
854                         break;
855                 case FT_EXT:
856                         lexrc.next();
857                         ext = lexrc.getString();
858                         break;
859                 case FT_WITHIN:
860                         lexrc.next();
861                         within = lexrc.getString();
862                         if (within == "none")
863                                 within.erase();
864                         break;
865                 case FT_STYLE:
866                         lexrc.next();
867                         style = lexrc.getString();
868                         break;
869                 case FT_LISTNAME:
870                         lexrc.next();
871                         listname = lexrc.getString();
872                         break;
873                 case FT_BUILTIN:
874                         lexrc.next();
875                         builtin = lexrc.getBool();
876                         break;
877                 case FT_END:
878                         getout = true;
879                         break;
880                 }
881         }
882
883         // Here if have a full float if getout == true
884         if (getout) {
885                 Floating newfloat(type, placement, ext, within,
886                                   style, name, listname, builtin);
887                 floatlist_->newFloat(newfloat);
888         }
889
890         lexrc.popTable();
891 }
892
893
894 enum CounterTags {
895         CT_NAME = 1,
896         CT_WITHIN,
897         CT_END
898 };
899
900 void TextClass::readCounter(Lexer & lexrc)
901 {
902         keyword_item counterTags[] = {
903                 { "end", CT_END },
904                 { "name", CT_NAME },
905                 { "within", CT_WITHIN }
906         };
907
908         lexrc.pushTable(counterTags, CT_END);
909
910         docstring name;
911         docstring within;
912
913         bool getout = false;
914         while (!getout && lexrc.isOK()) {
915                 int le = lexrc.lex();
916                 switch (le) {
917                 case Lexer::LEX_UNDEF:
918                         lexrc.printError("Unknown ClassOption tag `$$Token'");
919                         continue;
920                 default: break;
921                 }
922                 switch (static_cast<CounterTags>(le)) {
923                 case CT_NAME:
924                         lexrc.next();
925                         name = from_ascii(lexrc.getString());
926                         break;
927                 case CT_WITHIN:
928                         lexrc.next();
929                         within = from_ascii(lexrc.getString());
930                         if (within == "none")
931                                 within.erase();
932                         break;
933                 case CT_END:
934                         getout = true;
935                         break;
936                 }
937         }
938
939         // Here if have a full counter if getout == true
940         if (getout) {
941                 if (within.empty())
942                         ctrs_->newCounter(name);
943                 else
944                         ctrs_->newCounter(name, within);
945         }
946
947         lexrc.popTable();
948 }
949
950
951 Font const & TextClass::defaultfont() const
952 {
953         return defaultfont_;
954 }
955
956
957 string const & TextClass::leftmargin() const
958 {
959         return leftmargin_;
960 }
961
962
963 string const & TextClass::rightmargin() const
964 {
965         return rightmargin_;
966 }
967
968
969 bool TextClass::hasLayout(docstring const & n) const
970 {
971         docstring const name = n.empty() ? defaultLayoutName() : n;
972
973         return find_if(layoutlist_.begin(), layoutlist_.end(),
974                        LayoutNamesEqual(name))
975                 != layoutlist_.end();
976 }
977
978
979
980 Layout_ptr const & TextClass::operator[](docstring const & name) const
981 {
982         BOOST_ASSERT(!name.empty());
983
984         LayoutList::const_iterator cit =
985                 find_if(layoutlist_.begin(),
986                         layoutlist_.end(),
987                         LayoutNamesEqual(name));
988
989         if (cit == layoutlist_.end()) {
990                 lyxerr << "We failed to find the layout '" << to_utf8(name)
991                        << "' in the layout list. You MUST investigate!"
992                        << endl;
993                 for (LayoutList::const_iterator it = layoutlist_.begin();
994                          it != layoutlist_.end(); ++it)
995                         lyxerr  << " " << to_utf8(it->get()->name()) << endl;
996
997                 // we require the name to exist
998                 BOOST_ASSERT(false);
999         }
1000
1001         return (*cit);
1002 }
1003
1004
1005
1006 bool TextClass::delete_layout(docstring const & name)
1007 {
1008         if (name == defaultLayoutName())
1009                 return false;
1010
1011         LayoutList::iterator it =
1012                 remove_if(layoutlist_.begin(), layoutlist_.end(),
1013                           LayoutNamesEqual(name));
1014
1015         LayoutList::iterator end = layoutlist_.end();
1016         bool const ret = (it != end);
1017         layoutlist_.erase(it, end);
1018         return ret;
1019 }
1020
1021
1022 // Load textclass info if not loaded yet
1023 bool TextClass::load(string const & path) const
1024 {
1025         if (loaded_)
1026                 return true;
1027
1028         // Read style-file, provided path is searched before the system ones
1029         FileName layout_file;
1030         if (!path.empty())
1031                 layout_file = FileName(addName(path, name_ + ".layout"));
1032         if (layout_file.empty() || !fs::exists(layout_file.toFilesystemEncoding()))
1033                 layout_file = libFileSearch("layouts", name_, "layout");
1034         loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1035
1036         if (!loaded_) {
1037                 lyxerr << "Error reading `"
1038                        << to_utf8(makeDisplayPath(layout_file.absFilename()))
1039                        << "'\n(Check `" << name_
1040                        << "')\nCheck your installation and "
1041                         "try Options/Reconfigure..." << endl;
1042         }
1043
1044         return loaded_;
1045 }
1046
1047
1048 FloatList & TextClass::floats()
1049 {
1050         return *floatlist_.get();
1051 }
1052
1053
1054 FloatList const & TextClass::floats() const
1055 {
1056         return *floatlist_.get();
1057 }
1058
1059
1060 Counters & TextClass::counters() const
1061 {
1062         return *ctrs_.get();
1063 }
1064
1065 InsetLayout const & TextClass::insetlayout(docstring const & name) const 
1066 {
1067         return insetlayoutlist_[name]; 
1068 }
1069
1070
1071 CharStyles::iterator TextClass::charstyle(string const & s) const
1072 {
1073         CharStyles::iterator cs = charstyles().begin();
1074         CharStyles::iterator csend = charstyles().end();
1075         for (; cs != csend; ++cs) {
1076                 if (cs->name == s)
1077                         return cs;
1078         }
1079         return csend;
1080 }
1081
1082
1083 docstring const & TextClass::defaultLayoutName() const
1084 {
1085         // This really should come from the actual layout... (Lgb)
1086         return defaultlayout_;
1087 }
1088
1089
1090 Layout_ptr const & TextClass::defaultLayout() const
1091 {
1092         return operator[](defaultLayoutName());
1093 }
1094
1095
1096 string const & TextClass::name() const
1097 {
1098         return name_;
1099 }
1100
1101
1102 string const & TextClass::latexname() const
1103 {
1104         const_cast<TextClass*>(this)->load();
1105         return latexname_;
1106 }
1107
1108
1109 string const & TextClass::description() const
1110 {
1111         return description_;
1112 }
1113
1114
1115 string const & TextClass::opt_fontsize() const
1116 {
1117         return opt_fontsize_;
1118 }
1119
1120
1121 string const & TextClass::opt_pagestyle() const
1122 {
1123         return opt_pagestyle_;
1124 }
1125
1126
1127 string const & TextClass::options() const
1128 {
1129         return options_;
1130 }
1131
1132
1133 string const & TextClass::class_header() const
1134 {
1135         return class_header_;
1136 }
1137
1138
1139 string const & TextClass::pagestyle() const
1140 {
1141         return pagestyle_;
1142 }
1143
1144
1145 docstring const & TextClass::preamble() const
1146 {
1147         return preamble_;
1148 }
1149
1150
1151 TextClass::PageSides TextClass::sides() const
1152 {
1153         return sides_;
1154 }
1155
1156
1157 int TextClass::secnumdepth() const
1158 {
1159         return secnumdepth_;
1160 }
1161
1162
1163 int TextClass::tocdepth() const
1164 {
1165         return tocdepth_;
1166 }
1167
1168
1169 OutputType TextClass::outputType() const
1170 {
1171         return outputType_;
1172 }
1173
1174
1175 bool TextClass::provides(string const & p) const
1176 {
1177         return provides_.find(p) != provides_.end();
1178 }
1179
1180
1181 unsigned int TextClass::columns() const
1182 {
1183         return columns_;
1184 }
1185
1186
1187 LYX_TITLE_LATEX_TYPES TextClass::titletype() const
1188 {
1189         return titletype_;
1190 }
1191
1192
1193 string const & TextClass::titlename() const
1194 {
1195         return titlename_;
1196 }
1197
1198
1199 int TextClass::size() const
1200 {
1201         return layoutlist_.size();
1202 }
1203
1204
1205 int TextClass::min_toclevel() const
1206 {
1207         return min_toclevel_;
1208 }
1209
1210
1211 int TextClass::max_toclevel() const
1212 {
1213         return max_toclevel_;
1214 }
1215
1216
1217 bool TextClass::hasTocLevels() const
1218 {
1219         return min_toclevel_ != Layout::NOT_IN_TOC;
1220 }
1221
1222
1223 ostream & operator<<(ostream & os, TextClass::PageSides p)
1224 {
1225         switch (p) {
1226         case TextClass::OneSide:
1227                 os << '1';
1228                 break;
1229         case TextClass::TwoSides:
1230                 os << '2';
1231                 break;
1232         }
1233         return os;
1234 }
1235
1236
1237 } // namespace lyx