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 string const packages = lexrc.getString();
399 vector<string> req = getVectorFromString(packages);
400 for (vector<string>::const_iterator it = req.begin();
401 it != req.end(); ++it) {
402 if (find(requires_.begin(), requires_.end(), *it) == requires_.end())
403 requires_.push_back(*it);
408 case TC_LEFTMARGIN: // left margin type
410 leftmargin_ = lexrc.getDocString();
413 case TC_RIGHTMARGIN: // right margin type
415 rightmargin_ = lexrc.getDocString();
419 docstring const name = subst(lexrc.getDocString(), '_', ' ');
420 readInsetLayout(lexrc, name);
429 case TC_TITLELATEXTYPE:
430 readTitleType(lexrc);
432 case TC_TITLELATEXNAME:
434 titlename_ = lexrc.getString();
438 string const nofloat = lexrc.getString();
439 floatlist_->erase(nofloat);
443 if (format != FORMAT)
447 if (format != FORMAT) {
448 LYXERR(Debug::TCLASS, "Converting layout file from format "
449 << format << " to " << FORMAT);
450 FileName const tempfile = FileName::tempName();
451 error = !layout2layout(filename, tempfile);
453 error = read(tempfile, rt);
454 tempfile.removeFile();
459 LYXERR(Debug::TCLASS, "Finished reading module file "
460 << to_utf8(makeDisplayPath(filename.absFilename())));
461 else if (rt == MERGE)
462 LYXERR(Debug::TCLASS, "Finished reading input file "
463 << to_utf8(makeDisplayPath(filename.absFilename())));
464 else { // we are at top level here.
465 LYXERR(Debug::TCLASS, "Finished reading textclass "
466 << to_utf8(makeDisplayPath(filename.absFilename())));
467 if (defaultlayout_.empty()) {
468 lyxerr << "Error: Textclass '" << name_
469 << "' is missing a defaultstyle." << endl;
473 min_toclevel_ = Layout::NOT_IN_TOC;
474 max_toclevel_ = Layout::NOT_IN_TOC;
475 const_iterator cit = begin();
476 const_iterator the_end = end();
477 for ( ; cit != the_end ; ++cit) {
478 int const toclevel = (*cit)->toclevel;
479 if (toclevel != Layout::NOT_IN_TOC) {
480 if (min_toclevel_ == Layout::NOT_IN_TOC)
481 min_toclevel_ = toclevel;
483 min_toclevel_ = min(min_toclevel_,
485 max_toclevel_ = max(max_toclevel_,
489 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
490 << ", maximum is " << max_toclevel_);
498 void TextClass::readTitleType(Lexer & lexrc)
500 keyword_item titleTypeTags[] = {
501 { "commandafter", TITLE_COMMAND_AFTER },
502 { "environment", TITLE_ENVIRONMENT }
505 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
507 int le = lexrc.lex();
509 case Lexer::LEX_UNDEF:
510 lexrc.printError("Unknown output type `$$Token'");
512 case TITLE_COMMAND_AFTER:
513 case TITLE_ENVIRONMENT:
514 titletype_ = static_cast<TitleLatexType>(le);
517 lyxerr << "Unhandled value " << le
518 << " in TextClass::readTitleType." << endl;
525 void TextClass::readOutputType(Lexer & lexrc)
527 keyword_item outputTypeTags[] = {
528 { "docbook", DOCBOOK },
530 { "literate", LITERATE }
533 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
535 int le = lexrc.lex();
537 case Lexer::LEX_UNDEF:
538 lexrc.printError("Unknown output type `$$Token'");
543 outputType_ = static_cast<OutputType>(le);
546 lyxerr << "Unhandled value " << le
547 << " in TextClass::readOutputType." << endl;
554 enum ClassOptionsTags {
563 void TextClass::readClassOptions(Lexer & lexrc)
565 keyword_item classOptionsTags[] = {
567 {"fontsize", CO_FONTSIZE },
568 {"header", CO_HEADER },
569 {"other", CO_OTHER },
570 {"pagestyle", CO_PAGESTYLE }
573 lexrc.pushTable(classOptionsTags, CO_END);
575 while (!getout && lexrc.isOK()) {
576 int le = lexrc.lex();
578 case Lexer::LEX_UNDEF:
579 lexrc.printError("Unknown ClassOption tag `$$Token'");
583 switch (static_cast<ClassOptionsTags>(le)) {
586 opt_fontsize_ = rtrim(lexrc.getString());
590 opt_pagestyle_ = rtrim(lexrc.getString());
594 options_ = lexrc.getString();
598 class_header_ = subst(lexrc.getString(), """, "\"");
609 enum InsetLayoutTags {
631 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
633 keyword_item elementTags[] = {
634 { "bgcolor", IL_BGCOLOR },
635 { "decoration", IL_DECORATION },
638 { "forceltr", IL_FORCELTR },
639 { "freespacing", IL_FREESPACING },
640 { "keepempty", IL_KEEPEMPTY },
641 { "labelfont", IL_LABELFONT },
642 { "labelstring", IL_LABELSTRING },
643 { "latexname", IL_LATEXNAME },
644 { "latexparam", IL_LATEXPARAM },
645 { "latextype", IL_LATEXTYPE },
646 { "lyxtype", IL_LYXTYPE },
647 { "multipar", IL_MULTIPAR },
648 { "needprotect", IL_NEEDPROTECT },
649 { "passthru", IL_PASSTHRU },
650 { "preamble", IL_PREAMBLE },
651 { "requires", IL_REQUIRES }
654 lexrc.pushTable(elementTags, IL_END);
657 docstring labelstring;
662 FontInfo font = inherit_font;
663 FontInfo labelfont = inherit_font;
664 ColorCode bgcolor(Color_background);
666 vector<string> requires;
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 InsetLayout 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");
751 string const packages = lexrc.getString();
752 requires = getVectorFromString(packages);
761 // Here add element to list if getout == true
764 il.name = to_ascii(name);
765 il.lyxtype = lyxtype;
766 il.labelstring = labelstring;
767 il.decoration = decoration;
768 il.latextype = latextype;
769 il.latexname = latexname;
770 il.latexparam = latexparam;
771 il.multipar = multipar;
772 il.passthru = passthru;
773 il.needprotect = needprotect;
774 il.freespacing = freespacing;
775 il.forceltr = forceltr;
776 il.keepempty = keepempty;
778 // The label font is generally used as-is without
779 // any realization against a given context.
780 labelfont.realize(sane_font);
781 il.labelfont = labelfont;
782 il.bgcolor = bgcolor;
783 il.preamble = preamble;
784 il.requires = requires;
785 insetlayoutlist_[name] = il;
806 void TextClass::readFloat(Lexer & lexrc)
808 keyword_item floatTags[] = {
810 { "extension", FT_EXT },
811 { "guiname", FT_NAME },
812 { "latexbuiltin", FT_BUILTIN },
813 { "listname", FT_LISTNAME },
814 { "numberwithin", FT_WITHIN },
815 { "placement", FT_PLACEMENT },
816 { "style", FT_STYLE },
820 lexrc.pushTable(floatTags, FT_END);
829 bool builtin = false;
832 while (!getout && lexrc.isOK()) {
833 int le = lexrc.lex();
835 case Lexer::LEX_UNDEF:
836 lexrc.printError("Unknown float tag `$$Token'");
840 switch (static_cast<FloatTags>(le)) {
843 type = lexrc.getString();
844 if (floatlist_->typeExist(type)) {
845 Floating const & fl = floatlist_->getType(type);
846 placement = fl.placement();
848 within = fl.within();
851 listName = fl.listName();
852 builtin = fl.builtin();
857 name = lexrc.getString();
861 placement = lexrc.getString();
865 ext = lexrc.getString();
869 within = lexrc.getString();
870 if (within == "none")
875 style = lexrc.getString();
879 listName = lexrc.getString();
883 builtin = lexrc.getBool();
891 // Here if have a full float if getout == true
893 Floating fl(type, placement, ext, within,
894 style, name, listName, builtin);
895 floatlist_->newFloat(fl);
896 // each float has its own counter
897 counters_->newCounter(from_ascii(type), from_ascii(within),
898 docstring(), docstring());
909 CT_LABELSTRING_APPENDIX,
914 void TextClass::readCounter(Lexer & lexrc)
916 keyword_item counterTags[] = {
918 { "labelstring", CT_LABELSTRING },
919 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
921 { "within", CT_WITHIN }
924 lexrc.pushTable(counterTags, CT_END);
928 docstring labelstring;
929 docstring labelstring_appendix;
932 while (!getout && lexrc.isOK()) {
933 int le = lexrc.lex();
935 case Lexer::LEX_UNDEF:
936 lexrc.printError("Unknown counter tag `$$Token'");
940 switch (static_cast<CounterTags>(le)) {
943 name = lexrc.getDocString();
944 if (counters_->hasCounter(name))
945 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
947 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
951 within = lexrc.getDocString();
952 if (within == "none")
957 labelstring = lexrc.getDocString();
958 labelstring_appendix = labelstring;
960 case CT_LABELSTRING_APPENDIX:
962 labelstring_appendix = lexrc.getDocString();
970 // Here if have a full counter if getout == true
972 counters_->newCounter(name, within,
973 labelstring, labelstring_appendix);
979 FontInfo const & TextClass::defaultfont() const
985 docstring const & TextClass::leftmargin() const
991 docstring const & TextClass::rightmargin() const
997 bool TextClass::hasLayout(docstring const & n) const
999 docstring const name = n.empty() ? defaultLayoutName() : n;
1001 return find_if(layoutlist_.begin(), layoutlist_.end(),
1002 LayoutNamesEqual(name))
1003 != layoutlist_.end();
1008 LayoutPtr const & TextClass::operator[](docstring const & name) const
1010 BOOST_ASSERT(!name.empty());
1012 LayoutList::const_iterator cit =
1013 find_if(layoutlist_.begin(),
1015 LayoutNamesEqual(name));
1017 if (cit == layoutlist_.end()) {
1018 lyxerr << "We failed to find the layout '" << to_utf8(name)
1019 << "' in the layout list. You MUST investigate!"
1021 for (LayoutList::const_iterator it = layoutlist_.begin();
1022 it != layoutlist_.end(); ++it)
1023 lyxerr << " " << to_utf8(it->get()->name()) << endl;
1025 // we require the name to exist
1026 BOOST_ASSERT(false);
1033 bool TextClass::deleteLayout(docstring const & name)
1035 if (name == defaultLayoutName())
1038 LayoutList::iterator it =
1039 remove_if(layoutlist_.begin(), layoutlist_.end(),
1040 LayoutNamesEqual(name));
1042 LayoutList::iterator end = layoutlist_.end();
1043 bool const ret = (it != end);
1044 layoutlist_.erase(it, end);
1049 // Load textclass info if not loaded yet
1050 bool TextClass::load(string const & path) const
1055 // Read style-file, provided path is searched before the system ones
1056 FileName layout_file;
1058 layout_file = FileName(addName(path, name_ + ".layout"));
1059 if (layout_file.empty() || !layout_file.exists())
1060 layout_file = libFileSearch("layouts", name_, "layout");
1061 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1064 lyxerr << "Error reading `"
1065 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1066 << "'\n(Check `" << name_
1067 << "')\nCheck your installation and "
1068 "try Options/Reconfigure..." << endl;
1075 FloatList & TextClass::floats()
1077 return *floatlist_.get();
1081 FloatList const & TextClass::floats() const
1083 return *floatlist_.get();
1087 Counters & TextClass::counters() const
1089 return *counters_.get();
1093 // Return the layout object of an inset given by name. If the name
1094 // is not found as such, the part after the ':' is stripped off, and
1095 // searched again. In this way, an error fallback can be provided:
1096 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1097 // will invoke the layout object defined by name = 'CharStyle'.
1098 // If that doesn't work either, an empty object returns (shouldn't
1099 // happen). -- Idea JMarc, comment MV
1100 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1103 while (!n.empty()) {
1104 if (insetlayoutlist_.count(n) > 0)
1105 return insetlayoutlist_[n];
1106 docstring::size_type i = n.find(':');
1107 if (i == string::npos)
1111 static InsetLayout empty;
1112 empty.labelstring = from_utf8("UNDEFINED");
1113 empty.labelfont = sane_font;
1114 empty.labelfont.setColor(Color_error);
1115 empty.bgcolor = Color_error;
1120 docstring const & TextClass::defaultLayoutName() const
1122 // This really should come from the actual layout... (Lgb)
1123 return defaultlayout_;
1127 LayoutPtr const & TextClass::defaultLayout() const
1129 return operator[](defaultLayoutName());
1133 string const & TextClass::name() const
1139 string const & TextClass::latexname() const
1141 const_cast<TextClass*>(this)->load();
1146 string const & TextClass::description() const
1148 return description_;
1152 string const & TextClass::opt_fontsize() const
1154 return opt_fontsize_;
1158 string const & TextClass::opt_pagestyle() const
1160 return opt_pagestyle_;
1164 string const & TextClass::options() const
1170 string const & TextClass::class_header() const
1172 return class_header_;
1176 string const & TextClass::pagestyle() const
1182 docstring const & TextClass::preamble() const
1188 PageSides TextClass::sides() const
1194 int TextClass::secnumdepth() const
1196 return secnumdepth_;
1200 int TextClass::tocdepth() const
1206 OutputType TextClass::outputType() const
1212 bool TextClass::provides(string const & p) const
1214 return provides_.find(p) != provides_.end();
1218 unsigned int TextClass::columns() const
1224 TitleLatexType TextClass::titletype() const
1230 string const & TextClass::titlename() const
1236 int TextClass::size() const
1238 return layoutlist_.size();
1242 int TextClass::min_toclevel() const
1244 return min_toclevel_;
1248 int TextClass::max_toclevel() const
1250 return max_toclevel_;
1254 bool TextClass::hasTocLevels() const
1256 return min_toclevel_ != Layout::NOT_IN_TOC;
1260 ostream & operator<<(ostream & os, PageSides p)