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