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 int aid(authorlist.record(a));
193 author_ids.push_back(aid);
194 } else if (token == "\\paperorientation") {
195 int tmpret = lex.findToken(string_orientation);
199 static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
200 } else if (token == "\\paperwidth") {
202 paperwidth = lex.getString();
203 } else if (token == "\\paperheight") {
205 paperheight = lex.getString();
206 } else if (token == "\\leftmargin") {
208 leftmargin = lex.getString();
209 } else if (token == "\\topmargin") {
211 topmargin = lex.getString();
212 } else if (token == "\\rightmargin") {
214 rightmargin = lex.getString();
215 } else if (token == "\\bottommargin") {
217 bottommargin = lex.getString();
218 } else if (token == "\\headheight") {
220 headheight = lex.getString();
221 } else if (token == "\\headsep") {
223 headsep = lex.getString();
224 } else if (token == "\\footskip") {
226 footskip = lex.getString();
227 } else if (token == "\\paperfontsize") {
229 fontsize = rtrim(lex.getString());
230 } else if (token == "\\papercolumns") {
232 columns = lex.getInteger();
233 } else if (token == "\\papersides") {
235 switch (lex.getInteger()) {
237 case 1: sides = LyXTextClass::OneSide; break;
238 case 2: sides = LyXTextClass::TwoSides; break;
240 } else if (token == "\\paperpagestyle") {
242 pagestyle = rtrim(lex.getString());
243 } else if (token == "\\bullet") {
244 // FIXME: should be params.readBullets()
246 int const index = lex.getInteger();
248 int temp_int = lex.getInteger();
249 user_defined_bullets[index].setFont(temp_int);
250 temp_bullets[index].setFont(temp_int);
252 temp_int = lex.getInteger();
253 user_defined_bullets[index].setCharacter(temp_int);
254 temp_bullets[index].setCharacter(temp_int);
256 temp_int = lex.getInteger();
257 user_defined_bullets[index].setSize(temp_int);
258 temp_bullets[index].setSize(temp_int);
260 string const temp_str = lex.getString();
261 if (temp_str != "\\end_bullet") {
262 // this element isn't really necessary for
263 // parsing but is easier for humans
264 // to understand bullets. Put it back and
265 // set a debug message?
266 lex.printError("\\end_bullet expected, got" + temp_str);
267 //how can I put it back?
269 } else if (token == "\\bulletLaTeX") {
270 // The bullet class should be able to read this.
272 int const index = lex.getInteger();
274 string temp_str = lex.getString();
276 while (temp_str != "\\end_bullet") {
277 // this loop structure is needed when user
278 // enters an empty string since the first
279 // thing returned will be the \\end_bullet
281 // if the LaTeX entry has spaces. Each element
282 // therefore needs to be read in turn
285 temp_str = lex.getString();
288 user_defined_bullets[index].setText(sum_str);
289 temp_bullets[index].setText(sum_str);
290 } else if (token == "\\secnumdepth") {
292 secnumdepth = lex.getInteger();
293 } else if (token == "\\tocdepth") {
295 tocdepth = lex.getInteger();
296 } else if (token == "\\spacing") {
298 string const tmp = rtrim(lex.getString());
299 Spacing::Space tmp_space = Spacing::Default;
301 if (tmp == "single") {
302 tmp_space = Spacing::Single;
303 } else if (tmp == "onehalf") {
304 tmp_space = Spacing::Onehalf;
305 } else if (tmp == "double") {
306 tmp_space = Spacing::Double;
307 } else if (tmp == "other") {
309 tmp_space = Spacing::Other;
310 tmp_val = lex.getFloat();
312 lex.printError("Unknown spacing token: '$$Token'");
314 #if 0 // FIXME: Handled in lyx2lyx ?
315 // Small hack so that files written with klyx will be
318 par->params().spacing(Spacing(tmp_space, tmp_val));
320 spacing.set(tmp_space, tmp_val);
321 } else if (token == "\\float_placement") {
323 float_placement = lex.getString();
332 void BufferParams::writeFile(ostream & os) const
334 // The top of the file is written by the buffer.
335 // Prints out the buffer info into the .lyx file given by file
338 os << "\\textclass " << textclasslist[textclass].name() << '\n';
340 // then the the preamble
341 if (!preamble.empty()) {
342 // remove '\n' from the end of preamble
343 string const tmppreamble = rtrim(preamble, "\n");
344 os << "\\begin_preamble\n"
346 << "\n\\end_preamble\n";
350 if (!options.empty()) {
351 os << "\\options " << options << '\n';
354 /* then the text parameters */
355 if (language != ignore_language)
356 os << "\\language " << language->lang() << '\n';
357 os << "\\inputencoding " << inputenc
358 << "\n\\fontscheme " << fonts
359 << "\n\\graphics " << graphicsDriver << '\n';
361 if (!float_placement.empty()) {
362 os << "\\float_placement " << float_placement << '\n';
364 os << "\\paperfontsize " << fontsize << '\n';
366 spacing.writeFile(os);
368 os << "\\papersize " << string_papersize[papersize2]
369 << "\n\\paperpackage " << string_paperpackages[paperpackage]
370 << "\n\\use_geometry " << use_geometry
371 << "\n\\use_amsmath " << use_amsmath
372 << "\n\\use_natbib " << use_natbib
373 << "\n\\use_numerical_citations " << use_numerical_citations
374 << "\n\\paperorientation " << string_orientation[orientation]
376 if (!paperwidth.empty())
377 os << "\\paperwidth "
378 << VSpace(paperwidth).asLyXCommand() << '\n';
379 if (!paperheight.empty())
380 os << "\\paperheight "
381 << VSpace(paperheight).asLyXCommand() << '\n';
382 if (!leftmargin.empty())
383 os << "\\leftmargin "
384 << VSpace(leftmargin).asLyXCommand() << '\n';
385 if (!topmargin.empty())
387 << VSpace(topmargin).asLyXCommand() << '\n';
388 if (!rightmargin.empty())
389 os << "\\rightmargin "
390 << VSpace(rightmargin).asLyXCommand() << '\n';
391 if (!bottommargin.empty())
392 os << "\\bottommargin "
393 << VSpace(bottommargin).asLyXCommand() << '\n';
394 if (!headheight.empty())
395 os << "\\headheight "
396 << VSpace(headheight).asLyXCommand() << '\n';
397 if (!headsep.empty())
399 << VSpace(headsep).asLyXCommand() << '\n';
400 if (!footskip.empty())
402 << VSpace(footskip).asLyXCommand() << '\n';
403 os << "\\secnumdepth " << secnumdepth
404 << "\n\\tocdepth " << tocdepth
405 << "\n\\paragraph_separation "
406 << string_paragraph_separation[paragraph_separation]
407 << "\n\\defskip " << defskip.asLyXCommand()
408 << "\n\\quotes_language "
409 << string_quotes_language[quotes_language] << '\n';
410 switch (quotes_times) {
411 // An output operator for insetquotes would be nice
412 case InsetQuotes::SingleQ:
413 os << "\\quotes_times 1\n"; break;
414 case InsetQuotes::DoubleQ:
415 os << "\\quotes_times 2\n"; break;
417 os << "\\papercolumns " << columns
418 << "\n\\papersides " << sides
419 << "\n\\paperpagestyle " << pagestyle << '\n';
420 for (int i = 0; i < 4; ++i) {
421 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
422 if (user_defined_bullets[i].getFont() != -1) {
423 os << "\\bullet " << i
425 << user_defined_bullets[i].getFont()
427 << user_defined_bullets[i].getCharacter()
429 << user_defined_bullets[i].getSize()
430 << "\n\\end_bullet\n";
433 os << "\\bulletLaTeX " << i
435 << user_defined_bullets[i].getText()
436 << "\"\n\\end_bullet\n";
441 os << "\\tracking_changes " << tracking_changes << "\n";
443 if (tracking_changes) {
444 AuthorList::Authors::const_iterator it = authorlist.begin();
445 AuthorList::Authors::const_iterator end = authorlist.end();
446 for (; it != end; ++it) {
447 os << "\\author " << it->second << "\n";
453 void BufferParams::writeLaTeX(ostream & os, LaTeXFeatures & features,
454 TexRow & texrow) const
456 os << "\\documentclass";
458 LyXTextClass const & tclass = getLyXTextClass();
460 ostringstream clsoptions; // the document class options.
462 if (tokenPos(tclass.opt_fontsize(),
463 '|', fontsize) >= 0) {
464 // only write if existing in list (and not default)
465 clsoptions << fontsize << "pt,";
470 (paperpackage == PACKAGE_NONE)) {
473 clsoptions << "a4paper,";
476 clsoptions << "letterpaper,";
479 clsoptions << "a5paper,";
482 clsoptions << "b5paper,";
484 case PAPER_EXECUTIVEPAPER:
485 clsoptions << "executivepaper,";
487 case PAPER_LEGALPAPER:
488 clsoptions << "legalpaper,";
494 if (sides != tclass.sides()) {
496 case LyXTextClass::OneSide:
497 clsoptions << "oneside,";
499 case LyXTextClass::TwoSides:
500 clsoptions << "twoside,";
506 if (columns != tclass.columns()) {
508 clsoptions << "twocolumn,";
510 clsoptions << "onecolumn,";
514 && orientation == ORIENTATION_LANDSCAPE)
515 clsoptions << "landscape,";
517 // language should be a parameter to \documentclass
519 ostringstream language_options;
520 if (language->babel() == "hebrew"
521 && default_language->babel() != "hebrew")
522 // This seems necessary
523 features.useLanguage(default_language);
525 if (lyxrc.language_use_babel ||
526 language->lang() != lyxrc.default_language ||
527 features.hasLanguages()) {
529 language_options << features.getLanguages();
530 language_options << language->babel();
531 if (lyxrc.language_global_options)
532 clsoptions << language_options.str() << ',';
535 // the user-defined options
536 if (!options.empty()) {
537 clsoptions << options << ',';
540 string strOptions(STRCONV(clsoptions.str()));
541 if (!strOptions.empty()) {
542 strOptions = rtrim(strOptions, ",");
543 os << '[' << strOptions << ']';
546 os << '{' << tclass.latexname() << "}\n";
548 // end of \documentclass defs
550 // font selection must be done before loading fontenc.sty
551 // The ae package is not needed when using OT1 font encoding.
552 if (fonts != "default" &&
553 (fonts != "ae" || lyxrc.fontenc != "default")) {
554 os << "\\usepackage{" << fonts << "}\n";
557 os << "\\usepackage{aecompl}\n";
561 // this one is not per buffer
562 if (lyxrc.fontenc != "default") {
563 os << "\\usepackage[" << lyxrc.fontenc
568 if (inputenc == "auto") {
569 string const doc_encoding =
570 language->encoding()->LatexName();
572 // Create a list with all the input encodings used
574 std::set<string> encodings =
575 features.getEncodingSet(doc_encoding);
577 os << "\\usepackage[";
578 std::copy(encodings.begin(), encodings.end(),
579 std::ostream_iterator<string>(os, ","));
580 os << doc_encoding << "]{inputenc}\n";
582 } else if (inputenc != "default") {
583 os << "\\usepackage[" << inputenc
588 // At the very beginning the text parameters.
589 if (paperpackage != PACKAGE_NONE) {
590 switch (paperpackage) {
592 os << "\\usepackage{a4}\n";
596 os << "\\usepackage{a4wide}\n";
599 case PACKAGE_WIDEMARGINSA4:
600 os << "\\usepackage[widemargins]{a4}\n";
606 os << "\\usepackage{geometry}\n";
608 os << "\\geometry{verbose";
609 if (orientation == ORIENTATION_LANDSCAPE)
611 switch (papersize2) {
612 case VM_PAPER_CUSTOM:
613 if (!paperwidth.empty())
616 if (!paperheight.empty())
617 os << ",paperheight="
620 case VM_PAPER_USLETTER:
621 os << ",letterpaper";
623 case VM_PAPER_USLEGAL:
626 case VM_PAPER_USEXECUTIVE:
627 os << ",executivepaper";
648 // default papersize ie VM_PAPER_DEFAULT
649 switch (lyxrc.default_papersize) {
650 case PAPER_DEFAULT: // keep compiler happy
652 os << ",letterpaper";
654 case PAPER_LEGALPAPER:
657 case PAPER_EXECUTIVEPAPER:
658 os << ",executivepaper";
674 if (!topmargin.empty())
675 os << ",tmargin=" << topmargin;
676 if (!bottommargin.empty())
677 os << ",bmargin=" << bottommargin;
678 if (!leftmargin.empty())
679 os << ",lmargin=" << leftmargin;
680 if (!rightmargin.empty())
681 os << ",rmargin=" << rightmargin;
682 if (!headheight.empty())
683 os << ",headheight=" << headheight;
684 if (!headsep.empty())
685 os << ",headsep=" << headsep;
686 if (!footskip.empty())
687 os << ",footskip=" << footskip;
692 if (tokenPos(tclass.opt_pagestyle(),
693 '|', pagestyle) >= 0) {
694 if (pagestyle == "fancy") {
695 os << "\\usepackage{fancyhdr}\n";
698 os << "\\pagestyle{" << pagestyle << "}\n";
702 if (secnumdepth != tclass.secnumdepth()) {
703 os << "\\setcounter{secnumdepth}{"
708 if (tocdepth != tclass.tocdepth()) {
709 os << "\\setcounter{tocdepth}{"
715 if (paragraph_separation) {
716 switch (defskip.kind()) {
717 case VSpace::SMALLSKIP:
718 os << "\\setlength\\parskip{\\smallskipamount}\n";
720 case VSpace::MEDSKIP:
721 os << "\\setlength\\parskip{\\medskipamount}\n";
723 case VSpace::BIGSKIP:
724 os << "\\setlength\\parskip{\\bigskipamount}\n";
727 os << "\\setlength\\parskip{"
728 << defskip.length().asLatexString()
731 default: // should never happen // Then delete it.
732 os << "\\setlength\\parskip{\\medskipamount}\n";
737 os << "\\setlength\\parindent{0pt}\n";
741 // Now insert the LyX specific LaTeX commands...
743 // The optional packages;
744 string lyxpreamble(features.getPackages());
746 // this might be useful...
747 lyxpreamble += "\n\\makeatletter\n";
749 // Some macros LyX will need
750 string tmppreamble(features.getMacros());
752 if (!tmppreamble.empty()) {
753 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
754 "LyX specific LaTeX commands.\n"
755 + tmppreamble + '\n';
758 // the text class specific preamble
759 tmppreamble = features.getTClassPreamble();
760 if (!tmppreamble.empty()) {
761 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
762 "Textclass specific LaTeX commands.\n"
763 + tmppreamble + '\n';
766 /* the user-defined preamble */
767 if (!preamble.empty()) {
768 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
769 "User specified LaTeX commands.\n"
773 // Itemize bullet settings need to be last in case the user
774 // defines their own bullets that use a package included
775 // in the user-defined preamble -- ARRae
776 // Actually it has to be done much later than that
777 // since some packages like frenchb make modifications
778 // at \begin{document} time -- JMarc
780 for (int i = 0; i < 4; ++i) {
781 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
782 if (bullets_def.empty())
783 bullets_def="\\AtBeginDocument{\n";
784 bullets_def += " \\renewcommand{\\labelitemi";
786 // `i' is one less than the item to modify
799 bullets_def += "}{" +
800 user_defined_bullets[i].getText()
805 if (!bullets_def.empty())
806 lyxpreamble += bullets_def + "}\n\n";
808 // We try to load babel late, in case it interferes
809 // with other packages.
811 string tmp = lyxrc.language_package;
812 if (!lyxrc.language_global_options
813 && tmp == "\\usepackage{babel}")
814 tmp = string("\\usepackage[") +
815 STRCONV(language_options.str()) +
817 lyxpreamble += tmp + "\n";
818 lyxpreamble += features.getBabelOptions();
821 lyxpreamble += "\\makeatother\n";
823 // dvipost settings come after everything else
824 if (tracking_changes) {
827 "\\dvipost{osstart color push Red}\n"
828 "\\dvipost{osend color pop}\n"
829 "\\dvipost{cbstart color push Blue}\n"
830 "\\dvipost{cbend color pop} \n";
834 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
835 for (int j = 0; j != nlines; ++j) {
842 void BufferParams::setPaperStuff()
844 papersize = PAPER_DEFAULT;
845 char const c1 = paperpackage;
846 if (c1 == PACKAGE_NONE) {
847 char const c2 = papersize2;
848 if (c2 == VM_PAPER_USLETTER)
849 papersize = PAPER_USLETTER;
850 else if (c2 == VM_PAPER_USLEGAL)
851 papersize = PAPER_LEGALPAPER;
852 else if (c2 == VM_PAPER_USEXECUTIVE)
853 papersize = PAPER_EXECUTIVEPAPER;
854 else if (c2 == VM_PAPER_A3)
855 papersize = PAPER_A3PAPER;
856 else if (c2 == VM_PAPER_A4)
857 papersize = PAPER_A4PAPER;
858 else if (c2 == VM_PAPER_A5)
859 papersize = PAPER_A5PAPER;
860 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
862 papersize = PAPER_B5PAPER;
863 } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
864 (c1 == PACKAGE_WIDEMARGINSA4))
865 papersize = PAPER_A4PAPER;
869 void BufferParams::useClassDefaults()
871 LyXTextClass const & tclass = textclasslist[textclass];
873 sides = tclass.sides();
874 columns = tclass.columns();
875 pagestyle = tclass.pagestyle();
876 options = tclass.options();
877 secnumdepth = tclass.secnumdepth();
878 tocdepth = tclass.tocdepth();
882 bool BufferParams::hasClassDefaults() const
884 LyXTextClass const & tclass = textclasslist[textclass];
886 return (sides == tclass.sides()
887 && columns == tclass.columns()
888 && pagestyle == tclass.pagestyle()
889 && options == tclass.options()
890 && secnumdepth == tclass.secnumdepth()
891 && tocdepth == tclass.tocdepth());
895 LyXTextClass const & BufferParams::getLyXTextClass() const
897 return textclasslist[textclass];
901 void BufferParams::readPreamble(LyXLex & lex)
903 if (lex.getString() != "\\begin_preamble")
904 lyxerr << "Error (BufferParams::readPreamble):"
905 "consistency check failed." << endl;
907 preamble = lex.getLongString("\\end_preamble");
911 void BufferParams::readLanguage(LyXLex & lex)
913 if (!lex.next()) return;
915 string const tmptok = lex.getString();
917 // check if tmptok is part of tex_babel in tex-defs.h
918 language = languages.getLanguage(tmptok);
920 // Language tmptok was not found
921 language = default_language;
922 lyxerr << "Warning: Setting language `"
923 << tmptok << "' to `" << language->lang()
929 void BufferParams::readGraphicsDriver(LyXLex & lex)
931 if (!lex.next()) return;
933 string const tmptok = lex.getString();
934 // check if tmptok is part of tex_graphics in tex_defs.h
937 string const test = tex_graphics[n++];
939 if (test == tmptok) {
940 graphicsDriver = tmptok;
942 } else if (test == "last_item") {
944 "Warning: graphics driver `$$Token' not recognized!\n"
945 " Setting graphics driver to `default'.\n");
946 graphicsDriver = "default";