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"
25 #include "support/lyxalgo.h" // for lyx::count
26 #include "support/lyxlib.h"
27 #include "support/lstrings.h"
35 #warning Do we need this horrible thing? (JMarc)
39 BufferParams::BufferParams()
40 // Initialize textclass to point to article. if `first' is
41 // true in the returned pair, then `second' is the textclass
42 // number; if it is false, second is 0. In both cases, second
44 : textclass(textclasslist.NumberOfClass("article").second)
46 paragraph_separation = PARSEP_INDENT;
47 defskip = VSpace(VSpace::MEDSKIP);
48 quotes_language = InsetQuotes::EnglishQ;
49 quotes_times = InsetQuotes::DoubleQ;
53 papersize = PAPER_DEFAULT;
54 papersize2 = VM_PAPER_DEFAULT; /* DEFAULT */
55 paperpackage = PACKAGE_NONE;
56 orientation = ORIENTATION_PORTRAIT;
60 use_numerical_citations = false;
61 tracking_changes = false;
64 language = default_language;
67 graphicsDriver = "default";
68 sides = LyXTextClass::OneSide;
70 pagestyle = "default";
71 for (int iter = 0; iter < 4; ++iter) {
72 user_defined_bullets[iter] = ITEMIZE_DEFAULTS[iter];
73 temp_bullets[iter] = ITEMIZE_DEFAULTS[iter];
78 void BufferParams::writeFile(ostream & os) const
80 // The top of the file is written by the buffer.
81 // Prints out the buffer info into the .lyx file given by file
84 os << "\\textclass " << textclasslist[textclass].name() << '\n';
86 // then the the preamble
87 if (!preamble.empty()) {
88 // remove '\n' from the end of preamble
89 string const tmppreamble = rtrim(preamble, "\n");
90 os << "\\begin_preamble\n"
92 << "\n\\end_preamble\n";
96 if (!options.empty()) {
97 os << "\\options " << options << '\n';
100 /* then the text parameters */
101 if (language != ignore_language)
102 os << "\\language " << language->lang() << '\n';
103 os << "\\inputencoding " << inputenc
104 << "\n\\fontscheme " << fonts
105 << "\n\\graphics " << graphicsDriver << '\n';
107 if (!float_placement.empty()) {
108 os << "\\float_placement " << float_placement << '\n';
110 os << "\\paperfontsize " << fontsize << '\n';
112 spacing.writeFile(os);
114 os << "\\papersize " << string_papersize[papersize2]
115 << "\n\\paperpackage " << string_paperpackages[paperpackage]
116 << "\n\\use_geometry " << use_geometry
117 << "\n\\use_amsmath " << use_amsmath
118 << "\n\\use_natbib " << use_natbib
119 << "\n\\use_numerical_citations " << use_numerical_citations
120 << "\n\\paperorientation " << string_orientation[orientation]
122 if (!paperwidth.empty())
123 os << "\\paperwidth "
124 << VSpace(paperwidth).asLyXCommand() << '\n';
125 if (!paperheight.empty())
126 os << "\\paperheight "
127 << VSpace(paperheight).asLyXCommand() << '\n';
128 if (!leftmargin.empty())
129 os << "\\leftmargin "
130 << VSpace(leftmargin).asLyXCommand() << '\n';
131 if (!topmargin.empty())
133 << VSpace(topmargin).asLyXCommand() << '\n';
134 if (!rightmargin.empty())
135 os << "\\rightmargin "
136 << VSpace(rightmargin).asLyXCommand() << '\n';
137 if (!bottommargin.empty())
138 os << "\\bottommargin "
139 << VSpace(bottommargin).asLyXCommand() << '\n';
140 if (!headheight.empty())
141 os << "\\headheight "
142 << VSpace(headheight).asLyXCommand() << '\n';
143 if (!headsep.empty())
145 << VSpace(headsep).asLyXCommand() << '\n';
146 if (!footskip.empty())
148 << VSpace(footskip).asLyXCommand() << '\n';
149 os << "\\secnumdepth " << secnumdepth
150 << "\n\\tocdepth " << tocdepth
151 << "\n\\paragraph_separation "
152 << string_paragraph_separation[paragraph_separation]
153 << "\n\\defskip " << defskip.asLyXCommand()
154 << "\n\\quotes_language "
155 << string_quotes_language[quotes_language] << '\n';
156 switch (quotes_times) {
157 // An output operator for insetquotes would be nice
158 case InsetQuotes::SingleQ:
159 os << "\\quotes_times 1\n"; break;
160 case InsetQuotes::DoubleQ:
161 os << "\\quotes_times 2\n"; break;
163 os << "\\papercolumns " << columns
164 << "\n\\papersides " << sides
165 << "\n\\paperpagestyle " << pagestyle << '\n';
166 for (int i = 0; i < 4; ++i) {
167 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
168 if (user_defined_bullets[i].getFont() != -1) {
169 os << "\\bullet " << i
171 << user_defined_bullets[i].getFont()
173 << user_defined_bullets[i].getCharacter()
175 << user_defined_bullets[i].getSize()
176 << "\n\\end_bullet\n";
179 os << "\\bulletLaTeX " << i
181 << user_defined_bullets[i].getText()
182 << "\"\n\\end_bullet\n";
187 os << "\\tracking_changes " << tracking_changes << "\n";
191 void BufferParams::writeLaTeX(ostream & os, LaTeXFeatures & features,
192 TexRow & texrow) const
194 os << "\\documentclass";
196 LyXTextClass const & tclass = getLyXTextClass();
198 ostringstream clsoptions; // the document class options.
200 if (tokenPos(tclass.opt_fontsize(),
201 '|', fontsize) >= 0) {
202 // only write if existing in list (and not default)
203 clsoptions << fontsize << "pt,";
208 (paperpackage == PACKAGE_NONE)) {
211 clsoptions << "a4paper,";
214 clsoptions << "letterpaper,";
217 clsoptions << "a5paper,";
220 clsoptions << "b5paper,";
222 case PAPER_EXECUTIVEPAPER:
223 clsoptions << "executivepaper,";
225 case PAPER_LEGALPAPER:
226 clsoptions << "legalpaper,";
232 if (sides != tclass.sides()) {
234 case LyXTextClass::OneSide:
235 clsoptions << "oneside,";
237 case LyXTextClass::TwoSides:
238 clsoptions << "twoside,";
244 if (columns != tclass.columns()) {
246 clsoptions << "twocolumn,";
248 clsoptions << "onecolumn,";
252 && orientation == ORIENTATION_LANDSCAPE)
253 clsoptions << "landscape,";
255 // language should be a parameter to \documentclass
257 ostringstream language_options;
258 if (language->babel() == "hebrew"
259 && default_language->babel() != "hebrew")
260 // This seems necessary
261 features.useLanguage(default_language);
263 if (lyxrc.language_use_babel ||
264 language->lang() != lyxrc.default_language ||
265 features.hasLanguages()) {
267 language_options << features.getLanguages();
268 language_options << language->babel();
269 if (lyxrc.language_global_options)
270 clsoptions << language_options.str() << ',';
273 // the user-defined options
274 if (!options.empty()) {
275 clsoptions << options << ',';
278 string strOptions(STRCONV(clsoptions.str()));
279 if (!strOptions.empty()) {
280 strOptions = rtrim(strOptions, ",");
281 os << '[' << strOptions << ']';
284 os << '{' << tclass.latexname() << "}\n";
286 // end of \documentclass defs
288 // font selection must be done before loading fontenc.sty
289 // The ae package is not needed when using OT1 font encoding.
290 if (fonts != "default" &&
291 (fonts != "ae" || lyxrc.fontenc != "default")) {
292 os << "\\usepackage{" << fonts << "}\n";
295 os << "\\usepackage{aecompl}\n";
299 // this one is not per buffer
300 if (lyxrc.fontenc != "default") {
301 os << "\\usepackage[" << lyxrc.fontenc
306 if (inputenc == "auto") {
307 string const doc_encoding =
308 language->encoding()->LatexName();
310 // Create a list with all the input encodings used
312 std::set<string> encodings =
313 features.getEncodingSet(doc_encoding);
315 os << "\\usepackage[";
316 std::copy(encodings.begin(), encodings.end(),
317 std::ostream_iterator<string>(os, ","));
318 os << doc_encoding << "]{inputenc}\n";
320 } else if (inputenc != "default") {
321 os << "\\usepackage[" << inputenc
326 // At the very beginning the text parameters.
327 if (paperpackage != PACKAGE_NONE) {
328 switch (paperpackage) {
330 os << "\\usepackage{a4}\n";
334 os << "\\usepackage{a4wide}\n";
337 case PACKAGE_WIDEMARGINSA4:
338 os << "\\usepackage[widemargins]{a4}\n";
344 os << "\\usepackage{geometry}\n";
346 os << "\\geometry{verbose";
347 if (orientation == ORIENTATION_LANDSCAPE)
349 switch (papersize2) {
350 case VM_PAPER_CUSTOM:
351 if (!paperwidth.empty())
354 if (!paperheight.empty())
355 os << ",paperheight="
358 case VM_PAPER_USLETTER:
359 os << ",letterpaper";
361 case VM_PAPER_USLEGAL:
364 case VM_PAPER_USEXECUTIVE:
365 os << ",executivepaper";
386 // default papersize ie VM_PAPER_DEFAULT
387 switch (lyxrc.default_papersize) {
388 case PAPER_DEFAULT: // keep compiler happy
390 os << ",letterpaper";
392 case PAPER_LEGALPAPER:
395 case PAPER_EXECUTIVEPAPER:
396 os << ",executivepaper";
412 if (!topmargin.empty())
413 os << ",tmargin=" << topmargin;
414 if (!bottommargin.empty())
415 os << ",bmargin=" << bottommargin;
416 if (!leftmargin.empty())
417 os << ",lmargin=" << leftmargin;
418 if (!rightmargin.empty())
419 os << ",rmargin=" << rightmargin;
420 if (!headheight.empty())
421 os << ",headheight=" << headheight;
422 if (!headsep.empty())
423 os << ",headsep=" << headsep;
424 if (!footskip.empty())
425 os << ",footskip=" << footskip;
430 if (tokenPos(tclass.opt_pagestyle(),
431 '|', pagestyle) >= 0) {
432 if (pagestyle == "fancy") {
433 os << "\\usepackage{fancyhdr}\n";
436 os << "\\pagestyle{" << pagestyle << "}\n";
440 if (secnumdepth != tclass.secnumdepth()) {
441 os << "\\setcounter{secnumdepth}{"
446 if (tocdepth != tclass.tocdepth()) {
447 os << "\\setcounter{tocdepth}{"
453 if (paragraph_separation) {
454 switch (defskip.kind()) {
455 case VSpace::SMALLSKIP:
456 os << "\\setlength\\parskip{\\smallskipamount}\n";
458 case VSpace::MEDSKIP:
459 os << "\\setlength\\parskip{\\medskipamount}\n";
461 case VSpace::BIGSKIP:
462 os << "\\setlength\\parskip{\\bigskipamount}\n";
465 os << "\\setlength\\parskip{"
466 << defskip.length().asLatexString()
469 default: // should never happen // Then delete it.
470 os << "\\setlength\\parskip{\\medskipamount}\n";
475 os << "\\setlength\\parindent{0pt}\n";
479 // Now insert the LyX specific LaTeX commands...
481 // The optional packages;
482 string lyxpreamble(features.getPackages());
484 // this might be useful...
485 lyxpreamble += "\n\\makeatletter\n";
487 // Some macros LyX will need
488 string tmppreamble(features.getMacros());
490 if (!tmppreamble.empty()) {
491 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
492 "LyX specific LaTeX commands.\n"
493 + tmppreamble + '\n';
496 // the text class specific preamble
497 tmppreamble = features.getTClassPreamble();
498 if (!tmppreamble.empty()) {
499 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
500 "Textclass specific LaTeX commands.\n"
501 + tmppreamble + '\n';
504 /* the user-defined preamble */
505 if (!preamble.empty()) {
506 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
507 "User specified LaTeX commands.\n"
511 // Itemize bullet settings need to be last in case the user
512 // defines their own bullets that use a package included
513 // in the user-defined preamble -- ARRae
514 // Actually it has to be done much later than that
515 // since some packages like frenchb make modifications
516 // at \begin{document} time -- JMarc
518 for (int i = 0; i < 4; ++i) {
519 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
520 if (bullets_def.empty())
521 bullets_def="\\AtBeginDocument{\n";
522 bullets_def += " \\renewcommand{\\labelitemi";
524 // `i' is one less than the item to modify
537 bullets_def += "}{" +
538 user_defined_bullets[i].getText()
543 if (!bullets_def.empty())
544 lyxpreamble += bullets_def + "}\n\n";
546 // We try to load babel late, in case it interferes
547 // with other packages.
549 string tmp = lyxrc.language_package;
550 if (!lyxrc.language_global_options
551 && tmp == "\\usepackage{babel}")
552 tmp = string("\\usepackage[") +
553 STRCONV(language_options.str()) +
555 lyxpreamble += tmp + "\n";
556 lyxpreamble += features.getBabelOptions();
559 lyxpreamble += "\\makeatother\n";
561 // dvipost settings come after everything else
562 if (tracking_changes) {
565 "\\dvipost{osstart color push Red}\n"
566 "\\dvipost{osend color pop}\n"
567 "\\dvipost{cbstart color push Blue}\n"
568 "\\dvipost{cbend color pop} \n";
572 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
573 for (int j = 0; j != nlines; ++j) {
580 void BufferParams::setPaperStuff()
582 papersize = PAPER_DEFAULT;
583 char const c1 = paperpackage;
584 if (c1 == PACKAGE_NONE) {
585 char const c2 = papersize2;
586 if (c2 == VM_PAPER_USLETTER)
587 papersize = PAPER_USLETTER;
588 else if (c2 == VM_PAPER_USLEGAL)
589 papersize = PAPER_LEGALPAPER;
590 else if (c2 == VM_PAPER_USEXECUTIVE)
591 papersize = PAPER_EXECUTIVEPAPER;
592 else if (c2 == VM_PAPER_A3)
593 papersize = PAPER_A3PAPER;
594 else if (c2 == VM_PAPER_A4)
595 papersize = PAPER_A4PAPER;
596 else if (c2 == VM_PAPER_A5)
597 papersize = PAPER_A5PAPER;
598 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
600 papersize = PAPER_B5PAPER;
601 } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
602 (c1 == PACKAGE_WIDEMARGINSA4))
603 papersize = PAPER_A4PAPER;
607 void BufferParams::useClassDefaults()
609 LyXTextClass const & tclass = textclasslist[textclass];
611 sides = tclass.sides();
612 columns = tclass.columns();
613 pagestyle = tclass.pagestyle();
614 options = tclass.options();
615 secnumdepth = tclass.secnumdepth();
616 tocdepth = tclass.tocdepth();
620 bool BufferParams::hasClassDefaults() const
622 LyXTextClass const & tclass = textclasslist[textclass];
624 return (sides == tclass.sides()
625 && columns == tclass.columns()
626 && pagestyle == tclass.pagestyle()
627 && options == tclass.options()
628 && secnumdepth == tclass.secnumdepth()
629 && tocdepth == tclass.tocdepth());
633 LyXTextClass const & BufferParams::getLyXTextClass() const
635 return textclasslist[textclass];
639 void BufferParams::readPreamble(LyXLex & lex)
641 if (lex.getString() != "\\begin_preamble")
642 lyxerr << "Error (BufferParams::readPreamble):"
643 "consistency check failed." << endl;
645 preamble = lex.getLongString("\\end_preamble");
649 void BufferParams::readLanguage(LyXLex & lex)
651 if (!lex.next()) return;
653 string const tmptok = lex.getString();
655 // check if tmptok is part of tex_babel in tex-defs.h
656 language = languages.getLanguage(tmptok);
658 // Language tmptok was not found
659 language = default_language;
660 lyxerr << "Warning: Setting language `"
661 << tmptok << "' to `" << language->lang()
667 void BufferParams::readGraphicsDriver(LyXLex & lex)
669 if (!lex.next()) return;
671 string const tmptok = lex.getString();
672 // check if tmptok is part of tex_graphics in tex_defs.h
675 string const test = tex_graphics[n++];
677 if (test == tmptok) {
678 graphicsDriver = tmptok;
680 } else if (test == "last_item") {
682 "Warning: graphics driver `$$Token' not recognized!\n"
683 " Setting graphics driver to `default'.\n");
684 graphicsDriver = "default";