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"
26 #include "support/lyxalgo.h" // for lyx::count
27 #include "support/lyxlib.h"
28 #include "support/lstrings.h"
29 #include "support/types.h"
39 #warning Do we need this horrible thing? (JMarc)
43 BufferParams::BufferParams()
44 // Initialize textclass to point to article. if `first' is
45 // true in the returned pair, then `second' is the textclass
46 // number; if it is false, second is 0. In both cases, second
48 : textclass(textclasslist.NumberOfClass("article").second)
50 paragraph_separation = PARSEP_INDENT;
51 defskip = VSpace(VSpace::MEDSKIP);
52 quotes_language = InsetQuotes::EnglishQ;
53 quotes_times = InsetQuotes::DoubleQ;
57 papersize = PAPER_DEFAULT;
58 papersize2 = VM_PAPER_DEFAULT; /* DEFAULT */
59 paperpackage = PACKAGE_NONE;
60 orientation = ORIENTATION_PORTRAIT;
62 use_amsmath = AMS_AUTO;
64 use_numerical_citations = false;
65 tracking_changes = false;
68 language = default_language;
71 graphicsDriver = "default";
72 sides = LyXTextClass::OneSide;
74 pagestyle = "default";
75 for (int iter = 0; iter < 4; ++iter) {
76 user_defined_bullets[iter] = ITEMIZE_DEFAULTS[iter];
77 temp_bullets[iter] = ITEMIZE_DEFAULTS[iter];
82 string const BufferParams::readToken(LyXLex & lex, string const & token)
84 if (token == "\\textclass") {
86 string const classname = lex.getString();
87 pair<bool, lyx::textclass_type> pp =
88 textclasslist.NumberOfClass(classname);
90 textclass = pp.second;
95 } else if (token == "\\begin_preamble") {
97 } else if (token == "\\options") {
99 options = lex.getString();
100 } else if (token == "\\language") {
102 } else if (token == "\\inputencoding") {
104 inputenc = lex.getString();
105 } else if (token == "\\graphics") {
106 readGraphicsDriver(lex);
107 } else if (token == "\\fontscheme") {
109 fonts = lex.getString();
110 } else if (token == "\\paragraph_separation") {
111 int tmpret = lex.findToken(string_paragraph_separation);
114 paragraph_separation =
115 static_cast<BufferParams::PARSEP>(tmpret);
116 } else if (token == "\\defskip") {
118 defskip = VSpace(lex.getString());
119 } else if (token == "\\quotes_language") {
120 // FIXME: should be params.readQuotes()
121 int tmpret = lex.findToken(string_quotes_language);
124 InsetQuotes::quote_language tmpl =
125 InsetQuotes::EnglishQ;
128 tmpl = InsetQuotes::EnglishQ;
131 tmpl = InsetQuotes::SwedishQ;
134 tmpl = InsetQuotes::GermanQ;
137 tmpl = InsetQuotes::PolishQ;
140 tmpl = InsetQuotes::FrenchQ;
143 tmpl = InsetQuotes::DanishQ;
146 quotes_language = tmpl;
147 } else if (token == "\\quotes_times") {
148 // FIXME: should be params.readQuotes()
150 switch (lex.getInteger()) {
152 quotes_times = InsetQuotes::SingleQ;
155 quotes_times = InsetQuotes::DoubleQ;
158 } else if (token == "\\papersize") {
159 int tmpret = lex.findToken(string_papersize);
164 } else if (token == "\\paperpackage") {
165 int tmpret = lex.findToken(string_paperpackages);
168 paperpackage = BufferParams::PACKAGE_NONE;
170 paperpackage = tmpret;
171 } else if (token == "\\use_geometry") {
173 use_geometry = lex.getInteger();
174 } else if (token == "\\use_amsmath") {
176 use_amsmath = static_cast<BufferParams::AMS>(
178 } else if (token == "\\use_natbib") {
180 use_natbib = lex.getInteger();
181 } else if (token == "\\use_numerical_citations") {
183 use_numerical_citations = lex.getInteger();
184 } else if (token == "\\tracking_changes") {
186 tracking_changes = lex.getInteger();
187 } else if (token == "\\author") {
189 istringstream ss(lex.getString());
192 authorlist.record(a);
193 } else if (token == "\\paperorientation") {
194 int tmpret = lex.findToken(string_orientation);
198 static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
199 } else if (token == "\\paperwidth") {
201 paperwidth = lex.getString();
202 } else if (token == "\\paperheight") {
204 paperheight = lex.getString();
205 } else if (token == "\\leftmargin") {
207 leftmargin = lex.getString();
208 } else if (token == "\\topmargin") {
210 topmargin = lex.getString();
211 } else if (token == "\\rightmargin") {
213 rightmargin = lex.getString();
214 } else if (token == "\\bottommargin") {
216 bottommargin = lex.getString();
217 } else if (token == "\\headheight") {
219 headheight = lex.getString();
220 } else if (token == "\\headsep") {
222 headsep = lex.getString();
223 } else if (token == "\\footskip") {
225 footskip = lex.getString();
226 } else if (token == "\\paperfontsize") {
228 fontsize = rtrim(lex.getString());
229 } else if (token == "\\papercolumns") {
231 columns = lex.getInteger();
232 } else if (token == "\\papersides") {
234 switch (lex.getInteger()) {
236 case 1: sides = LyXTextClass::OneSide; break;
237 case 2: sides = LyXTextClass::TwoSides; break;
239 } else if (token == "\\paperpagestyle") {
241 pagestyle = rtrim(lex.getString());
242 } else if (token == "\\bullet") {
243 // FIXME: should be params.readBullets()
245 int const index = lex.getInteger();
247 int temp_int = lex.getInteger();
248 user_defined_bullets[index].setFont(temp_int);
249 temp_bullets[index].setFont(temp_int);
251 temp_int = lex.getInteger();
252 user_defined_bullets[index].setCharacter(temp_int);
253 temp_bullets[index].setCharacter(temp_int);
255 temp_int = lex.getInteger();
256 user_defined_bullets[index].setSize(temp_int);
257 temp_bullets[index].setSize(temp_int);
259 string const temp_str = lex.getString();
260 if (temp_str != "\\end_bullet") {
261 // this element isn't really necessary for
262 // parsing but is easier for humans
263 // to understand bullets. Put it back and
264 // set a debug message?
265 lex.printError("\\end_bullet expected, got" + temp_str);
266 //how can I put it back?
268 } else if (token == "\\bulletLaTeX") {
269 // The bullet class should be able to read this.
271 int const index = lex.getInteger();
273 string temp_str = lex.getString();
275 while (temp_str != "\\end_bullet") {
276 // this loop structure is needed when user
277 // enters an empty string since the first
278 // thing returned will be the \\end_bullet
280 // if the LaTeX entry has spaces. Each element
281 // therefore needs to be read in turn
284 temp_str = lex.getString();
287 user_defined_bullets[index].setText(sum_str);
288 temp_bullets[index].setText(sum_str);
289 } else if (token == "\\secnumdepth") {
291 secnumdepth = lex.getInteger();
292 } else if (token == "\\tocdepth") {
294 tocdepth = lex.getInteger();
295 } else if (token == "\\spacing") {
297 string const tmp = rtrim(lex.getString());
298 Spacing::Space tmp_space = Spacing::Default;
300 if (tmp == "single") {
301 tmp_space = Spacing::Single;
302 } else if (tmp == "onehalf") {
303 tmp_space = Spacing::Onehalf;
304 } else if (tmp == "double") {
305 tmp_space = Spacing::Double;
306 } else if (tmp == "other") {
308 tmp_space = Spacing::Other;
309 tmp_val = lex.getFloat();
311 lex.printError("Unknown spacing token: '$$Token'");
313 #if 0 // FIXME: Handled in lyx2lyx ?
314 // Small hack so that files written with klyx will be
317 par->params().spacing(Spacing(tmp_space, tmp_val));
319 spacing.set(tmp_space, tmp_val);
320 } else if (token == "\\float_placement") {
322 float_placement = lex.getString();
331 void BufferParams::writeFile(ostream & os) const
333 // The top of the file is written by the buffer.
334 // Prints out the buffer info into the .lyx file given by file
337 os << "\\textclass " << textclasslist[textclass].name() << '\n';
339 // then the the preamble
340 if (!preamble.empty()) {
341 // remove '\n' from the end of preamble
342 string const tmppreamble = rtrim(preamble, "\n");
343 os << "\\begin_preamble\n"
345 << "\n\\end_preamble\n";
349 if (!options.empty()) {
350 os << "\\options " << options << '\n';
353 /* then the text parameters */
354 if (language != ignore_language)
355 os << "\\language " << language->lang() << '\n';
356 os << "\\inputencoding " << inputenc
357 << "\n\\fontscheme " << fonts
358 << "\n\\graphics " << graphicsDriver << '\n';
360 if (!float_placement.empty()) {
361 os << "\\float_placement " << float_placement << '\n';
363 os << "\\paperfontsize " << fontsize << '\n';
365 spacing.writeFile(os);
367 os << "\\papersize " << string_papersize[papersize2]
368 << "\n\\paperpackage " << string_paperpackages[paperpackage]
369 << "\n\\use_geometry " << use_geometry
370 << "\n\\use_amsmath " << use_amsmath
371 << "\n\\use_natbib " << use_natbib
372 << "\n\\use_numerical_citations " << use_numerical_citations
373 << "\n\\paperorientation " << string_orientation[orientation]
375 if (!paperwidth.empty())
376 os << "\\paperwidth "
377 << VSpace(paperwidth).asLyXCommand() << '\n';
378 if (!paperheight.empty())
379 os << "\\paperheight "
380 << VSpace(paperheight).asLyXCommand() << '\n';
381 if (!leftmargin.empty())
382 os << "\\leftmargin "
383 << VSpace(leftmargin).asLyXCommand() << '\n';
384 if (!topmargin.empty())
386 << VSpace(topmargin).asLyXCommand() << '\n';
387 if (!rightmargin.empty())
388 os << "\\rightmargin "
389 << VSpace(rightmargin).asLyXCommand() << '\n';
390 if (!bottommargin.empty())
391 os << "\\bottommargin "
392 << VSpace(bottommargin).asLyXCommand() << '\n';
393 if (!headheight.empty())
394 os << "\\headheight "
395 << VSpace(headheight).asLyXCommand() << '\n';
396 if (!headsep.empty())
398 << VSpace(headsep).asLyXCommand() << '\n';
399 if (!footskip.empty())
401 << VSpace(footskip).asLyXCommand() << '\n';
402 os << "\\secnumdepth " << secnumdepth
403 << "\n\\tocdepth " << tocdepth
404 << "\n\\paragraph_separation "
405 << string_paragraph_separation[paragraph_separation]
406 << "\n\\defskip " << defskip.asLyXCommand()
407 << "\n\\quotes_language "
408 << string_quotes_language[quotes_language] << '\n';
409 switch (quotes_times) {
410 // An output operator for insetquotes would be nice
411 case InsetQuotes::SingleQ:
412 os << "\\quotes_times 1\n"; break;
413 case InsetQuotes::DoubleQ:
414 os << "\\quotes_times 2\n"; break;
416 os << "\\papercolumns " << columns
417 << "\n\\papersides " << sides
418 << "\n\\paperpagestyle " << pagestyle << '\n';
419 for (int i = 0; i < 4; ++i) {
420 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
421 if (user_defined_bullets[i].getFont() != -1) {
422 os << "\\bullet " << i
424 << user_defined_bullets[i].getFont()
426 << user_defined_bullets[i].getCharacter()
428 << user_defined_bullets[i].getSize()
429 << "\n\\end_bullet\n";
432 os << "\\bulletLaTeX " << i
434 << user_defined_bullets[i].getText()
435 << "\"\n\\end_bullet\n";
440 os << "\\tracking_changes " << tracking_changes << "\n";
442 if (tracking_changes) {
443 AuthorList::Authors::const_iterator it = authorlist.begin();
444 AuthorList::Authors::const_iterator end = authorlist.end();
445 for (; it != end; ++it) {
446 os << "\\author " << it->second << "\n";
452 void BufferParams::writeLaTeX(ostream & os, LaTeXFeatures & features,
453 TexRow & texrow) const
455 os << "\\documentclass";
457 LyXTextClass const & tclass = getLyXTextClass();
459 ostringstream clsoptions; // the document class options.
461 if (tokenPos(tclass.opt_fontsize(),
462 '|', fontsize) >= 0) {
463 // only write if existing in list (and not default)
464 clsoptions << fontsize << "pt,";
469 (paperpackage == PACKAGE_NONE)) {
472 clsoptions << "a4paper,";
475 clsoptions << "letterpaper,";
478 clsoptions << "a5paper,";
481 clsoptions << "b5paper,";
483 case PAPER_EXECUTIVEPAPER:
484 clsoptions << "executivepaper,";
486 case PAPER_LEGALPAPER:
487 clsoptions << "legalpaper,";
493 if (sides != tclass.sides()) {
495 case LyXTextClass::OneSide:
496 clsoptions << "oneside,";
498 case LyXTextClass::TwoSides:
499 clsoptions << "twoside,";
505 if (columns != tclass.columns()) {
507 clsoptions << "twocolumn,";
509 clsoptions << "onecolumn,";
513 && orientation == ORIENTATION_LANDSCAPE)
514 clsoptions << "landscape,";
516 // language should be a parameter to \documentclass
518 ostringstream language_options;
519 if (language->babel() == "hebrew"
520 && default_language->babel() != "hebrew")
521 // This seems necessary
522 features.useLanguage(default_language);
524 if (lyxrc.language_use_babel ||
525 language->lang() != lyxrc.default_language ||
526 features.hasLanguages()) {
528 language_options << features.getLanguages();
529 language_options << language->babel();
530 if (lyxrc.language_global_options)
531 clsoptions << language_options.str() << ',';
534 // the user-defined options
535 if (!options.empty()) {
536 clsoptions << options << ',';
539 string strOptions(STRCONV(clsoptions.str()));
540 if (!strOptions.empty()) {
541 strOptions = rtrim(strOptions, ",");
542 os << '[' << strOptions << ']';
545 os << '{' << tclass.latexname() << "}\n";
547 // end of \documentclass defs
549 // font selection must be done before loading fontenc.sty
550 // The ae package is not needed when using OT1 font encoding.
551 if (fonts != "default" &&
552 (fonts != "ae" || lyxrc.fontenc != "default")) {
553 os << "\\usepackage{" << fonts << "}\n";
556 os << "\\usepackage{aecompl}\n";
560 // this one is not per buffer
561 if (lyxrc.fontenc != "default") {
562 os << "\\usepackage[" << lyxrc.fontenc
567 if (inputenc == "auto") {
568 string const doc_encoding =
569 language->encoding()->LatexName();
571 // Create a list with all the input encodings used
573 std::set<string> encodings =
574 features.getEncodingSet(doc_encoding);
576 os << "\\usepackage[";
577 std::copy(encodings.begin(), encodings.end(),
578 std::ostream_iterator<string>(os, ","));
579 os << doc_encoding << "]{inputenc}\n";
581 } else if (inputenc != "default") {
582 os << "\\usepackage[" << inputenc
587 // At the very beginning the text parameters.
588 if (paperpackage != PACKAGE_NONE) {
589 switch (paperpackage) {
591 os << "\\usepackage{a4}\n";
595 os << "\\usepackage{a4wide}\n";
598 case PACKAGE_WIDEMARGINSA4:
599 os << "\\usepackage[widemargins]{a4}\n";
605 os << "\\usepackage{geometry}\n";
607 os << "\\geometry{verbose";
608 if (orientation == ORIENTATION_LANDSCAPE)
610 switch (papersize2) {
611 case VM_PAPER_CUSTOM:
612 if (!paperwidth.empty())
615 if (!paperheight.empty())
616 os << ",paperheight="
619 case VM_PAPER_USLETTER:
620 os << ",letterpaper";
622 case VM_PAPER_USLEGAL:
625 case VM_PAPER_USEXECUTIVE:
626 os << ",executivepaper";
647 // default papersize ie VM_PAPER_DEFAULT
648 switch (lyxrc.default_papersize) {
649 case PAPER_DEFAULT: // keep compiler happy
651 os << ",letterpaper";
653 case PAPER_LEGALPAPER:
656 case PAPER_EXECUTIVEPAPER:
657 os << ",executivepaper";
673 if (!topmargin.empty())
674 os << ",tmargin=" << topmargin;
675 if (!bottommargin.empty())
676 os << ",bmargin=" << bottommargin;
677 if (!leftmargin.empty())
678 os << ",lmargin=" << leftmargin;
679 if (!rightmargin.empty())
680 os << ",rmargin=" << rightmargin;
681 if (!headheight.empty())
682 os << ",headheight=" << headheight;
683 if (!headsep.empty())
684 os << ",headsep=" << headsep;
685 if (!footskip.empty())
686 os << ",footskip=" << footskip;
691 if (tokenPos(tclass.opt_pagestyle(),
692 '|', pagestyle) >= 0) {
693 if (pagestyle == "fancy") {
694 os << "\\usepackage{fancyhdr}\n";
697 os << "\\pagestyle{" << pagestyle << "}\n";
701 if (secnumdepth != tclass.secnumdepth()) {
702 os << "\\setcounter{secnumdepth}{"
707 if (tocdepth != tclass.tocdepth()) {
708 os << "\\setcounter{tocdepth}{"
714 if (paragraph_separation) {
715 switch (defskip.kind()) {
716 case VSpace::SMALLSKIP:
717 os << "\\setlength\\parskip{\\smallskipamount}\n";
719 case VSpace::MEDSKIP:
720 os << "\\setlength\\parskip{\\medskipamount}\n";
722 case VSpace::BIGSKIP:
723 os << "\\setlength\\parskip{\\bigskipamount}\n";
726 os << "\\setlength\\parskip{"
727 << defskip.length().asLatexString()
730 default: // should never happen // Then delete it.
731 os << "\\setlength\\parskip{\\medskipamount}\n";
736 os << "\\setlength\\parindent{0pt}\n";
740 // Now insert the LyX specific LaTeX commands...
742 // The optional packages;
743 string lyxpreamble(features.getPackages());
745 // this might be useful...
746 lyxpreamble += "\n\\makeatletter\n";
748 // Some macros LyX will need
749 string tmppreamble(features.getMacros());
751 if (!tmppreamble.empty()) {
752 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
753 "LyX specific LaTeX commands.\n"
754 + tmppreamble + '\n';
757 // the text class specific preamble
758 tmppreamble = features.getTClassPreamble();
759 if (!tmppreamble.empty()) {
760 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
761 "Textclass specific LaTeX commands.\n"
762 + tmppreamble + '\n';
765 /* the user-defined preamble */
766 if (!preamble.empty()) {
767 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
768 "User specified LaTeX commands.\n"
772 // Itemize bullet settings need to be last in case the user
773 // defines their own bullets that use a package included
774 // in the user-defined preamble -- ARRae
775 // Actually it has to be done much later than that
776 // since some packages like frenchb make modifications
777 // at \begin{document} time -- JMarc
779 for (int i = 0; i < 4; ++i) {
780 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
781 if (bullets_def.empty())
782 bullets_def="\\AtBeginDocument{\n";
783 bullets_def += " \\renewcommand{\\labelitemi";
785 // `i' is one less than the item to modify
798 bullets_def += "}{" +
799 user_defined_bullets[i].getText()
804 if (!bullets_def.empty())
805 lyxpreamble += bullets_def + "}\n\n";
807 // We try to load babel late, in case it interferes
808 // with other packages.
810 string tmp = lyxrc.language_package;
811 if (!lyxrc.language_global_options
812 && tmp == "\\usepackage{babel}")
813 tmp = string("\\usepackage[") +
814 STRCONV(language_options.str()) +
816 lyxpreamble += tmp + "\n";
817 lyxpreamble += features.getBabelOptions();
820 lyxpreamble += "\\makeatother\n";
822 // dvipost settings come after everything else
823 if (tracking_changes) {
826 "\\dvipost{osstart color push Red}\n"
827 "\\dvipost{osend color pop}\n"
828 "\\dvipost{cbstart color push Blue}\n"
829 "\\dvipost{cbend color pop} \n";
833 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
834 for (int j = 0; j != nlines; ++j) {
841 void BufferParams::setPaperStuff()
843 papersize = PAPER_DEFAULT;
844 char const c1 = paperpackage;
845 if (c1 == PACKAGE_NONE) {
846 char const c2 = papersize2;
847 if (c2 == VM_PAPER_USLETTER)
848 papersize = PAPER_USLETTER;
849 else if (c2 == VM_PAPER_USLEGAL)
850 papersize = PAPER_LEGALPAPER;
851 else if (c2 == VM_PAPER_USEXECUTIVE)
852 papersize = PAPER_EXECUTIVEPAPER;
853 else if (c2 == VM_PAPER_A3)
854 papersize = PAPER_A3PAPER;
855 else if (c2 == VM_PAPER_A4)
856 papersize = PAPER_A4PAPER;
857 else if (c2 == VM_PAPER_A5)
858 papersize = PAPER_A5PAPER;
859 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
861 papersize = PAPER_B5PAPER;
862 } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
863 (c1 == PACKAGE_WIDEMARGINSA4))
864 papersize = PAPER_A4PAPER;
868 void BufferParams::useClassDefaults()
870 LyXTextClass const & tclass = textclasslist[textclass];
872 sides = tclass.sides();
873 columns = tclass.columns();
874 pagestyle = tclass.pagestyle();
875 options = tclass.options();
876 secnumdepth = tclass.secnumdepth();
877 tocdepth = tclass.tocdepth();
881 bool BufferParams::hasClassDefaults() const
883 LyXTextClass const & tclass = textclasslist[textclass];
885 return (sides == tclass.sides()
886 && columns == tclass.columns()
887 && pagestyle == tclass.pagestyle()
888 && options == tclass.options()
889 && secnumdepth == tclass.secnumdepth()
890 && tocdepth == tclass.tocdepth());
894 LyXTextClass const & BufferParams::getLyXTextClass() const
896 return textclasslist[textclass];
900 void BufferParams::readPreamble(LyXLex & lex)
902 if (lex.getString() != "\\begin_preamble")
903 lyxerr << "Error (BufferParams::readPreamble):"
904 "consistency check failed." << endl;
906 preamble = lex.getLongString("\\end_preamble");
910 void BufferParams::readLanguage(LyXLex & lex)
912 if (!lex.next()) return;
914 string const tmptok = lex.getString();
916 // check if tmptok is part of tex_babel in tex-defs.h
917 language = languages.getLanguage(tmptok);
919 // Language tmptok was not found
920 language = default_language;
921 lyxerr << "Warning: Setting language `"
922 << tmptok << "' to `" << language->lang()
928 void BufferParams::readGraphicsDriver(LyXLex & lex)
930 if (!lex.next()) return;
932 string const tmptok = lex.getString();
933 // check if tmptok is part of tex_graphics in tex_defs.h
936 string const test = tex_graphics[n++];
938 if (test == tmptok) {
939 graphicsDriver = tmptok;
941 } else if (test == "last_item") {
943 "Warning: graphics driver `$$Token' not recognized!\n"
944 " Setting graphics driver to `default'.\n");
945 graphicsDriver = "default";