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)
46 BufferParams::BufferParams()
47 // Initialize textclass to point to article. if `first' is
48 // true in the returned pair, then `second' is the textclass
49 // number; if it is false, second is 0. In both cases, second
51 : textclass(textclasslist.NumberOfClass("article").second)
53 paragraph_separation = PARSEP_INDENT;
54 defskip = VSpace(VSpace::MEDSKIP);
55 quotes_language = InsetQuotes::EnglishQ;
56 quotes_times = InsetQuotes::DoubleQ;
60 papersize = PAPER_DEFAULT;
61 papersize2 = VM_PAPER_DEFAULT; /* DEFAULT */
62 paperpackage = PACKAGE_NONE;
63 orientation = ORIENTATION_PORTRAIT;
65 use_amsmath = AMS_AUTO;
67 use_numerical_citations = false;
68 tracking_changes = false;
71 language = default_language;
74 graphicsDriver = "default";
75 sides = LyXTextClass::OneSide;
77 pagestyle = "default";
78 for (int iter = 0; iter < 4; ++iter) {
79 user_defined_bullets[iter] = ITEMIZE_DEFAULTS[iter];
80 temp_bullets[iter] = ITEMIZE_DEFAULTS[iter];
85 string const BufferParams::readToken(LyXLex & lex, string const & token)
87 if (token == "\\textclass") {
89 string const classname = lex.getString();
90 pair<bool, lyx::textclass_type> pp =
91 textclasslist.NumberOfClass(classname);
93 textclass = pp.second;
98 if (!getLyXTextClass().isTeXClassAvailable()) {
99 string msg = bformat(_("The document uses a missing "
100 "TeX class \"%1$s\".\n"), classname);
101 Alert::warning(_("Document class not available"),
102 msg + _("LyX will not be able to produce output."));
104 } else if (token == "\\begin_preamble") {
106 } else if (token == "\\options") {
108 options = lex.getString();
109 } else if (token == "\\language") {
111 } else if (token == "\\inputencoding") {
113 inputenc = lex.getString();
114 } else if (token == "\\graphics") {
115 readGraphicsDriver(lex);
116 } else if (token == "\\fontscheme") {
118 fonts = lex.getString();
119 } else if (token == "\\paragraph_separation") {
120 int tmpret = lex.findToken(string_paragraph_separation);
123 paragraph_separation =
124 static_cast<BufferParams::PARSEP>(tmpret);
125 } else if (token == "\\defskip") {
127 defskip = VSpace(lex.getString());
128 } else if (token == "\\quotes_language") {
129 // FIXME: should be params.readQuotes()
130 int tmpret = lex.findToken(string_quotes_language);
133 InsetQuotes::quote_language tmpl =
134 InsetQuotes::EnglishQ;
137 tmpl = InsetQuotes::EnglishQ;
140 tmpl = InsetQuotes::SwedishQ;
143 tmpl = InsetQuotes::GermanQ;
146 tmpl = InsetQuotes::PolishQ;
149 tmpl = InsetQuotes::FrenchQ;
152 tmpl = InsetQuotes::DanishQ;
155 quotes_language = tmpl;
156 } else if (token == "\\quotes_times") {
157 // FIXME: should be params.readQuotes()
159 switch (lex.getInteger()) {
161 quotes_times = InsetQuotes::SingleQ;
164 quotes_times = InsetQuotes::DoubleQ;
167 } else if (token == "\\papersize") {
168 int tmpret = lex.findToken(string_papersize);
173 } else if (token == "\\paperpackage") {
174 int tmpret = lex.findToken(string_paperpackages);
177 paperpackage = BufferParams::PACKAGE_NONE;
179 paperpackage = tmpret;
180 } else if (token == "\\use_geometry") {
182 use_geometry = lex.getInteger();
183 } else if (token == "\\use_amsmath") {
185 use_amsmath = static_cast<BufferParams::AMS>(
187 } else if (token == "\\use_natbib") {
189 use_natbib = lex.getInteger();
190 } else if (token == "\\use_numerical_citations") {
192 use_numerical_citations = lex.getInteger();
193 } else if (token == "\\tracking_changes") {
195 tracking_changes = lex.getInteger();
196 } else if (token == "\\author") {
198 istringstream ss(lex.getString());
201 author_map.push_back(authorlist.record(a));
202 } else if (token == "\\paperorientation") {
203 int tmpret = lex.findToken(string_orientation);
207 static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
208 } else if (token == "\\paperwidth") {
210 paperwidth = lex.getString();
211 } else if (token == "\\paperheight") {
213 paperheight = lex.getString();
214 } else if (token == "\\leftmargin") {
216 leftmargin = lex.getString();
217 } else if (token == "\\topmargin") {
219 topmargin = lex.getString();
220 } else if (token == "\\rightmargin") {
222 rightmargin = lex.getString();
223 } else if (token == "\\bottommargin") {
225 bottommargin = lex.getString();
226 } else if (token == "\\headheight") {
228 headheight = lex.getString();
229 } else if (token == "\\headsep") {
231 headsep = lex.getString();
232 } else if (token == "\\footskip") {
234 footskip = lex.getString();
235 } else if (token == "\\paperfontsize") {
237 fontsize = rtrim(lex.getString());
238 } else if (token == "\\papercolumns") {
240 columns = lex.getInteger();
241 } else if (token == "\\papersides") {
243 switch (lex.getInteger()) {
245 case 1: sides = LyXTextClass::OneSide; break;
246 case 2: sides = LyXTextClass::TwoSides; break;
248 } else if (token == "\\paperpagestyle") {
250 pagestyle = rtrim(lex.getString());
251 } else if (token == "\\bullet") {
252 // FIXME: should be params.readBullets()
254 int const index = lex.getInteger();
256 int temp_int = lex.getInteger();
257 user_defined_bullets[index].setFont(temp_int);
258 temp_bullets[index].setFont(temp_int);
260 temp_int = lex.getInteger();
261 user_defined_bullets[index].setCharacter(temp_int);
262 temp_bullets[index].setCharacter(temp_int);
264 temp_int = lex.getInteger();
265 user_defined_bullets[index].setSize(temp_int);
266 temp_bullets[index].setSize(temp_int);
268 string const temp_str = lex.getString();
269 if (temp_str != "\\end_bullet") {
270 // this element isn't really necessary for
271 // parsing but is easier for humans
272 // to understand bullets. Put it back and
273 // set a debug message?
274 lex.printError("\\end_bullet expected, got" + temp_str);
275 //how can I put it back?
277 } else if (token == "\\bulletLaTeX") {
278 // The bullet class should be able to read this.
280 int const index = lex.getInteger();
282 string temp_str = lex.getString();
284 while (temp_str != "\\end_bullet") {
285 // this loop structure is needed when user
286 // enters an empty string since the first
287 // thing returned will be the \\end_bullet
289 // if the LaTeX entry has spaces. Each element
290 // therefore needs to be read in turn
293 temp_str = lex.getString();
296 user_defined_bullets[index].setText(sum_str);
297 temp_bullets[index].setText(sum_str);
298 } else if (token == "\\secnumdepth") {
300 secnumdepth = lex.getInteger();
301 } else if (token == "\\tocdepth") {
303 tocdepth = lex.getInteger();
304 } else if (token == "\\spacing") {
306 string const tmp = rtrim(lex.getString());
307 Spacing::Space tmp_space = Spacing::Default;
309 if (tmp == "single") {
310 tmp_space = Spacing::Single;
311 } else if (tmp == "onehalf") {
312 tmp_space = Spacing::Onehalf;
313 } else if (tmp == "double") {
314 tmp_space = Spacing::Double;
315 } else if (tmp == "other") {
317 tmp_space = Spacing::Other;
318 tmp_val = lex.getFloat();
320 lex.printError("Unknown spacing token: '$$Token'");
322 #if 0 // FIXME: Handled in lyx2lyx ?
323 // Small hack so that files written with klyx will be
326 par->params().spacing(Spacing(tmp_space, tmp_val));
328 spacing.set(tmp_space, tmp_val);
329 } else if (token == "\\float_placement") {
331 float_placement = lex.getString();
340 void BufferParams::writeFile(ostream & os) const
342 // The top of the file is written by the buffer.
343 // Prints out the buffer info into the .lyx file given by file
346 os << "\\textclass " << textclasslist[textclass].name() << '\n';
348 // then the the preamble
349 if (!preamble.empty()) {
350 // remove '\n' from the end of preamble
351 string const tmppreamble = rtrim(preamble, "\n");
352 os << "\\begin_preamble\n"
354 << "\n\\end_preamble\n";
358 if (!options.empty()) {
359 os << "\\options " << options << '\n';
362 /* then the text parameters */
363 if (language != ignore_language)
364 os << "\\language " << language->lang() << '\n';
365 os << "\\inputencoding " << inputenc
366 << "\n\\fontscheme " << fonts
367 << "\n\\graphics " << graphicsDriver << '\n';
369 if (!float_placement.empty()) {
370 os << "\\float_placement " << float_placement << '\n';
372 os << "\\paperfontsize " << fontsize << '\n';
374 spacing.writeFile(os);
376 os << "\\papersize " << string_papersize[papersize2]
377 << "\n\\paperpackage " << string_paperpackages[paperpackage]
378 << "\n\\use_geometry " << use_geometry
379 << "\n\\use_amsmath " << use_amsmath
380 << "\n\\use_natbib " << use_natbib
381 << "\n\\use_numerical_citations " << use_numerical_citations
382 << "\n\\paperorientation " << string_orientation[orientation]
384 if (!paperwidth.empty())
385 os << "\\paperwidth "
386 << VSpace(paperwidth).asLyXCommand() << '\n';
387 if (!paperheight.empty())
388 os << "\\paperheight "
389 << VSpace(paperheight).asLyXCommand() << '\n';
390 if (!leftmargin.empty())
391 os << "\\leftmargin "
392 << VSpace(leftmargin).asLyXCommand() << '\n';
393 if (!topmargin.empty())
395 << VSpace(topmargin).asLyXCommand() << '\n';
396 if (!rightmargin.empty())
397 os << "\\rightmargin "
398 << VSpace(rightmargin).asLyXCommand() << '\n';
399 if (!bottommargin.empty())
400 os << "\\bottommargin "
401 << VSpace(bottommargin).asLyXCommand() << '\n';
402 if (!headheight.empty())
403 os << "\\headheight "
404 << VSpace(headheight).asLyXCommand() << '\n';
405 if (!headsep.empty())
407 << VSpace(headsep).asLyXCommand() << '\n';
408 if (!footskip.empty())
410 << VSpace(footskip).asLyXCommand() << '\n';
411 os << "\\secnumdepth " << secnumdepth
412 << "\n\\tocdepth " << tocdepth
413 << "\n\\paragraph_separation "
414 << string_paragraph_separation[paragraph_separation]
415 << "\n\\defskip " << defskip.asLyXCommand()
416 << "\n\\quotes_language "
417 << string_quotes_language[quotes_language] << '\n';
418 switch (quotes_times) {
419 // An output operator for insetquotes would be nice
420 case InsetQuotes::SingleQ:
421 os << "\\quotes_times 1\n"; break;
422 case InsetQuotes::DoubleQ:
423 os << "\\quotes_times 2\n"; break;
425 os << "\\papercolumns " << columns
426 << "\n\\papersides " << sides
427 << "\n\\paperpagestyle " << pagestyle << '\n';
428 for (int i = 0; i < 4; ++i) {
429 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
430 if (user_defined_bullets[i].getFont() != -1) {
431 os << "\\bullet " << i
433 << user_defined_bullets[i].getFont()
435 << user_defined_bullets[i].getCharacter()
437 << user_defined_bullets[i].getSize()
438 << "\n\\end_bullet\n";
441 os << "\\bulletLaTeX " << i
443 << user_defined_bullets[i].getText()
444 << "\"\n\\end_bullet\n";
449 os << "\\tracking_changes " << tracking_changes << "\n";
451 if (tracking_changes) {
452 AuthorList::Authors::const_iterator it = authorlist.begin();
453 AuthorList::Authors::const_iterator end = authorlist.end();
454 for (; it != end; ++it) {
455 os << "\\author " << it->second << "\n";
461 void BufferParams::writeLaTeX(ostream & os, LaTeXFeatures & features,
462 TexRow & texrow) const
464 os << "\\documentclass";
466 LyXTextClass const & tclass = getLyXTextClass();
468 ostringstream clsoptions; // the document class options.
470 if (tokenPos(tclass.opt_fontsize(),
471 '|', fontsize) >= 0) {
472 // only write if existing in list (and not default)
473 clsoptions << fontsize << "pt,";
478 (paperpackage == PACKAGE_NONE)) {
481 clsoptions << "a4paper,";
484 clsoptions << "letterpaper,";
487 clsoptions << "a5paper,";
490 clsoptions << "b5paper,";
492 case PAPER_EXECUTIVEPAPER:
493 clsoptions << "executivepaper,";
495 case PAPER_LEGALPAPER:
496 clsoptions << "legalpaper,";
502 if (sides != tclass.sides()) {
504 case LyXTextClass::OneSide:
505 clsoptions << "oneside,";
507 case LyXTextClass::TwoSides:
508 clsoptions << "twoside,";
514 if (columns != tclass.columns()) {
516 clsoptions << "twocolumn,";
518 clsoptions << "onecolumn,";
522 && orientation == ORIENTATION_LANDSCAPE)
523 clsoptions << "landscape,";
525 // language should be a parameter to \documentclass
527 ostringstream language_options;
528 if (language->babel() == "hebrew"
529 && default_language->babel() != "hebrew")
530 // This seems necessary
531 features.useLanguage(default_language);
533 if (lyxrc.language_use_babel ||
534 language->lang() != lyxrc.default_language ||
535 features.hasLanguages()) {
537 language_options << features.getLanguages();
538 language_options << language->babel();
539 if (lyxrc.language_global_options)
540 clsoptions << language_options.str() << ',';
543 // the user-defined options
544 if (!options.empty()) {
545 clsoptions << options << ',';
548 string strOptions(STRCONV(clsoptions.str()));
549 if (!strOptions.empty()) {
550 strOptions = rtrim(strOptions, ",");
551 os << '[' << strOptions << ']';
554 os << '{' << tclass.latexname() << "}\n";
556 // end of \documentclass defs
558 // font selection must be done before loading fontenc.sty
559 // The ae package is not needed when using OT1 font encoding.
560 if (fonts != "default" &&
561 (fonts != "ae" || lyxrc.fontenc != "default")) {
562 os << "\\usepackage{" << fonts << "}\n";
565 os << "\\usepackage{aecompl}\n";
569 // this one is not per buffer
570 if (lyxrc.fontenc != "default") {
571 os << "\\usepackage[" << lyxrc.fontenc
576 if (inputenc == "auto") {
577 string const doc_encoding =
578 language->encoding()->LatexName();
580 // Create a list with all the input encodings used
582 std::set<string> encodings =
583 features.getEncodingSet(doc_encoding);
585 os << "\\usepackage[";
586 std::copy(encodings.begin(), encodings.end(),
587 std::ostream_iterator<string>(os, ","));
588 os << doc_encoding << "]{inputenc}\n";
590 } else if (inputenc != "default") {
591 os << "\\usepackage[" << inputenc
596 // At the very beginning the text parameters.
597 if (paperpackage != PACKAGE_NONE) {
598 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) {
850 void BufferParams::setPaperStuff()
852 papersize = PAPER_DEFAULT;
853 char const c1 = paperpackage;
854 if (c1 == PACKAGE_NONE) {
855 char const c2 = papersize2;
856 if (c2 == VM_PAPER_USLETTER)
857 papersize = PAPER_USLETTER;
858 else if (c2 == VM_PAPER_USLEGAL)
859 papersize = PAPER_LEGALPAPER;
860 else if (c2 == VM_PAPER_USEXECUTIVE)
861 papersize = PAPER_EXECUTIVEPAPER;
862 else if (c2 == VM_PAPER_A3)
863 papersize = PAPER_A3PAPER;
864 else if (c2 == VM_PAPER_A4)
865 papersize = PAPER_A4PAPER;
866 else if (c2 == VM_PAPER_A5)
867 papersize = PAPER_A5PAPER;
868 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
870 papersize = PAPER_B5PAPER;
871 } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
872 (c1 == PACKAGE_WIDEMARGINSA4))
873 papersize = PAPER_A4PAPER;
877 void BufferParams::useClassDefaults()
879 LyXTextClass const & tclass = textclasslist[textclass];
881 sides = tclass.sides();
882 columns = tclass.columns();
883 pagestyle = tclass.pagestyle();
884 options = tclass.options();
885 secnumdepth = tclass.secnumdepth();
886 tocdepth = tclass.tocdepth();
890 bool BufferParams::hasClassDefaults() const
892 LyXTextClass const & tclass = textclasslist[textclass];
894 return (sides == tclass.sides()
895 && columns == tclass.columns()
896 && pagestyle == tclass.pagestyle()
897 && options == tclass.options()
898 && secnumdepth == tclass.secnumdepth()
899 && tocdepth == tclass.tocdepth());
903 LyXTextClass const & BufferParams::getLyXTextClass() const
905 return textclasslist[textclass];
909 void BufferParams::readPreamble(LyXLex & lex)
911 if (lex.getString() != "\\begin_preamble")
912 lyxerr << "Error (BufferParams::readPreamble):"
913 "consistency check failed." << endl;
915 preamble = lex.getLongString("\\end_preamble");
919 void BufferParams::readLanguage(LyXLex & lex)
921 if (!lex.next()) return;
923 string const tmptok = lex.getString();
925 // check if tmptok is part of tex_babel in tex-defs.h
926 language = languages.getLanguage(tmptok);
928 // Language tmptok was not found
929 language = default_language;
930 lyxerr << "Warning: Setting language `"
931 << tmptok << "' to `" << language->lang()
937 void BufferParams::readGraphicsDriver(LyXLex & lex)
939 if (!lex.next()) return;
941 string const tmptok = lex.getString();
942 // check if tmptok is part of tex_graphics in tex_defs.h
945 string const test = tex_graphics[n++];
947 if (test == tmptok) {
948 graphicsDriver = tmptok;
950 } else if (test == "last_item") {
952 "Warning: graphics driver `$$Token' not recognized!\n"
953 " Setting graphics driver to `default'.\n");
954 graphicsDriver = "default";