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 (!filename.isFileReadable()) {
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 {
626 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
628 keyword_item elementTags[] = {
629 { "bgcolor", IL_BGCOLOR },
630 { "decoration", IL_DECORATION },
633 { "labelfont", IL_LABELFONT },
634 { "labelstring", IL_LABELSTRING },
635 { "latexname", IL_LATEXNAME },
636 { "latexparam", IL_LATEXPARAM },
637 { "latextype", IL_LATEXTYPE },
638 { "lyxtype", IL_LYXTYPE },
639 { "multipar", IL_MULTIPAR },
640 { "preamble", IL_PREAMBLE },
641 { "verbatim", IL_VERBATIM }
644 lexrc.pushTable(elementTags, IL_END);
647 docstring labelstring;
652 Font font(defaultfont());
653 Font labelfont(defaultfont());
654 Color::color bgcolor(Color::background);
656 bool multipar(false);
657 bool verbatim(false);
660 while (!getout && lexrc.isOK()) {
661 int le = lexrc.lex();
663 case Lexer::LEX_UNDEF:
664 lexrc.printError("Unknown ClassOption tag `$$Token'");
668 switch (static_cast<InsetLayoutTags>(le)) {
671 lyxtype = lexrc.getString();
675 latextype = lexrc.getString();
679 labelstring = lexrc.getDocString();
683 decoration = lexrc.getString();
687 latexname = lexrc.getString();
691 latexparam = subst(lexrc.getString(), """, "\"");
694 labelfont.lyxRead(lexrc);
695 labelfont.realize(defaultfont());
699 multipar = lexrc.getBool();
703 verbatim = lexrc.getBool();
707 font.realize(defaultfont());
708 // So: define font before labelfont
713 string const token = lexrc.getString();
714 bgcolor = lcolor.getFromLyXName(token);
718 preamble = lexrc.getLongString("EndPreamble");
727 // Here add element to list if getout == true
730 il.name = to_ascii(name);
731 il.lyxtype = lyxtype;
732 il.labelstring = labelstring;
733 il.decoration = decoration;
734 il.latextype = latextype;
735 il.latexname = latexname;
736 il.latexparam = latexparam;
737 il.multipar = multipar;
738 il.verbatim = verbatim;
740 il.labelfont = labelfont;
741 il.bgcolor = bgcolor;
742 il.preamble = preamble;
743 insetlayoutlist_[name] = il;
764 void TextClass::readFloat(Lexer & lexrc)
766 keyword_item floatTags[] = {
768 { "extension", FT_EXT },
769 { "guiname", FT_NAME },
770 { "latexbuiltin", FT_BUILTIN },
771 { "listname", FT_LISTNAME },
772 { "numberwithin", FT_WITHIN },
773 { "placement", FT_PLACEMENT },
774 { "style", FT_STYLE },
778 lexrc.pushTable(floatTags, FT_END);
787 bool builtin = false;
790 while (!getout && lexrc.isOK()) {
791 int le = lexrc.lex();
793 case Lexer::LEX_UNDEF:
794 lexrc.printError("Unknown ClassOption tag `$$Token'");
798 switch (static_cast<FloatTags>(le)) {
801 type = lexrc.getString();
802 if (floatlist_->typeExist(type)) {
803 Floating const & fl = floatlist_->getType(type);
804 placement = fl.placement();
806 within = fl.within();
809 listName = fl.listName();
810 builtin = fl.builtin();
815 name = lexrc.getString();
819 placement = lexrc.getString();
823 ext = lexrc.getString();
827 within = lexrc.getString();
828 if (within == "none")
833 style = lexrc.getString();
837 listName = lexrc.getString();
841 builtin = lexrc.getBool();
849 // Here if have a full float if getout == true
851 Floating fl(type, placement, ext, within,
852 style, name, listName, builtin);
853 floatlist_->newFloat(fl);
854 // each float has its own counter
855 counters_->newCounter(from_ascii(type), from_ascii(within),
856 docstring(), docstring());
867 CT_LABELSTRING_APPENDIX,
871 void TextClass::readCounter(Lexer & lexrc)
873 keyword_item counterTags[] = {
875 { "labelstring", CT_LABELSTRING },
876 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
878 { "within", CT_WITHIN }
881 lexrc.pushTable(counterTags, CT_END);
885 docstring labelstring;
886 docstring labelstring_appendix;
889 while (!getout && lexrc.isOK()) {
890 int le = lexrc.lex();
892 case Lexer::LEX_UNDEF:
893 lexrc.printError("Unknown ClassOption tag `$$Token'");
897 switch (static_cast<CounterTags>(le)) {
900 name = lexrc.getDocString();
901 if (counters_->hasCounter(name))
902 LYXERR(Debug::TCLASS)
903 << "Reading existing counter "
904 << to_utf8(name) << endl;
906 LYXERR(Debug::TCLASS)
907 << "Reading new counter "
908 << to_utf8(name) << endl;
912 within = lexrc.getDocString();
913 if (within == "none")
918 labelstring = lexrc.getDocString();
919 labelstring_appendix = labelstring;
921 case CT_LABELSTRING_APPENDIX:
923 labelstring_appendix = lexrc.getDocString();
931 // Here if have a full counter if getout == true
933 counters_->newCounter(name, within,
934 labelstring, labelstring_appendix);
940 Font const & TextClass::defaultfont() const
946 docstring const & TextClass::leftmargin() const
952 docstring const & TextClass::rightmargin() const
958 bool TextClass::hasLayout(docstring const & n) const
960 docstring const name = n.empty() ? defaultLayoutName() : n;
962 return find_if(layoutlist_.begin(), layoutlist_.end(),
963 LayoutNamesEqual(name))
964 != layoutlist_.end();
969 LayoutPtr const & TextClass::operator[](docstring const & name) const
971 BOOST_ASSERT(!name.empty());
973 LayoutList::const_iterator cit =
974 find_if(layoutlist_.begin(),
976 LayoutNamesEqual(name));
978 if (cit == layoutlist_.end()) {
979 lyxerr << "We failed to find the layout '" << to_utf8(name)
980 << "' in the layout list. You MUST investigate!"
982 for (LayoutList::const_iterator it = layoutlist_.begin();
983 it != layoutlist_.end(); ++it)
984 lyxerr << " " << to_utf8(it->get()->name()) << endl;
986 // we require the name to exist
995 bool TextClass::delete_layout(docstring const & name)
997 if (name == defaultLayoutName())
1000 LayoutList::iterator it =
1001 remove_if(layoutlist_.begin(), layoutlist_.end(),
1002 LayoutNamesEqual(name));
1004 LayoutList::iterator end = layoutlist_.end();
1005 bool const ret = (it != end);
1006 layoutlist_.erase(it, end);
1011 // Load textclass info if not loaded yet
1012 bool TextClass::load(string const & path) const
1017 // Read style-file, provided path is searched before the system ones
1018 FileName layout_file;
1020 layout_file = FileName(addName(path, name_ + ".layout"));
1021 if (layout_file.empty() || !layout_file.exists())
1022 layout_file = libFileSearch("layouts", name_, "layout");
1023 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1026 lyxerr << "Error reading `"
1027 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1028 << "'\n(Check `" << name_
1029 << "')\nCheck your installation and "
1030 "try Options/Reconfigure..." << endl;
1037 FloatList & TextClass::floats()
1039 return *floatlist_.get();
1043 FloatList const & TextClass::floats() const
1045 return *floatlist_.get();
1049 Counters & TextClass::counters() const
1051 return *counters_.get();
1055 // Return the layout object of an inset given by name. If the name
1056 // is not found as such, the part after the ':' is stripped off, and
1057 // searched again. In this way, an error fallback can be provided:
1058 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1059 // will invoke the layout object defined by name = 'CharStyle'.
1060 // If that doesn't work either, an empty object returns (shouldn't
1061 // happen). -- Idea JMarc, comment MV
1062 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1065 while (!n.empty()) {
1066 if (insetlayoutlist_.count(n) > 0)
1067 return insetlayoutlist_[n];
1068 docstring::size_type i = n.find(':');
1069 if (i == string::npos)
1073 static InsetLayout empty;
1074 empty.labelstring = from_utf8("UNDEFINED");
1075 empty.bgcolor = Color::error;
1080 docstring const & TextClass::defaultLayoutName() const
1082 // This really should come from the actual layout... (Lgb)
1083 return defaultlayout_;
1087 LayoutPtr const & TextClass::defaultLayout() const
1089 return operator[](defaultLayoutName());
1093 string const & TextClass::name() const
1099 string const & TextClass::latexname() const
1101 const_cast<TextClass*>(this)->load();
1106 string const & TextClass::description() const
1108 return description_;
1112 string const & TextClass::opt_fontsize() const
1114 return opt_fontsize_;
1118 string const & TextClass::opt_pagestyle() const
1120 return opt_pagestyle_;
1124 string const & TextClass::options() const
1130 string const & TextClass::class_header() const
1132 return class_header_;
1136 string const & TextClass::pagestyle() const
1142 docstring const & TextClass::preamble() const
1148 TextClass::PageSides TextClass::sides() const
1154 int TextClass::secnumdepth() const
1156 return secnumdepth_;
1160 int TextClass::tocdepth() const
1166 OutputType TextClass::outputType() const
1172 bool TextClass::provides(string const & p) const
1174 return provides_.find(p) != provides_.end();
1178 unsigned int TextClass::columns() const
1184 TitleLatexType TextClass::titletype() const
1190 string const & TextClass::titlename() const
1196 int TextClass::size() const
1198 return layoutlist_.size();
1202 int TextClass::min_toclevel() const
1204 return min_toclevel_;
1208 int TextClass::max_toclevel() const
1210 return max_toclevel_;
1214 bool TextClass::hasTocLevels() const
1216 return min_toclevel_ != Layout::NOT_IN_TOC;
1220 ostream & operator<<(ostream & os, TextClass::PageSides p)
1223 case TextClass::OneSide:
1226 case TextClass::TwoSides: