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