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