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