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"
24 #include "FloatList.h"
28 #include "frontends/alert.h"
30 #include "support/lstrings.h"
31 #include "support/lyxlib.h"
32 #include "support/filetools.h"
33 #include "support/os.h"
45 using support::FileName;
46 using support::libFileSearch;
47 using support::makeDisplayPath;
48 using support::quoteName;
51 using support::addName;
53 extern FontInfo lyxRead(Lexer &, FontInfo const & fi = sane_font);
57 class LayoutNamesEqual : public std::unary_function<LayoutPtr, bool> {
59 LayoutNamesEqual(docstring const & name)
62 bool operator()(LayoutPtr const & c) const
64 return c->name() == name_;
74 bool layout2layout(FileName const & filename, FileName const & tempfile)
76 FileName const script = libFileSearch("scripts", "layout2layout.py");
78 lyxerr << "Could not find layout conversion "
79 "script layout2layout.py." << endl;
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();
89 LYXERR(Debug::TCLASS) << "Running `" << command_str << '\'' << endl;
91 support::cmd_ret const ret =
92 support::runCommand(command_str);
94 lyxerr << "Could not run layout conversion "
95 "script layout2layout.py." << endl;
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)
116 pagestyle_ = "default";
117 defaultfont_ = sane_font;
118 opt_fontsize_ = "10|11|12";
119 opt_pagestyle_ = "empty|plain|headings|fancy";
120 titletype_ = TITLE_COMMAND_AFTER;
121 titlename_ = "maketitle";
126 bool TextClass::isTeXClassAvailable() const
128 return texClassAvail_;
132 bool TextClass::readStyle(Lexer & lexrc, Layout & lay)
134 LYXERR(Debug::TCLASS) << "Reading style " << to_utf8(lay.name()) << endl;
135 if (!lay.read(lexrc, *this)) {
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
143 lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
176 // Reads a textclass structure from file.
177 bool TextClass::read(FileName const & filename, ReadType rt)
179 if (!filename.isFileReadable()) {
180 lyxerr << "Cannot read layout file `" << filename << "'."
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 }
214 LYXERR(Debug::TCLASS) << "Reading textclass ";
217 LYXERR(Debug::TCLASS) << "Reading input file ";
220 LYXERR(Debug::TCLASS) << "Reading module file ";
225 LYXERR(Debug::TCLASS) << to_utf8(makeDisplayPath(filename.absFilename()))
228 Lexer lexrc(textClassTags,
229 sizeof(textClassTags) / sizeof(textClassTags[0]));
231 lexrc.setFile(filename);
232 bool error = !lexrc.isOK();
234 // Format of files before the 'Format' tag was introduced
238 while (lexrc.isOK() && !error) {
239 int le = lexrc.lex();
242 case Lexer::LEX_FEOF:
245 case Lexer::LEX_UNDEF:
246 lexrc.printError("Unknown TextClass tag `$$Token'");
254 switch (static_cast<TextClassTags>(le)) {
258 format = lexrc.getInteger();
261 case TC_OUTPUTTYPE: // output type definition
262 readOutputType(lexrc);
265 case TC_INPUT: // Include file
267 string const inc = lexrc.getString();
268 FileName tmp = libFileSearch("layouts", inc,
272 lexrc.printError("Could not find input"
275 } else if (read(tmp, MERGE)) {
276 lexrc.printError("Error reading input"
277 "file: " + tmp.absFilename());
283 case TC_DEFAULTSTYLE:
285 docstring const name = from_utf8(subst(lexrc.getString(),
287 defaultlayout_ = name;
294 docstring const name = from_utf8(subst(lexrc.getString(),
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());
301 error = readStyle(lexrc, lay);
302 } else if (hasLayout(name)) {
303 Layout * lay = operator[](name).get();
304 error = readStyle(lexrc, *lay);
308 if (le == TC_ENVIRONMENT)
309 lay.is_environment = true;
310 error = readStyle(lexrc, lay);
312 layoutlist_.push_back(
313 boost::shared_ptr<Layout>(new Layout(lay))
316 if (defaultlayout_.empty()) {
317 // We do not have a default
318 // layout yet, so we choose
319 // the first layout we
321 defaultlayout_ = name;
326 lexrc.printError("No name given for style: `$$Token'.");
333 docstring const style = from_utf8(subst(lexrc.getString(),
335 if (!deleteLayout(style))
336 lyxerr << "Cannot delete style `"
337 << to_utf8(style) << '\'' << endl;
338 // lexrc.printError("Cannot delete style"
345 columns_ = lexrc.getInteger();
350 switch (lexrc.getInteger()) {
351 case 1: sides_ = OneSide; break;
352 case 2: sides_ = TwoSides; break;
354 lyxerr << "Impossible number of page"
355 " sides, setting to one."
365 pagestyle_ = rtrim(lexrc.getString());
369 defaultfont_ = lyxRead(lexrc);
370 if (!defaultfont_.resolved()) {
371 lexrc.printError("Warning: defaultfont should "
372 "be fully instantiated!");
373 defaultfont_.realize(sane_font);
379 secnumdepth_ = lexrc.getInteger();
384 tocdepth_ = lexrc.getInteger();
387 // First step to support options
388 case TC_CLASSOPTIONS:
389 readClassOptions(lexrc);
393 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
398 string const feature = lexrc.getString();
400 if (lexrc.getInteger())
401 provides_.insert(feature);
403 provides_.erase(feature);
407 case TC_LEFTMARGIN: // left margin type
409 leftmargin_ = lexrc.getDocString();
412 case TC_RIGHTMARGIN: // right margin type
414 rightmargin_ = lexrc.getDocString();
418 docstring const name = subst(lexrc.getDocString(), '_', ' ');
419 readInsetLayout(lexrc, name);
428 case TC_TITLELATEXTYPE:
429 readTitleType(lexrc);
431 case TC_TITLELATEXNAME:
433 titlename_ = lexrc.getString();
437 string const nofloat = lexrc.getString();
438 floatlist_->erase(nofloat);
442 if (format != FORMAT)
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);
452 error = read(tempfile, rt);
453 support::unlink(tempfile);
458 LYXERR(Debug::TCLASS) << "Finished reading module file "
459 << to_utf8(makeDisplayPath(filename.absFilename()))
461 else if (rt == MERGE)
462 LYXERR(Debug::TCLASS) << "Finished reading input file "
463 << to_utf8(makeDisplayPath(filename.absFilename()))
465 else { // we are at top level here.
466 LYXERR(Debug::TCLASS) << "Finished reading textclass "
467 << to_utf8(makeDisplayPath(filename.absFilename()))
469 if (defaultlayout_.empty()) {
470 lyxerr << "Error: Textclass '" << name_
471 << "' is missing a defaultstyle." << endl;
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;
485 min_toclevel_ = std::min(min_toclevel_,
487 max_toclevel_ = std::max(max_toclevel_,
491 LYXERR(Debug::TCLASS)
492 << "Minimum TocLevel is " << min_toclevel_
493 << ", maximum is " << max_toclevel_ <<endl;
501 void TextClass::readTitleType(Lexer & lexrc)
503 keyword_item titleTypeTags[] = {
504 { "commandafter", TITLE_COMMAND_AFTER },
505 { "environment", TITLE_ENVIRONMENT }
508 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
510 int le = lexrc.lex();
512 case Lexer::LEX_UNDEF:
513 lexrc.printError("Unknown output type `$$Token'");
515 case TITLE_COMMAND_AFTER:
516 case TITLE_ENVIRONMENT:
517 titletype_ = static_cast<TitleLatexType>(le);
520 lyxerr << "Unhandled value " << le
521 << " in TextClass::readTitleType." << endl;
528 void TextClass::readOutputType(Lexer & lexrc)
530 keyword_item outputTypeTags[] = {
531 { "docbook", DOCBOOK },
533 { "literate", LITERATE }
536 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
538 int le = lexrc.lex();
540 case Lexer::LEX_UNDEF:
541 lexrc.printError("Unknown output type `$$Token'");
546 outputType_ = static_cast<OutputType>(le);
549 lyxerr << "Unhandled value " << le
550 << " in TextClass::readOutputType." << endl;
557 enum ClassOptionsTags {
566 void TextClass::readClassOptions(Lexer & lexrc)
568 keyword_item classOptionsTags[] = {
570 {"fontsize", CO_FONTSIZE },
571 {"header", CO_HEADER },
572 {"other", CO_OTHER },
573 {"pagestyle", CO_PAGESTYLE }
576 lexrc.pushTable(classOptionsTags, CO_END);
578 while (!getout && lexrc.isOK()) {
579 int le = lexrc.lex();
581 case Lexer::LEX_UNDEF:
582 lexrc.printError("Unknown ClassOption tag `$$Token'");
586 switch (static_cast<ClassOptionsTags>(le)) {
589 opt_fontsize_ = rtrim(lexrc.getString());
593 opt_pagestyle_ = rtrim(lexrc.getString());
597 options_ = lexrc.getString();
601 class_header_ = subst(lexrc.getString(), """, "\"");
612 enum InsetLayoutTags {
633 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
635 keyword_item elementTags[] = {
636 { "bgcolor", IL_BGCOLOR },
637 { "decoration", IL_DECORATION },
640 { "forceltr", IL_FORCELTR },
641 { "freespacing", IL_FREESPACING },
642 { "keepempty", IL_KEEPEMPTY },
643 { "labelfont", IL_LABELFONT },
644 { "labelstring", IL_LABELSTRING },
645 { "latexname", IL_LATEXNAME },
646 { "latexparam", IL_LATEXPARAM },
647 { "latextype", IL_LATEXTYPE },
648 { "lyxtype", IL_LYXTYPE },
649 { "multipar", IL_MULTIPAR },
650 { "needprotect", IL_NEEDPROTECT },
651 { "passthru", IL_PASSTHRU },
652 { "preamble", IL_PREAMBLE }
655 lexrc.pushTable(elementTags, IL_END);
658 docstring labelstring;
663 FontInfo font = inherit_font;
664 FontInfo labelfont = inherit_font;
665 ColorCode bgcolor(Color_background);
667 bool multipar = false;
668 bool passthru = false;
669 bool needprotect = false;
670 bool keepempty = false;
671 bool freespacing = false;
672 bool forceltr = false;
675 while (!getout && lexrc.isOK()) {
676 int le = lexrc.lex();
678 case Lexer::LEX_UNDEF:
679 lexrc.printError("Unknown ClassOption tag `$$Token'");
683 switch (static_cast<InsetLayoutTags>(le)) {
686 lyxtype = lexrc.getString();
690 latextype = lexrc.getString();
694 labelstring = lexrc.getDocString();
698 decoration = lexrc.getString();
702 latexname = lexrc.getString();
706 latexparam = subst(lexrc.getString(), """, "\"");
709 labelfont = lyxRead(lexrc, inherit_font);
713 forceltr = lexrc.getBool();
717 multipar = lexrc.getBool();
721 passthru = lexrc.getBool();
725 keepempty = lexrc.getBool();
729 freespacing = lexrc.getBool();
733 needprotect = lexrc.getBool();
736 font = lyxRead(lexrc, inherit_font);
737 // So: define font before labelfont
742 string const token = lexrc.getString();
743 bgcolor = lcolor.getFromLyXName(token);
747 preamble = lexrc.getLongString("EndPreamble");
755 // Here add element to list if getout == true
758 il.name = to_ascii(name);
759 il.lyxtype = lyxtype;
760 il.labelstring = labelstring;
761 il.decoration = decoration;
762 il.latextype = latextype;
763 il.latexname = latexname;
764 il.latexparam = latexparam;
765 il.multipar = multipar;
766 il.passthru = passthru;
767 il.needprotect = needprotect;
768 il.freespacing = freespacing;
769 il.forceltr = forceltr;
770 il.keepempty = keepempty;
772 // The label font is generally used as-is without
773 // any realization against a given context.
774 labelfont.realize(sane_font);
775 il.labelfont = labelfont;
776 il.bgcolor = bgcolor;
777 il.preamble = preamble;
778 insetlayoutlist_[name] = il;
799 void TextClass::readFloat(Lexer & lexrc)
801 keyword_item floatTags[] = {
803 { "extension", FT_EXT },
804 { "guiname", FT_NAME },
805 { "latexbuiltin", FT_BUILTIN },
806 { "listname", FT_LISTNAME },
807 { "numberwithin", FT_WITHIN },
808 { "placement", FT_PLACEMENT },
809 { "style", FT_STYLE },
813 lexrc.pushTable(floatTags, FT_END);
822 bool builtin = false;
825 while (!getout && lexrc.isOK()) {
826 int le = lexrc.lex();
828 case Lexer::LEX_UNDEF:
829 lexrc.printError("Unknown ClassOption tag `$$Token'");
833 switch (static_cast<FloatTags>(le)) {
836 type = lexrc.getString();
837 if (floatlist_->typeExist(type)) {
838 Floating const & fl = floatlist_->getType(type);
839 placement = fl.placement();
841 within = fl.within();
844 listName = fl.listName();
845 builtin = fl.builtin();
850 name = lexrc.getString();
854 placement = lexrc.getString();
858 ext = lexrc.getString();
862 within = lexrc.getString();
863 if (within == "none")
868 style = lexrc.getString();
872 listName = lexrc.getString();
876 builtin = lexrc.getBool();
884 // Here if have a full float if getout == true
886 Floating fl(type, placement, ext, within,
887 style, name, listName, builtin);
888 floatlist_->newFloat(fl);
889 // each float has its own counter
890 counters_->newCounter(from_ascii(type), from_ascii(within),
891 docstring(), docstring());
902 CT_LABELSTRING_APPENDIX,
906 void TextClass::readCounter(Lexer & lexrc)
908 keyword_item counterTags[] = {
910 { "labelstring", CT_LABELSTRING },
911 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
913 { "within", CT_WITHIN }
916 lexrc.pushTable(counterTags, CT_END);
920 docstring labelstring;
921 docstring labelstring_appendix;
924 while (!getout && lexrc.isOK()) {
925 int le = lexrc.lex();
927 case Lexer::LEX_UNDEF:
928 lexrc.printError("Unknown ClassOption tag `$$Token'");
932 switch (static_cast<CounterTags>(le)) {
935 name = lexrc.getDocString();
936 if (counters_->hasCounter(name))
937 LYXERR(Debug::TCLASS)
938 << "Reading existing counter "
939 << to_utf8(name) << endl;
941 LYXERR(Debug::TCLASS)
942 << "Reading new counter "
943 << to_utf8(name) << endl;
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)