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"
264 } else if (read(tmp, MERGE)) {
265 lexrc.printError("Error reading input"
266 "file: " + tmp.absFilename());
272 case TC_DEFAULTSTYLE:
274 docstring const name = from_utf8(subst(lexrc.getString(),
276 defaultlayout_ = name;
283 docstring const name = from_utf8(subst(lexrc.getString(),
286 string s = "Could not read name for style: `$$Token' "
287 + lexrc.getString() + " is probably not valid UTF-8!";
288 lexrc.printError(s.c_str());
290 error = readStyle(lexrc, lay);
291 } else if (hasLayout(name)) {
292 Layout * lay = operator[](name).get();
293 error = readStyle(lexrc, *lay);
297 if (le == TC_ENVIRONMENT)
298 lay.is_environment = true;
299 error = readStyle(lexrc, lay);
301 layoutlist_.push_back(
302 boost::shared_ptr<Layout>(new Layout(lay))
305 if (defaultlayout_.empty()) {
306 // We do not have a default
307 // layout yet, so we choose
308 // the first layout we
310 defaultlayout_ = name;
315 lexrc.printError("No name given for style: `$$Token'.");
322 docstring const style = from_utf8(subst(lexrc.getString(),
324 if (!deleteLayout(style))
325 lyxerr << "Cannot delete style `"
326 << to_utf8(style) << '\'' << endl;
327 // lexrc.printError("Cannot delete style"
334 columns_ = lexrc.getInteger();
339 switch (lexrc.getInteger()) {
340 case 1: sides_ = OneSide; break;
341 case 2: sides_ = TwoSides; break;
343 lyxerr << "Impossible number of page"
344 " sides, setting to one."
354 pagestyle_ = rtrim(lexrc.getString());
358 defaultfont_ = lyxRead(lexrc);
359 if (!defaultfont_.resolved()) {
360 lexrc.printError("Warning: defaultfont should "
361 "be fully instantiated!");
362 defaultfont_.realize(sane_font);
368 secnumdepth_ = lexrc.getInteger();
373 tocdepth_ = lexrc.getInteger();
376 // First step to support options
377 case TC_CLASSOPTIONS:
378 readClassOptions(lexrc);
382 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
387 string const feature = lexrc.getString();
389 if (lexrc.getInteger())
390 provides_.insert(feature);
392 provides_.erase(feature);
398 vector<string> const req
399 = getVectorFromString(lexrc.getString());
400 requires_.insert(req.begin(), req.end());
404 case TC_LEFTMARGIN: // left margin type
406 leftmargin_ = lexrc.getDocString();
409 case TC_RIGHTMARGIN: // right margin type
411 rightmargin_ = lexrc.getDocString();
415 docstring const name = subst(lexrc.getDocString(), '_', ' ');
416 readInsetLayout(lexrc, name);
425 case TC_TITLELATEXTYPE:
426 readTitleType(lexrc);
428 case TC_TITLELATEXNAME:
430 titlename_ = lexrc.getString();
434 string const nofloat = lexrc.getString();
435 floatlist_->erase(nofloat);
439 if (format != FORMAT)
443 if (format != FORMAT) {
444 LYXERR(Debug::TCLASS, "Converting layout file from format "
445 << format << " to " << FORMAT);
446 FileName const tempfile = FileName::tempName();
447 error = !layout2layout(filename, tempfile);
449 error = read(tempfile, rt);
450 tempfile.removeFile();
455 LYXERR(Debug::TCLASS, "Finished reading module file "
456 << to_utf8(makeDisplayPath(filename.absFilename())));
457 else if (rt == MERGE)
458 LYXERR(Debug::TCLASS, "Finished reading input file "
459 << to_utf8(makeDisplayPath(filename.absFilename())));
460 else { // we are at top level here.
461 LYXERR(Debug::TCLASS, "Finished reading textclass "
462 << to_utf8(makeDisplayPath(filename.absFilename())));
463 if (defaultlayout_.empty()) {
464 lyxerr << "Error: Textclass '" << name_
465 << "' is missing a defaultstyle." << endl;
469 min_toclevel_ = Layout::NOT_IN_TOC;
470 max_toclevel_ = Layout::NOT_IN_TOC;
471 const_iterator cit = begin();
472 const_iterator the_end = end();
473 for ( ; cit != the_end ; ++cit) {
474 int const toclevel = (*cit)->toclevel;
475 if (toclevel != Layout::NOT_IN_TOC) {
476 if (min_toclevel_ == Layout::NOT_IN_TOC)
477 min_toclevel_ = toclevel;
479 min_toclevel_ = min(min_toclevel_,
481 max_toclevel_ = max(max_toclevel_,
485 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
486 << ", maximum is " << max_toclevel_);
494 void TextClass::readTitleType(Lexer & lexrc)
496 keyword_item titleTypeTags[] = {
497 { "commandafter", TITLE_COMMAND_AFTER },
498 { "environment", TITLE_ENVIRONMENT }
501 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
503 int le = lexrc.lex();
505 case Lexer::LEX_UNDEF:
506 lexrc.printError("Unknown output type `$$Token'");
508 case TITLE_COMMAND_AFTER:
509 case TITLE_ENVIRONMENT:
510 titletype_ = static_cast<TitleLatexType>(le);
513 lyxerr << "Unhandled value " << le
514 << " in TextClass::readTitleType." << endl;
521 void TextClass::readOutputType(Lexer & lexrc)
523 keyword_item outputTypeTags[] = {
524 { "docbook", DOCBOOK },
526 { "literate", LITERATE }
529 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
531 int le = lexrc.lex();
533 case Lexer::LEX_UNDEF:
534 lexrc.printError("Unknown output type `$$Token'");
539 outputType_ = static_cast<OutputType>(le);
542 lyxerr << "Unhandled value " << le
543 << " in TextClass::readOutputType." << endl;
550 enum ClassOptionsTags {
559 void TextClass::readClassOptions(Lexer & lexrc)
561 keyword_item classOptionsTags[] = {
563 {"fontsize", CO_FONTSIZE },
564 {"header", CO_HEADER },
565 {"other", CO_OTHER },
566 {"pagestyle", CO_PAGESTYLE }
569 lexrc.pushTable(classOptionsTags, CO_END);
571 while (!getout && lexrc.isOK()) {
572 int le = lexrc.lex();
574 case Lexer::LEX_UNDEF:
575 lexrc.printError("Unknown ClassOption tag `$$Token'");
579 switch (static_cast<ClassOptionsTags>(le)) {
582 opt_fontsize_ = rtrim(lexrc.getString());
586 opt_pagestyle_ = rtrim(lexrc.getString());
590 options_ = lexrc.getString();
594 class_header_ = subst(lexrc.getString(), """, "\"");
605 enum InsetLayoutTags {
627 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
629 keyword_item elementTags[] = {
630 { "bgcolor", IL_BGCOLOR },
631 { "decoration", IL_DECORATION },
634 { "forceltr", IL_FORCELTR },
635 { "freespacing", IL_FREESPACING },
636 { "keepempty", IL_KEEPEMPTY },
637 { "labelfont", IL_LABELFONT },
638 { "labelstring", IL_LABELSTRING },
639 { "latexname", IL_LATEXNAME },
640 { "latexparam", IL_LATEXPARAM },
641 { "latextype", IL_LATEXTYPE },
642 { "lyxtype", IL_LYXTYPE },
643 { "multipar", IL_MULTIPAR },
644 { "needprotect", IL_NEEDPROTECT },
645 { "passthru", IL_PASSTHRU },
646 { "preamble", IL_PREAMBLE },
647 { "requires", IL_REQUIRES }
650 lexrc.pushTable(elementTags, IL_END);
653 docstring labelstring;
658 FontInfo font = inherit_font;
659 FontInfo labelfont = inherit_font;
660 ColorCode bgcolor(Color_background);
662 set<string> requires;
663 bool multipar = false;
664 bool passthru = false;
665 bool needprotect = false;
666 bool keepempty = false;
667 bool freespacing = false;
668 bool forceltr = false;
671 while (!getout && lexrc.isOK()) {
672 int le = lexrc.lex();
674 case Lexer::LEX_UNDEF:
675 lexrc.printError("Unknown InsetLayout tag `$$Token'");
679 switch (static_cast<InsetLayoutTags>(le)) {
682 lyxtype = lexrc.getString();
686 latextype = lexrc.getString();
690 labelstring = lexrc.getDocString();
694 decoration = lexrc.getString();
698 latexname = lexrc.getString();
702 latexparam = subst(lexrc.getString(), """, "\"");
705 labelfont = lyxRead(lexrc, inherit_font);
709 forceltr = lexrc.getBool();
713 multipar = lexrc.getBool();
717 passthru = lexrc.getBool();
721 keepempty = lexrc.getBool();
725 freespacing = lexrc.getBool();
729 needprotect = lexrc.getBool();
732 font = lyxRead(lexrc, inherit_font);
733 // So: define font before labelfont
738 string const token = lexrc.getString();
739 bgcolor = lcolor.getFromLyXName(token);
743 preamble = lexrc.getLongString("EndPreamble");
747 vector<string> const req
748 = getVectorFromString(lexrc.getString());
749 requires.insert(req.begin(), req.end());
758 // Here add element to list if getout == true
761 il.name = to_ascii(name);
762 il.lyxtype = lyxtype;
763 il.labelstring = labelstring;
764 il.decoration = decoration;
765 il.latextype = latextype;
766 il.latexname = latexname;
767 il.latexparam = latexparam;
768 il.multipar = multipar;
769 il.passthru = passthru;
770 il.needprotect = needprotect;
771 il.freespacing = freespacing;
772 il.forceltr = forceltr;
773 il.keepempty = keepempty;
775 // The label font is generally used as-is without
776 // any realization against a given context.
777 labelfont.realize(sane_font);
778 il.labelfont = labelfont;
779 il.bgcolor = bgcolor;
780 il.preamble = preamble;
781 il.requires = requires;
782 insetlayoutlist_[name] = il;
803 void TextClass::readFloat(Lexer & lexrc)
805 keyword_item floatTags[] = {
807 { "extension", FT_EXT },
808 { "guiname", FT_NAME },
809 { "latexbuiltin", FT_BUILTIN },
810 { "listname", FT_LISTNAME },
811 { "numberwithin", FT_WITHIN },
812 { "placement", FT_PLACEMENT },
813 { "style", FT_STYLE },
817 lexrc.pushTable(floatTags, FT_END);
826 bool builtin = false;
829 while (!getout && lexrc.isOK()) {
830 int le = lexrc.lex();
832 case Lexer::LEX_UNDEF:
833 lexrc.printError("Unknown float tag `$$Token'");
837 switch (static_cast<FloatTags>(le)) {
840 type = lexrc.getString();
841 if (floatlist_->typeExist(type)) {
842 Floating const & fl = floatlist_->getType(type);
843 placement = fl.placement();
845 within = fl.within();
848 listName = fl.listName();
849 builtin = fl.builtin();
854 name = lexrc.getString();
858 placement = lexrc.getString();
862 ext = lexrc.getString();
866 within = lexrc.getString();
867 if (within == "none")
872 style = lexrc.getString();
876 listName = lexrc.getString();
880 builtin = lexrc.getBool();
888 // Here if have a full float if getout == true
890 Floating fl(type, placement, ext, within,
891 style, name, listName, builtin);
892 floatlist_->newFloat(fl);
893 // each float has its own counter
894 counters_->newCounter(from_ascii(type), from_ascii(within),
895 docstring(), docstring());
906 CT_LABELSTRING_APPENDIX,
911 void TextClass::readCounter(Lexer & lexrc)
913 keyword_item counterTags[] = {
915 { "labelstring", CT_LABELSTRING },
916 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
918 { "within", CT_WITHIN }
921 lexrc.pushTable(counterTags, CT_END);
925 docstring labelstring;
926 docstring labelstring_appendix;
929 while (!getout && lexrc.isOK()) {
930 int le = lexrc.lex();
932 case Lexer::LEX_UNDEF:
933 lexrc.printError("Unknown counter tag `$$Token'");
937 switch (static_cast<CounterTags>(le)) {
940 name = lexrc.getDocString();
941 if (counters_->hasCounter(name))
942 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
944 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
948 within = lexrc.getDocString();
949 if (within == "none")
954 labelstring = lexrc.getDocString();
955 labelstring_appendix = labelstring;
957 case CT_LABELSTRING_APPENDIX:
959 labelstring_appendix = lexrc.getDocString();
967 // Here if have a full counter if getout == true
969 counters_->newCounter(name, within,
970 labelstring, labelstring_appendix);
976 FontInfo const & TextClass::defaultfont() const
982 docstring const & TextClass::leftmargin() const
988 docstring const & TextClass::rightmargin() const
994 bool TextClass::hasLayout(docstring const & n) const
996 docstring const name = n.empty() ? defaultLayoutName() : n;
998 return find_if(layoutlist_.begin(), layoutlist_.end(),
999 LayoutNamesEqual(name))
1000 != layoutlist_.end();
1005 LayoutPtr const & TextClass::operator[](docstring const & name) const
1007 BOOST_ASSERT(!name.empty());
1009 LayoutList::const_iterator cit =
1010 find_if(layoutlist_.begin(),
1012 LayoutNamesEqual(name));
1014 if (cit == layoutlist_.end()) {
1015 lyxerr << "We failed to find the layout '" << to_utf8(name)
1016 << "' in the layout list. You MUST investigate!"
1018 for (LayoutList::const_iterator it = layoutlist_.begin();
1019 it != layoutlist_.end(); ++it)
1020 lyxerr << " " << to_utf8(it->get()->name()) << endl;
1022 // we require the name to exist
1023 BOOST_ASSERT(false);
1030 bool TextClass::deleteLayout(docstring const & name)
1032 if (name == defaultLayoutName())
1035 LayoutList::iterator it =
1036 remove_if(layoutlist_.begin(), layoutlist_.end(),
1037 LayoutNamesEqual(name));
1039 LayoutList::iterator end = layoutlist_.end();
1040 bool const ret = (it != end);
1041 layoutlist_.erase(it, end);
1046 // Load textclass info if not loaded yet
1047 bool TextClass::load(string const & path) const
1052 // Read style-file, provided path is searched before the system ones
1053 FileName layout_file;
1055 layout_file = FileName(addName(path, name_ + ".layout"));
1056 if (layout_file.empty() || !layout_file.exists())
1057 layout_file = libFileSearch("layouts", name_, "layout");
1058 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1061 lyxerr << "Error reading `"
1062 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1063 << "'\n(Check `" << name_
1064 << "')\nCheck your installation and "
1065 "try Options/Reconfigure..." << endl;
1072 FloatList & TextClass::floats()
1074 return *floatlist_.get();
1078 FloatList const & TextClass::floats() const
1080 return *floatlist_.get();
1084 Counters & TextClass::counters() const
1086 return *counters_.get();
1090 // Return the layout object of an inset given by name. If the name
1091 // is not found as such, the part after the ':' is stripped off, and
1092 // searched again. In this way, an error fallback can be provided:
1093 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1094 // will invoke the layout object defined by name = 'CharStyle'.
1095 // If that doesn't work either, an empty object returns (shouldn't
1096 // happen). -- Idea JMarc, comment MV
1097 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1100 while (!n.empty()) {
1101 if (insetlayoutlist_.count(n) > 0)
1102 return insetlayoutlist_[n];
1103 docstring::size_type i = n.find(':');
1104 if (i == string::npos)
1108 static InsetLayout empty;
1109 empty.labelstring = from_utf8("UNDEFINED");
1110 empty.labelfont = sane_font;
1111 empty.labelfont.setColor(Color_error);
1112 empty.bgcolor = Color_error;
1117 docstring const & TextClass::defaultLayoutName() const
1119 // This really should come from the actual layout... (Lgb)
1120 return defaultlayout_;
1124 LayoutPtr const & TextClass::defaultLayout() const
1126 return operator[](defaultLayoutName());
1130 string const & TextClass::name() const
1136 string const & TextClass::latexname() const
1138 const_cast<TextClass*>(this)->load();
1143 string const & TextClass::description() const
1145 return description_;
1149 string const & TextClass::opt_fontsize() const
1151 return opt_fontsize_;
1155 string const & TextClass::opt_pagestyle() const
1157 return opt_pagestyle_;
1161 string const & TextClass::options() const
1167 string const & TextClass::class_header() const
1169 return class_header_;
1173 string const & TextClass::pagestyle() const
1179 docstring const & TextClass::preamble() const
1185 PageSides TextClass::sides() const
1191 int TextClass::secnumdepth() const
1193 return secnumdepth_;
1197 int TextClass::tocdepth() const
1203 OutputType TextClass::outputType() const
1209 bool TextClass::provides(string const & p) const
1211 return provides_.find(p) != provides_.end();
1215 unsigned int TextClass::columns() const
1221 TitleLatexType TextClass::titletype() const
1227 string const & TextClass::titlename() const
1233 int TextClass::size() const
1235 return layoutlist_.size();
1239 int TextClass::min_toclevel() const
1241 return min_toclevel_;
1245 int TextClass::max_toclevel() const
1247 return max_toclevel_;
1251 bool TextClass::hasTocLevels() const
1253 return min_toclevel_ != Layout::NOT_IN_TOC;
1257 ostream & operator<<(ostream & os, PageSides p)