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 set<string> encodings = features.getEncodingSet(doc_encoding);
314 os << "\\usepackage[";
315 std::copy(encodings.begin(), encodings.end(),
316 std::ostream_iterator<string>(os, ","));
317 os << doc_encoding << "]{inputenc}\n";
319 } else if (inputenc != "default") {
320 os << "\\usepackage[" << inputenc
325 // At the very beginning the text parameters.
326 if (paperpackage != PACKAGE_NONE) {
327 switch (paperpackage) {
329 os << "\\usepackage{a4}\n";
333 os << "\\usepackage{a4wide}\n";
336 case PACKAGE_WIDEMARGINSA4:
337 os << "\\usepackage[widemargins]{a4}\n";
343 os << "\\usepackage{geometry}\n";
345 os << "\\geometry{verbose";
346 if (orientation == ORIENTATION_LANDSCAPE)
348 switch (papersize2) {
349 case VM_PAPER_CUSTOM:
350 if (!paperwidth.empty())
353 if (!paperheight.empty())
354 os << ",paperheight="
357 case VM_PAPER_USLETTER:
358 os << ",letterpaper";
360 case VM_PAPER_USLEGAL:
363 case VM_PAPER_USEXECUTIVE:
364 os << ",executivepaper";
385 // default papersize ie VM_PAPER_DEFAULT
386 switch (lyxrc.default_papersize) {
387 case PAPER_DEFAULT: // keep compiler happy
389 os << ",letterpaper";
391 case PAPER_LEGALPAPER:
394 case PAPER_EXECUTIVEPAPER:
395 os << ",executivepaper";
411 if (!topmargin.empty())
412 os << ",tmargin=" << topmargin;
413 if (!bottommargin.empty())
414 os << ",bmargin=" << bottommargin;
415 if (!leftmargin.empty())
416 os << ",lmargin=" << leftmargin;
417 if (!rightmargin.empty())
418 os << ",rmargin=" << rightmargin;
419 if (!headheight.empty())
420 os << ",headheight=" << headheight;
421 if (!headsep.empty())
422 os << ",headsep=" << headsep;
423 if (!footskip.empty())
424 os << ",footskip=" << footskip;
429 if (tokenPos(tclass.opt_pagestyle(),
430 '|', pagestyle) >= 0) {
431 if (pagestyle == "fancy") {
432 os << "\\usepackage{fancyhdr}\n";
435 os << "\\pagestyle{" << pagestyle << "}\n";
439 if (secnumdepth != tclass.secnumdepth()) {
440 os << "\\setcounter{secnumdepth}{"
445 if (tocdepth != tclass.tocdepth()) {
446 os << "\\setcounter{tocdepth}{"
452 if (paragraph_separation) {
453 switch (defskip.kind()) {
454 case VSpace::SMALLSKIP:
455 os << "\\setlength\\parskip{\\smallskipamount}\n";
457 case VSpace::MEDSKIP:
458 os << "\\setlength\\parskip{\\medskipamount}\n";
460 case VSpace::BIGSKIP:
461 os << "\\setlength\\parskip{\\bigskipamount}\n";
464 os << "\\setlength\\parskip{"
465 << defskip.length().asLatexString()
468 default: // should never happen // Then delete it.
469 os << "\\setlength\\parskip{\\medskipamount}\n";
474 os << "\\setlength\\parindent{0pt}\n";
478 // Now insert the LyX specific LaTeX commands...
480 // The optional packages;
481 string lyxpreamble(features.getPackages());
483 // this might be useful...
484 lyxpreamble += "\n\\makeatletter\n";
486 // Some macros LyX will need
487 string tmppreamble(features.getMacros());
489 if (!tmppreamble.empty()) {
490 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
491 "LyX specific LaTeX commands.\n"
492 + tmppreamble + '\n';
495 // the text class specific preamble
496 tmppreamble = features.getTClassPreamble();
497 if (!tmppreamble.empty()) {
498 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
499 "Textclass specific LaTeX commands.\n"
500 + tmppreamble + '\n';
503 /* the user-defined preamble */
504 if (!preamble.empty()) {
505 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
506 "User specified LaTeX commands.\n"
510 // Itemize bullet settings need to be last in case the user
511 // defines their own bullets that use a package included
512 // in the user-defined preamble -- ARRae
513 // Actually it has to be done much later than that
514 // since some packages like frenchb make modifications
515 // at \begin{document} time -- JMarc
517 for (int i = 0; i < 4; ++i) {
518 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
519 if (bullets_def.empty())
520 bullets_def="\\AtBeginDocument{\n";
521 bullets_def += " \\renewcommand{\\labelitemi";
523 // `i' is one less than the item to modify
536 bullets_def += "}{" +
537 user_defined_bullets[i].getText()
542 if (!bullets_def.empty())
543 lyxpreamble += bullets_def + "}\n\n";
545 // We try to load babel late, in case it interferes
546 // with other packages.
548 string tmp = lyxrc.language_package;
549 if (!lyxrc.language_global_options
550 && tmp == "\\usepackage{babel}")
551 tmp = string("\\usepackage[") +
552 STRCONV(language_options.str()) +
554 lyxpreamble += tmp + "\n";
555 lyxpreamble += features.getBabelOptions();
558 lyxpreamble += "\\makeatother\n";
560 // dvipost settings come after everything else
561 if (tracking_changes) {
564 "\\dvipost{osstart color push Red}\n"
565 "\\dvipost{osend color pop}\n"
566 "\\dvipost{cbstart color push Blue}\n"
567 "\\dvipost{cbend color pop} \n";
571 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
572 for (int j = 0; j != nlines; ++j) {
579 void BufferParams::setPaperStuff()
581 papersize = PAPER_DEFAULT;
582 char const c1 = paperpackage;
583 if (c1 == PACKAGE_NONE) {
584 char const c2 = papersize2;
585 if (c2 == VM_PAPER_USLETTER)
586 papersize = PAPER_USLETTER;
587 else if (c2 == VM_PAPER_USLEGAL)
588 papersize = PAPER_LEGALPAPER;
589 else if (c2 == VM_PAPER_USEXECUTIVE)
590 papersize = PAPER_EXECUTIVEPAPER;
591 else if (c2 == VM_PAPER_A3)
592 papersize = PAPER_A3PAPER;
593 else if (c2 == VM_PAPER_A4)
594 papersize = PAPER_A4PAPER;
595 else if (c2 == VM_PAPER_A5)
596 papersize = PAPER_A5PAPER;
597 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
599 papersize = PAPER_B5PAPER;
600 } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
601 (c1 == PACKAGE_WIDEMARGINSA4))
602 papersize = PAPER_A4PAPER;
606 void BufferParams::useClassDefaults()
608 LyXTextClass const & tclass = textclasslist[textclass];
610 sides = tclass.sides();
611 columns = tclass.columns();
612 pagestyle = tclass.pagestyle();
613 options = tclass.options();
614 secnumdepth = tclass.secnumdepth();
615 tocdepth = tclass.tocdepth();
619 bool BufferParams::hasClassDefaults() const
621 LyXTextClass const & tclass = textclasslist[textclass];
623 return (sides == tclass.sides()
624 && columns == tclass.columns()
625 && pagestyle == tclass.pagestyle()
626 && options == tclass.options()
627 && secnumdepth == tclass.secnumdepth()
628 && tocdepth == tclass.tocdepth());
632 LyXTextClass const & BufferParams::getLyXTextClass() const
634 return textclasslist[textclass];
638 void BufferParams::readPreamble(LyXLex & lex)
640 if (lex.getString() != "\\begin_preamble")
641 lyxerr << "Error (BufferParams::readPreamble):"
642 "consistency check failed." << endl;
644 preamble = lex.getLongString("\\end_preamble");
648 void BufferParams::readLanguage(LyXLex & lex)
650 if (!lex.next()) return;
652 string const tmptok = lex.getString();
654 // check if tmptok is part of tex_babel in tex-defs.h
655 language = languages.getLanguage(tmptok);
657 // Language tmptok was not found
658 language = default_language;
659 lyxerr << "Warning: Setting language `"
660 << tmptok << "' to `" << language->lang()
666 void BufferParams::readGraphicsDriver(LyXLex & lex)
668 if (!lex.next()) return;
670 string const tmptok = lex.getString();
671 // check if tmptok is part of tex_graphics in tex_defs.h
674 string const test = tex_graphics[n++];
676 if (test == tmptok) {
677 graphicsDriver = tmptok;
679 } else if (test == "last_item") {
681 "Warning: graphics driver `$$Token' not recognized!\n"
682 " Setting graphics driver to `default'.\n");
683 graphicsDriver = "default";