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"
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"
39 using support::FileName;
40 using support::libFileSearch;
41 using support::makeDisplayPath;
42 using support::quoteName;
45 using support::addName;
56 class LayoutNamesEqual : public std::unary_function<LayoutPtr, bool> {
58 LayoutNamesEqual(docstring const & name)
61 bool operator()(LayoutPtr const & c) const
63 return c->name() == name_;
73 bool layout2layout(FileName const & filename, FileName const & tempfile)
75 FileName const script = libFileSearch("scripts", "layout2layout.py");
77 lyxerr << "Could not find layout conversion "
78 "script layout2layout.py." << endl;
82 std::ostringstream command;
83 command << support::os::python() << ' ' << quoteName(script.toFilesystemEncoding())
84 << ' ' << quoteName(filename.toFilesystemEncoding())
85 << ' ' << quoteName(tempfile.toFilesystemEncoding());
86 string const command_str = command.str();
88 LYXERR(Debug::TCLASS) << "Running `" << command_str << '\'' << endl;
90 support::cmd_ret const ret =
91 support::runCommand(command_str);
93 lyxerr << "Could not run layout conversion "
94 "script layout2layout.py." << endl;
103 TextClass::TextClass(string const & fn, string const & cln,
104 string const & desc, bool texClassAvail )
105 : name_(fn), latexname_(cln), description_(desc),
106 floatlist_(new FloatList), counters_(new Counters),
107 texClassAvail_(texClassAvail)
115 pagestyle_ = "default";
116 defaultfont_ = Font(Font::ALL_SANE);
117 opt_fontsize_ = "10|11|12";
118 opt_pagestyle_ = "empty|plain|headings|fancy";
119 titletype_ = TITLE_COMMAND_AFTER;
120 titlename_ = "maketitle";
125 bool TextClass::isTeXClassAvailable() const
127 return texClassAvail_;
131 bool TextClass::do_readStyle(Lexer & lexrc, Layout & lay)
133 LYXERR(Debug::TCLASS) << "Reading style " << to_utf8(lay.name()) << endl;
134 if (!lay.read(lexrc, *this)) {
136 lay.resfont = lay.font;
137 lay.resfont.realize(defaultfont());
138 lay.reslabelfont = lay.labelfont;
139 lay.reslabelfont.realize(defaultfont());
140 return false; // no errors
142 lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
175 // Reads a textclass structure from file.
176 bool TextClass::read(FileName const & filename, ReadType rt)
178 if (!filename.isFileReadable()) {
179 lyxerr << "Cannot read layout file `" << filename << "'."
184 keyword_item textClassTags[] = {
185 { "classoptions", TC_CLASSOPTIONS },
186 { "columns", TC_COLUMNS },
187 { "counter", TC_COUNTER },
188 { "defaultfont", TC_DEFAULTFONT },
189 { "defaultstyle", TC_DEFAULTSTYLE },
190 { "environment", TC_ENVIRONMENT },
191 { "float", TC_FLOAT },
192 { "format", TC_FORMAT },
193 { "input", TC_INPUT },
194 { "insetlayout", TC_INSETLAYOUT },
195 { "leftmargin", TC_LEFTMARGIN },
196 { "nofloat", TC_NOFLOAT },
197 { "nostyle", TC_NOSTYLE },
198 { "outputtype", TC_OUTPUTTYPE },
199 { "pagestyle", TC_PAGESTYLE },
200 { "preamble", TC_PREAMBLE },
201 { "provides", TC_PROVIDES },
202 { "rightmargin", TC_RIGHTMARGIN },
203 { "secnumdepth", TC_SECNUMDEPTH },
204 { "sides", TC_SIDES },
205 { "style", TC_STYLE },
206 { "titlelatexname", TC_TITLELATEXNAME },
207 { "titlelatextype", TC_TITLELATEXTYPE },
208 { "tocdepth", TC_TOCDEPTH }
213 LYXERR(Debug::TCLASS) << "Reading textclass ";
216 LYXERR(Debug::TCLASS) << "Reading input file ";
219 LYXERR(Debug::TCLASS) << "Reading module file ";
224 LYXERR(Debug::TCLASS) << to_utf8(makeDisplayPath(filename.absFilename()))
227 Lexer lexrc(textClassTags,
228 sizeof(textClassTags) / sizeof(textClassTags[0]));
230 lexrc.setFile(filename);
231 bool error = !lexrc.isOK();
233 // Format of files before the 'Format' tag was introduced
237 while (lexrc.isOK() && !error) {
238 int le = lexrc.lex();
241 case Lexer::LEX_FEOF:
244 case Lexer::LEX_UNDEF:
245 lexrc.printError("Unknown TextClass tag `$$Token'");
253 switch (static_cast<TextClassTags>(le)) {
257 format = lexrc.getInteger();
260 case TC_OUTPUTTYPE: // output type definition
261 readOutputType(lexrc);
264 case TC_INPUT: // Include file
266 string const inc = lexrc.getString();
267 FileName tmp = libFileSearch("layouts", inc,
271 lexrc.printError("Could not find input"
274 } else if (read(tmp, MERGE)) {
275 lexrc.printError("Error reading input"
276 "file: " + tmp.absFilename());
282 case TC_DEFAULTSTYLE:
284 docstring const name = from_utf8(subst(lexrc.getString(),
286 defaultlayout_ = name;
293 docstring const name = from_utf8(subst(lexrc.getString(),
296 string s = "Could not read name for style: `$$Token' "
297 + lexrc.getString() + " is probably not valid UTF-8!";
298 lexrc.printError(s.c_str());
300 error = do_readStyle(lexrc, lay);
301 } else if (hasLayout(name)) {
302 Layout * lay = operator[](name).get();
303 error = do_readStyle(lexrc, *lay);
307 if (le == TC_ENVIRONMENT)
308 lay.is_environment = true;
309 error = do_readStyle(lexrc, lay);
311 layoutlist_.push_back(
312 boost::shared_ptr<Layout>(new Layout(lay))
315 if (defaultlayout_.empty()) {
316 // We do not have a default
317 // layout yet, so we choose
318 // the first layout we
320 defaultlayout_ = name;
325 lexrc.printError("No name given for style: `$$Token'.");
332 docstring const style = from_utf8(subst(lexrc.getString(),
334 if (!delete_layout(style))
335 lyxerr << "Cannot delete style `"
336 << to_utf8(style) << '\'' << endl;
337 // lexrc.printError("Cannot delete style"
344 columns_ = lexrc.getInteger();
349 switch (lexrc.getInteger()) {
350 case 1: sides_ = OneSide; break;
351 case 2: sides_ = TwoSides; break;
353 lyxerr << "Impossible number of page"
354 " sides, setting to one."
364 pagestyle_ = rtrim(lexrc.getString());
368 defaultfont_.lyxRead(lexrc);
369 if (!defaultfont_.resolved()) {
370 lexrc.printError("Warning: defaultfont should "
371 "be fully instantiated!");
372 defaultfont_.realize(Font(Font::ALL_SANE));
378 secnumdepth_ = lexrc.getInteger();
383 tocdepth_ = lexrc.getInteger();
386 // First step to support options
387 case TC_CLASSOPTIONS:
388 readClassOptions(lexrc);
392 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
397 string const feature = lexrc.getString();
399 if (lexrc.getInteger())
400 provides_.insert(feature);
402 provides_.erase(feature);
406 case TC_LEFTMARGIN: // left margin type
408 leftmargin_ = lexrc.getDocString();
411 case TC_RIGHTMARGIN: // right margin type
413 rightmargin_ = lexrc.getDocString();
417 docstring const name = subst(lexrc.getDocString(), '_', ' ');
418 readInsetLayout(lexrc, name);
427 case TC_TITLELATEXTYPE:
428 readTitleType(lexrc);
430 case TC_TITLELATEXNAME:
432 titlename_ = lexrc.getString();
436 string const nofloat = lexrc.getString();
437 floatlist_->erase(nofloat);
441 if (format != FORMAT)
445 if (format != FORMAT) {
446 LYXERR(Debug::TCLASS) << "Converting layout file from format "
447 << format << " to " << FORMAT << endl;
448 FileName const tempfile(support::tempName());
449 error = !layout2layout(filename, tempfile);
451 error = read(tempfile, rt);
452 support::unlink(tempfile);
457 LYXERR(Debug::TCLASS) << "Finished reading module file "
458 << to_utf8(makeDisplayPath(filename.absFilename()))
460 else if (rt == MERGE)
461 LYXERR(Debug::TCLASS) << "Finished reading input file "
462 << to_utf8(makeDisplayPath(filename.absFilename()))
464 else { // we are at top level here.
465 LYXERR(Debug::TCLASS) << "Finished reading textclass "
466 << to_utf8(makeDisplayPath(filename.absFilename()))
468 if (defaultlayout_.empty()) {
469 lyxerr << "Error: Textclass '" << name_
470 << "' is missing a defaultstyle." << endl;
474 min_toclevel_ = Layout::NOT_IN_TOC;
475 max_toclevel_ = Layout::NOT_IN_TOC;
476 const_iterator cit = begin();
477 const_iterator the_end = end();
478 for ( ; cit != the_end ; ++cit) {
479 int const toclevel = (*cit)->toclevel;
480 if (toclevel != Layout::NOT_IN_TOC) {
481 if (min_toclevel_ == Layout::NOT_IN_TOC)
482 min_toclevel_ = toclevel;
484 min_toclevel_ = std::min(min_toclevel_,
486 max_toclevel_ = std::max(max_toclevel_,
490 LYXERR(Debug::TCLASS)
491 << "Minimum TocLevel is " << min_toclevel_
492 << ", maximum is " << max_toclevel_ <<endl;
500 void TextClass::readTitleType(Lexer & lexrc)
502 keyword_item titleTypeTags[] = {
503 { "commandafter", TITLE_COMMAND_AFTER },
504 { "environment", TITLE_ENVIRONMENT }
507 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
509 int le = lexrc.lex();
511 case Lexer::LEX_UNDEF:
512 lexrc.printError("Unknown output type `$$Token'");
514 case TITLE_COMMAND_AFTER:
515 case TITLE_ENVIRONMENT:
516 titletype_ = static_cast<TitleLatexType>(le);
519 lyxerr << "Unhandled value " << le
520 << " in TextClass::readTitleType." << endl;
527 void TextClass::readOutputType(Lexer & lexrc)
529 keyword_item outputTypeTags[] = {
530 { "docbook", DOCBOOK },
532 { "literate", LITERATE }
535 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
537 int le = lexrc.lex();
539 case Lexer::LEX_UNDEF:
540 lexrc.printError("Unknown output type `$$Token'");
545 outputType_ = static_cast<OutputType>(le);
548 lyxerr << "Unhandled value " << le
549 << " in TextClass::readOutputType." << endl;
556 enum ClassOptionsTags {
565 void TextClass::readClassOptions(Lexer & lexrc)
567 keyword_item classOptionsTags[] = {
569 {"fontsize", CO_FONTSIZE },
570 {"header", CO_HEADER },
571 {"other", CO_OTHER },
572 {"pagestyle", CO_PAGESTYLE }
575 lexrc.pushTable(classOptionsTags, CO_END);
577 while (!getout && lexrc.isOK()) {
578 int le = lexrc.lex();
580 case Lexer::LEX_UNDEF:
581 lexrc.printError("Unknown ClassOption tag `$$Token'");
585 switch (static_cast<ClassOptionsTags>(le)) {
588 opt_fontsize_ = rtrim(lexrc.getString());
592 opt_pagestyle_ = rtrim(lexrc.getString());
596 options_ = lexrc.getString();
600 class_header_ = subst(lexrc.getString(), """, "\"");
611 enum InsetLayoutTags {
631 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
633 keyword_item elementTags[] = {
634 { "bgcolor", IL_BGCOLOR },
635 { "decoration", IL_DECORATION },
638 { "freespacing", IL_FREESPACING },
639 { "keepempty", IL_KEEPEMPTY },
640 { "labelfont", IL_LABELFONT },
641 { "labelstring", IL_LABELSTRING },
642 { "latexname", IL_LATEXNAME },
643 { "latexparam", IL_LATEXPARAM },
644 { "latextype", IL_LATEXTYPE },
645 { "lyxtype", IL_LYXTYPE },
646 { "multipar", IL_MULTIPAR },
647 { "needprotect", IL_NEEDPROTECT },
648 { "passthru", IL_PASSTHRU },
649 { "preamble", IL_PREAMBLE }
652 lexrc.pushTable(elementTags, IL_END);
655 docstring labelstring;
660 Font font(defaultfont());
661 Font labelfont(defaultfont());
662 ColorCode bgcolor(Color_background);
664 bool multipar(false);
665 bool passthru(false);
666 bool needprotect(false);
667 bool keepempty(false);
668 bool freespacing(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);
706 labelfont.realize(defaultfont());
710 multipar = lexrc.getBool();
714 passthru = lexrc.getBool();
718 keepempty = lexrc.getBool();
722 freespacing = lexrc.getBool();
726 needprotect = lexrc.getBool();
730 font.realize(defaultfont());
731 // So: define font before labelfont
736 string const token = lexrc.getString();
737 bgcolor = lcolor.getFromLyXName(token);
741 preamble = lexrc.getLongString("EndPreamble");
750 // Here add element to list if getout == true
753 il.name = to_ascii(name);
754 il.lyxtype = lyxtype;
755 il.labelstring = labelstring;
756 il.decoration = decoration;
757 il.latextype = latextype;
758 il.latexname = latexname;
759 il.latexparam = latexparam;
760 il.multipar = multipar;
761 il.passthru = passthru;
762 il.needprotect = needprotect;
763 il.freespacing = freespacing;
764 il.keepempty = keepempty;
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,
897 void TextClass::readCounter(Lexer & lexrc)
899 keyword_item counterTags[] = {
901 { "labelstring", CT_LABELSTRING },
902 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
904 { "within", CT_WITHIN }
907 lexrc.pushTable(counterTags, CT_END);
911 docstring labelstring;
912 docstring labelstring_appendix;
915 while (!getout && lexrc.isOK()) {
916 int le = lexrc.lex();
918 case Lexer::LEX_UNDEF:
919 lexrc.printError("Unknown ClassOption tag `$$Token'");
923 switch (static_cast<CounterTags>(le)) {
926 name = lexrc.getDocString();
927 if (counters_->hasCounter(name))
928 LYXERR(Debug::TCLASS)
929 << "Reading existing counter "
930 << to_utf8(name) << endl;
932 LYXERR(Debug::TCLASS)
933 << "Reading new counter "
934 << to_utf8(name) << endl;
938 within = lexrc.getDocString();
939 if (within == "none")
944 labelstring = lexrc.getDocString();
945 labelstring_appendix = labelstring;
947 case CT_LABELSTRING_APPENDIX:
949 labelstring_appendix = lexrc.getDocString();
957 // Here if have a full counter if getout == true
959 counters_->newCounter(name, within,
960 labelstring, labelstring_appendix);
966 Font const & TextClass::defaultfont() const
972 docstring const & TextClass::leftmargin() const
978 docstring const & TextClass::rightmargin() const
984 bool TextClass::hasLayout(docstring const & n) const
986 docstring const name = n.empty() ? defaultLayoutName() : n;
988 return find_if(layoutlist_.begin(), layoutlist_.end(),
989 LayoutNamesEqual(name))
990 != layoutlist_.end();
995 LayoutPtr const & TextClass::operator[](docstring const & name) const
997 BOOST_ASSERT(!name.empty());
999 LayoutList::const_iterator cit =
1000 find_if(layoutlist_.begin(),
1002 LayoutNamesEqual(name));
1004 if (cit == layoutlist_.end()) {
1005 lyxerr << "We failed to find the layout '" << to_utf8(name)
1006 << "' in the layout list. You MUST investigate!"
1008 for (LayoutList::const_iterator it = layoutlist_.begin();
1009 it != layoutlist_.end(); ++it)
1010 lyxerr << " " << to_utf8(it->get()->name()) << endl;
1012 // we require the name to exist
1013 BOOST_ASSERT(false);
1021 bool TextClass::delete_layout(docstring const & name)
1023 if (name == defaultLayoutName())
1026 LayoutList::iterator it =
1027 remove_if(layoutlist_.begin(), layoutlist_.end(),
1028 LayoutNamesEqual(name));
1030 LayoutList::iterator end = layoutlist_.end();
1031 bool const ret = (it != end);
1032 layoutlist_.erase(it, end);
1037 // Load textclass info if not loaded yet
1038 bool TextClass::load(string const & path) const
1043 // Read style-file, provided path is searched before the system ones
1044 FileName layout_file;
1046 layout_file = FileName(addName(path, name_ + ".layout"));
1047 if (layout_file.empty() || !layout_file.exists())
1048 layout_file = libFileSearch("layouts", name_, "layout");
1049 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1052 lyxerr << "Error reading `"
1053 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1054 << "'\n(Check `" << name_
1055 << "')\nCheck your installation and "
1056 "try Options/Reconfigure..." << endl;
1063 FloatList & TextClass::floats()
1065 return *floatlist_.get();
1069 FloatList const & TextClass::floats() const
1071 return *floatlist_.get();
1075 Counters & TextClass::counters() const
1077 return *counters_.get();
1081 // Return the layout object of an inset given by name. If the name
1082 // is not found as such, the part after the ':' is stripped off, and
1083 // searched again. In this way, an error fallback can be provided:
1084 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1085 // will invoke the layout object defined by name = 'CharStyle'.
1086 // If that doesn't work either, an empty object returns (shouldn't
1087 // happen). -- Idea JMarc, comment MV
1088 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1091 while (!n.empty()) {
1092 if (insetlayoutlist_.count(n) > 0)
1093 return insetlayoutlist_[n];
1094 docstring::size_type i = n.find(':');
1095 if (i == string::npos)
1099 static InsetLayout empty;
1100 empty.labelstring = from_utf8("UNDEFINED");
1101 empty.bgcolor = Color_error;
1106 docstring const & TextClass::defaultLayoutName() const
1108 // This really should come from the actual layout... (Lgb)
1109 return defaultlayout_;
1113 LayoutPtr const & TextClass::defaultLayout() const
1115 return operator[](defaultLayoutName());
1119 string const & TextClass::name() const
1125 string const & TextClass::latexname() const
1127 const_cast<TextClass*>(this)->load();
1132 string const & TextClass::description() const
1134 return description_;
1138 string const & TextClass::opt_fontsize() const
1140 return opt_fontsize_;
1144 string const & TextClass::opt_pagestyle() const
1146 return opt_pagestyle_;
1150 string const & TextClass::options() const
1156 string const & TextClass::class_header() const
1158 return class_header_;
1162 string const & TextClass::pagestyle() const
1168 docstring const & TextClass::preamble() const
1174 TextClass::PageSides TextClass::sides() const
1180 int TextClass::secnumdepth() const
1182 return secnumdepth_;
1186 int TextClass::tocdepth() const
1192 OutputType TextClass::outputType() const
1198 bool TextClass::provides(string const & p) const
1200 return provides_.find(p) != provides_.end();
1204 unsigned int TextClass::columns() const
1210 TitleLatexType TextClass::titletype() const
1216 string const & TextClass::titlename() const
1222 int TextClass::size() const
1224 return layoutlist_.size();
1228 int TextClass::min_toclevel() const
1230 return min_toclevel_;
1234 int TextClass::max_toclevel() const
1236 return max_toclevel_;
1240 bool TextClass::hasTocLevels() const
1242 return min_toclevel_ != Layout::NOT_IN_TOC;
1246 ostream & operator<<(ostream & os, TextClass::PageSides p)
1249 case TextClass::OneSide:
1252 case TextClass::TwoSides: