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