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/lyxlib.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;
164 // Reads a textclass structure from file.
165 bool TextClass::read(FileName const & filename, ReadType rt)
167 if (!filename.isReadableFile()) {
168 lyxerr << "Cannot read layout file `" << filename << "'."
173 keyword_item textClassTags[] = {
174 { "classoptions", TC_CLASSOPTIONS },
175 { "columns", TC_COLUMNS },
176 { "counter", TC_COUNTER },
177 { "defaultfont", TC_DEFAULTFONT },
178 { "defaultstyle", TC_DEFAULTSTYLE },
179 { "environment", TC_ENVIRONMENT },
180 { "float", TC_FLOAT },
181 { "format", TC_FORMAT },
182 { "input", TC_INPUT },
183 { "insetlayout", TC_INSETLAYOUT },
184 { "leftmargin", TC_LEFTMARGIN },
185 { "nofloat", TC_NOFLOAT },
186 { "nostyle", TC_NOSTYLE },
187 { "outputtype", TC_OUTPUTTYPE },
188 { "pagestyle", TC_PAGESTYLE },
189 { "preamble", TC_PREAMBLE },
190 { "provides", TC_PROVIDES },
191 { "rightmargin", TC_RIGHTMARGIN },
192 { "secnumdepth", TC_SECNUMDEPTH },
193 { "sides", TC_SIDES },
194 { "style", TC_STYLE },
195 { "titlelatexname", TC_TITLELATEXNAME },
196 { "titlelatextype", TC_TITLELATEXTYPE },
197 { "tocdepth", TC_TOCDEPTH }
202 LYXERR(Debug::TCLASS, "Reading textclass ");
205 LYXERR(Debug::TCLASS, "Reading input file ");
208 LYXERR(Debug::TCLASS, "Reading module file ");
213 LYXERR(Debug::TCLASS, to_utf8(makeDisplayPath(filename.absFilename())));
215 Lexer lexrc(textClassTags,
216 sizeof(textClassTags) / sizeof(textClassTags[0]));
218 lexrc.setFile(filename);
219 bool error = !lexrc.isOK();
221 // Format of files before the 'Format' tag was introduced
225 while (lexrc.isOK() && !error) {
226 int le = lexrc.lex();
229 case Lexer::LEX_FEOF:
232 case Lexer::LEX_UNDEF:
233 lexrc.printError("Unknown TextClass tag `$$Token'");
241 switch (static_cast<TextClassTags>(le)) {
245 format = lexrc.getInteger();
248 case TC_OUTPUTTYPE: // output type definition
249 readOutputType(lexrc);
252 case TC_INPUT: // Include file
254 string const inc = lexrc.getString();
255 FileName tmp = libFileSearch("layouts", inc,
259 lexrc.printError("Could not find input"
262 } else if (read(tmp, MERGE)) {
263 lexrc.printError("Error reading input"
264 "file: " + tmp.absFilename());
270 case TC_DEFAULTSTYLE:
272 docstring const name = from_utf8(subst(lexrc.getString(),
274 defaultlayout_ = name;
281 docstring const name = from_utf8(subst(lexrc.getString(),
284 string s = "Could not read name for style: `$$Token' "
285 + lexrc.getString() + " is probably not valid UTF-8!";
286 lexrc.printError(s.c_str());
288 error = readStyle(lexrc, lay);
289 } else if (hasLayout(name)) {
290 Layout * lay = operator[](name).get();
291 error = readStyle(lexrc, *lay);
295 if (le == TC_ENVIRONMENT)
296 lay.is_environment = true;
297 error = readStyle(lexrc, lay);
299 layoutlist_.push_back(
300 boost::shared_ptr<Layout>(new Layout(lay))
303 if (defaultlayout_.empty()) {
304 // We do not have a default
305 // layout yet, so we choose
306 // the first layout we
308 defaultlayout_ = name;
313 lexrc.printError("No name given for style: `$$Token'.");
320 docstring const style = from_utf8(subst(lexrc.getString(),
322 if (!deleteLayout(style))
323 lyxerr << "Cannot delete style `"
324 << to_utf8(style) << '\'' << endl;
325 // lexrc.printError("Cannot delete style"
332 columns_ = lexrc.getInteger();
337 switch (lexrc.getInteger()) {
338 case 1: sides_ = OneSide; break;
339 case 2: sides_ = TwoSides; break;
341 lyxerr << "Impossible number of page"
342 " sides, setting to one."
352 pagestyle_ = rtrim(lexrc.getString());
356 defaultfont_ = lyxRead(lexrc);
357 if (!defaultfont_.resolved()) {
358 lexrc.printError("Warning: defaultfont should "
359 "be fully instantiated!");
360 defaultfont_.realize(sane_font);
366 secnumdepth_ = lexrc.getInteger();
371 tocdepth_ = lexrc.getInteger();
374 // First step to support options
375 case TC_CLASSOPTIONS:
376 readClassOptions(lexrc);
380 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
385 string const feature = lexrc.getString();
387 if (lexrc.getInteger())
388 provides_.insert(feature);
390 provides_.erase(feature);
394 case TC_LEFTMARGIN: // left margin type
396 leftmargin_ = lexrc.getDocString();
399 case TC_RIGHTMARGIN: // right margin type
401 rightmargin_ = lexrc.getDocString();
405 docstring const name = subst(lexrc.getDocString(), '_', ' ');
406 readInsetLayout(lexrc, name);
415 case TC_TITLELATEXTYPE:
416 readTitleType(lexrc);
418 case TC_TITLELATEXNAME:
420 titlename_ = lexrc.getString();
424 string const nofloat = lexrc.getString();
425 floatlist_->erase(nofloat);
429 if (format != FORMAT)
433 if (format != FORMAT) {
434 LYXERR(Debug::TCLASS, "Converting layout file from format "
435 << format << " to " << FORMAT);
436 FileName const tempfile(tempName());
437 error = !layout2layout(filename, tempfile);
439 error = read(tempfile, rt);
440 tempfile.removeFile();
445 LYXERR(Debug::TCLASS, "Finished reading module file "
446 << to_utf8(makeDisplayPath(filename.absFilename())));
447 else if (rt == MERGE)
448 LYXERR(Debug::TCLASS, "Finished reading input file "
449 << to_utf8(makeDisplayPath(filename.absFilename())));
450 else { // we are at top level here.
451 LYXERR(Debug::TCLASS, "Finished reading textclass "
452 << to_utf8(makeDisplayPath(filename.absFilename())));
453 if (defaultlayout_.empty()) {
454 lyxerr << "Error: Textclass '" << name_
455 << "' is missing a defaultstyle." << endl;
459 min_toclevel_ = Layout::NOT_IN_TOC;
460 max_toclevel_ = Layout::NOT_IN_TOC;
461 const_iterator cit = begin();
462 const_iterator the_end = end();
463 for ( ; cit != the_end ; ++cit) {
464 int const toclevel = (*cit)->toclevel;
465 if (toclevel != Layout::NOT_IN_TOC) {
466 if (min_toclevel_ == Layout::NOT_IN_TOC)
467 min_toclevel_ = toclevel;
469 min_toclevel_ = min(min_toclevel_,
471 max_toclevel_ = max(max_toclevel_,
475 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
476 << ", maximum is " << max_toclevel_);
484 void TextClass::readTitleType(Lexer & lexrc)
486 keyword_item titleTypeTags[] = {
487 { "commandafter", TITLE_COMMAND_AFTER },
488 { "environment", TITLE_ENVIRONMENT }
491 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
493 int le = lexrc.lex();
495 case Lexer::LEX_UNDEF:
496 lexrc.printError("Unknown output type `$$Token'");
498 case TITLE_COMMAND_AFTER:
499 case TITLE_ENVIRONMENT:
500 titletype_ = static_cast<TitleLatexType>(le);
503 lyxerr << "Unhandled value " << le
504 << " in TextClass::readTitleType." << endl;
511 void TextClass::readOutputType(Lexer & lexrc)
513 keyword_item outputTypeTags[] = {
514 { "docbook", DOCBOOK },
516 { "literate", LITERATE }
519 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
521 int le = lexrc.lex();
523 case Lexer::LEX_UNDEF:
524 lexrc.printError("Unknown output type `$$Token'");
529 outputType_ = static_cast<OutputType>(le);
532 lyxerr << "Unhandled value " << le
533 << " in TextClass::readOutputType." << endl;
540 enum ClassOptionsTags {
549 void TextClass::readClassOptions(Lexer & lexrc)
551 keyword_item classOptionsTags[] = {
553 {"fontsize", CO_FONTSIZE },
554 {"header", CO_HEADER },
555 {"other", CO_OTHER },
556 {"pagestyle", CO_PAGESTYLE }
559 lexrc.pushTable(classOptionsTags, CO_END);
561 while (!getout && lexrc.isOK()) {
562 int le = lexrc.lex();
564 case Lexer::LEX_UNDEF:
565 lexrc.printError("Unknown ClassOption tag `$$Token'");
569 switch (static_cast<ClassOptionsTags>(le)) {
572 opt_fontsize_ = rtrim(lexrc.getString());
576 opt_pagestyle_ = rtrim(lexrc.getString());
580 options_ = lexrc.getString();
584 class_header_ = subst(lexrc.getString(), """, "\"");
595 enum InsetLayoutTags {
616 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
618 keyword_item elementTags[] = {
619 { "bgcolor", IL_BGCOLOR },
620 { "decoration", IL_DECORATION },
623 { "forceltr", IL_FORCELTR },
624 { "freespacing", IL_FREESPACING },
625 { "keepempty", IL_KEEPEMPTY },
626 { "labelfont", IL_LABELFONT },
627 { "labelstring", IL_LABELSTRING },
628 { "latexname", IL_LATEXNAME },
629 { "latexparam", IL_LATEXPARAM },
630 { "latextype", IL_LATEXTYPE },
631 { "lyxtype", IL_LYXTYPE },
632 { "multipar", IL_MULTIPAR },
633 { "needprotect", IL_NEEDPROTECT },
634 { "passthru", IL_PASSTHRU },
635 { "preamble", IL_PREAMBLE }
638 lexrc.pushTable(elementTags, IL_END);
641 docstring labelstring;
646 FontInfo font = inherit_font;
647 FontInfo labelfont = inherit_font;
648 ColorCode bgcolor(Color_background);
650 bool multipar = false;
651 bool passthru = false;
652 bool needprotect = false;
653 bool keepempty = false;
654 bool freespacing = false;
655 bool forceltr = false;
658 while (!getout && lexrc.isOK()) {
659 int le = lexrc.lex();
661 case Lexer::LEX_UNDEF:
662 lexrc.printError("Unknown ClassOption tag `$$Token'");
666 switch (static_cast<InsetLayoutTags>(le)) {
669 lyxtype = lexrc.getString();
673 latextype = lexrc.getString();
677 labelstring = lexrc.getDocString();
681 decoration = lexrc.getString();
685 latexname = lexrc.getString();
689 latexparam = subst(lexrc.getString(), """, "\"");
692 labelfont = lyxRead(lexrc, inherit_font);
696 forceltr = lexrc.getBool();
700 multipar = lexrc.getBool();
704 passthru = lexrc.getBool();
708 keepempty = lexrc.getBool();
712 freespacing = lexrc.getBool();
716 needprotect = lexrc.getBool();
719 font = lyxRead(lexrc, inherit_font);
720 // So: define font before labelfont
725 string const token = lexrc.getString();
726 bgcolor = lcolor.getFromLyXName(token);
730 preamble = lexrc.getLongString("EndPreamble");
738 // Here add element to list if getout == true
741 il.name = to_ascii(name);
742 il.lyxtype = lyxtype;
743 il.labelstring = labelstring;
744 il.decoration = decoration;
745 il.latextype = latextype;
746 il.latexname = latexname;
747 il.latexparam = latexparam;
748 il.multipar = multipar;
749 il.passthru = passthru;
750 il.needprotect = needprotect;
751 il.freespacing = freespacing;
752 il.forceltr = forceltr;
753 il.keepempty = keepempty;
755 // The label font is generally used as-is without
756 // any realization against a given context.
757 labelfont.realize(sane_font);
758 il.labelfont = labelfont;
759 il.bgcolor = bgcolor;
760 il.preamble = preamble;
761 insetlayoutlist_[name] = il;
782 void TextClass::readFloat(Lexer & lexrc)
784 keyword_item floatTags[] = {
786 { "extension", FT_EXT },
787 { "guiname", FT_NAME },
788 { "latexbuiltin", FT_BUILTIN },
789 { "listname", FT_LISTNAME },
790 { "numberwithin", FT_WITHIN },
791 { "placement", FT_PLACEMENT },
792 { "style", FT_STYLE },
796 lexrc.pushTable(floatTags, FT_END);
805 bool builtin = false;
808 while (!getout && lexrc.isOK()) {
809 int le = lexrc.lex();
811 case Lexer::LEX_UNDEF:
812 lexrc.printError("Unknown ClassOption tag `$$Token'");
816 switch (static_cast<FloatTags>(le)) {
819 type = lexrc.getString();
820 if (floatlist_->typeExist(type)) {
821 Floating const & fl = floatlist_->getType(type);
822 placement = fl.placement();
824 within = fl.within();
827 listName = fl.listName();
828 builtin = fl.builtin();
833 name = lexrc.getString();
837 placement = lexrc.getString();
841 ext = lexrc.getString();
845 within = lexrc.getString();
846 if (within == "none")
851 style = lexrc.getString();
855 listName = lexrc.getString();
859 builtin = lexrc.getBool();
867 // Here if have a full float if getout == true
869 Floating fl(type, placement, ext, within,
870 style, name, listName, builtin);
871 floatlist_->newFloat(fl);
872 // each float has its own counter
873 counters_->newCounter(from_ascii(type), from_ascii(within),
874 docstring(), docstring());
885 CT_LABELSTRING_APPENDIX,
890 void TextClass::readCounter(Lexer & lexrc)
892 keyword_item counterTags[] = {
894 { "labelstring", CT_LABELSTRING },
895 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
897 { "within", CT_WITHIN }
900 lexrc.pushTable(counterTags, CT_END);
904 docstring labelstring;
905 docstring labelstring_appendix;
908 while (!getout && lexrc.isOK()) {
909 int le = lexrc.lex();
911 case Lexer::LEX_UNDEF:
912 lexrc.printError("Unknown ClassOption tag `$$Token'");
916 switch (static_cast<CounterTags>(le)) {
919 name = lexrc.getDocString();
920 if (counters_->hasCounter(name))
921 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
923 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
927 within = lexrc.getDocString();
928 if (within == "none")
933 labelstring = lexrc.getDocString();
934 labelstring_appendix = labelstring;
936 case CT_LABELSTRING_APPENDIX:
938 labelstring_appendix = lexrc.getDocString();
946 // Here if have a full counter if getout == true
948 counters_->newCounter(name, within,
949 labelstring, labelstring_appendix);
955 FontInfo const & TextClass::defaultfont() const
961 docstring const & TextClass::leftmargin() const
967 docstring const & TextClass::rightmargin() const
973 bool TextClass::hasLayout(docstring const & n) const
975 docstring const name = n.empty() ? defaultLayoutName() : n;
977 return find_if(layoutlist_.begin(), layoutlist_.end(),
978 LayoutNamesEqual(name))
979 != layoutlist_.end();
984 LayoutPtr const & TextClass::operator[](docstring const & name) const
986 BOOST_ASSERT(!name.empty());
988 LayoutList::const_iterator cit =
989 find_if(layoutlist_.begin(),
991 LayoutNamesEqual(name));
993 if (cit == layoutlist_.end()) {
994 lyxerr << "We failed to find the layout '" << to_utf8(name)
995 << "' in the layout list. You MUST investigate!"
997 for (LayoutList::const_iterator it = layoutlist_.begin();
998 it != layoutlist_.end(); ++it)
999 lyxerr << " " << to_utf8(it->get()->name()) << endl;
1001 // we require the name to exist
1002 BOOST_ASSERT(false);
1009 bool TextClass::deleteLayout(docstring const & name)
1011 if (name == defaultLayoutName())
1014 LayoutList::iterator it =
1015 remove_if(layoutlist_.begin(), layoutlist_.end(),
1016 LayoutNamesEqual(name));
1018 LayoutList::iterator end = layoutlist_.end();
1019 bool const ret = (it != end);
1020 layoutlist_.erase(it, end);
1025 // Load textclass info if not loaded yet
1026 bool TextClass::load(string const & path) const
1031 // Read style-file, provided path is searched before the system ones
1032 FileName layout_file;
1034 layout_file = FileName(addName(path, name_ + ".layout"));
1035 if (layout_file.empty() || !layout_file.exists())
1036 layout_file = libFileSearch("layouts", name_, "layout");
1037 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1040 lyxerr << "Error reading `"
1041 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1042 << "'\n(Check `" << name_
1043 << "')\nCheck your installation and "
1044 "try Options/Reconfigure..." << endl;
1051 FloatList & TextClass::floats()
1053 return *floatlist_.get();
1057 FloatList const & TextClass::floats() const
1059 return *floatlist_.get();
1063 Counters & TextClass::counters() const
1065 return *counters_.get();
1069 // Return the layout object of an inset given by name. If the name
1070 // is not found as such, the part after the ':' is stripped off, and
1071 // searched again. In this way, an error fallback can be provided:
1072 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1073 // will invoke the layout object defined by name = 'CharStyle'.
1074 // If that doesn't work either, an empty object returns (shouldn't
1075 // happen). -- Idea JMarc, comment MV
1076 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1079 while (!n.empty()) {
1080 if (insetlayoutlist_.count(n) > 0)
1081 return insetlayoutlist_[n];
1082 docstring::size_type i = n.find(':');
1083 if (i == string::npos)
1087 static InsetLayout empty;
1088 empty.labelstring = from_utf8("UNDEFINED");
1089 empty.labelfont = sane_font;
1090 empty.labelfont.setColor(Color_error);
1091 empty.bgcolor = Color_error;
1096 docstring const & TextClass::defaultLayoutName() const
1098 // This really should come from the actual layout... (Lgb)
1099 return defaultlayout_;
1103 LayoutPtr const & TextClass::defaultLayout() const
1105 return operator[](defaultLayoutName());
1109 string const & TextClass::name() const
1115 string const & TextClass::latexname() const
1117 const_cast<TextClass*>(this)->load();
1122 string const & TextClass::description() const
1124 return description_;
1128 string const & TextClass::opt_fontsize() const
1130 return opt_fontsize_;
1134 string const & TextClass::opt_pagestyle() const
1136 return opt_pagestyle_;
1140 string const & TextClass::options() const
1146 string const & TextClass::class_header() const
1148 return class_header_;
1152 string const & TextClass::pagestyle() const
1158 docstring const & TextClass::preamble() const
1164 PageSides TextClass::sides() const
1170 int TextClass::secnumdepth() const
1172 return secnumdepth_;
1176 int TextClass::tocdepth() const
1182 OutputType TextClass::outputType() const
1188 bool TextClass::provides(string const & p) const
1190 return provides_.find(p) != provides_.end();
1194 unsigned int TextClass::columns() const
1200 TitleLatexType TextClass::titletype() const
1206 string const & TextClass::titlename() const
1212 int TextClass::size() const
1214 return layoutlist_.size();
1218 int TextClass::min_toclevel() const
1220 return min_toclevel_;
1224 int TextClass::max_toclevel() const
1226 return max_toclevel_;
1230 bool TextClass::hasTocLevels() const
1232 return min_toclevel_ != Layout::NOT_IN_TOC;
1236 ostream & operator<<(ostream & os, PageSides p)