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