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"
32 #include "frontends/Alert.h"
42 BufferParams::BufferParams()
43 // Initialize textclass to point to article. if `first' is
44 // true in the returned pair, then `second' is the textclass
45 // number; if it is false, second is 0. In both cases, second
47 : textclass(textclasslist.NumberOfClass("article").second)
49 paragraph_separation = PARSEP_INDENT;
50 defskip = VSpace(VSpace::MEDSKIP);
51 quotes_language = InsetQuotes::EnglishQ;
52 quotes_times = InsetQuotes::DoubleQ;
56 papersize = PAPER_DEFAULT;
57 papersize2 = VM_PAPER_DEFAULT; /* DEFAULT */
58 paperpackage = PACKAGE_NONE;
59 orientation = ORIENTATION_PORTRAIT;
61 use_amsmath = AMS_AUTO;
63 use_numerical_citations = false;
64 tracking_changes = false;
67 language = default_language;
70 graphicsDriver = "default";
71 sides = LyXTextClass::OneSide;
73 pagestyle = "default";
74 for (int iter = 0; iter < 4; ++iter) {
75 user_defined_bullets[iter] = ITEMIZE_DEFAULTS[iter];
76 temp_bullets[iter] = ITEMIZE_DEFAULTS[iter];
81 string const BufferParams::readToken(LyXLex & lex, string const & token)
83 if (token == "\\textclass") {
85 string const classname = lex.getString();
86 pair<bool, lyx::textclass_type> pp =
87 textclasslist.NumberOfClass(classname);
89 textclass = pp.second;
94 if (!getLyXTextClass().isTeXClassAvailable()) {
95 string msg = bformat(_("The document uses a missing "
96 "TeX class \"%1$s\".\n"), classname);
97 Alert::warning(_("Document class not available"),
98 msg + _("LyX will not be able to produce output."));
100 } else if (token == "\\begin_preamble") {
102 } else if (token == "\\options") {
104 options = lex.getString();
105 } else if (token == "\\language") {
107 } else if (token == "\\inputencoding") {
109 inputenc = lex.getString();
110 } else if (token == "\\graphics") {
111 readGraphicsDriver(lex);
112 } else if (token == "\\fontscheme") {
114 fonts = lex.getString();
115 } else if (token == "\\paragraph_separation") {
116 int tmpret = lex.findToken(string_paragraph_separation);
119 paragraph_separation =
120 static_cast<BufferParams::PARSEP>(tmpret);
121 } else if (token == "\\defskip") {
123 defskip = VSpace(lex.getString());
124 } else if (token == "\\quotes_language") {
125 // FIXME: should be params.readQuotes()
126 int tmpret = lex.findToken(string_quotes_language);
129 InsetQuotes::quote_language tmpl =
130 InsetQuotes::EnglishQ;
133 tmpl = InsetQuotes::EnglishQ;
136 tmpl = InsetQuotes::SwedishQ;
139 tmpl = InsetQuotes::GermanQ;
142 tmpl = InsetQuotes::PolishQ;
145 tmpl = InsetQuotes::FrenchQ;
148 tmpl = InsetQuotes::DanishQ;
151 quotes_language = tmpl;
152 } else if (token == "\\quotes_times") {
153 // FIXME: should be params.readQuotes()
155 switch (lex.getInteger()) {
157 quotes_times = InsetQuotes::SingleQ;
160 quotes_times = InsetQuotes::DoubleQ;
163 } else if (token == "\\papersize") {
164 int tmpret = lex.findToken(string_papersize);
168 papersize2 = VMARGIN_PAPER_TYPE(tmpret);
169 } else if (token == "\\paperpackage") {
170 int tmpret = lex.findToken(string_paperpackages);
173 paperpackage = BufferParams::PACKAGE_NONE;
175 paperpackage = PAPER_PACKAGES(tmpret);
176 } else if (token == "\\use_geometry") {
178 use_geometry = lex.getInteger();
179 } else if (token == "\\use_amsmath") {
181 use_amsmath = static_cast<BufferParams::AMS>(
183 } else if (token == "\\use_natbib") {
185 use_natbib = lex.getInteger();
186 } else if (token == "\\use_numerical_citations") {
188 use_numerical_citations = lex.getInteger();
189 } else if (token == "\\tracking_changes") {
191 tracking_changes = lex.getInteger();
192 } else if (token == "\\author") {
194 istringstream ss(STRCONV(lex.getString()));
197 author_map.push_back(authorlist.record(a));
198 } else if (token == "\\paperorientation") {
199 int tmpret = lex.findToken(string_orientation);
203 static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
204 } else if (token == "\\paperwidth") {
206 paperwidth = lex.getString();
207 } else if (token == "\\paperheight") {
209 paperheight = lex.getString();
210 } else if (token == "\\leftmargin") {
212 leftmargin = lex.getString();
213 } else if (token == "\\topmargin") {
215 topmargin = lex.getString();
216 } else if (token == "\\rightmargin") {
218 rightmargin = lex.getString();
219 } else if (token == "\\bottommargin") {
221 bottommargin = lex.getString();
222 } else if (token == "\\headheight") {
224 headheight = lex.getString();
225 } else if (token == "\\headsep") {
227 headsep = lex.getString();
228 } else if (token == "\\footskip") {
230 footskip = lex.getString();
231 } else if (token == "\\paperfontsize") {
233 fontsize = rtrim(lex.getString());
234 } else if (token == "\\papercolumns") {
236 columns = lex.getInteger();
237 } else if (token == "\\papersides") {
239 switch (lex.getInteger()) {
241 case 1: sides = LyXTextClass::OneSide; break;
242 case 2: sides = LyXTextClass::TwoSides; break;
244 } else if (token == "\\paperpagestyle") {
246 pagestyle = rtrim(lex.getString());
247 } else if (token == "\\bullet") {
248 // FIXME: should be params.readBullets()
250 int const index = lex.getInteger();
252 int temp_int = lex.getInteger();
253 user_defined_bullets[index].setFont(temp_int);
254 temp_bullets[index].setFont(temp_int);
256 temp_int = lex.getInteger();
257 user_defined_bullets[index].setCharacter(temp_int);
258 temp_bullets[index].setCharacter(temp_int);
260 temp_int = lex.getInteger();
261 user_defined_bullets[index].setSize(temp_int);
262 temp_bullets[index].setSize(temp_int);
264 string const temp_str = lex.getString();
265 if (temp_str != "\\end_bullet") {
266 // this element isn't really necessary for
267 // parsing but is easier for humans
268 // to understand bullets. Put it back and
269 // set a debug message?
270 lex.printError("\\end_bullet expected, got" + temp_str);
271 //how can I put it back?
273 } else if (token == "\\bulletLaTeX") {
274 // The bullet class should be able to read this.
276 int const index = lex.getInteger();
278 string temp_str = lex.getString();
280 while (temp_str != "\\end_bullet") {
281 // this loop structure is needed when user
282 // enters an empty string since the first
283 // thing returned will be the \\end_bullet
285 // if the LaTeX entry has spaces. Each element
286 // therefore needs to be read in turn
289 temp_str = lex.getString();
292 user_defined_bullets[index].setText(sum_str);
293 temp_bullets[index].setText(sum_str);
294 } else if (token == "\\secnumdepth") {
296 secnumdepth = lex.getInteger();
297 } else if (token == "\\tocdepth") {
299 tocdepth = lex.getInteger();
300 } else if (token == "\\spacing") {
302 string const tmp = rtrim(lex.getString());
303 Spacing::Space tmp_space = Spacing::Default;
305 if (tmp == "single") {
306 tmp_space = Spacing::Single;
307 } else if (tmp == "onehalf") {
308 tmp_space = Spacing::Onehalf;
309 } else if (tmp == "double") {
310 tmp_space = Spacing::Double;
311 } else if (tmp == "other") {
313 tmp_space = Spacing::Other;
314 tmp_val = lex.getFloat();
316 lex.printError("Unknown spacing token: '$$Token'");
318 #if 0 // FIXME: Handled in lyx2lyx ?
319 // Small hack so that files written with klyx will be
322 par->params().spacing(Spacing(tmp_space, tmp_val));
324 spacing.set(tmp_space, tmp_val);
325 } else if (token == "\\float_placement") {
327 float_placement = lex.getString();
336 void BufferParams::writeFile(ostream & os) const
338 // The top of the file is written by the buffer.
339 // Prints out the buffer info into the .lyx file given by file
342 os << "\\textclass " << textclasslist[textclass].name() << '\n';
344 // then the the preamble
345 if (!preamble.empty()) {
346 // remove '\n' from the end of preamble
347 string const tmppreamble = rtrim(preamble, "\n");
348 os << "\\begin_preamble\n"
350 << "\n\\end_preamble\n";
354 if (!options.empty()) {
355 os << "\\options " << options << '\n';
358 /* then the text parameters */
359 if (language != ignore_language)
360 os << "\\language " << language->lang() << '\n';
361 os << "\\inputencoding " << inputenc
362 << "\n\\fontscheme " << fonts
363 << "\n\\graphics " << graphicsDriver << '\n';
365 if (!float_placement.empty()) {
366 os << "\\float_placement " << float_placement << '\n';
368 os << "\\paperfontsize " << fontsize << '\n';
370 spacing.writeFile(os);
372 os << "\\papersize " << string_papersize[papersize2]
373 << "\n\\paperpackage " << string_paperpackages[paperpackage]
374 << "\n\\use_geometry " << use_geometry
375 << "\n\\use_amsmath " << use_amsmath
376 << "\n\\use_natbib " << use_natbib
377 << "\n\\use_numerical_citations " << use_numerical_citations
378 << "\n\\paperorientation " << string_orientation[orientation]
380 if (!paperwidth.empty())
381 os << "\\paperwidth "
382 << VSpace(paperwidth).asLyXCommand() << '\n';
383 if (!paperheight.empty())
384 os << "\\paperheight "
385 << VSpace(paperheight).asLyXCommand() << '\n';
386 if (!leftmargin.empty())
387 os << "\\leftmargin "
388 << VSpace(leftmargin).asLyXCommand() << '\n';
389 if (!topmargin.empty())
391 << VSpace(topmargin).asLyXCommand() << '\n';
392 if (!rightmargin.empty())
393 os << "\\rightmargin "
394 << VSpace(rightmargin).asLyXCommand() << '\n';
395 if (!bottommargin.empty())
396 os << "\\bottommargin "
397 << VSpace(bottommargin).asLyXCommand() << '\n';
398 if (!headheight.empty())
399 os << "\\headheight "
400 << VSpace(headheight).asLyXCommand() << '\n';
401 if (!headsep.empty())
403 << VSpace(headsep).asLyXCommand() << '\n';
404 if (!footskip.empty())
406 << VSpace(footskip).asLyXCommand() << '\n';
407 os << "\\secnumdepth " << secnumdepth
408 << "\n\\tocdepth " << tocdepth
409 << "\n\\paragraph_separation "
410 << string_paragraph_separation[paragraph_separation]
411 << "\n\\defskip " << defskip.asLyXCommand()
412 << "\n\\quotes_language "
413 << string_quotes_language[quotes_language] << '\n';
414 switch (quotes_times) {
415 // An output operator for insetquotes would be nice
416 case InsetQuotes::SingleQ:
417 os << "\\quotes_times 1\n"; break;
418 case InsetQuotes::DoubleQ:
419 os << "\\quotes_times 2\n"; break;
421 os << "\\papercolumns " << columns
422 << "\n\\papersides " << sides
423 << "\n\\paperpagestyle " << pagestyle << '\n';
424 for (int i = 0; i < 4; ++i) {
425 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
426 if (user_defined_bullets[i].getFont() != -1) {
427 os << "\\bullet " << i
429 << user_defined_bullets[i].getFont()
431 << user_defined_bullets[i].getCharacter()
433 << user_defined_bullets[i].getSize()
434 << "\n\\end_bullet\n";
437 os << "\\bulletLaTeX " << i
439 << user_defined_bullets[i].getText()
440 << "\"\n\\end_bullet\n";
445 os << "\\tracking_changes " << tracking_changes << "\n";
447 if (tracking_changes) {
448 AuthorList::Authors::const_iterator it = authorlist.begin();
449 AuthorList::Authors::const_iterator end = authorlist.end();
450 for (; it != end; ++it) {
451 os << "\\author " << it->second << "\n";
457 bool BufferParams::writeLaTeX(ostream & os, LaTeXFeatures & features,
458 TexRow & texrow) const
460 os << "\\documentclass";
462 LyXTextClass const & tclass = getLyXTextClass();
464 ostringstream clsoptions; // the document class options.
466 if (tokenPos(tclass.opt_fontsize(),
467 '|', fontsize) >= 0) {
468 // only write if existing in list (and not default)
469 clsoptions << fontsize << "pt,";
474 (paperpackage == PACKAGE_NONE)) {
477 clsoptions << "a3paper,";
480 clsoptions << "a4paper,";
483 clsoptions << "letterpaper,";
486 clsoptions << "a5paper,";
489 clsoptions << "b5paper,";
491 case PAPER_EXECUTIVEPAPER:
492 clsoptions << "executivepaper,";
494 case PAPER_LEGALPAPER:
495 clsoptions << "legalpaper,";
503 if (sides != tclass.sides()) {
505 case LyXTextClass::OneSide:
506 clsoptions << "oneside,";
508 case LyXTextClass::TwoSides:
509 clsoptions << "twoside,";
515 if (columns != tclass.columns()) {
517 clsoptions << "twocolumn,";
519 clsoptions << "onecolumn,";
523 && orientation == ORIENTATION_LANDSCAPE)
524 clsoptions << "landscape,";
526 // language should be a parameter to \documentclass
527 if (language->babel() == "hebrew"
528 && default_language->babel() != "hebrew")
529 // This seems necessary
530 features.useLanguage(default_language);
532 ostringstream language_options;
533 bool const use_babel = features.useBabel();
535 language_options << features.getLanguages();
536 language_options << language->babel();
537 if (lyxrc.language_global_options)
538 clsoptions << language_options.str() << ',';
541 // the user-defined options
542 if (!options.empty()) {
543 clsoptions << options << ',';
546 string strOptions(STRCONV(clsoptions.str()));
547 if (!strOptions.empty()) {
548 strOptions = rtrim(strOptions, ",");
549 os << '[' << strOptions << ']';
552 os << '{' << tclass.latexname() << "}\n";
554 // end of \documentclass defs
556 // font selection must be done before loading fontenc.sty
557 // The ae package is not needed when using OT1 font encoding.
558 if (fonts != "default" &&
559 (fonts != "ae" || lyxrc.fontenc != "default")) {
560 os << "\\usepackage{" << fonts << "}\n";
563 os << "\\usepackage{aecompl}\n";
567 // this one is not per buffer
568 if (lyxrc.fontenc != "default") {
569 os << "\\usepackage[" << lyxrc.fontenc
574 if (inputenc == "auto") {
575 string const doc_encoding =
576 language->encoding()->LatexName();
578 // Create a list with all the input encodings used
580 std::set<string> encodings =
581 features.getEncodingSet(doc_encoding);
583 os << "\\usepackage[";
584 std::copy(encodings.begin(), encodings.end(),
585 std::ostream_iterator<string>(os, ","));
586 os << doc_encoding << "]{inputenc}\n";
588 } else if (inputenc != "default") {
589 os << "\\usepackage[" << inputenc
594 // At the very beginning the text parameters.
595 if (paperpackage != PACKAGE_NONE) {
596 switch (paperpackage) {
600 os << "\\usepackage{a4}\n";
604 os << "\\usepackage{a4wide}\n";
607 case PACKAGE_WIDEMARGINSA4:
608 os << "\\usepackage[widemargins]{a4}\n";
614 os << "\\usepackage{geometry}\n";
616 os << "\\geometry{verbose";
617 if (orientation == ORIENTATION_LANDSCAPE)
619 switch (papersize2) {
620 case VM_PAPER_CUSTOM:
621 if (!paperwidth.empty())
624 if (!paperheight.empty())
625 os << ",paperheight="
628 case VM_PAPER_USLETTER:
629 os << ",letterpaper";
631 case VM_PAPER_USLEGAL:
634 case VM_PAPER_USEXECUTIVE:
635 os << ",executivepaper";
656 // default papersize ie VM_PAPER_DEFAULT
657 switch (lyxrc.default_papersize) {
658 case PAPER_DEFAULT: // keep compiler happy
660 os << ",letterpaper";
662 case PAPER_LEGALPAPER:
665 case PAPER_EXECUTIVEPAPER:
666 os << ",executivepaper";
682 if (!topmargin.empty())
683 os << ",tmargin=" << topmargin;
684 if (!bottommargin.empty())
685 os << ",bmargin=" << bottommargin;
686 if (!leftmargin.empty())
687 os << ",lmargin=" << leftmargin;
688 if (!rightmargin.empty())
689 os << ",rmargin=" << rightmargin;
690 if (!headheight.empty())
691 os << ",headheight=" << headheight;
692 if (!headsep.empty())
693 os << ",headsep=" << headsep;
694 if (!footskip.empty())
695 os << ",footskip=" << footskip;
700 if (tokenPos(tclass.opt_pagestyle(),
701 '|', pagestyle) >= 0) {
702 if (pagestyle == "fancy") {
703 os << "\\usepackage{fancyhdr}\n";
706 os << "\\pagestyle{" << pagestyle << "}\n";
710 if (secnumdepth != tclass.secnumdepth()) {
711 os << "\\setcounter{secnumdepth}{"
716 if (tocdepth != tclass.tocdepth()) {
717 os << "\\setcounter{tocdepth}{"
723 if (paragraph_separation) {
724 switch (defskip.kind()) {
725 case VSpace::SMALLSKIP:
726 os << "\\setlength\\parskip{\\smallskipamount}\n";
728 case VSpace::MEDSKIP:
729 os << "\\setlength\\parskip{\\medskipamount}\n";
731 case VSpace::BIGSKIP:
732 os << "\\setlength\\parskip{\\bigskipamount}\n";
735 os << "\\setlength\\parskip{"
736 << defskip.length().asLatexString()
739 default: // should never happen // Then delete it.
740 os << "\\setlength\\parskip{\\medskipamount}\n";
745 os << "\\setlength\\parindent{0pt}\n";
749 // Now insert the LyX specific LaTeX commands...
751 // The optional packages;
752 string lyxpreamble(features.getPackages());
754 // this might be useful...
755 lyxpreamble += "\n\\makeatletter\n";
757 // Some macros LyX will need
758 string tmppreamble(features.getMacros());
760 if (!tmppreamble.empty()) {
761 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
762 "LyX specific LaTeX commands.\n"
763 + tmppreamble + '\n';
766 // the text class specific preamble
767 tmppreamble = features.getTClassPreamble();
768 if (!tmppreamble.empty()) {
769 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
770 "Textclass specific LaTeX commands.\n"
771 + tmppreamble + '\n';
774 /* the user-defined preamble */
775 if (!preamble.empty()) {
776 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
777 "User specified LaTeX commands.\n"
781 // Itemize bullet settings need to be last in case the user
782 // defines their own bullets that use a package included
783 // in the user-defined preamble -- ARRae
784 // Actually it has to be done much later than that
785 // since some packages like frenchb make modifications
786 // at \begin{document} time -- JMarc
788 for (int i = 0; i < 4; ++i) {
789 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
790 if (bullets_def.empty())
791 bullets_def="\\AtBeginDocument{\n";
792 bullets_def += " \\renewcommand{\\labelitemi";
794 // `i' is one less than the item to modify
807 bullets_def += "}{" +
808 user_defined_bullets[i].getText()
813 if (!bullets_def.empty())
814 lyxpreamble += bullets_def + "}\n\n";
816 // We try to load babel late, in case it interferes
817 // with other packages.
819 string tmp = lyxrc.language_package;
820 if (!lyxrc.language_global_options
821 && tmp == "\\usepackage{babel}")
822 tmp = string("\\usepackage[") +
823 STRCONV(language_options.str()) +
825 lyxpreamble += tmp + "\n";
826 lyxpreamble += features.getBabelOptions();
829 lyxpreamble += "\\makeatother\n";
831 // dvipost settings come after everything else
832 if (tracking_changes) {
835 "\\dvipost{osstart color push Red}\n"
836 "\\dvipost{osend color pop}\n"
837 "\\dvipost{cbstart color push Blue}\n"
838 "\\dvipost{cbend color pop} \n";
842 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
843 for (int j = 0; j != nlines; ++j) {
851 void BufferParams::setPaperStuff()
853 papersize = PAPER_DEFAULT;
854 char const c1 = paperpackage;
855 if (c1 == PACKAGE_NONE) {
856 char const c2 = papersize2;
857 if (c2 == VM_PAPER_USLETTER)
858 papersize = PAPER_USLETTER;
859 else if (c2 == VM_PAPER_USLEGAL)
860 papersize = PAPER_LEGALPAPER;
861 else if (c2 == VM_PAPER_USEXECUTIVE)
862 papersize = PAPER_EXECUTIVEPAPER;
863 else if (c2 == VM_PAPER_A3)
864 papersize = PAPER_A3PAPER;
865 else if (c2 == VM_PAPER_A4)
866 papersize = PAPER_A4PAPER;
867 else if (c2 == VM_PAPER_A5)
868 papersize = PAPER_A5PAPER;
869 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
871 papersize = PAPER_B5PAPER;
872 } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
873 (c1 == PACKAGE_WIDEMARGINSA4))
874 papersize = PAPER_A4PAPER;
878 void BufferParams::useClassDefaults()
880 LyXTextClass const & tclass = textclasslist[textclass];
882 sides = tclass.sides();
883 columns = tclass.columns();
884 pagestyle = tclass.pagestyle();
885 options = tclass.options();
886 secnumdepth = tclass.secnumdepth();
887 tocdepth = tclass.tocdepth();
891 bool BufferParams::hasClassDefaults() const
893 LyXTextClass const & tclass = textclasslist[textclass];
895 return (sides == tclass.sides()
896 && columns == tclass.columns()
897 && pagestyle == tclass.pagestyle()
898 && options == tclass.options()
899 && secnumdepth == tclass.secnumdepth()
900 && tocdepth == tclass.tocdepth());
904 LyXTextClass const & BufferParams::getLyXTextClass() const
906 return textclasslist[textclass];
910 void BufferParams::readPreamble(LyXLex & lex)
912 if (lex.getString() != "\\begin_preamble")
913 lyxerr << "Error (BufferParams::readPreamble):"
914 "consistency check failed." << endl;
916 preamble = lex.getLongString("\\end_preamble");
920 void BufferParams::readLanguage(LyXLex & lex)
922 if (!lex.next()) return;
924 string const tmptok = lex.getString();
926 // check if tmptok is part of tex_babel in tex-defs.h
927 language = languages.getLanguage(tmptok);
929 // Language tmptok was not found
930 language = default_language;
931 lyxerr << "Warning: Setting language `"
932 << tmptok << "' to `" << language->lang()
938 void BufferParams::readGraphicsDriver(LyXLex & lex)
940 if (!lex.next()) return;
942 string const tmptok = lex.getString();
943 // check if tmptok is part of tex_graphics in tex_defs.h
946 string const test = tex_graphics[n++];
948 if (test == tmptok) {
949 graphicsDriver = tmptok;
951 } else if (test == "last_item") {
953 "Warning: graphics driver `$$Token' not recognized!\n"
954 " Setting graphics driver to `default'.\n");
955 graphicsDriver = "default";