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