]> git.lyx.org Git - lyx.git/blob - src/bufferparams.C
Reorder member initializers to avoid compiler warning.
[lyx.git] / src / bufferparams.C
1 /**
2  * \file bufferparams.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  * \author André Pönitz
11  * \author Martin Vermeer
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "bufferparams.h"
19
20 #include "author.h"
21 #include "BranchList.h"
22 #include "Bullet.h"
23 #include "debug.h"
24 #include "encoding.h"
25 #include "gettext.h"
26 #include "language.h"
27 #include "LaTeXFeatures.h"
28 #include "latexrunparams.h"
29 #include "lyxlex.h"
30 #include "lyxrc.h"
31 #include "lyxtextclasslist.h"
32 #include "tex-strings.h"
33 #include "Spacing.h"
34 #include "texrow.h"
35 #include "vspace.h"
36
37 #include "frontends/Alert.h"
38
39 #include "support/lyxalgo.h" // for lyx::count
40
41 #include <boost/array.hpp>
42
43 #include "support/std_sstream.h"
44
45 namespace support = lyx::support;
46 using lyx::support::bformat;
47 using lyx::support::rtrim;
48 using lyx::support::tokenPos;
49
50 using std::endl;
51
52 using std::istringstream;
53 using std::ostream;
54 using std::ostringstream;
55 using std::pair;
56
57
58 struct BufferParams::Impl
59 {
60         Impl();
61
62         AuthorList authorlist;
63         BranchList branchlist;
64         boost::array<Bullet, 4> temp_bullets;
65         boost::array<Bullet, 4> user_defined_bullets;
66         Spacing spacing;
67         /** This is the amount of space used for paragraph_separation "skip",
68          * and for detached paragraphs in "indented" documents.
69          */
70         VSpace defskip;
71 };
72
73
74 BufferParams::Impl::Impl()
75         : defskip(VSpace::MEDSKIP)
76 {
77         // set initial author
78         authorlist.record(Author(lyxrc.user_name, lyxrc.user_email));
79 }
80
81
82 BufferParams::BufferParams()
83         : // Initialize textclass to point to article. if `first' is
84           // true in the returned pair, then `second' is the textclass
85           // number; if it is false, second is 0. In both cases, second
86           // is what we want.
87         textclass(textclasslist.NumberOfClass("article").second),
88         pimpl_(new Impl)
89 {
90         paragraph_separation = PARSEP_INDENT;
91         quotes_language = InsetQuotes::EnglishQ;
92         quotes_times = InsetQuotes::DoubleQ;
93         fontsize = "default";
94
95         /*  PaperLayout */
96         papersize = PAPER_DEFAULT;
97         papersize2 = VM_PAPER_DEFAULT; /* DEFAULT */
98         paperpackage = PACKAGE_NONE;
99         orientation = ORIENTATION_PORTRAIT;
100         use_geometry = false;
101         use_amsmath = AMS_AUTO;
102         use_natbib = false;
103         use_numerical_citations = false;
104         tracking_changes = false;
105         secnumdepth = 3;
106         tocdepth = 3;
107         language = default_language;
108         fonts = "default";
109         inputenc = "auto";
110         graphicsDriver = "default";
111         sides = LyXTextClass::OneSide;
112         columns = 1;
113         pagestyle = "default";
114         compressed = false;
115         for (int iter = 0; iter < 4; ++iter) {
116                 user_defined_bullet(iter) = ITEMIZE_DEFAULTS[iter];
117                 temp_bullet(iter) = ITEMIZE_DEFAULTS[iter];
118         }
119 }
120
121
122 BufferParams::~BufferParams()
123 {}
124
125
126 AuthorList & BufferParams::authors()
127 {
128         return pimpl_->authorlist;
129 }
130
131
132 AuthorList const & BufferParams::authors() const
133 {
134         return pimpl_->authorlist;
135 }
136
137
138 BranchList & BufferParams::branchlist()
139 {
140         return pimpl_->branchlist;
141 }
142
143
144 BranchList const & BufferParams::branchlist() const
145 {
146         return pimpl_->branchlist;
147 }
148
149
150 Bullet & BufferParams::temp_bullet(lyx::size_type index)
151 {
152         BOOST_ASSERT(index < 4);
153         return pimpl_->temp_bullets[index];
154 }
155
156
157 Bullet const & BufferParams::temp_bullet(lyx::size_type index) const
158 {
159         BOOST_ASSERT(index < 4);
160         return pimpl_->temp_bullets[index];
161 }
162
163
164 Bullet & BufferParams::user_defined_bullet(lyx::size_type index)
165 {
166         BOOST_ASSERT(index < 4);
167         return pimpl_->user_defined_bullets[index];
168 }
169
170
171 Bullet const & BufferParams::user_defined_bullet(lyx::size_type index) const
172 {
173         BOOST_ASSERT(index < 4);
174         return pimpl_->user_defined_bullets[index];
175 }
176
177
178 Spacing & BufferParams::spacing()
179 {
180         return pimpl_->spacing;
181 }
182
183
184 Spacing const & BufferParams::spacing() const
185 {
186         return pimpl_->spacing;
187 }
188
189
190 VSpace const & BufferParams::getDefSkip() const
191 {
192         return pimpl_->defskip;
193 }
194
195
196 void BufferParams::setDefSkip(VSpace const & vs)
197 {
198         pimpl_->defskip = vs;
199 }
200
201
202 string const BufferParams::readToken(LyXLex & lex, string const & token)
203 {
204         if (token == "\\textclass") {
205                 lex.eatLine();
206                 string const classname = lex.getString();
207                 pair<bool, lyx::textclass_type> pp =
208                         textclasslist.NumberOfClass(classname);
209                 if (pp.first) {
210                         textclass = pp.second;
211                 } else {
212                         textclass = 0;
213                         return classname;
214                 }
215                 if (!getLyXTextClass().isTeXClassAvailable()) {
216                         string msg = bformat(_("The document uses a missing "
217                                 "TeX class \"%1$s\".\n"), classname);
218                         Alert::warning(_("Document class not available"),
219                                        msg + _("LyX will not be able to produce output."));
220                 }
221         } else if (token == "\\begin_preamble") {
222                 readPreamble(lex);
223         } else if (token == "\\options") {
224                 lex.eatLine();
225                 options = lex.getString();
226         } else if (token == "\\language") {
227                 readLanguage(lex);
228         } else if (token == "\\inputencoding") {
229                 lex.eatLine();
230                 inputenc = lex.getString();
231         } else if (token == "\\graphics") {
232                 readGraphicsDriver(lex);
233         } else if (token == "\\fontscheme") {
234                 lex.eatLine();
235                 fonts = lex.getString();
236         } else if (token == "\\paragraph_separation") {
237                 int tmpret = lex.findToken(string_paragraph_separation);
238                 if (tmpret == -1)
239                         ++tmpret;
240                 paragraph_separation =
241                         static_cast<PARSEP>(tmpret);
242         } else if (token == "\\defskip") {
243                 lex.nextToken();
244                 pimpl_->defskip = VSpace(lex.getString());
245         } else if (token == "\\quotes_language") {
246                 // FIXME: should be params.readQuotes()
247                 int tmpret = lex.findToken(string_quotes_language);
248                 if (tmpret == -1)
249                         ++tmpret;
250                 InsetQuotes::quote_language tmpl =
251                         InsetQuotes::EnglishQ;
252                 switch (tmpret) {
253                 case 0:
254                         tmpl = InsetQuotes::EnglishQ;
255                         break;
256                 case 1:
257                         tmpl = InsetQuotes::SwedishQ;
258                         break;
259                 case 2:
260                         tmpl = InsetQuotes::GermanQ;
261                         break;
262                 case 3:
263                         tmpl = InsetQuotes::PolishQ;
264                         break;
265                 case 4:
266                         tmpl = InsetQuotes::FrenchQ;
267                         break;
268                 case 5:
269                         tmpl = InsetQuotes::DanishQ;
270                         break;
271                 }
272                 quotes_language = tmpl;
273         } else if (token == "\\quotes_times") {
274                 // FIXME: should be params.readQuotes()
275                 lex.nextToken();
276                 switch (lex.getInteger()) {
277                 case 1:
278                         quotes_times = InsetQuotes::SingleQ;
279                         break;
280                 case 2:
281                         quotes_times = InsetQuotes::DoubleQ;
282                         break;
283                 }
284         } else if (token == "\\papersize") {
285                 int tmpret = lex.findToken(string_papersize);
286                 if (tmpret == -1)
287                         ++tmpret;
288                 else
289                         papersize2 = VMARGIN_PAPER_TYPE(tmpret);
290         } else if (token == "\\paperpackage") {
291                 int tmpret = lex.findToken(string_paperpackages);
292                 if (tmpret == -1) {
293                         ++tmpret;
294                         paperpackage = PACKAGE_NONE;
295                 } else
296                         paperpackage = PAPER_PACKAGES(tmpret);
297         } else if (token == "\\use_geometry") {
298                 lex.nextToken();
299                 use_geometry = lex.getInteger();
300         } else if (token == "\\use_amsmath") {
301                 lex.nextToken();
302                 use_amsmath = static_cast<AMS>(
303                         lex.getInteger());
304         } else if (token == "\\use_natbib") {
305                 lex.nextToken();
306                 use_natbib = lex.getInteger();
307         } else if (token == "\\use_numerical_citations") {
308                 lex.nextToken();
309                 use_numerical_citations = lex.getInteger();
310         } else if (token == "\\tracking_changes") {
311                 lex.nextToken();
312                 tracking_changes = lex.getInteger();
313         } else if (token == "\\branch") {
314                 lex.nextToken();
315                 string branch = lex.getString();
316                 branchlist().add(branch);
317                 while (true) {
318                         lex.nextToken();
319                         string const tok = lex.getString();
320                         if (tok == "\\end_branch")
321                                 break;
322                         if (tok == "\\selected") {
323                                 lex.nextToken();
324                                 branchlist().setSelected(branch, lex.getInteger());
325                         }
326                         // not yet operational
327                         if (tok == "\\color") {
328                                 lex.nextToken();
329                                 string color = lex.getString();
330                                 branchlist().setColor(branch, color);
331                                 // Update also the LColor table:
332                                 if (color == "none")
333                                         color = lcolor.getX11Name(LColor::background);
334                                 lcolor.fill(static_cast<LColor::color>(lcolor.size()),
335                                                 branch, color);
336                         }
337                 }
338         } else if (token == "\\author") {
339                 lex.nextToken();
340                 istringstream ss(STRCONV(lex.getString()));
341                 Author a;
342                 ss >> a;
343                 author_map.push_back(pimpl_->authorlist.record(a));
344         } else if (token == "\\paperorientation") {
345                 int tmpret = lex.findToken(string_orientation);
346                 if (tmpret == -1)
347                         ++tmpret;
348                 orientation =
349                         static_cast<PAPER_ORIENTATION>(tmpret);
350         } else if (token == "\\paperwidth") {
351                 lex.next();
352                 paperwidth = lex.getString();
353         } else if (token == "\\paperheight") {
354                 lex.next();
355                 paperheight = lex.getString();
356         } else if (token == "\\leftmargin") {
357                 lex.next();
358                 leftmargin = lex.getString();
359         } else if (token == "\\topmargin") {
360                 lex.next();
361                 topmargin = lex.getString();
362         } else if (token == "\\rightmargin") {
363                 lex.next();
364                 rightmargin = lex.getString();
365         } else if (token == "\\bottommargin") {
366                 lex.next();
367                 bottommargin = lex.getString();
368         } else if (token == "\\headheight") {
369                 lex.next();
370                 headheight = lex.getString();
371         } else if (token == "\\headsep") {
372                 lex.next();
373                 headsep = lex.getString();
374         } else if (token == "\\footskip") {
375                 lex.next();
376                 footskip = lex.getString();
377         } else if (token == "\\paperfontsize") {
378                 lex.nextToken();
379                 fontsize = rtrim(lex.getString());
380         } else if (token == "\\papercolumns") {
381                 lex.nextToken();
382                 columns = lex.getInteger();
383         } else if (token == "\\papersides") {
384                 lex.nextToken();
385                 switch (lex.getInteger()) {
386                 default:
387                 case 1: sides = LyXTextClass::OneSide; break;
388                 case 2: sides = LyXTextClass::TwoSides; break;
389                 }
390         } else if (token == "\\paperpagestyle") {
391                 lex.nextToken();
392                 pagestyle = rtrim(lex.getString());
393         } else if (token == "\\bullet") {
394                 // FIXME: should be params.readBullets()
395                 lex.nextToken();
396                 int const index = lex.getInteger();
397                 lex.nextToken();
398                 int temp_int = lex.getInteger();
399                 user_defined_bullet(index).setFont(temp_int);
400                 temp_bullet(index).setFont(temp_int);
401                 lex.nextToken();
402                 temp_int = lex.getInteger();
403                 user_defined_bullet(index).setCharacter(temp_int);
404                 temp_bullet(index).setCharacter(temp_int);
405                 lex.nextToken();
406                 temp_int = lex.getInteger();
407                 user_defined_bullet(index).setSize(temp_int);
408                 temp_bullet(index).setSize(temp_int);
409                 lex.nextToken();
410                 string const temp_str = lex.getString();
411                 if (temp_str != "\\end_bullet") {
412                                 // this element isn't really necessary for
413                                 // parsing but is easier for humans
414                                 // to understand bullets. Put it back and
415                                 // set a debug message?
416                         lex.printError("\\end_bullet expected, got" + temp_str);
417                                 //how can I put it back?
418                 }
419         } else if (token == "\\bulletLaTeX") {
420                 // The bullet class should be able to read this.
421                 lex.nextToken();
422                 int const index = lex.getInteger();
423                 lex.next();
424                 string temp_str = lex.getString();
425                 string sum_str;
426                 while (temp_str != "\\end_bullet") {
427                                 // this loop structure is needed when user
428                                 // enters an empty string since the first
429                                 // thing returned will be the \\end_bullet
430                                 // OR
431                                 // if the LaTeX entry has spaces. Each element
432                                 // therefore needs to be read in turn
433                         sum_str += temp_str;
434                         lex.next();
435                         temp_str = lex.getString();
436                 }
437
438                 user_defined_bullet(index).setText(sum_str);
439                 temp_bullet(index).setText(sum_str);
440         } else if (token == "\\secnumdepth") {
441                 lex.nextToken();
442                 secnumdepth = lex.getInteger();
443         } else if (token == "\\tocdepth") {
444                 lex.nextToken();
445                 tocdepth = lex.getInteger();
446         } else if (token == "\\spacing") {
447                 lex.next();
448                 string const tmp = rtrim(lex.getString());
449                 Spacing::Space tmp_space = Spacing::Default;
450                 float tmp_val = 0.0;
451                 if (tmp == "single") {
452                         tmp_space = Spacing::Single;
453                 } else if (tmp == "onehalf") {
454                         tmp_space = Spacing::Onehalf;
455                 } else if (tmp == "double") {
456                         tmp_space = Spacing::Double;
457                 } else if (tmp == "other") {
458                         lex.next();
459                         tmp_space = Spacing::Other;
460                         tmp_val = lex.getFloat();
461                 } else {
462                         lex.printError("Unknown spacing token: '$$Token'");
463                 }
464 #if 0 // FIXME: Handled in lyx2lyx ?
465                 // Small hack so that files written with klyx will be
466                 // parsed correctly.
467                 if (first_par)
468                         par->params().spacing(Spacing(tmp_space, tmp_val));
469 #endif
470                 spacing().set(tmp_space, tmp_val);
471         } else if (token == "\\float_placement") {
472                 lex.nextToken();
473                 float_placement = lex.getString();
474         } else {
475                 return token;
476         }
477
478         return string();
479 }
480
481
482 void BufferParams::writeFile(ostream & os) const
483 {
484         // The top of the file is written by the buffer.
485         // Prints out the buffer info into the .lyx file given by file
486
487         // the textclass
488         os << "\\textclass " << textclasslist[textclass].name() << '\n';
489
490         // then the the preamble
491         if (!preamble.empty()) {
492                 // remove '\n' from the end of preamble
493                 string const tmppreamble = rtrim(preamble, "\n");
494                 os << "\\begin_preamble\n"
495                    << tmppreamble
496                    << "\n\\end_preamble\n";
497         }
498
499         /* the options */
500         if (!options.empty()) {
501                 os << "\\options " << options << '\n';
502         }
503
504         /* then the text parameters */
505         if (language != ignore_language)
506                 os << "\\language " << language->lang() << '\n';
507         os << "\\inputencoding " << inputenc
508            << "\n\\fontscheme " << fonts
509            << "\n\\graphics " << graphicsDriver << '\n';
510
511         if (!float_placement.empty()) {
512                 os << "\\float_placement " << float_placement << '\n';
513         }
514         os << "\\paperfontsize " << fontsize << '\n';
515
516         spacing().writeFile(os);
517
518         os << "\\papersize " << string_papersize[papersize2]
519            << "\n\\paperpackage " << string_paperpackages[paperpackage]
520            << "\n\\use_geometry " << use_geometry
521            << "\n\\use_amsmath " << use_amsmath
522            << "\n\\use_natbib " << use_natbib
523            << "\n\\use_numerical_citations " << use_numerical_citations
524            << "\n\\paperorientation " << string_orientation[orientation]
525            << '\n';
526
527         std::list<Branch>::const_iterator it = branchlist().begin();
528         std::list<Branch>::const_iterator end = branchlist().end();
529         for (; it != end; ++it) {
530                 os << "\\branch " << it->getBranch()
531                    << "\n\\selected " << it->getSelected()
532                    << "\n\\color " << it->getColor()
533                    << "\n\\end_branch"
534                    << "\n";
535         }
536
537         if (!paperwidth.empty())
538                 os << "\\paperwidth "
539                    << VSpace(paperwidth).asLyXCommand() << '\n';
540         if (!paperheight.empty())
541                 os << "\\paperheight "
542                    << VSpace(paperheight).asLyXCommand() << '\n';
543         if (!leftmargin.empty())
544                 os << "\\leftmargin "
545                    << VSpace(leftmargin).asLyXCommand() << '\n';
546         if (!topmargin.empty())
547                 os << "\\topmargin "
548                    << VSpace(topmargin).asLyXCommand() << '\n';
549         if (!rightmargin.empty())
550                 os << "\\rightmargin "
551                    << VSpace(rightmargin).asLyXCommand() << '\n';
552         if (!bottommargin.empty())
553                 os << "\\bottommargin "
554                    << VSpace(bottommargin).asLyXCommand() << '\n';
555         if (!headheight.empty())
556                 os << "\\headheight "
557                    << VSpace(headheight).asLyXCommand() << '\n';
558         if (!headsep.empty())
559                 os << "\\headsep "
560                    << VSpace(headsep).asLyXCommand() << '\n';
561         if (!footskip.empty())
562                 os << "\\footskip "
563                    << VSpace(footskip).asLyXCommand() << '\n';
564         os << "\\secnumdepth " << secnumdepth
565            << "\n\\tocdepth " << tocdepth
566            << "\n\\paragraph_separation "
567            << string_paragraph_separation[paragraph_separation]
568            << "\n\\defskip " << getDefSkip().asLyXCommand()
569            << "\n\\quotes_language "
570            << string_quotes_language[quotes_language] << '\n';
571         switch (quotes_times) {
572                 // An output operator for insetquotes would be nice
573         case InsetQuotes::SingleQ:
574                 os << "\\quotes_times 1\n"; break;
575         case InsetQuotes::DoubleQ:
576                 os << "\\quotes_times 2\n"; break;
577         }
578         os << "\\papercolumns " << columns
579            << "\n\\papersides " << sides
580            << "\n\\paperpagestyle " << pagestyle << '\n';
581         for (int i = 0; i < 4; ++i) {
582                 if (user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
583                         if (user_defined_bullet(i).getFont() != -1) {
584                                 os << "\\bullet " << i
585                                    << "\n\t"
586                                    << user_defined_bullet(i).getFont()
587                                    << "\n\t"
588                                    << user_defined_bullet(i).getCharacter()
589                                    << "\n\t"
590                                    << user_defined_bullet(i).getSize()
591                                    << "\n\\end_bullet\n";
592                         }
593                         else {
594                                 os << "\\bulletLaTeX " << i
595                                    << "\n\t\""
596                                    << user_defined_bullet(i).getText()
597                                    << "\"\n\\end_bullet\n";
598                         }
599                 }
600         }
601
602         os << "\\tracking_changes " << tracking_changes << "\n";
603
604         if (tracking_changes) {
605                 AuthorList::Authors::const_iterator it = pimpl_->authorlist.begin();
606                 AuthorList::Authors::const_iterator end = pimpl_->authorlist.end();
607                 for (; it != end; ++it) {
608                         os << "\\author " << it->second << "\n";
609                 }
610         }
611 }
612
613
614 bool BufferParams::writeLaTeX(ostream & os, LaTeXFeatures & features,
615                               TexRow & texrow) const
616 {
617         os << "\\documentclass";
618
619         LyXTextClass const & tclass = getLyXTextClass();
620
621         ostringstream clsoptions; // the document class options.
622
623         if (tokenPos(tclass.opt_fontsize(),
624                      '|', fontsize) >= 0) {
625                 // only write if existing in list (and not default)
626                 clsoptions << fontsize << "pt,";
627         }
628
629
630         if (!use_geometry &&
631             (paperpackage == PACKAGE_NONE)) {
632                 switch (papersize) {
633                 case PAPER_A3PAPER:
634                         clsoptions << "a3paper,";
635                         break;
636                 case PAPER_A4PAPER:
637                         clsoptions << "a4paper,";
638                         break;
639                 case PAPER_USLETTER:
640                         clsoptions << "letterpaper,";
641                         break;
642                 case PAPER_A5PAPER:
643                         clsoptions << "a5paper,";
644                         break;
645                 case PAPER_B5PAPER:
646                         clsoptions << "b5paper,";
647                         break;
648                 case PAPER_EXECUTIVEPAPER:
649                         clsoptions << "executivepaper,";
650                         break;
651                 case PAPER_LEGALPAPER:
652                         clsoptions << "legalpaper,";
653                         break;
654                 case PAPER_DEFAULT:
655                         break;
656                 }
657         }
658
659         // if needed
660         if (sides != tclass.sides()) {
661                 switch (sides) {
662                 case LyXTextClass::OneSide:
663                         clsoptions << "oneside,";
664                         break;
665                 case LyXTextClass::TwoSides:
666                         clsoptions << "twoside,";
667                         break;
668                 }
669         }
670
671         // if needed
672         if (columns != tclass.columns()) {
673                 if (columns == 2)
674                         clsoptions << "twocolumn,";
675                 else
676                         clsoptions << "onecolumn,";
677         }
678
679         if (!use_geometry
680             && orientation == ORIENTATION_LANDSCAPE)
681                 clsoptions << "landscape,";
682
683         // language should be a parameter to \documentclass
684         if (language->babel() == "hebrew"
685             && default_language->babel() != "hebrew")
686                 // This seems necessary
687                 features.useLanguage(default_language);
688
689         ostringstream language_options;
690         bool const use_babel = features.useBabel();
691         if (use_babel) {
692                 language_options << features.getLanguages();
693                 language_options << language->babel();
694                 if (lyxrc.language_global_options)
695                         clsoptions << language_options.str() << ',';
696         }
697
698         // the user-defined options
699         if (!options.empty()) {
700                 clsoptions << options << ',';
701         }
702
703         string strOptions(STRCONV(clsoptions.str()));
704         if (!strOptions.empty()) {
705                 strOptions = rtrim(strOptions, ",");
706                 os << '[' << strOptions << ']';
707         }
708
709         os << '{' << tclass.latexname() << "}\n";
710         texrow.newline();
711         // end of \documentclass defs
712
713         // font selection must be done before loading fontenc.sty
714         // The ae package is not needed when using OT1 font encoding.
715         if (fonts != "default" &&
716             (fonts != "ae" || lyxrc.fontenc != "default")) {
717                 os << "\\usepackage{" << fonts << "}\n";
718                 texrow.newline();
719                 if (fonts == "ae") {
720                         os << "\\usepackage{aecompl}\n";
721                         texrow.newline();
722                 }
723         }
724         // this one is not per buffer
725         if (lyxrc.fontenc != "default") {
726                 os << "\\usepackage[" << lyxrc.fontenc
727                    << "]{fontenc}\n";
728                 texrow.newline();
729         }
730
731         if (inputenc == "auto") {
732                 string const doc_encoding =
733                         language->encoding()->LatexName();
734
735                 // Create a list with all the input encodings used
736                 // in the document
737                 std::set<string> encodings =
738                         features.getEncodingSet(doc_encoding);
739
740                 os << "\\usepackage[";
741                 std::copy(encodings.begin(), encodings.end(),
742                           std::ostream_iterator<string>(os, ","));
743                 os << doc_encoding << "]{inputenc}\n";
744                 texrow.newline();
745         } else if (inputenc != "default") {
746                 os << "\\usepackage[" << inputenc
747                    << "]{inputenc}\n";
748                 texrow.newline();
749         }
750
751         // At the very beginning the text parameters.
752         if (paperpackage != PACKAGE_NONE) {
753                 switch (paperpackage) {
754                 case PACKAGE_NONE:
755                         break;
756                 case PACKAGE_A4:
757                         os << "\\usepackage{a4}\n";
758                         texrow.newline();
759                         break;
760                 case PACKAGE_A4WIDE:
761                         os << "\\usepackage{a4wide}\n";
762                         texrow.newline();
763                         break;
764                 case PACKAGE_WIDEMARGINSA4:
765                         os << "\\usepackage[widemargins]{a4}\n";
766                         texrow.newline();
767                         break;
768                 }
769         }
770         if (use_geometry) {
771                 os << "\\usepackage{geometry}\n";
772                 texrow.newline();
773                 os << "\\geometry{verbose";
774                 if (orientation == ORIENTATION_LANDSCAPE)
775                         os << ",landscape";
776                 switch (papersize2) {
777                 case VM_PAPER_CUSTOM:
778                         if (!paperwidth.empty())
779                                 os << ",paperwidth="
780                                    << paperwidth;
781                         if (!paperheight.empty())
782                                 os << ",paperheight="
783                                    << paperheight;
784                         break;
785                 case VM_PAPER_USLETTER:
786                         os << ",letterpaper";
787                         break;
788                 case VM_PAPER_USLEGAL:
789                         os << ",legalpaper";
790                         break;
791                 case VM_PAPER_USEXECUTIVE:
792                         os << ",executivepaper";
793                         break;
794                 case VM_PAPER_A3:
795                         os << ",a3paper";
796                         break;
797                 case VM_PAPER_A4:
798                         os << ",a4paper";
799                         break;
800                 case VM_PAPER_A5:
801                         os << ",a5paper";
802                         break;
803                 case VM_PAPER_B3:
804                         os << ",b3paper";
805                         break;
806                 case VM_PAPER_B4:
807                         os << ",b4paper";
808                         break;
809                 case VM_PAPER_B5:
810                         os << ",b5paper";
811                         break;
812                 default:
813                                 // default papersize ie VM_PAPER_DEFAULT
814                         switch (lyxrc.default_papersize) {
815                         case PAPER_DEFAULT: // keep compiler happy
816                         case PAPER_USLETTER:
817                                 os << ",letterpaper";
818                                 break;
819                         case PAPER_LEGALPAPER:
820                                 os << ",legalpaper";
821                                 break;
822                         case PAPER_EXECUTIVEPAPER:
823                                 os << ",executivepaper";
824                                 break;
825                         case PAPER_A3PAPER:
826                                 os << ",a3paper";
827                                 break;
828                         case PAPER_A4PAPER:
829                                 os << ",a4paper";
830                                 break;
831                         case PAPER_A5PAPER:
832                                 os << ",a5paper";
833                                 break;
834                         case PAPER_B5PAPER:
835                                 os << ",b5paper";
836                                 break;
837                         }
838                 }
839                 if (!topmargin.empty())
840                         os << ",tmargin=" << topmargin;
841                 if (!bottommargin.empty())
842                         os << ",bmargin=" << bottommargin;
843                 if (!leftmargin.empty())
844                         os << ",lmargin=" << leftmargin;
845                 if (!rightmargin.empty())
846                         os << ",rmargin=" << rightmargin;
847                 if (!headheight.empty())
848                         os << ",headheight=" << headheight;
849                 if (!headsep.empty())
850                         os << ",headsep=" << headsep;
851                 if (!footskip.empty())
852                         os << ",footskip=" << footskip;
853                 os << "}\n";
854                 texrow.newline();
855         }
856
857         if (tokenPos(tclass.opt_pagestyle(),
858                      '|', pagestyle) >= 0) {
859                 if (pagestyle == "fancy") {
860                         os << "\\usepackage{fancyhdr}\n";
861                         texrow.newline();
862                 }
863                 os << "\\pagestyle{" << pagestyle << "}\n";
864                 texrow.newline();
865         }
866
867         if (secnumdepth != tclass.secnumdepth()) {
868                 os << "\\setcounter{secnumdepth}{"
869                    << secnumdepth
870                    << "}\n";
871                 texrow.newline();
872         }
873         if (tocdepth != tclass.tocdepth()) {
874                 os << "\\setcounter{tocdepth}{"
875                    << tocdepth
876                    << "}\n";
877                 texrow.newline();
878         }
879
880         if (paragraph_separation) {
881                 switch (getDefSkip().kind()) {
882                 case VSpace::SMALLSKIP:
883                         os << "\\setlength\\parskip{\\smallskipamount}\n";
884                         break;
885                 case VSpace::MEDSKIP:
886                         os << "\\setlength\\parskip{\\medskipamount}\n";
887                         break;
888                 case VSpace::BIGSKIP:
889                         os << "\\setlength\\parskip{\\bigskipamount}\n";
890                         break;
891                 case VSpace::LENGTH:
892                         os << "\\setlength\\parskip{"
893                            << getDefSkip().length().asLatexString()
894                            << "}\n";
895                         break;
896                 default: // should never happen // Then delete it.
897                         os << "\\setlength\\parskip{\\medskipamount}\n";
898                         break;
899                 }
900                 texrow.newline();
901
902                 os << "\\setlength\\parindent{0pt}\n";
903                 texrow.newline();
904         }
905
906         // Now insert the LyX specific LaTeX commands...
907
908         // The optional packages;
909         string lyxpreamble(features.getPackages());
910
911         // this might be useful...
912         lyxpreamble += "\n\\makeatletter\n";
913
914         // Some macros LyX will need
915         string tmppreamble(features.getMacros());
916
917         if (!tmppreamble.empty()) {
918                 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
919                         "LyX specific LaTeX commands.\n"
920                         + tmppreamble + '\n';
921         }
922
923         // the text class specific preamble
924         tmppreamble = features.getTClassPreamble();
925         if (!tmppreamble.empty()) {
926                 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
927                         "Textclass specific LaTeX commands.\n"
928                         + tmppreamble + '\n';
929         }
930
931         /* the user-defined preamble */
932         if (!preamble.empty()) {
933                 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
934                         "User specified LaTeX commands.\n"
935                         + preamble + '\n';
936         }
937
938         // Itemize bullet settings need to be last in case the user
939         // defines their own bullets that use a package included
940         // in the user-defined preamble -- ARRae
941         // Actually it has to be done much later than that
942         // since some packages like frenchb make modifications
943         // at \begin{document} time -- JMarc
944         string bullets_def;
945         for (int i = 0; i < 4; ++i) {
946                 if (user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
947                         if (bullets_def.empty())
948                                 bullets_def="\\AtBeginDocument{\n";
949                         bullets_def += "  \\renewcommand{\\labelitemi";
950                         switch (i) {
951                                 // `i' is one less than the item to modify
952                         case 0:
953                                 break;
954                         case 1:
955                                 bullets_def += 'i';
956                                 break;
957                         case 2:
958                                 bullets_def += "ii";
959                                 break;
960                         case 3:
961                                 bullets_def += 'v';
962                                 break;
963                         }
964                         bullets_def += "}{" +
965                                 user_defined_bullet(i).getText()
966                                 + "}\n";
967                 }
968         }
969
970         if (!bullets_def.empty())
971                 lyxpreamble += bullets_def + "}\n\n";
972
973         // We try to load babel late, in case it interferes
974         // with other packages.
975         if (use_babel) {
976                 string tmp = lyxrc.language_package;
977                 if (!lyxrc.language_global_options
978                     && tmp == "\\usepackage{babel}")
979                         tmp = string("\\usepackage[") +
980                                 STRCONV(language_options.str()) +
981                                 "]{babel}";
982                 lyxpreamble += tmp + "\n";
983                 lyxpreamble += features.getBabelOptions();
984         }
985
986         lyxpreamble += "\\makeatother\n";
987
988         // dvipost settings come after everything else
989         if (tracking_changes) {
990                 lyxpreamble +=
991                         "\\dvipostlayout\n"
992                         "\\dvipost{osstart color push Red}\n"
993                         "\\dvipost{osend color pop}\n"
994                         "\\dvipost{cbstart color push Blue}\n"
995                         "\\dvipost{cbend color pop} \n";
996         }
997
998         int const nlines =
999                 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
1000         for (int j = 0; j != nlines; ++j) {
1001                 texrow.newline();
1002         }
1003
1004         os << lyxpreamble;
1005         return use_babel;
1006 }
1007
1008 void BufferParams::setPaperStuff()
1009 {
1010         papersize = PAPER_DEFAULT;
1011         char const c1 = paperpackage;
1012         if (c1 == PACKAGE_NONE) {
1013                 char const c2 = papersize2;
1014                 if (c2 == VM_PAPER_USLETTER)
1015                         papersize = PAPER_USLETTER;
1016                 else if (c2 == VM_PAPER_USLEGAL)
1017                         papersize = PAPER_LEGALPAPER;
1018                 else if (c2 == VM_PAPER_USEXECUTIVE)
1019                         papersize = PAPER_EXECUTIVEPAPER;
1020                 else if (c2 == VM_PAPER_A3)
1021                         papersize = PAPER_A3PAPER;
1022                 else if (c2 == VM_PAPER_A4)
1023                         papersize = PAPER_A4PAPER;
1024                 else if (c2 == VM_PAPER_A5)
1025                         papersize = PAPER_A5PAPER;
1026                 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
1027                          (c2 == VM_PAPER_B5))
1028                         papersize = PAPER_B5PAPER;
1029         } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
1030                    (c1 == PACKAGE_WIDEMARGINSA4))
1031                 papersize = PAPER_A4PAPER;
1032 }
1033
1034
1035 void BufferParams::useClassDefaults()
1036 {
1037         LyXTextClass const & tclass = textclasslist[textclass];
1038
1039         sides = tclass.sides();
1040         columns = tclass.columns();
1041         pagestyle = tclass.pagestyle();
1042         options = tclass.options();
1043         secnumdepth = tclass.secnumdepth();
1044         tocdepth = tclass.tocdepth();
1045 }
1046
1047
1048 bool BufferParams::hasClassDefaults() const
1049 {
1050         LyXTextClass const & tclass = textclasslist[textclass];
1051
1052         return (sides == tclass.sides()
1053                 && columns == tclass.columns()
1054                 && pagestyle == tclass.pagestyle()
1055                 && options == tclass.options()
1056                 && secnumdepth == tclass.secnumdepth()
1057                 && tocdepth == tclass.tocdepth());
1058 }
1059
1060
1061 LyXTextClass const & BufferParams::getLyXTextClass() const
1062 {
1063         return textclasslist[textclass];
1064 }
1065
1066
1067 void BufferParams::readPreamble(LyXLex & lex)
1068 {
1069         if (lex.getString() != "\\begin_preamble")
1070                 lyxerr << "Error (BufferParams::readPreamble):"
1071                         "consistency check failed." << endl;
1072
1073         preamble = lex.getLongString("\\end_preamble");
1074 }
1075
1076
1077 void BufferParams::readLanguage(LyXLex & lex)
1078 {
1079         if (!lex.next()) return;
1080
1081         string const tmptok = lex.getString();
1082
1083         // check if tmptok is part of tex_babel in tex-defs.h
1084         language = languages.getLanguage(tmptok);
1085         if (!language) {
1086                 // Language tmptok was not found
1087                 language = default_language;
1088                 lyxerr << "Warning: Setting language `"
1089                        << tmptok << "' to `" << language->lang()
1090                        << "'." << endl;
1091         }
1092 }
1093
1094
1095 void BufferParams::readGraphicsDriver(LyXLex & lex)
1096 {
1097         if (!lex.next()) return;
1098
1099         string const tmptok = lex.getString();
1100         // check if tmptok is part of tex_graphics in tex_defs.h
1101         int n = 0;
1102         while (true) {
1103                 string const test = tex_graphics[n++];
1104
1105                 if (test == tmptok) {
1106                         graphicsDriver = tmptok;
1107                         break;
1108                 } else if (test == "last_item") {
1109                         lex.printError(
1110                                 "Warning: graphics driver `$$Token' not recognized!\n"
1111                                 "         Setting graphics driver to `default'.\n");
1112                         graphicsDriver = "default";
1113                         break;
1114                 }
1115         }
1116 }
1117
1118
1119 string const BufferParams::paperSizeName() const
1120 {
1121         char real_papersize = papersize;
1122         if (real_papersize == PAPER_DEFAULT)
1123                 real_papersize = lyxrc.default_papersize;
1124
1125         switch (real_papersize) {
1126         case PAPER_A3PAPER:
1127                 return "a3";
1128         case PAPER_A4PAPER:
1129                 return "a4";
1130         case PAPER_A5PAPER:
1131                 return "a5";
1132         case PAPER_B5PAPER:
1133                 return "b5";
1134         case PAPER_EXECUTIVEPAPER:
1135                 return "foolscap";
1136         case PAPER_LEGALPAPER:
1137                 return "legal";
1138         case PAPER_USLETTER:
1139         default:
1140                 return "letter";
1141         }
1142 }
1143
1144
1145 string const BufferParams::dvips_options() const
1146 {
1147         string result;
1148
1149         if (use_geometry
1150             && papersize2 == VM_PAPER_CUSTOM
1151             && !lyxrc.print_paper_dimension_flag.empty()
1152             && !paperwidth.empty()
1153             && !paperheight.empty()) {
1154                 // using a custom papersize
1155                 result = lyxrc.print_paper_dimension_flag;
1156                 result += ' ' + paperwidth;
1157                 result += ',' + paperheight;
1158         } else {
1159                 string const paper_option = paperSizeName();
1160                 if (paper_option != "letter" ||
1161                     orientation != ORIENTATION_LANDSCAPE) {
1162                         // dvips won't accept -t letter -t landscape.
1163                         // In all other cases, include the paper size
1164                         // explicitly.
1165                         result = lyxrc.print_paper_flag;
1166                         result += ' ' + paper_option;
1167                 }
1168         }
1169         if (orientation == ORIENTATION_LANDSCAPE &&
1170             papersize2 != VM_PAPER_CUSTOM)
1171                 result += ' ' + lyxrc.print_landscape_flag;
1172         return result;
1173 }