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