]> git.lyx.org Git - lyx.git/blob - src/buffer.C
some visual feedback for extra vertical space
[lyx.git] / src / buffer.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  *           This file is Copyright 1996-2001
10  *           Lars Gullik Bjønnes
11  *
12  * ====================================================== 
13  */
14
15 #include <config.h>
16
17 #include <fstream>
18 #include <iomanip>
19 #include <map>
20 #include <stack>
21 #include <list>
22
23 #include <cstdlib>
24 #include <cmath>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <utime.h>
28
29 #include <algorithm>
30
31 #ifdef HAVE_LOCALE
32 #include <locale>
33 #endif
34
35 #ifdef __GNUG__
36 #pragma implementation
37 #endif
38
39 #include "buffer.h"
40 #include "bufferlist.h"
41 #include "lyx_main.h"
42 #include "lyx_gui_misc.h"
43 #include "LyXAction.h"
44 #include "lyxrc.h"
45 #include "lyxlex.h"
46 #include "tex-strings.h"
47 #include "layout.h"
48 #include "bufferview_funcs.h"
49 #include "lyxfont.h"
50 #include "version.h"
51 #include "mathed/formulamacro.h"
52 #include "mathed/formula.h"
53 #include "insets/inset.h"
54 #include "insets/inseterror.h"
55 #include "insets/insetlabel.h"
56 #include "insets/insetref.h"
57 #include "insets/inseturl.h"
58 #include "insets/insetnote.h"
59 #include "insets/insetquotes.h"
60 #include "insets/insetlatexaccent.h"
61 #include "insets/insetbib.h" 
62 #include "insets/insetcite.h" 
63 #include "insets/insetexternal.h"
64 #include "insets/insetindex.h" 
65 #include "insets/insetinclude.h"
66 #include "insets/insettoc.h"
67 #include "insets/insetparent.h"
68 #include "insets/insetspecialchar.h"
69 #include "insets/figinset.h"
70 #include "insets/insettext.h"
71 #include "insets/insetert.h"
72 #include "insets/insetgraphics.h"
73 #include "insets/insetfoot.h"
74 #include "insets/insetmarginal.h"
75 #include "insets/insetminipage.h"
76 #include "insets/insetfloat.h"
77 #include "insets/insettabular.h"
78 #if 0
79 #include "insets/insettheorem.h"
80 #include "insets/insetlist.h"
81 #endif
82 #include "insets/insetcaption.h"
83 #include "insets/insetfloatlist.h"
84 #include "support/textutils.h"
85 #include "support/filetools.h"
86 #include "support/path.h"
87 #include "support/os.h"
88 #include "LaTeX.h"
89 #include "Chktex.h"
90 #include "LyXView.h"
91 #include "debug.h"
92 #include "LaTeXFeatures.h"
93 #include "support/syscall.h"
94 #include "support/lyxlib.h"
95 #include "support/FileInfo.h"
96 #include "support/lyxmanip.h"
97 #include "lyxtext.h"
98 #include "gettext.h"
99 #include "language.h"
100 #include "lyx_gui_misc.h"       // WarnReadonly()
101 #include "frontends/Dialogs.h"
102 #include "encoding.h"
103 #include "exporter.h"
104 #include "Lsstream.h"
105 #include "converter.h"
106 #include "BufferView.h"
107 #include "ParagraphParameters.h"
108
109 using std::ostream;
110 using std::ofstream;
111 using std::ifstream;
112 using std::fstream;
113 using std::ios;
114 using std::setw;
115 using std::endl;
116 using std::pair;
117 using std::make_pair;
118 using std::vector;
119 using std::map;
120 using std::max;
121 using std::set;
122 using std::stack;
123 using std::list;
124
125 // all these externs should eventually be removed.
126 extern BufferList bufferlist;
127
128 extern LyXAction lyxaction;
129
130 namespace {
131
132 const int LYX_FORMAT = 220;
133
134 } // namespace anon
135
136 extern int tex_code_break_column;
137
138
139 Buffer::Buffer(string const & file, bool ronly)
140 {
141         lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
142         filename = file;
143         filepath = OnlyPath(file);
144         paragraph = 0;
145         lyx_clean = true;
146         bak_clean = true;
147         dep_clean = 0;
148         read_only = ronly;
149         unnamed = false;
150         users = 0;
151         lyxvc.buffer(this);
152         if (read_only || (lyxrc.use_tempdir)) {
153                 tmppath = CreateBufferTmpDir();
154         } else tmppath.erase();
155 }
156
157
158 Buffer::~Buffer()
159 {
160         lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
161         // here the buffer should take care that it is
162         // saved properly, before it goes into the void.
163
164         // make sure that views using this buffer
165         // forgets it.
166         if (users)
167                 users->buffer(0);
168         
169         if (!tmppath.empty()) {
170                 DestroyBufferTmpDir(tmppath);
171         }
172         
173         Paragraph * par = paragraph;
174         Paragraph * tmppar;
175         while (par) {
176                 tmppar = par->next();
177                 delete par;
178                 par = tmppar;
179         }
180         paragraph = 0;
181 }
182
183
184 string const Buffer::getLatexName(bool no_path) const
185 {
186         string name = ChangeExtension(MakeLatexName(filename), ".tex");
187         if (no_path)
188                 return OnlyFilename(name);
189         else
190                 return name;
191 }
192
193
194 pair<Buffer::LogType, string> const Buffer::getLogName(void) const
195 {
196         string const filename = getLatexName(false);
197
198         if (filename.empty())
199                 return make_pair(Buffer::latexlog, string());
200
201         string path = OnlyPath(filename);
202
203         if (lyxrc.use_tempdir || !IsDirWriteable(path))
204                 path = tmppath;
205
206         string const fname = AddName(path,
207                                      OnlyFilename(ChangeExtension(filename,
208                                                                   ".log")));
209         string const bname =
210                 AddName(path, OnlyFilename(
211                         ChangeExtension(filename,
212                                         formats.extension("literate") + ".out")));
213
214         // If no Latex log or Build log is newer, show Build log
215
216         FileInfo const f_fi(fname);
217         FileInfo const b_fi(bname);
218
219         if (b_fi.exist() &&
220             (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
221                 lyxerr[Debug::FILES] << "Log name calculated as : " << bname << endl;
222                 return make_pair(Buffer::buildlog, bname);
223         }
224         lyxerr[Debug::FILES] << "Log name calculated as : " << fname << endl;
225         return make_pair(Buffer::latexlog, fname);
226 }
227
228
229 void Buffer::setReadonly(bool flag)
230 {
231         if (read_only != flag) {
232                 read_only = flag; 
233                 updateTitles();
234                 users->owner()->getDialogs()->updateBufferDependent(false);
235         }
236         if (read_only) {
237                 WarnReadonly(filename);
238         }
239 }
240
241
242 bool Buffer::saveParamsAsDefaults() // const
243 {
244         string const fname = AddName(AddPath(user_lyxdir, "templates/"),
245                                "defaults.lyx");
246         Buffer defaults = Buffer(fname);
247         
248         // Use the current buffer's parameters as default
249         defaults.params = params;
250         
251         // add an empty paragraph. Is this enough?
252         defaults.paragraph = new Paragraph;
253
254         return defaults.writeFile(defaults.filename, false);
255 }
256
257
258 /// Update window titles of all users
259 // Should work on a list
260 void Buffer::updateTitles() const
261 {
262         if (users) users->owner()->updateWindowTitle();
263 }
264
265
266 /// Reset autosave timer of all users
267 // Should work on a list
268 void Buffer::resetAutosaveTimers() const
269 {
270         if (users) users->owner()->resetAutosaveTimer();
271 }
272
273
274 void Buffer::setFileName(string const & newfile)
275 {
276         filename = MakeAbsPath(newfile);
277         filepath = OnlyPath(filename);
278         setReadonly(IsFileWriteable(filename) == 0);
279         updateTitles();
280 }
281
282
283 // We'll remove this later. (Lgb)
284 namespace {
285
286 string last_inset_read;
287
288 #ifndef NO_COMPABILITY
289 struct ErtComp 
290 {
291         ErtComp() : active(false), in_tabular(false) {
292         }
293         string contents;
294         bool active;
295         bool in_tabular;
296 };
297
298 std::stack<ErtComp> ert_stack;
299 ErtComp ert_comp;
300 #endif
301
302 } // anon
303
304
305 #warning And _why_ is this here? (Lgb)
306 int unknown_layouts;
307
308 // candidate for move to BufferView
309 // (at least some parts in the beginning of the func)
310 //
311 // Uwe C. Schroeder
312 // changed to be public and have one parameter
313 // if par = 0 normal behavior
314 // else insert behavior
315 // Returns false if "\the_end" is not read for formats >= 2.13. (Asger)
316 bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
317 {
318         unknown_layouts = 0;
319 #ifndef NO_COMPABILITY
320         ert_comp.contents.erase();
321         ert_comp.active = false;
322         ert_comp.in_tabular = false;
323 #endif
324         int pos = 0;
325         Paragraph::depth_type depth = 0; 
326         bool the_end_read = false;
327
328         Paragraph * first_par = 0;
329         LyXFont font(LyXFont::ALL_INHERIT, params.language);
330         if (file_format < 216 && params.language->lang() == "hebrew")
331                 font.setLanguage(default_language);
332
333         if (!par) {
334                 par = new Paragraph;
335         } else {
336                 // We are inserting into an existing document
337                 users->text->breakParagraph(users);
338                 first_par = users->text->firstParagraph();
339                 pos = 0;
340                 markDirty();
341                 // We don't want to adopt the parameters from the
342                 // document we insert, so we skip until the text begins:
343                 while (lex.isOK()) {
344                         lex.nextToken();
345                         string const pretoken = lex.getString();
346                         if (pretoken == "\\layout") {
347                                 lex.pushToken(pretoken);
348                                 break;
349                         }
350                 }
351         }
352
353         while (lex.isOK()) {
354                 lex.nextToken();
355                 string const token = lex.getString();
356
357                 if (token.empty()) continue;
358
359                 lyxerr[Debug::PARSER] << "Handling token: `"
360                                       << token << "'" << endl;
361                 
362                 the_end_read =
363                         parseSingleLyXformat2Token(lex, par, first_par,
364                                                    token, pos, depth,
365                                                    font);
366         }
367    
368         if (!first_par)
369                 first_par = par;
370
371         paragraph = first_par;
372
373         if (unknown_layouts > 0) {
374                 string s = _("Couldn't set the layout for ");
375                 if (unknown_layouts == 1) {
376                         s += _("one paragraph");
377                 } else {
378                         s += tostr(unknown_layouts);
379                         s += _(" paragraphs");
380                 }
381                 WriteAlert(_("Textclass Loading Error!"), s,
382                            _("When reading " + fileName()));
383         }       
384
385         return the_end_read;
386 }
387
388
389 #ifndef NO_COMPABILITY
390 void Buffer::insertErtContents(Paragraph * par, int & pos,
391                                LyXFont const & font, bool set_inactive) 
392 {
393         if (!ert_comp.contents.empty()) {
394                 lyxerr[Debug::INSETS] << "ERT contents:\n"
395                        << ert_comp.contents << endl;
396                 Inset * inset = new InsetERT(ert_comp.contents, true);
397                 par->insertInset(pos++, inset, font);
398                 ert_comp.contents.erase();
399         }
400         if (set_inactive) {
401                 ert_comp.active = false;
402         }
403 }
404 #endif
405
406
407 bool
408 Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
409                                    Paragraph *& first_par,
410                                    string const & token, int & pos,
411                                    Paragraph::depth_type & depth, 
412                                    LyXFont & font
413         )
414 {
415         bool the_end_read = false;
416 #ifndef NO_COMPABILITY
417 #ifndef NO_PEXTRA_REALLY
418         // This is super temporary but is needed to get the compability
419         // mode for minipages work correctly together with new tabulars.
420         static int call_depth;
421         ++call_depth;
422         bool checkminipage = false;
423         static Paragraph * minipar;
424         static Paragraph * parBeforeMinipage;
425 #endif
426 #endif
427         if (token[0] != '\\') {
428 #ifndef NO_COMPABILITY
429                 if (ert_comp.active) {
430                         ert_comp.contents += token;
431                 } else {
432 #endif
433                 for (string::const_iterator cit = token.begin();
434                      cit != token.end(); ++cit) {
435                         par->insertChar(pos, (*cit), font);
436                         ++pos;
437                 }
438 #ifndef NO_COMPABILITY
439                 }
440 #endif
441         } else if (token == "\\i") {
442                 Inset * inset = new InsetLatexAccent;
443                 inset->read(this, lex);
444                 par->insertInset(pos, inset, font);
445                 ++pos;
446         } else if (token == "\\layout") {
447 #ifndef NO_COMPABILITY
448                 ert_comp.in_tabular = false;
449                 // Do the insetert.
450                 insertErtContents(par, pos, font);
451 #endif
452                 lex.eatLine();
453                 string const layoutname = lex.getString();
454                 pair<bool, LyXTextClass::LayoutList::size_type> pp
455                         = textclasslist.NumberOfLayout(params.textclass,
456                                                        layoutname);
457
458 #ifndef NO_COMPABILITY
459                 if (compare_no_case(layoutname, "latex") == 0) {
460                         ert_comp.active = true;
461                 }
462 #endif
463 #ifdef USE_CAPTION
464                 // The is the compability reading of layout caption.
465                 // It can be removed in LyX version 1.3.0. (Lgb)
466                 if (compare_no_case(layoutname, "caption") == 0) {
467                         // We expect that the par we are now working on is
468                         // really inside a InsetText inside a InsetFloat.
469                         // We also know that captions can only be
470                         // one paragraph. (Lgb)
471                         
472                         // We should now read until the next "\layout"
473                         // is reached.
474                         // This is probably not good enough, what if the
475                         // caption is the last par in the document (Lgb)
476                         istream & ist = lex.getStream();
477                         stringstream ss;
478                         string line;
479                         int begin = 0;
480                         while (true) {
481                                 getline(ist, line);
482                                 if (prefixIs(line, "\\layout")) {
483                                         lex.pushToken(line);
484                                         break;
485                                 }
486                                 if (prefixIs(line, "\\begin_inset"))
487                                         ++begin;
488                                 if (prefixIs(line, "\\end_inset")) {
489                                         if (begin)
490                                                 --begin;
491                                         else {
492                                                 lex.pushToken(line);
493                                                 break;
494                                         }
495                                 }
496                                 
497                                 ss << line << '\n';
498                         }
499                         // Now we should have the whole layout in ss
500                         // we should now be able to give this to the
501                         // caption inset.
502                         ss << "\\end_inset\n";
503                         
504                         // This seems like a bug in stringstream.
505                         // We really should be able to use ss
506                         // directly. (Lgb)
507                         istringstream is(ss.str());
508                         LyXLex tmplex(0, 0);
509                         tmplex.setStream(is);
510                         Inset * inset = new InsetCaption;
511                         inset->Read(this, tmplex);
512                         par->InsertInset(pos, inset, font);
513                         ++pos;
514                 } else {
515 #endif
516                         if (!first_par)
517                                 first_par = par;
518                         else {
519                                 par = new Paragraph(par);
520                         }
521                         pos = 0;
522                         if (pp.first) {
523                                 par->layout = pp.second;
524                         } else {
525                                 // layout not found
526                                 // use default layout "Standard" (0)
527                                 par->layout = 0;
528                                 ++unknown_layouts;
529                                 string const s = _("Layout had to be changed from\n")
530                                         + layoutname + _(" to ")
531                                         + textclasslist.NameOfLayout(params.textclass, par->layout);
532                                 InsetError * new_inset = new InsetError(s);
533                                 par->insertInset(0, new_inset);
534                         }
535                         // Test whether the layout is obsolete.
536                         LyXLayout const & layout =
537                                 textclasslist.Style(params.textclass,
538                                                     par->layout);
539                         if (!layout.obsoleted_by().empty())
540                                 par->layout = textclasslist
541                                         .NumberOfLayout(params.textclass,
542                                                         layout.obsoleted_by())
543                                         .second;
544                         par->params().depth(depth);
545                         font = LyXFont(LyXFont::ALL_INHERIT, params.language);
546                         if (file_format < 216
547                             && params.language->lang() == "hebrew")
548                                 font.setLanguage(default_language);
549 #if USE_CAPTION
550                 }
551 #endif
552
553 #ifndef NO_COMPABILITY
554         } else if (token == "\\begin_float") {
555                 insertErtContents(par, pos, font);
556                 //insertErtContents(par, pos, font, false);
557                 //ert_stack.push(ert_comp);
558                 //ert_comp = ErtComp();
559                 
560                 // This is the compability reader. It can be removed in
561                 // LyX version 1.3.0. (Lgb)
562                 lex.next();
563                 string const tmptok = lex.getString();
564                 //lyxerr << "old float: " << tmptok << endl;
565                 
566                 Inset * inset = 0;
567                 stringstream old_float;
568                 
569                 if (tmptok == "footnote") {
570                         inset = new InsetFoot;
571                 } else if (tmptok == "margin") {
572                         inset = new InsetMarginal;
573                 } else if (tmptok == "fig") {
574                         inset = new InsetFloat("figure");
575                         old_float << "placement htbp\n"
576                                   << "wide false\n";
577                 } else if (tmptok == "tab") {
578                         inset = new InsetFloat("table");
579                         old_float << "placement htbp\n"
580                                   << "wide false\n";
581                 } else if (tmptok == "alg") {
582                         inset = new InsetFloat("algorithm");
583                         old_float << "placement htbp\n"
584                                   << "wide false\n";
585                 } else if (tmptok == "wide-fig") {
586                         inset = new InsetFloat("figure");
587                         //InsetFloat * tmp = new InsetFloat("figure");
588                         //tmp->wide(true);
589                         //inset = tmp;
590                         old_float << "placement htbp\n"
591                                   << "wide true\n";
592                 } else if (tmptok == "wide-tab") {
593                         inset = new InsetFloat("table");
594                         //InsetFloat * tmp = new InsetFloat("table");
595                         //tmp->wide(true);
596                         //inset = tmp;
597                         old_float << "placement htbp\n"
598                                   << "wide true\n";
599                 }
600
601                 if (!inset) {
602 #ifndef NO_PEXTRA_REALLY
603                         --call_depth;
604 #endif
605                         return false; // no end read yet
606                 }
607                 
608                 old_float << "collapsed true\n";
609
610                 // Here we need to check for \end_deeper and handle that
611                 // before we do the footnote parsing.
612                 // This _is_ a hack! (Lgb)
613                 while (true) {
614                         lex.next();
615                         string const tmp = lex.getString();
616                         if (tmp == "\\end_deeper") {
617                                 //lyxerr << "\\end_deeper caught!" << endl;
618                                 if (!depth) {
619                                         lex.printError("\\end_deeper: "
620                                                        "depth is already null");
621                                 } else
622                                         --depth;
623                                 
624                         } else {
625                                 old_float << tmp << ' ';
626                                 break;
627                         }
628                 }
629                 
630                 old_float << lex.getLongString("\\end_float")
631                           << "\n\\end_inset\n";
632                 //lyxerr << "Float Body:\n" << old_float.str() << endl;
633                 // That this does not work seems like a bug
634                 // in stringstream. (Lgb)
635                 istringstream istr(old_float.str());
636                 LyXLex nylex(0, 0);
637                 nylex.setStream(istr);
638                 inset->read(this, nylex);
639                 par->insertInset(pos, inset, font);
640                 ++pos;
641                 insertErtContents(par, pos, font);
642 #endif
643         } else if (token == "\\begin_deeper") {
644                 ++depth;
645         } else if (token == "\\end_deeper") {
646                 if (!depth) {
647                         lex.printError("\\end_deeper: "
648                                        "depth is already null");
649                 }
650                 else
651                         --depth;
652         } else if (token == "\\begin_preamble") {
653                 params.readPreamble(lex);
654         } else if (token == "\\textclass") {
655                 lex.eatLine();
656                 pair<bool, LyXTextClassList::size_type> pp = 
657                         textclasslist.NumberOfClass(lex.getString());
658                 if (pp.first) {
659                         params.textclass = pp.second;
660                 } else {
661                         WriteAlert(string(_("Textclass error")), 
662                                 string(_("The document uses an unknown textclass \"")) + 
663                                 lex.getString() + string("\"."),
664                                 string(_("LyX will not be able to produce output correctly.")));
665                         params.textclass = 0;
666                 }
667                 if (!textclasslist.Load(params.textclass)) {
668                                 // if the textclass wasn't loaded properly
669                                 // we need to either substitute another
670                                 // or stop loading the file.
671                                 // I can substitute but I don't see how I can
672                                 // stop loading... ideas??  ARRae980418
673                         WriteAlert(_("Textclass Loading Error!"),
674                                    string(_("Can't load textclass ")) +
675                                    textclasslist.NameOfClass(params.textclass),
676                                    _("-- substituting default"));
677                         params.textclass = 0;
678                 }
679         } else if (token == "\\options") {
680                 lex.eatLine();
681                 params.options = lex.getString();
682         } else if (token == "\\language") {
683                 params.readLanguage(lex);    
684         } else if (token == "\\fontencoding") {
685                 lex.eatLine();
686         } else if (token == "\\inputencoding") {
687                 lex.eatLine();
688                 params.inputenc = lex.getString();
689         } else if (token == "\\graphics") {
690                 params.readGraphicsDriver(lex);
691         } else if (token == "\\fontscheme") {
692                 lex.eatLine();
693                 params.fonts = lex.getString();
694         } else if (token == "\\noindent") {
695                 par->params().noindent(true);
696         } else if (token == "\\fill_top") {
697                 par->params().spaceTop(VSpace(VSpace::VFILL));
698         } else if (token == "\\fill_bottom") {
699                 par->params().spaceBottom(VSpace(VSpace::VFILL));
700         } else if (token == "\\line_top") {
701                 par->params().lineTop(true);
702         } else if (token == "\\line_bottom") {
703                 par->params().lineBottom(true);
704         } else if (token == "\\pagebreak_top") {
705                 par->params().pagebreakTop(true);
706         } else if (token == "\\pagebreak_bottom") {
707                 par->params().pagebreakBottom(true);
708         } else if (token == "\\start_of_appendix") {
709                 par->params().startOfAppendix(true);
710         } else if (token == "\\paragraph_separation") {
711                 int tmpret = lex.findToken(string_paragraph_separation);
712                 if (tmpret == -1) ++tmpret;
713                 if (tmpret != LYX_LAYOUT_DEFAULT) 
714                         params.paragraph_separation =
715                                 static_cast<BufferParams::PARSEP>(tmpret);
716         } else if (token == "\\defskip") {
717                 lex.nextToken();
718                 params.defskip = VSpace(lex.getString());
719         } else if (token == "\\epsfig") { // obsolete
720                 // Indeed it is obsolete, but we HAVE to be backwards
721                 // compatible until 0.14, because otherwise all figures
722                 // in existing documents are irretrivably lost. (Asger)
723                 params.readGraphicsDriver(lex);
724         } else if (token == "\\quotes_language") {
725                 int tmpret = lex.findToken(string_quotes_language);
726                 if (tmpret == -1) ++tmpret;
727                 if (tmpret != LYX_LAYOUT_DEFAULT) {
728                         InsetQuotes::quote_language tmpl = 
729                                 InsetQuotes::EnglishQ;
730                         switch (tmpret) {
731                         case 0:
732                                 tmpl = InsetQuotes::EnglishQ;
733                                 break;
734                         case 1:
735                                 tmpl = InsetQuotes::SwedishQ;
736                                 break;
737                         case 2:
738                                 tmpl = InsetQuotes::GermanQ;
739                                 break;
740                         case 3:
741                                 tmpl = InsetQuotes::PolishQ;
742                                 break;
743                         case 4:
744                                 tmpl = InsetQuotes::FrenchQ;
745                                 break;
746                         case 5:
747                                 tmpl = InsetQuotes::DanishQ;
748                                 break;  
749                         }
750                         params.quotes_language = tmpl;
751                 }
752         } else if (token == "\\quotes_times") {
753                 lex.nextToken();
754                 switch (lex.getInteger()) {
755                 case 1: 
756                         params.quotes_times = InsetQuotes::SingleQ; 
757                         break;
758                 case 2: 
759                         params.quotes_times = InsetQuotes::DoubleQ; 
760                         break;
761                 }
762         } else if (token == "\\papersize") {
763                 int tmpret = lex.findToken(string_papersize);
764                 if (tmpret == -1)
765                         ++tmpret;
766                 else
767                         params.papersize2 = tmpret;
768         } else if (token == "\\paperpackage") {
769                 int tmpret = lex.findToken(string_paperpackages);
770                 if (tmpret == -1) {
771                         ++tmpret;
772                         params.paperpackage = BufferParams::PACKAGE_NONE;
773                 } else
774                         params.paperpackage = tmpret;
775         } else if (token == "\\use_geometry") {
776                 lex.nextToken();
777                 params.use_geometry = lex.getInteger();
778         } else if (token == "\\use_amsmath") {
779                 lex.nextToken();
780                 params.use_amsmath = lex.getInteger();
781         } else if (token == "\\use_natbib") {
782                 lex.nextToken();
783                 params.use_natbib = lex.getInteger();
784         } else if (token == "\\use_numerical_citations") {
785                 lex.nextToken();
786                 params.use_numerical_citations = lex.getInteger();
787         } else if (token == "\\paperorientation") {
788                 int tmpret = lex.findToken(string_orientation);
789                 if (tmpret == -1) ++tmpret;
790                 if (tmpret != LYX_LAYOUT_DEFAULT) 
791                         params.orientation = static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
792         } else if (token == "\\paperwidth") {
793                 lex.next();
794                 params.paperwidth = lex.getString();
795         } else if (token == "\\paperheight") {
796                 lex.next();
797                 params.paperheight = lex.getString();
798         } else if (token == "\\leftmargin") {
799                 lex.next();
800                 params.leftmargin = lex.getString();
801         } else if (token == "\\topmargin") {
802                 lex.next();
803                 params.topmargin = lex.getString();
804         } else if (token == "\\rightmargin") {
805                 lex.next();
806                 params.rightmargin = lex.getString();
807         } else if (token == "\\bottommargin") {
808                 lex.next();
809                 params.bottommargin = lex.getString();
810         } else if (token == "\\headheight") {
811                 lex.next();
812                 params.headheight = lex.getString();
813         } else if (token == "\\headsep") {
814                 lex.next();
815                 params.headsep = lex.getString();
816         } else if (token == "\\footskip") {
817                 lex.next();
818                 params.footskip = lex.getString();
819         } else if (token == "\\paperfontsize") {
820                 lex.nextToken();
821                 params.fontsize = strip(lex.getString());
822         } else if (token == "\\papercolumns") {
823                 lex.nextToken();
824                 params.columns = lex.getInteger();
825         } else if (token == "\\papersides") {
826                 lex.nextToken();
827                 switch (lex.getInteger()) {
828                 default:
829                 case 1: params.sides = LyXTextClass::OneSide; break;
830                 case 2: params.sides = LyXTextClass::TwoSides; break;
831                 }
832         } else if (token == "\\paperpagestyle") {
833                 lex.nextToken();
834                 params.pagestyle = strip(lex.getString());
835         } else if (token == "\\bullet") {
836                 lex.nextToken();
837                 int const index = lex.getInteger();
838                 lex.nextToken();
839                 int temp_int = lex.getInteger();
840                 params.user_defined_bullets[index].setFont(temp_int);
841                 params.temp_bullets[index].setFont(temp_int);
842                 lex.nextToken();
843                 temp_int = lex.getInteger();
844                 params.user_defined_bullets[index].setCharacter(temp_int);
845                 params.temp_bullets[index].setCharacter(temp_int);
846                 lex.nextToken();
847                 temp_int = lex.getInteger();
848                 params.user_defined_bullets[index].setSize(temp_int);
849                 params.temp_bullets[index].setSize(temp_int);
850                 lex.nextToken();
851                 string const temp_str = lex.getString();
852                 if (temp_str != "\\end_bullet") {
853                                 // this element isn't really necessary for
854                                 // parsing but is easier for humans
855                                 // to understand bullets. Put it back and
856                                 // set a debug message?
857                         lex.printError("\\end_bullet expected, got" + temp_str);
858                                 //how can I put it back?
859                 }
860         } else if (token == "\\bulletLaTeX") {
861                 lex.nextToken();
862                 int const index = lex.getInteger();
863                 lex.next();
864                 string temp_str = lex.getString();
865                 string sum_str;
866                 while (temp_str != "\\end_bullet") {
867                                 // this loop structure is needed when user
868                                 // enters an empty string since the first
869                                 // thing returned will be the \\end_bullet
870                                 // OR
871                                 // if the LaTeX entry has spaces. Each element
872                                 // therefore needs to be read in turn
873                         sum_str += temp_str;
874                         lex.next();
875                         temp_str = lex.getString();
876                 }
877                 params.user_defined_bullets[index].setText(sum_str);
878                 params.temp_bullets[index].setText(sum_str);
879         } else if (token == "\\secnumdepth") {
880                 lex.nextToken();
881                 params.secnumdepth = lex.getInteger();
882         } else if (token == "\\tocdepth") {
883                 lex.nextToken();
884                 params.tocdepth = lex.getInteger();
885         } else if (token == "\\spacing") {
886                 lex.next();
887                 string const tmp = strip(lex.getString());
888                 Spacing::Space tmp_space = Spacing::Default;
889                 float tmp_val = 0.0;
890                 if (tmp == "single") {
891                         tmp_space = Spacing::Single;
892                 } else if (tmp == "onehalf") {
893                         tmp_space = Spacing::Onehalf;
894                 } else if (tmp == "double") {
895                         tmp_space = Spacing::Double;
896                 } else if (tmp == "other") {
897                         lex.next();
898                         tmp_space = Spacing::Other;
899                         tmp_val = lex.getFloat();
900                 } else {
901                         lex.printError("Unknown spacing token: '$$Token'");
902                 }
903                 // Small hack so that files written with klyx will be
904                 // parsed correctly.
905                 if (first_par) {
906                         par->params().spacing(Spacing(tmp_space, tmp_val));
907                 } else {
908                         params.spacing.set(tmp_space, tmp_val);
909                 }
910         } else if (token == "\\paragraph_spacing") {
911                 lex.next();
912                 string const tmp = strip(lex.getString());
913                 if (tmp == "single") {
914                         par->params().spacing(Spacing(Spacing::Single));
915                 } else if (tmp == "onehalf") {
916                         par->params().spacing(Spacing(Spacing::Onehalf));
917                 } else if (tmp == "double") {
918                         par->params().spacing(Spacing(Spacing::Double));
919                 } else if (tmp == "other") {
920                         lex.next();
921                         par->params().spacing(Spacing(Spacing::Other,
922                                          lex.getFloat()));
923                 } else {
924                         lex.printError("Unknown spacing token: '$$Token'");
925                 }
926         } else if (token == "\\float_placement") {
927                 lex.nextToken();
928                 params.float_placement = lex.getString();
929         } else if (token == "\\family") { 
930                 lex.next();
931                 font.setLyXFamily(lex.getString());
932         } else if (token == "\\series") {
933                 lex.next();
934                 font.setLyXSeries(lex.getString());
935         } else if (token == "\\shape") {
936                 lex.next();
937                 font.setLyXShape(lex.getString());
938         } else if (token == "\\size") {
939                 lex.next();
940                 font.setLyXSize(lex.getString());
941 #ifndef NO_COMPABILITY
942         } else if (token == "\\latex") {
943                 lex.next();
944                 string const tok = lex.getString();
945                 if (tok == "no_latex") {
946                         // Do the insetert.
947                         insertErtContents(par, pos, font);
948                 } else if (tok == "latex") {
949                         ert_comp.active = true;
950                 } else if (tok == "default") {
951                         // Do the insetert.
952                         insertErtContents(par, pos, font);
953                 } else {
954                         lex.printError("Unknown LaTeX font flag "
955                                        "`$$Token'");
956                 }
957 #endif
958         } else if (token == "\\lang") {
959                 lex.next();
960                 string const tok = lex.getString();
961                 Language const * lang = languages.getLanguage(tok);
962                 if (lang) {
963                         font.setLanguage(lang);
964                 } else {
965                         font.setLanguage(params.language);
966                         lex.printError("Unknown language `$$Token'");
967                 }
968         } else if (token == "\\numeric") {
969                 lex.next();
970                 font.setNumber(font.setLyXMisc(lex.getString()));
971         } else if (token == "\\emph") {
972                 lex.next();
973                 font.setEmph(font.setLyXMisc(lex.getString()));
974         } else if (token == "\\bar") {
975                 lex.next();
976                 string const tok = lex.getString();
977                 // This is dirty, but gone with LyX3. (Asger)
978                 if (tok == "under")
979                         font.setUnderbar(LyXFont::ON);
980                 else if (tok == "no")
981                         font.setUnderbar(LyXFont::OFF);
982                 else if (tok == "default")
983                         font.setUnderbar(LyXFont::INHERIT);
984                 else
985                         lex.printError("Unknown bar font flag "
986                                        "`$$Token'");
987         } else if (token == "\\noun") {
988                 lex.next();
989                 font.setNoun(font.setLyXMisc(lex.getString()));
990         } else if (token == "\\color") {
991                 lex.next();
992                 font.setLyXColor(lex.getString());
993         } else if (token == "\\align") {
994                 int tmpret = lex.findToken(string_align);
995                 if (tmpret == -1) ++tmpret;
996                 if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ???
997                         int const tmpret2 = int(pow(2.0, tmpret));
998                         //lyxerr << "Tmpret2 = " << tmpret2 << endl;
999                         par->params().align(LyXAlignment(tmpret2));
1000                 }
1001         } else if (token == "\\added_space_top") {
1002                 lex.nextToken();
1003                 par->params().spaceTop(VSpace(lex.getString()));
1004         } else if (token == "\\added_space_bottom") {
1005                 lex.nextToken();
1006                 par->params().spaceBottom(VSpace(lex.getString()));
1007 #ifndef NO_COMPABILITY
1008 #ifndef NO_PEXTRA_REALLY
1009         } else if (token == "\\pextra_type") {
1010                 lex.nextToken();
1011                 par->params().pextraType(lex.getInteger());
1012         } else if (token == "\\pextra_width") {
1013                 lex.nextToken();
1014                 par->params().pextraWidth(lex.getString());
1015         } else if (token == "\\pextra_widthp") {
1016                 lex.nextToken();
1017                 par->params().pextraWidthp(lex.getString());
1018         } else if (token == "\\pextra_alignment") {
1019                 lex.nextToken();
1020                 par->params().pextraAlignment(lex.getInteger());
1021         } else if (token == "\\pextra_hfill") {
1022                 lex.nextToken();
1023                 par->params().pextraHfill(lex.getInteger());
1024         } else if (token == "\\pextra_start_minipage") {
1025                 lex.nextToken();
1026                 par->params().pextraStartMinipage(lex.getInteger());
1027 #endif
1028 #endif
1029         } else if (token == "\\labelwidthstring") {
1030                 lex.eatLine();
1031                 par->params().labelWidthString(lex.getString());
1032                 // do not delete this token, it is still needed!
1033         } else if (token == "\\end_inset") {
1034                 lyxerr << "Solitary \\end_inset. Missing \\begin_inset?.\n"
1035                        << "Last inset read was: " << last_inset_read
1036                        << endl;
1037                 // Simply ignore this. The insets do not have
1038                 // to read this.
1039                 // But insets should read it, it is a part of
1040                 // the inset isn't it? Lgb.
1041         } else if (token == "\\begin_inset") {
1042 #ifndef NO_COMPABILITY
1043                 insertErtContents(par, pos, font, false);
1044                 ert_stack.push(ert_comp);
1045                 ert_comp = ErtComp();
1046 #endif
1047                 readInset(lex, par, pos, font);
1048 #ifndef NO_COMPABILITY
1049                 ert_comp = ert_stack.top();
1050                 ert_stack.pop();
1051                 insertErtContents(par, pos, font);
1052 #endif
1053         } else if (token == "\\SpecialChar") {
1054                 LyXLayout const & layout =
1055                         textclasslist.Style(params.textclass, 
1056                                             par->getLayout());
1057
1058                 // Insets don't make sense in a free-spacing context! ---Kayvan
1059                 if (layout.free_spacing) {
1060                         if (lex.isOK()) {
1061                                 lex.next();
1062                                 string next_token = lex.getString();
1063                                 if (next_token == "\\-") {
1064                                         par->insertChar(pos, '-', font);
1065                                 } else if (next_token == "\\protected_separator"
1066                                         || next_token == "~") {
1067                                         par->insertChar(pos, ' ', font);
1068                                 } else {
1069                                         lex.printError("Token `$$Token' "
1070                                                        "is in free space "
1071                                                        "paragraph layout!");
1072                                         --pos;
1073                                 }
1074                         }
1075                 } else {
1076                         Inset * inset = new InsetSpecialChar;
1077                         inset->read(this, lex);
1078                         par->insertInset(pos, inset, font);
1079                 }
1080                 ++pos;
1081         } else if (token == "\\newline") {
1082 #ifndef NO_COMPABILITY
1083                 if (!ert_comp.in_tabular && ert_comp.active) {
1084                         ert_comp.contents += char(Paragraph::META_NEWLINE);
1085                 } else {
1086                         // Since we cannot know it this is only a regular
1087                         // newline or a tabular cell delimter we have to
1088                         // handle the ERT here.
1089                         insertErtContents(par, pos, font, false);
1090
1091                         par->insertChar(pos, Paragraph::META_NEWLINE, font);
1092                         ++pos;
1093                 }
1094 #else
1095                 par->insertChar(pos, Paragraph::META_NEWLINE, font);
1096                 ++pos;
1097 #endif
1098         } else if (token == "\\LyXTable") {
1099 #ifndef NO_COMPABILITY
1100                 ert_comp.in_tabular = true;
1101 #endif
1102                 Inset * inset = new InsetTabular(*this);
1103                 inset->read(this, lex);
1104                 par->insertInset(pos, inset, font);
1105                 ++pos;
1106         } else if (token == "\\hfill") {
1107                 par->insertChar(pos, Paragraph::META_HFILL, font);
1108                 ++pos;
1109         } else if (token == "\\protected_separator") { // obsolete
1110                 // This is a backward compability thingie. (Lgb)
1111                 // Remove it later some time...introduced with fileformat
1112                 // 2.16. (Lgb)
1113                 LyXLayout const & layout =
1114                         textclasslist.Style(params.textclass, 
1115                                             par->getLayout());
1116
1117                 if (layout.free_spacing) {
1118                         par->insertChar(pos, ' ', font);
1119                 } else {
1120                         Inset * inset = new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
1121                         par->insertInset(pos, inset, font);
1122                 }
1123                 ++pos;
1124         } else if (token == "\\bibitem") {  // ale970302
1125                 if (!par->bibkey) {
1126                         InsetCommandParams p("bibitem", "dummy");
1127                         par->bibkey = new InsetBibKey(p);
1128                 }
1129                 par->bibkey->read(this, lex);                   
1130         } else if (token == "\\backslash") {
1131 #ifndef NO_COMPABILITY
1132                 if (ert_comp.active) {
1133                         ert_comp.contents += "\\";
1134                 } else {
1135 #endif
1136                 par->insertChar(pos, '\\', font);
1137                 ++pos;
1138 #ifndef NO_COMPABILITY
1139                 }
1140 #endif
1141         } else if (token == "\\the_end") {
1142 #ifndef NO_COMPABILITY
1143                 // If we still have some ert active here we have to insert
1144                 // it so we don't loose it. (Lgb)
1145                 insertErtContents(par, pos, font);
1146 #endif
1147                 the_end_read = true;
1148 #ifndef NO_COMPABILITY
1149 #ifndef NO_PEXTRA_REALLY
1150                 minipar = parBeforeMinipage = 0;
1151 #endif
1152 #endif
1153         } else {
1154 #ifndef NO_COMPABILITY
1155                 if (ert_comp.active) {
1156                         ert_comp.contents += token;
1157                 } else {
1158 #endif
1159                 // This should be insurance for the future: (Asger)
1160                 lex.printError("Unknown token `$$Token'. "
1161                                "Inserting as text.");
1162                 string::const_iterator cit = token.begin();
1163                 string::const_iterator end = token.end();
1164                 for (; cit != end; ++cit) {
1165                         par->insertChar(pos, (*cit), font);
1166                         ++pos;
1167                 }
1168 #ifndef NO_COMPABILITY
1169                 }
1170 #endif
1171         }
1172
1173 #ifndef NO_COMPABILITY
1174 #ifndef NO_PEXTRA_REALLY
1175         // I wonder if we could use this blanket fix for all the
1176         // checkminipage cases...
1177         if (par && par->size()) {
1178                 // It is possible that this will check to often,
1179                 // but that should not be an correctness issue.
1180                 // Only a speed issue.
1181                 checkminipage = true;
1182         }
1183         
1184         // now check if we have a minipage paragraph as at this
1185         // point we already read all the necessary data!
1186         // this cannot be done in layout because there we did
1187         // not read yet the paragraph PEXTRA-params (Jug)
1188         //
1189         // BEGIN pextra_minipage compability
1190         // This should be removed in 1.3.x (Lgb)
1191         
1192         // This compability code is not perfect. In a couple
1193         // of rand cases it fails. When the minipage par is
1194         // the first par in the document, and when there are
1195         // none or only one regular paragraphs after the
1196         // minipage. Currently I am not investing any effort
1197         // in fixing those cases.
1198
1199         //lyxerr << "Call depth: " << call_depth << endl;
1200         if (checkminipage && (call_depth == 1)) {
1201         checkminipage = false;
1202         if (minipar && (minipar != par) &&
1203             (par->params().pextraType()==Paragraph::PEXTRA_MINIPAGE))
1204         {
1205                 lyxerr << "minipages in a row" << endl;
1206                 if (par->params().pextraStartMinipage()) {
1207                         lyxerr << "start new minipage" << endl;
1208                         // minipages in a row
1209                         par->previous()->next(0);
1210                         par->previous(0);
1211                                 
1212                         Paragraph * tmp = minipar;
1213                         while (tmp) {
1214                                 tmp->params().pextraType(0);
1215                                 tmp->params().pextraWidth(string());
1216                                 tmp->params().pextraWidthp(string());
1217                                 tmp->params().pextraAlignment(0);
1218                                 tmp->params().pextraHfill(false);
1219                                 tmp->params().pextraStartMinipage(false);
1220                                 tmp = tmp->next();
1221                         }
1222                         // create a new paragraph to insert the
1223                         // minipages in the following case
1224                         if (par->params().pextraStartMinipage() &&
1225                             !par->params().pextraHfill())
1226                         {
1227                                 Paragraph * p = new Paragraph;
1228                                 p->layout = 0;
1229                                 p->previous(parBeforeMinipage);
1230                                 parBeforeMinipage->next(p);
1231                                 p->next(0);
1232                                 p->params().depth(parBeforeMinipage->params().depth());
1233                                 parBeforeMinipage = p;
1234                         }
1235                         InsetMinipage * mini = new InsetMinipage;
1236                         mini->pos(static_cast<InsetMinipage::Position>(par->params().pextraAlignment()));
1237                         mini->width(par->params().pextraWidth());
1238                         if (!par->params().pextraWidthp().empty()) {
1239                             lyxerr << "WP:" << mini->width() << endl;
1240                             mini->width(tostr(par->params().pextraWidthp())+"%");
1241                         }
1242                         mini->inset.paragraph(par);
1243                         // Insert the minipage last in the
1244                         // previous paragraph.
1245                         if (par->params().pextraHfill()) {
1246                                 parBeforeMinipage->insertChar
1247                                         (parBeforeMinipage->size(), Paragraph::META_HFILL);
1248                         }
1249                         parBeforeMinipage->insertInset
1250                                 (parBeforeMinipage->size(), mini);
1251                                 
1252                         minipar = par;
1253                 } else {
1254                         lyxerr << "new minipage par" << endl;
1255                         //nothing to do just continue reading
1256                 }
1257                         
1258         } else if (minipar && (minipar != par)) {
1259                 lyxerr << "last minipage par read" << endl;
1260                 // The last paragraph read was not part of a
1261                 // minipage but the par linked list is...
1262                 // So we need to remove the last par from the
1263                 // rest
1264                 if (par->previous())
1265                         par->previous()->next(0);
1266                 par->previous(parBeforeMinipage);
1267                 parBeforeMinipage->next(par);
1268                 Paragraph * tmp = minipar;
1269                 while (tmp) {
1270                         tmp->params().pextraType(0);
1271                         tmp->params().pextraWidth(string());
1272                         tmp->params().pextraWidthp(string());
1273                         tmp->params().pextraAlignment(0);
1274                         tmp->params().pextraHfill(false);
1275                         tmp->params().pextraStartMinipage(false);
1276                         tmp = tmp->next();
1277                 }
1278                 depth = parBeforeMinipage->params().depth();
1279                 minipar = parBeforeMinipage = 0;
1280         } else if (!minipar &&
1281                    (par->params().pextraType() == Paragraph::PEXTRA_MINIPAGE))
1282         {
1283                 // par is the first paragraph in a minipage
1284                 lyxerr << "begin minipage" << endl;
1285                 // To minimize problems for
1286                 // the users we will insert
1287                 // the first minipage in
1288                 // a sequence of minipages
1289                 // in its own paragraph.
1290                 Paragraph * p = new Paragraph;
1291                 p->layout = 0;
1292                 p->previous(par->previous());
1293                 p->next(0);
1294                 p->params().depth(depth);
1295                 par->params().depth(0);
1296                 depth = 0;
1297                 if (par->previous())
1298                         par->previous()->next(p);
1299                 par->previous(0);
1300                 parBeforeMinipage = p;
1301                 minipar = par;
1302                 if (!first_par || (first_par == par))
1303                         first_par = p;
1304
1305                 InsetMinipage * mini = new InsetMinipage;
1306                 mini->pos(static_cast<InsetMinipage::Position>(minipar->params().pextraAlignment()));
1307                 mini->width(minipar->params().pextraWidth());
1308                 if (!par->params().pextraWidthp().empty()) {
1309                     lyxerr << "WP:" << mini->width() << endl;
1310                     mini->width(tostr(par->params().pextraWidthp())+"%");
1311                 }
1312                 mini->inset.paragraph(minipar);
1313                         
1314                 // Insert the minipage last in the
1315                 // previous paragraph.
1316                 if (minipar->params().pextraHfill()) {
1317                         parBeforeMinipage->insertChar
1318                                 (parBeforeMinipage->size(),Paragraph::META_HFILL);
1319                 }
1320                 parBeforeMinipage->insertInset
1321                         (parBeforeMinipage->size(), mini);
1322         }
1323         }
1324         // End of pextra_minipage compability
1325         --call_depth;
1326 #endif
1327 #endif
1328         return the_end_read;
1329 }
1330
1331 // needed to insert the selection
1332 void Buffer::insertStringAsLines(Paragraph *& par, Paragraph::size_type & pos,
1333                                  LyXFont const & font, 
1334                                  string const & str) const
1335 {
1336         LyXLayout const & layout = textclasslist.Style(params.textclass, 
1337                                                      par->getLayout());
1338         // insert the string, don't insert doublespace
1339         bool space_inserted = true;
1340         for(string::const_iterator cit = str.begin(); 
1341             cit != str.end(); ++cit) {
1342                 if (*cit == '\n') {
1343                         if (par->size() || layout.keepempty) { 
1344                                 par->breakParagraph(params, pos, 
1345                                                     layout.isEnvironment());
1346                                 par = par->next();
1347                                 pos = 0;
1348                                 space_inserted = true;
1349                         } else {
1350                                 continue;
1351                         }
1352                         // do not insert consecutive spaces if !free_spacing
1353                 } else if ((*cit == ' ' || *cit == '\t')
1354                            && space_inserted && !layout.free_spacing) {
1355                         continue;
1356                 } else if (*cit == '\t') {
1357                         if (!layout.free_spacing) {
1358                                 // tabs are like spaces here
1359                                 par->insertChar(pos, ' ', font);
1360                                 ++pos;
1361                                 space_inserted = true;
1362                         } else {
1363                                 const Paragraph::value_type nb = 8 - pos % 8;
1364                                 for (Paragraph::size_type a = 0; 
1365                                      a < nb ; ++a) {
1366                                         par->insertChar(pos, ' ', font);
1367                                         ++pos;
1368                                 }
1369                                 space_inserted = true;
1370                         }
1371                 } else if (!IsPrintable(*cit)) {
1372                         // Ignore unprintables
1373                         continue;
1374                 } else {
1375                         // just insert the character
1376                         par->insertChar(pos, *cit, font);
1377                         ++pos;
1378                         space_inserted = (*cit == ' ');
1379                 }
1380
1381         }       
1382 }
1383
1384
1385 void Buffer::readInset(LyXLex & lex, Paragraph *& par,
1386                        int & pos, LyXFont & font)
1387 {
1388         // consistency check
1389         if (lex.getString() != "\\begin_inset") {
1390                 lyxerr << "Buffer::readInset: Consistency check failed."
1391                        << endl;
1392         }
1393         
1394         Inset * inset = 0;
1395
1396         lex.next();
1397         string const tmptok = lex.getString();
1398         last_inset_read = tmptok;
1399
1400         // test the different insets
1401         if (tmptok == "LatexCommand") {
1402                 InsetCommandParams inscmd;
1403                 inscmd.read(lex);
1404
1405                 string const cmdName = inscmd.getCmdName();
1406                 
1407                 // This strange command allows LyX to recognize "natbib" style
1408                 // citations: citet, citep, Citet etc.
1409                 if (compare_no_case(cmdName, "cite", 4) == 0) {
1410                         inset = new InsetCitation(inscmd);
1411                 } else if (cmdName == "bibitem") {
1412                         lex.printError("Wrong place for bibitem");
1413                         inset = new InsetBibKey(inscmd);
1414                 } else if (cmdName == "BibTeX") {
1415                         inset = new InsetBibtex(inscmd);
1416                 } else if (cmdName == "index") {
1417                         inset = new InsetIndex(inscmd);
1418                 } else if (cmdName == "include") {
1419                         inset = new InsetInclude(inscmd, *this);
1420                 } else if (cmdName == "label") {
1421                         inset = new InsetLabel(inscmd);
1422                 } else if (cmdName == "url"
1423                            || cmdName == "htmlurl") {
1424                         inset = new InsetUrl(inscmd);
1425                 } else if (cmdName == "ref"
1426                            || cmdName == "pageref"
1427                            || cmdName == "vref"
1428                            || cmdName == "vpageref"
1429                            || cmdName == "prettyref") {
1430                         if (!inscmd.getOptions().empty()
1431                             || !inscmd.getContents().empty()) {
1432                                 inset = new InsetRef(inscmd, *this);
1433                         }
1434                 } else if (cmdName == "tableofcontents") {
1435                         inset = new InsetTOC(inscmd);
1436                 } else if (cmdName == "listofalgorithms") {
1437                         inset = new InsetFloatList("algorithm");
1438                 } else if (cmdName == "listoffigures") {
1439                         inset = new InsetFloatList("figure");
1440                 } else if (cmdName == "listoftables") {
1441                         inset = new InsetFloatList("table");
1442                 } else if (cmdName == "printindex") {
1443                         inset = new InsetPrintIndex(inscmd);
1444                 } else if (cmdName == "lyxparent") {
1445                         inset = new InsetParent(inscmd, *this);
1446                 }
1447         } else {
1448                 bool alreadyread = false;
1449                 if (tmptok == "Quotes") {
1450                         inset = new InsetQuotes;
1451                 } else if (tmptok == "External") {
1452                         inset = new InsetExternal;
1453                 } else if (tmptok == "FormulaMacro") {
1454                         inset = new InsetFormulaMacro;
1455                 } else if (tmptok == "Formula") {
1456                         inset = new InsetFormula;
1457                 } else if (tmptok == "Figure") { // Backward compatibility
1458                         inset = new InsetFig(100, 100, *this);
1459                         //inset = new InsetGraphics;
1460                 } else if (tmptok == "Graphics") {
1461                         inset = new InsetGraphics;
1462                 } else if (tmptok == "Info") {// backwards compatibility
1463                         inset = new InsetNote(this,
1464                                               lex.getLongString("\\end_inset"),
1465                                               true);
1466                         alreadyread = true;
1467                 } else if (tmptok == "Note") {
1468                         inset = new InsetNote;
1469                 } else if (tmptok == "Include") {
1470                         InsetCommandParams p( "Include" );
1471                         inset = new InsetInclude(p, *this);
1472                 } else if (tmptok == "ERT") {
1473                         inset = new InsetERT;
1474                 } else if (tmptok == "Tabular") {
1475                         inset = new InsetTabular(*this);
1476                 } else if (tmptok == "Text") {
1477                         inset = new InsetText;
1478                 } else if (tmptok == "Foot") {
1479                         inset = new InsetFoot;
1480                 } else if (tmptok == "Marginal") {
1481                         inset = new InsetMarginal;
1482                 } else if (tmptok == "Minipage") {
1483                         inset = new InsetMinipage;
1484                 } else if (tmptok == "Float") {
1485                         lex.next();
1486                         string tmptok = lex.getString();
1487                         inset = new InsetFloat(tmptok);
1488 #if 0
1489                 } else if (tmptok == "List") {
1490                         inset = new InsetList;
1491                 } else if (tmptok == "Theorem") {
1492                         inset = new InsetList;
1493 #endif
1494                 } else if (tmptok == "Caption") {
1495                         inset = new InsetCaption;
1496                 } else if (tmptok == "FloatList") {
1497                         inset = new InsetFloatList;
1498                 }
1499                 
1500                 if (inset && !alreadyread) inset->read(this, lex);
1501         }
1502         
1503         if (inset) {
1504                 par->insertInset(pos, inset, font);
1505                 ++pos;
1506         }
1507 }
1508
1509
1510 bool Buffer::readFile(LyXLex & lex, Paragraph * par)
1511 {
1512         if (lex.isOK()) {
1513                 lex.next();
1514                 string const token(lex.getString());
1515                 if (token == "\\lyxformat") { // the first token _must_ be...
1516                         lex.eatLine();
1517                         string tmp_format = lex.getString();
1518                         //lyxerr << "LyX Format: `" << tmp_format << "'" << endl;
1519                         // if present remove ".," from string.
1520                         string::size_type dot = tmp_format.find_first_of(".,");
1521                         //lyxerr << "           dot found at " << dot << endl;
1522                         if (dot != string::npos)
1523                                 tmp_format.erase(dot, 1);
1524                         file_format = strToInt(tmp_format);
1525                         if (file_format == LYX_FORMAT) {
1526                                 // current format
1527                         } else if (file_format > LYX_FORMAT) {
1528                                 // future format
1529                                 WriteAlert(_("Warning!"),
1530                                            _("LyX file format is newer that what"),
1531                                            _("is supported in this LyX version. Expect some problems."));
1532                                 
1533                         } else if (file_format < LYX_FORMAT) {
1534                                 // old formats
1535                                 if (file_format < 200) {
1536                                         WriteAlert(_("ERROR!"),
1537                                                    _("Old LyX file format found. "
1538                                                      "Use LyX 0.10.x to read this!"));
1539                                         return false;
1540                                 }
1541                         }
1542                         bool the_end = readLyXformat2(lex, par);
1543                         setPaperStuff();
1544                         // the_end was added in 213
1545                         if (file_format < 213)
1546                                 the_end = true;
1547
1548                         if (!the_end)
1549                                 WriteAlert(_("Warning!"),
1550                                            _("Reading of document is not complete"),
1551                                            _("Maybe the document is truncated"));
1552                         return true;
1553                 } else { // "\\lyxformat" not found
1554                         WriteAlert(_("ERROR!"), _("Not a LyX file!"));
1555                 }
1556         } else
1557                 WriteAlert(_("ERROR!"), _("Unable to read file!"));
1558         return false;
1559 }
1560                     
1561
1562 // Should probably be moved to somewhere else: BufferView? LyXView?
1563 bool Buffer::save() const
1564 {
1565         // We don't need autosaves in the immediate future. (Asger)
1566         resetAutosaveTimers();
1567
1568         // make a backup
1569         string s;
1570         if (lyxrc.make_backup) {
1571                 s = fileName() + '~';
1572                 if (!lyxrc.backupdir_path.empty())
1573                         s = AddName(lyxrc.backupdir_path,
1574                                     subst(os::slashify_path(s),'/','!'));
1575
1576                 // Rename is the wrong way of making a backup,
1577                 // this is the correct way.
1578                 /* truss cp fil fil2:
1579                    lstat("LyXVC3.lyx", 0xEFFFF898)                 Err#2 ENOENT
1580                    stat("LyXVC.lyx", 0xEFFFF688)                   = 0
1581                    open("LyXVC.lyx", O_RDONLY)                     = 3
1582                    open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
1583                    fstat(4, 0xEFFFF508)                            = 0
1584                    fstat(3, 0xEFFFF508)                            = 0
1585                    read(3, " # T h i s   f i l e   w".., 8192)     = 5579
1586                    write(4, " # T h i s   f i l e   w".., 5579)    = 5579
1587                    read(3, 0xEFFFD4A0, 8192)                       = 0
1588                    close(4)                                        = 0
1589                    close(3)                                        = 0
1590                    chmod("LyXVC3.lyx", 0100644)                    = 0
1591                    lseek(0, 0, SEEK_CUR)                           = 46440
1592                    _exit(0)
1593                 */
1594
1595                 // Should probably have some more error checking here.
1596                 // Should be cleaned up in 0.13, at least a bit.
1597                 // Doing it this way, also makes the inodes stay the same.
1598                 // This is still not a very good solution, in particular we
1599                 // might loose the owner of the backup.
1600                 FileInfo finfo(fileName());
1601                 if (finfo.exist()) {
1602                         mode_t fmode = finfo.getMode();
1603                         struct utimbuf times = {
1604                                 finfo.getAccessTime(),
1605                                 finfo.getModificationTime() };
1606
1607                         ifstream ifs(fileName().c_str());
1608                         ofstream ofs(s.c_str(), ios::out|ios::trunc);
1609                         if (ifs && ofs) {
1610                                 ofs << ifs.rdbuf();
1611                                 ifs.close();
1612                                 ofs.close();
1613                                 ::chmod(s.c_str(), fmode);
1614                                 
1615                                 if (::utime(s.c_str(), &times)) {
1616                                         lyxerr << "utime error." << endl;
1617                                 }
1618                         } else {
1619                                 lyxerr << "LyX was not able to make "
1620                                         "backup copy. Beware." << endl;
1621                         }
1622                 }
1623         }
1624         
1625         if (writeFile(fileName(), false)) {
1626                 markLyxClean();
1627                 removeAutosaveFile(fileName());
1628         } else {
1629                 // Saving failed, so backup is not backup
1630                 if (lyxrc.make_backup) {
1631                         lyx::rename(s, fileName());
1632                 }
1633                 return false;
1634         }
1635         return true;
1636 }
1637
1638
1639 // Returns false if unsuccesful
1640 bool Buffer::writeFile(string const & fname, bool flag) const
1641 {
1642         // if flag is false writeFile will not create any GUI
1643         // warnings, only cerr.
1644         // Needed for autosave in background or panic save (Matthias 120496)
1645
1646         if (read_only && (fname == filename)) {
1647                 // Here we should come with a question if we should
1648                 // perform the write anyway.
1649                 if (flag)
1650                         lyxerr << _("Error! Document is read-only: ")
1651                                << fname << endl;
1652                 else
1653                         WriteAlert(_("Error! Document is read-only: "),
1654                                    fname);
1655                 return false;
1656         }
1657
1658         FileInfo finfo(fname);
1659         if (finfo.exist() && !finfo.writable()) {
1660                 // Here we should come with a question if we should
1661                 // try to do the save anyway. (i.e. do a chmod first)
1662                 if (flag)
1663                         lyxerr << _("Error! Cannot write file: ")
1664                                << fname << endl;
1665                 else
1666                         WriteFSAlert(_("Error! Cannot write file: "),
1667                                      fname);
1668                 return false;
1669         }
1670
1671         ofstream ofs(fname.c_str());
1672         if (!ofs) {
1673                 if (flag)
1674                         lyxerr << _("Error! Cannot open file: ")
1675                                << fname << endl;
1676                 else
1677                         WriteFSAlert(_("Error! Cannot open file: "),
1678                                      fname);
1679                 return false;
1680         }
1681
1682 #ifdef HAVE_LOCALE
1683         // Use the standard "C" locale for file output.
1684         ofs.imbue(std::locale::classic());
1685 #endif
1686
1687         // The top of the file should not be written by params.
1688
1689         // write out a comment in the top of the file
1690         ofs << '#' << LYX_DOCVERSION 
1691             << " created this file. For more info see http://www.lyx.org/\n"
1692             << "\\lyxformat " << LYX_FORMAT << "\n";
1693
1694         // now write out the buffer paramters.
1695         params.writeFile(ofs);
1696
1697         Paragraph::depth_type depth = 0;
1698
1699         // this will write out all the paragraphs
1700         // using recursive descent.
1701         paragraph->writeFile(this, ofs, params, depth);
1702
1703         // Write marker that shows file is complete
1704         ofs << "\n\\the_end" << endl;
1705
1706         ofs.close();
1707
1708         // how to check if close went ok?
1709         // Following is an attempt... (BE 20001011)
1710         
1711         // good() returns false if any error occured, including some
1712         //        formatting error.
1713         // bad()  returns true if something bad happened in the buffer,
1714         //        which should include file system full errors.
1715
1716         bool status = true;
1717         if (!ofs.good()) {
1718                 status = false;
1719 #if 0
1720                 if (ofs.bad()) {
1721                         lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
1722                 } else {
1723                         lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
1724                                << endl;
1725                 }
1726 #endif
1727         }
1728         
1729         return status;
1730 }
1731
1732
1733 string const Buffer::asciiParagraph(Paragraph const * par,
1734                                     unsigned int linelen) const
1735 {
1736         ostringstream buffer;
1737         Paragraph::depth_type depth = 0;
1738         int ltype = 0;
1739         Paragraph::depth_type ltype_depth = 0;
1740         unsigned int currlinelen = 0;
1741         bool ref_printed = false;
1742
1743         int noparbreak = 0;
1744         int islatex = 0;
1745         if (!par->previous()) {
1746                 // begins or ends a deeper area ?
1747                 if (depth != par->params().depth()) {
1748                         if (par->params().depth() > depth) {
1749                                 while (par->params().depth() > depth) {
1750                                         ++depth;
1751                                 }
1752                         } else {
1753                                 while (par->params().depth() < depth) {
1754                                         --depth;
1755                                 }
1756                         }
1757                 }
1758                 
1759                 // First write the layout
1760                 string const tmp = textclasslist.NameOfLayout(params.textclass, par->layout);
1761                 if (tmp == "Itemize") {
1762                         ltype = 1;
1763                         ltype_depth = depth + 1;
1764                 } else if (tmp == "Enumerate") {
1765                         ltype = 2;
1766                         ltype_depth = depth + 1;
1767                 } else if (contains(tmp, "ection")) {
1768                         ltype = 3;
1769                         ltype_depth = depth + 1;
1770                 } else if (contains(tmp, "aragraph")) {
1771                         ltype = 4;
1772                         ltype_depth = depth + 1;
1773                 } else if (tmp == "Description") {
1774                         ltype = 5;
1775                         ltype_depth = depth + 1;
1776                 } else if (tmp == "Abstract") {
1777                         ltype = 6;
1778                         ltype_depth = 0;
1779                 } else if (tmp == "Bibliography") {
1780                         ltype = 7;
1781                         ltype_depth = 0;
1782                 } else {
1783                         ltype = 0;
1784                         ltype_depth = 0;
1785                 }
1786                 
1787                 /* maybe some vertical spaces */ 
1788                 
1789                 /* the labelwidthstring used in lists */ 
1790                 
1791                 /* some lines? */ 
1792                 
1793                 /* some pagebreaks? */ 
1794                 
1795                 /* noindent ? */ 
1796                 
1797                 /* what about the alignment */ 
1798         } else {
1799                 lyxerr << "Should this ever happen?" << endl;
1800         }
1801
1802         for (Paragraph::size_type i = 0; i < par->size(); ++i) {
1803                 if (!i && !noparbreak) {
1804                         if (linelen > 0)
1805                                 buffer << "\n\n";
1806                         for (Paragraph::depth_type j = 0; j < depth; ++j)
1807                                 buffer << "  ";
1808                         currlinelen = depth * 2;
1809                         switch (ltype) {
1810                         case 0: // Standard
1811                         case 4: // (Sub)Paragraph
1812                         case 5: // Description
1813                                 break;
1814                         case 6: // Abstract
1815                                 if (linelen > 0)
1816                                         buffer << "Abstract\n\n";
1817                                 else
1818                                         buffer << "Abstract: ";
1819                                 break;
1820                         case 7: // Bibliography
1821                                 if (!ref_printed) {
1822                                         if (linelen > 0)
1823                                                 buffer << "References\n\n";
1824                                         else
1825                                                 buffer << "References: ";
1826                                         ref_printed = true;
1827                                 }
1828                                 break;
1829                         default:
1830                                 buffer << par->params().labelString() << " ";
1831                                 break;
1832                         }
1833                         if (ltype_depth > depth) {
1834                                 for (Paragraph::depth_type j = ltype_depth - 1; 
1835                                      j > depth; --j)
1836                                         buffer << "  ";
1837                                 currlinelen += (ltype_depth-depth)*2;
1838                         }
1839                 }
1840                 
1841                 char c = par->getUChar(params, i);
1842                 if (islatex)
1843                         continue;
1844                 switch (c) {
1845                 case Paragraph::META_INSET:
1846                 {
1847                         Inset const * inset = par->getInset(i);
1848                         if (inset) {
1849                                 if (!inset->ascii(this, buffer)) {
1850                                         string dummy;
1851                                         string const s =
1852                                                 rsplit(buffer.str().c_str(),
1853                                                        dummy, '\n');
1854                                         currlinelen += s.length();
1855                                 } else {
1856                                         // to be sure it breaks paragraph
1857                                         currlinelen += linelen;
1858                                 }
1859                         }
1860                 }
1861                 break;
1862                 
1863                 case Paragraph::META_NEWLINE:
1864                         if (linelen > 0) {
1865                                 buffer << "\n";
1866                                 for (Paragraph::depth_type j = 0; 
1867                                      j < depth; ++j)
1868                                         buffer << "  ";
1869                         }
1870                         currlinelen = depth * 2;
1871                         if (ltype_depth > depth) {
1872                                 for (Paragraph::depth_type j = ltype_depth;
1873                                      j > depth; --j)
1874                                         buffer << "  ";
1875                                 currlinelen += (ltype_depth - depth) * 2;
1876                         }
1877                         break;
1878                         
1879                 case Paragraph::META_HFILL: 
1880                         buffer << "\t";
1881                         break;
1882                         
1883                 case '\\':
1884                         buffer << "\\";
1885                         break;
1886                         
1887                 default:
1888                         if ((linelen > 0) && (currlinelen > (linelen - 10)) &&
1889                             (c == ' ') && ((i + 2) < par->size()))
1890                         {
1891                                 buffer << "\n";
1892                                 for (Paragraph::depth_type j = 0; 
1893                                      j < depth; ++j)
1894                                         buffer << "  ";
1895                                 currlinelen = depth * 2;
1896                                 if (ltype_depth > depth) {
1897                                         for (Paragraph::depth_type j = ltype_depth;
1898                                             j > depth; --j)
1899                                                 buffer << "  ";
1900                                         currlinelen += (ltype_depth-depth)*2;
1901                                 }
1902                         } else if (c != '\0')
1903                                 buffer << c;
1904                         else if (c == '\0')
1905                                 lyxerr[Debug::INFO] << "writeAsciiFile: NULL char in structure." << endl;
1906                         ++currlinelen;
1907                         break;
1908                 }
1909         }
1910         return buffer.str().c_str();
1911 }
1912
1913
1914 void Buffer::writeFileAscii(string const & fname, int linelen) 
1915 {
1916         ofstream ofs(fname.c_str());
1917         if (!ofs) {
1918                 WriteFSAlert(_("Error: Cannot write file:"), fname);
1919                 return;
1920         }
1921         writeFileAscii(ofs, linelen);
1922 }
1923
1924
1925 void Buffer::writeFileAscii(ostream & ofs, int linelen) 
1926 {
1927         Paragraph * par = paragraph;
1928         while (par) {
1929                 ofs << asciiParagraph(par, linelen);
1930                 par = par->next();
1931         }
1932         ofs << "\n";
1933 }
1934
1935 bool use_babel;
1936
1937 void Buffer::makeLaTeXFile(string const & fname, 
1938                            string const & original_path,
1939                            bool nice, bool only_body)
1940 {
1941         lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1942         
1943         niceFile = nice; // this will be used by Insetincludes.
1944
1945         tex_code_break_column = lyxrc.ascii_linelen;
1946
1947         LyXTextClass const & tclass =
1948                 textclasslist.TextClass(params.textclass);
1949
1950         ofstream ofs(fname.c_str());
1951         if (!ofs) {
1952                 WriteFSAlert(_("Error: Cannot open file: "), fname);
1953                 return;
1954         }
1955         
1956         // validate the buffer.
1957         lyxerr[Debug::LATEX] << "  Validating buffer..." << endl;
1958         LaTeXFeatures features(params, tclass.numLayouts());
1959         validate(features);
1960         lyxerr[Debug::LATEX] << "  Buffer validation done." << endl;
1961         
1962         texrow.reset();
1963         // The starting paragraph of the coming rows is the 
1964         // first paragraph of the document. (Asger)
1965         texrow.start(paragraph, 0);
1966
1967         if (!only_body && nice) {
1968                 ofs << "%% " LYX_DOCVERSION " created this file.  "
1969                         "For more info, see http://www.lyx.org/.\n"
1970                         "%% Do not edit unless you really know what "
1971                         "you are doing.\n";
1972                 texrow.newline();
1973                 texrow.newline();
1974         }
1975         lyxerr[Debug::INFO] << "lyx header finished" << endl;
1976         // There are a few differences between nice LaTeX and usual files:
1977         // usual is \batchmode and has a 
1978         // special input@path to allow the including of figures
1979         // with either \input or \includegraphics (what figinsets do).
1980         // batchmode is not set if there is a tex_code_break_column.
1981         // In this case somebody is interested in the generated LaTeX,
1982         // so this is OK. input@path is set when the actual parameter
1983         // original_path is set. This is done for usual tex-file, but not
1984         // for nice-latex-file. (Matthias 250696)
1985         if (!only_body) {
1986                 if (!nice){
1987                         // code for usual, NOT nice-latex-file
1988                         ofs << "\\batchmode\n"; // changed
1989                         // from \nonstopmode
1990                         texrow.newline();
1991                 }
1992                 if (!original_path.empty()) {
1993                         ofs << "\\makeatletter\n"
1994                             << "\\def\\input@path{{"
1995                             << os::external_path(original_path) << "/}}\n"
1996                             << "\\makeatother\n";
1997                         texrow.newline();
1998                         texrow.newline();
1999                         texrow.newline();
2000                 }
2001                 
2002                 ofs << "\\documentclass";
2003                 
2004                 ostringstream options; // the document class options.
2005                 
2006                 if (tokenPos(tclass.opt_fontsize(),
2007                              '|', params.fontsize) >= 0) {
2008                         // only write if existing in list (and not default)
2009                         options << params.fontsize << "pt,";
2010                 }
2011                 
2012                 
2013                 if (!params.use_geometry &&
2014                     (params.paperpackage == BufferParams::PACKAGE_NONE)) {
2015                         switch (params.papersize) {
2016                         case BufferParams::PAPER_A4PAPER:
2017                                 options << "a4paper,";
2018                                 break;
2019                         case BufferParams::PAPER_USLETTER:
2020                                 options << "letterpaper,";
2021                                 break;
2022                         case BufferParams::PAPER_A5PAPER:
2023                                 options << "a5paper,";
2024                                 break;
2025                         case BufferParams::PAPER_B5PAPER:
2026                                 options << "b5paper,";
2027                                 break;
2028                         case BufferParams::PAPER_EXECUTIVEPAPER:
2029                                 options << "executivepaper,";
2030                                 break;
2031                         case BufferParams::PAPER_LEGALPAPER:
2032                                 options << "legalpaper,";
2033                                 break;
2034                         }
2035                 }
2036
2037                 // if needed
2038                 if (params.sides != tclass.sides()) {
2039                         switch (params.sides) {
2040                         case LyXTextClass::OneSide:
2041                                 options << "oneside,";
2042                                 break;
2043                         case LyXTextClass::TwoSides:
2044                                 options << "twoside,";
2045                                 break;
2046                         }
2047                 }
2048
2049                 // if needed
2050                 if (params.columns != tclass.columns()) {
2051                         if (params.columns == 2)
2052                                 options << "twocolumn,";
2053                         else
2054                                 options << "onecolumn,";
2055                 }
2056
2057                 if (!params.use_geometry 
2058                     && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2059                         options << "landscape,";
2060                 
2061                 // language should be a parameter to \documentclass
2062                 use_babel = false;
2063                 ostringstream language_options;
2064                 if (params.language->babel() == "hebrew"
2065                     && default_language->babel() != "hebrew")
2066                          // This seems necessary
2067                         features.UsedLanguages.insert(default_language);
2068
2069                 if (lyxrc.language_use_babel ||
2070                     params.language->lang() != lyxrc.default_language ||
2071                     !features.UsedLanguages.empty()) {
2072                         use_babel = true;
2073                         for (LaTeXFeatures::LanguageList::const_iterator cit =
2074                                      features.UsedLanguages.begin();
2075                              cit != features.UsedLanguages.end(); ++cit)
2076                                 language_options << (*cit)->babel() << ',';
2077                         language_options << params.language->babel();
2078                         if (lyxrc.language_global_options)
2079                                 options << language_options.str() << ',';
2080                 }
2081
2082                 // the user-defined options
2083                 if (!params.options.empty()) {
2084                         options << params.options << ',';
2085                 }
2086
2087                 string strOptions(options.str().c_str());
2088                 if (!strOptions.empty()){
2089                         strOptions = strip(strOptions, ',');
2090                         ofs << '[' << strOptions << ']';
2091                 }
2092                 
2093                 ofs << '{'
2094                     << textclasslist.LatexnameOfClass(params.textclass)
2095                     << "}\n";
2096                 texrow.newline();
2097                 // end of \documentclass defs
2098                 
2099                 // font selection must be done before loading fontenc.sty
2100                 // The ae package is not needed when using OT1 font encoding.
2101                 if (params.fonts != "default" &&
2102                     (params.fonts != "ae" || lyxrc.fontenc != "default")) {
2103                         ofs << "\\usepackage{" << params.fonts << "}\n";
2104                         texrow.newline();
2105                         if (params.fonts == "ae") {
2106                                 ofs << "\\usepackage{aecompl}\n";
2107                                 texrow.newline();
2108                         }
2109                 }
2110                 // this one is not per buffer
2111                 if (lyxrc.fontenc != "default") {
2112                         ofs << "\\usepackage[" << lyxrc.fontenc
2113                             << "]{fontenc}\n";
2114                         texrow.newline();
2115                 }
2116
2117                 if (params.inputenc == "auto") {
2118                         string const doc_encoding =
2119                                 params.language->encoding()->LatexName();
2120
2121                         // Create a list with all the input encodings used 
2122                         // in the document
2123                         set<string> encodings;
2124                         for (LaTeXFeatures::LanguageList::const_iterator it =
2125                                      features.UsedLanguages.begin();
2126                              it != features.UsedLanguages.end(); ++it)
2127                                 if ((*it)->encoding()->LatexName() != doc_encoding)
2128                                         encodings.insert((*it)->encoding()->LatexName());
2129
2130                         ofs << "\\usepackage[";
2131                         std::copy(encodings.begin(), encodings.end(),
2132                                   std::ostream_iterator<string>(ofs, ","));
2133                         ofs << doc_encoding << "]{inputenc}\n";
2134                         texrow.newline();
2135                 } else if (params.inputenc != "default") {
2136                         ofs << "\\usepackage[" << params.inputenc
2137                             << "]{inputenc}\n";
2138                         texrow.newline();
2139                 }
2140
2141                 // At the very beginning the text parameters.
2142                 if (params.paperpackage != BufferParams::PACKAGE_NONE) {
2143                         switch (params.paperpackage) {
2144                         case BufferParams::PACKAGE_A4:
2145                                 ofs << "\\usepackage{a4}\n";
2146                                 texrow.newline();
2147                                 break;
2148                         case BufferParams::PACKAGE_A4WIDE:
2149                                 ofs << "\\usepackage{a4wide}\n";
2150                                 texrow.newline();
2151                                 break;
2152                         case BufferParams::PACKAGE_WIDEMARGINSA4:
2153                                 ofs << "\\usepackage[widemargins]{a4}\n";
2154                                 texrow.newline();
2155                                 break;
2156                         }
2157                 }
2158                 if (params.use_geometry) {
2159                         ofs << "\\usepackage{geometry}\n";
2160                         texrow.newline();
2161                         ofs << "\\geometry{verbose";
2162                         if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2163                                 ofs << ",landscape";
2164                         switch (params.papersize2) {
2165                         case BufferParams::VM_PAPER_CUSTOM:
2166                                 if (!params.paperwidth.empty())
2167                                         ofs << ",paperwidth="
2168                                             << params.paperwidth;
2169                                 if (!params.paperheight.empty())
2170                                         ofs << ",paperheight="
2171                                             << params.paperheight;
2172                                 break;
2173                         case BufferParams::VM_PAPER_USLETTER:
2174                                 ofs << ",letterpaper";
2175                                 break;
2176                         case BufferParams::VM_PAPER_USLEGAL:
2177                                 ofs << ",legalpaper";
2178                                 break;
2179                         case BufferParams::VM_PAPER_USEXECUTIVE:
2180                                 ofs << ",executivepaper";
2181                                 break;
2182                         case BufferParams::VM_PAPER_A3:
2183                                 ofs << ",a3paper";
2184                                 break;
2185                         case BufferParams::VM_PAPER_A4:
2186                                 ofs << ",a4paper";
2187                                 break;
2188                         case BufferParams::VM_PAPER_A5:
2189                                 ofs << ",a5paper";
2190                                 break;
2191                         case BufferParams::VM_PAPER_B3:
2192                                 ofs << ",b3paper";
2193                                 break;
2194                         case BufferParams::VM_PAPER_B4:
2195                                 ofs << ",b4paper";
2196                                 break;
2197                         case BufferParams::VM_PAPER_B5:
2198                                 ofs << ",b5paper";
2199                                 break;
2200                         default:
2201                                 // default papersize ie BufferParams::VM_PAPER_DEFAULT
2202                                 switch (lyxrc.default_papersize) {
2203                                 case BufferParams::PAPER_DEFAULT: // keep compiler happy
2204                                 case BufferParams::PAPER_USLETTER:
2205                                         ofs << ",letterpaper";
2206                                         break;
2207                                 case BufferParams::PAPER_LEGALPAPER:
2208                                         ofs << ",legalpaper";
2209                                         break;
2210                                 case BufferParams::PAPER_EXECUTIVEPAPER:
2211                                         ofs << ",executivepaper";
2212                                         break;
2213                                 case BufferParams::PAPER_A3PAPER:
2214                                         ofs << ",a3paper";
2215                                         break;
2216                                 case BufferParams::PAPER_A4PAPER:
2217                                         ofs << ",a4paper";
2218                                         break;
2219                                 case BufferParams::PAPER_A5PAPER:
2220                                         ofs << ",a5paper";
2221                                         break;
2222                                 case BufferParams::PAPER_B5PAPER:
2223                                         ofs << ",b5paper";
2224                                         break;
2225                                 }
2226                         }
2227                         if (!params.topmargin.empty())
2228                                 ofs << ",tmargin=" << params.topmargin;
2229                         if (!params.bottommargin.empty())
2230                                 ofs << ",bmargin=" << params.bottommargin;
2231                         if (!params.leftmargin.empty())
2232                                 ofs << ",lmargin=" << params.leftmargin;
2233                         if (!params.rightmargin.empty())
2234                                 ofs << ",rmargin=" << params.rightmargin;
2235                         if (!params.headheight.empty())
2236                                 ofs << ",headheight=" << params.headheight;
2237                         if (!params.headsep.empty())
2238                                 ofs << ",headsep=" << params.headsep;
2239                         if (!params.footskip.empty())
2240                                 ofs << ",footskip=" << params.footskip;
2241                         ofs << "}\n";
2242                         texrow.newline();
2243                 }
2244                 if (features.amsstyle
2245                     && !tclass.provides(LyXTextClass::amsmath)) {
2246                         ofs << "\\usepackage{amsmath}\n";
2247                         texrow.newline();
2248                 }
2249
2250                 if (tokenPos(tclass.opt_pagestyle(),
2251                              '|', params.pagestyle) >= 0) {
2252                         if (params.pagestyle == "fancy") {
2253                                 ofs << "\\usepackage{fancyhdr}\n";
2254                                 texrow.newline();
2255                         }
2256                         ofs << "\\pagestyle{" << params.pagestyle << "}\n";
2257                         texrow.newline();
2258                 }
2259
2260                 // We try to load babel late, in case it interferes
2261                 // with other packages.
2262                 if (use_babel) {
2263                         string tmp = lyxrc.language_package;
2264                         if (!lyxrc.language_global_options
2265                             && tmp == "\\usepackage{babel}")
2266                                 tmp = string("\\usepackage[") +
2267                                         language_options.str().c_str() +
2268                                         "]{babel}";
2269                         ofs << tmp << "\n";
2270                         texrow.newline();
2271                 }
2272
2273                 if (params.secnumdepth != tclass.secnumdepth()) {
2274                         ofs << "\\setcounter{secnumdepth}{"
2275                             << params.secnumdepth
2276                             << "}\n";
2277                         texrow.newline();
2278                 }
2279                 if (params.tocdepth != tclass.tocdepth()) {
2280                         ofs << "\\setcounter{tocdepth}{"
2281                             << params.tocdepth
2282                             << "}\n";
2283                         texrow.newline();
2284                 }
2285                 
2286                 if (params.paragraph_separation) {
2287                         switch (params.defskip.kind()) {
2288                         case VSpace::SMALLSKIP: 
2289                                 ofs << "\\setlength\\parskip{\\smallskipamount}\n";
2290                                 break;
2291                         case VSpace::MEDSKIP:
2292                                 ofs << "\\setlength\\parskip{\\medskipamount}\n";
2293                                 break;
2294                         case VSpace::BIGSKIP:
2295                                 ofs << "\\setlength\\parskip{\\bigskipamount}\n";
2296                                 break;
2297                         case VSpace::LENGTH:
2298                                 ofs << "\\setlength\\parskip{"
2299                                     << params.defskip.length().asLatexString()
2300                                     << "}\n";
2301                                 break;
2302                         default: // should never happen // Then delete it.
2303                                 ofs << "\\setlength\\parskip{\\medskipamount}\n";
2304                                 break;
2305                         }
2306                         texrow.newline();
2307                         
2308                         ofs << "\\setlength\\parindent{0pt}\n";
2309                         texrow.newline();
2310                 }
2311
2312                 // Now insert the LyX specific LaTeX commands...
2313
2314                 // The optional packages;
2315                 string preamble(features.getPackages());
2316
2317                 // this might be useful...
2318                 preamble += "\n\\makeatletter\n";
2319
2320                 // Some macros LyX will need
2321                 string tmppreamble(features.getMacros());
2322
2323                 if (!tmppreamble.empty()) {
2324                         preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2325                                 "LyX specific LaTeX commands.\n"
2326                                 + tmppreamble + '\n';
2327                 }
2328
2329                 // the text class specific preamble 
2330                 tmppreamble = features.getTClassPreamble();
2331                 if (!tmppreamble.empty()) {
2332                         preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2333                                 "Textclass specific LaTeX commands.\n"
2334                                 + tmppreamble + '\n';
2335                 }
2336
2337                 /* the user-defined preamble */
2338                 if (!params.preamble.empty()) {
2339                         preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2340                                 "User specified LaTeX commands.\n"
2341                                 + params.preamble + '\n';
2342                 }
2343
2344                 preamble += "\\makeatother\n";
2345
2346                 // Itemize bullet settings need to be last in case the user
2347                 // defines their own bullets that use a package included
2348                 // in the user-defined preamble -- ARRae
2349                 // Actually it has to be done much later than that
2350                 // since some packages like frenchb make modifications
2351                 // at \begin{document} time -- JMarc 
2352                 string bullets_def;
2353                 for (int i = 0; i < 4; ++i) {
2354                         if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
2355                                 if (bullets_def.empty())
2356                                         bullets_def="\\AtBeginDocument{\n";
2357                                 bullets_def += "  \\renewcommand{\\labelitemi";
2358                                 switch (i) {
2359                                 // `i' is one less than the item to modify
2360                                 case 0:
2361                                         break;
2362                                 case 1:
2363                                         bullets_def += 'i';
2364                                         break;
2365                                 case 2:
2366                                         bullets_def += "ii";
2367                                         break;
2368                                 case 3:
2369                                         bullets_def += 'v';
2370                                         break;
2371                                 }
2372                                 bullets_def += "}{" + 
2373                                   params.user_defined_bullets[i].getText() 
2374                                   + "}\n";
2375                         }
2376                 }
2377
2378                 if (!bullets_def.empty())
2379                   preamble += bullets_def + "}\n\n";
2380
2381                 for (int j = countChar(preamble, '\n'); j-- ;) {
2382                         texrow.newline();
2383                 }
2384
2385                 ofs << preamble;
2386
2387                 // make the body.
2388                 ofs << "\\begin{document}\n";
2389                 texrow.newline();
2390         } // only_body
2391         lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
2392
2393         if (!lyxrc.language_auto_begin) {
2394                 ofs << subst(lyxrc.language_command_begin, "$$lang",
2395                              params.language->babel())
2396                     << endl;
2397                 texrow.newline();
2398         }
2399         
2400         latexParagraphs(ofs, paragraph, 0, texrow);
2401
2402         // add this just in case after all the paragraphs
2403         ofs << endl;
2404         texrow.newline();
2405
2406         if (!lyxrc.language_auto_end) {
2407                 ofs << subst(lyxrc.language_command_end, "$$lang",
2408                              params.language->babel())
2409                     << endl;
2410                 texrow.newline();
2411         }
2412
2413         if (!only_body) {
2414                 ofs << "\\end{document}\n";
2415                 texrow.newline();
2416         
2417                 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
2418         } else {
2419                 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
2420                                      << endl;
2421         }
2422
2423         // Just to be sure. (Asger)
2424         texrow.newline();
2425
2426         // tex_code_break_column's value is used to decide
2427         // if we are in batchmode or not (within mathed_write()
2428         // in math_write.C) so we must set it to a non-zero
2429         // value when we leave otherwise we save incorrect .lyx files.
2430         tex_code_break_column = lyxrc.ascii_linelen;
2431
2432         ofs.close();
2433         if (ofs.fail()) {
2434                 lyxerr << "File was not closed properly." << endl;
2435         }
2436         
2437         lyxerr[Debug::INFO] << "Finished making latex file." << endl;
2438 }
2439
2440
2441 //
2442 // LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
2443 //
2444 void Buffer::latexParagraphs(ostream & ofs, Paragraph * par,
2445                              Paragraph * endpar, TexRow & texrow) const
2446 {
2447         bool was_title = false;
2448         bool already_title = false;
2449
2450         // if only_body
2451         while (par != endpar) {
2452                 LyXLayout const & layout =
2453                         textclasslist.Style(params.textclass,
2454                                             par->layout);
2455             
2456                 if (layout.intitle) {
2457                         if (already_title) {
2458                                 lyxerr <<"Error in latexParagraphs: You"
2459                                         " should not mix title layouts"
2460                                         " with normal ones." << endl;
2461                         } else
2462                                 was_title = true;
2463                 } else if (was_title && !already_title) {
2464                         ofs << "\\maketitle\n";
2465                         texrow.newline();
2466                         already_title = true;
2467                         was_title = false;                  
2468                 }
2469                 
2470                 if (layout.isEnvironment()) {
2471                         par = par->TeXEnvironment(this, params, ofs, texrow);
2472                 } else {
2473                         par = par->TeXOnePar(this, params, ofs, texrow, false);
2474                 }
2475         }
2476         // It might be that we only have a title in this document
2477         if (was_title && !already_title) {
2478                 ofs << "\\maketitle\n";
2479                 texrow.newline();
2480         }
2481 }
2482
2483
2484 bool Buffer::isLatex() const
2485 {
2486         return textclasslist.TextClass(params.textclass).outputType() == LATEX;
2487 }
2488
2489
2490 bool Buffer::isLinuxDoc() const
2491 {
2492         return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC;
2493 }
2494
2495
2496 bool Buffer::isLiterate() const
2497 {
2498         return textclasslist.TextClass(params.textclass).outputType() == LITERATE;
2499 }
2500
2501
2502 bool Buffer::isDocBook() const
2503 {
2504         return textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2505 }
2506
2507
2508 bool Buffer::isSGML() const
2509 {
2510         return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC ||
2511                textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2512 }
2513
2514
2515 void Buffer::sgmlOpenTag(ostream & os, Paragraph::depth_type depth,
2516                          string const & latexname) const
2517 {
2518         if (!latexname.empty() && latexname != "!-- --")
2519                 os << "<!-- " << depth << " -->" << "<" << latexname << ">";
2520         //os << string(depth, ' ') << "<" << latexname << ">\n";
2521 }
2522
2523
2524 void Buffer::sgmlCloseTag(ostream & os, Paragraph::depth_type depth,
2525                           string const & latexname) const
2526 {
2527         if (!latexname.empty() && latexname != "!-- --")
2528                 os << "<!-- " << depth << " -->" << "</" << latexname << ">\n";
2529         //os << string(depth, ' ') << "</" << latexname << ">\n";
2530 }
2531
2532
2533 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
2534 {
2535         ofstream ofs(fname.c_str());
2536
2537         if (!ofs) {
2538                 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
2539                 return;
2540         }
2541
2542         niceFile = nice; // this will be used by included files.
2543
2544         LyXTextClass const & tclass =
2545                 textclasslist.TextClass(params.textclass);
2546
2547         LaTeXFeatures features(params, tclass.numLayouts());
2548         validate(features);
2549
2550         texrow.reset();
2551
2552         string top_element = textclasslist.LatexnameOfClass(params.textclass);
2553
2554         if (!body_only) {
2555                 string sgml_includedfiles=features.getIncludedFiles(fname);
2556
2557                 if (params.preamble.empty() && sgml_includedfiles.empty()) {
2558                         ofs << "<!doctype linuxdoc system>\n\n";
2559                 } else {
2560                         ofs << "<!doctype linuxdoc system [ "
2561                             << params.preamble << sgml_includedfiles << " \n]>\n\n";
2562                 }
2563
2564                 if (params.options.empty())
2565                         sgmlOpenTag(ofs, 0, top_element);
2566                 else {
2567                         string top = top_element;
2568                         top += " ";
2569                         top += params.options;
2570                         sgmlOpenTag(ofs, 0, top);
2571                 }
2572         }
2573
2574         ofs << "<!-- "  << LYX_DOCVERSION 
2575             << " created this file. For more info see http://www.lyx.org/"
2576             << " -->\n";
2577
2578         Paragraph::depth_type depth = 0; // paragraph depth
2579         Paragraph * par = paragraph;
2580         string item_name;
2581         vector<string> environment_stack(5);
2582
2583         while (par) {
2584                 LyXLayout const & style =
2585                         textclasslist.Style(params.textclass,
2586                                             par->layout);
2587
2588                 // treat <toc> as a special case for compatibility with old code
2589                 if (par->getChar(0) == Paragraph::META_INSET) {
2590                         Inset * inset = par->getInset(0);
2591                         Inset::Code lyx_code = inset->lyxCode();
2592                         if (lyx_code == Inset::TOC_CODE){
2593                                 string const temp = "toc";
2594                                 sgmlOpenTag(ofs, depth, temp);
2595
2596                                 par = par->next();
2597                                 continue;
2598                         }
2599                 }
2600
2601                 // environment tag closing
2602                 for (; depth > par->params().depth(); --depth) {
2603                         sgmlCloseTag(ofs, depth, environment_stack[depth]);
2604                         environment_stack[depth].erase();
2605                 }
2606
2607                 // write opening SGML tags
2608                 switch (style.latextype) {
2609                 case LATEX_PARAGRAPH:
2610                         if (depth == par->params().depth() 
2611                            && !environment_stack[depth].empty()) {
2612                                 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2613                                 environment_stack[depth].erase();
2614                                 if (depth) 
2615                                         --depth;
2616                                 else
2617                                         ofs << "</p>";
2618                         }
2619                         sgmlOpenTag(ofs, depth, style.latexname());
2620                         break;
2621
2622                 case LATEX_COMMAND:
2623                         if (depth!= 0)
2624                                 linuxDocError(par, 0,
2625                                               _("Error : Wrong depth for"
2626                                                 " LatexType Command.\n"));
2627
2628                         if (!environment_stack[depth].empty()){
2629                                 sgmlCloseTag(ofs, depth,
2630                                              environment_stack[depth]);
2631                                 ofs << "</p>";
2632                         }
2633
2634                         environment_stack[depth].erase();
2635                         sgmlOpenTag(ofs, depth, style.latexname());
2636                         break;
2637
2638                 case LATEX_ENVIRONMENT:
2639                 case LATEX_ITEM_ENVIRONMENT:
2640                         if (depth == par->params().depth() 
2641                             && environment_stack[depth] != style.latexname()) {
2642                                 sgmlCloseTag(ofs, depth,
2643                                              environment_stack[depth]);
2644                                 environment_stack[depth].erase();
2645                         }
2646                         if (depth < par->params().depth()) {
2647                                depth = par->params().depth();
2648                                environment_stack[depth].erase();
2649                         }
2650                         if (environment_stack[depth] != style.latexname()) {
2651                                 if (depth == 0) {
2652                                         sgmlOpenTag(ofs, depth, "p");
2653                                 }
2654                                 sgmlOpenTag(ofs, depth, style.latexname());
2655
2656                                 if (environment_stack.size() == depth + 1)
2657                                         environment_stack.push_back("!-- --");
2658                                 environment_stack[depth] = style.latexname();
2659                         }
2660
2661                         if (style.latexparam() == "CDATA")
2662                                 ofs << "<![CDATA[";
2663
2664                         if (style.latextype == LATEX_ENVIRONMENT) break;
2665
2666                         if (style.labeltype == LABEL_MANUAL)
2667                                 item_name = "tag";
2668                         else
2669                                 item_name = "item";
2670
2671                         sgmlOpenTag(ofs, depth + 1, item_name);
2672                         break;
2673                 default:
2674                         sgmlOpenTag(ofs, depth, style.latexname());
2675                         break;
2676                 }
2677
2678                 simpleLinuxDocOnePar(ofs, par, depth);
2679
2680                 par = par->next();
2681
2682                 ofs << "\n";
2683                 // write closing SGML tags
2684                 switch (style.latextype) {
2685                 case LATEX_COMMAND:
2686                         break;
2687                 case LATEX_ENVIRONMENT:
2688                 case LATEX_ITEM_ENVIRONMENT:
2689                         if (style.latexparam() == "CDATA")
2690                                 ofs << "]]>";
2691                         break;
2692                 default:
2693                         sgmlCloseTag(ofs, depth, style.latexname());
2694                         break;
2695                 }
2696         }
2697    
2698         // Close open tags
2699         for (int i=depth; i >= 0; --i)
2700                 sgmlCloseTag(ofs, depth, environment_stack[i]);
2701
2702         if (!body_only) {
2703                 ofs << "\n\n";
2704                 sgmlCloseTag(ofs, 0, top_element);
2705         }
2706
2707         ofs.close();
2708         // How to check for successful close
2709 }
2710
2711
2712 void Buffer::docBookHandleCaption(ostream & os, string & inner_tag,
2713                                   Paragraph::depth_type depth, int desc_on,
2714                                   Paragraph * & par)
2715 {
2716         Paragraph * tpar = par;
2717         while (tpar
2718                && (tpar->layout != textclasslist.NumberOfLayout(params.textclass,
2719                                                                 "Caption").second))
2720                 tpar = tpar->next();
2721
2722         if (tpar &&
2723             tpar->layout == textclasslist.NumberOfLayout(params.textclass,
2724                                                          "Caption").second) {
2725                 sgmlOpenTag(os, depth + 1, inner_tag);
2726                 string extra_par;
2727                 simpleDocBookOnePar(os, extra_par, tpar,
2728                                     desc_on, depth + 2);
2729                 sgmlCloseTag(os, depth+1, inner_tag);
2730                 if (!extra_par.empty())
2731                         os << extra_par;
2732         }
2733 }
2734
2735
2736 // checks, if newcol chars should be put into this line
2737 // writes newline, if necessary.
2738 namespace {
2739
2740 void linux_doc_line_break(ostream & os, string::size_type & colcount,
2741                           string::size_type newcol)
2742 {
2743         colcount += newcol;
2744         if (colcount > lyxrc.ascii_linelen) {
2745                 os << "\n";
2746                 colcount = newcol; // assume write after this call
2747         }
2748 }
2749
2750 enum PAR_TAG {
2751         NONE=0,
2752         TT = 1,
2753         SF = 2,
2754         BF = 4,
2755         IT = 8,
2756         SL = 16,
2757         EM = 32
2758 };
2759
2760
2761 string tag_name(PAR_TAG const & pt) {
2762         switch (pt) {
2763         case NONE: return "!-- --";
2764         case TT: return "tt";
2765         case SF: return "sf";
2766         case BF: return "bf";
2767         case IT: return "it";
2768         case SL: return "sl";
2769         case EM: return "em";
2770         }
2771         return "";
2772 }
2773
2774
2775 inline
2776 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
2777 {
2778         p1 = static_cast<PAR_TAG>(p1 | p2);
2779 }
2780
2781
2782 inline
2783 void reset(PAR_TAG & p1, PAR_TAG const & p2)
2784 {
2785         p1 = static_cast<PAR_TAG>( p1 & ~p2);
2786 }
2787
2788 } // namespace anon
2789
2790
2791
2792
2793 // Handle internal paragraph parsing -- layout already processed.
2794 void Buffer::simpleLinuxDocOnePar(ostream & os,
2795                                   Paragraph * par, 
2796                                   Paragraph::depth_type /*depth*/)
2797 {
2798         LyXLayout const & style = textclasslist.Style(params.textclass,
2799                                                       par->getLayout());
2800         string::size_type char_line_count = 5;     // Heuristic choice ;-) 
2801
2802         // gets paragraph main font
2803         LyXFont font_old;
2804         bool desc_on;
2805         if (style.labeltype == LABEL_MANUAL) {
2806                 font_old = style.labelfont;
2807                 desc_on = true;
2808         } else {
2809                 font_old = style.font;
2810                 desc_on = false;
2811         }
2812
2813         LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
2814         LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
2815         LyXFont::FONT_SHAPE  shape_type  = LyXFont::UP_SHAPE;
2816         bool is_em = false;
2817
2818         stack<PAR_TAG> tag_state;
2819         // parsing main loop
2820         for (Paragraph::size_type i = 0; i < par->size(); ++i) {
2821
2822                 PAR_TAG tag_close = NONE;
2823                 list < PAR_TAG > tag_open;
2824
2825                 LyXFont const font = par->getFont(params, i);
2826
2827                 if (font_old.family() != font.family()) {
2828                         switch (family_type) {
2829                         case LyXFont::SANS_FAMILY:
2830                                 tag_close |= SF;
2831                                 break;
2832                         case LyXFont::TYPEWRITER_FAMILY:
2833                                 tag_close |= TT;
2834                                 break;
2835                         default:
2836                                 break;
2837                         }
2838
2839                         family_type = font.family();
2840
2841                         switch (family_type) {
2842                         case LyXFont::SANS_FAMILY:
2843                                 tag_open.push_back(SF);
2844                                 break;
2845                         case LyXFont::TYPEWRITER_FAMILY:
2846                                 tag_open.push_back(TT);
2847                                 break;
2848                         default:
2849                                 break;
2850                         }
2851                 }
2852
2853                 if (font_old.series() != font.series()) {
2854                         switch (series_type) {
2855                         case LyXFont::BOLD_SERIES:
2856                                 tag_close |= BF;
2857                                 break;
2858                         default:
2859                                 break;
2860                         }
2861
2862                         series_type = font.series();
2863
2864                         switch (series_type) {
2865                         case LyXFont::BOLD_SERIES:
2866                                 tag_open.push_back(BF);
2867                                 break;
2868                         default:
2869                                 break;
2870                         }
2871
2872                 }
2873
2874                 if (font_old.shape() != font.shape()) {
2875                         switch (shape_type) {
2876                         case LyXFont::ITALIC_SHAPE:
2877                                 tag_close |= IT;
2878                                 break;
2879                         case LyXFont::SLANTED_SHAPE:
2880                                 tag_close |= SL;
2881                                 break;
2882                         default:
2883                                 break;
2884                         }
2885
2886                         shape_type = font.shape();
2887
2888                         switch (shape_type) {
2889                         case LyXFont::ITALIC_SHAPE:
2890                                 tag_open.push_back(IT);
2891                                 break;
2892                         case LyXFont::SLANTED_SHAPE:
2893                                 tag_open.push_back(SL);
2894                                 break;
2895                         default:
2896                                 break;
2897                         }
2898                 }
2899                 // handle <em> tag
2900                 if (font_old.emph() != font.emph()) {
2901                         if (font.emph() == LyXFont::ON) {
2902                                 tag_open.push_back(EM);
2903                                 is_em = true;
2904                         }
2905                         else if (is_em) {
2906                                 tag_close |= EM;
2907                                 is_em = false;
2908                         }
2909                 }
2910
2911                 list < PAR_TAG > temp;
2912                 while(!tag_state.empty() && tag_close ) {
2913                         PAR_TAG k =  tag_state.top();
2914                         tag_state.pop();
2915                         os << "</" << tag_name(k) << ">";
2916                         if (tag_close & k)
2917                                 reset(tag_close,k);
2918                         else
2919                                 temp.push_back(k);
2920                 }
2921
2922                 for(list< PAR_TAG >::const_iterator j = temp.begin();
2923                     j != temp.end(); ++j) {
2924                         tag_state.push(*j);
2925                         os << "<" << tag_name(*j) << ">";
2926                 }
2927
2928                 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
2929                     j != tag_open.end(); ++j) {
2930                         tag_state.push(*j);
2931                         os << "<" << tag_name(*j) << ">";
2932                 }
2933
2934                 char c = par->getChar(i);
2935
2936                 if (c == Paragraph::META_INSET) {
2937                         Inset * inset = par->getInset(i);
2938                         inset->linuxdoc(this, os);
2939                         font_old = font;
2940                         continue;
2941                 }
2942
2943                 if (style.latexparam() == "CDATA") {
2944                         // "TeX"-Mode on == > SGML-Mode on.
2945                         if (c != '\0')
2946                                 os << c;
2947                         ++char_line_count;
2948                 } else {
2949                         string sgml_string;
2950                         if (par->linuxDocConvertChar(c, sgml_string)
2951                             && !style.free_spacing) { 
2952                                 // in freespacing mode, spaces are
2953                                 // non-breaking characters
2954                                 if (desc_on) {// if char is ' ' then...
2955
2956                                         ++char_line_count;
2957                                         linux_doc_line_break(os, char_line_count, 6);
2958                                         os << "</tag>";
2959                                         desc_on = false;
2960                                 } else  {
2961                                         linux_doc_line_break(os, char_line_count, 1);
2962                                         os << c;
2963                                 }
2964                         } else {
2965                                 os << sgml_string;
2966                                 char_line_count += sgml_string.length();
2967                         }
2968                 }
2969                 font_old = font;
2970         }
2971
2972         while (!tag_state.empty()) {
2973                 os << "</" << tag_name(tag_state.top()) << ">";
2974                 tag_state.pop();
2975         }
2976
2977         // resets description flag correctly
2978         if (desc_on) {
2979                 // <tag> not closed...
2980                 linux_doc_line_break(os, char_line_count, 6);
2981                 os << "</tag>";
2982         }
2983 }
2984
2985
2986 // Print an error message.
2987 void Buffer::linuxDocError(Paragraph * par, int pos,
2988                            string const & message) 
2989 {
2990         // insert an error marker in text
2991         InsetError * new_inset = new InsetError(message);
2992         par->insertInset(pos, new_inset);
2993 }
2994
2995
2996 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
2997 {
2998         ofstream ofs(fname.c_str());
2999         if (!ofs) {
3000                 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
3001                 return;
3002         }
3003
3004         Paragraph * par = paragraph;
3005
3006         niceFile = nice; // this will be used by Insetincludes.
3007
3008         LyXTextClass const & tclass =
3009                 textclasslist.TextClass(params.textclass);
3010
3011         LaTeXFeatures features(params, tclass.numLayouts());
3012         validate(features);
3013    
3014         texrow.reset();
3015
3016         string top_element = textclasslist.LatexnameOfClass(params.textclass);
3017
3018         if (!only_body) {
3019                 string sgml_includedfiles = features.getIncludedFiles(fname);
3020
3021                 ofs << "<!doctype " << top_element
3022                     << " public \"-//OASIS//DTD DocBook V3.1//EN\"";
3023
3024                 if (params.preamble.empty() && sgml_includedfiles.empty())
3025                         ofs << ">\n\n";
3026                 else
3027                         ofs << "\n [ " << params.preamble 
3028                             << sgml_includedfiles << " \n]>\n\n";
3029         }
3030
3031         string top = top_element;       
3032         top += " lang=\"";
3033         top += params.language->code();
3034         top += "\"";
3035
3036         if (!params.options.empty()) {
3037                 top += " ";
3038                 top += params.options;
3039         }
3040         sgmlOpenTag(ofs, 0, top);
3041
3042         ofs << "<!-- DocBook file was created by " << LYX_DOCVERSION 
3043             << "\n  See http://www.lyx.org/ for more information -->\n";
3044
3045         vector<string> environment_stack(10);
3046         vector<string> environment_inner(10);
3047         vector<string> command_stack(10);
3048
3049         bool command_flag = false;
3050         Paragraph::depth_type command_depth = 0;
3051         Paragraph::depth_type command_base = 0;
3052         Paragraph::depth_type cmd_depth = 0;
3053         Paragraph::depth_type depth = 0; // paragraph depth
3054
3055         string item_name;
3056         string command_name;
3057
3058         while (par) {
3059                 string sgmlparam;
3060                 string c_depth;
3061                 string c_params;
3062                 int desc_on = 0; // description mode
3063
3064                 LyXLayout const & style =
3065                         textclasslist.Style(params.textclass,
3066                                             par->layout);
3067
3068                 // environment tag closing
3069                 for (; depth > par->params().depth(); --depth) {
3070                         if (environment_inner[depth] != "!-- --") {
3071                                 item_name = "listitem";
3072                                 sgmlCloseTag(ofs, command_depth + depth,
3073                                              item_name);
3074                                 if (environment_inner[depth] == "varlistentry")
3075                                         sgmlCloseTag(ofs, depth+command_depth,
3076                                                      environment_inner[depth]);
3077                         }
3078                         sgmlCloseTag(ofs, depth + command_depth,
3079                                      environment_stack[depth]);
3080                         environment_stack[depth].erase();
3081                         environment_inner[depth].erase();
3082                 }
3083
3084                 if (depth == par->params().depth()
3085                    && environment_stack[depth] != style.latexname()
3086                    && !environment_stack[depth].empty()) {
3087                         if (environment_inner[depth] != "!-- --") {
3088                                 item_name= "listitem";
3089                                 sgmlCloseTag(ofs, command_depth+depth,
3090                                              item_name);
3091                                 if (environment_inner[depth] == "varlistentry")
3092                                         sgmlCloseTag(ofs,
3093                                                      depth + command_depth,
3094                                                      environment_inner[depth]);
3095                         }
3096                         
3097                         sgmlCloseTag(ofs, depth + command_depth,
3098                                      environment_stack[depth]);
3099                         
3100                         environment_stack[depth].erase();
3101                         environment_inner[depth].erase();
3102                 }
3103
3104                 // Write opening SGML tags.
3105                 switch (style.latextype) {
3106                 case LATEX_PARAGRAPH:
3107                         sgmlOpenTag(ofs, depth + command_depth,
3108                                     style.latexname());
3109                         break;
3110
3111                 case LATEX_COMMAND:
3112                         if (depth != 0)
3113                                 linuxDocError(par, 0,
3114                                               _("Error : Wrong depth for "
3115                                                 "LatexType Command.\n"));
3116                         
3117                         command_name = style.latexname();
3118                         
3119                         sgmlparam = style.latexparam();
3120                         c_params = split(sgmlparam, c_depth,'|');
3121                         
3122                         cmd_depth = lyx::atoi(c_depth);
3123                         
3124                         if (command_flag) {
3125                                 if (cmd_depth < command_base) {
3126                                         for (Paragraph::depth_type j = command_depth; j >= command_base; --j)
3127                                                 sgmlCloseTag(ofs, j, command_stack[j]);
3128                                         command_depth = command_base = cmd_depth;
3129                                 } else if (cmd_depth <= command_depth) {
3130                                         for (int j = command_depth; j >= int(cmd_depth); --j)
3131                                                 sgmlCloseTag(ofs, j, command_stack[j]);
3132                                         command_depth = cmd_depth;
3133                                 } else
3134                                         command_depth = cmd_depth;
3135                         } else {
3136                                 command_depth = command_base = cmd_depth;
3137                                 command_flag = true;
3138                         }
3139                         if (command_stack.size() == command_depth + 1)
3140                                 command_stack.push_back(string());
3141                         command_stack[command_depth] = command_name;
3142
3143                         // treat label as a special case for
3144                         // more WYSIWYM handling.
3145                         if (par->getChar(0) == Paragraph::META_INSET) {
3146                                 Inset * inset = par->getInset(0);
3147                                 Inset::Code lyx_code = inset->lyxCode();
3148                                 if (lyx_code == Inset::LABEL_CODE){
3149                                         command_name += " id=\"";
3150                                         command_name += (static_cast<InsetCommand *>(inset))->getContents();
3151                                         command_name += "\"";
3152                                         desc_on = 3;
3153                                 }
3154                         }
3155
3156                         sgmlOpenTag(ofs, depth + command_depth, command_name);
3157                         if (c_params.empty())
3158                                 item_name = "title";
3159                         else
3160                                 item_name = c_params;
3161                         sgmlOpenTag(ofs, depth + 1 + command_depth, item_name);
3162                         break;
3163
3164                 case LATEX_ENVIRONMENT:
3165                 case LATEX_ITEM_ENVIRONMENT:
3166                         if (depth < par->params().depth()) {
3167                                 depth = par->params().depth();
3168                                 environment_stack[depth].erase();
3169                         }
3170
3171                         if (environment_stack[depth] != style.latexname()) {
3172                                 if(environment_stack.size() == depth + 1) {
3173                                         environment_stack.push_back("!-- --");
3174                                         environment_inner.push_back("!-- --");
3175                                 }
3176                                 environment_stack[depth] = style.latexname();
3177                                 environment_inner[depth] = "!-- --";
3178                                 sgmlOpenTag(ofs, depth + command_depth,
3179                                             environment_stack[depth]);
3180                         } else {
3181                                 if (environment_inner[depth] != "!-- --") {
3182                                         item_name= "listitem";
3183                                         sgmlCloseTag(ofs,
3184                                                      command_depth + depth,
3185                                                      item_name);
3186                                         if (environment_inner[depth] == "varlistentry")
3187                                                 sgmlCloseTag(ofs,
3188                                                              depth + command_depth,
3189                                                              environment_inner[depth]);
3190                                 }
3191                         }
3192                         
3193                         if (style.latextype == LATEX_ENVIRONMENT) {
3194                                 if (!style.latexparam().empty()) {
3195                                         if(style.latexparam() == "CDATA")
3196                                                 ofs << "<![CDATA[";
3197                                         else
3198                                                 sgmlOpenTag(ofs, depth + command_depth,
3199                                                             style.latexparam());
3200                                 }
3201                                 break;
3202                         }
3203
3204                         desc_on = (style.labeltype == LABEL_MANUAL);
3205
3206                         if (desc_on)
3207                                 environment_inner[depth]= "varlistentry";
3208                         else
3209                                 environment_inner[depth]= "listitem";
3210
3211                         sgmlOpenTag(ofs, depth + 1 + command_depth,
3212                                     environment_inner[depth]);
3213
3214                         if (desc_on) {
3215                                 item_name= "term";
3216                                 sgmlOpenTag(ofs, depth + 1 + command_depth,
3217                                             item_name);
3218                         } else {
3219                                 item_name= "para";
3220                                 sgmlOpenTag(ofs, depth + 1 + command_depth,
3221                                             item_name);
3222                         }
3223                         break;
3224                 default:
3225                         sgmlOpenTag(ofs, depth + command_depth,
3226                                     style.latexname());
3227                         break;
3228                 }
3229
3230                 string extra_par;
3231                 simpleDocBookOnePar(ofs, extra_par, par, desc_on,
3232                                     depth + 1 + command_depth);
3233                 par = par->next();
3234
3235                 string end_tag;
3236                 // write closing SGML tags
3237                 switch (style.latextype) {
3238                 case LATEX_COMMAND:
3239                         if (c_params.empty())
3240                                 end_tag = "title";
3241                         else
3242                                 end_tag = c_params;
3243                         sgmlCloseTag(ofs, depth + command_depth, end_tag);
3244                         break;
3245                 case LATEX_ENVIRONMENT:
3246                         if (!style.latexparam().empty()) {
3247                                 if(style.latexparam() == "CDATA")
3248                                         ofs << "]]>";
3249                                 else
3250                                         sgmlCloseTag(ofs, depth + command_depth,
3251                                                      style.latexparam());
3252                         }
3253                         break;
3254                 case LATEX_ITEM_ENVIRONMENT:
3255                         if (desc_on == 1) break;
3256                         end_tag= "para";
3257                         sgmlCloseTag(ofs, depth + 1 + command_depth, end_tag);
3258                         break;
3259                 case LATEX_PARAGRAPH:
3260                         sgmlCloseTag(ofs, depth + command_depth, style.latexname());
3261                         break;
3262                 default:
3263                         sgmlCloseTag(ofs, depth + command_depth, style.latexname());
3264                         break;
3265                 }
3266         }
3267
3268         // Close open tags
3269         for (int d = depth; d >= 0; --d) {
3270                 if (!environment_stack[depth].empty()) {
3271                         if (environment_inner[depth] != "!-- --") {
3272                                 item_name = "listitem";
3273                                 sgmlCloseTag(ofs, command_depth + depth,
3274                                              item_name);
3275                                if (environment_inner[depth] == "varlistentry")
3276                                        sgmlCloseTag(ofs, depth + command_depth,
3277                                                     environment_inner[depth]);
3278                         }
3279                         
3280                         sgmlCloseTag(ofs, depth + command_depth,
3281                                      environment_stack[depth]);
3282                 }
3283         }
3284         
3285         for (int j = command_depth; j >= 0 ; --j)
3286                 if (!command_stack[j].empty())
3287                         sgmlCloseTag(ofs, j, command_stack[j]);
3288
3289         ofs << "\n\n";
3290         sgmlCloseTag(ofs, 0, top_element);
3291
3292         ofs.close();
3293         // How to check for successful close
3294 }
3295
3296
3297 void Buffer::simpleDocBookOnePar(ostream & os, string & extra,
3298                                  Paragraph * par, int & desc_on,
3299                                  Paragraph::depth_type depth) const
3300 {
3301         bool emph_flag = false;
3302
3303         LyXLayout const & style = textclasslist.Style(params.textclass,
3304                                                       par->getLayout());
3305
3306         LyXFont font_old = style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
3307
3308         int char_line_count = depth;
3309         //if (!style.free_spacing)
3310         //      os << string(depth,' ');
3311
3312         // parsing main loop
3313         for (Paragraph::size_type i = 0;
3314              i < par->size(); ++i) {
3315                 LyXFont font = par->getFont(params, i);
3316
3317                 // handle <emphasis> tag
3318                 if (font_old.emph() != font.emph()) {
3319                         if (font.emph() == LyXFont::ON) {
3320                                 os << "<emphasis>";
3321                                 emph_flag = true;
3322                         }else if(i) {
3323                                 os << "</emphasis>";
3324                                 emph_flag = false;
3325                         }
3326                 }
3327       
3328                 char c = par->getChar(i);
3329
3330                 if (c == Paragraph::META_INSET) {
3331                         Inset * inset = par->getInset(i);
3332                         ostringstream ost;
3333                         inset->docBook(this, ost);
3334                         string tmp_out = ost.str().c_str();
3335
3336                         //
3337                         // This code needs some explanation:
3338                         // Two insets are treated specially
3339                         //   label if it is the first element in a command paragraph
3340                         //         desc_on == 3
3341                         //   graphics inside tables or figure floats can't go on
3342                         //   title (the equivalente in latex for this case is caption
3343                         //   and title should come first
3344                         //         desc_on == 4
3345                         //
3346                         if (desc_on!= 3 || i!= 0) {
3347                                 if (!tmp_out.empty() && tmp_out[0] == '@') {
3348                                         if (desc_on == 4)
3349                                                 extra += frontStrip(tmp_out, '@');
3350                                         else
3351                                                 os << frontStrip(tmp_out, '@');
3352                                 }
3353                                 else
3354                                         os << tmp_out;
3355                         }
3356                 } else {
3357                         string sgml_string;
3358                         if (par->linuxDocConvertChar(c, sgml_string)
3359                             && !style.free_spacing) { // in freespacing
3360                                                      // mode, spaces are
3361                                                      // non-breaking characters
3362                                 // char is ' '
3363                                 if (desc_on == 1) {
3364                                         ++char_line_count;
3365                                         os << "\n</term><listitem><para>";
3366                                         desc_on = 2;
3367                                 } else {
3368                                         os << c;
3369                                 }
3370                         } else {
3371                                 os << sgml_string;
3372                         }
3373                 }
3374                 font_old = font;
3375         }
3376
3377         if (emph_flag) {
3378                 os << "</emphasis>";
3379         }
3380         
3381         // resets description flag correctly
3382         if (desc_on == 1) {
3383                 // <term> not closed...
3384                 os << "</term>";
3385         }
3386         if(style.free_spacing) os << '\n';
3387 }
3388
3389
3390 // This should be enabled when the Chktex class is implemented. (Asger)
3391 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
3392 // Other flags: -wall -v0 -x
3393 int Buffer::runChktex()
3394 {
3395         if (!users->text) return 0;
3396
3397         users->owner()->prohibitInput();
3398
3399         // get LaTeX-Filename
3400         string const name = getLatexName();
3401         string path = OnlyPath(filename);
3402
3403         string const org_path = path;
3404         if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
3405                 path = tmppath;  
3406         }
3407
3408         Path p(path); // path to LaTeX file
3409         users->owner()->message(_("Running chktex..."));
3410
3411         // Remove all error insets
3412         bool const removedErrorInsets = users->removeAutoInsets();
3413
3414         // Generate the LaTeX file if neccessary
3415         makeLaTeXFile(name, org_path, false);
3416
3417         TeXErrors terr;
3418         Chktex chktex(lyxrc.chktex_command, name, filepath);
3419         int res = chktex.run(terr); // run chktex
3420
3421         if (res == -1) {
3422                 WriteAlert(_("chktex did not work!"),
3423                            _("Could not run with file:"), name);
3424         } else if (res > 0) {
3425                 // Insert all errors as errors boxes
3426                 users->insertErrors(terr);
3427         }
3428
3429         // if we removed error insets before we ran chktex or if we inserted
3430         // error insets after we ran chktex, this must be run:
3431         if (removedErrorInsets || res){
3432                 users->redraw();
3433                 users->fitCursor();
3434         }
3435         users->owner()->allowInput();
3436
3437         return res;
3438 }
3439
3440
3441 void Buffer::validate(LaTeXFeatures & features) const
3442 {
3443         Paragraph * par = paragraph;
3444         LyXTextClass const & tclass = 
3445                 textclasslist.TextClass(params.textclass);
3446     
3447         // AMS Style is at document level
3448         features.amsstyle = (params.use_amsmath ||
3449                              tclass.provides(LyXTextClass::amsmath));
3450     
3451         while (par) {
3452                 // We don't use "lyxerr.debug" because of speed. (Asger)
3453                 if (lyxerr.debugging(Debug::LATEX))
3454                         lyxerr << "Paragraph: " <<  par << endl;
3455
3456                 // Now just follow the list of paragraphs and run
3457                 // validate on each of them.
3458                 par->validate(features);
3459
3460                 // and then the next paragraph
3461                 par = par->next();
3462         }
3463
3464         // the bullet shapes are buffer level not paragraph level
3465         // so they are tested here
3466         for (int i = 0; i < 4; ++i) {
3467                 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
3468                         int const font = params.user_defined_bullets[i].getFont();
3469                         if (font == 0) {
3470                                 int const c = params
3471                                         .user_defined_bullets[i]
3472                                         .getCharacter();
3473                                 if (c == 16
3474                                    || c == 17
3475                                    || c == 25
3476                                    || c == 26
3477                                    || c == 31) {
3478                                         features.latexsym = true;
3479                                 }
3480                         } else if (font == 1) {
3481                                 features.amssymb = true;
3482                         } else if ((font >= 2 && font <= 5)) {
3483                                 features.pifont = true;
3484                         }
3485                 }
3486         }
3487         
3488         if (lyxerr.debugging(Debug::LATEX)) {
3489                 features.showStruct();
3490         }
3491 }
3492
3493
3494 void Buffer::setPaperStuff()
3495 {
3496         params.papersize = BufferParams::PAPER_DEFAULT;
3497         char const c1 = params.paperpackage;
3498         if (c1 == BufferParams::PACKAGE_NONE) {
3499                 char const c2 = params.papersize2;
3500                 if (c2 == BufferParams::VM_PAPER_USLETTER)
3501                         params.papersize = BufferParams::PAPER_USLETTER;
3502                 else if (c2 == BufferParams::VM_PAPER_USLEGAL)
3503                         params.papersize = BufferParams::PAPER_LEGALPAPER;
3504                 else if (c2 == BufferParams::VM_PAPER_USEXECUTIVE)
3505                         params.papersize = BufferParams::PAPER_EXECUTIVEPAPER;
3506                 else if (c2 == BufferParams::VM_PAPER_A3)
3507                         params.papersize = BufferParams::PAPER_A3PAPER;
3508                 else if (c2 == BufferParams::VM_PAPER_A4)
3509                         params.papersize = BufferParams::PAPER_A4PAPER;
3510                 else if (c2 == BufferParams::VM_PAPER_A5)
3511                         params.papersize = BufferParams::PAPER_A5PAPER;
3512                 else if ((c2 == BufferParams::VM_PAPER_B3) || (c2 == BufferParams::VM_PAPER_B4) ||
3513                          (c2 == BufferParams::VM_PAPER_B5))
3514                         params.papersize = BufferParams::PAPER_B5PAPER;
3515         } else if ((c1 == BufferParams::PACKAGE_A4) || (c1 == BufferParams::PACKAGE_A4WIDE) ||
3516                    (c1 == BufferParams::PACKAGE_WIDEMARGINSA4))
3517                 params.papersize = BufferParams::PAPER_A4PAPER;
3518 }
3519
3520
3521 // This function should be in Buffer because it's a buffer's property (ale)
3522 string const Buffer::getIncludeonlyList(char delim)
3523 {
3524         string lst;
3525         for (inset_iterator it = inset_iterator_begin();
3526             it != inset_iterator_end(); ++it) {
3527                 if ((*it)->lyxCode() == Inset::INCLUDE_CODE) {
3528                         InsetInclude * insetinc = 
3529                                 static_cast<InsetInclude *>(*it);
3530                         if (insetinc->isIncludeOnly()) {
3531                                 if (!lst.empty())
3532                                         lst += delim;
3533                                 lst += insetinc->getRelFileBaseName();
3534                         }
3535                 }
3536         }
3537         lyxerr[Debug::INFO] << "Includeonly(" << lst << ')' << endl;
3538         return lst;
3539 }
3540
3541
3542 vector<string> const Buffer::getLabelList()
3543 {
3544         /// if this is a child document and the parent is already loaded
3545         /// Use the parent's list instead  [ale990407]
3546         if (!params.parentname.empty()
3547             && bufferlist.exists(params.parentname)) {
3548                 Buffer * tmp = bufferlist.getBuffer(params.parentname);
3549                 if (tmp)
3550                         return tmp->getLabelList();
3551         }
3552
3553         vector<string> label_list;
3554         for (inset_iterator it = inset_iterator_begin();
3555              it != inset_iterator_end(); ++it) {
3556                 vector<string> const l = (*it)->getLabelList();
3557                 label_list.insert(label_list.end(), l.begin(), l.end());
3558         }
3559         return label_list;
3560 }
3561
3562
3563 Buffer::Lists const Buffer::getLists() const
3564 {
3565         Lists l;
3566         Paragraph * par = paragraph;
3567         bool found;
3568         LyXTextClassList::size_type cap;
3569         boost::tie(found, cap) = textclasslist
3570                 .NumberOfLayout(params.textclass, "Caption");
3571
3572         while (par) {
3573                 char const labeltype =
3574                         textclasslist.Style(params.textclass, 
3575                                             par->getLayout()).labeltype;
3576                 
3577                 if (labeltype >= LABEL_COUNTER_CHAPTER
3578                     && labeltype <= LABEL_COUNTER_CHAPTER + params.tocdepth) {
3579                                 // insert this into the table of contents
3580                         SingleList & item = l["TOC"];
3581                         int depth = max(0,
3582                                         labeltype - 
3583                                         textclasslist.TextClass(params.textclass).maxcounter());
3584                         item.push_back(TocItem(par, depth, par->asString(this, true)));
3585                 }
3586                 // For each paragrph, traverse its insets and look for
3587                 // FLOAT_CODE
3588                 
3589                 if (found) {
3590                         Paragraph::inset_iterator it =
3591                                 par->inset_iterator_begin();
3592                         Paragraph::inset_iterator end =
3593                                 par->inset_iterator_end();
3594                         
3595                         for (; it != end; ++it) {
3596                                 if ((*it)->lyxCode() == Inset::FLOAT_CODE) {
3597                                         InsetFloat * il =
3598                                                 static_cast<InsetFloat*>(*it);
3599                                         
3600                                         string const type = il->type();
3601                                         
3602                                         // Now find the caption in the float...
3603                                         // We now tranverse the paragraphs of
3604                                         // the inset...
3605                                         Paragraph * tmp = il->inset.paragraph();
3606                                         while (tmp) {
3607                                                 if (tmp->layout == cap) {
3608                                                         SingleList & item = l[type];
3609                                                         string const str =
3610                                                                 tostr(item.size()+1) + ". " + tmp->asString(this, false);
3611                                                         item.push_back(TocItem(tmp, 0 , str));
3612                                                 }
3613                                                 tmp = tmp->next();
3614                                         }
3615                                 }
3616                         }
3617                 } else {
3618                         lyxerr << "caption not found" << endl;
3619                 }
3620                 
3621                 par = par->next();
3622         }
3623         return l;
3624 }
3625
3626
3627 // This is also a buffer property (ale)
3628 vector<pair<string, string> > const Buffer::getBibkeyList()
3629 {
3630         /// if this is a child document and the parent is already loaded
3631         /// Use the parent's list instead  [ale990412]
3632         if (!params.parentname.empty() && bufferlist.exists(params.parentname)) {
3633                 Buffer * tmp = bufferlist.getBuffer(params.parentname);
3634                 if (tmp)
3635                         return tmp->getBibkeyList();
3636         }
3637
3638         vector<pair<string, string> > keys;
3639         Paragraph * par = paragraph;
3640         while (par) {
3641                 if (par->bibkey)
3642                         keys.push_back(pair<string, string>(par->bibkey->getContents(),
3643                                                            par->asString(this, false)));
3644                 par = par->next();
3645         }
3646
3647         // Might be either using bibtex or a child has bibliography
3648         if (keys.empty()) {
3649                 for (inset_iterator it = inset_iterator_begin();
3650                         it != inset_iterator_end(); ++it) {
3651                         // Search for Bibtex or Include inset
3652                         if ((*it)->lyxCode() == Inset::BIBTEX_CODE) {
3653                                 vector<pair<string,string> > tmp =
3654                                         static_cast<InsetBibtex*>(*it)->getKeys(this);
3655                                 keys.insert(keys.end(), tmp.begin(), tmp.end());
3656                         } else if ((*it)->lyxCode() == Inset::INCLUDE_CODE) {
3657                                 vector<pair<string,string> > const tmp =
3658                                         static_cast<InsetInclude*>(*it)->getKeys();
3659                                 keys.insert(keys.end(), tmp.begin(), tmp.end());
3660                         }
3661                 }
3662         }
3663  
3664         return keys;
3665 }
3666
3667
3668 bool Buffer::isDepClean(string const & name) const
3669 {
3670         DEPCLEAN * item = dep_clean;
3671         while (item && item->master != name)
3672                 item = item->next;
3673         if (!item) return true;
3674         return item->clean;
3675 }
3676
3677
3678 void Buffer::markDepClean(string const & name)
3679 {
3680         if (!dep_clean) {
3681                 dep_clean = new DEPCLEAN;
3682                 dep_clean->clean = true;
3683                 dep_clean->master = name;
3684                 dep_clean->next = 0;
3685         } else {
3686                 DEPCLEAN * item = dep_clean;
3687                 while (item && item->master != name)
3688                         item = item->next;
3689                 if (item) {
3690                         item->clean = true;
3691                 } else {
3692                         item = new DEPCLEAN;
3693                         item->clean = true;
3694                         item->master = name;
3695                         item->next = 0;
3696                 }
3697         }
3698 }
3699
3700
3701 bool Buffer::dispatch(string const & command)
3702 {
3703         // Split command string into command and argument
3704         string cmd;
3705         string line = frontStrip(command);
3706         string const arg = strip(frontStrip(split(line, cmd, ' ')));
3707
3708         return dispatch(lyxaction.LookupFunc(cmd), arg);
3709 }
3710
3711
3712 bool Buffer::dispatch(int action, string const & argument)
3713 {
3714         bool dispatched = true;
3715         switch (action) {
3716                 case LFUN_EXPORT: 
3717                         Exporter::Export(this, argument, false);
3718                         break;
3719
3720                 default:
3721                         dispatched = false;
3722         }
3723         return dispatched;
3724 }
3725
3726
3727 void Buffer::resizeInsets(BufferView * bv)
3728 {
3729         /// then remove all LyXText in text-insets
3730         Paragraph * par = paragraph;
3731         for (; par; par = par->next()) {
3732             par->resizeInsetsLyXText(bv);
3733         }
3734 }
3735
3736
3737 void Buffer::redraw()
3738 {
3739         users->redraw(); 
3740         users->fitCursor(); 
3741 }
3742
3743
3744 void Buffer::changeLanguage(Language const * from, Language const * to)
3745 {
3746
3747         Paragraph * par = paragraph;
3748         while (par) {
3749                 par->changeLanguage(params, from, to);
3750                 par = par->next();
3751         }
3752 }
3753
3754
3755 bool Buffer::isMultiLingual()
3756 {
3757         Paragraph * par = paragraph;
3758         while (par) {
3759                 if (par->isMultiLingual(params))
3760                         return true;
3761                 par = par->next();
3762         }
3763         return false;
3764 }
3765
3766
3767 Buffer::inset_iterator::inset_iterator(Paragraph * paragraph,
3768                                        Paragraph::size_type pos)
3769         : par(paragraph)
3770 {
3771         it = par->InsetIterator(pos);
3772         if (it == par->inset_iterator_end()) {
3773                 par = par->next();
3774                 setParagraph();
3775         }
3776 }
3777
3778
3779 void Buffer::inset_iterator::setParagraph()
3780 {
3781         while (par) {
3782                 it = par->inset_iterator_begin();
3783                 if (it != par->inset_iterator_end())
3784                         return;
3785                 par = par->next();
3786         }
3787         //it = 0;
3788         // We maintain an invariant that whenever par = 0 then it = 0
3789 }
3790
3791
3792 Inset * Buffer::getInsetFromID(int id_arg) const
3793 {
3794         for (inset_iterator it = inset_const_iterator_begin();
3795                  it != inset_const_iterator_end(); ++it)
3796         {
3797                 if ((*it)->id() == id_arg)
3798                         return *it;
3799                 Inset * in = (*it)->getInsetFromID(id_arg);
3800                 if (in)
3801                         return in;
3802         }
3803         return 0;
3804 }
3805
3806
3807 Paragraph * Buffer::getParFromID(int id) const
3808 {
3809         if (id < 0) return 0;
3810         Paragraph * par = paragraph;
3811         while (par) {
3812                 if (par->id() == id) {
3813                         return par;
3814                 }
3815                 Paragraph * tmp = par->getParFromID(id);
3816                 if (tmp) {
3817                         return tmp;
3818                 }
3819                 par = par->next();
3820         }
3821         return 0;
3822 }