3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
8 * \author Angus Leeming
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
17 #include "TextClass.h"
21 #include "support/debug.h"
22 #include "support/gettext.h"
24 #include "FloatList.h"
29 #include "frontends/alert.h"
31 #include "support/lstrings.h"
32 #include "support/FileName.h"
33 #include "support/filetools.h"
34 #include "support/os.h"
39 using namespace lyx::support;
45 class LayoutNamesEqual : public unary_function<LayoutPtr, bool> {
47 LayoutNamesEqual(docstring const & name)
50 bool operator()(LayoutPtr const & c) const
52 return c->name() == name_;
62 bool layout2layout(FileName const & filename, FileName const & tempfile)
64 FileName const script = libFileSearch("scripts", "layout2layout.py");
66 lyxerr << "Could not find layout conversion "
67 "script layout2layout.py." << endl;
71 ostringstream command;
72 command << os::python() << ' ' << quoteName(script.toFilesystemEncoding())
73 << ' ' << quoteName(filename.toFilesystemEncoding())
74 << ' ' << quoteName(tempfile.toFilesystemEncoding());
75 string const command_str = command.str();
77 LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
80 runCommand(command_str);
82 lyxerr << "Could not run layout conversion "
83 "script layout2layout.py." << endl;
92 TextClass::TextClass(string const & fn, string const & cln,
93 string const & desc, bool texClassAvail )
94 : name_(fn), latexname_(cln), description_(desc),
95 floatlist_(new FloatList), counters_(new Counters),
96 texClassAvail_(texClassAvail)
104 pagestyle_ = "default";
105 defaultfont_ = sane_font;
106 opt_fontsize_ = "10|11|12";
107 opt_pagestyle_ = "empty|plain|headings|fancy";
108 titletype_ = TITLE_COMMAND_AFTER;
109 titlename_ = "maketitle";
114 bool TextClass::isTeXClassAvailable() const
116 return texClassAvail_;
120 bool TextClass::readStyle(Lexer & lexrc, Layout & lay)
122 LYXERR(Debug::TCLASS, "Reading style " << to_utf8(lay.name()));
123 if (!lay.read(lexrc, *this)) {
125 lay.resfont = lay.font;
126 lay.resfont.realize(defaultfont());
127 lay.reslabelfont = lay.labelfont;
128 lay.reslabelfont.realize(defaultfont());
129 return false; // no errors
131 lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
165 // Reads a textclass structure from file.
166 bool TextClass::read(FileName const & filename, ReadType rt)
168 if (!filename.isReadableFile()) {
169 lyxerr << "Cannot read layout file `" << filename << "'."
174 keyword_item textClassTags[] = {
175 { "classoptions", TC_CLASSOPTIONS },
176 { "columns", TC_COLUMNS },
177 { "counter", TC_COUNTER },
178 { "defaultfont", TC_DEFAULTFONT },
179 { "defaultstyle", TC_DEFAULTSTYLE },
180 { "environment", TC_ENVIRONMENT },
181 { "float", TC_FLOAT },
182 { "format", TC_FORMAT },
183 { "input", TC_INPUT },
184 { "insetlayout", TC_INSETLAYOUT },
185 { "leftmargin", TC_LEFTMARGIN },
186 { "nofloat", TC_NOFLOAT },
187 { "nostyle", TC_NOSTYLE },
188 { "outputtype", TC_OUTPUTTYPE },
189 { "pagestyle", TC_PAGESTYLE },
190 { "preamble", TC_PREAMBLE },
191 { "provides", TC_PROVIDES },
192 { "requires", TC_REQUIRES },
193 { "rightmargin", TC_RIGHTMARGIN },
194 { "secnumdepth", TC_SECNUMDEPTH },
195 { "sides", TC_SIDES },
196 { "style", TC_STYLE },
197 { "titlelatexname", TC_TITLELATEXNAME },
198 { "titlelatextype", TC_TITLELATEXTYPE },
199 { "tocdepth", TC_TOCDEPTH }
204 LYXERR(Debug::TCLASS, "Reading textclass ");
207 LYXERR(Debug::TCLASS, "Reading input file ");
210 LYXERR(Debug::TCLASS, "Reading module file ");
215 LYXERR(Debug::TCLASS, to_utf8(makeDisplayPath(filename.absFilename())));
217 Lexer lexrc(textClassTags,
218 sizeof(textClassTags) / sizeof(textClassTags[0]));
220 lexrc.setFile(filename);
221 bool error = !lexrc.isOK();
223 // Format of files before the 'Format' tag was introduced
227 while (lexrc.isOK() && !error) {
228 int le = lexrc.lex();
231 case Lexer::LEX_FEOF:
234 case Lexer::LEX_UNDEF:
235 lexrc.printError("Unknown TextClass tag `$$Token'");
243 switch (static_cast<TextClassTags>(le)) {
247 format = lexrc.getInteger();
250 case TC_OUTPUTTYPE: // output type definition
251 readOutputType(lexrc);
254 case TC_INPUT: // Include file
256 string const inc = lexrc.getString();
257 FileName tmp = libFileSearch("layouts", inc,
261 lexrc.printError("Could not find input file: " + inc);
263 } else if (read(tmp, MERGE)) {
264 lexrc.printError("Error reading input"
265 "file: " + tmp.absFilename());
271 case TC_DEFAULTSTYLE:
273 docstring const name = from_utf8(subst(lexrc.getString(),
275 defaultlayout_ = name;
282 docstring const name = from_utf8(subst(lexrc.getString(),
285 string s = "Could not read name for style: `$$Token' "
286 + lexrc.getString() + " is probably not valid UTF-8!";
287 lexrc.printError(s.c_str());
289 error = readStyle(lexrc, lay);
290 } else if (hasLayout(name)) {
291 Layout * lay = operator[](name).get();
292 error = readStyle(lexrc, *lay);
296 if (le == TC_ENVIRONMENT)
297 lay.is_environment = true;
298 error = readStyle(lexrc, lay);
300 layoutlist_.push_back(
301 boost::shared_ptr<Layout>(new Layout(lay))
304 if (defaultlayout_.empty()) {
305 // We do not have a default
306 // layout yet, so we choose
307 // the first layout we
309 defaultlayout_ = name;
314 lexrc.printError("No name given for style: `$$Token'.");
321 docstring const style = from_utf8(subst(lexrc.getString(),
323 if (!deleteLayout(style))
324 lyxerr << "Cannot delete style `"
325 << to_utf8(style) << '\'' << endl;
326 // lexrc.printError("Cannot delete style"
333 columns_ = lexrc.getInteger();
338 switch (lexrc.getInteger()) {
339 case 1: sides_ = OneSide; break;
340 case 2: sides_ = TwoSides; break;
342 lyxerr << "Impossible number of page"
343 " sides, setting to one."
353 pagestyle_ = rtrim(lexrc.getString());
357 defaultfont_ = lyxRead(lexrc);
358 if (!defaultfont_.resolved()) {
359 lexrc.printError("Warning: defaultfont should "
360 "be fully instantiated!");
361 defaultfont_.realize(sane_font);
367 secnumdepth_ = lexrc.getInteger();
372 tocdepth_ = lexrc.getInteger();
375 // First step to support options
376 case TC_CLASSOPTIONS:
377 readClassOptions(lexrc);
381 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
386 string const feature = lexrc.getString();
388 if (lexrc.getInteger())
389 provides_.insert(feature);
391 provides_.erase(feature);
397 vector<string> const req
398 = getVectorFromString(lexrc.getString());
399 requires_.insert(req.begin(), req.end());
403 case TC_LEFTMARGIN: // left margin type
405 leftmargin_ = lexrc.getDocString();
408 case TC_RIGHTMARGIN: // right margin type
410 rightmargin_ = lexrc.getDocString();
414 docstring const name = subst(lexrc.getDocString(), '_', ' ');
415 readInsetLayout(lexrc, name);
424 case TC_TITLELATEXTYPE:
425 readTitleType(lexrc);
427 case TC_TITLELATEXNAME:
429 titlename_ = lexrc.getString();
433 string const nofloat = lexrc.getString();
434 floatlist_->erase(nofloat);
438 if (format != FORMAT)
442 if (format != FORMAT) {
443 LYXERR(Debug::TCLASS, "Converting layout file from format "
444 << format << " to " << FORMAT);
445 FileName const tempfile = FileName::tempName();
446 error = !layout2layout(filename, tempfile);
448 error = read(tempfile, rt);
449 tempfile.removeFile();
454 LYXERR(Debug::TCLASS, "Finished reading module file "
455 << to_utf8(makeDisplayPath(filename.absFilename())));
456 else if (rt == MERGE)
457 LYXERR(Debug::TCLASS, "Finished reading input file "
458 << to_utf8(makeDisplayPath(filename.absFilename())));
459 else { // we are at top level here.
460 LYXERR(Debug::TCLASS, "Finished reading textclass "
461 << to_utf8(makeDisplayPath(filename.absFilename())));
462 if (defaultlayout_.empty()) {
463 lyxerr << "Error: Textclass '" << name_
464 << "' is missing a defaultstyle." << endl;
468 min_toclevel_ = Layout::NOT_IN_TOC;
469 max_toclevel_ = Layout::NOT_IN_TOC;
470 const_iterator cit = begin();
471 const_iterator the_end = end();
472 for ( ; cit != the_end ; ++cit) {
473 int const toclevel = (*cit)->toclevel;
474 if (toclevel != Layout::NOT_IN_TOC) {
475 if (min_toclevel_ == Layout::NOT_IN_TOC)
476 min_toclevel_ = toclevel;
478 min_toclevel_ = min(min_toclevel_,
480 max_toclevel_ = max(max_toclevel_,
484 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
485 << ", maximum is " << max_toclevel_);
493 void TextClass::readTitleType(Lexer & lexrc)
495 keyword_item titleTypeTags[] = {
496 { "commandafter", TITLE_COMMAND_AFTER },
497 { "environment", TITLE_ENVIRONMENT }
500 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
502 int le = lexrc.lex();
504 case Lexer::LEX_UNDEF:
505 lexrc.printError("Unknown output type `$$Token'");
507 case TITLE_COMMAND_AFTER:
508 case TITLE_ENVIRONMENT:
509 titletype_ = static_cast<TitleLatexType>(le);
512 lyxerr << "Unhandled value " << le
513 << " in TextClass::readTitleType." << endl;
520 void TextClass::readOutputType(Lexer & lexrc)
522 keyword_item outputTypeTags[] = {
523 { "docbook", DOCBOOK },
525 { "literate", LITERATE }
528 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
530 int le = lexrc.lex();
532 case Lexer::LEX_UNDEF:
533 lexrc.printError("Unknown output type `$$Token'");
538 outputType_ = static_cast<OutputType>(le);
541 lyxerr << "Unhandled value " << le
542 << " in TextClass::readOutputType." << endl;
549 enum ClassOptionsTags {
558 void TextClass::readClassOptions(Lexer & lexrc)
560 keyword_item classOptionsTags[] = {
562 {"fontsize", CO_FONTSIZE },
563 {"header", CO_HEADER },
564 {"other", CO_OTHER },
565 {"pagestyle", CO_PAGESTYLE }
568 lexrc.pushTable(classOptionsTags, CO_END);
570 while (!getout && lexrc.isOK()) {
571 int le = lexrc.lex();
573 case Lexer::LEX_UNDEF:
574 lexrc.printError("Unknown ClassOption tag `$$Token'");
578 switch (static_cast<ClassOptionsTags>(le)) {
581 opt_fontsize_ = rtrim(lexrc.getString());
585 opt_pagestyle_ = rtrim(lexrc.getString());
589 options_ = lexrc.getString();
593 class_header_ = subst(lexrc.getString(), """, "\"");
604 enum InsetLayoutTags {
626 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
628 keyword_item elementTags[] = {
629 { "bgcolor", IL_BGCOLOR },
630 { "decoration", IL_DECORATION },
633 { "forceltr", IL_FORCELTR },
634 { "freespacing", IL_FREESPACING },
635 { "keepempty", IL_KEEPEMPTY },
636 { "labelfont", IL_LABELFONT },
637 { "labelstring", IL_LABELSTRING },
638 { "latexname", IL_LATEXNAME },
639 { "latexparam", IL_LATEXPARAM },
640 { "latextype", IL_LATEXTYPE },
641 { "lyxtype", IL_LYXTYPE },
642 { "multipar", IL_MULTIPAR },
643 { "needprotect", IL_NEEDPROTECT },
644 { "passthru", IL_PASSTHRU },
645 { "preamble", IL_PREAMBLE },
646 { "requires", IL_REQUIRES }
649 lexrc.pushTable(elementTags, IL_END);
652 docstring labelstring;
657 FontInfo font = inherit_font;
658 FontInfo labelfont = inherit_font;
659 ColorCode bgcolor(Color_background);
661 set<string> requires;
662 bool multipar = false;
663 bool passthru = false;
664 bool needprotect = false;
665 bool keepempty = false;
666 bool freespacing = false;
667 bool forceltr = false;
670 while (!getout && lexrc.isOK()) {
671 int le = lexrc.lex();
673 case Lexer::LEX_UNDEF:
674 lexrc.printError("Unknown InsetLayout tag `$$Token'");
678 switch (static_cast<InsetLayoutTags>(le)) {
681 lyxtype = lexrc.getString();
685 latextype = lexrc.getString();
689 labelstring = lexrc.getDocString();
693 decoration = lexrc.getString();
697 latexname = lexrc.getString();
701 latexparam = subst(lexrc.getString(), """, "\"");
704 labelfont = lyxRead(lexrc, inherit_font);
708 forceltr = lexrc.getBool();
712 multipar = lexrc.getBool();
716 passthru = lexrc.getBool();
720 keepempty = lexrc.getBool();
724 freespacing = lexrc.getBool();
728 needprotect = lexrc.getBool();
731 font = lyxRead(lexrc, inherit_font);
732 // So: define font before labelfont
737 string const token = lexrc.getString();
738 bgcolor = lcolor.getFromLyXName(token);
742 preamble = lexrc.getLongString("EndPreamble");
746 vector<string> const req
747 = getVectorFromString(lexrc.getString());
748 requires.insert(req.begin(), req.end());
757 // Here add element to list if getout == true
760 il.name = to_ascii(name);
761 il.lyxtype = lyxtype;
762 il.labelstring = labelstring;
763 il.decoration = decoration;
764 il.latextype = latextype;
765 il.latexname = latexname;
766 il.latexparam = latexparam;
767 il.multipar = multipar;
768 il.passthru = passthru;
769 il.needprotect = needprotect;
770 il.freespacing = freespacing;
771 il.forceltr = forceltr;
772 il.keepempty = keepempty;
774 // The label font is generally used as-is without
775 // any realization against a given context.
776 labelfont.realize(sane_font);
777 il.labelfont = labelfont;
778 il.bgcolor = bgcolor;
779 il.preamble = preamble;
780 il.requires = requires;
781 insetlayoutlist_[name] = il;
802 void TextClass::readFloat(Lexer & lexrc)
804 keyword_item floatTags[] = {
806 { "extension", FT_EXT },
807 { "guiname", FT_NAME },
808 { "latexbuiltin", FT_BUILTIN },
809 { "listname", FT_LISTNAME },
810 { "numberwithin", FT_WITHIN },
811 { "placement", FT_PLACEMENT },
812 { "style", FT_STYLE },
816 lexrc.pushTable(floatTags, FT_END);
825 bool builtin = false;
828 while (!getout && lexrc.isOK()) {
829 int le = lexrc.lex();
831 case Lexer::LEX_UNDEF:
832 lexrc.printError("Unknown float tag `$$Token'");
836 switch (static_cast<FloatTags>(le)) {
839 type = lexrc.getString();
840 if (floatlist_->typeExist(type)) {
841 Floating const & fl = floatlist_->getType(type);
842 placement = fl.placement();
844 within = fl.within();
847 listName = fl.listName();
848 builtin = fl.builtin();
853 name = lexrc.getString();
857 placement = lexrc.getString();
861 ext = lexrc.getString();
865 within = lexrc.getString();
866 if (within == "none")
871 style = lexrc.getString();
875 listName = lexrc.getString();
879 builtin = lexrc.getBool();
887 // Here if have a full float if getout == true
889 Floating fl(type, placement, ext, within,
890 style, name, listName, builtin);
891 floatlist_->newFloat(fl);
892 // each float has its own counter
893 counters_->newCounter(from_ascii(type), from_ascii(within),
894 docstring(), docstring());
905 CT_LABELSTRING_APPENDIX,
910 void TextClass::readCounter(Lexer & lexrc)
912 keyword_item counterTags[] = {
914 { "labelstring", CT_LABELSTRING },
915 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
917 { "within", CT_WITHIN }
920 lexrc.pushTable(counterTags, CT_END);
924 docstring labelstring;
925 docstring labelstring_appendix;
928 while (!getout && lexrc.isOK()) {
929 int le = lexrc.lex();
931 case Lexer::LEX_UNDEF:
932 lexrc.printError("Unknown counter tag `$$Token'");
936 switch (static_cast<CounterTags>(le)) {
939 name = lexrc.getDocString();
940 if (counters_->hasCounter(name))
941 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
943 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
947 within = lexrc.getDocString();
948 if (within == "none")
953 labelstring = lexrc.getDocString();
954 labelstring_appendix = labelstring;
956 case CT_LABELSTRING_APPENDIX:
958 labelstring_appendix = lexrc.getDocString();
966 // Here if have a full counter if getout == true
968 counters_->newCounter(name, within,
969 labelstring, labelstring_appendix);
975 FontInfo const & TextClass::defaultfont() const
981 docstring const & TextClass::leftmargin() const
987 docstring const & TextClass::rightmargin() const
993 bool TextClass::hasLayout(docstring const & n) const
995 docstring const name = n.empty() ? defaultLayoutName() : n;
997 return find_if(layoutlist_.begin(), layoutlist_.end(),
998 LayoutNamesEqual(name))
999 != layoutlist_.end();
1004 LayoutPtr const & TextClass::operator[](docstring const & name) const
1006 BOOST_ASSERT(!name.empty());
1008 LayoutList::const_iterator cit =
1009 find_if(layoutlist_.begin(),
1011 LayoutNamesEqual(name));
1013 if (cit == layoutlist_.end()) {
1014 lyxerr << "We failed to find the layout '" << to_utf8(name)
1015 << "' in the layout list. You MUST investigate!"
1017 for (LayoutList::const_iterator it = layoutlist_.begin();
1018 it != layoutlist_.end(); ++it)
1019 lyxerr << " " << to_utf8(it->get()->name()) << endl;
1021 // we require the name to exist
1022 BOOST_ASSERT(false);
1029 bool TextClass::deleteLayout(docstring const & name)
1031 if (name == defaultLayoutName())
1034 LayoutList::iterator it =
1035 remove_if(layoutlist_.begin(), layoutlist_.end(),
1036 LayoutNamesEqual(name));
1038 LayoutList::iterator end = layoutlist_.end();
1039 bool const ret = (it != end);
1040 layoutlist_.erase(it, end);
1045 // Load textclass info if not loaded yet
1046 bool TextClass::load(string const & path) const
1051 // Read style-file, provided path is searched before the system ones
1052 FileName layout_file;
1054 layout_file = FileName(addName(path, name_ + ".layout"));
1055 if (layout_file.empty() || !layout_file.exists())
1056 layout_file = libFileSearch("layouts", name_, "layout");
1057 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1060 lyxerr << "Error reading `"
1061 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1062 << "'\n(Check `" << name_
1063 << "')\nCheck your installation and "
1064 "try Options/Reconfigure..." << endl;
1071 FloatList & TextClass::floats()
1073 return *floatlist_.get();
1077 FloatList const & TextClass::floats() const
1079 return *floatlist_.get();
1083 Counters & TextClass::counters() const
1085 return *counters_.get();
1089 // Return the layout object of an inset given by name. If the name
1090 // is not found as such, the part after the ':' is stripped off, and
1091 // searched again. In this way, an error fallback can be provided:
1092 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1093 // will invoke the layout object defined by name = 'CharStyle'.
1094 // If that doesn't work either, an empty object returns (shouldn't
1095 // happen). -- Idea JMarc, comment MV
1096 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1099 while (!n.empty()) {
1100 if (insetlayoutlist_.count(n) > 0)
1101 return insetlayoutlist_[n];
1102 docstring::size_type i = n.find(':');
1103 if (i == string::npos)
1107 static InsetLayout empty;
1108 empty.labelstring = from_utf8("UNDEFINED");
1109 empty.labelfont = sane_font;
1110 empty.labelfont.setColor(Color_error);
1111 empty.bgcolor = Color_error;
1116 docstring const & TextClass::defaultLayoutName() const
1118 // This really should come from the actual layout... (Lgb)
1119 return defaultlayout_;
1123 LayoutPtr const & TextClass::defaultLayout() const
1125 return operator[](defaultLayoutName());
1129 string const & TextClass::name() const
1135 string const & TextClass::latexname() const
1137 const_cast<TextClass*>(this)->load();
1142 string const & TextClass::description() const
1144 return description_;
1148 string const & TextClass::opt_fontsize() const
1150 return opt_fontsize_;
1154 string const & TextClass::opt_pagestyle() const
1156 return opt_pagestyle_;
1160 string const & TextClass::options() const
1166 string const & TextClass::class_header() const
1168 return class_header_;
1172 string const & TextClass::pagestyle() const
1178 docstring const & TextClass::preamble() const
1184 PageSides TextClass::sides() const
1190 int TextClass::secnumdepth() const
1192 return secnumdepth_;
1196 int TextClass::tocdepth() const
1202 OutputType TextClass::outputType() const
1208 bool TextClass::provides(string const & p) const
1210 return provides_.find(p) != provides_.end();
1214 unsigned int TextClass::columns() const
1220 TitleLatexType TextClass::titletype() const
1226 string const & TextClass::titlename() const
1232 int TextClass::size() const
1234 return layoutlist_.size();
1238 int TextClass::min_toclevel() const
1240 return min_toclevel_;
1244 int TextClass::max_toclevel() const
1246 return max_toclevel_;
1250 bool TextClass::hasTocLevels() const
1252 return min_toclevel_ != Layout::NOT_IN_TOC;
1256 ostream & operator<<(ostream & os, PageSides p)