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"
24 #include "FloatList.h"
26 #include "frontends/alert.h"
28 #include "support/lstrings.h"
29 #include "support/lyxlib.h"
30 #include "support/filetools.h"
31 #include "support/os.h"
37 using support::FileName;
38 using support::libFileSearch;
39 using support::makeDisplayPath;
40 using support::quoteName;
43 using support::addName;
54 class LayoutNamesEqual : public std::unary_function<LayoutPtr, bool> {
56 LayoutNamesEqual(docstring const & name)
59 bool operator()(LayoutPtr const & c) const
61 return c->name() == name_;
71 bool layout2layout(FileName const & filename, FileName const & tempfile)
73 FileName const script = libFileSearch("scripts", "layout2layout.py");
75 lyxerr << "Could not find layout conversion "
76 "script layout2layout.py." << endl;
80 std::ostringstream command;
81 command << support::os::python() << ' ' << quoteName(script.toFilesystemEncoding())
82 << ' ' << quoteName(filename.toFilesystemEncoding())
83 << ' ' << quoteName(tempfile.toFilesystemEncoding());
84 string const command_str = command.str();
86 LYXERR(Debug::TCLASS) << "Running `" << command_str << '\'' << endl;
88 support::cmd_ret const ret =
89 support::runCommand(command_str);
91 lyxerr << "Could not run layout conversion "
92 "script layout2layout.py." << endl;
101 TextClass::TextClass(string const & fn, string const & cln,
102 string const & desc, bool texClassAvail )
103 : name_(fn), latexname_(cln), description_(desc),
104 floatlist_(new FloatList), counters_(new Counters),
105 texClassAvail_(texClassAvail)
113 pagestyle_ = "default";
114 defaultfont_ = Font(Font::ALL_SANE);
115 opt_fontsize_ = "10|11|12";
116 opt_pagestyle_ = "empty|plain|headings|fancy";
117 titletype_ = TITLE_COMMAND_AFTER;
118 titlename_ = "maketitle";
123 bool TextClass::isTeXClassAvailable() const
125 return texClassAvail_;
129 bool TextClass::do_readStyle(Lexer & lexrc, Layout & lay)
131 LYXERR(Debug::TCLASS) << "Reading style " << to_utf8(lay.name()) << endl;
132 if (!lay.read(lexrc, *this)) {
134 lay.resfont = lay.font;
135 lay.resfont.realize(defaultfont());
136 lay.reslabelfont = lay.labelfont;
137 lay.reslabelfont.realize(defaultfont());
138 return false; // no errors
140 lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
173 // Reads a textclass structure from file.
174 bool TextClass::read(FileName const & filename, ReadType rt)
176 if (!support::isFileReadable(filename)) {
177 lyxerr << "Cannot read layout file `" << filename << "'."
182 keyword_item textClassTags[] = {
183 { "classoptions", TC_CLASSOPTIONS },
184 { "columns", TC_COLUMNS },
185 { "counter", TC_COUNTER },
186 { "defaultfont", TC_DEFAULTFONT },
187 { "defaultstyle", TC_DEFAULTSTYLE },
188 { "environment", TC_ENVIRONMENT },
189 { "float", TC_FLOAT },
190 { "format", TC_FORMAT },
191 { "input", TC_INPUT },
192 { "insetlayout", TC_INSETLAYOUT },
193 { "leftmargin", TC_LEFTMARGIN },
194 { "nofloat", TC_NOFLOAT },
195 { "nostyle", TC_NOSTYLE },
196 { "outputtype", TC_OUTPUTTYPE },
197 { "pagestyle", TC_PAGESTYLE },
198 { "preamble", TC_PREAMBLE },
199 { "provides", TC_PROVIDES },
200 { "rightmargin", TC_RIGHTMARGIN },
201 { "secnumdepth", TC_SECNUMDEPTH },
202 { "sides", TC_SIDES },
203 { "style", TC_STYLE },
204 { "titlelatexname", TC_TITLELATEXNAME },
205 { "titlelatextype", TC_TITLELATEXTYPE },
206 { "tocdepth", TC_TOCDEPTH }
211 LYXERR(Debug::TCLASS) << "Reading textclass ";
214 LYXERR(Debug::TCLASS) << "Reading input file ";
217 LYXERR(Debug::TCLASS) << "Reading module file ";
222 LYXERR(Debug::TCLASS) << to_utf8(makeDisplayPath(filename.absFilename()))
225 Lexer lexrc(textClassTags,
226 sizeof(textClassTags) / sizeof(textClassTags[0]));
228 lexrc.setFile(filename);
229 bool error = !lexrc.isOK();
231 // Format of files before the 'Format' tag was introduced
235 while (lexrc.isOK() && !error) {
236 int le = lexrc.lex();
239 case Lexer::LEX_FEOF:
242 case Lexer::LEX_UNDEF:
243 lexrc.printError("Unknown TextClass tag `$$Token'");
251 switch (static_cast<TextClassTags>(le)) {
255 format = lexrc.getInteger();
258 case TC_OUTPUTTYPE: // output type definition
259 readOutputType(lexrc);
262 case TC_INPUT: // Include file
264 string const inc = lexrc.getString();
265 FileName tmp = libFileSearch("layouts", inc,
269 lexrc.printError("Could not find input"
272 } else if (read(tmp, MERGE)) {
273 lexrc.printError("Error reading input"
274 "file: " + tmp.absFilename());
280 case TC_DEFAULTSTYLE:
282 docstring const name = from_utf8(subst(lexrc.getString(),
284 defaultlayout_ = name;
291 docstring const name = from_utf8(subst(lexrc.getString(),
294 string s = "Could not read name for style: `$$Token' "
295 + lexrc.getString() + " is probably not valid UTF-8!";
296 lexrc.printError(s.c_str());
298 error = do_readStyle(lexrc, lay);
299 } else if (hasLayout(name)) {
300 Layout * lay = operator[](name).get();
301 error = do_readStyle(lexrc, *lay);
305 if (le == TC_ENVIRONMENT)
306 lay.is_environment = true;
307 error = do_readStyle(lexrc, lay);
309 layoutlist_.push_back(
310 boost::shared_ptr<Layout>(new Layout(lay))
313 if (defaultlayout_.empty()) {
314 // We do not have a default
315 // layout yet, so we choose
316 // the first layout we
318 defaultlayout_ = name;
323 lexrc.printError("No name given for style: `$$Token'.");
330 docstring const style = from_utf8(subst(lexrc.getString(),
332 if (!delete_layout(style))
333 lyxerr << "Cannot delete style `"
334 << to_utf8(style) << '\'' << endl;
335 // lexrc.printError("Cannot delete style"
342 columns_ = lexrc.getInteger();
347 switch (lexrc.getInteger()) {
348 case 1: sides_ = OneSide; break;
349 case 2: sides_ = TwoSides; break;
351 lyxerr << "Impossible number of page"
352 " sides, setting to one."
362 pagestyle_ = rtrim(lexrc.getString());
366 defaultfont_.lyxRead(lexrc);
367 if (!defaultfont_.resolved()) {
368 lexrc.printError("Warning: defaultfont should "
369 "be fully instantiated!");
370 defaultfont_.realize(Font(Font::ALL_SANE));
376 secnumdepth_ = lexrc.getInteger();
381 tocdepth_ = lexrc.getInteger();
384 // First step to support options
385 case TC_CLASSOPTIONS:
386 readClassOptions(lexrc);
390 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
395 string const feature = lexrc.getString();
397 if (lexrc.getInteger())
398 provides_.insert(feature);
400 provides_.erase(feature);
404 case TC_LEFTMARGIN: // left margin type
406 leftmargin_ = lexrc.getDocString();
409 case TC_RIGHTMARGIN: // right margin type
411 rightmargin_ = lexrc.getDocString();
415 docstring const name = subst(lexrc.getDocString(), '_', ' ');
416 readInsetLayout(lexrc, name);
425 case TC_TITLELATEXTYPE:
426 readTitleType(lexrc);
428 case TC_TITLELATEXNAME:
430 titlename_ = lexrc.getString();
434 string const nofloat = lexrc.getString();
435 floatlist_->erase(nofloat);
439 if (format != FORMAT)
443 if (format != FORMAT) {
444 LYXERR(Debug::TCLASS) << "Converting layout file from format "
445 << format << " to " << FORMAT << endl;
446 FileName const tempfile(support::tempName());
447 error = !layout2layout(filename, tempfile);
449 error = read(tempfile, rt);
450 support::unlink(tempfile);
455 LYXERR(Debug::TCLASS) << "Finished reading module file "
456 << to_utf8(makeDisplayPath(filename.absFilename()))
458 else if (rt == MERGE)
459 LYXERR(Debug::TCLASS) << "Finished reading input file "
460 << to_utf8(makeDisplayPath(filename.absFilename()))
462 else { // we are at top level here.
463 LYXERR(Debug::TCLASS) << "Finished reading textclass "
464 << 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)
489 << "Minimum TocLevel is " << min_toclevel_
490 << ", maximum is " << max_toclevel_ <<endl;
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 {
625 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
627 keyword_item elementTags[] = {
628 { "bgcolor", IL_BGCOLOR },
629 { "decoration", IL_DECORATION },
632 { "labelfont", IL_LABELFONT },
633 { "labelstring", IL_LABELSTRING },
634 { "latexname", IL_LATEXNAME },
635 { "latexparam", IL_LATEXPARAM },
636 { "latextype", IL_LATEXTYPE },
637 { "lyxtype", IL_LYXTYPE },
638 { "multipar", IL_MULTIPAR },
639 { "preamble", IL_PREAMBLE }
642 lexrc.pushTable(elementTags, IL_END);
645 docstring labelstring;
650 Font font(defaultfont());
651 Font labelfont(defaultfont());
652 Color::color bgcolor(Color::background);
654 bool multipar(false);
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 multipar = lexrc.getBool();
700 font.realize(defaultfont());
701 // So: define font before labelfont
706 string const token = lexrc.getString();
707 bgcolor = lcolor.getFromLyXName(token);
711 preamble = lexrc.getLongString("EndPreamble");
720 // Here add element to list if getout == true
723 il.name = to_ascii(name);
724 il.lyxtype = lyxtype;
725 il.labelstring = labelstring;
726 il.decoration = decoration;
727 il.latextype = latextype;
728 il.latexname = latexname;
729 il.latexparam = latexparam;
730 il.multipar = multipar;
732 il.labelfont = labelfont;
733 il.bgcolor = bgcolor;
734 il.preamble = preamble;
735 insetlayoutlist_[name] = il;
756 void TextClass::readFloat(Lexer & lexrc)
758 keyword_item floatTags[] = {
760 { "extension", FT_EXT },
761 { "guiname", FT_NAME },
762 { "latexbuiltin", FT_BUILTIN },
763 { "listname", FT_LISTNAME },
764 { "numberwithin", FT_WITHIN },
765 { "placement", FT_PLACEMENT },
766 { "style", FT_STYLE },
770 lexrc.pushTable(floatTags, FT_END);
779 bool builtin = false;
782 while (!getout && lexrc.isOK()) {
783 int le = lexrc.lex();
785 case Lexer::LEX_UNDEF:
786 lexrc.printError("Unknown ClassOption tag `$$Token'");
790 switch (static_cast<FloatTags>(le)) {
793 type = lexrc.getString();
794 if (floatlist_->typeExist(type)) {
795 Floating const & fl = floatlist_->getType(type);
796 placement = fl.placement();
798 within = fl.within();
801 listName = fl.listName();
802 builtin = fl.builtin();
807 name = lexrc.getString();
811 placement = lexrc.getString();
815 ext = lexrc.getString();
819 within = lexrc.getString();
820 if (within == "none")
825 style = lexrc.getString();
829 listName = lexrc.getString();
833 builtin = lexrc.getBool();
841 // Here if have a full float if getout == true
843 Floating fl(type, placement, ext, within,
844 style, name, listName, builtin);
845 floatlist_->newFloat(fl);
846 // each float has its own counter
847 counters_->newCounter(from_ascii(type), from_ascii(within),
848 docstring(), docstring());
859 CT_LABELSTRING_APPENDIX,
863 void TextClass::readCounter(Lexer & lexrc)
865 keyword_item counterTags[] = {
867 { "labelstring", CT_LABELSTRING },
868 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
870 { "within", CT_WITHIN }
873 lexrc.pushTable(counterTags, CT_END);
877 docstring labelstring;
878 docstring labelstring_appendix;
881 while (!getout && lexrc.isOK()) {
882 int le = lexrc.lex();
884 case Lexer::LEX_UNDEF:
885 lexrc.printError("Unknown ClassOption tag `$$Token'");
889 switch (static_cast<CounterTags>(le)) {
892 name = lexrc.getDocString();
893 if (counters_->hasCounter(name))
894 LYXERR(Debug::TCLASS)
895 << "Reading existing counter "
896 << to_utf8(name) << endl;
898 LYXERR(Debug::TCLASS)
899 << "Reading new counter "
900 << to_utf8(name) << endl;
904 within = lexrc.getDocString();
905 if (within == "none")
910 labelstring = lexrc.getDocString();
911 labelstring_appendix = labelstring;
913 case CT_LABELSTRING_APPENDIX:
915 labelstring_appendix = lexrc.getDocString();
923 // Here if have a full counter if getout == true
925 counters_->newCounter(name, within,
926 labelstring, labelstring_appendix);
932 Font const & TextClass::defaultfont() const
938 docstring const & TextClass::leftmargin() const
944 docstring const & TextClass::rightmargin() const
950 bool TextClass::hasLayout(docstring const & n) const
952 docstring const name = n.empty() ? defaultLayoutName() : n;
954 return find_if(layoutlist_.begin(), layoutlist_.end(),
955 LayoutNamesEqual(name))
956 != layoutlist_.end();
961 LayoutPtr const & TextClass::operator[](docstring const & name) const
963 BOOST_ASSERT(!name.empty());
965 LayoutList::const_iterator cit =
966 find_if(layoutlist_.begin(),
968 LayoutNamesEqual(name));
970 if (cit == layoutlist_.end()) {
971 lyxerr << "We failed to find the layout '" << to_utf8(name)
972 << "' in the layout list. You MUST investigate!"
974 for (LayoutList::const_iterator it = layoutlist_.begin();
975 it != layoutlist_.end(); ++it)
976 lyxerr << " " << to_utf8(it->get()->name()) << endl;
978 // we require the name to exist
987 bool TextClass::delete_layout(docstring const & name)
989 if (name == defaultLayoutName())
992 LayoutList::iterator it =
993 remove_if(layoutlist_.begin(), layoutlist_.end(),
994 LayoutNamesEqual(name));
996 LayoutList::iterator end = layoutlist_.end();
997 bool const ret = (it != end);
998 layoutlist_.erase(it, end);
1003 // Load textclass info if not loaded yet
1004 bool TextClass::load(string const & path) const
1009 // Read style-file, provided path is searched before the system ones
1010 FileName layout_file;
1012 layout_file = FileName(addName(path, name_ + ".layout"));
1013 if (layout_file.empty() || !layout_file.exists())
1014 layout_file = libFileSearch("layouts", name_, "layout");
1015 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1018 lyxerr << "Error reading `"
1019 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1020 << "'\n(Check `" << name_
1021 << "')\nCheck your installation and "
1022 "try Options/Reconfigure..." << endl;
1029 FloatList & TextClass::floats()
1031 return *floatlist_.get();
1035 FloatList const & TextClass::floats() const
1037 return *floatlist_.get();
1041 Counters & TextClass::counters() const
1043 return *counters_.get();
1047 // Return the layout object of an inset given by name. If the name
1048 // is not found as such, the part after the ':' is stripped off, and
1049 // searched again. In this way, an error fallback can be provided:
1050 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1051 // will invoke the layout object defined by name = 'CharStyle'.
1052 // If that doesn't work either, an empty object returns (shouldn't
1053 // happen). -- Idea JMarc, comment MV
1054 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1057 while (!n.empty()) {
1058 if (insetlayoutlist_.count(n) > 0)
1059 return insetlayoutlist_[n];
1060 docstring::size_type i = n.find(':');
1061 if (i == string::npos)
1065 static InsetLayout empty;
1066 empty.labelstring = from_utf8("UNDEFINED");
1067 empty.bgcolor = Color::error;
1072 docstring const & TextClass::defaultLayoutName() const
1074 // This really should come from the actual layout... (Lgb)
1075 return defaultlayout_;
1079 LayoutPtr const & TextClass::defaultLayout() const
1081 return operator[](defaultLayoutName());
1085 string const & TextClass::name() const
1091 string const & TextClass::latexname() const
1093 const_cast<TextClass*>(this)->load();
1098 string const & TextClass::description() const
1100 return description_;
1104 string const & TextClass::opt_fontsize() const
1106 return opt_fontsize_;
1110 string const & TextClass::opt_pagestyle() const
1112 return opt_pagestyle_;
1116 string const & TextClass::options() const
1122 string const & TextClass::class_header() const
1124 return class_header_;
1128 string const & TextClass::pagestyle() const
1134 docstring const & TextClass::preamble() const
1140 TextClass::PageSides TextClass::sides() const
1146 int TextClass::secnumdepth() const
1148 return secnumdepth_;
1152 int TextClass::tocdepth() const
1158 OutputType TextClass::outputType() const
1164 bool TextClass::provides(string const & p) const
1166 return provides_.find(p) != provides_.end();
1170 unsigned int TextClass::columns() const
1176 TitleLatexType TextClass::titletype() const
1182 string const & TextClass::titlename() const
1188 int TextClass::size() const
1190 return layoutlist_.size();
1194 int TextClass::min_toclevel() const
1196 return min_toclevel_;
1200 int TextClass::max_toclevel() const
1202 return max_toclevel_;
1206 bool TextClass::hasTocLevels() const
1208 return min_toclevel_ != Layout::NOT_IN_TOC;
1212 ostream & operator<<(ostream & os, TextClass::PageSides p)
1215 case TextClass::OneSide:
1218 case TextClass::TwoSides: