]> git.lyx.org Git - lyx.git/blob - src/bufferparams.C
Fix natbib bug spotted by JMarc.
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "bufferparams.h"
18 #include "tex-strings.h"
19 #include "layout.h"
20 #include "vspace.h"
21 #include "debug.h"
22 #include "lyxrc.h"
23 #include "language.h"
24 #include "lyxtextclasslist.h"
25 #include "lyxlex.h"
26
27 #include "support/lyxlib.h"
28 #include "support/lstrings.h"
29
30 #include <cstdlib>
31
32 using std::ostream;
33 using std::endl;
34
35 BufferParams::BufferParams()
36         // Initialize textclass to point to article. if `first' is
37         // true in the returned pair, then `second' is the textclass
38         // number; if it is false, second is 0. In both cases, second
39         // is what we want.
40         : textclass(textclasslist.NumberOfClass("article").second)
41 {
42         paragraph_separation = PARSEP_INDENT;
43         defskip = VSpace(VSpace::MEDSKIP);
44         quotes_language = InsetQuotes::EnglishQ;
45         quotes_times = InsetQuotes::DoubleQ;
46         fontsize = "default";
47
48         /*  PaperLayout */
49         papersize = PAPER_DEFAULT;
50         papersize2 = VM_PAPER_DEFAULT; /* DEFAULT */
51         paperpackage = PACKAGE_NONE;
52         orientation = ORIENTATION_PORTRAIT;
53         use_geometry = false;
54         use_amsmath = false;
55         use_natbib = false;
56         use_numerical_citations = false;
57         tracking_changes = false;
58         secnumdepth = 3;
59         tocdepth = 3;
60         language = default_language;
61         fonts = "default";
62         inputenc = "auto";
63         graphicsDriver = "default";
64         sides = LyXTextClass::OneSide;
65         columns = 1;
66         pagestyle = "default";
67         for (int iter = 0; iter < 4; ++iter) {
68                 user_defined_bullets[iter] = ITEMIZE_DEFAULTS[iter];
69                 temp_bullets[iter] = ITEMIZE_DEFAULTS[iter];
70         }
71 }
72
73
74 void BufferParams::writeFile(ostream & os) const
75 {
76         // The top of the file is written by the buffer.
77         // Prints out the buffer info into the .lyx file given by file
78
79         // the textclass
80         os << "\\textclass " << textclasslist[textclass].name() << '\n';
81
82         // then the the preamble
83         if (!preamble.empty()) {
84                 // remove '\n' from the end of preamble
85                 string const tmppreamble = rtrim(preamble, "\n");
86                 os << "\\begin_preamble\n"
87                    << tmppreamble
88                    << "\n\\end_preamble\n";
89         }
90
91         /* the options */
92         if (!options.empty()) {
93                 os << "\\options " << options << '\n';
94         }
95
96         /* then the text parameters */
97         if (language != ignore_language)
98                 os << "\\language " << language->lang() << '\n';
99         os << "\\inputencoding " << inputenc
100            << "\n\\fontscheme " << fonts
101            << "\n\\graphics " << graphicsDriver << '\n';
102
103         if (!float_placement.empty()) {
104                 os << "\\float_placement " << float_placement << '\n';
105         }
106         os << "\\paperfontsize " << fontsize << '\n';
107
108         spacing.writeFile(os);
109
110         os << "\\papersize " << string_papersize[papersize2]
111            << "\n\\paperpackage " << string_paperpackages[paperpackage]
112            << "\n\\use_geometry " << use_geometry
113            << "\n\\use_amsmath " << use_amsmath
114            << "\n\\use_natbib " << use_natbib
115            << "\n\\use_numerical_citations " << use_numerical_citations
116            << "\n\\paperorientation " << string_orientation[orientation]
117            << '\n';
118         if (!paperwidth.empty())
119                 os << "\\paperwidth "
120                    << VSpace(paperwidth).asLyXCommand() << '\n';
121         if (!paperheight.empty())
122                 os << "\\paperheight "
123                    << VSpace(paperheight).asLyXCommand() << '\n';
124         if (!leftmargin.empty())
125                 os << "\\leftmargin "
126                    << VSpace(leftmargin).asLyXCommand() << '\n';
127         if (!topmargin.empty())
128                 os << "\\topmargin "
129                    << VSpace(topmargin).asLyXCommand() << '\n';
130         if (!rightmargin.empty())
131                 os << "\\rightmargin "
132                    << VSpace(rightmargin).asLyXCommand() << '\n';
133         if (!bottommargin.empty())
134                 os << "\\bottommargin "
135                    << VSpace(bottommargin).asLyXCommand() << '\n';
136         if (!headheight.empty())
137                 os << "\\headheight "
138                    << VSpace(headheight).asLyXCommand() << '\n';
139         if (!headsep.empty())
140                 os << "\\headsep "
141                    << VSpace(headsep).asLyXCommand() << '\n';
142         if (!footskip.empty())
143                 os << "\\footskip "
144                    << VSpace(footskip).asLyXCommand() << '\n';
145         os << "\\secnumdepth " << secnumdepth
146            << "\n\\tocdepth " << tocdepth
147            << "\n\\paragraph_separation "
148            << string_paragraph_separation[paragraph_separation]
149            << "\n\\defskip " << defskip.asLyXCommand()
150            << "\n\\quotes_language "
151            << string_quotes_language[quotes_language] << '\n';
152         switch (quotes_times) {
153                 // An output operator for insetquotes would be nice
154         case InsetQuotes::SingleQ:
155                 os << "\\quotes_times 1\n"; break;
156         case InsetQuotes::DoubleQ:
157                 os << "\\quotes_times 2\n"; break;
158         }
159         os << "\\papercolumns " << columns
160            << "\n\\papersides " << sides
161            << "\n\\paperpagestyle " << pagestyle << '\n';
162         for (int i = 0; i < 4; ++i) {
163                 if (user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
164                         if (user_defined_bullets[i].getFont() != -1) {
165                                 os << "\\bullet " << i
166                                    << "\n\t"
167                                    << user_defined_bullets[i].getFont()
168                                    << "\n\t"
169                                    << user_defined_bullets[i].getCharacter()
170                                    << "\n\t"
171                                    << user_defined_bullets[i].getSize()
172                                    << "\n\\end_bullet\n";
173                         }
174                         else {
175                                 os << "\\bulletLaTeX " << i
176                                    << "\n\t\""
177                                    << user_defined_bullets[i].getText()
178                                    << "\"\n\\end_bullet\n";
179                         }
180                 }
181         }
182
183         os << "\\tracking_changes " << tracking_changes << "\n";
184 }
185
186
187 void BufferParams::setPaperStuff()
188 {
189         papersize = PAPER_DEFAULT;
190         char const c1 = paperpackage;
191         if (c1 == PACKAGE_NONE) {
192                 char const c2 = papersize2;
193                 if (c2 == VM_PAPER_USLETTER)
194                         papersize = PAPER_USLETTER;
195                 else if (c2 == VM_PAPER_USLEGAL)
196                         papersize = PAPER_LEGALPAPER;
197                 else if (c2 == VM_PAPER_USEXECUTIVE)
198                         papersize = PAPER_EXECUTIVEPAPER;
199                 else if (c2 == VM_PAPER_A3)
200                         papersize = PAPER_A3PAPER;
201                 else if (c2 == VM_PAPER_A4)
202                         papersize = PAPER_A4PAPER;
203                 else if (c2 == VM_PAPER_A5)
204                         papersize = PAPER_A5PAPER;
205                 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
206                          (c2 == VM_PAPER_B5))
207                         papersize = PAPER_B5PAPER;
208         } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
209                    (c1 == PACKAGE_WIDEMARGINSA4))
210                 papersize = PAPER_A4PAPER;
211 }
212
213
214 void BufferParams::useClassDefaults()
215 {
216         LyXTextClass const & tclass = textclasslist[textclass];
217
218         sides = tclass.sides();
219         columns = tclass.columns();
220         pagestyle = tclass.pagestyle();
221         options = tclass.options();
222         secnumdepth = tclass.secnumdepth();
223         tocdepth = tclass.tocdepth();
224 }
225
226
227 bool BufferParams::hasClassDefaults() const
228 {
229         LyXTextClass const & tclass = textclasslist[textclass];
230
231         return (sides == tclass.sides()
232                 && columns == tclass.columns()
233                 && pagestyle == tclass.pagestyle()
234                 && options == tclass.options()
235                 && secnumdepth == tclass.secnumdepth()
236                 && tocdepth == tclass.tocdepth());
237 }
238
239
240 LyXTextClass const & BufferParams::getLyXTextClass() const
241 {
242         return textclasslist[textclass];
243 }
244
245
246 void BufferParams::readPreamble(LyXLex & lex)
247 {
248         if (lex.getString() != "\\begin_preamble")
249                 lyxerr << "Error (BufferParams::readPreamble):"
250                         "consistency check failed." << endl;
251
252         preamble = lex.getLongString("\\end_preamble");
253 }
254
255
256 void BufferParams::readLanguage(LyXLex & lex)
257 {
258         if (!lex.next()) return;
259
260         string const tmptok = lex.getString();
261
262         // check if tmptok is part of tex_babel in tex-defs.h
263         language = languages.getLanguage(tmptok);
264         if (!language) {
265                 // Language tmptok was not found
266                 language = default_language;
267                 lyxerr << "Warning: Setting language `"
268                        << tmptok << "' to `" << language->lang()
269                        << "'." << endl;
270         }
271 }
272
273
274 void BufferParams::readGraphicsDriver(LyXLex & lex)
275 {
276         if (!lex.next()) return;
277
278         string const tmptok = lex.getString();
279         // check if tmptok is part of tex_graphics in tex_defs.h
280         int n = 0;
281         while (true) {
282                 string const test = tex_graphics[n++];
283
284                 if (test == tmptok) {
285                         graphicsDriver = tmptok;
286                         break;
287                 } else if (test == "last_item") {
288                         lex.printError(
289                                 "Warning: graphics driver `$$Token' not recognized!\n"
290                                 "         Setting graphics driver to `default'.\n");
291                         graphicsDriver = "default";
292                         break;
293                 }
294         }
295 }