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"
42 using support::FileName;
43 using support::libFileSearch;
44 using support::makeDisplayPath;
45 using support::quoteName;
48 using support::addName;
53 class LayoutNamesEqual : public std::unary_function<LayoutPtr, bool> {
55 LayoutNamesEqual(docstring const & name)
58 bool operator()(LayoutPtr const & c) const
60 return c->name() == name_;
70 bool layout2layout(FileName const & filename, FileName const & tempfile)
72 FileName const script = libFileSearch("scripts", "layout2layout.py");
74 lyxerr << "Could not find layout conversion "
75 "script layout2layout.py." << endl;
79 std::ostringstream command;
80 command << support::os::python() << ' ' << quoteName(script.toFilesystemEncoding())
81 << ' ' << quoteName(filename.toFilesystemEncoding())
82 << ' ' << quoteName(tempfile.toFilesystemEncoding());
83 string const command_str = command.str();
85 LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
87 support::cmd_ret const ret =
88 support::runCommand(command_str);
90 lyxerr << "Could not run layout conversion "
91 "script layout2layout.py." << endl;
100 TextClass::TextClass(string const & fn, string const & cln,
101 string const & desc, bool texClassAvail )
102 : name_(fn), latexname_(cln), description_(desc),
103 floatlist_(new FloatList), counters_(new Counters),
104 texClassAvail_(texClassAvail)
112 pagestyle_ = "default";
113 defaultfont_ = sane_font;
114 opt_fontsize_ = "10|11|12";
115 opt_pagestyle_ = "empty|plain|headings|fancy";
116 titletype_ = TITLE_COMMAND_AFTER;
117 titlename_ = "maketitle";
122 bool TextClass::isTeXClassAvailable() const
124 return texClassAvail_;
128 bool TextClass::readStyle(Lexer & lexrc, Layout & lay)
130 LYXERR(Debug::TCLASS, "Reading style " << to_utf8(lay.name()));
131 if (!lay.read(lexrc, *this)) {
133 lay.resfont = lay.font;
134 lay.resfont.realize(defaultfont());
135 lay.reslabelfont = lay.labelfont;
136 lay.reslabelfont.realize(defaultfont());
137 return false; // no errors
139 lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
172 // Reads a textclass structure from file.
173 bool TextClass::read(FileName const & filename, ReadType rt)
175 if (!filename.isReadableFile()) {
176 lyxerr << "Cannot read layout file `" << filename << "'."
181 keyword_item textClassTags[] = {
182 { "classoptions", TC_CLASSOPTIONS },
183 { "columns", TC_COLUMNS },
184 { "counter", TC_COUNTER },
185 { "defaultfont", TC_DEFAULTFONT },
186 { "defaultstyle", TC_DEFAULTSTYLE },
187 { "environment", TC_ENVIRONMENT },
188 { "float", TC_FLOAT },
189 { "format", TC_FORMAT },
190 { "input", TC_INPUT },
191 { "insetlayout", TC_INSETLAYOUT },
192 { "leftmargin", TC_LEFTMARGIN },
193 { "nofloat", TC_NOFLOAT },
194 { "nostyle", TC_NOSTYLE },
195 { "outputtype", TC_OUTPUTTYPE },
196 { "pagestyle", TC_PAGESTYLE },
197 { "preamble", TC_PREAMBLE },
198 { "provides", TC_PROVIDES },
199 { "rightmargin", TC_RIGHTMARGIN },
200 { "secnumdepth", TC_SECNUMDEPTH },
201 { "sides", TC_SIDES },
202 { "style", TC_STYLE },
203 { "titlelatexname", TC_TITLELATEXNAME },
204 { "titlelatextype", TC_TITLELATEXTYPE },
205 { "tocdepth", TC_TOCDEPTH }
210 LYXERR(Debug::TCLASS, "Reading textclass ");
213 LYXERR(Debug::TCLASS, "Reading input file ");
216 LYXERR(Debug::TCLASS, "Reading module file ");
221 LYXERR(Debug::TCLASS, to_utf8(makeDisplayPath(filename.absFilename())));
223 Lexer lexrc(textClassTags,
224 sizeof(textClassTags) / sizeof(textClassTags[0]));
226 lexrc.setFile(filename);
227 bool error = !lexrc.isOK();
229 // Format of files before the 'Format' tag was introduced
233 while (lexrc.isOK() && !error) {
234 int le = lexrc.lex();
237 case Lexer::LEX_FEOF:
240 case Lexer::LEX_UNDEF:
241 lexrc.printError("Unknown TextClass tag `$$Token'");
249 switch (static_cast<TextClassTags>(le)) {
253 format = lexrc.getInteger();
256 case TC_OUTPUTTYPE: // output type definition
257 readOutputType(lexrc);
260 case TC_INPUT: // Include file
262 string const inc = lexrc.getString();
263 FileName tmp = libFileSearch("layouts", inc,
267 lexrc.printError("Could not find input"
270 } else if (read(tmp, MERGE)) {
271 lexrc.printError("Error reading input"
272 "file: " + tmp.absFilename());
278 case TC_DEFAULTSTYLE:
280 docstring const name = from_utf8(subst(lexrc.getString(),
282 defaultlayout_ = name;
289 docstring const name = from_utf8(subst(lexrc.getString(),
292 string s = "Could not read name for style: `$$Token' "
293 + lexrc.getString() + " is probably not valid UTF-8!";
294 lexrc.printError(s.c_str());
296 error = readStyle(lexrc, lay);
297 } else if (hasLayout(name)) {
298 Layout * lay = operator[](name).get();
299 error = readStyle(lexrc, *lay);
303 if (le == TC_ENVIRONMENT)
304 lay.is_environment = true;
305 error = readStyle(lexrc, lay);
307 layoutlist_.push_back(
308 boost::shared_ptr<Layout>(new Layout(lay))
311 if (defaultlayout_.empty()) {
312 // We do not have a default
313 // layout yet, so we choose
314 // the first layout we
316 defaultlayout_ = name;
321 lexrc.printError("No name given for style: `$$Token'.");
328 docstring const style = from_utf8(subst(lexrc.getString(),
330 if (!deleteLayout(style))
331 lyxerr << "Cannot delete style `"
332 << to_utf8(style) << '\'' << endl;
333 // lexrc.printError("Cannot delete style"
340 columns_ = lexrc.getInteger();
345 switch (lexrc.getInteger()) {
346 case 1: sides_ = OneSide; break;
347 case 2: sides_ = TwoSides; break;
349 lyxerr << "Impossible number of page"
350 " sides, setting to one."
360 pagestyle_ = rtrim(lexrc.getString());
364 defaultfont_ = lyxRead(lexrc);
365 if (!defaultfont_.resolved()) {
366 lexrc.printError("Warning: defaultfont should "
367 "be fully instantiated!");
368 defaultfont_.realize(sane_font);
374 secnumdepth_ = lexrc.getInteger();
379 tocdepth_ = lexrc.getInteger();
382 // First step to support options
383 case TC_CLASSOPTIONS:
384 readClassOptions(lexrc);
388 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
393 string const feature = lexrc.getString();
395 if (lexrc.getInteger())
396 provides_.insert(feature);
398 provides_.erase(feature);
402 case TC_LEFTMARGIN: // left margin type
404 leftmargin_ = lexrc.getDocString();
407 case TC_RIGHTMARGIN: // right margin type
409 rightmargin_ = lexrc.getDocString();
413 docstring const name = subst(lexrc.getDocString(), '_', ' ');
414 readInsetLayout(lexrc, name);
423 case TC_TITLELATEXTYPE:
424 readTitleType(lexrc);
426 case TC_TITLELATEXNAME:
428 titlename_ = lexrc.getString();
432 string const nofloat = lexrc.getString();
433 floatlist_->erase(nofloat);
437 if (format != FORMAT)
441 if (format != FORMAT) {
442 LYXERR(Debug::TCLASS, "Converting layout file from format "
443 << format << " to " << FORMAT);
444 FileName const tempfile(support::tempName());
445 error = !layout2layout(filename, tempfile);
447 error = read(tempfile, rt);
448 tempfile.removeFile();
453 LYXERR(Debug::TCLASS, "Finished reading module file "
454 << to_utf8(makeDisplayPath(filename.absFilename())));
455 else if (rt == MERGE)
456 LYXERR(Debug::TCLASS, "Finished reading input file "
457 << to_utf8(makeDisplayPath(filename.absFilename())));
458 else { // we are at top level here.
459 LYXERR(Debug::TCLASS, "Finished reading textclass "
460 << to_utf8(makeDisplayPath(filename.absFilename())));
461 if (defaultlayout_.empty()) {
462 lyxerr << "Error: Textclass '" << name_
463 << "' is missing a defaultstyle." << endl;
467 min_toclevel_ = Layout::NOT_IN_TOC;
468 max_toclevel_ = Layout::NOT_IN_TOC;
469 const_iterator cit = begin();
470 const_iterator the_end = end();
471 for ( ; cit != the_end ; ++cit) {
472 int const toclevel = (*cit)->toclevel;
473 if (toclevel != Layout::NOT_IN_TOC) {
474 if (min_toclevel_ == Layout::NOT_IN_TOC)
475 min_toclevel_ = toclevel;
477 min_toclevel_ = std::min(min_toclevel_,
479 max_toclevel_ = std::max(max_toclevel_,
483 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
484 << ", maximum is " << max_toclevel_);
492 void TextClass::readTitleType(Lexer & lexrc)
494 keyword_item titleTypeTags[] = {
495 { "commandafter", TITLE_COMMAND_AFTER },
496 { "environment", TITLE_ENVIRONMENT }
499 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
501 int le = lexrc.lex();
503 case Lexer::LEX_UNDEF:
504 lexrc.printError("Unknown output type `$$Token'");
506 case TITLE_COMMAND_AFTER:
507 case TITLE_ENVIRONMENT:
508 titletype_ = static_cast<TitleLatexType>(le);
511 lyxerr << "Unhandled value " << le
512 << " in TextClass::readTitleType." << endl;
519 void TextClass::readOutputType(Lexer & lexrc)
521 keyword_item outputTypeTags[] = {
522 { "docbook", DOCBOOK },
524 { "literate", LITERATE }
527 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
529 int le = lexrc.lex();
531 case Lexer::LEX_UNDEF:
532 lexrc.printError("Unknown output type `$$Token'");
537 outputType_ = static_cast<OutputType>(le);
540 lyxerr << "Unhandled value " << le
541 << " in TextClass::readOutputType." << endl;
548 enum ClassOptionsTags {
557 void TextClass::readClassOptions(Lexer & lexrc)
559 keyword_item classOptionsTags[] = {
561 {"fontsize", CO_FONTSIZE },
562 {"header", CO_HEADER },
563 {"other", CO_OTHER },
564 {"pagestyle", CO_PAGESTYLE }
567 lexrc.pushTable(classOptionsTags, CO_END);
569 while (!getout && lexrc.isOK()) {
570 int le = lexrc.lex();
572 case Lexer::LEX_UNDEF:
573 lexrc.printError("Unknown ClassOption tag `$$Token'");
577 switch (static_cast<ClassOptionsTags>(le)) {
580 opt_fontsize_ = rtrim(lexrc.getString());
584 opt_pagestyle_ = rtrim(lexrc.getString());
588 options_ = lexrc.getString();
592 class_header_ = subst(lexrc.getString(), """, "\"");
603 enum InsetLayoutTags {
624 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
626 keyword_item elementTags[] = {
627 { "bgcolor", IL_BGCOLOR },
628 { "decoration", IL_DECORATION },
631 { "forceltr", IL_FORCELTR },
632 { "freespacing", IL_FREESPACING },
633 { "keepempty", IL_KEEPEMPTY },
634 { "labelfont", IL_LABELFONT },
635 { "labelstring", IL_LABELSTRING },
636 { "latexname", IL_LATEXNAME },
637 { "latexparam", IL_LATEXPARAM },
638 { "latextype", IL_LATEXTYPE },
639 { "lyxtype", IL_LYXTYPE },
640 { "multipar", IL_MULTIPAR },
641 { "needprotect", IL_NEEDPROTECT },
642 { "passthru", IL_PASSTHRU },
643 { "preamble", IL_PREAMBLE }
646 lexrc.pushTable(elementTags, IL_END);
649 docstring labelstring;
654 FontInfo font = inherit_font;
655 FontInfo labelfont = inherit_font;
656 ColorCode bgcolor(Color_background);
658 bool multipar = false;
659 bool passthru = false;
660 bool needprotect = false;
661 bool keepempty = false;
662 bool freespacing = false;
663 bool forceltr = false;
666 while (!getout && lexrc.isOK()) {
667 int le = lexrc.lex();
669 case Lexer::LEX_UNDEF:
670 lexrc.printError("Unknown ClassOption tag `$$Token'");
674 switch (static_cast<InsetLayoutTags>(le)) {
677 lyxtype = lexrc.getString();
681 latextype = lexrc.getString();
685 labelstring = lexrc.getDocString();
689 decoration = lexrc.getString();
693 latexname = lexrc.getString();
697 latexparam = subst(lexrc.getString(), """, "\"");
700 labelfont = lyxRead(lexrc, inherit_font);
704 forceltr = lexrc.getBool();
708 multipar = lexrc.getBool();
712 passthru = lexrc.getBool();
716 keepempty = lexrc.getBool();
720 freespacing = lexrc.getBool();
724 needprotect = lexrc.getBool();
727 font = lyxRead(lexrc, inherit_font);
728 // So: define font before labelfont
733 string const token = lexrc.getString();
734 bgcolor = lcolor.getFromLyXName(token);
738 preamble = lexrc.getLongString("EndPreamble");
746 // Here add element to list if getout == true
749 il.name = to_ascii(name);
750 il.lyxtype = lyxtype;
751 il.labelstring = labelstring;
752 il.decoration = decoration;
753 il.latextype = latextype;
754 il.latexname = latexname;
755 il.latexparam = latexparam;
756 il.multipar = multipar;
757 il.passthru = passthru;
758 il.needprotect = needprotect;
759 il.freespacing = freespacing;
760 il.forceltr = forceltr;
761 il.keepempty = keepempty;
763 // The label font is generally used as-is without
764 // any realization against a given context.
765 labelfont.realize(sane_font);
766 il.labelfont = labelfont;
767 il.bgcolor = bgcolor;
768 il.preamble = preamble;
769 insetlayoutlist_[name] = il;
790 void TextClass::readFloat(Lexer & lexrc)
792 keyword_item floatTags[] = {
794 { "extension", FT_EXT },
795 { "guiname", FT_NAME },
796 { "latexbuiltin", FT_BUILTIN },
797 { "listname", FT_LISTNAME },
798 { "numberwithin", FT_WITHIN },
799 { "placement", FT_PLACEMENT },
800 { "style", FT_STYLE },
804 lexrc.pushTable(floatTags, FT_END);
813 bool builtin = false;
816 while (!getout && lexrc.isOK()) {
817 int le = lexrc.lex();
819 case Lexer::LEX_UNDEF:
820 lexrc.printError("Unknown ClassOption tag `$$Token'");
824 switch (static_cast<FloatTags>(le)) {
827 type = lexrc.getString();
828 if (floatlist_->typeExist(type)) {
829 Floating const & fl = floatlist_->getType(type);
830 placement = fl.placement();
832 within = fl.within();
835 listName = fl.listName();
836 builtin = fl.builtin();
841 name = lexrc.getString();
845 placement = lexrc.getString();
849 ext = lexrc.getString();
853 within = lexrc.getString();
854 if (within == "none")
859 style = lexrc.getString();
863 listName = lexrc.getString();
867 builtin = lexrc.getBool();
875 // Here if have a full float if getout == true
877 Floating fl(type, placement, ext, within,
878 style, name, listName, builtin);
879 floatlist_->newFloat(fl);
880 // each float has its own counter
881 counters_->newCounter(from_ascii(type), from_ascii(within),
882 docstring(), docstring());
893 CT_LABELSTRING_APPENDIX,
898 void TextClass::readCounter(Lexer & lexrc)
900 keyword_item counterTags[] = {
902 { "labelstring", CT_LABELSTRING },
903 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
905 { "within", CT_WITHIN }
908 lexrc.pushTable(counterTags, CT_END);
912 docstring labelstring;
913 docstring labelstring_appendix;
916 while (!getout && lexrc.isOK()) {
917 int le = lexrc.lex();
919 case Lexer::LEX_UNDEF:
920 lexrc.printError("Unknown ClassOption tag `$$Token'");
924 switch (static_cast<CounterTags>(le)) {
927 name = lexrc.getDocString();
928 if (counters_->hasCounter(name))
929 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
931 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
935 within = lexrc.getDocString();
936 if (within == "none")
941 labelstring = lexrc.getDocString();
942 labelstring_appendix = labelstring;
944 case CT_LABELSTRING_APPENDIX:
946 labelstring_appendix = lexrc.getDocString();
954 // Here if have a full counter if getout == true
956 counters_->newCounter(name, within,
957 labelstring, labelstring_appendix);
963 FontInfo const & TextClass::defaultfont() const
969 docstring const & TextClass::leftmargin() const
975 docstring const & TextClass::rightmargin() const
981 bool TextClass::hasLayout(docstring const & n) const
983 docstring const name = n.empty() ? defaultLayoutName() : n;
985 return find_if(layoutlist_.begin(), layoutlist_.end(),
986 LayoutNamesEqual(name))
987 != layoutlist_.end();
992 LayoutPtr const & TextClass::operator[](docstring const & name) const
994 BOOST_ASSERT(!name.empty());
996 LayoutList::const_iterator cit =
997 find_if(layoutlist_.begin(),
999 LayoutNamesEqual(name));
1001 if (cit == layoutlist_.end()) {
1002 lyxerr << "We failed to find the layout '" << to_utf8(name)
1003 << "' in the layout list. You MUST investigate!"
1005 for (LayoutList::const_iterator it = layoutlist_.begin();
1006 it != layoutlist_.end(); ++it)
1007 lyxerr << " " << to_utf8(it->get()->name()) << endl;
1009 // we require the name to exist
1010 BOOST_ASSERT(false);
1017 bool TextClass::deleteLayout(docstring const & name)
1019 if (name == defaultLayoutName())
1022 LayoutList::iterator it =
1023 remove_if(layoutlist_.begin(), layoutlist_.end(),
1024 LayoutNamesEqual(name));
1026 LayoutList::iterator end = layoutlist_.end();
1027 bool const ret = (it != end);
1028 layoutlist_.erase(it, end);
1033 // Load textclass info if not loaded yet
1034 bool TextClass::load(string const & path) const
1039 // Read style-file, provided path is searched before the system ones
1040 FileName layout_file;
1042 layout_file = FileName(addName(path, name_ + ".layout"));
1043 if (layout_file.empty() || !layout_file.exists())
1044 layout_file = libFileSearch("layouts", name_, "layout");
1045 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1048 lyxerr << "Error reading `"
1049 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1050 << "'\n(Check `" << name_
1051 << "')\nCheck your installation and "
1052 "try Options/Reconfigure..." << endl;
1059 FloatList & TextClass::floats()
1061 return *floatlist_.get();
1065 FloatList const & TextClass::floats() const
1067 return *floatlist_.get();
1071 Counters & TextClass::counters() const
1073 return *counters_.get();
1077 // Return the layout object of an inset given by name. If the name
1078 // is not found as such, the part after the ':' is stripped off, and
1079 // searched again. In this way, an error fallback can be provided:
1080 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1081 // will invoke the layout object defined by name = 'CharStyle'.
1082 // If that doesn't work either, an empty object returns (shouldn't
1083 // happen). -- Idea JMarc, comment MV
1084 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1087 while (!n.empty()) {
1088 if (insetlayoutlist_.count(n) > 0)
1089 return insetlayoutlist_[n];
1090 docstring::size_type i = n.find(':');
1091 if (i == string::npos)
1095 static InsetLayout empty;
1096 empty.labelstring = from_utf8("UNDEFINED");
1097 empty.labelfont = sane_font;
1098 empty.labelfont.setColor(Color_error);
1099 empty.bgcolor = Color_error;
1104 docstring const & TextClass::defaultLayoutName() const
1106 // This really should come from the actual layout... (Lgb)
1107 return defaultlayout_;
1111 LayoutPtr const & TextClass::defaultLayout() const
1113 return operator[](defaultLayoutName());
1117 string const & TextClass::name() const
1123 string const & TextClass::latexname() const
1125 const_cast<TextClass*>(this)->load();
1130 string const & TextClass::description() const
1132 return description_;
1136 string const & TextClass::opt_fontsize() const
1138 return opt_fontsize_;
1142 string const & TextClass::opt_pagestyle() const
1144 return opt_pagestyle_;
1148 string const & TextClass::options() const
1154 string const & TextClass::class_header() const
1156 return class_header_;
1160 string const & TextClass::pagestyle() const
1166 docstring const & TextClass::preamble() const
1172 PageSides TextClass::sides() const
1178 int TextClass::secnumdepth() const
1180 return secnumdepth_;
1184 int TextClass::tocdepth() const
1190 OutputType TextClass::outputType() const
1196 bool TextClass::provides(string const & p) const
1198 return provides_.find(p) != provides_.end();
1202 unsigned int TextClass::columns() const
1208 TitleLatexType TextClass::titletype() const
1214 string const & TextClass::titlename() const
1220 int TextClass::size() const
1222 return layoutlist_.size();
1226 int TextClass::min_toclevel() const
1228 return min_toclevel_;
1232 int TextClass::max_toclevel() const
1234 return max_toclevel_;
1238 bool TextClass::hasTocLevels() const
1240 return min_toclevel_ != Layout::NOT_IN_TOC;
1244 ostream & operator<<(ostream & os, PageSides p)