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 #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()) {
100 string msg = bformat(_("The document uses a missing "
101 "TeX class \"%1$s\".\n"), classname);
102 Alert::warning(_("Document class not available"),
103 msg + _("LyX will not be able to produce output."));
105 } else if (token == "\\begin_preamble") {
107 } else if (token == "\\options") {
109 options = lex.getString();
110 } else if (token == "\\language") {
112 } else if (token == "\\inputencoding") {
114 inputenc = lex.getString();
115 } else if (token == "\\graphics") {
116 readGraphicsDriver(lex);
117 } else if (token == "\\fontscheme") {
119 fonts = lex.getString();
120 } else if (token == "\\paragraph_separation") {
121 int tmpret = lex.findToken(string_paragraph_separation);
124 paragraph_separation =
125 static_cast<BufferParams::PARSEP>(tmpret);
126 } else if (token == "\\defskip") {
128 defskip = VSpace(lex.getString());
129 } else if (token == "\\quotes_language") {
130 // FIXME: should be params.readQuotes()
131 int tmpret = lex.findToken(string_quotes_language);
134 InsetQuotes::quote_language tmpl =
135 InsetQuotes::EnglishQ;
138 tmpl = InsetQuotes::EnglishQ;
141 tmpl = InsetQuotes::SwedishQ;
144 tmpl = InsetQuotes::GermanQ;
147 tmpl = InsetQuotes::PolishQ;
150 tmpl = InsetQuotes::FrenchQ;
153 tmpl = InsetQuotes::DanishQ;
156 quotes_language = tmpl;
157 } else if (token == "\\quotes_times") {
158 // FIXME: should be params.readQuotes()
160 switch (lex.getInteger()) {
162 quotes_times = InsetQuotes::SingleQ;
165 quotes_times = InsetQuotes::DoubleQ;
168 } else if (token == "\\papersize") {
169 int tmpret = lex.findToken(string_papersize);
174 } else if (token == "\\paperpackage") {
175 int tmpret = lex.findToken(string_paperpackages);
178 paperpackage = BufferParams::PACKAGE_NONE;
180 paperpackage = tmpret;
181 } else if (token == "\\use_geometry") {
183 use_geometry = lex.getInteger();
184 } else if (token == "\\use_amsmath") {
186 use_amsmath = static_cast<BufferParams::AMS>(
188 } else if (token == "\\use_natbib") {
190 use_natbib = lex.getInteger();
191 } else if (token == "\\use_numerical_citations") {
193 use_numerical_citations = lex.getInteger();
194 } else if (token == "\\tracking_changes") {
196 tracking_changes = lex.getInteger();
197 } else if (token == "\\author") {
199 istringstream ss(STRCONV(lex.getString()));
202 author_map.push_back(authorlist.record(a));
203 } else if (token == "\\paperorientation") {
204 int tmpret = lex.findToken(string_orientation);
208 static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
209 } else if (token == "\\paperwidth") {
211 paperwidth = lex.getString();
212 } else if (token == "\\paperheight") {
214 paperheight = lex.getString();
215 } else if (token == "\\leftmargin") {
217 leftmargin = lex.getString();
218 } else if (token == "\\topmargin") {
220 topmargin = lex.getString();
221 } else if (token == "\\rightmargin") {
223 rightmargin = lex.getString();
224 } else if (token == "\\bottommargin") {
226 bottommargin = lex.getString();
227 } else if (token == "\\headheight") {
229 headheight = lex.getString();
230 } else if (token == "\\headsep") {
232 headsep = lex.getString();
233 } else if (token == "\\footskip") {
235 footskip = lex.getString();
236 } else if (token == "\\paperfontsize") {
238 fontsize = rtrim(lex.getString());
239 } else if (token == "\\papercolumns") {
241 columns = lex.getInteger();
242 } else if (token == "\\papersides") {
244 switch (lex.getInteger()) {
246 case 1: sides = LyXTextClass::OneSide; break;
247 case 2: sides = LyXTextClass::TwoSides; break;
249 } else if (token == "\\paperpagestyle") {
251 pagestyle = rtrim(lex.getString());
252 } else if (token == "\\bullet") {
253 // FIXME: should be params.readBullets()
255 int const index = lex.getInteger();
257 int temp_int = lex.getInteger();
258 user_defined_bullets[index].setFont(temp_int);
259 temp_bullets[index].setFont(temp_int);
261 temp_int = lex.getInteger();
262 user_defined_bullets[index].setCharacter(temp_int);
263 temp_bullets[index].setCharacter(temp_int);
265 temp_int = lex.getInteger();
266 user_defined_bullets[index].setSize(temp_int);
267 temp_bullets[index].setSize(temp_int);
269 string const temp_str = lex.getString();
270 if (temp_str != "\\end_bullet") {
271 // this element isn't really necessary for
272 // parsing but is easier for humans
273 // to understand bullets. Put it back and
274 // set a debug message?
275 lex.printError("\\end_bullet expected, got" + temp_str);
276 //how can I put it back?
278 } else if (token == "\\bulletLaTeX") {
279 // The bullet class should be able to read this.
281 int const index = lex.getInteger();
283 string temp_str = lex.getString();
285 while (temp_str != "\\end_bullet") {
286 // this loop structure is needed when user
287 // enters an empty string since the first
288 // thing returned will be the \\end_bullet
290 // if the LaTeX entry has spaces. Each element
291 // therefore needs to be read in turn
294 temp_str = lex.getString();
297 user_defined_bullets[index].setText(sum_str);
298 temp_bullets[index].setText(sum_str);
299 } else if (token == "\\secnumdepth") {
301 secnumdepth = lex.getInteger();
302 } else if (token == "\\tocdepth") {
304 tocdepth = lex.getInteger();
305 } else if (token == "\\spacing") {
307 string const tmp = rtrim(lex.getString());
308 Spacing::Space tmp_space = Spacing::Default;
310 if (tmp == "single") {
311 tmp_space = Spacing::Single;
312 } else if (tmp == "onehalf") {
313 tmp_space = Spacing::Onehalf;
314 } else if (tmp == "double") {
315 tmp_space = Spacing::Double;
316 } else if (tmp == "other") {
318 tmp_space = Spacing::Other;
319 tmp_val = lex.getFloat();
321 lex.printError("Unknown spacing token: '$$Token'");
323 #if 0 // FIXME: Handled in lyx2lyx ?
324 // Small hack so that files written with klyx will be
327 par->params().spacing(Spacing(tmp_space, tmp_val));
329 spacing.set(tmp_space, tmp_val);
330 } else if (token == "\\float_placement") {
332 float_placement = lex.getString();
341 void BufferParams::writeFile(ostream & os) const
343 // The top of the file is written by the buffer.
344 // Prints out the buffer info into the .lyx file given by file
347 os << "\\textclass " << textclasslist[textclass].name() << '\n';
349 // then the the preamble
350 if (!preamble.empty()) {
351 // remove '\n' from the end of preamble
352 string const tmppreamble = rtrim(preamble, "\n");
353 os << "\\begin_preamble\n"
355 << "\n\\end_preamble\n";
359 if (!options.empty()) {
360 os << "\\options " << options << '\n';
363 /* then the text parameters */
364 if (language != ignore_language)
365 os << "\\language " << language->lang() << '\n';
366 os << "\\inputencoding " << inputenc
367 << "\n\\fontscheme " << fonts
368 << "\n\\graphics " << graphicsDriver << '\n';
370 if (!float_placement.empty()) {
371 os << "\\float_placement " << float_placement << '\n';
373 os << "\\paperfontsize " << fontsize << '\n';
375 spacing.writeFile(os);
377 os << "\\papersize " << string_papersize[papersize2]
378 << "\n\\paperpackage " << string_paperpackages[paperpackage]
379 << "\n\\use_geometry " << use_geometry
380 << "\n\\use_amsmath " << use_amsmath
381 << "\n\\use_natbib " << use_natbib
382 << "\n\\use_numerical_citations " << use_numerical_citations
383 << "\n\\paperorientation " << string_orientation[orientation]
385 if (!paperwidth.empty())
386 os << "\\paperwidth "
387 << VSpace(paperwidth).asLyXCommand() << '\n';
388 if (!paperheight.empty())
389 os << "\\paperheight "
390 << VSpace(paperheight).asLyXCommand() << '\n';
391 if (!leftmargin.empty())
392 os << "\\leftmargin "
393 << VSpace(leftmargin).asLyXCommand() << '\n';
394 if (!topmargin.empty())
396 << VSpace(topmargin).asLyXCommand() << '\n';
397 if (!rightmargin.empty())
398 os << "\\rightmargin "
399 << VSpace(rightmargin).asLyXCommand() << '\n';
400 if (!bottommargin.empty())
401 os << "\\bottommargin "
402 << VSpace(bottommargin).asLyXCommand() << '\n';
403 if (!headheight.empty())
404 os << "\\headheight "
405 << VSpace(headheight).asLyXCommand() << '\n';
406 if (!headsep.empty())
408 << VSpace(headsep).asLyXCommand() << '\n';
409 if (!footskip.empty())
411 << VSpace(footskip).asLyXCommand() << '\n';
412 os << "\\secnumdepth " << secnumdepth
413 << "\n\\tocdepth " << tocdepth
414 << "\n\\paragraph_separation "
415 << string_paragraph_separation[paragraph_separation]
416 << "\n\\defskip " << defskip.asLyXCommand()
417 << "\n\\quotes_language "
418 << string_quotes_language[quotes_language] << '\n';
419 switch (quotes_times) {
420 // An output operator for insetquotes would be nice
421 case InsetQuotes::SingleQ:
422 os << "\\quotes_times 1\n"; break;
423 case InsetQuotes::DoubleQ:
424 os << "\\quotes_times 2\n"; break;
426 os << "\\papercolumns " << columns
427 << "\n\\papersides " << sides
428 << "\n\\paperpagestyle " << pagestyle << '\n';
429 for (int i = 0; i < 4; ++i) {
430 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
431 if (user_defined_bullets[i].getFont() != -1) {
432 os << "\\bullet " << i
434 << user_defined_bullets[i].getFont()
436 << user_defined_bullets[i].getCharacter()
438 << user_defined_bullets[i].getSize()
439 << "\n\\end_bullet\n";
442 os << "\\bulletLaTeX " << i
444 << user_defined_bullets[i].getText()
445 << "\"\n\\end_bullet\n";
450 os << "\\tracking_changes " << tracking_changes << "\n";
452 if (tracking_changes) {
453 AuthorList::Authors::const_iterator it = authorlist.begin();
454 AuthorList::Authors::const_iterator end = authorlist.end();
455 for (; it != end; ++it) {
456 os << "\\author " << it->second << "\n";
462 void BufferParams::writeLaTeX(ostream & os, LaTeXFeatures & features,
463 TexRow & texrow) const
465 os << "\\documentclass";
467 LyXTextClass const & tclass = getLyXTextClass();
469 ostringstream clsoptions; // the document class options.
471 if (tokenPos(tclass.opt_fontsize(),
472 '|', fontsize) >= 0) {
473 // only write if existing in list (and not default)
474 clsoptions << fontsize << "pt,";
479 (paperpackage == PACKAGE_NONE)) {
482 clsoptions << "a4paper,";
485 clsoptions << "letterpaper,";
488 clsoptions << "a5paper,";
491 clsoptions << "b5paper,";
493 case PAPER_EXECUTIVEPAPER:
494 clsoptions << "executivepaper,";
496 case PAPER_LEGALPAPER:
497 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
528 ostringstream language_options;
529 if (language->babel() == "hebrew"
530 && default_language->babel() != "hebrew")
531 // This seems necessary
532 features.useLanguage(default_language);
534 if (lyxrc.language_use_babel ||
535 language->lang() != lyxrc.default_language ||
536 features.hasLanguages()) {
538 language_options << features.getLanguages();
539 language_options << language->babel();
540 if (lyxrc.language_global_options)
541 clsoptions << language_options.str() << ',';
544 // the user-defined options
545 if (!options.empty()) {
546 clsoptions << options << ',';
549 string strOptions(STRCONV(clsoptions.str()));
550 if (!strOptions.empty()) {
551 strOptions = rtrim(strOptions, ",");
552 os << '[' << strOptions << ']';
555 os << '{' << tclass.latexname() << "}\n";
557 // end of \documentclass defs
559 // font selection must be done before loading fontenc.sty
560 // The ae package is not needed when using OT1 font encoding.
561 if (fonts != "default" &&
562 (fonts != "ae" || lyxrc.fontenc != "default")) {
563 os << "\\usepackage{" << fonts << "}\n";
566 os << "\\usepackage{aecompl}\n";
570 // this one is not per buffer
571 if (lyxrc.fontenc != "default") {
572 os << "\\usepackage[" << lyxrc.fontenc
577 if (inputenc == "auto") {
578 string const doc_encoding =
579 language->encoding()->LatexName();
581 // Create a list with all the input encodings used
583 std::set<string> encodings =
584 features.getEncodingSet(doc_encoding);
586 os << "\\usepackage[";
587 std::copy(encodings.begin(), encodings.end(),
588 std::ostream_iterator<string>(os, ","));
589 os << doc_encoding << "]{inputenc}\n";
591 } else if (inputenc != "default") {
592 os << "\\usepackage[" << inputenc
597 // At the very beginning the text parameters.
598 if (paperpackage != PACKAGE_NONE) {
599 switch (paperpackage) {
601 os << "\\usepackage{a4}\n";
605 os << "\\usepackage{a4wide}\n";
608 case PACKAGE_WIDEMARGINSA4:
609 os << "\\usepackage[widemargins]{a4}\n";
615 os << "\\usepackage{geometry}\n";
617 os << "\\geometry{verbose";
618 if (orientation == ORIENTATION_LANDSCAPE)
620 switch (papersize2) {
621 case VM_PAPER_CUSTOM:
622 if (!paperwidth.empty())
625 if (!paperheight.empty())
626 os << ",paperheight="
629 case VM_PAPER_USLETTER:
630 os << ",letterpaper";
632 case VM_PAPER_USLEGAL:
635 case VM_PAPER_USEXECUTIVE:
636 os << ",executivepaper";
657 // default papersize ie VM_PAPER_DEFAULT
658 switch (lyxrc.default_papersize) {
659 case PAPER_DEFAULT: // keep compiler happy
661 os << ",letterpaper";
663 case PAPER_LEGALPAPER:
666 case PAPER_EXECUTIVEPAPER:
667 os << ",executivepaper";
683 if (!topmargin.empty())
684 os << ",tmargin=" << topmargin;
685 if (!bottommargin.empty())
686 os << ",bmargin=" << bottommargin;
687 if (!leftmargin.empty())
688 os << ",lmargin=" << leftmargin;
689 if (!rightmargin.empty())
690 os << ",rmargin=" << rightmargin;
691 if (!headheight.empty())
692 os << ",headheight=" << headheight;
693 if (!headsep.empty())
694 os << ",headsep=" << headsep;
695 if (!footskip.empty())
696 os << ",footskip=" << footskip;
701 if (tokenPos(tclass.opt_pagestyle(),
702 '|', pagestyle) >= 0) {
703 if (pagestyle == "fancy") {
704 os << "\\usepackage{fancyhdr}\n";
707 os << "\\pagestyle{" << pagestyle << "}\n";
711 if (secnumdepth != tclass.secnumdepth()) {
712 os << "\\setcounter{secnumdepth}{"
717 if (tocdepth != tclass.tocdepth()) {
718 os << "\\setcounter{tocdepth}{"
724 if (paragraph_separation) {
725 switch (defskip.kind()) {
726 case VSpace::SMALLSKIP:
727 os << "\\setlength\\parskip{\\smallskipamount}\n";
729 case VSpace::MEDSKIP:
730 os << "\\setlength\\parskip{\\medskipamount}\n";
732 case VSpace::BIGSKIP:
733 os << "\\setlength\\parskip{\\bigskipamount}\n";
736 os << "\\setlength\\parskip{"
737 << defskip.length().asLatexString()
740 default: // should never happen // Then delete it.
741 os << "\\setlength\\parskip{\\medskipamount}\n";
746 os << "\\setlength\\parindent{0pt}\n";
750 // Now insert the LyX specific LaTeX commands...
752 // The optional packages;
753 string lyxpreamble(features.getPackages());
755 // this might be useful...
756 lyxpreamble += "\n\\makeatletter\n";
758 // Some macros LyX will need
759 string tmppreamble(features.getMacros());
761 if (!tmppreamble.empty()) {
762 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
763 "LyX specific LaTeX commands.\n"
764 + tmppreamble + '\n';
767 // the text class specific preamble
768 tmppreamble = features.getTClassPreamble();
769 if (!tmppreamble.empty()) {
770 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
771 "Textclass specific LaTeX commands.\n"
772 + tmppreamble + '\n';
775 /* the user-defined preamble */
776 if (!preamble.empty()) {
777 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
778 "User specified LaTeX commands.\n"
782 // Itemize bullet settings need to be last in case the user
783 // defines their own bullets that use a package included
784 // in the user-defined preamble -- ARRae
785 // Actually it has to be done much later than that
786 // since some packages like frenchb make modifications
787 // at \begin{document} time -- JMarc
789 for (int i = 0; i < 4; ++i) {
790 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
791 if (bullets_def.empty())
792 bullets_def="\\AtBeginDocument{\n";
793 bullets_def += " \\renewcommand{\\labelitemi";
795 // `i' is one less than the item to modify
808 bullets_def += "}{" +
809 user_defined_bullets[i].getText()
814 if (!bullets_def.empty())
815 lyxpreamble += bullets_def + "}\n\n";
817 // We try to load babel late, in case it interferes
818 // with other packages.
820 string tmp = lyxrc.language_package;
821 if (!lyxrc.language_global_options
822 && tmp == "\\usepackage{babel}")
823 tmp = string("\\usepackage[") +
824 STRCONV(language_options.str()) +
826 lyxpreamble += tmp + "\n";
827 lyxpreamble += features.getBabelOptions();
830 lyxpreamble += "\\makeatother\n";
832 // dvipost settings come after everything else
833 if (tracking_changes) {
836 "\\dvipost{osstart color push Red}\n"
837 "\\dvipost{osend color pop}\n"
838 "\\dvipost{cbstart color push Blue}\n"
839 "\\dvipost{cbend color pop} \n";
843 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
844 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";