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"
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);
58 class LayoutNamesEqual : public std::unary_function<LayoutPtr, bool> {
60 LayoutNamesEqual(docstring const & name)
63 bool operator()(LayoutPtr const & c) const
65 return c->name() == name_;
75 bool layout2layout(FileName const & filename, FileName const & tempfile)
77 FileName const script = libFileSearch("scripts", "layout2layout.py");
79 lyxerr << "Could not find layout conversion "
80 "script layout2layout.py." << endl;
84 std::ostringstream command;
85 command << support::os::python() << ' ' << quoteName(script.toFilesystemEncoding())
86 << ' ' << quoteName(filename.toFilesystemEncoding())
87 << ' ' << quoteName(tempfile.toFilesystemEncoding());
88 string const command_str = command.str();
90 LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
92 support::cmd_ret const ret =
93 support::runCommand(command_str);
95 lyxerr << "Could not run layout conversion "
96 "script layout2layout.py." << endl;
105 TextClass::TextClass(string const & fn, string const & cln,
106 string const & desc, bool texClassAvail )
107 : name_(fn), latexname_(cln), description_(desc),
108 floatlist_(new FloatList), counters_(new Counters),
109 texClassAvail_(texClassAvail)
117 pagestyle_ = "default";
118 defaultfont_ = sane_font;
119 opt_fontsize_ = "10|11|12";
120 opt_pagestyle_ = "empty|plain|headings|fancy";
121 titletype_ = TITLE_COMMAND_AFTER;
122 titlename_ = "maketitle";
127 bool TextClass::isTeXClassAvailable() const
129 return texClassAvail_;
133 bool TextClass::readStyle(Lexer & lexrc, Layout & lay)
135 LYXERR(Debug::TCLASS, "Reading style " << to_utf8(lay.name()));
136 if (!lay.read(lexrc, *this)) {
138 lay.resfont = lay.font;
139 lay.resfont.realize(defaultfont());
140 lay.reslabelfont = lay.labelfont;
141 lay.reslabelfont.realize(defaultfont());
142 return false; // no errors
144 lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
177 // Reads a textclass structure from file.
178 bool TextClass::read(FileName const & filename, ReadType rt)
180 if (!filename.isReadableFile()) {
181 lyxerr << "Cannot read layout file `" << filename << "'."
186 keyword_item textClassTags[] = {
187 { "classoptions", TC_CLASSOPTIONS },
188 { "columns", TC_COLUMNS },
189 { "counter", TC_COUNTER },
190 { "defaultfont", TC_DEFAULTFONT },
191 { "defaultstyle", TC_DEFAULTSTYLE },
192 { "environment", TC_ENVIRONMENT },
193 { "float", TC_FLOAT },
194 { "format", TC_FORMAT },
195 { "input", TC_INPUT },
196 { "insetlayout", TC_INSETLAYOUT },
197 { "leftmargin", TC_LEFTMARGIN },
198 { "nofloat", TC_NOFLOAT },
199 { "nostyle", TC_NOSTYLE },
200 { "outputtype", TC_OUTPUTTYPE },
201 { "pagestyle", TC_PAGESTYLE },
202 { "preamble", TC_PREAMBLE },
203 { "provides", TC_PROVIDES },
204 { "rightmargin", TC_RIGHTMARGIN },
205 { "secnumdepth", TC_SECNUMDEPTH },
206 { "sides", TC_SIDES },
207 { "style", TC_STYLE },
208 { "titlelatexname", TC_TITLELATEXNAME },
209 { "titlelatextype", TC_TITLELATEXTYPE },
210 { "tocdepth", TC_TOCDEPTH }
215 LYXERR(Debug::TCLASS, "Reading textclass ");
218 LYXERR(Debug::TCLASS, "Reading input file ");
221 LYXERR(Debug::TCLASS, "Reading module file ");
226 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);
449 FileName const tempfile(support::tempName());
450 error = !layout2layout(filename, tempfile);
452 error = read(tempfile, rt);
453 tempfile.removeFile();
458 LYXERR(Debug::TCLASS, "Finished reading module file "
459 << to_utf8(makeDisplayPath(filename.absFilename())));
460 else if (rt == MERGE)
461 LYXERR(Debug::TCLASS, "Finished reading input file "
462 << to_utf8(makeDisplayPath(filename.absFilename())));
463 else { // we are at top level here.
464 LYXERR(Debug::TCLASS, "Finished reading textclass "
465 << to_utf8(makeDisplayPath(filename.absFilename())));
466 if (defaultlayout_.empty()) {
467 lyxerr << "Error: Textclass '" << name_
468 << "' is missing a defaultstyle." << endl;
472 min_toclevel_ = Layout::NOT_IN_TOC;
473 max_toclevel_ = Layout::NOT_IN_TOC;
474 const_iterator cit = begin();
475 const_iterator the_end = end();
476 for ( ; cit != the_end ; ++cit) {
477 int const toclevel = (*cit)->toclevel;
478 if (toclevel != Layout::NOT_IN_TOC) {
479 if (min_toclevel_ == Layout::NOT_IN_TOC)
480 min_toclevel_ = toclevel;
482 min_toclevel_ = std::min(min_toclevel_,
484 max_toclevel_ = std::max(max_toclevel_,
488 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
489 << ", maximum is " << max_toclevel_);
497 void TextClass::readTitleType(Lexer & lexrc)
499 keyword_item titleTypeTags[] = {
500 { "commandafter", TITLE_COMMAND_AFTER },
501 { "environment", TITLE_ENVIRONMENT }
504 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
506 int le = lexrc.lex();
508 case Lexer::LEX_UNDEF:
509 lexrc.printError("Unknown output type `$$Token'");
511 case TITLE_COMMAND_AFTER:
512 case TITLE_ENVIRONMENT:
513 titletype_ = static_cast<TitleLatexType>(le);
516 lyxerr << "Unhandled value " << le
517 << " in TextClass::readTitleType." << endl;
524 void TextClass::readOutputType(Lexer & lexrc)
526 keyword_item outputTypeTags[] = {
527 { "docbook", DOCBOOK },
529 { "literate", LITERATE }
532 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
534 int le = lexrc.lex();
536 case Lexer::LEX_UNDEF:
537 lexrc.printError("Unknown output type `$$Token'");
542 outputType_ = static_cast<OutputType>(le);
545 lyxerr << "Unhandled value " << le
546 << " in TextClass::readOutputType." << endl;
553 enum ClassOptionsTags {
562 void TextClass::readClassOptions(Lexer & lexrc)
564 keyword_item classOptionsTags[] = {
566 {"fontsize", CO_FONTSIZE },
567 {"header", CO_HEADER },
568 {"other", CO_OTHER },
569 {"pagestyle", CO_PAGESTYLE }
572 lexrc.pushTable(classOptionsTags, CO_END);
574 while (!getout && lexrc.isOK()) {
575 int le = lexrc.lex();
577 case Lexer::LEX_UNDEF:
578 lexrc.printError("Unknown ClassOption tag `$$Token'");
582 switch (static_cast<ClassOptionsTags>(le)) {
585 opt_fontsize_ = rtrim(lexrc.getString());
589 opt_pagestyle_ = rtrim(lexrc.getString());
593 options_ = lexrc.getString();
597 class_header_ = subst(lexrc.getString(), """, "\"");
608 enum InsetLayoutTags {
629 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
631 keyword_item elementTags[] = {
632 { "bgcolor", IL_BGCOLOR },
633 { "decoration", IL_DECORATION },
636 { "forceltr", IL_FORCELTR },
637 { "freespacing", IL_FREESPACING },
638 { "keepempty", IL_KEEPEMPTY },
639 { "labelfont", IL_LABELFONT },
640 { "labelstring", IL_LABELSTRING },
641 { "latexname", IL_LATEXNAME },
642 { "latexparam", IL_LATEXPARAM },
643 { "latextype", IL_LATEXTYPE },
644 { "lyxtype", IL_LYXTYPE },
645 { "multipar", IL_MULTIPAR },
646 { "needprotect", IL_NEEDPROTECT },
647 { "passthru", IL_PASSTHRU },
648 { "preamble", IL_PREAMBLE }
651 lexrc.pushTable(elementTags, IL_END);
654 docstring labelstring;
659 FontInfo font = inherit_font;
660 FontInfo labelfont = inherit_font;
661 ColorCode bgcolor(Color_background);
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 ClassOption 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");
751 // Here add element to list if getout == true
754 il.name = to_ascii(name);
755 il.lyxtype = lyxtype;
756 il.labelstring = labelstring;
757 il.decoration = decoration;
758 il.latextype = latextype;
759 il.latexname = latexname;
760 il.latexparam = latexparam;
761 il.multipar = multipar;
762 il.passthru = passthru;
763 il.needprotect = needprotect;
764 il.freespacing = freespacing;
765 il.forceltr = forceltr;
766 il.keepempty = keepempty;
768 // The label font is generally used as-is without
769 // any realization against a given context.
770 labelfont.realize(sane_font);
771 il.labelfont = labelfont;
772 il.bgcolor = bgcolor;
773 il.preamble = preamble;
774 insetlayoutlist_[name] = il;
795 void TextClass::readFloat(Lexer & lexrc)
797 keyword_item floatTags[] = {
799 { "extension", FT_EXT },
800 { "guiname", FT_NAME },
801 { "latexbuiltin", FT_BUILTIN },
802 { "listname", FT_LISTNAME },
803 { "numberwithin", FT_WITHIN },
804 { "placement", FT_PLACEMENT },
805 { "style", FT_STYLE },
809 lexrc.pushTable(floatTags, FT_END);
818 bool builtin = false;
821 while (!getout && lexrc.isOK()) {
822 int le = lexrc.lex();
824 case Lexer::LEX_UNDEF:
825 lexrc.printError("Unknown ClassOption tag `$$Token'");
829 switch (static_cast<FloatTags>(le)) {
832 type = lexrc.getString();
833 if (floatlist_->typeExist(type)) {
834 Floating const & fl = floatlist_->getType(type);
835 placement = fl.placement();
837 within = fl.within();
840 listName = fl.listName();
841 builtin = fl.builtin();
846 name = lexrc.getString();
850 placement = lexrc.getString();
854 ext = lexrc.getString();
858 within = lexrc.getString();
859 if (within == "none")
864 style = lexrc.getString();
868 listName = lexrc.getString();
872 builtin = lexrc.getBool();
880 // Here if have a full float if getout == true
882 Floating fl(type, placement, ext, within,
883 style, name, listName, builtin);
884 floatlist_->newFloat(fl);
885 // each float has its own counter
886 counters_->newCounter(from_ascii(type), from_ascii(within),
887 docstring(), docstring());
898 CT_LABELSTRING_APPENDIX,
903 void TextClass::readCounter(Lexer & lexrc)
905 keyword_item counterTags[] = {
907 { "labelstring", CT_LABELSTRING },
908 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
910 { "within", CT_WITHIN }
913 lexrc.pushTable(counterTags, CT_END);
917 docstring labelstring;
918 docstring labelstring_appendix;
921 while (!getout && lexrc.isOK()) {
922 int le = lexrc.lex();
924 case Lexer::LEX_UNDEF:
925 lexrc.printError("Unknown ClassOption tag `$$Token'");
929 switch (static_cast<CounterTags>(le)) {
932 name = lexrc.getDocString();
933 if (counters_->hasCounter(name))
934 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
936 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
940 within = lexrc.getDocString();
941 if (within == "none")
946 labelstring = lexrc.getDocString();
947 labelstring_appendix = labelstring;
949 case CT_LABELSTRING_APPENDIX:
951 labelstring_appendix = lexrc.getDocString();
959 // Here if have a full counter if getout == true
961 counters_->newCounter(name, within,
962 labelstring, labelstring_appendix);
968 FontInfo const & TextClass::defaultfont() const
974 docstring const & TextClass::leftmargin() const
980 docstring const & TextClass::rightmargin() const
986 bool TextClass::hasLayout(docstring const & n) const
988 docstring const name = n.empty() ? defaultLayoutName() : n;
990 return find_if(layoutlist_.begin(), layoutlist_.end(),
991 LayoutNamesEqual(name))
992 != layoutlist_.end();
997 LayoutPtr const & TextClass::operator[](docstring const & name) const
999 BOOST_ASSERT(!name.empty());
1001 LayoutList::const_iterator cit =
1002 find_if(layoutlist_.begin(),
1004 LayoutNamesEqual(name));
1006 if (cit == layoutlist_.end()) {
1007 lyxerr << "We failed to find the layout '" << to_utf8(name)
1008 << "' in the layout list. You MUST investigate!"
1010 for (LayoutList::const_iterator it = layoutlist_.begin();
1011 it != layoutlist_.end(); ++it)
1012 lyxerr << " " << to_utf8(it->get()->name()) << endl;
1014 // we require the name to exist
1015 BOOST_ASSERT(false);
1022 bool TextClass::deleteLayout(docstring const & name)
1024 if (name == defaultLayoutName())
1027 LayoutList::iterator it =
1028 remove_if(layoutlist_.begin(), layoutlist_.end(),
1029 LayoutNamesEqual(name));
1031 LayoutList::iterator end = layoutlist_.end();
1032 bool const ret = (it != end);
1033 layoutlist_.erase(it, end);
1038 // Load textclass info if not loaded yet
1039 bool TextClass::load(string const & path) const
1044 // Read style-file, provided path is searched before the system ones
1045 FileName layout_file;
1047 layout_file = FileName(addName(path, name_ + ".layout"));
1048 if (layout_file.empty() || !layout_file.exists())
1049 layout_file = libFileSearch("layouts", name_, "layout");
1050 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1053 lyxerr << "Error reading `"
1054 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1055 << "'\n(Check `" << name_
1056 << "')\nCheck your installation and "
1057 "try Options/Reconfigure..." << endl;
1064 FloatList & TextClass::floats()
1066 return *floatlist_.get();
1070 FloatList const & TextClass::floats() const
1072 return *floatlist_.get();
1076 Counters & TextClass::counters() const
1078 return *counters_.get();
1082 // Return the layout object of an inset given by name. If the name
1083 // is not found as such, the part after the ':' is stripped off, and
1084 // searched again. In this way, an error fallback can be provided:
1085 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1086 // will invoke the layout object defined by name = 'CharStyle'.
1087 // If that doesn't work either, an empty object returns (shouldn't
1088 // happen). -- Idea JMarc, comment MV
1089 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1092 while (!n.empty()) {
1093 if (insetlayoutlist_.count(n) > 0)
1094 return insetlayoutlist_[n];
1095 docstring::size_type i = n.find(':');
1096 if (i == string::npos)
1100 static InsetLayout empty;
1101 empty.labelstring = from_utf8("UNDEFINED");
1102 empty.labelfont = sane_font;
1103 empty.labelfont.setColor(Color_error);
1104 empty.bgcolor = Color_error;
1109 docstring const & TextClass::defaultLayoutName() const
1111 // This really should come from the actual layout... (Lgb)
1112 return defaultlayout_;
1116 LayoutPtr const & TextClass::defaultLayout() const
1118 return operator[](defaultLayoutName());
1122 string const & TextClass::name() const
1128 string const & TextClass::latexname() const
1130 const_cast<TextClass*>(this)->load();
1135 string const & TextClass::description() const
1137 return description_;
1141 string const & TextClass::opt_fontsize() const
1143 return opt_fontsize_;
1147 string const & TextClass::opt_pagestyle() const
1149 return opt_pagestyle_;
1153 string const & TextClass::options() const
1159 string const & TextClass::class_header() const
1161 return class_header_;
1165 string const & TextClass::pagestyle() const
1171 docstring const & TextClass::preamble() const
1177 PageSides TextClass::sides() const
1183 int TextClass::secnumdepth() const
1185 return secnumdepth_;
1189 int TextClass::tocdepth() const
1195 OutputType TextClass::outputType() const
1201 bool TextClass::provides(string const & p) const
1203 return provides_.find(p) != provides_.end();
1207 unsigned int TextClass::columns() const
1213 TitleLatexType TextClass::titletype() const
1219 string const & TextClass::titlename() const
1225 int TextClass::size() const
1227 return layoutlist_.size();
1231 int TextClass::min_toclevel() const
1233 return min_toclevel_;
1237 int TextClass::max_toclevel() const
1239 return max_toclevel_;
1243 bool TextClass::hasTocLevels() const
1245 return min_toclevel_ != Layout::NOT_IN_TOC;
1249 ostream & operator<<(ostream & os, PageSides p)