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);
169 } else if (token == "\\paperpackage") {
170 int tmpret = lex.findToken(string_paperpackages);
173 paperpackage = BufferParams::PACKAGE_NONE;
175 paperpackage = 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 << "a4paper,";
480 clsoptions << "letterpaper,";
483 clsoptions << "a5paper,";
486 clsoptions << "b5paper,";
488 case PAPER_EXECUTIVEPAPER:
489 clsoptions << "executivepaper,";
491 case PAPER_LEGALPAPER:
492 clsoptions << "legalpaper,";
498 if (sides != tclass.sides()) {
500 case LyXTextClass::OneSide:
501 clsoptions << "oneside,";
503 case LyXTextClass::TwoSides:
504 clsoptions << "twoside,";
510 if (columns != tclass.columns()) {
512 clsoptions << "twocolumn,";
514 clsoptions << "onecolumn,";
518 && orientation == ORIENTATION_LANDSCAPE)
519 clsoptions << "landscape,";
521 // language should be a parameter to \documentclass
522 if (language->babel() == "hebrew"
523 && default_language->babel() != "hebrew")
524 // This seems necessary
525 features.useLanguage(default_language);
527 ostringstream language_options;
528 bool const use_babel = features.useBabel();
530 language_options << features.getLanguages();
531 language_options << language->babel();
532 if (lyxrc.language_global_options)
533 clsoptions << language_options.str() << ',';
536 // the user-defined options
537 if (!options.empty()) {
538 clsoptions << options << ',';
541 string strOptions(STRCONV(clsoptions.str()));
542 if (!strOptions.empty()) {
543 strOptions = rtrim(strOptions, ",");
544 os << '[' << strOptions << ']';
547 os << '{' << tclass.latexname() << "}\n";
549 // end of \documentclass defs
551 // font selection must be done before loading fontenc.sty
552 // The ae package is not needed when using OT1 font encoding.
553 if (fonts != "default" &&
554 (fonts != "ae" || lyxrc.fontenc != "default")) {
555 os << "\\usepackage{" << fonts << "}\n";
558 os << "\\usepackage{aecompl}\n";
562 // this one is not per buffer
563 if (lyxrc.fontenc != "default") {
564 os << "\\usepackage[" << lyxrc.fontenc
569 if (inputenc == "auto") {
570 string const doc_encoding =
571 language->encoding()->LatexName();
573 // Create a list with all the input encodings used
575 std::set<string> encodings =
576 features.getEncodingSet(doc_encoding);
578 os << "\\usepackage[";
579 std::copy(encodings.begin(), encodings.end(),
580 std::ostream_iterator<string>(os, ","));
581 os << doc_encoding << "]{inputenc}\n";
583 } else if (inputenc != "default") {
584 os << "\\usepackage[" << inputenc
589 // At the very beginning the text parameters.
590 if (paperpackage != PACKAGE_NONE) {
591 switch (paperpackage) {
593 os << "\\usepackage{a4}\n";
597 os << "\\usepackage{a4wide}\n";
600 case PACKAGE_WIDEMARGINSA4:
601 os << "\\usepackage[widemargins]{a4}\n";
607 os << "\\usepackage{geometry}\n";
609 os << "\\geometry{verbose";
610 if (orientation == ORIENTATION_LANDSCAPE)
612 switch (papersize2) {
613 case VM_PAPER_CUSTOM:
614 if (!paperwidth.empty())
617 if (!paperheight.empty())
618 os << ",paperheight="
621 case VM_PAPER_USLETTER:
622 os << ",letterpaper";
624 case VM_PAPER_USLEGAL:
627 case VM_PAPER_USEXECUTIVE:
628 os << ",executivepaper";
649 // default papersize ie VM_PAPER_DEFAULT
650 switch (lyxrc.default_papersize) {
651 case PAPER_DEFAULT: // keep compiler happy
653 os << ",letterpaper";
655 case PAPER_LEGALPAPER:
658 case PAPER_EXECUTIVEPAPER:
659 os << ",executivepaper";
675 if (!topmargin.empty())
676 os << ",tmargin=" << topmargin;
677 if (!bottommargin.empty())
678 os << ",bmargin=" << bottommargin;
679 if (!leftmargin.empty())
680 os << ",lmargin=" << leftmargin;
681 if (!rightmargin.empty())
682 os << ",rmargin=" << rightmargin;
683 if (!headheight.empty())
684 os << ",headheight=" << headheight;
685 if (!headsep.empty())
686 os << ",headsep=" << headsep;
687 if (!footskip.empty())
688 os << ",footskip=" << footskip;
693 if (tokenPos(tclass.opt_pagestyle(),
694 '|', pagestyle) >= 0) {
695 if (pagestyle == "fancy") {
696 os << "\\usepackage{fancyhdr}\n";
699 os << "\\pagestyle{" << pagestyle << "}\n";
703 if (secnumdepth != tclass.secnumdepth()) {
704 os << "\\setcounter{secnumdepth}{"
709 if (tocdepth != tclass.tocdepth()) {
710 os << "\\setcounter{tocdepth}{"
716 if (paragraph_separation) {
717 switch (defskip.kind()) {
718 case VSpace::SMALLSKIP:
719 os << "\\setlength\\parskip{\\smallskipamount}\n";
721 case VSpace::MEDSKIP:
722 os << "\\setlength\\parskip{\\medskipamount}\n";
724 case VSpace::BIGSKIP:
725 os << "\\setlength\\parskip{\\bigskipamount}\n";
728 os << "\\setlength\\parskip{"
729 << defskip.length().asLatexString()
732 default: // should never happen // Then delete it.
733 os << "\\setlength\\parskip{\\medskipamount}\n";
738 os << "\\setlength\\parindent{0pt}\n";
742 // Now insert the LyX specific LaTeX commands...
744 // The optional packages;
745 string lyxpreamble(features.getPackages());
747 // this might be useful...
748 lyxpreamble += "\n\\makeatletter\n";
750 // Some macros LyX will need
751 string tmppreamble(features.getMacros());
753 if (!tmppreamble.empty()) {
754 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
755 "LyX specific LaTeX commands.\n"
756 + tmppreamble + '\n';
759 // the text class specific preamble
760 tmppreamble = features.getTClassPreamble();
761 if (!tmppreamble.empty()) {
762 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
763 "Textclass specific LaTeX commands.\n"
764 + tmppreamble + '\n';
767 /* the user-defined preamble */
768 if (!preamble.empty()) {
769 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
770 "User specified LaTeX commands.\n"
774 // Itemize bullet settings need to be last in case the user
775 // defines their own bullets that use a package included
776 // in the user-defined preamble -- ARRae
777 // Actually it has to be done much later than that
778 // since some packages like frenchb make modifications
779 // at \begin{document} time -- JMarc
781 for (int i = 0; i < 4; ++i) {
782 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
783 if (bullets_def.empty())
784 bullets_def="\\AtBeginDocument{\n";
785 bullets_def += " \\renewcommand{\\labelitemi";
787 // `i' is one less than the item to modify
800 bullets_def += "}{" +
801 user_defined_bullets[i].getText()
806 if (!bullets_def.empty())
807 lyxpreamble += bullets_def + "}\n\n";
809 // We try to load babel late, in case it interferes
810 // with other packages.
812 string tmp = lyxrc.language_package;
813 if (!lyxrc.language_global_options
814 && tmp == "\\usepackage{babel}")
815 tmp = string("\\usepackage[") +
816 STRCONV(language_options.str()) +
818 lyxpreamble += tmp + "\n";
819 lyxpreamble += features.getBabelOptions();
822 lyxpreamble += "\\makeatother\n";
824 // dvipost settings come after everything else
825 if (tracking_changes) {
828 "\\dvipost{osstart color push Red}\n"
829 "\\dvipost{osend color pop}\n"
830 "\\dvipost{cbstart color push Blue}\n"
831 "\\dvipost{cbend color pop} \n";
835 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
836 for (int j = 0; j != nlines; ++j) {
844 void BufferParams::setPaperStuff()
846 papersize = PAPER_DEFAULT;
847 char const c1 = paperpackage;
848 if (c1 == PACKAGE_NONE) {
849 char const c2 = papersize2;
850 if (c2 == VM_PAPER_USLETTER)
851 papersize = PAPER_USLETTER;
852 else if (c2 == VM_PAPER_USLEGAL)
853 papersize = PAPER_LEGALPAPER;
854 else if (c2 == VM_PAPER_USEXECUTIVE)
855 papersize = PAPER_EXECUTIVEPAPER;
856 else if (c2 == VM_PAPER_A3)
857 papersize = PAPER_A3PAPER;
858 else if (c2 == VM_PAPER_A4)
859 papersize = PAPER_A4PAPER;
860 else if (c2 == VM_PAPER_A5)
861 papersize = PAPER_A5PAPER;
862 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
864 papersize = PAPER_B5PAPER;
865 } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
866 (c1 == PACKAGE_WIDEMARGINSA4))
867 papersize = PAPER_A4PAPER;
871 void BufferParams::useClassDefaults()
873 LyXTextClass const & tclass = textclasslist[textclass];
875 sides = tclass.sides();
876 columns = tclass.columns();
877 pagestyle = tclass.pagestyle();
878 options = tclass.options();
879 secnumdepth = tclass.secnumdepth();
880 tocdepth = tclass.tocdepth();
884 bool BufferParams::hasClassDefaults() const
886 LyXTextClass const & tclass = textclasslist[textclass];
888 return (sides == tclass.sides()
889 && columns == tclass.columns()
890 && pagestyle == tclass.pagestyle()
891 && options == tclass.options()
892 && secnumdepth == tclass.secnumdepth()
893 && tocdepth == tclass.tocdepth());
897 LyXTextClass const & BufferParams::getLyXTextClass() const
899 return textclasslist[textclass];
903 void BufferParams::readPreamble(LyXLex & lex)
905 if (lex.getString() != "\\begin_preamble")
906 lyxerr << "Error (BufferParams::readPreamble):"
907 "consistency check failed." << endl;
909 preamble = lex.getLongString("\\end_preamble");
913 void BufferParams::readLanguage(LyXLex & lex)
915 if (!lex.next()) return;
917 string const tmptok = lex.getString();
919 // check if tmptok is part of tex_babel in tex-defs.h
920 language = languages.getLanguage(tmptok);
922 // Language tmptok was not found
923 language = default_language;
924 lyxerr << "Warning: Setting language `"
925 << tmptok << "' to `" << language->lang()
931 void BufferParams::readGraphicsDriver(LyXLex & lex)
933 if (!lex.next()) return;
935 string const tmptok = lex.getString();
936 // check if tmptok is part of tex_graphics in tex_defs.h
939 string const test = tex_graphics[n++];
941 if (test == tmptok) {
942 graphicsDriver = tmptok;
944 } else if (test == "last_item") {
946 "Warning: graphics driver `$$Token' not recognized!\n"
947 " Setting graphics driver to `default'.\n");
948 graphicsDriver = "default";