]> git.lyx.org Git - lyx.git/blob - src/bufferparams.C
move some code from buffer.C to bufferparams.c
[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                 set<string> encodings = features.getEncodingSet(doc_encoding);
313
314                 os << "\\usepackage[";
315                 std::copy(encodings.begin(), encodings.end(),
316                           std::ostream_iterator<string>(os, ","));
317                 os << doc_encoding << "]{inputenc}\n";
318                 texrow.newline();
319         } else if (inputenc != "default") {
320                 os << "\\usepackage[" << inputenc
321                    << "]{inputenc}\n";
322                 texrow.newline();
323         }
324
325         // At the very beginning the text parameters.
326         if (paperpackage != PACKAGE_NONE) {
327                 switch (paperpackage) {
328                 case PACKAGE_A4:
329                         os << "\\usepackage{a4}\n";
330                         texrow.newline();
331                         break;
332                 case PACKAGE_A4WIDE:
333                         os << "\\usepackage{a4wide}\n";
334                         texrow.newline();
335                         break;
336                 case PACKAGE_WIDEMARGINSA4:
337                         os << "\\usepackage[widemargins]{a4}\n";
338                         texrow.newline();
339                         break;
340                 }
341         }
342         if (use_geometry) {
343                 os << "\\usepackage{geometry}\n";
344                 texrow.newline();
345                 os << "\\geometry{verbose";
346                 if (orientation == ORIENTATION_LANDSCAPE)
347                         os << ",landscape";
348                 switch (papersize2) {
349                 case VM_PAPER_CUSTOM:
350                         if (!paperwidth.empty())
351                                 os << ",paperwidth="
352                                    << paperwidth;
353                         if (!paperheight.empty())
354                                 os << ",paperheight="
355                                    << paperheight;
356                         break;
357                 case VM_PAPER_USLETTER:
358                         os << ",letterpaper";
359                         break;
360                 case VM_PAPER_USLEGAL:
361                         os << ",legalpaper";
362                         break;
363                 case VM_PAPER_USEXECUTIVE:
364                         os << ",executivepaper";
365                         break;
366                 case VM_PAPER_A3:
367                         os << ",a3paper";
368                         break;
369                 case VM_PAPER_A4:
370                         os << ",a4paper";
371                         break;
372                 case VM_PAPER_A5:
373                         os << ",a5paper";
374                         break;
375                 case VM_PAPER_B3:
376                         os << ",b3paper";
377                         break;
378                 case VM_PAPER_B4:
379                         os << ",b4paper";
380                         break;
381                 case VM_PAPER_B5:
382                         os << ",b5paper";
383                         break;
384                 default:
385                                 // default papersize ie VM_PAPER_DEFAULT
386                         switch (lyxrc.default_papersize) {
387                         case PAPER_DEFAULT: // keep compiler happy
388                         case PAPER_USLETTER:
389                                 os << ",letterpaper";
390                                 break;
391                         case PAPER_LEGALPAPER:
392                                 os << ",legalpaper";
393                                 break;
394                         case PAPER_EXECUTIVEPAPER:
395                                 os << ",executivepaper";
396                                 break;
397                         case PAPER_A3PAPER:
398                                 os << ",a3paper";
399                                 break;
400                         case PAPER_A4PAPER:
401                                 os << ",a4paper";
402                                 break;
403                         case PAPER_A5PAPER:
404                                 os << ",a5paper";
405                                 break;
406                         case PAPER_B5PAPER:
407                                 os << ",b5paper";
408                                 break;
409                         }
410                 }
411                 if (!topmargin.empty())
412                         os << ",tmargin=" << topmargin;
413                 if (!bottommargin.empty())
414                         os << ",bmargin=" << bottommargin;
415                 if (!leftmargin.empty())
416                         os << ",lmargin=" << leftmargin;
417                 if (!rightmargin.empty())
418                         os << ",rmargin=" << rightmargin;
419                 if (!headheight.empty())
420                         os << ",headheight=" << headheight;
421                 if (!headsep.empty())
422                         os << ",headsep=" << headsep;
423                 if (!footskip.empty())
424                         os << ",footskip=" << footskip;
425                 os << "}\n";
426                 texrow.newline();
427         }
428
429         if (tokenPos(tclass.opt_pagestyle(),
430                      '|', pagestyle) >= 0) {
431                 if (pagestyle == "fancy") {
432                         os << "\\usepackage{fancyhdr}\n";
433                         texrow.newline();
434                 }
435                 os << "\\pagestyle{" << pagestyle << "}\n";
436                 texrow.newline();
437         }
438
439         if (secnumdepth != tclass.secnumdepth()) {
440                 os << "\\setcounter{secnumdepth}{"
441                    << secnumdepth
442                    << "}\n";
443                 texrow.newline();
444         }
445         if (tocdepth != tclass.tocdepth()) {
446                 os << "\\setcounter{tocdepth}{"
447                    << tocdepth
448                    << "}\n";
449                 texrow.newline();
450         }
451
452         if (paragraph_separation) {
453                 switch (defskip.kind()) {
454                 case VSpace::SMALLSKIP:
455                         os << "\\setlength\\parskip{\\smallskipamount}\n";
456                         break;
457                 case VSpace::MEDSKIP:
458                         os << "\\setlength\\parskip{\\medskipamount}\n";
459                         break;
460                 case VSpace::BIGSKIP:
461                         os << "\\setlength\\parskip{\\bigskipamount}\n";
462                         break;
463                 case VSpace::LENGTH:
464                         os << "\\setlength\\parskip{"
465                            << defskip.length().asLatexString()
466                            << "}\n";
467                         break;
468                 default: // should never happen // Then delete it.
469                         os << "\\setlength\\parskip{\\medskipamount}\n";
470                         break;
471                 }
472                 texrow.newline();
473
474                 os << "\\setlength\\parindent{0pt}\n";
475                 texrow.newline();
476         }
477
478         // Now insert the LyX specific LaTeX commands...
479
480         // The optional packages;
481         string lyxpreamble(features.getPackages());
482
483         // this might be useful...
484         lyxpreamble += "\n\\makeatletter\n";
485
486         // Some macros LyX will need
487         string tmppreamble(features.getMacros());
488
489         if (!tmppreamble.empty()) {
490                 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
491                         "LyX specific LaTeX commands.\n"
492                         + tmppreamble + '\n';
493         }
494
495         // the text class specific preamble
496         tmppreamble = features.getTClassPreamble();
497         if (!tmppreamble.empty()) {
498                 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
499                         "Textclass specific LaTeX commands.\n"
500                         + tmppreamble + '\n';
501         }
502
503         /* the user-defined preamble */
504         if (!preamble.empty()) {
505                 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
506                         "User specified LaTeX commands.\n"
507                         + preamble + '\n';
508         }
509
510         // Itemize bullet settings need to be last in case the user
511         // defines their own bullets that use a package included
512         // in the user-defined preamble -- ARRae
513         // Actually it has to be done much later than that
514         // since some packages like frenchb make modifications
515         // at \begin{document} time -- JMarc
516         string bullets_def;
517         for (int i = 0; i < 4; ++i) {
518                 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
519                         if (bullets_def.empty())
520                                 bullets_def="\\AtBeginDocument{\n";
521                         bullets_def += "  \\renewcommand{\\labelitemi";
522                         switch (i) {
523                                 // `i' is one less than the item to modify
524                         case 0:
525                                 break;
526                         case 1:
527                                 bullets_def += 'i';
528                                 break;
529                         case 2:
530                                 bullets_def += "ii";
531                                 break;
532                         case 3:
533                                 bullets_def += 'v';
534                                 break;
535                         }
536                         bullets_def += "}{" +
537                                 user_defined_bullets[i].getText()
538                                 + "}\n";
539                 }
540         }
541
542         if (!bullets_def.empty())
543                 lyxpreamble += bullets_def + "}\n\n";
544
545         // We try to load babel late, in case it interferes
546         // with other packages.
547         if (use_babel) {
548                 string tmp = lyxrc.language_package;
549                 if (!lyxrc.language_global_options
550                     && tmp == "\\usepackage{babel}")
551                         tmp = string("\\usepackage[") +
552                                 STRCONV(language_options.str()) +
553                                 "]{babel}";
554                 lyxpreamble += tmp + "\n";
555                 lyxpreamble += features.getBabelOptions();
556         }
557
558         lyxpreamble += "\\makeatother\n";
559
560         // dvipost settings come after everything else
561         if (tracking_changes) {
562                 lyxpreamble +=
563                         "\\dvipostlayout\n"
564                         "\\dvipost{osstart color push Red}\n"
565                         "\\dvipost{osend color pop}\n"
566                         "\\dvipost{cbstart color push Blue}\n"
567                         "\\dvipost{cbend color pop} \n";
568         }
569
570         int const nlines =
571                 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
572         for (int j = 0; j != nlines; ++j) {
573                 texrow.newline();
574         }
575
576         os << preamble;
577 }
578
579 void BufferParams::setPaperStuff()
580 {
581         papersize = PAPER_DEFAULT;
582         char const c1 = paperpackage;
583         if (c1 == PACKAGE_NONE) {
584                 char const c2 = papersize2;
585                 if (c2 == VM_PAPER_USLETTER)
586                         papersize = PAPER_USLETTER;
587                 else if (c2 == VM_PAPER_USLEGAL)
588                         papersize = PAPER_LEGALPAPER;
589                 else if (c2 == VM_PAPER_USEXECUTIVE)
590                         papersize = PAPER_EXECUTIVEPAPER;
591                 else if (c2 == VM_PAPER_A3)
592                         papersize = PAPER_A3PAPER;
593                 else if (c2 == VM_PAPER_A4)
594                         papersize = PAPER_A4PAPER;
595                 else if (c2 == VM_PAPER_A5)
596                         papersize = PAPER_A5PAPER;
597                 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
598                          (c2 == VM_PAPER_B5))
599                         papersize = PAPER_B5PAPER;
600         } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
601                    (c1 == PACKAGE_WIDEMARGINSA4))
602                 papersize = PAPER_A4PAPER;
603 }
604
605
606 void BufferParams::useClassDefaults()
607 {
608         LyXTextClass const & tclass = textclasslist[textclass];
609
610         sides = tclass.sides();
611         columns = tclass.columns();
612         pagestyle = tclass.pagestyle();
613         options = tclass.options();
614         secnumdepth = tclass.secnumdepth();
615         tocdepth = tclass.tocdepth();
616 }
617
618
619 bool BufferParams::hasClassDefaults() const
620 {
621         LyXTextClass const & tclass = textclasslist[textclass];
622
623         return (sides == tclass.sides()
624                 && columns == tclass.columns()
625                 && pagestyle == tclass.pagestyle()
626                 && options == tclass.options()
627                 && secnumdepth == tclass.secnumdepth()
628                 && tocdepth == tclass.tocdepth());
629 }
630
631
632 LyXTextClass const & BufferParams::getLyXTextClass() const
633 {
634         return textclasslist[textclass];
635 }
636
637
638 void BufferParams::readPreamble(LyXLex & lex)
639 {
640         if (lex.getString() != "\\begin_preamble")
641                 lyxerr << "Error (BufferParams::readPreamble):"
642                         "consistency check failed." << endl;
643
644         preamble = lex.getLongString("\\end_preamble");
645 }
646
647
648 void BufferParams::readLanguage(LyXLex & lex)
649 {
650         if (!lex.next()) return;
651
652         string const tmptok = lex.getString();
653
654         // check if tmptok is part of tex_babel in tex-defs.h
655         language = languages.getLanguage(tmptok);
656         if (!language) {
657                 // Language tmptok was not found
658                 language = default_language;
659                 lyxerr << "Warning: Setting language `"
660                        << tmptok << "' to `" << language->lang()
661                        << "'." << endl;
662         }
663 }
664
665
666 void BufferParams::readGraphicsDriver(LyXLex & lex)
667 {
668         if (!lex.next()) return;
669
670         string const tmptok = lex.getString();
671         // check if tmptok is part of tex_graphics in tex_defs.h
672         int n = 0;
673         while (true) {
674                 string const test = tex_graphics[n++];
675
676                 if (test == tmptok) {
677                         graphicsDriver = tmptok;
678                         break;
679                 } else if (test == "last_item") {
680                         lex.printError(
681                                 "Warning: graphics driver `$$Token' not recognized!\n"
682                                 "         Setting graphics driver to `default'.\n");
683                         graphicsDriver = "default";
684                         break;
685                 }
686         }
687 }