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