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"
40 using namespace lyx::support;
46 class LayoutNamesEqual : public unary_function<LayoutPtr, bool> {
48 LayoutNamesEqual(docstring const & name)
51 bool operator()(LayoutPtr const & c) const
53 return c->name() == name_;
63 bool layout2layout(FileName const & filename, FileName const & tempfile)
65 FileName const script = libFileSearch("scripts", "layout2layout.py");
67 lyxerr << "Could not find layout conversion "
68 "script layout2layout.py." << endl;
72 ostringstream command;
73 command << os::python() << ' ' << quoteName(script.toFilesystemEncoding())
74 << ' ' << quoteName(filename.toFilesystemEncoding())
75 << ' ' << quoteName(tempfile.toFilesystemEncoding());
76 string const command_str = command.str();
78 LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
81 runCommand(command_str);
83 lyxerr << "Could not run layout conversion "
84 "script layout2layout.py." << endl;
93 TextClass::TextClass(string const & fn, string const & cln,
94 string const & desc, bool texClassAvail )
95 : name_(fn), latexname_(cln), description_(desc),
96 floatlist_(new FloatList), counters_(new Counters),
97 texClassAvail_(texClassAvail)
105 pagestyle_ = "default";
106 defaultfont_ = sane_font;
107 opt_fontsize_ = "10|11|12";
108 opt_pagestyle_ = "empty|plain|headings|fancy";
109 titletype_ = TITLE_COMMAND_AFTER;
110 titlename_ = "maketitle";
115 bool TextClass::isTeXClassAvailable() const
117 return texClassAvail_;
121 bool TextClass::readStyle(Lexer & lexrc, Layout & lay)
123 LYXERR(Debug::TCLASS, "Reading style " << to_utf8(lay.name()));
124 if (!lay.read(lexrc, *this)) {
126 lay.resfont = lay.font;
127 lay.resfont.realize(defaultfont());
128 lay.reslabelfont = lay.labelfont;
129 lay.reslabelfont.realize(defaultfont());
130 return false; // no errors
132 lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
166 // Reads a textclass structure from file.
167 bool TextClass::read(FileName const & filename, ReadType rt)
169 if (!filename.isReadableFile()) {
170 lyxerr << "Cannot read layout file `" << filename << "'."
175 keyword_item textClassTags[] = {
176 { "classoptions", TC_CLASSOPTIONS },
177 { "columns", TC_COLUMNS },
178 { "counter", TC_COUNTER },
179 { "defaultfont", TC_DEFAULTFONT },
180 { "defaultstyle", TC_DEFAULTSTYLE },
181 { "environment", TC_ENVIRONMENT },
182 { "float", TC_FLOAT },
183 { "format", TC_FORMAT },
184 { "input", TC_INPUT },
185 { "insetlayout", TC_INSETLAYOUT },
186 { "leftmargin", TC_LEFTMARGIN },
187 { "nofloat", TC_NOFLOAT },
188 { "nostyle", TC_NOSTYLE },
189 { "outputtype", TC_OUTPUTTYPE },
190 { "pagestyle", TC_PAGESTYLE },
191 { "preamble", TC_PREAMBLE },
192 { "provides", TC_PROVIDES },
193 { "requires", TC_REQUIRES },
194 { "rightmargin", TC_RIGHTMARGIN },
195 { "secnumdepth", TC_SECNUMDEPTH },
196 { "sides", TC_SIDES },
197 { "style", TC_STYLE },
198 { "titlelatexname", TC_TITLELATEXNAME },
199 { "titlelatextype", TC_TITLELATEXTYPE },
200 { "tocdepth", TC_TOCDEPTH }
205 LYXERR(Debug::TCLASS, "Reading textclass ");
208 LYXERR(Debug::TCLASS, "Reading input file ");
211 LYXERR(Debug::TCLASS, "Reading module file ");
216 LYXERR(Debug::TCLASS, to_utf8(makeDisplayPath(filename.absFilename())));
218 Lexer lexrc(textClassTags,
219 sizeof(textClassTags) / sizeof(textClassTags[0]));
221 lexrc.setFile(filename);
222 bool error = !lexrc.isOK();
224 // Format of files before the 'Format' tag was introduced
228 while (lexrc.isOK() && !error) {
229 int le = lexrc.lex();
232 case Lexer::LEX_FEOF:
235 case Lexer::LEX_UNDEF:
236 lexrc.printError("Unknown TextClass tag `$$Token'");
244 switch (static_cast<TextClassTags>(le)) {
248 format = lexrc.getInteger();
251 case TC_OUTPUTTYPE: // output type definition
252 readOutputType(lexrc);
255 case TC_INPUT: // Include file
257 string const inc = lexrc.getString();
258 FileName tmp = libFileSearch("layouts", inc,
262 lexrc.printError("Could not find input"
265 } else if (read(tmp, MERGE)) {
266 lexrc.printError("Error reading input"
267 "file: " + tmp.absFilename());
273 case TC_DEFAULTSTYLE:
275 docstring const name = from_utf8(subst(lexrc.getString(),
277 defaultlayout_ = name;
284 docstring const name = from_utf8(subst(lexrc.getString(),
287 string s = "Could not read name for style: `$$Token' "
288 + lexrc.getString() + " is probably not valid UTF-8!";
289 lexrc.printError(s.c_str());
291 error = readStyle(lexrc, lay);
292 } else if (hasLayout(name)) {
293 Layout * lay = operator[](name).get();
294 error = readStyle(lexrc, *lay);
298 if (le == TC_ENVIRONMENT)
299 lay.is_environment = true;
300 error = readStyle(lexrc, lay);
302 layoutlist_.push_back(
303 boost::shared_ptr<Layout>(new Layout(lay))
306 if (defaultlayout_.empty()) {
307 // We do not have a default
308 // layout yet, so we choose
309 // the first layout we
311 defaultlayout_ = name;
316 lexrc.printError("No name given for style: `$$Token'.");
323 docstring const style = from_utf8(subst(lexrc.getString(),
325 if (!deleteLayout(style))
326 lyxerr << "Cannot delete style `"
327 << to_utf8(style) << '\'' << endl;
328 // lexrc.printError("Cannot delete style"
335 columns_ = lexrc.getInteger();
340 switch (lexrc.getInteger()) {
341 case 1: sides_ = OneSide; break;
342 case 2: sides_ = TwoSides; break;
344 lyxerr << "Impossible number of page"
345 " sides, setting to one."
355 pagestyle_ = rtrim(lexrc.getString());
359 defaultfont_ = lyxRead(lexrc);
360 if (!defaultfont_.resolved()) {
361 lexrc.printError("Warning: defaultfont should "
362 "be fully instantiated!");
363 defaultfont_.realize(sane_font);
369 secnumdepth_ = lexrc.getInteger();
374 tocdepth_ = lexrc.getInteger();
377 // First step to support options
378 case TC_CLASSOPTIONS:
379 readClassOptions(lexrc);
383 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
388 string const feature = lexrc.getString();
390 if (lexrc.getInteger())
391 provides_.insert(feature);
393 provides_.erase(feature);
399 string const packages = lexrc.getString();
400 vector<string> req = getVectorFromString(packages);
401 if (requires_.empty()) {
405 for (vector<string>::const_iterator it = req.begin();
406 it != req.end(); ++it) {
407 if (find(requires_.begin(), requires_.end(), *it) == requires_.end())
408 requires_.push_back(*it);
413 case TC_LEFTMARGIN: // left margin type
415 leftmargin_ = lexrc.getDocString();
418 case TC_RIGHTMARGIN: // right margin type
420 rightmargin_ = lexrc.getDocString();
424 docstring const name = subst(lexrc.getDocString(), '_', ' ');
425 readInsetLayout(lexrc, name);
434 case TC_TITLELATEXTYPE:
435 readTitleType(lexrc);
437 case TC_TITLELATEXNAME:
439 titlename_ = lexrc.getString();
443 string const nofloat = lexrc.getString();
444 floatlist_->erase(nofloat);
448 if (format != FORMAT)
452 if (format != FORMAT) {
453 LYXERR(Debug::TCLASS, "Converting layout file from format "
454 << format << " to " << FORMAT);
455 FileName const tempfile = FileName::tempName();
456 error = !layout2layout(filename, tempfile);
458 error = read(tempfile, rt);
459 tempfile.removeFile();
464 LYXERR(Debug::TCLASS, "Finished reading module file "
465 << to_utf8(makeDisplayPath(filename.absFilename())));
466 else if (rt == MERGE)
467 LYXERR(Debug::TCLASS, "Finished reading input file "
468 << to_utf8(makeDisplayPath(filename.absFilename())));
469 else { // we are at top level here.
470 LYXERR(Debug::TCLASS, "Finished reading textclass "
471 << to_utf8(makeDisplayPath(filename.absFilename())));
472 if (defaultlayout_.empty()) {
473 lyxerr << "Error: Textclass '" << name_
474 << "' is missing a defaultstyle." << endl;
478 min_toclevel_ = Layout::NOT_IN_TOC;
479 max_toclevel_ = Layout::NOT_IN_TOC;
480 const_iterator cit = begin();
481 const_iterator the_end = end();
482 for ( ; cit != the_end ; ++cit) {
483 int const toclevel = (*cit)->toclevel;
484 if (toclevel != Layout::NOT_IN_TOC) {
485 if (min_toclevel_ == Layout::NOT_IN_TOC)
486 min_toclevel_ = toclevel;
488 min_toclevel_ = min(min_toclevel_,
490 max_toclevel_ = max(max_toclevel_,
494 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
495 << ", maximum is " << max_toclevel_);
503 void TextClass::readTitleType(Lexer & lexrc)
505 keyword_item titleTypeTags[] = {
506 { "commandafter", TITLE_COMMAND_AFTER },
507 { "environment", TITLE_ENVIRONMENT }
510 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
512 int le = lexrc.lex();
514 case Lexer::LEX_UNDEF:
515 lexrc.printError("Unknown output type `$$Token'");
517 case TITLE_COMMAND_AFTER:
518 case TITLE_ENVIRONMENT:
519 titletype_ = static_cast<TitleLatexType>(le);
522 lyxerr << "Unhandled value " << le
523 << " in TextClass::readTitleType." << endl;
530 void TextClass::readOutputType(Lexer & lexrc)
532 keyword_item outputTypeTags[] = {
533 { "docbook", DOCBOOK },
535 { "literate", LITERATE }
538 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
540 int le = lexrc.lex();
542 case Lexer::LEX_UNDEF:
543 lexrc.printError("Unknown output type `$$Token'");
548 outputType_ = static_cast<OutputType>(le);
551 lyxerr << "Unhandled value " << le
552 << " in TextClass::readOutputType." << endl;
559 enum ClassOptionsTags {
568 void TextClass::readClassOptions(Lexer & lexrc)
570 keyword_item classOptionsTags[] = {
572 {"fontsize", CO_FONTSIZE },
573 {"header", CO_HEADER },
574 {"other", CO_OTHER },
575 {"pagestyle", CO_PAGESTYLE }
578 lexrc.pushTable(classOptionsTags, CO_END);
580 while (!getout && lexrc.isOK()) {
581 int le = lexrc.lex();
583 case Lexer::LEX_UNDEF:
584 lexrc.printError("Unknown ClassOption tag `$$Token'");
588 switch (static_cast<ClassOptionsTags>(le)) {
591 opt_fontsize_ = rtrim(lexrc.getString());
595 opt_pagestyle_ = rtrim(lexrc.getString());
599 options_ = lexrc.getString();
603 class_header_ = subst(lexrc.getString(), """, "\"");
614 enum InsetLayoutTags {
636 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
638 keyword_item elementTags[] = {
639 { "bgcolor", IL_BGCOLOR },
640 { "decoration", IL_DECORATION },
643 { "forceltr", IL_FORCELTR },
644 { "freespacing", IL_FREESPACING },
645 { "keepempty", IL_KEEPEMPTY },
646 { "labelfont", IL_LABELFONT },
647 { "labelstring", IL_LABELSTRING },
648 { "latexname", IL_LATEXNAME },
649 { "latexparam", IL_LATEXPARAM },
650 { "latextype", IL_LATEXTYPE },
651 { "lyxtype", IL_LYXTYPE },
652 { "multipar", IL_MULTIPAR },
653 { "needprotect", IL_NEEDPROTECT },
654 { "passthru", IL_PASSTHRU },
655 { "preamble", IL_PREAMBLE },
656 { "requires", IL_REQUIRES }
659 lexrc.pushTable(elementTags, IL_END);
662 docstring labelstring;
667 FontInfo font = inherit_font;
668 FontInfo labelfont = inherit_font;
669 ColorCode bgcolor(Color_background);
671 vector<string> requires;
672 bool multipar = false;
673 bool passthru = false;
674 bool needprotect = false;
675 bool keepempty = false;
676 bool freespacing = false;
677 bool forceltr = false;
680 while (!getout && lexrc.isOK()) {
681 int le = lexrc.lex();
683 case Lexer::LEX_UNDEF:
684 lexrc.printError("Unknown InsetLayout tag `$$Token'");
688 switch (static_cast<InsetLayoutTags>(le)) {
691 lyxtype = lexrc.getString();
695 latextype = lexrc.getString();
699 labelstring = lexrc.getDocString();
703 decoration = lexrc.getString();
707 latexname = lexrc.getString();
711 latexparam = subst(lexrc.getString(), """, "\"");
714 labelfont = lyxRead(lexrc, inherit_font);
718 forceltr = lexrc.getBool();
722 multipar = lexrc.getBool();
726 passthru = lexrc.getBool();
730 keepempty = lexrc.getBool();
734 freespacing = lexrc.getBool();
738 needprotect = lexrc.getBool();
741 font = lyxRead(lexrc, inherit_font);
742 // So: define font before labelfont
747 string const token = lexrc.getString();
748 bgcolor = lcolor.getFromLyXName(token);
752 preamble = lexrc.getLongString("EndPreamble");
756 string const packages = lexrc.getString();
757 requires = getVectorFromString(packages);
766 // Here add element to list if getout == true
769 il.name = to_ascii(name);
770 il.lyxtype = lyxtype;
771 il.labelstring = labelstring;
772 il.decoration = decoration;
773 il.latextype = latextype;
774 il.latexname = latexname;
775 il.latexparam = latexparam;
776 il.multipar = multipar;
777 il.passthru = passthru;
778 il.needprotect = needprotect;
779 il.freespacing = freespacing;
780 il.forceltr = forceltr;
781 il.keepempty = keepempty;
783 // The label font is generally used as-is without
784 // any realization against a given context.
785 labelfont.realize(sane_font);
786 il.labelfont = labelfont;
787 il.bgcolor = bgcolor;
788 il.preamble = preamble;
789 il.requires = requires;
790 insetlayoutlist_[name] = il;
811 void TextClass::readFloat(Lexer & lexrc)
813 keyword_item floatTags[] = {
815 { "extension", FT_EXT },
816 { "guiname", FT_NAME },
817 { "latexbuiltin", FT_BUILTIN },
818 { "listname", FT_LISTNAME },
819 { "numberwithin", FT_WITHIN },
820 { "placement", FT_PLACEMENT },
821 { "style", FT_STYLE },
825 lexrc.pushTable(floatTags, FT_END);
834 bool builtin = false;
837 while (!getout && lexrc.isOK()) {
838 int le = lexrc.lex();
840 case Lexer::LEX_UNDEF:
841 lexrc.printError("Unknown float tag `$$Token'");
845 switch (static_cast<FloatTags>(le)) {
848 type = lexrc.getString();
849 if (floatlist_->typeExist(type)) {
850 Floating const & fl = floatlist_->getType(type);
851 placement = fl.placement();
853 within = fl.within();
856 listName = fl.listName();
857 builtin = fl.builtin();
862 name = lexrc.getString();
866 placement = lexrc.getString();
870 ext = lexrc.getString();
874 within = lexrc.getString();
875 if (within == "none")
880 style = lexrc.getString();
884 listName = lexrc.getString();
888 builtin = lexrc.getBool();
896 // Here if have a full float if getout == true
898 Floating fl(type, placement, ext, within,
899 style, name, listName, builtin);
900 floatlist_->newFloat(fl);
901 // each float has its own counter
902 counters_->newCounter(from_ascii(type), from_ascii(within),
903 docstring(), docstring());
914 CT_LABELSTRING_APPENDIX,
919 void TextClass::readCounter(Lexer & lexrc)
921 keyword_item counterTags[] = {
923 { "labelstring", CT_LABELSTRING },
924 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
926 { "within", CT_WITHIN }
929 lexrc.pushTable(counterTags, CT_END);
933 docstring labelstring;
934 docstring labelstring_appendix;
937 while (!getout && lexrc.isOK()) {
938 int le = lexrc.lex();
940 case Lexer::LEX_UNDEF:
941 lexrc.printError("Unknown counter tag `$$Token'");
945 switch (static_cast<CounterTags>(le)) {
948 name = lexrc.getDocString();
949 if (counters_->hasCounter(name))
950 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
952 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
956 within = lexrc.getDocString();
957 if (within == "none")
962 labelstring = lexrc.getDocString();
963 labelstring_appendix = labelstring;
965 case CT_LABELSTRING_APPENDIX:
967 labelstring_appendix = lexrc.getDocString();
975 // Here if have a full counter if getout == true
977 counters_->newCounter(name, within,
978 labelstring, labelstring_appendix);
984 FontInfo const & TextClass::defaultfont() const
990 docstring const & TextClass::leftmargin() const
996 docstring const & TextClass::rightmargin() const
1002 bool TextClass::hasLayout(docstring const & n) const
1004 docstring const name = n.empty() ? defaultLayoutName() : n;
1006 return find_if(layoutlist_.begin(), layoutlist_.end(),
1007 LayoutNamesEqual(name))
1008 != layoutlist_.end();
1013 LayoutPtr const & TextClass::operator[](docstring const & name) const
1015 BOOST_ASSERT(!name.empty());
1017 LayoutList::const_iterator cit =
1018 find_if(layoutlist_.begin(),
1020 LayoutNamesEqual(name));
1022 if (cit == layoutlist_.end()) {
1023 lyxerr << "We failed to find the layout '" << to_utf8(name)
1024 << "' in the layout list. You MUST investigate!"
1026 for (LayoutList::const_iterator it = layoutlist_.begin();
1027 it != layoutlist_.end(); ++it)
1028 lyxerr << " " << to_utf8(it->get()->name()) << endl;
1030 // we require the name to exist
1031 BOOST_ASSERT(false);
1038 bool TextClass::deleteLayout(docstring const & name)
1040 if (name == defaultLayoutName())
1043 LayoutList::iterator it =
1044 remove_if(layoutlist_.begin(), layoutlist_.end(),
1045 LayoutNamesEqual(name));
1047 LayoutList::iterator end = layoutlist_.end();
1048 bool const ret = (it != end);
1049 layoutlist_.erase(it, end);
1054 // Load textclass info if not loaded yet
1055 bool TextClass::load(string const & path) const
1060 // Read style-file, provided path is searched before the system ones
1061 FileName layout_file;
1063 layout_file = FileName(addName(path, name_ + ".layout"));
1064 if (layout_file.empty() || !layout_file.exists())
1065 layout_file = libFileSearch("layouts", name_, "layout");
1066 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1069 lyxerr << "Error reading `"
1070 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1071 << "'\n(Check `" << name_
1072 << "')\nCheck your installation and "
1073 "try Options/Reconfigure..." << endl;
1080 FloatList & TextClass::floats()
1082 return *floatlist_.get();
1086 FloatList const & TextClass::floats() const
1088 return *floatlist_.get();
1092 Counters & TextClass::counters() const
1094 return *counters_.get();
1098 // Return the layout object of an inset given by name. If the name
1099 // is not found as such, the part after the ':' is stripped off, and
1100 // searched again. In this way, an error fallback can be provided:
1101 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1102 // will invoke the layout object defined by name = 'CharStyle'.
1103 // If that doesn't work either, an empty object returns (shouldn't
1104 // happen). -- Idea JMarc, comment MV
1105 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1108 while (!n.empty()) {
1109 if (insetlayoutlist_.count(n) > 0)
1110 return insetlayoutlist_[n];
1111 docstring::size_type i = n.find(':');
1112 if (i == string::npos)
1116 static InsetLayout empty;
1117 empty.labelstring = from_utf8("UNDEFINED");
1118 empty.labelfont = sane_font;
1119 empty.labelfont.setColor(Color_error);
1120 empty.bgcolor = Color_error;
1125 docstring const & TextClass::defaultLayoutName() const
1127 // This really should come from the actual layout... (Lgb)
1128 return defaultlayout_;
1132 LayoutPtr const & TextClass::defaultLayout() const
1134 return operator[](defaultLayoutName());
1138 string const & TextClass::name() const
1144 string const & TextClass::latexname() const
1146 const_cast<TextClass*>(this)->load();
1151 string const & TextClass::description() const
1153 return description_;
1157 string const & TextClass::opt_fontsize() const
1159 return opt_fontsize_;
1163 string const & TextClass::opt_pagestyle() const
1165 return opt_pagestyle_;
1169 string const & TextClass::options() const
1175 string const & TextClass::class_header() const
1177 return class_header_;
1181 string const & TextClass::pagestyle() const
1187 docstring const & TextClass::preamble() const
1193 PageSides TextClass::sides() const
1199 int TextClass::secnumdepth() const
1201 return secnumdepth_;
1205 int TextClass::tocdepth() const
1211 OutputType TextClass::outputType() const
1217 bool TextClass::provides(string const & p) const
1219 return provides_.find(p) != provides_.end();
1223 unsigned int TextClass::columns() const
1229 TitleLatexType TextClass::titletype() const
1235 string const & TextClass::titlename() const
1241 int TextClass::size() const
1243 return layoutlist_.size();
1247 int TextClass::min_toclevel() const
1249 return min_toclevel_;
1253 int TextClass::max_toclevel() const
1255 return max_toclevel_;
1259 bool TextClass::hasTocLevels() const
1261 return min_toclevel_ != Layout::NOT_IN_TOC;
1265 ostream & operator<<(ostream & os, PageSides p)