]> git.lyx.org Git - lyx.git/blob - src/bufferparams.C
Oops...
[lyx.git] / src / bufferparams.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "bufferparams.h"
14 #include "tex-strings.h"
15 #include "encoding.h"
16 #include "layout.h"
17 #include "vspace.h"
18 #include "debug.h"
19 #include "lyxrc.h"
20 #include "language.h"
21 #include "lyxtextclasslist.h"
22 #include "lyxlex.h"
23 #include "Lsstream.h"
24
25 #include "support/lyxalgo.h" // for lyx::count
26 #include "support/lyxlib.h"
27 #include "support/lstrings.h"
28
29 #include <cstdlib>
30
31 using std::ostream;
32 using std::endl;
33
34 #ifdef WITH_WARNINGS
35 #warning Do we need this horrible thing? (JMarc)
36 #endif
37 bool use_babel;
38
39 BufferParams::BufferParams()
40         // Initialize textclass to point to article. if `first' is
41         // true in the returned pair, then `second' is the textclass
42         // number; if it is false, second is 0. In both cases, second
43         // is what we want.
44         : textclass(textclasslist.NumberOfClass("article").second)
45 {
46         paragraph_separation = PARSEP_INDENT;
47         defskip = VSpace(VSpace::MEDSKIP);
48         quotes_language = InsetQuotes::EnglishQ;
49         quotes_times = InsetQuotes::DoubleQ;
50         fontsize = "default";
51
52         /*  PaperLayout */
53         papersize = PAPER_DEFAULT;
54         papersize2 = VM_PAPER_DEFAULT; /* DEFAULT */
55         paperpackage = PACKAGE_NONE;
56         orientation = ORIENTATION_PORTRAIT;
57         use_geometry = false;
58         use_amsmath = false;
59         use_natbib = false;
60         use_numerical_citations = false;
61         tracking_changes = false;
62         secnumdepth = 3;
63         tocdepth = 3;
64         language = default_language;
65         fonts = "default";
66         inputenc = "auto";
67         graphicsDriver = "default";
68         sides = LyXTextClass::OneSide;
69         columns = 1;
70         pagestyle = "default";
71         for (int iter = 0; iter < 4; ++iter) {
72                 user_defined_bullets[iter] = ITEMIZE_DEFAULTS[iter];
73                 temp_bullets[iter] = ITEMIZE_DEFAULTS[iter];
74         }
75 }
76
77
78 void BufferParams::writeFile(ostream & os) const
79 {
80         // The top of the file is written by the buffer.
81         // Prints out the buffer info into the .lyx file given by file
82
83         // the textclass
84         os << "\\textclass " << textclasslist[textclass].name() << '\n';
85
86         // then the the preamble
87         if (!preamble.empty()) {
88                 // remove '\n' from the end of preamble
89                 string const tmppreamble = rtrim(preamble, "\n");
90                 os << "\\begin_preamble\n"
91                    << tmppreamble
92                    << "\n\\end_preamble\n";
93         }
94
95         /* the options */
96         if (!options.empty()) {
97                 os << "\\options " << options << '\n';
98         }
99
100         /* then the text parameters */
101         if (language != ignore_language)
102                 os << "\\language " << language->lang() << '\n';
103         os << "\\inputencoding " << inputenc
104            << "\n\\fontscheme " << fonts
105            << "\n\\graphics " << graphicsDriver << '\n';
106
107         if (!float_placement.empty()) {
108                 os << "\\float_placement " << float_placement << '\n';
109         }
110         os << "\\paperfontsize " << fontsize << '\n';
111
112         spacing.writeFile(os);
113
114         os << "\\papersize " << string_papersize[papersize2]
115            << "\n\\paperpackage " << string_paperpackages[paperpackage]
116            << "\n\\use_geometry " << use_geometry
117            << "\n\\use_amsmath " << use_amsmath
118            << "\n\\use_natbib " << use_natbib
119            << "\n\\use_numerical_citations " << use_numerical_citations
120            << "\n\\paperorientation " << string_orientation[orientation]
121            << '\n';
122         if (!paperwidth.empty())
123                 os << "\\paperwidth "
124                    << VSpace(paperwidth).asLyXCommand() << '\n';
125         if (!paperheight.empty())
126                 os << "\\paperheight "
127                    << VSpace(paperheight).asLyXCommand() << '\n';
128         if (!leftmargin.empty())
129                 os << "\\leftmargin "
130                    << VSpace(leftmargin).asLyXCommand() << '\n';
131         if (!topmargin.empty())
132                 os << "\\topmargin "
133                    << VSpace(topmargin).asLyXCommand() << '\n';
134         if (!rightmargin.empty())
135                 os << "\\rightmargin "
136                    << VSpace(rightmargin).asLyXCommand() << '\n';
137         if (!bottommargin.empty())
138                 os << "\\bottommargin "
139                    << VSpace(bottommargin).asLyXCommand() << '\n';
140         if (!headheight.empty())
141                 os << "\\headheight "
142                    << VSpace(headheight).asLyXCommand() << '\n';
143         if (!headsep.empty())
144                 os << "\\headsep "
145                    << VSpace(headsep).asLyXCommand() << '\n';
146         if (!footskip.empty())
147                 os << "\\footskip "
148                    << VSpace(footskip).asLyXCommand() << '\n';
149         os << "\\secnumdepth " << secnumdepth
150            << "\n\\tocdepth " << tocdepth
151            << "\n\\paragraph_separation "
152            << string_paragraph_separation[paragraph_separation]
153            << "\n\\defskip " << defskip.asLyXCommand()
154            << "\n\\quotes_language "
155            << string_quotes_language[quotes_language] << '\n';
156         switch (quotes_times) {
157                 // An output operator for insetquotes would be nice
158         case InsetQuotes::SingleQ:
159                 os << "\\quotes_times 1\n"; break;
160         case InsetQuotes::DoubleQ:
161                 os << "\\quotes_times 2\n"; break;
162         }
163         os << "\\papercolumns " << columns
164            << "\n\\papersides " << sides
165            << "\n\\paperpagestyle " << pagestyle << '\n';
166         for (int i = 0; i < 4; ++i) {
167                 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
168                         if (user_defined_bullets[i].getFont() != -1) {
169                                 os << "\\bullet " << i
170                                    << "\n\t"
171                                    << user_defined_bullets[i].getFont()
172                                    << "\n\t"
173                                    << user_defined_bullets[i].getCharacter()
174                                    << "\n\t"
175                                    << user_defined_bullets[i].getSize()
176                                    << "\n\\end_bullet\n";
177                         }
178                         else {
179                                 os << "\\bulletLaTeX " << i
180                                    << "\n\t\""
181                                    << user_defined_bullets[i].getText()
182                                    << "\"\n\\end_bullet\n";
183                         }
184                 }
185         }
186
187         os << "\\tracking_changes " << tracking_changes << "\n";
188 }
189
190
191 void BufferParams::writeLaTeX(ostream & os, LaTeXFeatures & features,
192                               TexRow & texrow) const
193 {
194         os << "\\documentclass";
195
196         LyXTextClass const & tclass = getLyXTextClass();
197
198         ostringstream clsoptions; // the document class options.
199
200         if (tokenPos(tclass.opt_fontsize(),
201                      '|', fontsize) >= 0) {
202                 // only write if existing in list (and not default)
203                 clsoptions << fontsize << "pt,";
204         }
205
206
207         if (!use_geometry &&
208             (paperpackage == PACKAGE_NONE)) {
209                 switch (papersize) {
210                 case PAPER_A4PAPER:
211                         clsoptions << "a4paper,";
212                         break;
213                 case PAPER_USLETTER:
214                         clsoptions << "letterpaper,";
215                         break;
216                 case PAPER_A5PAPER:
217                         clsoptions << "a5paper,";
218                         break;
219                 case PAPER_B5PAPER:
220                         clsoptions << "b5paper,";
221                         break;
222                 case PAPER_EXECUTIVEPAPER:
223                         clsoptions << "executivepaper,";
224                         break;
225                 case PAPER_LEGALPAPER:
226                         clsoptions << "legalpaper,";
227                         break;
228                 }
229         }
230
231         // if needed
232         if (sides != tclass.sides()) {
233                 switch (sides) {
234                 case LyXTextClass::OneSide:
235                         clsoptions << "oneside,";
236                         break;
237                 case LyXTextClass::TwoSides:
238                         clsoptions << "twoside,";
239                         break;
240                 }
241         }
242
243         // if needed
244         if (columns != tclass.columns()) {
245                 if (columns == 2)
246                         clsoptions << "twocolumn,";
247                 else
248                         clsoptions << "onecolumn,";
249         }
250
251         if (!use_geometry
252             && orientation == ORIENTATION_LANDSCAPE)
253                 clsoptions << "landscape,";
254
255         // language should be a parameter to \documentclass
256         use_babel = false;
257         ostringstream language_options;
258         if (language->babel() == "hebrew"
259             && default_language->babel() != "hebrew")
260                 // This seems necessary
261                 features.useLanguage(default_language);
262
263         if (lyxrc.language_use_babel ||
264             language->lang() != lyxrc.default_language ||
265             features.hasLanguages()) {
266                 use_babel = true;
267                 language_options << features.getLanguages();
268                 language_options << language->babel();
269                 if (lyxrc.language_global_options)
270                         clsoptions << language_options.str() << ',';
271         }
272
273         // the user-defined options
274         if (!options.empty()) {
275                 clsoptions << options << ',';
276         }
277
278         string strOptions(STRCONV(clsoptions.str()));
279         if (!strOptions.empty()) {
280                 strOptions = rtrim(strOptions, ",");
281                 os << '[' << strOptions << ']';
282         }
283
284         os << '{' << tclass.latexname() << "}\n";
285         texrow.newline();
286         // end of \documentclass defs
287
288         // font selection must be done before loading fontenc.sty
289         // The ae package is not needed when using OT1 font encoding.
290         if (fonts != "default" &&
291             (fonts != "ae" || lyxrc.fontenc != "default")) {
292                 os << "\\usepackage{" << fonts << "}\n";
293                 texrow.newline();
294                 if (fonts == "ae") {
295                         os << "\\usepackage{aecompl}\n";
296                         texrow.newline();
297                 }
298         }
299         // this one is not per buffer
300         if (lyxrc.fontenc != "default") {
301                 os << "\\usepackage[" << lyxrc.fontenc
302                    << "]{fontenc}\n";
303                 texrow.newline();
304         }
305
306         if (inputenc == "auto") {
307                 string const doc_encoding =
308                         language->encoding()->LatexName();
309
310                 // Create a list with all the input encodings used
311                 // in the document
312                 std::set<string> encodings =
313                         features.getEncodingSet(doc_encoding);
314
315                 os << "\\usepackage[";
316                 std::copy(encodings.begin(), encodings.end(),
317                           std::ostream_iterator<string>(os, ","));
318                 os << doc_encoding << "]{inputenc}\n";
319                 texrow.newline();
320         } else if (inputenc != "default") {
321                 os << "\\usepackage[" << inputenc
322                    << "]{inputenc}\n";
323                 texrow.newline();
324         }
325
326         // At the very beginning the text parameters.
327         if (paperpackage != PACKAGE_NONE) {
328                 switch (paperpackage) {
329                 case PACKAGE_A4:
330                         os << "\\usepackage{a4}\n";
331                         texrow.newline();
332                         break;
333                 case PACKAGE_A4WIDE:
334                         os << "\\usepackage{a4wide}\n";
335                         texrow.newline();
336                         break;
337                 case PACKAGE_WIDEMARGINSA4:
338                         os << "\\usepackage[widemargins]{a4}\n";
339                         texrow.newline();
340                         break;
341                 }
342         }
343         if (use_geometry) {
344                 os << "\\usepackage{geometry}\n";
345                 texrow.newline();
346                 os << "\\geometry{verbose";
347                 if (orientation == ORIENTATION_LANDSCAPE)
348                         os << ",landscape";
349                 switch (papersize2) {
350                 case VM_PAPER_CUSTOM:
351                         if (!paperwidth.empty())
352                                 os << ",paperwidth="
353                                    << paperwidth;
354                         if (!paperheight.empty())
355                                 os << ",paperheight="
356                                    << paperheight;
357                         break;
358                 case VM_PAPER_USLETTER:
359                         os << ",letterpaper";
360                         break;
361                 case VM_PAPER_USLEGAL:
362                         os << ",legalpaper";
363                         break;
364                 case VM_PAPER_USEXECUTIVE:
365                         os << ",executivepaper";
366                         break;
367                 case VM_PAPER_A3:
368                         os << ",a3paper";
369                         break;
370                 case VM_PAPER_A4:
371                         os << ",a4paper";
372                         break;
373                 case VM_PAPER_A5:
374                         os << ",a5paper";
375                         break;
376                 case VM_PAPER_B3:
377                         os << ",b3paper";
378                         break;
379                 case VM_PAPER_B4:
380                         os << ",b4paper";
381                         break;
382                 case VM_PAPER_B5:
383                         os << ",b5paper";
384                         break;
385                 default:
386                                 // default papersize ie VM_PAPER_DEFAULT
387                         switch (lyxrc.default_papersize) {
388                         case PAPER_DEFAULT: // keep compiler happy
389                         case PAPER_USLETTER:
390                                 os << ",letterpaper";
391                                 break;
392                         case PAPER_LEGALPAPER:
393                                 os << ",legalpaper";
394                                 break;
395                         case PAPER_EXECUTIVEPAPER:
396                                 os << ",executivepaper";
397                                 break;
398                         case PAPER_A3PAPER:
399                                 os << ",a3paper";
400                                 break;
401                         case PAPER_A4PAPER:
402                                 os << ",a4paper";
403                                 break;
404                         case PAPER_A5PAPER:
405                                 os << ",a5paper";
406                                 break;
407                         case PAPER_B5PAPER:
408                                 os << ",b5paper";
409                                 break;
410                         }
411                 }
412                 if (!topmargin.empty())
413                         os << ",tmargin=" << topmargin;
414                 if (!bottommargin.empty())
415                         os << ",bmargin=" << bottommargin;
416                 if (!leftmargin.empty())
417                         os << ",lmargin=" << leftmargin;
418                 if (!rightmargin.empty())
419                         os << ",rmargin=" << rightmargin;
420                 if (!headheight.empty())
421                         os << ",headheight=" << headheight;
422                 if (!headsep.empty())
423                         os << ",headsep=" << headsep;
424                 if (!footskip.empty())
425                         os << ",footskip=" << footskip;
426                 os << "}\n";
427                 texrow.newline();
428         }
429
430         if (tokenPos(tclass.opt_pagestyle(),
431                      '|', pagestyle) >= 0) {
432                 if (pagestyle == "fancy") {
433                         os << "\\usepackage{fancyhdr}\n";
434                         texrow.newline();
435                 }
436                 os << "\\pagestyle{" << pagestyle << "}\n";
437                 texrow.newline();
438         }
439
440         if (secnumdepth != tclass.secnumdepth()) {
441                 os << "\\setcounter{secnumdepth}{"
442                    << secnumdepth
443                    << "}\n";
444                 texrow.newline();
445         }
446         if (tocdepth != tclass.tocdepth()) {
447                 os << "\\setcounter{tocdepth}{"
448                    << tocdepth
449                    << "}\n";
450                 texrow.newline();
451         }
452
453         if (paragraph_separation) {
454                 switch (defskip.kind()) {
455                 case VSpace::SMALLSKIP:
456                         os << "\\setlength\\parskip{\\smallskipamount}\n";
457                         break;
458                 case VSpace::MEDSKIP:
459                         os << "\\setlength\\parskip{\\medskipamount}\n";
460                         break;
461                 case VSpace::BIGSKIP:
462                         os << "\\setlength\\parskip{\\bigskipamount}\n";
463                         break;
464                 case VSpace::LENGTH:
465                         os << "\\setlength\\parskip{"
466                            << defskip.length().asLatexString()
467                            << "}\n";
468                         break;
469                 default: // should never happen // Then delete it.
470                         os << "\\setlength\\parskip{\\medskipamount}\n";
471                         break;
472                 }
473                 texrow.newline();
474
475                 os << "\\setlength\\parindent{0pt}\n";
476                 texrow.newline();
477         }
478
479         // Now insert the LyX specific LaTeX commands...
480
481         // The optional packages;
482         string lyxpreamble(features.getPackages());
483
484         // this might be useful...
485         lyxpreamble += "\n\\makeatletter\n";
486
487         // Some macros LyX will need
488         string tmppreamble(features.getMacros());
489
490         if (!tmppreamble.empty()) {
491                 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
492                         "LyX specific LaTeX commands.\n"
493                         + tmppreamble + '\n';
494         }
495
496         // the text class specific preamble
497         tmppreamble = features.getTClassPreamble();
498         if (!tmppreamble.empty()) {
499                 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
500                         "Textclass specific LaTeX commands.\n"
501                         + tmppreamble + '\n';
502         }
503
504         /* the user-defined preamble */
505         if (!preamble.empty()) {
506                 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
507                         "User specified LaTeX commands.\n"
508                         + preamble + '\n';
509         }
510
511         // Itemize bullet settings need to be last in case the user
512         // defines their own bullets that use a package included
513         // in the user-defined preamble -- ARRae
514         // Actually it has to be done much later than that
515         // since some packages like frenchb make modifications
516         // at \begin{document} time -- JMarc
517         string bullets_def;
518         for (int i = 0; i < 4; ++i) {
519                 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
520                         if (bullets_def.empty())
521                                 bullets_def="\\AtBeginDocument{\n";
522                         bullets_def += "  \\renewcommand{\\labelitemi";
523                         switch (i) {
524                                 // `i' is one less than the item to modify
525                         case 0:
526                                 break;
527                         case 1:
528                                 bullets_def += 'i';
529                                 break;
530                         case 2:
531                                 bullets_def += "ii";
532                                 break;
533                         case 3:
534                                 bullets_def += 'v';
535                                 break;
536                         }
537                         bullets_def += "}{" +
538                                 user_defined_bullets[i].getText()
539                                 + "}\n";
540                 }
541         }
542
543         if (!bullets_def.empty())
544                 lyxpreamble += bullets_def + "}\n\n";
545
546         // We try to load babel late, in case it interferes
547         // with other packages.
548         if (use_babel) {
549                 string tmp = lyxrc.language_package;
550                 if (!lyxrc.language_global_options
551                     && tmp == "\\usepackage{babel}")
552                         tmp = string("\\usepackage[") +
553                                 STRCONV(language_options.str()) +
554                                 "]{babel}";
555                 lyxpreamble += tmp + "\n";
556                 lyxpreamble += features.getBabelOptions();
557         }
558
559         lyxpreamble += "\\makeatother\n";
560
561         // dvipost settings come after everything else
562         if (tracking_changes) {
563                 lyxpreamble +=
564                         "\\dvipostlayout\n"
565                         "\\dvipost{osstart color push Red}\n"
566                         "\\dvipost{osend color pop}\n"
567                         "\\dvipost{cbstart color push Blue}\n"
568                         "\\dvipost{cbend color pop} \n";
569         }
570
571         int const nlines =
572                 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
573         for (int j = 0; j != nlines; ++j) {
574                 texrow.newline();
575         }
576
577         os << lyxpreamble;
578 }
579
580 void BufferParams::setPaperStuff()
581 {
582         papersize = PAPER_DEFAULT;
583         char const c1 = paperpackage;
584         if (c1 == PACKAGE_NONE) {
585                 char const c2 = papersize2;
586                 if (c2 == VM_PAPER_USLETTER)
587                         papersize = PAPER_USLETTER;
588                 else if (c2 == VM_PAPER_USLEGAL)
589                         papersize = PAPER_LEGALPAPER;
590                 else if (c2 == VM_PAPER_USEXECUTIVE)
591                         papersize = PAPER_EXECUTIVEPAPER;
592                 else if (c2 == VM_PAPER_A3)
593                         papersize = PAPER_A3PAPER;
594                 else if (c2 == VM_PAPER_A4)
595                         papersize = PAPER_A4PAPER;
596                 else if (c2 == VM_PAPER_A5)
597                         papersize = PAPER_A5PAPER;
598                 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
599                          (c2 == VM_PAPER_B5))
600                         papersize = PAPER_B5PAPER;
601         } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
602                    (c1 == PACKAGE_WIDEMARGINSA4))
603                 papersize = PAPER_A4PAPER;
604 }
605
606
607 void BufferParams::useClassDefaults()
608 {
609         LyXTextClass const & tclass = textclasslist[textclass];
610
611         sides = tclass.sides();
612         columns = tclass.columns();
613         pagestyle = tclass.pagestyle();
614         options = tclass.options();
615         secnumdepth = tclass.secnumdepth();
616         tocdepth = tclass.tocdepth();
617 }
618
619
620 bool BufferParams::hasClassDefaults() const
621 {
622         LyXTextClass const & tclass = textclasslist[textclass];
623
624         return (sides == tclass.sides()
625                 && columns == tclass.columns()
626                 && pagestyle == tclass.pagestyle()
627                 && options == tclass.options()
628                 && secnumdepth == tclass.secnumdepth()
629                 && tocdepth == tclass.tocdepth());
630 }
631
632
633 LyXTextClass const & BufferParams::getLyXTextClass() const
634 {
635         return textclasslist[textclass];
636 }
637
638
639 void BufferParams::readPreamble(LyXLex & lex)
640 {
641         if (lex.getString() != "\\begin_preamble")
642                 lyxerr << "Error (BufferParams::readPreamble):"
643                         "consistency check failed." << endl;
644
645         preamble = lex.getLongString("\\end_preamble");
646 }
647
648
649 void BufferParams::readLanguage(LyXLex & lex)
650 {
651         if (!lex.next()) return;
652
653         string const tmptok = lex.getString();
654
655         // check if tmptok is part of tex_babel in tex-defs.h
656         language = languages.getLanguage(tmptok);
657         if (!language) {
658                 // Language tmptok was not found
659                 language = default_language;
660                 lyxerr << "Warning: Setting language `"
661                        << tmptok << "' to `" << language->lang()
662                        << "'." << endl;
663         }
664 }
665
666
667 void BufferParams::readGraphicsDriver(LyXLex & lex)
668 {
669         if (!lex.next()) return;
670
671         string const tmptok = lex.getString();
672         // check if tmptok is part of tex_graphics in tex_defs.h
673         int n = 0;
674         while (true) {
675                 string const test = tex_graphics[n++];
676
677                 if (test == tmptok) {
678                         graphicsDriver = tmptok;
679                         break;
680                 } else if (test == "last_item") {
681                         lex.printError(
682                                 "Warning: graphics driver `$$Token' not recognized!\n"
683                                 "         Setting graphics driver to `default'.\n");
684                         graphicsDriver = "default";
685                         break;
686                 }
687         }
688 }