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