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