1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
13 #include "bufferparams.h"
14 #include "tex-strings.h"
21 #include "lyxtextclasslist.h"
27 #include "support/lyxalgo.h" // for lyx::count
28 #include "support/lyxlib.h"
29 #include "support/lstrings.h"
30 #include "support/types.h"
31 #include "support/BoostFormat.h"
33 #include "frontends/Alert.h"
43 #warning Do we need this horrible thing? (JMarc)
47 BufferParams::BufferParams()
48 // Initialize textclass to point to article. if `first' is
49 // true in the returned pair, then `second' is the textclass
50 // number; if it is false, second is 0. In both cases, second
52 : textclass(textclasslist.NumberOfClass("article").second)
54 paragraph_separation = PARSEP_INDENT;
55 defskip = VSpace(VSpace::MEDSKIP);
56 quotes_language = InsetQuotes::EnglishQ;
57 quotes_times = InsetQuotes::DoubleQ;
61 papersize = PAPER_DEFAULT;
62 papersize2 = VM_PAPER_DEFAULT; /* DEFAULT */
63 paperpackage = PACKAGE_NONE;
64 orientation = ORIENTATION_PORTRAIT;
66 use_amsmath = AMS_AUTO;
68 use_numerical_citations = false;
69 tracking_changes = false;
72 language = default_language;
75 graphicsDriver = "default";
76 sides = LyXTextClass::OneSide;
78 pagestyle = "default";
79 for (int iter = 0; iter < 4; ++iter) {
80 user_defined_bullets[iter] = ITEMIZE_DEFAULTS[iter];
81 temp_bullets[iter] = ITEMIZE_DEFAULTS[iter];
86 string const BufferParams::readToken(LyXLex & lex, string const & token)
88 if (token == "\\textclass") {
90 string const classname = lex.getString();
91 pair<bool, lyx::textclass_type> pp =
92 textclasslist.NumberOfClass(classname);
94 textclass = pp.second;
99 if (!getLyXTextClass().isTeXClassAvailable()) {
102 boost::io::str(boost::format(_("The document uses a missing TeX class \"%1$s\".\n")) % classname);
104 _("The document uses a missing TeX class ") + classname + ".\n";
106 Alert::warning(_("Document class not available"),
107 msg + _("LyX will not be able to produce output."));
109 } else if (token == "\\begin_preamble") {
111 } else if (token == "\\options") {
113 options = lex.getString();
114 } else if (token == "\\language") {
116 } else if (token == "\\inputencoding") {
118 inputenc = lex.getString();
119 } else if (token == "\\graphics") {
120 readGraphicsDriver(lex);
121 } else if (token == "\\fontscheme") {
123 fonts = lex.getString();
124 } else if (token == "\\paragraph_separation") {
125 int tmpret = lex.findToken(string_paragraph_separation);
128 paragraph_separation =
129 static_cast<BufferParams::PARSEP>(tmpret);
130 } else if (token == "\\defskip") {
132 defskip = VSpace(lex.getString());
133 } else if (token == "\\quotes_language") {
134 // FIXME: should be params.readQuotes()
135 int tmpret = lex.findToken(string_quotes_language);
138 InsetQuotes::quote_language tmpl =
139 InsetQuotes::EnglishQ;
142 tmpl = InsetQuotes::EnglishQ;
145 tmpl = InsetQuotes::SwedishQ;
148 tmpl = InsetQuotes::GermanQ;
151 tmpl = InsetQuotes::PolishQ;
154 tmpl = InsetQuotes::FrenchQ;
157 tmpl = InsetQuotes::DanishQ;
160 quotes_language = tmpl;
161 } else if (token == "\\quotes_times") {
162 // FIXME: should be params.readQuotes()
164 switch (lex.getInteger()) {
166 quotes_times = InsetQuotes::SingleQ;
169 quotes_times = InsetQuotes::DoubleQ;
172 } else if (token == "\\papersize") {
173 int tmpret = lex.findToken(string_papersize);
178 } else if (token == "\\paperpackage") {
179 int tmpret = lex.findToken(string_paperpackages);
182 paperpackage = BufferParams::PACKAGE_NONE;
184 paperpackage = tmpret;
185 } else if (token == "\\use_geometry") {
187 use_geometry = lex.getInteger();
188 } else if (token == "\\use_amsmath") {
190 use_amsmath = static_cast<BufferParams::AMS>(
192 } else if (token == "\\use_natbib") {
194 use_natbib = lex.getInteger();
195 } else if (token == "\\use_numerical_citations") {
197 use_numerical_citations = lex.getInteger();
198 } else if (token == "\\tracking_changes") {
200 tracking_changes = lex.getInteger();
201 } else if (token == "\\author") {
203 istringstream ss(lex.getString());
206 author_map.push_back(authorlist.record(a));
207 } else if (token == "\\paperorientation") {
208 int tmpret = lex.findToken(string_orientation);
212 static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
213 } else if (token == "\\paperwidth") {
215 paperwidth = lex.getString();
216 } else if (token == "\\paperheight") {
218 paperheight = lex.getString();
219 } else if (token == "\\leftmargin") {
221 leftmargin = lex.getString();
222 } else if (token == "\\topmargin") {
224 topmargin = lex.getString();
225 } else if (token == "\\rightmargin") {
227 rightmargin = lex.getString();
228 } else if (token == "\\bottommargin") {
230 bottommargin = lex.getString();
231 } else if (token == "\\headheight") {
233 headheight = lex.getString();
234 } else if (token == "\\headsep") {
236 headsep = lex.getString();
237 } else if (token == "\\footskip") {
239 footskip = lex.getString();
240 } else if (token == "\\paperfontsize") {
242 fontsize = rtrim(lex.getString());
243 } else if (token == "\\papercolumns") {
245 columns = lex.getInteger();
246 } else if (token == "\\papersides") {
248 switch (lex.getInteger()) {
250 case 1: sides = LyXTextClass::OneSide; break;
251 case 2: sides = LyXTextClass::TwoSides; break;
253 } else if (token == "\\paperpagestyle") {
255 pagestyle = rtrim(lex.getString());
256 } else if (token == "\\bullet") {
257 // FIXME: should be params.readBullets()
259 int const index = lex.getInteger();
261 int temp_int = lex.getInteger();
262 user_defined_bullets[index].setFont(temp_int);
263 temp_bullets[index].setFont(temp_int);
265 temp_int = lex.getInteger();
266 user_defined_bullets[index].setCharacter(temp_int);
267 temp_bullets[index].setCharacter(temp_int);
269 temp_int = lex.getInteger();
270 user_defined_bullets[index].setSize(temp_int);
271 temp_bullets[index].setSize(temp_int);
273 string const temp_str = lex.getString();
274 if (temp_str != "\\end_bullet") {
275 // this element isn't really necessary for
276 // parsing but is easier for humans
277 // to understand bullets. Put it back and
278 // set a debug message?
279 lex.printError("\\end_bullet expected, got" + temp_str);
280 //how can I put it back?
282 } else if (token == "\\bulletLaTeX") {
283 // The bullet class should be able to read this.
285 int const index = lex.getInteger();
287 string temp_str = lex.getString();
289 while (temp_str != "\\end_bullet") {
290 // this loop structure is needed when user
291 // enters an empty string since the first
292 // thing returned will be the \\end_bullet
294 // if the LaTeX entry has spaces. Each element
295 // therefore needs to be read in turn
298 temp_str = lex.getString();
301 user_defined_bullets[index].setText(sum_str);
302 temp_bullets[index].setText(sum_str);
303 } else if (token == "\\secnumdepth") {
305 secnumdepth = lex.getInteger();
306 } else if (token == "\\tocdepth") {
308 tocdepth = lex.getInteger();
309 } else if (token == "\\spacing") {
311 string const tmp = rtrim(lex.getString());
312 Spacing::Space tmp_space = Spacing::Default;
314 if (tmp == "single") {
315 tmp_space = Spacing::Single;
316 } else if (tmp == "onehalf") {
317 tmp_space = Spacing::Onehalf;
318 } else if (tmp == "double") {
319 tmp_space = Spacing::Double;
320 } else if (tmp == "other") {
322 tmp_space = Spacing::Other;
323 tmp_val = lex.getFloat();
325 lex.printError("Unknown spacing token: '$$Token'");
327 #if 0 // FIXME: Handled in lyx2lyx ?
328 // Small hack so that files written with klyx will be
331 par->params().spacing(Spacing(tmp_space, tmp_val));
333 spacing.set(tmp_space, tmp_val);
334 } else if (token == "\\float_placement") {
336 float_placement = lex.getString();
345 void BufferParams::writeFile(ostream & os) const
347 // The top of the file is written by the buffer.
348 // Prints out the buffer info into the .lyx file given by file
351 os << "\\textclass " << textclasslist[textclass].name() << '\n';
353 // then the the preamble
354 if (!preamble.empty()) {
355 // remove '\n' from the end of preamble
356 string const tmppreamble = rtrim(preamble, "\n");
357 os << "\\begin_preamble\n"
359 << "\n\\end_preamble\n";
363 if (!options.empty()) {
364 os << "\\options " << options << '\n';
367 /* then the text parameters */
368 if (language != ignore_language)
369 os << "\\language " << language->lang() << '\n';
370 os << "\\inputencoding " << inputenc
371 << "\n\\fontscheme " << fonts
372 << "\n\\graphics " << graphicsDriver << '\n';
374 if (!float_placement.empty()) {
375 os << "\\float_placement " << float_placement << '\n';
377 os << "\\paperfontsize " << fontsize << '\n';
379 spacing.writeFile(os);
381 os << "\\papersize " << string_papersize[papersize2]
382 << "\n\\paperpackage " << string_paperpackages[paperpackage]
383 << "\n\\use_geometry " << use_geometry
384 << "\n\\use_amsmath " << use_amsmath
385 << "\n\\use_natbib " << use_natbib
386 << "\n\\use_numerical_citations " << use_numerical_citations
387 << "\n\\paperorientation " << string_orientation[orientation]
389 if (!paperwidth.empty())
390 os << "\\paperwidth "
391 << VSpace(paperwidth).asLyXCommand() << '\n';
392 if (!paperheight.empty())
393 os << "\\paperheight "
394 << VSpace(paperheight).asLyXCommand() << '\n';
395 if (!leftmargin.empty())
396 os << "\\leftmargin "
397 << VSpace(leftmargin).asLyXCommand() << '\n';
398 if (!topmargin.empty())
400 << VSpace(topmargin).asLyXCommand() << '\n';
401 if (!rightmargin.empty())
402 os << "\\rightmargin "
403 << VSpace(rightmargin).asLyXCommand() << '\n';
404 if (!bottommargin.empty())
405 os << "\\bottommargin "
406 << VSpace(bottommargin).asLyXCommand() << '\n';
407 if (!headheight.empty())
408 os << "\\headheight "
409 << VSpace(headheight).asLyXCommand() << '\n';
410 if (!headsep.empty())
412 << VSpace(headsep).asLyXCommand() << '\n';
413 if (!footskip.empty())
415 << VSpace(footskip).asLyXCommand() << '\n';
416 os << "\\secnumdepth " << secnumdepth
417 << "\n\\tocdepth " << tocdepth
418 << "\n\\paragraph_separation "
419 << string_paragraph_separation[paragraph_separation]
420 << "\n\\defskip " << defskip.asLyXCommand()
421 << "\n\\quotes_language "
422 << string_quotes_language[quotes_language] << '\n';
423 switch (quotes_times) {
424 // An output operator for insetquotes would be nice
425 case InsetQuotes::SingleQ:
426 os << "\\quotes_times 1\n"; break;
427 case InsetQuotes::DoubleQ:
428 os << "\\quotes_times 2\n"; break;
430 os << "\\papercolumns " << columns
431 << "\n\\papersides " << sides
432 << "\n\\paperpagestyle " << pagestyle << '\n';
433 for (int i = 0; i < 4; ++i) {
434 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
435 if (user_defined_bullets[i].getFont() != -1) {
436 os << "\\bullet " << i
438 << user_defined_bullets[i].getFont()
440 << user_defined_bullets[i].getCharacter()
442 << user_defined_bullets[i].getSize()
443 << "\n\\end_bullet\n";
446 os << "\\bulletLaTeX " << i
448 << user_defined_bullets[i].getText()
449 << "\"\n\\end_bullet\n";
454 os << "\\tracking_changes " << tracking_changes << "\n";
456 if (tracking_changes) {
457 AuthorList::Authors::const_iterator it = authorlist.begin();
458 AuthorList::Authors::const_iterator end = authorlist.end();
459 for (; it != end; ++it) {
460 os << "\\author " << it->second << "\n";
466 void BufferParams::writeLaTeX(ostream & os, LaTeXFeatures & features,
467 TexRow & texrow) const
469 os << "\\documentclass";
471 LyXTextClass const & tclass = getLyXTextClass();
473 ostringstream clsoptions; // the document class options.
475 if (tokenPos(tclass.opt_fontsize(),
476 '|', fontsize) >= 0) {
477 // only write if existing in list (and not default)
478 clsoptions << fontsize << "pt,";
483 (paperpackage == PACKAGE_NONE)) {
486 clsoptions << "a4paper,";
489 clsoptions << "letterpaper,";
492 clsoptions << "a5paper,";
495 clsoptions << "b5paper,";
497 case PAPER_EXECUTIVEPAPER:
498 clsoptions << "executivepaper,";
500 case PAPER_LEGALPAPER:
501 clsoptions << "legalpaper,";
507 if (sides != tclass.sides()) {
509 case LyXTextClass::OneSide:
510 clsoptions << "oneside,";
512 case LyXTextClass::TwoSides:
513 clsoptions << "twoside,";
519 if (columns != tclass.columns()) {
521 clsoptions << "twocolumn,";
523 clsoptions << "onecolumn,";
527 && orientation == ORIENTATION_LANDSCAPE)
528 clsoptions << "landscape,";
530 // language should be a parameter to \documentclass
532 ostringstream language_options;
533 if (language->babel() == "hebrew"
534 && default_language->babel() != "hebrew")
535 // This seems necessary
536 features.useLanguage(default_language);
538 if (lyxrc.language_use_babel ||
539 language->lang() != lyxrc.default_language ||
540 features.hasLanguages()) {
542 language_options << features.getLanguages();
543 language_options << language->babel();
544 if (lyxrc.language_global_options)
545 clsoptions << language_options.str() << ',';
548 // the user-defined options
549 if (!options.empty()) {
550 clsoptions << options << ',';
553 string strOptions(STRCONV(clsoptions.str()));
554 if (!strOptions.empty()) {
555 strOptions = rtrim(strOptions, ",");
556 os << '[' << strOptions << ']';
559 os << '{' << tclass.latexname() << "}\n";
561 // end of \documentclass defs
563 // font selection must be done before loading fontenc.sty
564 // The ae package is not needed when using OT1 font encoding.
565 if (fonts != "default" &&
566 (fonts != "ae" || lyxrc.fontenc != "default")) {
567 os << "\\usepackage{" << fonts << "}\n";
570 os << "\\usepackage{aecompl}\n";
574 // this one is not per buffer
575 if (lyxrc.fontenc != "default") {
576 os << "\\usepackage[" << lyxrc.fontenc
581 if (inputenc == "auto") {
582 string const doc_encoding =
583 language->encoding()->LatexName();
585 // Create a list with all the input encodings used
587 std::set<string> encodings =
588 features.getEncodingSet(doc_encoding);
590 os << "\\usepackage[";
591 std::copy(encodings.begin(), encodings.end(),
592 std::ostream_iterator<string>(os, ","));
593 os << doc_encoding << "]{inputenc}\n";
595 } else if (inputenc != "default") {
596 os << "\\usepackage[" << inputenc
601 // At the very beginning the text parameters.
602 if (paperpackage != PACKAGE_NONE) {
603 switch (paperpackage) {
605 os << "\\usepackage{a4}\n";
609 os << "\\usepackage{a4wide}\n";
612 case PACKAGE_WIDEMARGINSA4:
613 os << "\\usepackage[widemargins]{a4}\n";
619 os << "\\usepackage{geometry}\n";
621 os << "\\geometry{verbose";
622 if (orientation == ORIENTATION_LANDSCAPE)
624 switch (papersize2) {
625 case VM_PAPER_CUSTOM:
626 if (!paperwidth.empty())
629 if (!paperheight.empty())
630 os << ",paperheight="
633 case VM_PAPER_USLETTER:
634 os << ",letterpaper";
636 case VM_PAPER_USLEGAL:
639 case VM_PAPER_USEXECUTIVE:
640 os << ",executivepaper";
661 // default papersize ie VM_PAPER_DEFAULT
662 switch (lyxrc.default_papersize) {
663 case PAPER_DEFAULT: // keep compiler happy
665 os << ",letterpaper";
667 case PAPER_LEGALPAPER:
670 case PAPER_EXECUTIVEPAPER:
671 os << ",executivepaper";
687 if (!topmargin.empty())
688 os << ",tmargin=" << topmargin;
689 if (!bottommargin.empty())
690 os << ",bmargin=" << bottommargin;
691 if (!leftmargin.empty())
692 os << ",lmargin=" << leftmargin;
693 if (!rightmargin.empty())
694 os << ",rmargin=" << rightmargin;
695 if (!headheight.empty())
696 os << ",headheight=" << headheight;
697 if (!headsep.empty())
698 os << ",headsep=" << headsep;
699 if (!footskip.empty())
700 os << ",footskip=" << footskip;
705 if (tokenPos(tclass.opt_pagestyle(),
706 '|', pagestyle) >= 0) {
707 if (pagestyle == "fancy") {
708 os << "\\usepackage{fancyhdr}\n";
711 os << "\\pagestyle{" << pagestyle << "}\n";
715 if (secnumdepth != tclass.secnumdepth()) {
716 os << "\\setcounter{secnumdepth}{"
721 if (tocdepth != tclass.tocdepth()) {
722 os << "\\setcounter{tocdepth}{"
728 if (paragraph_separation) {
729 switch (defskip.kind()) {
730 case VSpace::SMALLSKIP:
731 os << "\\setlength\\parskip{\\smallskipamount}\n";
733 case VSpace::MEDSKIP:
734 os << "\\setlength\\parskip{\\medskipamount}\n";
736 case VSpace::BIGSKIP:
737 os << "\\setlength\\parskip{\\bigskipamount}\n";
740 os << "\\setlength\\parskip{"
741 << defskip.length().asLatexString()
744 default: // should never happen // Then delete it.
745 os << "\\setlength\\parskip{\\medskipamount}\n";
750 os << "\\setlength\\parindent{0pt}\n";
754 // Now insert the LyX specific LaTeX commands...
756 // The optional packages;
757 string lyxpreamble(features.getPackages());
759 // this might be useful...
760 lyxpreamble += "\n\\makeatletter\n";
762 // Some macros LyX will need
763 string tmppreamble(features.getMacros());
765 if (!tmppreamble.empty()) {
766 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
767 "LyX specific LaTeX commands.\n"
768 + tmppreamble + '\n';
771 // the text class specific preamble
772 tmppreamble = features.getTClassPreamble();
773 if (!tmppreamble.empty()) {
774 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
775 "Textclass specific LaTeX commands.\n"
776 + tmppreamble + '\n';
779 /* the user-defined preamble */
780 if (!preamble.empty()) {
781 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
782 "User specified LaTeX commands.\n"
786 // Itemize bullet settings need to be last in case the user
787 // defines their own bullets that use a package included
788 // in the user-defined preamble -- ARRae
789 // Actually it has to be done much later than that
790 // since some packages like frenchb make modifications
791 // at \begin{document} time -- JMarc
793 for (int i = 0; i < 4; ++i) {
794 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
795 if (bullets_def.empty())
796 bullets_def="\\AtBeginDocument{\n";
797 bullets_def += " \\renewcommand{\\labelitemi";
799 // `i' is one less than the item to modify
812 bullets_def += "}{" +
813 user_defined_bullets[i].getText()
818 if (!bullets_def.empty())
819 lyxpreamble += bullets_def + "}\n\n";
821 // We try to load babel late, in case it interferes
822 // with other packages.
824 string tmp = lyxrc.language_package;
825 if (!lyxrc.language_global_options
826 && tmp == "\\usepackage{babel}")
827 tmp = string("\\usepackage[") +
828 STRCONV(language_options.str()) +
830 lyxpreamble += tmp + "\n";
831 lyxpreamble += features.getBabelOptions();
834 lyxpreamble += "\\makeatother\n";
836 // dvipost settings come after everything else
837 if (tracking_changes) {
840 "\\dvipost{osstart color push Red}\n"
841 "\\dvipost{osend color pop}\n"
842 "\\dvipost{cbstart color push Blue}\n"
843 "\\dvipost{cbend color pop} \n";
847 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
848 for (int j = 0; j != nlines; ++j) {
855 void BufferParams::setPaperStuff()
857 papersize = PAPER_DEFAULT;
858 char const c1 = paperpackage;
859 if (c1 == PACKAGE_NONE) {
860 char const c2 = papersize2;
861 if (c2 == VM_PAPER_USLETTER)
862 papersize = PAPER_USLETTER;
863 else if (c2 == VM_PAPER_USLEGAL)
864 papersize = PAPER_LEGALPAPER;
865 else if (c2 == VM_PAPER_USEXECUTIVE)
866 papersize = PAPER_EXECUTIVEPAPER;
867 else if (c2 == VM_PAPER_A3)
868 papersize = PAPER_A3PAPER;
869 else if (c2 == VM_PAPER_A4)
870 papersize = PAPER_A4PAPER;
871 else if (c2 == VM_PAPER_A5)
872 papersize = PAPER_A5PAPER;
873 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
875 papersize = PAPER_B5PAPER;
876 } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
877 (c1 == PACKAGE_WIDEMARGINSA4))
878 papersize = PAPER_A4PAPER;
882 void BufferParams::useClassDefaults()
884 LyXTextClass const & tclass = textclasslist[textclass];
886 sides = tclass.sides();
887 columns = tclass.columns();
888 pagestyle = tclass.pagestyle();
889 options = tclass.options();
890 secnumdepth = tclass.secnumdepth();
891 tocdepth = tclass.tocdepth();
895 bool BufferParams::hasClassDefaults() const
897 LyXTextClass const & tclass = textclasslist[textclass];
899 return (sides == tclass.sides()
900 && columns == tclass.columns()
901 && pagestyle == tclass.pagestyle()
902 && options == tclass.options()
903 && secnumdepth == tclass.secnumdepth()
904 && tocdepth == tclass.tocdepth());
908 LyXTextClass const & BufferParams::getLyXTextClass() const
910 return textclasslist[textclass];
914 void BufferParams::readPreamble(LyXLex & lex)
916 if (lex.getString() != "\\begin_preamble")
917 lyxerr << "Error (BufferParams::readPreamble):"
918 "consistency check failed." << endl;
920 preamble = lex.getLongString("\\end_preamble");
924 void BufferParams::readLanguage(LyXLex & lex)
926 if (!lex.next()) return;
928 string const tmptok = lex.getString();
930 // check if tmptok is part of tex_babel in tex-defs.h
931 language = languages.getLanguage(tmptok);
933 // Language tmptok was not found
934 language = default_language;
935 lyxerr << "Warning: Setting language `"
936 << tmptok << "' to `" << language->lang()
942 void BufferParams::readGraphicsDriver(LyXLex & lex)
944 if (!lex.next()) return;
946 string const tmptok = lex.getString();
947 // check if tmptok is part of tex_graphics in tex_defs.h
950 string const test = tex_graphics[n++];
952 if (test == tmptok) {
953 graphicsDriver = tmptok;
955 } else if (test == "last_item") {
957 "Warning: graphics driver `$$Token' not recognized!\n"
958 " Setting graphics driver to `default'.\n");
959 graphicsDriver = "default";