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