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"
23 #include "FloatList.h"
25 #include "frontends/alert.h"
27 #include "support/lstrings.h"
28 #include "support/lyxlib.h"
29 #include "support/filetools.h"
30 #include "support/os.h"
32 #include <boost/filesystem/operations.hpp>
33 namespace fs = boost::filesystem;
40 using support::FileName;
41 using support::libFileSearch;
42 using support::makeDisplayPath;
43 using support::quoteName;
46 using support::addName;
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_ = 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";
126 bool TextClass::isTeXClassAvailable() const
128 return texClassAvail_;
132 bool TextClass::do_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 (!support::isFileReadable(filename)) {
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 = do_readStyle(lexrc, lay);
302 } else if (hasLayout(name)) {
303 Layout * lay = operator[](name).get();
304 error = do_readStyle(lexrc, *lay);
308 if (le == TC_ENVIRONMENT)
309 lay.is_environment = true;
310 error = do_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 (!delete_layout(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(Font(Font::ALL_SANE));
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<LYX_TITLE_LATEX_TYPES>(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 {
627 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
629 keyword_item elementTags[] = {
630 { "bgcolor", IL_BGCOLOR },
631 { "decoration", IL_DECORATION },
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 }
643 lexrc.pushTable(elementTags, IL_END);
646 docstring labelstring;
651 Font font(Font::ALL_INHERIT);
652 Font labelfont(Font::ALL_INHERIT);
653 Color::color bgcolor(Color::background);
657 while (!getout && lexrc.isOK()) {
658 int le = lexrc.lex();
660 case Lexer::LEX_UNDEF:
661 lexrc.printError("Unknown ClassOption tag `$$Token'");
665 switch (static_cast<InsetLayoutTags>(le)) {
668 lyxtype = lexrc.getString();
672 latextype = lexrc.getString();
676 labelstring = lexrc.getDocString();
680 decoration = lexrc.getString();
684 latexname = lexrc.getString();
688 latexparam = subst(lexrc.getString(), """, "\"");
691 labelfont.lyxRead(lexrc);
692 labelfont.realize(defaultfont());
696 font.realize(defaultfont());
697 // So: define font before labelfont
702 string const token = lexrc.getString();
703 bgcolor = lcolor.getFromLyXName(token);
707 preamble = lexrc.getLongString("EndPreamble");
716 // Here add element to list if getout == true
719 il.name = to_ascii(name);
720 il.lyxtype = lyxtype;
721 il.labelstring = labelstring;
722 il.decoration = decoration;
723 il.latextype = latextype;
724 il.latexname = latexname;
725 il.latexparam = latexparam;
727 il.labelfont = labelfont;
728 il.bgcolor = bgcolor;
729 il.preamble = preamble;
730 insetlayoutlist_[name] = il;
751 void TextClass::readFloat(Lexer & lexrc)
753 keyword_item floatTags[] = {
755 { "extension", FT_EXT },
756 { "guiname", FT_NAME },
757 { "latexbuiltin", FT_BUILTIN },
758 { "listname", FT_LISTNAME },
759 { "numberwithin", FT_WITHIN },
760 { "placement", FT_PLACEMENT },
761 { "style", FT_STYLE },
765 lexrc.pushTable(floatTags, FT_END);
774 bool builtin = false;
777 while (!getout && lexrc.isOK()) {
778 int le = lexrc.lex();
780 case Lexer::LEX_UNDEF:
781 lexrc.printError("Unknown ClassOption tag `$$Token'");
785 switch (static_cast<FloatTags>(le)) {
788 type = lexrc.getString();
789 if (floatlist_->typeExist(type)) {
790 Floating const & fl = floatlist_->getType(type);
791 placement = fl.placement();
793 within = fl.within();
796 listName = fl.listName();
797 builtin = fl.builtin();
802 name = lexrc.getString();
806 placement = lexrc.getString();
810 ext = lexrc.getString();
814 within = lexrc.getString();
815 if (within == "none")
820 style = lexrc.getString();
824 listName = lexrc.getString();
828 builtin = lexrc.getBool();
836 // Here if have a full float if getout == true
838 Floating fl(type, placement, ext, within,
839 style, name, listName, builtin);
840 floatlist_->newFloat(fl);
841 // each float has its own counter
842 counters_->newCounter(from_ascii(type), from_ascii(within),
843 docstring(), docstring());
854 CT_LABELSTRING_APPENDIX,
858 void TextClass::readCounter(Lexer & lexrc)
860 keyword_item counterTags[] = {
862 { "labelstring", CT_LABELSTRING },
863 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
865 { "within", CT_WITHIN }
868 lexrc.pushTable(counterTags, CT_END);
872 docstring labelstring;
873 docstring labelstring_appendix;
876 while (!getout && lexrc.isOK()) {
877 int le = lexrc.lex();
879 case Lexer::LEX_UNDEF:
880 lexrc.printError("Unknown ClassOption tag `$$Token'");
884 switch (static_cast<CounterTags>(le)) {
887 name = lexrc.getDocString();
888 if (counters_->hasCounter(name))
889 LYXERR(Debug::TCLASS)
890 << "Reading existing counter "
891 << to_utf8(name) << endl;
893 LYXERR(Debug::TCLASS)
894 << "Reading new counter "
895 << to_utf8(name) << endl;
899 within = lexrc.getDocString();
900 if (within == "none")
905 labelstring = lexrc.getDocString();
906 labelstring_appendix = labelstring;
908 case CT_LABELSTRING_APPENDIX:
910 labelstring_appendix = lexrc.getDocString();
918 // Here if have a full counter if getout == true
920 counters_->newCounter(name, within,
921 labelstring, labelstring_appendix);
927 Font const & TextClass::defaultfont() const
933 docstring const & TextClass::leftmargin() const
939 docstring const & TextClass::rightmargin() const
945 bool TextClass::hasLayout(docstring const & n) const
947 docstring const name = n.empty() ? defaultLayoutName() : n;
949 return find_if(layoutlist_.begin(), layoutlist_.end(),
950 LayoutNamesEqual(name))
951 != layoutlist_.end();
956 LayoutPtr const & TextClass::operator[](docstring const & name) const
958 BOOST_ASSERT(!name.empty());
960 LayoutList::const_iterator cit =
961 find_if(layoutlist_.begin(),
963 LayoutNamesEqual(name));
965 if (cit == layoutlist_.end()) {
966 lyxerr << "We failed to find the layout '" << to_utf8(name)
967 << "' in the layout list. You MUST investigate!"
969 for (LayoutList::const_iterator it = layoutlist_.begin();
970 it != layoutlist_.end(); ++it)
971 lyxerr << " " << to_utf8(it->get()->name()) << endl;
973 // we require the name to exist
982 bool TextClass::delete_layout(docstring const & name)
984 if (name == defaultLayoutName())
987 LayoutList::iterator it =
988 remove_if(layoutlist_.begin(), layoutlist_.end(),
989 LayoutNamesEqual(name));
991 LayoutList::iterator end = layoutlist_.end();
992 bool const ret = (it != end);
993 layoutlist_.erase(it, end);
998 // Load textclass info if not loaded yet
999 bool TextClass::load(string const & path) const
1004 // Read style-file, provided path is searched before the system ones
1005 FileName layout_file;
1007 layout_file = FileName(addName(path, name_ + ".layout"));
1008 if (layout_file.empty() || !fs::exists(layout_file.toFilesystemEncoding()))
1009 layout_file = libFileSearch("layouts", name_, "layout");
1010 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1013 lyxerr << "Error reading `"
1014 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1015 << "'\n(Check `" << name_
1016 << "')\nCheck your installation and "
1017 "try Options/Reconfigure..." << endl;
1024 FloatList & TextClass::floats()
1026 return *floatlist_.get();
1030 FloatList const & TextClass::floats() const
1032 return *floatlist_.get();
1036 Counters & TextClass::counters() const
1038 return *counters_.get();
1042 // Return the layout object of an inset given by name. If the name
1043 // is not found as such, the part after the ':' is stripped off, and
1044 // searched again. In this way, an error fallback can be provided:
1045 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1046 // will invoke the layout object defined by name = 'CharStyle'.
1047 // If that doesn't work either, an empty object returns (shouldn't
1048 // happen). -- Idea JMarc, comment MV
1049 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1052 while (!n.empty()) {
1053 if (insetlayoutlist_.count(n) > 0)
1054 return insetlayoutlist_[n];
1055 docstring::size_type i = n.find(':');
1056 if (i == string::npos)
1060 static InsetLayout empty;
1061 empty.labelstring = from_utf8("UNDEFINED");
1062 empty.bgcolor = Color::error;
1067 docstring const & TextClass::defaultLayoutName() const
1069 // This really should come from the actual layout... (Lgb)
1070 return defaultlayout_;
1074 LayoutPtr const & TextClass::defaultLayout() const
1076 return operator[](defaultLayoutName());
1080 string const & TextClass::name() const
1086 string const & TextClass::latexname() const
1088 const_cast<TextClass*>(this)->load();
1093 string const & TextClass::description() const
1095 return description_;
1099 string const & TextClass::opt_fontsize() const
1101 return opt_fontsize_;
1105 string const & TextClass::opt_pagestyle() const
1107 return opt_pagestyle_;
1111 string const & TextClass::options() const
1117 string const & TextClass::class_header() const
1119 return class_header_;
1123 string const & TextClass::pagestyle() const
1129 docstring const & TextClass::preamble() const
1135 TextClass::PageSides TextClass::sides() const
1141 int TextClass::secnumdepth() const
1143 return secnumdepth_;
1147 int TextClass::tocdepth() const
1153 OutputType TextClass::outputType() const
1159 bool TextClass::provides(string const & p) const
1161 return provides_.find(p) != provides_.end();
1165 unsigned int TextClass::columns() const
1171 LYX_TITLE_LATEX_TYPES TextClass::titletype() const
1177 string const & TextClass::titlename() const
1183 int TextClass::size() const
1185 return layoutlist_.size();
1189 int TextClass::min_toclevel() const
1191 return min_toclevel_;
1195 int TextClass::max_toclevel() const
1197 return max_toclevel_;
1201 bool TextClass::hasTocLevels() const
1203 return min_toclevel_ != Layout::NOT_IN_TOC;
1207 ostream & operator<<(ostream & os, TextClass::PageSides p)
1210 case TextClass::OneSide:
1213 case TextClass::TwoSides: