]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
e1833d2248a9bcd40869a11219ffa0441ec264f3
[lyx.git] / src / paragraph.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2000 The LyX Team. 
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation "lyxparagraph.h"
15 #endif
16
17 #include <algorithm>
18 #include <fstream>
19
20 #include "lyxparagraph.h"
21 #include "support/textutils.h"
22 #include "lyxrc.h"
23 #include "layout.h"
24 #include "tex-strings.h"
25 #include "bufferparams.h"
26 #include "support/FileInfo.h"
27 #include "support/LAssert.h"
28 #include "debug.h"
29 #include "LaTeXFeatures.h"
30 #include "insets/insetinclude.h"
31 #include "insets/insetbib.h"
32 #include "support/filetools.h"
33 #include "lyx_gui_misc.h"
34 #include "texrow.h"
35 #include "support/lyxmanip.h"
36
37 using std::ostream;
38 using std::endl;
39 using std::fstream;
40 using std::ios;
41 using std::lower_bound;
42 using std::upper_bound;
43
44 int tex_code_break_column = 72;  // needs non-zero initialization. set later.
45 // this is a bad idea, but how can LyXParagraph find its buffer to get
46 // parameters? (JMarc)
47 extern BufferView * current_view;
48
49
50 // ale970405
51 extern string bibitemWidthest(Painter &);
52
53 // this is a minibuffer
54 static char minibuffer_char;
55 static LyXFont minibuffer_font;
56 static Inset * minibuffer_inset;
57
58
59 // Initialization of the counter for the paragraph id's,
60 // declared in lyxparagraph.h
61 unsigned int LyXParagraph::paragraph_id = 0;
62
63
64 LyXParagraph::LyXParagraph()
65 {
66         text.reserve(500); // is this number too big?
67
68         for (int i = 0; i < 10; ++i) setCounter(i , 0);
69         appendix = false;
70         enumdepth = 0;
71         itemdepth = 0;
72         next = 0;
73         previous = 0;
74         footnoteflag = LyXParagraph::NO_FOOTNOTE;
75         footnotekind = LyXParagraph::FOOTNOTE; // should not be needed
76         
77         align = LYX_ALIGN_BLOCK;
78
79         /* table stuff -- begin*/ 
80         table = 0;
81         /* table stuff -- end*/ 
82         inset_owner = 0;
83         id_ = paragraph_id++;
84         bibkey = 0; // ale970302
85         Clear();
86 }
87
88
89 // This konstruktor inserts the new paragraph in a list.
90 LyXParagraph::LyXParagraph(LyXParagraph * par)
91 {
92         text.reserve(500);
93         par->text.resize(par->text.size());
94
95         for (int i = 0; i < 10; ++i) setCounter(i, 0);
96         appendix = false;
97         enumdepth = 0;
98         itemdepth = 0;
99         // double linked list begin
100         next = par->next;
101         if (next)
102                 next->previous = this;
103         previous = par;
104         previous->next = this;
105         // end
106         footnoteflag = LyXParagraph::NO_FOOTNOTE;
107         footnotekind = LyXParagraph::FOOTNOTE;
108         
109         /* table stuff -- begin*/ 
110         table = 0;
111         /* table stuff -- end*/ 
112         inset_owner = 0;
113         id_ = paragraph_id++;
114
115         bibkey = 0; // ale970302        
116     
117         Clear();
118 }
119
120
121 void LyXParagraph::writeFile(ostream & os, BufferParams const & params,
122                              char footflag, char dth) const
123 {
124         LyXFont font1, font2;
125         int column = 0;
126         int h = 0;
127         char c = 0;
128
129         if (footnoteflag != LyXParagraph::NO_FOOTNOTE
130             || !previous
131             || previous->footnoteflag == LyXParagraph::NO_FOOTNOTE){
132                 
133                 // The beginning or the end of a footnote environment?
134                 if (footflag != footnoteflag) {
135                         footflag = footnoteflag;
136                         if (footflag) {
137                                 os << "\n\\begin_float "
138                                    << string_footnotekinds[footnotekind]
139                                    << " ";
140                         } else {
141                                 os << "\n\\end_float ";
142                         }
143                 }
144
145                 // The beginning or end of a deeper (i.e. nested) area?
146                 if (dth != depth) {
147                         if (depth > dth) {
148                                 while (depth > dth) {
149                                         os << "\n\\begin_deeper ";
150                                         ++dth;
151                                 }
152                         } else {
153                                 while (depth < dth) {
154                                         os << "\n\\end_deeper ";
155                                         --dth;
156                                 }
157                         }
158                 }
159
160                 // First write the layout
161                 os << "\n\\layout "
162                    << textclasslist.NameOfLayout(params.textclass, layout)
163                    << "\n";
164
165                 // Maybe some vertical spaces.
166                 if (added_space_top.kind() != VSpace::NONE)
167                         os << "\\added_space_top "
168                            << added_space_top.asLyXCommand() << " ";
169                 if (added_space_bottom.kind() != VSpace::NONE)
170                         os << "\\added_space_bottom "
171                            << added_space_bottom.asLyXCommand() << " ";
172
173                 // Maybe the paragraph has special spacing
174                 spacing.writeFile(os, true);
175                 
176                 // The labelwidth string used in lists.
177                 if (!labelwidthstring.empty())
178                         os << "\\labelwidthstring "
179                            << labelwidthstring << '\n';
180
181                 // Lines above or below?
182                 if (line_top)
183                         os << "\\line_top ";
184                 if (line_bottom)
185                         os << "\\line_bottom ";
186
187                 // Pagebreaks above or below?
188                 if (pagebreak_top)
189                         os << "\\pagebreak_top ";
190                 if (pagebreak_bottom)
191                         os << "\\pagebreak_bottom ";
192                         
193                 // Start of appendix?
194                 if (start_of_appendix)
195                         os << "\\start_of_appendix ";
196
197                 // Noindent?
198                 if (noindent)
199                         os << "\\noindent ";
200                         
201                 // Alignment?
202                 if (align != LYX_ALIGN_LAYOUT) {
203                         switch (align) {
204                         case LYX_ALIGN_LEFT: h = 1; break;
205                         case LYX_ALIGN_RIGHT: h = 2; break;
206                         case LYX_ALIGN_CENTER: h = 3; break;
207                         default: h = 0; break;
208                         }
209                         os << "\\align " << string_align[h] << " ";
210                 }
211                 if (pextra_type != PEXTRA_NONE) {
212                         os << "\\pextra_type " << pextra_type;
213                         if (pextra_type == PEXTRA_MINIPAGE) {
214                                 os << " \\pextra_alignment "
215                                    << pextra_alignment;
216                                 if (pextra_hfill)
217                                         os << " \\pextra_hfill "
218                                            << pextra_hfill;
219                                 if (pextra_start_minipage)
220                                         os << " \\pextra_start_minipage "
221                                            << pextra_start_minipage;
222                         }
223                         if (!pextra_width.empty()) {
224                                 os << " \\pextra_width "
225                                    << VSpace(pextra_width).asLyXCommand();
226                         } else if (!pextra_widthp.empty()) {
227                                 os << " \\pextra_widthp "
228                                    << pextra_widthp;
229                         }
230                         os << '\n';
231                 }
232         } else {
233                 // Dummy layout. This means that a footnote ended.
234                 os << "\n\\end_float ";
235                 footflag = LyXParagraph::NO_FOOTNOTE;
236         }
237                 
238         // It might be a table.
239         if (table){
240                 os << "\\LyXTable\n";
241                 table->Write(os);
242         }
243
244         // bibitem  ale970302
245         if (bibkey)
246                 bibkey->Write(os);
247
248         font1 = LyXFont(LyXFont::ALL_INHERIT,params.language_info);
249
250         column = 0;
251         for (size_type i = 0; i < size(); ++i) {
252                 if (!i) {
253                         os << "\n";
254                         column = 0;
255                 }
256                 
257                 // Write font changes
258                 font2 = GetFontSettings(i);
259                 if (font2 != font1) {
260                         font2.lyxWriteChanges(font1, os);
261                         column = 0;
262                         font1 = font2;
263                 }
264
265                 c = GetChar(i);
266                 switch (c) {
267                 case META_INSET:
268                 {
269                         Inset const * inset = GetInset(i);
270                         if (inset)
271                                 if (inset->DirectWrite()) {
272                                         // international char, let it write
273                                         // code directly so it's shorter in
274                                         // the file
275                                         inset->Write(os);
276                                 } else {
277                                         os << "\n\\begin_inset ";
278                                         inset->Write(os);
279                                         os << "\n\\end_inset \n\n";
280                                         column = 0;
281                                 }
282                 }
283                 break;
284                 case META_NEWLINE: 
285                         os << "\n\\newline \n";
286                         column = 0;
287                         break;
288                 case META_HFILL: 
289                         os << "\n\\hfill \n";
290                         column = 0;
291                         break;
292                 case '\\':
293                         os << "\n\\backslash \n";
294                         column = 0;
295                         break;
296                 case '.':
297                         if (i + 1 < size() && GetChar(i + 1) == ' ') {
298                                 os << ".\n";
299                                 column = 0;
300                         } else
301                                 os << ".";
302                         break;
303                 default:
304                         if ((column > 70 && c == ' ')
305                             || column > 79) {
306                                 os << "\n";
307                                 column = 0;
308                         }
309                         // this check is to amend a bug. LyX sometimes
310                         // inserts '\0' this could cause problems.
311                         if (c != '\0')
312                                 os << c;
313                         else
314                                 lyxerr << "ERROR (LyXParagraph::writeFile):"
315                                         " NULL char in structure." << endl;
316                         ++column;
317                         break;
318                 }
319         }
320
321         // now write the next paragraph
322         if (next)
323                 next->writeFile(os, params, footflag, dth);
324 }
325
326
327 void LyXParagraph::validate(LaTeXFeatures & features) const
328 {
329         BufferParams const & params = features.bufferParams();
330         
331         // this will be useful later
332         LyXLayout const & layout =
333                 textclasslist.Style(params.textclass, 
334                                     GetLayout());
335         
336         // check the params.
337         if (line_top || line_bottom)
338                 features.lyxline = true;
339         if (!spacing.isDefault())
340                 features.setspace = true;
341         
342         // then the layouts
343         features.layout[GetLayout()] = true;
344
345         // then the fonts
346         Language const * doc_language = params.language_info;
347         
348         for (FontList::const_iterator cit = fontlist.begin();
349              cit != fontlist.end(); ++cit) {
350                 if ((*cit).font.noun() == LyXFont::ON) {
351                         lyxerr[Debug::LATEX] << "font.noun: "
352                                              << (*cit).font.noun()
353                                              << endl;
354                         features.noun = true;
355                         lyxerr[Debug::LATEX] << "Noun enabled. Font: "
356                                              << (*cit).font.stateText()
357                                              << endl;
358                 }
359                 switch ((*cit).font.color()) {
360                 case LColor::none:
361                 case LColor::inherit:
362                 case LColor::ignore:
363                         break;
364                 default:
365                         features.color = true;
366                         lyxerr[Debug::LATEX] << "Color enabled. Font: "
367                                              << (*cit).font.stateText()
368                                              << endl;
369                 }
370                 Language const * language = (*cit).font.language();
371                 if (language != doc_language && language != default_language) {
372                         features.UsedLanguages.insert(language);
373                         lyxerr[Debug::LATEX] << "Found language "
374                                              << language->lang << endl;
375                 }               
376         }
377
378         // then the insets
379         for (InsetList::const_iterator cit = insetlist.begin();
380              cit != insetlist.end(); ++cit) {
381                 if ((*cit).inset)
382                         (*cit).inset->Validate(features);
383         }
384
385         if (table && table->IsLongTable())
386                 features.longtable = true;
387         if (pextra_type == PEXTRA_INDENT)
388                 features.LyXParagraphIndent = true;
389         if (pextra_type == PEXTRA_FLOATFLT)
390                 features.floatflt = true;
391         if (layout.needprotect 
392             && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
393                 features.NeedLyXFootnoteCode = true;
394         if (params.paragraph_separation == BufferParams::PARSEP_INDENT
395             && pextra_type == LyXParagraph::PEXTRA_MINIPAGE)
396                 features.NeedLyXMinipageIndent = true;
397         if (table && table->NeedRotating())
398                 features.rotating = true;
399         if (footnoteflag != NO_FOOTNOTE && footnotekind == ALGORITHM)
400                 features.algorithm = true;
401 }
402
403
404 // First few functions needed for cut and paste and paragraph breaking.
405 void LyXParagraph::CopyIntoMinibuffer(LyXParagraph::size_type pos) const
406 {
407         minibuffer_char = GetChar(pos);
408         minibuffer_font = GetFontSettings(pos);
409         minibuffer_inset = 0;
410         if (minibuffer_char == LyXParagraph::META_INSET) {
411                 if (GetInset(pos)) {
412                         minibuffer_inset = GetInset(pos)->Clone();
413                 } else {
414                         minibuffer_inset = 0;
415                         minibuffer_char = ' ';
416                         // This reflects what GetInset() does (ARRae)
417                 }
418         }
419 }
420
421
422 void LyXParagraph::CutIntoMinibuffer(LyXParagraph::size_type pos)
423 {
424         minibuffer_char = GetChar(pos);
425         minibuffer_font = GetFontSettings(pos);
426         minibuffer_inset = 0;
427         if (minibuffer_char == LyXParagraph::META_INSET) {
428                 if (GetInset(pos)) {
429                         minibuffer_inset = GetInset(pos);
430                         // This is a little hack since I want exactly
431                         // the inset, not just a clone. Otherwise
432                         // the inset would be deleted when calling Erase(pos)
433                         // find the entry
434                         InsetList::iterator it = lower_bound(insetlist.begin(),
435                                                              insetlist.end(),
436                                                              pos, matchIT());
437                         if (it != insetlist.end() && (*it).pos == pos)
438                                 (*it).inset = 0;
439                 } else {
440                         minibuffer_inset = 0;
441                         minibuffer_char = ' ';
442                         // This reflects what GetInset() does (ARRae)
443                 }
444
445         }
446
447         // Erase(pos); now the caller is responsible for that.
448 }
449
450
451 bool LyXParagraph::InsertFromMinibuffer(LyXParagraph::size_type pos)
452 {
453         if ((minibuffer_char == LyXParagraph::META_INSET) &&
454             !InsertInsetAllowed(minibuffer_inset))
455                 return false;
456         InsertChar(pos, minibuffer_char);
457         SetFont(pos, minibuffer_font);
458         if (minibuffer_char == LyXParagraph::META_INSET)
459                 InsertInset(pos, minibuffer_inset);
460         return true;
461 }
462
463 // end of minibuffer
464
465
466
467 void LyXParagraph::Clear()
468 {
469         line_top = false;
470         line_bottom = false;
471    
472         pagebreak_top = false;
473         pagebreak_bottom = false;
474
475         added_space_top = VSpace(VSpace::NONE);
476         added_space_bottom = VSpace(VSpace::NONE);
477         spacing.set(Spacing::Default);
478         
479         align = LYX_ALIGN_LAYOUT;
480         depth = 0;
481         noindent = false;
482
483         pextra_type = PEXTRA_NONE;
484         pextra_width.erase();
485         pextra_widthp.erase();
486         pextra_alignment = MINIPAGE_ALIGN_TOP;
487         pextra_hfill = false;
488         pextra_start_minipage = false;
489
490         labelstring.erase();
491         labelwidthstring.erase();
492         layout = 0;
493         bibkey = 0;
494         
495         start_of_appendix = false;
496 }
497
498
499 // the destructor removes the new paragraph from the list
500 LyXParagraph::~LyXParagraph()
501 {
502         if (previous)
503                 previous->next = next;
504         if (next)
505                 next->previous = previous;
506
507         for (InsetList::iterator it = insetlist.begin();
508              it != insetlist.end(); ++it) {
509                 delete (*it).inset;
510         }
511
512         /* table stuff -- begin*/ 
513         delete table;
514         /* table stuff -- end*/ 
515
516         // ale970302
517         delete bibkey;
518         //
519         //lyxerr << "LyXParagraph::paragraph_id = "
520         //       << LyXParagraph::paragraph_id << endl;
521 }
522
523
524 void LyXParagraph::Erase(LyXParagraph::size_type pos)
525 {
526         // > because last is the next unused position, and you can 
527         // use it if you want
528         if (pos > size()) {
529                 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) 
530                         NextAfterFootnote()->Erase(pos - text.size() - 1);
531                 else 
532                         lyxerr.debug() << "ERROR (LyXParagraph::Erase): "
533                                 "position does not exist." << endl;
534                 return;
535         }
536         if (pos < size()) { // last is free for insertation, but should be empty
537                 // if it is an inset, delete the inset entry 
538                 if (text[pos] == LyXParagraph::META_INSET) {
539                         // find the entry
540                         InsetList::iterator it = lower_bound(insetlist.begin(),
541                                                              insetlist.end(),
542                                                              pos, matchIT());
543                         if (it != insetlist.end() && (*it).pos == pos) {
544                                 delete (*it).inset;
545                                 insetlist.erase(it);
546                         }
547                 }
548                 text.erase(text.begin() + pos);
549                 // Erase entries in the tables.
550                 for (FontList::iterator it = fontlist.begin();
551                      it != fontlist.end(); ++it) {
552                         if (pos >= (*it).pos && pos <= (*it).pos_end) {
553                                 if ((*it).pos == (*it).pos_end) {
554                                         // If it is a multi-character font
555                                         // entry, we just make it smaller
556                                         // (see update below), otherwise we
557                                         // should delete it.
558                                         fontlist.erase(it);
559                                         break;
560                                 }
561                         }
562                 }
563                 // Update all other entries.
564                 for (FontList::iterator it = fontlist.begin();
565                      it != fontlist.end(); ++it) {
566                         if ((*it).pos > pos)
567                                 (*it).pos--;
568                         if ((*it).pos_end >= pos)
569                                 (*it).pos_end--;
570                 }
571
572                 // Update the inset table.
573                 for (InsetList::iterator it = upper_bound(insetlist.begin(),
574                                                           insetlist.end(),
575                                                           pos, matchIT());
576                      it != insetlist.end(); ++it)
577                         --(*it).pos;
578         } else {
579                 lyxerr << "ERROR (LyXParagraph::Erase): "
580                         "can't erase non-existant char." << endl;
581         }
582 }
583
584
585 void LyXParagraph::InsertChar(LyXParagraph::size_type pos, char c)
586 {
587         // > because last is the next unused position, and you can 
588         // use it if you want
589         if (pos > size()) {
590                 if (next
591                     && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) 
592                         NextAfterFootnote()->InsertChar(pos - text.size() - 1,
593                                                         c);
594                 else 
595                         lyxerr.debug() << "ERROR (LyXParagraph::InsertChar): "
596                                 "position does not exist." << endl;
597                 return;
598         }
599         text.insert(text.begin() + pos, c);
600         // Update the font table.
601         for (FontList::iterator it = fontlist.begin();
602              it != fontlist.end(); ++it) {
603                 if ((*it).pos >= pos)
604                         (*it).pos++;
605                 if ((*it).pos_end >= pos)
606                         (*it).pos_end++;
607         }
608    
609         // Update the inset table.
610         for (InsetList::iterator it = lower_bound(insetlist.begin(),
611                                                   insetlist.end(),
612                                                   pos, matchIT());
613              it != insetlist.end(); ++it)
614                 ++(*it).pos;
615 }
616
617
618 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
619                                Inset * inset)
620 {
621         // > because last is the next unused position, and you can 
622         // use it if you want
623         if (pos > size()) {
624                 if (next
625                     && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) 
626                         NextAfterFootnote()
627                                 ->InsertInset(pos - text.size() - 1, inset);
628                 else 
629                         lyxerr << "ERROR (LyXParagraph::InsertInset): " 
630                                 "position does not exist: " << pos << endl;
631                 return;
632         }
633         if (text[pos] != LyXParagraph::META_INSET) {
634                 lyxerr << "ERROR (LyXParagraph::InsertInset): "
635                         "there is no LyXParagraph::META_INSET" << endl;
636                 return;
637         }
638
639         if (inset) {
640                 // Add a new entry in the inset table.
641                 InsetList::iterator it = lower_bound(insetlist.begin(),
642                                                      insetlist.end(),
643                                                      pos, matchIT());
644                 if (it != insetlist.end() && (*it).pos == pos)
645                         lyxerr << "ERROR (LyXParagraph::InsertInset): "
646                                 "there is an inset in position: " << pos << endl;
647                 else
648                         insetlist.insert(it, InsetTable(pos,inset));
649                 if (inset_owner)
650                         inset->setOwner(inset_owner);
651         }
652 }
653
654
655 bool LyXParagraph::InsertInsetAllowed(Inset *inset)
656 {
657         if (inset_owner) {
658                 printf("CODE:%d\n",inset->LyxCode());
659                 return inset_owner->InsertInsetAllowed(inset);
660         }
661         return true;
662 }
663
664 Inset * LyXParagraph::GetInset(LyXParagraph::size_type pos)
665 {
666         if (pos >= size()) {
667                 if (next
668                     && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) 
669                         return NextAfterFootnote()
670                                 ->GetInset(pos - text.size() - 1);
671                 else { 
672                         lyxerr << "ERROR (LyXParagraph::GetInset): "
673                                 "position does not exist: "
674                                << pos << endl;
675                 }
676                 return 0;
677         }
678         // Find the inset.
679         InsetList::iterator it = lower_bound(insetlist.begin(),
680                                              insetlist.end(),
681                                              pos, matchIT());
682         if (it != insetlist.end() && (*it).pos == pos)
683                 return (*it).inset;
684
685         lyxerr << "ERROR (LyXParagraph::GetInset): "
686                 "Inset does not exist: " << pos << endl;
687         // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
688         // Did this commenting out introduce a bug? So far I have not
689         // see any, please enlighten me. (Lgb)
690         // My guess is that since the inset does not exist, we might
691         // as well replace it with a space to prevent craches. (Asger)
692         return 0;
693 }
694
695
696 Inset const * LyXParagraph::GetInset(LyXParagraph::size_type pos) const
697 {
698         if (pos >= size()) {
699                 if (next
700                     && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) 
701                         return NextAfterFootnote()
702                                 ->GetInset(pos - text.size() - 1);
703                 else { 
704                         lyxerr << "ERROR (LyXParagraph::GetInset): "
705                                 "position does not exist: "
706                                << pos << endl;
707                 }
708                 return 0;
709         }
710         // Find the inset.
711         InsetList::const_iterator cit = lower_bound(insetlist.begin(),
712                                                     insetlist.end(),
713                                                     pos, matchIT());
714         if (cit != insetlist.end() && (*cit).pos == pos)
715                 return (*cit).inset;
716
717         lyxerr << "ERROR (LyXParagraph::GetInset): "
718                 "Inset does not exist: " << pos << endl;
719         //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
720         // Did this commenting out introduce a bug? So far I have not
721         // see any, please enlighten me. (Lgb)
722         // My guess is that since the inset does not exist, we might
723         // as well replace it with a space to prevent craches. (Asger)
724         return 0;
725 }
726
727
728 // Gets uninstantiated font setting at position.
729 // Optimized after profiling. (Asger)
730 LyXFont LyXParagraph::GetFontSettings(LyXParagraph::size_type pos) const
731 {
732         if (pos < size()) {
733 #ifdef SORTED_FONT_LIST
734                 for (FontList::const_iterator cit = fontlist.begin();
735                      cit != fontlist.end() && pos <= (*cit).pos_end; ++cit)
736                         if (pos >= (*cit).pos)
737                                 return (*cit).font;
738 #else
739                 for (FontList::const_iterator cit = fontlist.begin();
740                      cit != fontlist.end(); ++cit)
741                         if (pos >= (*cit).pos && pos <= (*cit).pos_end)
742                                 return (*cit).font;
743 #endif
744         }
745         // > because last is the next unused position, and you can 
746         // use it if you want
747         else if (pos > size()) {
748                 if (next
749                     && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) 
750                         return NextAfterFootnote()
751                                 ->GetFontSettings(pos - text.size() - 1);
752                 else {
753                         // Why is it an error to ask for the font of a
754                         // position that does not exist? Would it be
755                         // enough for this to be enabled on debug?
756                         // We want strict error checking, but it's ok to only
757                         // have it when debugging. (Asger)
758                         lyxerr << "ERROR (LyXParagraph::GetFontSettings): "
759                                 "position does not exist. "
760                                << pos << " (" << static_cast<int>(pos)
761                                << ")" << endl;
762                 }
763         } else if (pos > 0) {
764                 return GetFontSettings(pos - 1);
765         } else // pos = size() = 0
766                 return LyXFont(LyXFont::ALL_INHERIT,getParLanguage());
767
768         return LyXFont(LyXFont::ALL_INHERIT);
769 }
770
771 // Gets uninstantiated font setting at position 0
772 LyXFont LyXParagraph::GetFirstFontSettings() const
773 {
774         if (size() > 0) {
775 #ifdef SORTED_FONT_LIST
776                 if (!fontlist.empty() && fontlist.front().pos == 0)
777                         return fontlist.front().font;
778 #else
779                 for (FontList::const_iterator cit = fontlist.begin();
780                      cit != fontlist.end(); ++cit)
781                         if (0 >= (*cit).pos && 0 <= (*cit).pos_end)
782                                 return (*cit).font;
783 #endif
784         } else if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) 
785                 return NextAfterFootnote()->GetFirstFontSettings();
786         return LyXFont(LyXFont::ALL_INHERIT);
787 }
788
789 // Gets the fully instantiated font at a given position in a paragraph
790 // This is basically the same function as LyXText::GetFont() in text2.C.
791 // The difference is that this one is used for generating the LaTeX file,
792 // and thus cosmetic "improvements" are disallowed: This has to deliver
793 // the true picture of the buffer. (Asger)
794 // If position is -1, we get the layout font of the paragraph.
795 // If position is -2, we get the font of the manual label of the paragraph.
796 LyXFont LyXParagraph::getFont(LyXParagraph::size_type pos) const
797 {
798         LyXFont tmpfont;
799         LyXLayout const & layout =
800                 textclasslist.Style(current_view->buffer()->params.textclass, 
801                                     GetLayout());
802         LyXParagraph::size_type main_body = 0;
803         if (layout.labeltype == LABEL_MANUAL)
804                 main_body = BeginningOfMainBody();
805
806         if (pos >= 0){
807                 LyXFont layoutfont;
808                 if (pos < main_body)
809                         layoutfont = layout.labelfont;
810                 else
811                         layoutfont = layout.font;
812                 tmpfont = GetFontSettings(pos);
813                 tmpfont.realize(layoutfont);
814         } else {
815                 // process layoutfont for pos == -1 and labelfont for pos < -1
816                 if (pos == -1)
817                         tmpfont = layout.font;
818                 else
819                         tmpfont = layout.labelfont;
820                 tmpfont.setLanguage(getParLanguage());
821         }
822
823         // check for environment font information
824         char par_depth = GetDepth();
825         LyXParagraph const * par = this;
826         while (par && par_depth && !tmpfont.resolved()) {
827                 par = par->DepthHook(par_depth - 1);
828                 if (par) {
829                         tmpfont.realize(textclasslist.
830                                         Style(current_view->buffer()->params.textclass,
831                                               par->GetLayout()).font);
832                         par_depth = par->GetDepth();
833                 }
834         }
835
836         tmpfont.realize(textclasslist
837                         .TextClass(current_view->buffer()->params.textclass)
838                         .defaultfont());
839         return tmpfont;
840 }
841
842
843 /// Returns the height of the highest font in range
844 LyXFont::FONT_SIZE
845 LyXParagraph::HighestFontInRange(LyXParagraph::size_type startpos,
846                                  LyXParagraph::size_type endpos) const
847 {
848         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
849         for (FontList::const_iterator cit = fontlist.begin();
850              cit != fontlist.end(); ++cit) {
851                 if (startpos <= (*cit).pos_end && endpos >= (*cit).pos) {
852                         LyXFont::FONT_SIZE size = (*cit).font.size();
853                         if (size > maxsize && size <= LyXFont::SIZE_HUGER)
854                                 maxsize = size;
855                 }
856         }
857         return maxsize;
858 }
859
860
861 char LyXParagraph::GetChar(LyXParagraph::size_type pos)
862 {
863         Assert(pos >= 0);
864
865         if (pos < size()) {
866                 return text[pos];
867         }
868         // > because last is the next unused position, and you can 
869         // use it if you want
870         else if (pos > size()) {
871                 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) 
872                         return NextAfterFootnote()
873                                 ->GetChar(pos - text.size() - 1);
874                 else {
875                         lyxerr << "ERROR (LyXParagraph::GetChar): "
876                                 "position does not exist."
877                                << pos << " (" << static_cast<int>(pos)
878                                << ")\n";
879                         // Assert(false); // This triggers sometimes...
880                         // Why?
881                 }
882                 return '\0';
883         } else {
884                 // We should have a footnote environment.
885                 if (!next || next->footnoteflag == LyXParagraph::NO_FOOTNOTE) {
886                         // Notice that LyX does request the
887                         // last char from time to time. (Asger)
888                         //lyxerr << "ERROR (LyXParagraph::GetChar): "
889                         //      "expected footnote." << endl;
890                         return '\0';
891                 }
892                 switch (next->footnotekind) {
893                 case LyXParagraph::FOOTNOTE:
894                         return LyXParagraph::META_FOOTNOTE;
895                 case LyXParagraph::MARGIN:
896                         return LyXParagraph::META_MARGIN;
897                 case LyXParagraph::FIG:
898                 case LyXParagraph::WIDE_FIG:
899                         return LyXParagraph::META_FIG;
900                 case LyXParagraph::TAB:
901                 case LyXParagraph::WIDE_TAB:
902                         return LyXParagraph::META_TAB;
903                 case LyXParagraph::ALGORITHM:
904                         return LyXParagraph::META_ALGORITHM;
905                 }
906                 return '\0'; // to shut up gcc
907         }
908 }
909
910
911 char LyXParagraph::GetChar(LyXParagraph::size_type pos) const
912 {
913         Assert(pos >= 0);
914
915         if (pos < size()) {
916                 return text[pos];
917         }
918         // > because last is the next unused position, and you can 
919         // use it if you want
920         else if (pos > size()) {
921                 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) 
922                         return NextAfterFootnote()
923                                 ->GetChar(pos - text.size() - 1);
924                 else {
925                         lyxerr << "ERROR (LyXParagraph::GetChar const): "
926                                 "position does not exist."
927                                << pos << " (" << static_cast<int>(pos)
928                                << ")\n";
929                         Assert(false);
930                 }
931                 return '\0';
932         } else {
933                 // We should have a footnote environment.
934                 if (!next || next->footnoteflag == LyXParagraph::NO_FOOTNOTE) {
935                         // Notice that LyX does request the
936                         // last char from time to time. (Asger)
937                         //lyxerr << "ERROR (LyXParagraph::GetChar): "
938                         //      "expected footnote." << endl;
939                         return '\0';
940                 }
941                 switch (next->footnotekind) {
942                 case LyXParagraph::FOOTNOTE:
943                         return LyXParagraph::META_FOOTNOTE;
944                 case LyXParagraph::MARGIN:
945                         return LyXParagraph::META_MARGIN;
946                 case LyXParagraph::FIG:
947                 case LyXParagraph::WIDE_FIG:
948                         return LyXParagraph::META_FIG;
949                 case LyXParagraph::TAB:
950                 case LyXParagraph::WIDE_TAB:
951                         return LyXParagraph::META_TAB;
952                 case LyXParagraph::ALGORITHM:
953                         return LyXParagraph::META_ALGORITHM;
954                 }
955                 return '\0'; // to shut up gcc
956         }
957 }
958
959
960 // return an string of the current word, and the end of the word in lastpos.
961 string LyXParagraph::GetWord(LyXParagraph::size_type & lastpos) const
962 {
963         Assert(lastpos>=0);
964
965         // the current word is defined as starting at the first character
966         // from the immediate left of lastpospos which meets the definition
967         // of IsLetter(), continuing to the last character to the right
968         // of this meeting IsLetter.
969
970         string theword;
971
972         // grab a word
973                 
974         // move back until we have a letter
975
976         //there's no real reason to have firstpos & lastpos as
977         //separate variables as this is written, but maybe someon
978         // will want to return firstpos in the future.
979
980         //since someone might have typed a punctuation first
981         int firstpos = lastpos;
982         
983         while ((firstpos >= 0) && !IsLetter(firstpos))
984                 --firstpos;
985
986         // now find the beginning by looking for a nonletter
987         
988         while ((firstpos>= 0) && IsLetter(firstpos))
989                 --firstpos;
990
991         // the above is now pointing to the preceeding non-letter
992         ++firstpos;
993         lastpos = firstpos;
994
995         // so copy characters into theword  until we get a nonletter
996         // note that this can easily exceed lastpos, wich means
997         // that if used in the middle of a word, the whole word
998         // is included
999
1000         while (IsLetter(lastpos)) theword += GetChar(lastpos++);
1001         
1002         return theword;
1003
1004 }
1005
1006  
1007 LyXParagraph::size_type LyXParagraph::Last() const
1008 {
1009         if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1010                 return text.size() + NextAfterFootnote()->Last() + 1;
1011         // the 1 is the symbol
1012         // for the footnote
1013         else
1014                 return text.size();
1015 }
1016
1017
1018 LyXParagraph * LyXParagraph::ParFromPos(LyXParagraph::size_type pos)
1019 {
1020         // > because last is the next unused position, and you can 
1021         // use it if you want
1022         if (pos > size()) {
1023                 if (next
1024                     && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) 
1025                         return NextAfterFootnote()
1026                                 ->ParFromPos(pos - text.size() - 1);
1027                 else 
1028                         lyxerr << "ERROR (LyXParagraph::ParFromPos): "
1029                                 "position does not exist." << endl;
1030                 return this;
1031         } else
1032                 return this;
1033 }
1034
1035
1036 int LyXParagraph::PositionInParFromPos(LyXParagraph::size_type pos) const
1037 {
1038         // > because last is the next unused position, and you can 
1039         // use it if you want
1040         if (pos > size()) {
1041                 if (next
1042                     && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) 
1043                         return NextAfterFootnote()
1044                                 ->PositionInParFromPos(pos - text.size() - 1);
1045                 else 
1046                         lyxerr <<
1047                                 "ERROR (LyXParagraph::PositionInParFromPos): "
1048                                 "position does not exist." << endl;
1049                 return pos;
1050         }
1051         else
1052                 return pos;
1053 }
1054
1055
1056 void LyXParagraph::SetFont(LyXParagraph::size_type pos,
1057                            LyXFont const & font)
1058 {
1059         // > because last is the next unused position, and you can 
1060         // use it if you want
1061         if (pos > size()) {
1062                 if (next &&
1063                     next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1064                         NextAfterFootnote()->SetFont(pos - text.size() - 1,
1065                                                      font);
1066                 } else {
1067                         lyxerr << "ERROR (LyXParagraph::SetFont): "
1068                                 "position does not exist." << endl;
1069                 }
1070                 return;
1071         }
1072         LyXFont patternfont(LyXFont::ALL_INHERIT);
1073
1074         // First, reduce font against layout/label font
1075         // Update: The SetCharFont() routine in text2.C already
1076         // reduces font, so we don't need to do that here. (Asger)
1077         // No need to simplify this because it will disappear
1078         // in a new kernel. (Asger)
1079         // Next search font table
1080         FontList::iterator tmp = fontlist.begin();
1081         for (; tmp != fontlist.end(); ++tmp) {
1082                 if (pos >= (*tmp).pos && pos <= (*tmp).pos_end) {
1083                         break;
1084                 }
1085         }
1086         if (tmp == fontlist.end()) { // !found
1087                 // if we did not find a font entry, but if the font at hand
1088                 // is the same as default, we just forget it
1089                 if (font == patternfont) return;
1090
1091                 // ok, we did not find a font entry. But maybe there is exactly
1092                 // the needed font ientry one position left
1093                 FontList::iterator tmp2 = fontlist.begin();
1094                 for (; tmp2 != fontlist.end(); ++tmp2) {
1095                         if (pos - 1 >= (*tmp2).pos
1096                             && pos - 1 <= (*tmp2).pos_end)
1097                                 break;
1098                 }
1099                 if (tmp2 != fontlist.end()) {
1100                         // ok there is one, maybe it is exactly
1101                         // the needed font
1102                         if ((*tmp2).font == font) {
1103                                 // put the position under the font
1104                                 (*tmp2).pos_end++;
1105                                 return;
1106                         }
1107                 }
1108                 // Add a new entry in the
1109                 // fontlist for the position
1110                 FontTable ft;
1111                 ft.pos = pos;
1112                 ft.pos_end = pos;
1113                 ft.font = font; // or patternfont
1114                 // It seems that using font instead of patternfont here
1115                 // fixes all the problems. This also surfaces a "bug" in
1116                 // the old code.
1117                 fontlist.insert(fontlist.begin(), ft);
1118         } else if ((*tmp).pos != (*tmp).pos_end) { // we found a font entry. maybe we have to split it and create a new one.
1119
1120 // more than one character
1121                 if (pos == (*tmp).pos) {
1122                         // maybe we should enlarge the left fonttable
1123                         FontList::iterator tmp2 = fontlist.begin();
1124                         for (; tmp2 != fontlist.end(); ++tmp2) {
1125                                 if (pos - 1 >= (*tmp2).pos
1126                                     && pos - 1 <= (*tmp2).pos_end)
1127                                         break;
1128                         }
1129                         // Is there is one, and is it exactly
1130                         // the needed font?
1131                         if (tmp2 != fontlist.end() &&
1132                             (*tmp2).font == font) {
1133                                 // Put the position under the font
1134                                 (*tmp2).pos_end++;
1135                                 (*tmp).pos++;
1136                                 return;
1137                         }
1138
1139                         // Add a new entry in the
1140                         // fontlist for the position
1141                         FontTable ft;
1142                         ft.pos = pos + 1;
1143                         ft.pos_end = (*tmp).pos_end;
1144                         ft.font = (*tmp).font;
1145                         (*tmp).pos_end = pos;
1146                         (*tmp).font = font;
1147                         fontlist.insert(fontlist.begin(), ft);
1148                 } else if (pos == (*tmp).pos_end) {
1149                         // Add a new entry in the
1150                         // fontlist for the position
1151                         FontTable ft;
1152                         ft.pos = (*tmp).pos;
1153                         ft.pos_end = (*tmp).pos_end - 1;
1154                         ft.font = (*tmp).font;
1155                         (*tmp).pos = (*tmp).pos_end;
1156                         (*tmp).font = font;
1157                         fontlist.insert(fontlist.begin(), ft);
1158                 } else {
1159                         // Add a new entry in the
1160                         // fontlist for the position
1161                         FontTable ft;
1162                         ft.pos = (*tmp).pos;
1163                         ft.pos_end = pos - 1;
1164                         ft.font = (*tmp).font;
1165                         
1166                         FontTable ft2;
1167                         ft2.pos = pos + 1;
1168                         ft2.pos_end = (*tmp).pos_end;
1169                         ft2.font = (*tmp).font;
1170                         
1171                         (*tmp).pos = pos;
1172                         (*tmp).pos_end = pos;
1173                         (*tmp).font = font;
1174                         
1175                         fontlist.insert(fontlist.begin(), ft);
1176                         fontlist.insert(fontlist.begin(), ft2);
1177                 }
1178         } else {
1179                 (*tmp).font = font;
1180         }
1181 }
1182
1183    
1184 // This function is able to hide closed footnotes.
1185 LyXParagraph * LyXParagraph::Next()
1186 {
1187         if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1188                 LyXParagraph * tmp = next;
1189                 while (tmp
1190                        && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1191                         tmp = tmp->next;
1192                 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE) 
1193                         return tmp->Next(); /* there can be more than one
1194                                                footnote in a logical
1195                                                paragraph */
1196                 else
1197                         return next;  // This should never happen!
1198         } else
1199                 return next;
1200 }
1201
1202
1203 LyXParagraph * LyXParagraph::NextAfterFootnote()
1204 {
1205         if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1206                 LyXParagraph * tmp = next;
1207                 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1208                         tmp = tmp->next;
1209                 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE) 
1210                         return tmp;   /* there can be more than one footnote
1211                                          in a logical paragraph */
1212                 else
1213                         return next;  // This should never happen!
1214         } else
1215                 return next;
1216 }
1217
1218
1219 LyXParagraph const * LyXParagraph::NextAfterFootnote() const
1220 {
1221         if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1222                 LyXParagraph * tmp = next;
1223                 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1224                         tmp = tmp->next;
1225                 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE) 
1226                         return tmp;   /* there can be more than one footnote
1227                                          in a logical paragraph */
1228                 else
1229                         return next;  // This should never happen!
1230         } else
1231                 return next;
1232 }
1233
1234
1235 LyXParagraph * LyXParagraph::PreviousBeforeFootnote()
1236 {
1237         LyXParagraph * tmp;
1238         if (previous && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1239                 tmp = previous;
1240                 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1241                         tmp = tmp->previous;
1242                 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE) 
1243                         return tmp;    /* there can be more than one footnote
1244                                           in a logical paragraph */
1245                 else
1246                         return previous;  // This should never happen!
1247         } else
1248                 return previous;
1249 }
1250
1251
1252 LyXParagraph * LyXParagraph::LastPhysicalPar()
1253 {
1254         if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1255                 return this;
1256    
1257         LyXParagraph * tmp = this;
1258         while (tmp->next
1259                && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1260                 tmp = tmp->NextAfterFootnote();
1261    
1262         return tmp;
1263 }
1264
1265 LyXParagraph const * LyXParagraph::LastPhysicalPar() const
1266 {
1267         if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1268                 return this;
1269    
1270         LyXParagraph const * tmp = this;
1271         while (tmp->next
1272                && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1273                 tmp = tmp->NextAfterFootnote();
1274    
1275         return tmp;  
1276 }
1277
1278 LyXParagraph * LyXParagraph::FirstPhysicalPar()
1279 {
1280         if (!IsDummy())
1281                 return this;
1282         LyXParagraph * tmppar = this;
1283
1284         while (tmppar &&
1285                (tmppar->IsDummy()
1286                 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1287                 tmppar = tmppar->previous;
1288    
1289         if (!tmppar) {
1290                 return this;
1291         } else
1292                 return tmppar;
1293 }
1294
1295
1296 LyXParagraph const * LyXParagraph::FirstPhysicalPar() const
1297 {
1298         if (!IsDummy())
1299                 return this;
1300         LyXParagraph const * tmppar = this;
1301
1302         while (tmppar &&
1303                (tmppar->IsDummy()
1304                 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1305                 tmppar = tmppar->previous;
1306    
1307         if (!tmppar) {
1308                 return this;
1309         } else
1310                 return tmppar;
1311 }
1312
1313
1314 // This function is able to hide closed footnotes.
1315 LyXParagraph * LyXParagraph::Previous()
1316 {
1317         LyXParagraph * tmp = previous;
1318         if (!tmp)
1319                 return tmp;
1320    
1321         if (tmp->previous
1322             && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1323                 tmp = tmp->previous;
1324                 while (tmp
1325                        && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1326                         tmp = tmp->previous;
1327                 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE) 
1328                         return tmp->next->Previous();   
1329
1330                 else
1331                         return previous; 
1332         } else
1333                 return previous;
1334 }
1335
1336
1337 // This function is able to hide closed footnotes.
1338 LyXParagraph const * LyXParagraph::Previous() const
1339 {
1340         LyXParagraph * tmp = previous;
1341         if (!tmp)
1342                 return tmp;
1343    
1344         if (tmp->previous
1345             && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1346                 tmp = tmp->previous;
1347                 while (tmp
1348                        && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1349                         tmp = tmp->previous;
1350                 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE) 
1351                         return tmp->next->Previous();   
1352
1353                 else
1354                         return previous; 
1355         } else
1356                 return previous;
1357 }
1358
1359
1360 void LyXParagraph::BreakParagraph(LyXParagraph::size_type pos,
1361                                   int flag)
1362 {
1363         size_type i, j, pos_end, pos_first;
1364         // create a new paragraph
1365         LyXParagraph * par = ParFromPos(pos);
1366         LyXParagraph * firstpar = FirstPhysicalPar();
1367    
1368         LyXParagraph * tmp = new LyXParagraph(par);
1369         
1370         tmp->footnoteflag = footnoteflag;
1371         tmp->footnotekind = footnotekind;
1372    
1373         // this is an idea for a more userfriendly layout handling, I will
1374         // see what the users say
1375    
1376         // layout stays the same with latex-environments
1377         if (flag) {
1378                 tmp->SetOnlyLayout(firstpar->layout);
1379                 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1380         }
1381
1382         if (Last() > pos || !Last() || flag == 2) {
1383                 tmp->SetOnlyLayout(firstpar->layout);
1384                 tmp->align = firstpar->align;
1385                 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1386       
1387                 tmp->line_bottom = firstpar->line_bottom;
1388                 firstpar->line_bottom = false;
1389                 tmp->pagebreak_bottom = firstpar->pagebreak_bottom;
1390                 firstpar->pagebreak_bottom = false;
1391                 tmp->added_space_bottom = firstpar->added_space_bottom;
1392                 firstpar->added_space_bottom = VSpace(VSpace::NONE);
1393       
1394                 tmp->depth = firstpar->depth;
1395                 tmp->noindent = firstpar->noindent;
1396    
1397                 // copy everything behind the break-position
1398                 // to the new paragraph
1399                 pos_first = 0;
1400                 while (ParFromPos(pos_first) != par)
1401                         ++pos_first;
1402
1403                 pos_end = pos_first + par->text.size() - 1;
1404
1405                 for (i = j = pos; i <= pos_end; ++i) {
1406                         par->CutIntoMinibuffer(i - pos_first);
1407                         if (tmp->InsertFromMinibuffer(j - pos))
1408                                 ++j;
1409                 }
1410                 tmp->text.resize(tmp->text.size());
1411                 for (i = pos_end; i >= pos; --i)
1412                         par->Erase(i - pos_first);
1413
1414                 par->text.resize(par->text.size());
1415         }
1416
1417         // just an idea of me
1418         if (!pos) {
1419                 tmp->line_top = firstpar->line_top;
1420                 tmp->pagebreak_top = firstpar->pagebreak_top;
1421                 tmp->added_space_top = firstpar->added_space_top;
1422                 tmp->bibkey = firstpar->bibkey;
1423                 firstpar->Clear();
1424                 // layout stays the same with latex-environments
1425                 if (flag) {
1426                         firstpar->SetOnlyLayout(tmp->layout);
1427                         firstpar->SetLabelWidthString(tmp->labelwidthstring);
1428                         firstpar->depth = tmp->depth;
1429                 }
1430         }
1431 }
1432
1433
1434 void LyXParagraph::MakeSameLayout(LyXParagraph const * par)
1435 {
1436         par = par->FirstPhysicalPar();
1437         footnoteflag = par->footnoteflag;
1438         footnotekind = par->footnotekind;
1439
1440         layout = par->layout;
1441         align = par-> align;
1442         SetLabelWidthString(par->labelwidthstring);
1443
1444         line_bottom = par->line_bottom;
1445         pagebreak_bottom = par->pagebreak_bottom;
1446         added_space_bottom = par->added_space_bottom;
1447
1448         line_top = par->line_top;
1449         pagebreak_top = par->pagebreak_top;
1450         added_space_top = par->added_space_top;
1451
1452         spacing = par->spacing;
1453         
1454         pextra_type = par->pextra_type;
1455         pextra_width = par->pextra_width;
1456         pextra_widthp = par->pextra_widthp;
1457         pextra_alignment = par->pextra_alignment;
1458         pextra_hfill = par->pextra_hfill;
1459         pextra_start_minipage = par->pextra_start_minipage;
1460
1461         noindent = par->noindent;
1462         depth = par->depth;
1463 }
1464
1465
1466 LyXParagraph * LyXParagraph::FirstSelfrowPar()
1467 {
1468         LyXParagraph * tmppar = this;
1469         while (tmppar && (
1470                 (tmppar->IsDummy()
1471                  && tmppar->previous->footnoteflag == 
1472                  LyXParagraph::CLOSED_FOOTNOTE)
1473                 || tmppar->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE))
1474                 tmppar = tmppar->previous;
1475    
1476         if (!tmppar)
1477                 return this;  // This should never happen!
1478         else
1479                 return tmppar;
1480 }
1481
1482
1483 LyXParagraph * LyXParagraph::Clone() const
1484 {
1485         // create a new paragraph
1486         LyXParagraph * result = new LyXParagraph;
1487    
1488         result->MakeSameLayout(this);
1489
1490         // this is because of the dummy layout of the paragraphs that
1491         // follow footnotes
1492         result->layout = layout;
1493    
1494         /* table stuff -- begin*/ 
1495         if (table)
1496                 result->table = table->Clone();
1497         else
1498                 result->table = 0;
1499         /* table stuff -- end*/ 
1500
1501         result->inset_owner = inset_owner;
1502    
1503         // ale970302
1504         result->bibkey = (bibkey) ? new InsetBibKey(bibkey): 0;
1505                
1506     
1507         // copy everything behind the break-position to the new paragraph
1508
1509         result->text = text;
1510         result->fontlist = fontlist;
1511         result->insetlist = insetlist;
1512         for (InsetList::iterator it = result->insetlist.begin();
1513              it != result->insetlist.end(); ++it)
1514                 (*it).inset = (*it).inset->Clone();
1515         return result;
1516 }
1517
1518
1519 bool LyXParagraph::HasSameLayout(LyXParagraph const * par) const
1520 {
1521         par = par->FirstPhysicalPar();
1522
1523         return (
1524                 par->footnoteflag == footnoteflag &&
1525                 par->footnotekind == footnotekind &&
1526
1527                 par->layout == layout &&
1528
1529                 par->align == align &&
1530
1531                 par->line_bottom == line_bottom &&
1532                 par->pagebreak_bottom == pagebreak_bottom &&
1533                 par->added_space_bottom == added_space_bottom &&
1534
1535                 par->line_top == line_top &&
1536                 par->pagebreak_top == pagebreak_top &&
1537                 par->added_space_top == added_space_top &&
1538
1539                 par->spacing == spacing &&
1540                 
1541                 par->pextra_type == pextra_type &&
1542                 par->pextra_width == pextra_width && 
1543                 par->pextra_widthp == pextra_widthp && 
1544                 par->pextra_alignment == pextra_alignment && 
1545                 par->pextra_hfill == pextra_hfill && 
1546                 par->pextra_start_minipage == pextra_start_minipage && 
1547
1548                 par->table == table && // what means: NO TABLE AT ALL 
1549
1550                 par->noindent == noindent &&
1551                 par->depth == depth);
1552 }
1553
1554
1555 void LyXParagraph::BreakParagraphConservative(LyXParagraph::size_type pos)
1556 {
1557         // create a new paragraph
1558         LyXParagraph * par = ParFromPos(pos);
1559
1560         LyXParagraph * tmp = new LyXParagraph(par);
1561    
1562         tmp->MakeSameLayout(par);
1563
1564         // When can pos < Last()?
1565         // I guess pos == Last() is possible.
1566         if (Last() > pos) {
1567                 // copy everything behind the break-position to the new
1568                 // paragraph
1569                 size_type pos_first = 0;
1570                 while (ParFromPos(pos_first) != par)
1571                         ++pos_first;
1572                 size_type pos_end = pos_first + par->text.size() - 1;
1573
1574                 size_type i, j;
1575                 for (i = j = pos; i <= pos_end; ++i) {
1576                         par->CutIntoMinibuffer(i - pos_first);
1577                         if (tmp->InsertFromMinibuffer(j - pos))
1578                                 ++j;
1579                 }
1580                 tmp->text.resize(tmp->text.size());
1581                 for (size_type i = pos_end; i >= pos; --i)
1582                         par->Erase(i - pos_first);
1583
1584                 par->text.resize(par->text.size());
1585         }
1586 }
1587    
1588
1589 // Be carefull, this does not make any check at all.
1590 void LyXParagraph::PasteParagraph()
1591 {
1592         // copy the next paragraph to this one
1593         LyXParagraph * the_next = Next();
1594    
1595         LyXParagraph * firstpar = FirstPhysicalPar();
1596    
1597         // first the DTP-stuff
1598         firstpar->line_bottom = the_next->line_bottom;
1599         firstpar->added_space_bottom = the_next->added_space_bottom;
1600         firstpar->pagebreak_bottom = the_next->pagebreak_bottom;
1601
1602         size_type pos_end = the_next->text.size() - 1;
1603         size_type pos_insert = Last();
1604
1605         // ok, now copy the paragraph
1606         size_type i, j;
1607         for (i = j = 0; i <= pos_end; ++i) {
1608                 the_next->CutIntoMinibuffer(i);
1609                 if (InsertFromMinibuffer(pos_insert + j))
1610                         ++j;
1611         }
1612    
1613         // delete the next paragraph
1614         delete the_next;
1615 }
1616
1617
1618 void LyXParagraph::OpenFootnote(LyXParagraph::size_type pos)
1619 {
1620         LyXParagraph * par = ParFromPos(pos);
1621         par = par->next;
1622         while (par && par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1623                 par->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
1624                 par = par->next;
1625         }
1626 }
1627
1628
1629 void LyXParagraph::CloseFootnote(LyXParagraph::size_type pos)
1630 {
1631         LyXParagraph * par = ParFromPos(pos);
1632         par = par->next;
1633         while (par && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
1634                 par->footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
1635                 par = par->next;
1636         }
1637 }
1638
1639 int LyXParagraph::GetEndLabel() const
1640 {
1641         LyXParagraph const * par = this;
1642         int par_depth = GetDepth();
1643         while (par) {
1644                 LyXTextClass::LayoutList::size_type layout = par->GetLayout();
1645                 int endlabeltype =
1646                         textclasslist.Style(current_view->buffer()->params.textclass,
1647                                             layout).endlabeltype;
1648                 if (endlabeltype != END_LABEL_NO_LABEL) {
1649                         LyXParagraph const * last = this;
1650                         if( footnoteflag == NO_FOOTNOTE)
1651                                 last = LastPhysicalPar();
1652                         else if (next->footnoteflag == NO_FOOTNOTE)
1653                                 return endlabeltype;
1654
1655                         if (!last || !last->next)
1656                                 return endlabeltype;
1657
1658                         int next_depth = last->next->GetDepth();
1659                         if (par_depth > next_depth ||
1660                             (par_depth == next_depth && layout != last->next->GetLayout() ))
1661                                 return endlabeltype;
1662                         break;
1663                 }
1664                 if (par_depth == 0)
1665                         break;
1666                 par = par->DepthHook(par_depth - 1);
1667                 if (par)
1668                         par_depth = par->GetDepth();
1669         }
1670         return END_LABEL_NO_LABEL;
1671 }
1672
1673
1674 LyXTextClass::size_type LyXParagraph::GetLayout() const
1675 {
1676         return FirstPhysicalPar()->layout;
1677 }
1678
1679
1680 char LyXParagraph::GetDepth() const
1681 {
1682         return FirstPhysicalPar()->depth;
1683 }
1684
1685
1686 char LyXParagraph::GetAlign() const
1687 {
1688         return FirstPhysicalPar()->align;
1689 }
1690
1691
1692 string LyXParagraph::GetLabelstring() const
1693 {
1694         return FirstPhysicalPar()->labelstring;
1695 }
1696
1697
1698 int LyXParagraph::GetFirstCounter(int i) const
1699 {
1700         return FirstPhysicalPar()->counter_[i];
1701 }
1702
1703
1704 // the next two functions are for the manual labels
1705 string LyXParagraph::GetLabelWidthString() const
1706 {
1707         if (!FirstPhysicalPar()->labelwidthstring.empty())
1708                 return FirstPhysicalPar()->labelwidthstring;
1709         else
1710                 return _("Senseless with this layout!");
1711 }
1712
1713
1714 void LyXParagraph::SetLabelWidthString(string const & s)
1715 {
1716         LyXParagraph * par = FirstPhysicalPar();
1717
1718         par->labelwidthstring = s;
1719 }
1720
1721
1722 void LyXParagraph::SetOnlyLayout(LyXTextClass::size_type new_layout)
1723 {
1724         LyXParagraph * par = FirstPhysicalPar();
1725         LyXParagraph * ppar = 0;
1726         LyXParagraph * npar = 0;
1727
1728         par->layout = new_layout;
1729         /* table stuff -- begin*/ 
1730         if (table) 
1731                 par->layout = 0;
1732         /* table stuff -- end*/ 
1733         if (par->pextra_type == PEXTRA_NONE) {
1734                 if (par->Previous()) {
1735                         ppar = par->Previous()->FirstPhysicalPar();
1736                         while(ppar
1737                               && ppar->Previous()
1738                               && (ppar->depth > par->depth))
1739                                 ppar = ppar->Previous()->FirstPhysicalPar();
1740                 }
1741                 if (par->Next()) {
1742                         npar = par->Next()->NextAfterFootnote();
1743                         while(npar
1744                               && npar->Next()
1745                               && (npar->depth > par->depth))
1746                                 npar = npar->Next()->NextAfterFootnote();
1747                 }
1748                 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
1749                         string
1750                                 p1 = ppar->pextra_width,
1751                                 p2 = ppar->pextra_widthp;
1752                         ppar->SetPExtraType(ppar->pextra_type,
1753                                             p1.c_str(), p2.c_str());
1754                 }
1755                 if ((par->pextra_type == PEXTRA_NONE) &&
1756                     npar && (npar->pextra_type != PEXTRA_NONE)) {
1757                         string
1758                                 p1 = npar->pextra_width,
1759                                 p2 = npar->pextra_widthp;
1760                         npar->SetPExtraType(npar->pextra_type,
1761                                             p1.c_str(), p2.c_str());
1762                 }
1763         }
1764 }
1765
1766
1767 void LyXParagraph::SetLayout(LyXTextClass::size_type new_layout)
1768 {
1769         LyXParagraph
1770                 * par = FirstPhysicalPar(),
1771                 * ppar = 0,
1772                 * npar = 0;
1773
1774         par->layout = new_layout;
1775         par->labelwidthstring.erase();
1776         par->align = LYX_ALIGN_LAYOUT;
1777         par->added_space_top = VSpace(VSpace::NONE);
1778         par->added_space_bottom = VSpace(VSpace::NONE);
1779         par->spacing.set(Spacing::Default);
1780         
1781         /* table stuff -- begin*/ 
1782         if (table) 
1783                 par->layout = 0;
1784         /* table stuff -- end*/
1785         if (par->pextra_type == PEXTRA_NONE) {
1786                 if (par->Previous()) {
1787                         ppar = par->Previous()->FirstPhysicalPar();
1788                         while(ppar
1789                               && ppar->Previous()
1790                               && (ppar->depth > par->depth))
1791                                 ppar = ppar->Previous()->FirstPhysicalPar();
1792                 }
1793                 if (par->Next()) {
1794                         npar = par->Next()->NextAfterFootnote();
1795                         while(npar
1796                               && npar->Next()
1797                               && (npar->depth > par->depth))
1798                                 npar = npar->Next()->NextAfterFootnote();
1799                 }
1800                 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
1801                         string
1802                                 p1 = ppar->pextra_width,
1803                                 p2 = ppar->pextra_widthp;
1804                         ppar->SetPExtraType(ppar->pextra_type,
1805                                             p1.c_str(), p2.c_str());
1806                 }
1807                 if ((par->pextra_type == PEXTRA_NONE) &&
1808                     npar && (npar->pextra_type != PEXTRA_NONE)) {
1809                         string
1810                                 p1 = npar->pextra_width,
1811                                 p2 = npar->pextra_widthp;
1812                         npar->SetPExtraType(npar->pextra_type,
1813                                             p1.c_str(), p2.c_str());
1814                 }
1815         }
1816 }
1817
1818
1819 // if the layout of a paragraph contains a manual label, the beginning of the 
1820 // main body is the beginning of the second word. This is what the par-
1821 // function returns. If the layout does not contain a label, the main
1822 // body always starts with position 0. This differentiation is necessary,
1823 // because there cannot be a newline or a blank <= the beginning of the 
1824 // main body in TeX.
1825
1826 int LyXParagraph::BeginningOfMainBody() const
1827 {
1828         if (FirstPhysicalPar() != this)
1829                 return -1;
1830    
1831         // Unroll the first two cycles of the loop
1832         // and remember the previous character to
1833         // remove unnecessary GetChar() calls
1834         size_type i = 0;
1835         if (i < size()
1836             && GetChar(i) != LyXParagraph::META_NEWLINE
1837                 ) {
1838                 ++i;
1839                 char previous_char = 0, temp = 0; 
1840                 if (i < size()
1841                     && (previous_char = GetChar(i)) != LyXParagraph::META_NEWLINE) {
1842                         // Yes, this  ^ is supposed to be "= " not "=="
1843                         ++i;
1844                         while (i < size()
1845                                && previous_char != ' '
1846                                && (temp = GetChar(i)) != LyXParagraph::META_NEWLINE) {
1847                                 ++i;
1848                                 previous_char = temp;
1849                         }
1850                 }
1851         }
1852
1853         if (i == 0 && i == size() &&
1854             !(footnoteflag == LyXParagraph::NO_FOOTNOTE
1855               && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1856                 ++i;                           /* the cursor should not jump  
1857                                                 * to the main body if there
1858                                                 * is nothing in! */
1859         return i;
1860 }
1861
1862
1863 LyXParagraph * LyXParagraph::DepthHook(int deth)
1864 {
1865         LyXParagraph * newpar = this;
1866         if (deth < 0)
1867                 return 0;
1868    
1869         do {
1870                 newpar = newpar->FirstPhysicalPar()->Previous();
1871         } while (newpar && newpar->GetDepth() > deth
1872                  && newpar->footnoteflag == footnoteflag);
1873    
1874         if (!newpar) {
1875                 if (Previous() || GetDepth())
1876                         lyxerr << "ERROR (LyXParagraph::DepthHook): "
1877                                 "no hook." << endl;
1878                 newpar = this;
1879         }
1880         return newpar->FirstPhysicalPar();
1881 }
1882
1883
1884 LyXParagraph const * LyXParagraph::DepthHook(int deth) const
1885 {
1886         LyXParagraph const * newpar = this;
1887         if (deth < 0)
1888                 return 0;
1889    
1890         do {
1891                 newpar = newpar->FirstPhysicalPar()->Previous();
1892         } while (newpar && newpar->GetDepth() > deth
1893                  && newpar->footnoteflag == footnoteflag);
1894    
1895         if (!newpar) {
1896                 if (Previous() || GetDepth())
1897                         lyxerr << "ERROR (LyXParagraph::DepthHook): "
1898                                 "no hook." << endl;
1899                 newpar = this;
1900         }
1901         return newpar->FirstPhysicalPar();
1902 }
1903
1904
1905 int LyXParagraph::AutoDeleteInsets()
1906 {
1907         int count = 0;
1908         InsetList::size_type index = 0;
1909         while (index < insetlist.size()) {
1910                 if (insetlist[index].inset && insetlist[index].inset->AutoDelete()) {
1911                         Erase(insetlist[index].pos); 
1912                         // Erase() calls to insetlist.erase(&insetlist[index])
1913                         // so index shouldn't be increased.
1914                         ++count;
1915                 } else
1916                         ++index;
1917         }
1918         return count;
1919 }
1920
1921
1922 Inset * LyXParagraph::ReturnNextInsetPointer(LyXParagraph::size_type & pos)
1923 {
1924         InsetList::iterator it = lower_bound(insetlist.begin(),
1925                                              insetlist.end(),
1926                                              pos, matchIT());
1927         if (it != insetlist.end()) {
1928                 pos = (*it).pos;
1929                 return (*it).inset;
1930         }
1931         return 0;
1932 }
1933
1934
1935 // returns -1 if inset not found
1936 int LyXParagraph::GetPositionOfInset(Inset * inset) const
1937 {
1938         // Find the entry.
1939         // We could use lower_bound here too, we just need to add
1940         // the approp. operator() to matchIT (and change the name
1941         // of that struct). Code would then be:
1942         // InsetList::const_iterator cit = lower_bound(insetlist.begin(),
1943         //                                             insetlist.end(),
1944         //                                             inset, matchIT());
1945         // if ((*cit).inset == inset) {
1946         //         return (*cit).pos;
1947         // }
1948         for (InsetList::const_iterator cit = insetlist.begin();
1949              cit != insetlist.end(); ++cit) {
1950                 if ((*cit).inset == inset) {
1951                         return (*cit).pos;
1952                 }
1953         }
1954         // Think about footnotes.
1955         if (footnoteflag == LyXParagraph::NO_FOOTNOTE 
1956             && next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1957                 int further = 
1958                         NextAfterFootnote()->GetPositionOfInset(inset);
1959                 if (further != -1)
1960                         return text.size() + 1 + further;
1961         }
1962         return -1;
1963 }
1964
1965
1966 LyXParagraph * LyXParagraph::TeXOnePar(ostream & os, TexRow & texrow,
1967                                        ostream & foot,
1968                                        TexRow & foot_texrow,
1969                                        int & foot_count)
1970 {
1971         lyxerr[Debug::LATEX] << "TeXOnePar...     " << this << endl;
1972         LyXLayout const & style =
1973                 textclasslist.Style(current_view->buffer()->params.textclass,
1974                                     layout);
1975
1976         bool further_blank_line = false;
1977         if (IsDummy())
1978                 lyxerr << "ERROR (LyXParagraph::TeXOnePar) is dummy." << endl;
1979
1980         if (start_of_appendix) {
1981                 os << "\\appendix\n";
1982                 texrow.newline();
1983         }
1984
1985         if (!spacing.isDefault()
1986             && (!Previous() || !Previous()->HasSameLayout(this))) {
1987                 os << spacing.writeEnvirBegin() << "\n";
1988                 texrow.newline();
1989         }
1990         
1991         if (tex_code_break_column && style.isCommand()){
1992                 os << '\n';
1993                 texrow.newline();
1994         }
1995
1996         if (pagebreak_top) {
1997                 os << "\\newpage";
1998                 further_blank_line = true;
1999         }
2000         if (added_space_top.kind() != VSpace::NONE) {
2001                 os << added_space_top.asLatexCommand(current_view->buffer()->params);
2002                 further_blank_line = true;
2003         }
2004       
2005         if (line_top) {
2006                 os << "\\lyxline{\\" << getFont(0).latexSize() << '}'
2007                    << "\\vspace{-1\\parskip}";
2008                 further_blank_line = true;
2009         }
2010
2011         if (further_blank_line){
2012                 os << '\n';
2013                 texrow.newline();
2014         }
2015
2016         Language const * language = getParLanguage();
2017         Language const * doc_language = current_view->buffer()->params.language_info;
2018         if (language != doc_language) {
2019                 os << subst(lyxrc.language_command_begin, "$$lang",
2020                             language->lang)
2021                    << endl;
2022                 texrow.newline();
2023         }
2024         
2025         switch (style.latextype) {
2026         case LATEX_COMMAND:
2027                 os << '\\'
2028                    << style.latexname()
2029                    << style.latexparam();
2030                 break;
2031         case LATEX_ITEM_ENVIRONMENT:
2032                 if (bibkey) {
2033                         bibkey->Latex(os, false, false);
2034                 } else
2035                         os << "\\item ";
2036                 break;
2037         case LATEX_LIST_ENVIRONMENT:
2038                 os << "\\item ";
2039                 break;
2040         default:
2041                 break;
2042         }
2043
2044         bool need_par = SimpleTeXOnePar(os, texrow);
2045  
2046         // Spit out footnotes
2047         LyXParagraph * par = next;
2048         if (lyxrc.rtl_support) {
2049                 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2050                     && next->footnoteflag != footnoteflag) {
2051                         LyXParagraph * p = 0;
2052                         bool is_rtl = GetFontSettings(size()-1).isRightToLeft();
2053                         if ( (p = NextAfterFootnote()) != 0 &&
2054                              p->GetFontSettings(0).isRightToLeft() != is_rtl)
2055                                 is_rtl = GetFontSettings(0).isRightToLeft();
2056                         while (par &&
2057                                par->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2058                                par->footnoteflag != footnoteflag) {
2059                                 par = par->TeXFootnote(os, texrow, foot,
2060                                                        foot_texrow, foot_count,
2061                                                        is_rtl);
2062                                 par->SimpleTeXOnePar(os, texrow);
2063                                 is_rtl = par->GetFontSettings(par->size()-1).isRightToLeft();
2064                                 if (par->next &&
2065                                     par->next->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2066                                     (p = par->NextAfterFootnote()) != 0 &&
2067                                     p->GetFontSettings(0).isRightToLeft() != is_rtl)
2068                                         is_rtl = GetFontSettings(0).isRightToLeft();
2069                                 par = par->next;
2070                         }
2071                 }
2072         } else {
2073                 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2074                        && par->footnoteflag != footnoteflag) {
2075                         par = par->TeXFootnote(os, texrow,
2076                                                foot, foot_texrow, foot_count,
2077                                                false);
2078                         par->SimpleTeXOnePar(os, texrow);
2079                         par = par->next;
2080                 }
2081         }
2082
2083         // Make sure that \\par is done with the font of the last
2084         // character if this has another size as the default.
2085         // This is necessary because LaTeX (and LyX on the screen)
2086         // calculates the space between the baselines according
2087         // to this font. (Matthias)
2088         LyXFont font = getFont(Last() - 1);
2089         if (need_par) {
2090                 if (style.resfont.size() != font.size()) {
2091                         os << '\\'
2092                            << font.latexSize()
2093                            << ' ';
2094                 }
2095                 os << "\\par}";
2096         } else if (textclasslist.Style(current_view->buffer()->params.textclass,
2097                                        GetLayout()).isCommand()){
2098                 if (style.resfont.size() != font.size()) {
2099                         os << '\\'
2100                            << font.latexSize()
2101                            << ' ';
2102                 }
2103                 os << '}';
2104         } else if (style.resfont.size() != font.size()){
2105                 os << "{\\" << font.latexSize() << " \\par}";
2106         }
2107
2108         if (language != doc_language) {
2109                 os << endl 
2110                    << subst(lyxrc.language_command_end, "$$lang",
2111                             doc_language->lang);
2112         }
2113         
2114         switch (style.latextype) {
2115         case LATEX_ITEM_ENVIRONMENT:
2116         case LATEX_LIST_ENVIRONMENT:
2117                 if (par && (depth < par->depth)) {
2118                         os << '\n';
2119                         texrow.newline();
2120                 }
2121                 break;
2122         case LATEX_ENVIRONMENT:
2123                 // if its the last paragraph of the current environment
2124                 // skip it otherwise fall through
2125                 if (par
2126                     && (par->layout != layout
2127                         || par->depth != depth
2128                         || par->pextra_type != pextra_type))
2129                         break;
2130         default:
2131                 // we don't need it for the last paragraph!!!
2132                 if (next && !(footnoteflag != LyXParagraph::NO_FOOTNOTE
2133                       && footnotekind != LyXParagraph::FOOTNOTE
2134                       && footnotekind != LyXParagraph::MARGIN
2135                       && (table
2136                           || (par
2137                               && par->table)))) {
2138                         // don't insert this if we would be adding it
2139                         // before or after a table in a float.  This 
2140                         // little trick is needed in order to allow
2141                         // use of tables in \subfigures or \subtables.
2142                         os << '\n';
2143                         texrow.newline();
2144                 }
2145         }
2146         
2147         further_blank_line = false;
2148         if (line_bottom) {
2149                 os << "\\lyxline{\\" << getFont(Last() - 1).latexSize() << '}';
2150                 further_blank_line = true;
2151         }
2152
2153         if (added_space_bottom.kind() != VSpace::NONE) {
2154                 os << added_space_bottom.asLatexCommand(current_view->buffer()->params);
2155                 further_blank_line = true;
2156         }
2157       
2158         if (pagebreak_bottom) {
2159                 os << "\\newpage";
2160                 further_blank_line = true;
2161         }
2162
2163         if (further_blank_line){
2164                 os << '\n';
2165                 texrow.newline();
2166         }
2167
2168         if (!spacing.isDefault()
2169             && (!par || !par->HasSameLayout(this))) {
2170                 os << spacing.writeEnvirEnd() << "\n";
2171                 texrow.newline();
2172         }
2173         
2174         // we don't need it for the last paragraph!!!
2175         if (next && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2176               par->footnoteflag == LyXParagraph::NO_FOOTNOTE)) {
2177                 os << '\n';
2178                 texrow.newline();
2179         }
2180
2181         lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2182         return par;
2183 }
2184
2185
2186 // This one spits out the text of the paragraph
2187 bool LyXParagraph::SimpleTeXOnePar(ostream & os, TexRow & texrow)
2188 {
2189         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
2190
2191         if (table)
2192                 return SimpleTeXOneTablePar(os, texrow);
2193
2194         bool return_value = false;
2195
2196         LyXLayout const & style =
2197                 textclasslist.Style(current_view->buffer()->params.textclass,
2198                                     GetLayout());
2199         LyXFont basefont, last_font;
2200
2201         // Maybe we have to create a optional argument.
2202         size_type main_body;
2203         if (style.labeltype != LABEL_MANUAL)
2204                 main_body = 0;
2205         else
2206                 main_body = BeginningOfMainBody();
2207
2208         if (main_body > 0) {
2209                 os << '[';
2210                 basefont = getFont(-2); // Get label font
2211         } else {
2212                 basefont = getFont(-1); // Get layout font
2213         }
2214
2215         int column = 0;
2216
2217         if (main_body >= 0
2218             && !text.size()
2219             && !IsDummy()) {
2220                 if (style.isCommand()) {
2221                         os << '{';
2222                         ++column;
2223                 } else if (align != LYX_ALIGN_LAYOUT) {
2224                         os << '{';
2225                         ++column;
2226                         return_value = true;
2227                 }
2228         }
2229  
2230         // Which font is currently active?
2231         LyXFont running_font(basefont);
2232         // Do we have an open font change?
2233         bool open_font = false;
2234
2235         texrow.start(this, 0);
2236
2237         for (size_type i = 0; i < size(); ++i) {
2238                 ++column;
2239                 // First char in paragraph or after label?
2240                 if (i == main_body && !IsDummy()) {
2241                         if (main_body > 0) {
2242                                 if (open_font) {
2243                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
2244                                         open_font = false;
2245                                 }
2246                                 basefont = getFont(-1); // Now use the layout font
2247                                 running_font = basefont;
2248                                 os << ']';
2249                                 ++column;
2250                         }
2251                         if (style.isCommand()) {
2252                                 os << '{';
2253                                 ++column;
2254                         } else if (align != LYX_ALIGN_LAYOUT) {
2255                                 os << "{\\par";
2256                                 column += 4;
2257                                 return_value = true;
2258                         }
2259
2260                         if (noindent) {
2261                                 os << "\\noindent ";
2262                                 column += 10;
2263                         }
2264                         switch (align) {
2265                         case LYX_ALIGN_NONE:
2266                         case LYX_ALIGN_BLOCK:
2267                         case LYX_ALIGN_LAYOUT:
2268                         case LYX_ALIGN_SPECIAL:
2269                                 break;
2270                         case LYX_ALIGN_LEFT:
2271                                 if (getParLanguage()->lang != "hebrew") {
2272                                         os << "\\raggedright ";
2273                                         column+= 13;
2274                                 } else {
2275                                         os << "\\raggedleft ";
2276                                         column+= 12;
2277                                 }
2278                                 break;
2279                         case LYX_ALIGN_RIGHT:
2280                                 if (getParLanguage()->lang != "hebrew") {
2281                                         os << "\\raggedleft ";
2282                                         column+= 12;
2283                                 } else {
2284                                         os << "\\raggedright ";
2285                                         column+= 13;
2286                                 }
2287                                 break;
2288                         case LYX_ALIGN_CENTER:
2289                                 os << "\\centering ";
2290                                 column+= 11;
2291                                 break;
2292                         }        
2293                 }
2294
2295                 int c = GetChar(i);
2296
2297                 // Fully instantiated font
2298                 LyXFont font = getFont(i);
2299                 LyXParagraph * p = 0;
2300                 if (i == 0 && previous && 
2301                     previous->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2302                     (p = PreviousBeforeFootnote()) != 0)
2303                         last_font = p->getFont(p->size()-1);
2304                 else
2305                         last_font = running_font;
2306
2307                 // Spaces at end of font change are simulated to be
2308                 // outside font change, i.e. we write "\textXX{text} "
2309                 // rather than "\textXX{text }". (Asger)
2310                 if (open_font && c == ' ' && i <= size() - 2 
2311                     && !getFont(i+1).equalExceptLatex(running_font) 
2312                     && !getFont(i+1).equalExceptLatex(font)) {
2313                         font = getFont(i + 1);
2314                 }
2315                 // We end font definition before blanks
2316                 if (!font.equalExceptLatex(running_font) && open_font) {
2317                         column += running_font.latexWriteEndChanges(os,
2318                                                                     basefont,
2319                                                                     (i == main_body-1) ? basefont : font);
2320                         running_font = basefont;
2321                         open_font = false;
2322                 }
2323
2324                 // Blanks are printed before start of fontswitch
2325                 if (c == ' '){
2326                         // Do not print the separation of the optional argument
2327                         if (i != main_body - 1) {
2328                                 SimpleTeXBlanks(os, texrow, i,
2329                                                 column, font, style);
2330                         }
2331                 }
2332
2333                 // Do we need to change font?
2334                 if (!font.equalExceptLatex(running_font)
2335                     && i != main_body-1) {
2336                         column += font.latexWriteStartChanges(os, basefont,
2337                                                               last_font);
2338                         running_font = font;
2339                         open_font = true;
2340                 }
2341
2342                 if (c == LyXParagraph::META_NEWLINE) {
2343                         // newlines are handled differently here than
2344                         // the default in SimpleTeXSpecialChars().
2345                         if (!style.newline_allowed
2346                             || font.latex() == LyXFont::ON) {
2347                                 os << '\n';
2348                         } else {
2349                                 if (open_font) {
2350                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
2351                                         open_font = false;
2352                                 }
2353                                 basefont = getFont(-1);
2354                                 running_font = basefont;
2355                                 if (font.family() == 
2356                                     LyXFont::TYPEWRITER_FAMILY) {
2357                                         os << "~";
2358                                 }
2359                                 os << "\\\\\n";
2360                         }
2361                         texrow.newline();
2362                         texrow.start(this, i + 1);
2363                         column = 0;
2364                 } else {
2365                         SimpleTeXSpecialChars(os, texrow,
2366                                               font, running_font, basefont,
2367                                               open_font, style, i, column, c);
2368                 }
2369         }
2370
2371         // If we have an open font definition, we have to close it
2372         if (open_font) {
2373                 LyXParagraph * p = 0;
2374                 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2375                     && (p =  NextAfterFootnote()) != 0)
2376                         running_font.latexWriteEndChanges(os, basefont,
2377                                                           p->getFont(0));
2378                 else
2379                         running_font.latexWriteEndChanges(os, basefont, basefont);
2380         }
2381
2382         // Needed if there is an optional argument but no contents.
2383         if (main_body > 0 && main_body == size()) {
2384                 os << "]~";
2385                 return_value = false;
2386         }
2387
2388         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2389         return return_value;
2390 }
2391
2392
2393 // This one spits out the text of a table paragraph
2394 bool LyXParagraph::SimpleTeXOneTablePar(ostream & os, TexRow & texrow)
2395 {
2396         lyxerr[Debug::LATEX] << "SimpleTeXOneTablePar...     " << this << endl;
2397    
2398         bool return_value = false;
2399
2400         LyXLayout const & style = 
2401                 textclasslist.Style(current_view->buffer()->params.textclass,
2402                                     GetLayout());
2403  
2404         int column = 0;
2405         if (!IsDummy()) { // it is dummy if it is in a float!!!
2406                 if (style.isCommand()) {
2407                         os << '{';
2408                         ++column;
2409                 } else if (align != LYX_ALIGN_LAYOUT) {
2410                         os << '{';
2411                         ++column;
2412                         return_value = true;
2413                 }
2414                 if (noindent) {
2415                         os << "\\noindent ";
2416                         column += 10;
2417                 }
2418                 switch (align) {
2419                 case LYX_ALIGN_NONE:
2420                 case LYX_ALIGN_BLOCK:
2421                 case LYX_ALIGN_LAYOUT:
2422                 case LYX_ALIGN_SPECIAL: break;
2423                 case LYX_ALIGN_LEFT:
2424                         os << "\\raggedright ";
2425                         column+= 13;
2426                         break;
2427                 case LYX_ALIGN_RIGHT:
2428                         os << "\\raggedleft ";
2429                         column+= 12;
2430                         break;
2431                 case LYX_ALIGN_CENTER:
2432                         os << "\\centering ";
2433                         column+= 11;
2434                         break;
2435                 }
2436         }
2437
2438         LyXFont basefont = getFont(-1); // Get layout font
2439         // Which font is currently active?
2440         LyXFont running_font = basefont;
2441         LyXFont last_font;
2442         // Do we have an open font change?
2443         bool open_font = false;
2444         int current_cell_number = -1;
2445         int tmp = table->TexEndOfCell(os, current_cell_number);
2446         for (; tmp > 0 ; --tmp)
2447                 texrow.newline();
2448         
2449         texrow.start(this, 0);
2450
2451         bool is_rtl = getParLanguage()->RightToLeft;
2452         bool first_in_cell = true;
2453                 
2454         for (size_type i = 0; i < size(); ++i) {
2455                 char c = GetChar(i);
2456                 if (table->IsContRow(current_cell_number + 1)) {
2457                         if (c == LyXParagraph::META_NEWLINE)
2458                                 ++current_cell_number;
2459                         continue;
2460                 }
2461                 ++column;
2462
2463                 if (first_in_cell && is_rtl) {
2464                         os << "\\R{";
2465                         column += 3;
2466                         first_in_cell = false;
2467                 }
2468
2469                 // Fully instantiated font
2470                 LyXFont font = getFont(i);
2471                 last_font = running_font;
2472
2473                 // Spaces at end of font change are simulated to be
2474                 // outside font change.
2475                 // i.e. we write "\textXX{text} " rather than
2476                 // "\textXX{text }". (Asger)
2477                 if (open_font && c == ' ' && i <= size() - 2
2478                     && getFont(i + 1) != running_font
2479                     && getFont(i + 1) != font) {
2480                         font = getFont(i + 1);
2481                 }
2482
2483                 // We end font definition before blanks
2484                 if (font != running_font && open_font) {
2485                         column += running_font.latexWriteEndChanges(os,
2486                                                                     basefont,
2487                                                                     font);
2488                         running_font = basefont;
2489                         open_font = false;
2490                 }
2491                 // Blanks are printed before start of fontswitch
2492                 if (c == ' '){
2493                         SimpleTeXBlanks(os, texrow, i, column, font, style);
2494                 }
2495                 // Do we need to change font?
2496                 if (font != running_font) {
2497                         column += font.latexWriteStartChanges(os, basefont,
2498                                                               last_font);
2499                         running_font = font;
2500                         open_font = true;
2501                 }
2502                 // Do we need to turn on LaTeX mode?
2503                 if (font.latex() != running_font.latex()) {
2504                         if (font.latex() == LyXFont::ON
2505                             && style.needprotect) {
2506                                 os << "\\protect ";
2507                                 column += 9;
2508                         }
2509                 }
2510                 if (c == LyXParagraph::META_NEWLINE) {
2511                         // special case for inside a table
2512                         // different from default case in
2513                         // SimpleTeXSpecialChars()
2514                         if (open_font) {
2515                                 column += running_font
2516                                         .latexWriteEndChanges(os, basefont,
2517                                                               basefont);
2518                                 open_font = false;
2519                         }
2520                         basefont = getFont(-1);
2521                         running_font = basefont;
2522                         ++current_cell_number;
2523                         if (table->CellHasContRow(current_cell_number) >= 0) {
2524                                 TeXContTableRows(os, i + 1,
2525                                                  current_cell_number,
2526                                                  column, texrow);
2527                         }
2528                         if (is_rtl && !first_in_cell) {
2529                                 os << "}";
2530                                 first_in_cell = true;
2531                         }
2532
2533                         // if this cell follow only ContRows till end don't
2534                         // put the EndOfCell because it is put after the
2535                         // for(...)
2536                         if (table->ShouldBeVeryLastCell(current_cell_number)) {
2537                                 --current_cell_number;
2538                                 break;
2539                         }
2540                         int tmp = table->TexEndOfCell(os,
2541                                                       current_cell_number);
2542                         if (tmp > 0) {
2543                                 column = 0;
2544                         } else if (tmp < 0) {
2545                                 tmp = -tmp;
2546                         }
2547                         for (; tmp--;) {
2548                                 texrow.newline();
2549                         }
2550                         texrow.start(this, i + 1);
2551                 } else {
2552                         SimpleTeXSpecialChars(os, texrow,
2553                                               font, running_font, basefont,
2554                                               open_font, style, i, column, c);
2555                 }
2556         }
2557
2558         // If we have an open font definition, we have to close it
2559         if (open_font) {
2560                 running_font.latexWriteEndChanges(os, basefont, basefont);
2561         }
2562         ++current_cell_number;
2563         if (is_rtl && !first_in_cell)
2564                 os << "}";
2565         tmp = table->TexEndOfCell(os, current_cell_number);
2566         for (; tmp > 0; --tmp)
2567                 texrow.newline();
2568         lyxerr[Debug::LATEX] << "SimpleTeXOneTablePar...done " << this << endl;
2569         return return_value;
2570 }
2571
2572
2573 // This one spits out the text off ContRows in tables
2574 bool LyXParagraph::TeXContTableRows(ostream & os,
2575                                     LyXParagraph::size_type i,
2576                                     int current_cell_number,
2577                                     int & column, TexRow & texrow)
2578 {
2579         lyxerr[Debug::LATEX] << "TeXContTableRows...     " << this << endl;
2580         if (!table)
2581                 return false;
2582     
2583         char c;
2584    
2585         bool return_value = false;
2586         LyXLayout const & style =
2587                 textclasslist.Style(current_view->buffer()->params.textclass,
2588                                     GetLayout());
2589         LyXFont basefont = getFont(-1); // Get layout font
2590         LyXFont last_font;
2591         // Which font is currently active?
2592         LyXFont running_font = basefont;
2593         // Do we have an open font change?
2594         bool open_font = false;
2595
2596         size_type lastpos = i;
2597         int cell = table->CellHasContRow(current_cell_number);
2598         ++current_cell_number;
2599         while(cell >= 0) {
2600                 // first find the right position
2601                 i = lastpos;
2602                 for (; (i < size()) && (current_cell_number<cell); ++i) {
2603                         c = GetChar(i);
2604                         if (c == LyXParagraph::META_NEWLINE)
2605                                 ++current_cell_number;
2606                 }
2607                 lastpos = i;
2608                 c = GetChar(i);
2609                 if (table->Linebreaks(table->FirstVirtualCell(cell))) {
2610                         os << " \\\\\n";
2611                         texrow.newline();
2612                         column = 0;
2613                 } else if ((c != ' ') && (c != LyXParagraph::META_NEWLINE)) {
2614                         os << ' ';
2615                 }
2616
2617                 for (; i < size()
2618                              && (c = GetChar(i)) != LyXParagraph::META_NEWLINE;
2619                      ++i) {
2620                         ++column;
2621
2622                         // Fully instantiated font
2623                         LyXFont font = getFont(i);
2624                         last_font = running_font;
2625
2626                         // Spaces at end of font change are simulated to
2627                         // be outside font change. i.e. we write
2628                         // "\textXX{text} " rather than "\textXX{text }".
2629                         // (Asger)
2630                         if (open_font && c == ' ' && i <= size() - 2 
2631                             && getFont(i + 1) != running_font
2632                             && getFont(i + 1) != font) {
2633                                 font = getFont(i + 1);
2634                         }
2635
2636                         // We end font definition before blanks
2637                         if (font != running_font && open_font) {
2638                                 column += running_font.latexWriteEndChanges(os, basefont, font);
2639                                 running_font = basefont;
2640                                 open_font = false;
2641                         }
2642                         // Blanks are printed before start of fontswitch
2643                         if (c == ' '){
2644                                 SimpleTeXBlanks(os, texrow, i,
2645                                                 column, font, style);
2646                         }
2647                         // Do we need to change font?
2648                         if (font != running_font) {
2649                                 column +=
2650                                         font.latexWriteStartChanges(os,
2651                                                                     basefont,
2652                                                                     last_font);
2653                                 running_font = font;
2654                                 open_font = true;
2655                         }
2656                         // Do we need to turn on LaTeX mode?
2657                         if (font.latex() != running_font.latex()) {
2658                                 if (font.latex() == LyXFont::ON
2659                                     && style.needprotect) {
2660                                         os << "\\protect ";
2661                                         column += 9;
2662                                 }
2663                         }
2664                         SimpleTeXSpecialChars(os, texrow, font,
2665                                               running_font, basefont,
2666                                               open_font, style, i, column, c);
2667                 }
2668                 // If we have an open font definition, we have to close it
2669                 if (open_font) {
2670                         running_font.latexWriteEndChanges(os, basefont,
2671                                                           basefont);
2672                         open_font = false;
2673                 }
2674                 basefont = getFont(-1);
2675                 running_font = basefont;
2676                 cell = table->CellHasContRow(current_cell_number);
2677         }
2678         lyxerr[Debug::LATEX] << "TeXContTableRows...done " << this << endl;
2679         return return_value;
2680 }
2681
2682
2683 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2684 {
2685         bool retval = false;
2686         switch (c) {
2687         case LyXParagraph::META_HFILL:
2688                 sgml_string.erase();
2689                 break;
2690         case LyXParagraph::META_NEWLINE:
2691                 sgml_string = '\n';
2692                 break;
2693         case '&': 
2694                 sgml_string = "&amp;";
2695                 break;
2696         case '<': 
2697                 sgml_string = "&lt;"; 
2698                 break;
2699         case '>':
2700                 sgml_string = "&gt;"; 
2701                 break;
2702         case '$': 
2703                 sgml_string = "&dollar;"; 
2704                 break;
2705         case '#': 
2706                 sgml_string = "&num;";
2707                 break;
2708         case '%': 
2709                 sgml_string = "&percnt;";
2710                 break;
2711         case '[': 
2712                 sgml_string = "&lsqb;";
2713                 break;
2714         case ']': 
2715                 sgml_string = "&rsqb;";
2716                 break;
2717         case '{': 
2718                 sgml_string = "&lcub;";
2719                 break;
2720         case '}': 
2721                 sgml_string = "&rcub;";
2722                 break;
2723         case '~': 
2724                 sgml_string = "&tilde;";
2725                 break;
2726         case '"': 
2727                 sgml_string = "&quot;";
2728                 break;
2729         case '\\': 
2730                 sgml_string = "&bsol;";
2731                 break;
2732         case ' ':
2733                 retval = true;
2734                 sgml_string = ' ';
2735                 break;
2736         case '\0': // Ignore :-)
2737                 sgml_string.erase();
2738                 break;
2739         default:
2740                 sgml_string = c;
2741                 break;
2742         }
2743         return retval;
2744 }
2745
2746
2747 void LyXParagraph::SimpleDocBookOneTablePar(ostream & os, string & extra,
2748                                             int & desc_on, int depth) 
2749 {
2750         if (!table) return;
2751         lyxerr[Debug::LATEX] << "SimpleDocbookOneTablePar... " << this << endl;
2752         int column = 0;
2753         LyXFont font1, font2;
2754         char c;
2755         Inset * inset;
2756         size_type main_body;
2757         bool emph_flag = false;
2758         
2759         LyXLayout const & style =
2760                 textclasslist.Style(current_view->buffer()->params.textclass,
2761                                     GetLayout());
2762         
2763         if (style.labeltype != LABEL_MANUAL)
2764                 main_body = 0;
2765         else
2766                 main_body = BeginningOfMainBody();
2767         
2768         // Gets paragraph main font.
2769         if (main_body > 0)
2770                 font1 = style.labelfont;
2771         else
2772                 font1 = style.font;
2773         
2774         int char_line_count = depth;
2775         os << newlineAndDepth(depth);
2776         if (footnoteflag == LyXParagraph::NO_FOOTNOTE) {
2777                 os << "<INFORMALTABLE>"
2778                    << newlineAndDepth(++depth);
2779         }
2780         int current_cell_number = -1;
2781         int tmp = table->DocBookEndOfCell(os, current_cell_number, depth);
2782         
2783         // Parsing main loop.
2784         for (size_type i = 0; i < size(); ++i) {
2785                 c = GetChar(i);
2786                 if (table->IsContRow(current_cell_number+1)) {
2787                         if (c == LyXParagraph::META_NEWLINE)
2788                                 ++current_cell_number;
2789                         continue;
2790                 }
2791                 ++column;
2792                 
2793                 // Fully instantiated font
2794                 font2 = getFont(i);
2795                 
2796                 // Handle <emphasis> tag.
2797                 if (font1.emph() != font2.emph() && i) {
2798                         if (font2.emph() == LyXFont::ON) {
2799                                 os << "<emphasis>";
2800                                 emph_flag= true;
2801                         } else if (emph_flag) {
2802                                 os << "</emphasis>";
2803                                 emph_flag= false;
2804                         }
2805                 }
2806                 if (c == LyXParagraph::META_NEWLINE) {
2807                         // We have only to control for emphasis open here!
2808                         if (emph_flag) {
2809                                 os << "</emphasis>";
2810                                 emph_flag= false;
2811                         }
2812                         font1 = font2 = getFont(-1);
2813                         ++current_cell_number;
2814                         if (table->CellHasContRow(current_cell_number) >= 0) {
2815                                 DocBookContTableRows(os, extra, desc_on, i + 1,
2816                                                      current_cell_number,
2817                                                      column);
2818                         }
2819                         // if this cell follow only ContRows till end don't
2820                         // put the EndOfCell because it is put after the
2821                         // for(...)
2822                         if (table->ShouldBeVeryLastCell(current_cell_number)) {
2823                                 --current_cell_number;
2824                                 break;
2825                         }
2826                         tmp = table->DocBookEndOfCell(os,
2827                                                       current_cell_number,
2828                                                       depth);
2829                         
2830                         if (tmp > 0)
2831                                 column = 0;
2832                 } else if (c == LyXParagraph::META_INSET) {
2833                         inset = GetInset(i);
2834 #ifdef HAVE_SSTREAM
2835                         std::ostringstream ost;
2836                         inset->DocBook(ost);
2837                         string tmp_out = ost.str().c_str();
2838 #else
2839                         ostrstream ost;
2840                         inset->DocBook(ost);
2841                         ost << '\0';
2842                         char * ctmp = ost.str();
2843                         string tmp_out(ctmp);
2844                         delete [] ctmp;
2845 #endif
2846                         //
2847                         // This code needs some explanation:
2848                         // Two insets are treated specially
2849                         //   label if it is the first element in a
2850                         //   command paragraph
2851                         //         desc_on == 3
2852                         //   graphics inside tables or figure floats
2853                         //   can't go on
2854                         //   title (the equivalente in latex for this
2855                         //   case is caption
2856                         //   and title should come first
2857                         //         desc_on == 4
2858                         //
2859                         if(desc_on != 3 || i != 0) {
2860                                 if(tmp_out[0] == '@') {
2861                                         if(desc_on == 4)
2862                                                 extra += frontStrip(tmp_out,
2863                                                                     '@');
2864                                         else
2865                                                 os << frontStrip(tmp_out,
2866                                                                  '@');
2867                                 } else
2868                                         os << tmp_out;
2869                         }
2870                 } else if (font2.latex() == LyXFont::ON) {
2871                         // "TeX"-Mode on == > SGML-Mode on.
2872                         if (c != '\0')
2873                                 os << c;
2874                         ++char_line_count;
2875                 } else {
2876                         string sgml_string;
2877                         if (linuxDocConvertChar(c, sgml_string) 
2878                             && !style.free_spacing) {
2879                                 // in freespacing mode, spaces are
2880                                 // non-breaking characters
2881                                 // char is ' '
2882                                 if (desc_on == 1) {
2883                                         ++char_line_count;
2884                                         os << '\n'
2885                                            << "</term><listitem><para>";
2886                                         desc_on = 2;
2887                                 } else  {
2888                                         os << c;
2889                                 }
2890                         } else {
2891                                 os << sgml_string;
2892                         }
2893                 }
2894                 font1 = font2;
2895         }
2896         
2897         // Needed if there is an optional argument but no contents.
2898         if (main_body > 0 && main_body == size()) {
2899                 font1 = style.font;
2900         }
2901
2902         if (emph_flag) {
2903                 os << "</emphasis>";
2904         }
2905         
2906         ++current_cell_number;
2907         tmp = table->DocBookEndOfCell(os, current_cell_number, depth);
2908         // Resets description flag correctly.
2909         switch(desc_on){
2910         case 1:
2911                 // <term> not closed...
2912                 os << "</term>";
2913                 break;
2914         }
2915         if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
2916                 os << "</INFORMALTABLE>";
2917         os << '\n';
2918         lyxerr[Debug::LATEX] << "SimpleDocbookOneTablePar...done "
2919                              << this << endl;
2920 }
2921
2922
2923 void LyXParagraph::DocBookContTableRows(ostream & os, string & extra,
2924                                         int & desc_on,
2925                                         LyXParagraph::size_type i,
2926                                         int current_cell_number, int &column) 
2927
2928 {
2929         if (!table) return;
2930         
2931         lyxerr[Debug::LATEX] << "DocBookContTableRows... " << this << endl;
2932
2933         LyXFont font2;
2934         char c;
2935         Inset * inset;
2936         //string emph = "emphasis";
2937         bool emph_flag = false;
2938         int char_line_count = 0;
2939         
2940         LyXLayout const & style =
2941                 textclasslist.Style(current_view->buffer()->params.textclass,
2942                                     GetLayout());
2943         
2944         size_type main_body;
2945         if (style.labeltype != LABEL_MANUAL)
2946                 main_body = 0;
2947         else
2948                 main_body = BeginningOfMainBody();
2949         
2950         // Gets paragraph main font.
2951         LyXFont font1;
2952         if (main_body > 0)
2953                 font1 = style.labelfont;
2954         else
2955                 font1 = style.font;
2956         
2957         size_type lastpos = i;
2958         int cell = table->CellHasContRow(current_cell_number);
2959         ++current_cell_number;
2960         while(cell >= 0) {
2961                 // first find the right position
2962                 i = lastpos;
2963                 for (; i < size() && current_cell_number < cell; ++i) {
2964                         c = GetChar(i);
2965                         if (c == LyXParagraph::META_NEWLINE)
2966                                 ++current_cell_number;
2967                 }
2968                 lastpos = i;
2969                 c = GetChar(i);
2970                 // I don't know how to handle this so I comment it
2971                 // for the moment (Jug)
2972 //             if (table->Linebreaks(table->FirstVirtualCell(cell))) {
2973 //                     file += " \\\\\n";
2974 //                     column = 0;
2975 //             } else
2976                 if ((c != ' ') && (c != LyXParagraph::META_NEWLINE)) {
2977                         os << ' ';
2978                 }
2979
2980                 for (; i < size()
2981                              && (c = GetChar(i)) != LyXParagraph::META_NEWLINE;
2982                      ++i) {
2983                         ++column;
2984                         
2985                         // Fully instantiated font
2986                         font2 = getFont(i);
2987                         
2988                         // Handle <emphasis> tag.
2989                         if (font1.emph() != font2.emph() && i) {
2990                                 if (font2.emph() == LyXFont::ON) {
2991                                         os << "<emphasis>";
2992                                         emph_flag= true;
2993                                 } else if (emph_flag) {
2994                                         os << "</emphasis>";
2995                                         emph_flag= false;
2996                                 }
2997                         }
2998                         if (c == LyXParagraph::META_INSET) {
2999                                 inset = GetInset(i);
3000 #ifdef HAVE_SSTREAM
3001                                 std::ostringstream ost;
3002                                 inset->DocBook(ost);
3003                                 string tmp_out = ost.str().c_str();
3004 #else
3005                                 ostrstream ost;
3006                                 inset->DocBook(ost);
3007                                 ost << '\0';
3008                                 char * ctmp = ost.str();
3009                                 string tmp_out(ctmp);
3010                                 delete [] ctmp;
3011 #endif
3012                                 //
3013                                 // This code needs some explanation:
3014                                 // Two insets are treated specially
3015                                 //   label if it is the first element in a
3016                                 //   command paragraph
3017                                 //       desc_on == 3
3018                                 //   graphics inside tables or figure floats
3019                                 //   can't go on title (the equivalente in
3020                                 //   latex for this case is caption and title
3021                                 //   should come first
3022                                 //       desc_on == 4
3023                                 //
3024                                 if(desc_on != 3 || i != 0) {
3025                                         if(tmp_out[0] == '@') {
3026                                                 if(desc_on == 4)
3027                                                         extra += frontStrip(tmp_out, '@');
3028                                                 else
3029                                                         os << frontStrip(tmp_out, '@');
3030                                         } else
3031                                                 os << tmp_out;
3032                                 }
3033                         } else if (font2.latex() == LyXFont::ON) {
3034                                 // "TeX"-Mode on == > SGML-Mode on.
3035                                 if (c!= '\0')
3036                                         os << c;
3037                                 ++char_line_count;
3038                         } else {
3039                                 string sgml_string;
3040                                 if (linuxDocConvertChar(c, sgml_string) 
3041                                     && !style.free_spacing) {
3042                                         // in freespacing mode, spaces are
3043                                         // non-breaking characters
3044                                         // char is ' '
3045                                         if (desc_on == 1) {
3046                                                 ++char_line_count;
3047                                                 os << '\n'
3048                                                    << "</term><listitem><para>";
3049                                                 desc_on = 2;
3050                                         } else  {
3051                                                 os << c;
3052                                         }
3053                                 } else {
3054                                         os << sgml_string;
3055                                 }
3056                         }
3057                 }
3058                 // we have only to control for emphasis open here!
3059                 if (emph_flag) {
3060                         os << "</emphasis>";
3061                         emph_flag= false;
3062                 }
3063                 font1 = font2 = getFont(-1);
3064                 cell = table->CellHasContRow(current_cell_number);
3065         }
3066         lyxerr[Debug::LATEX] << "DocBookContTableRows...done " << this << endl;
3067 }
3068
3069
3070 void LyXParagraph::SimpleTeXBlanks(ostream & os, TexRow & texrow,
3071                                    LyXParagraph::size_type const i,
3072                                    int & column, LyXFont const & font,
3073                                    LyXLayout const & style)
3074 {
3075         if (column > tex_code_break_column
3076             && i 
3077             && GetChar(i - 1) != ' '
3078             && (i < size() - 1)
3079             // In LaTeX mode, we don't want to
3080             // break lines since some commands
3081             // do not like this
3082             && ! (font.latex() == LyXFont::ON)
3083             // same in FreeSpacing mode
3084             && !style.free_spacing
3085             // In typewriter mode, we want to avoid 
3086             // ! . ? : at the end of a line
3087             && !(font.family() == LyXFont::TYPEWRITER_FAMILY
3088                  && (GetChar(i-1) == '.'
3089                      || GetChar(i-1) == '?' 
3090                      || GetChar(i-1) == ':'
3091                      || GetChar(i-1) == '!'))) {
3092                 if (tex_code_break_column == 0) {
3093                         // in batchmode we need LaTeX to still
3094                         // see it as a space not as an extra '\n'
3095                         os << " %\n";
3096                 } else {
3097                         os << '\n';
3098                 }
3099                 texrow.newline();
3100                 texrow.start(this, i + 1);
3101                 column = 0;
3102         } else if (font.latex() == LyXFont::OFF) {
3103                 if (style.free_spacing) {
3104                         os << '~';
3105                 } else {
3106                         os << ' ';
3107                 }
3108         }
3109 }
3110
3111
3112 void LyXParagraph::SimpleTeXSpecialChars(ostream & os, TexRow & texrow,
3113                                          LyXFont & font,
3114                                          LyXFont & running_font,
3115                                          LyXFont & basefont,
3116                                          bool & open_font,
3117                                          LyXLayout const & style,
3118                                          LyXParagraph::size_type & i,
3119                                          int & column, char const c)
3120 {
3121         // Two major modes:  LaTeX or plain
3122         // Handle here those cases common to both modes
3123         // and then split to handle the two modes separately.
3124         switch (c) {
3125         case LyXParagraph::META_INSET: {
3126                 Inset * inset = GetInset(i);
3127                 if (inset) {
3128                         bool close = false;
3129                         int len = os.tellp();
3130                         if ((inset->LyxCode() == Inset::GRAPHICS_CODE
3131                              || inset->LyxCode() == Inset::MATH_CODE
3132                              || inset->LyxCode() == Inset::URL_CODE)
3133                             && running_font.isRightToLeft()) {
3134                                 os << "\\L{";
3135                                 close = true;
3136                         }
3137
3138                         int tmp = inset->Latex(os, style.isCommand(),
3139                                                style.free_spacing);
3140
3141                         if (close)
3142                                 os << "}";
3143
3144                         if (tmp) {
3145                                 column = 0;
3146                         } else {
3147                                 column += os.tellp() - len;
3148                         }
3149                         for (; tmp--;) {
3150                                 texrow.newline();
3151                         }
3152                 }
3153         }
3154         break;
3155
3156         case LyXParagraph::META_NEWLINE:
3157                 if (open_font) {
3158                         column += running_font.latexWriteEndChanges(os,
3159                                                                     basefont,
3160                                                                     basefont);
3161                         open_font = false;
3162                 }
3163                 basefont = getFont(-1);
3164                 running_font = basefont;
3165                 break;
3166
3167         case LyXParagraph::META_HFILL: 
3168                 os << "\\hfill{}";
3169                 column += 7;
3170                 break;
3171
3172         default:
3173                 // And now for the special cases within each mode
3174                 // Are we in LaTeX mode?
3175                 if (font.latex() == LyXFont::ON) {
3176                         // at present we only have one option
3177                         // but I'll leave it as a switch statement
3178                         // so its simpler to extend. (ARRae)
3179                         switch (c) {
3180                         default:
3181                                 // make sure that we will not print
3182                                 // error generating chars to the tex
3183                                 // file. This test would not be needed
3184                                 // if it were done in the buffer
3185                                 // itself.
3186                                 if (c != '\0') {
3187                                         os << c;
3188                                 }
3189                                 break;
3190                         }
3191                 } else {
3192                         // Plain mode (i.e. not LaTeX)
3193                         switch (c) {
3194                         case '\\': 
3195                                 os << "\\textbackslash{}";
3196                                 column += 15;
3197                                 break;
3198                 
3199                         case '°': case '±': case '²': case '³':  
3200                         case '×': case '÷': case '¹': case 'ª':
3201                         case 'º': case '¬': case 'µ':
3202                                 if (current_view->buffer()->params.inputenc == "latin1") {
3203                                         os << "\\ensuremath{"
3204                                            << c
3205                                            << '}';
3206                                         column += 13;
3207                                 } else {
3208                                         os << c;
3209                                 }
3210                                 break;
3211
3212                         case '|': case '<': case '>':
3213                                 // In T1 encoding, these characters exist
3214                                 if (lyxrc.fontenc == "T1") {
3215                                         os << c;
3216                                         //... but we should avoid ligatures
3217                                         if ((c == '>' || c == '<')
3218                                             && i <= size() - 2
3219                                             && GetChar(i + 1) == c){
3220                                                 os << "\\textcompwordmark{}";
3221                                                 column += 19;
3222                                         }
3223                                         break;
3224                                 }
3225                                 // Typewriter font also has them
3226                                 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
3227                                         os << c;
3228                                         break;
3229                                 } 
3230                                 // Otherwise, we use what LaTeX
3231                                 // provides us.
3232                                 switch(c) {
3233                                 case '<':
3234                                         os << "\\textless{}";
3235                                         column += 10;
3236                                         break;
3237                                 case '>':
3238                                         os << "\\textgreater{}";
3239                                         column += 13;
3240                                         break;
3241                                 case '|':
3242                                         os << "\\textbar{}";
3243                                         column += 9;
3244                                         break;
3245                                 }
3246                                 break;
3247
3248                         case '-': // "--" in Typewriter mode -> "-{}-"
3249                                 if (i <= size() - 2
3250                                     && GetChar(i + 1) == '-'
3251                                     && font.family() == LyXFont::TYPEWRITER_FAMILY) {
3252                                         os << "-{}";
3253                                         column += 2;
3254                                 } else {
3255                                         os << '-';
3256                                 }
3257                                 break;
3258
3259                         case '\"': 
3260                                 os << "\\char`\\\"{}";
3261                                 column += 9;
3262                                 break;
3263
3264                         case '£':
3265                                 if (current_view->buffer()->params.inputenc == "default") {
3266                                         os << "\\pounds{}";
3267                                         column += 8;
3268                                 } else {
3269                                         os << c;
3270                                 }
3271                                 break;
3272
3273                         case '$': case '&':
3274                         case '%': case '#': case '{':
3275                         case '}': case '_':
3276                                 os << '\\' << c;
3277                                 column += 1;
3278                                 break;
3279
3280                         case '~':
3281                                 os << "\\textasciitilde{}";
3282                                 column += 16;
3283                                 break;
3284
3285                         case '^':
3286                                 os << "\\textasciicircum{}";
3287                                 column += 17;
3288                                 break;
3289
3290                         case '*': case '[': case ']':
3291                                 // avoid being mistaken for optional arguments
3292                                 os << '{' << c << '}';
3293                                 column += 2;
3294                                 break;
3295
3296                         case ' ':
3297                                 // Blanks are printed before font switching.
3298                                 // Sure? I am not! (try nice-latex)
3299                                 // I am sure it's correct. LyX might be smarter
3300                                 // in the future, but for now, nothing wrong is
3301                                 // written. (Asger)
3302                                 break;
3303
3304                         default:
3305                                 /* idea for labels --- begin*/
3306                                 // Check for "LyX"
3307                                 if (c ==  'L'
3308                                     && i <= size() - 3
3309                                     && font.family() != LyXFont::TYPEWRITER_FAMILY
3310                                     && GetChar(i + 1) == 'y'
3311                                     && GetChar(i + 2) == 'X') {
3312                                         os << "\\LyX{}";
3313                                         i += 2;
3314                                         column += 5;
3315                                 }
3316                                 // Check for "TeX"
3317                                 else if (c == 'T'
3318                                          && i <= size() - 3
3319                                          && font.family() != LyXFont::TYPEWRITER_FAMILY
3320                                          && GetChar(i + 1) == 'e'
3321                                          && GetChar(i + 2) == 'X') {
3322                                         os << "\\TeX{}";
3323                                         i += 2;
3324                                         column += 5;
3325                                 }
3326                                 // Check for "LaTeX2e"
3327                                 else if (c == 'L'
3328                                          && i <= size() - 7
3329                                          && font.family() != LyXFont::TYPEWRITER_FAMILY
3330                                          && GetChar(i + 1) == 'a'
3331                                          && GetChar(i + 2) == 'T'
3332                                          && GetChar(i + 3) == 'e'
3333                                          && GetChar(i + 4) == 'X'
3334                                          && GetChar(i + 5) == '2'
3335                                          && GetChar(i + 6) == 'e') {
3336                                         os << "\\LaTeXe{}";
3337                                         i += 6;
3338                                         column += 8;
3339                                 }
3340                                 // Check for "LaTeX"
3341                                 else if (c == 'L'
3342                                          && i <= size() - 5
3343                                          && font.family() != LyXFont::TYPEWRITER_FAMILY
3344                                          && GetChar(i + 1) == 'a'
3345                                          && GetChar(i + 2) == 'T'
3346                                          && GetChar(i + 3) == 'e'
3347                                          && GetChar(i + 4) == 'X') {
3348                                         os << "\\LaTeX{}";
3349                                         i += 4;
3350                                         column += 7;
3351                                         /* idea for labels --- end*/ 
3352                                 } else if (c != '\0') {
3353                                         os << c;
3354                                 }
3355                                 break;
3356                         }
3357                 }
3358         }
3359 }
3360
3361
3362 #if 0
3363 bool LyXParagraph::RoffContTableRows(ostream & os,
3364                                      LyXParagraph::size_type i,
3365                                      int actcell)
3366 {
3367         if (!table)
3368                 return false;
3369
3370         LyXFont font1(LyXFont::ALL_INHERIT);
3371         LyXFont font2;
3372         Inset * inset;
3373         char c;
3374
3375         string fname2 = TmpFileName(string(), "RAT2");
3376         int lastpos = i;
3377         int cell = table->CellHasContRow(actcell);
3378         ++actcell;
3379         while(cell >= 0) {
3380                 // first find the right position
3381                 i = lastpos;
3382                 for (; i < size() && actcell < cell; ++i) {
3383                         c = GetChar(i);
3384                         if (c == LyXParagraph::META_NEWLINE)
3385                                 ++actcell;
3386                 }
3387                 lastpos = i;
3388                 c = GetChar(i);
3389                 if ((c != ' ') && (c != LyXParagraph::META_NEWLINE))
3390                         os << " ";
3391                 for (; i < size()
3392                              && (c = GetChar(i)) != LyXParagraph::META_NEWLINE;
3393                      ++i) {
3394                         font2 = GetFontSettings(i);
3395                         if (font1.latex() != font2.latex()) {
3396                                 if (font2.latex() != LyXFont::OFF)
3397                                         continue;
3398                         }
3399                         c = GetChar(i);
3400                         switch (c) {
3401                         case LyXParagraph::META_INSET:
3402                                 if ((inset = GetInset(i))) {
3403 #ifdef HAVE_SSTREAM
3404                                         stringstream ss(ios::in | ios::out);
3405                                         inset->Ascii(ss);
3406                                         ss.seekp(0);
3407                                         ss.get(c);
3408                                         while (!ss) {
3409                                                 if (c == '\\')
3410                                                         os << "\\\\";
3411                                                 else
3412                                                         os << c;
3413                                                 ss.get(c);
3414                                         }
3415 #else
3416                                         strstream ss;
3417                                         inset->Ascii(ss);
3418                                         ss.seekp(0);
3419                                         ss.get(c);
3420                                         while (!ss) {
3421                                                 if (c == '\\')
3422                                                         os << "\\\\";
3423                                                 else
3424                                                         os << c;
3425                                                 ss.get(c);
3426                                         }
3427                                         delete [] ss.str();
3428 #endif
3429                                 }
3430                                 break;
3431                         case LyXParagraph::META_NEWLINE:
3432                                 break;
3433                         case LyXParagraph::META_HFILL: 
3434                                 break;
3435                         case '\\': 
3436                                 os << "\\\\";
3437                                 break;
3438                         default:
3439                                 if (c != '\0')
3440                                         os << c;
3441                                 else
3442                                         lyxerr.debug() << "RoffAsciiTable: "
3443                                                 "NULL char in structure."
3444                                                        << endl;
3445                                 break;
3446                         }
3447                 }
3448                 cell = table->CellHasContRow(actcell);
3449         }
3450         return true;
3451 }
3452 #endif
3453
3454
3455 LyXParagraph * LyXParagraph::TeXDeeper(ostream & os, TexRow & texrow,
3456                                        ostream & foot,
3457                                        TexRow & foot_texrow,
3458                                        int & foot_count)
3459 {
3460         lyxerr[Debug::LATEX] << "TeXDeeper...     " << this << endl;
3461         LyXParagraph * par = this;
3462
3463         while (par &&
3464                (par->depth == depth) &&
3465                (par->footnoteflag == footnoteflag)) {
3466                 if (par->IsDummy())
3467                         lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
3468                 if (textclasslist.Style(current_view->buffer()->params.textclass, 
3469                                         par->layout).isEnvironment()
3470                     || par->pextra_type != PEXTRA_NONE) {
3471                         par = par->TeXEnvironment(os, texrow,
3472                                                   foot, foot_texrow,
3473                                                   foot_count);
3474                 } else {
3475                         par = par->TeXOnePar(os, texrow,
3476                                              foot, foot_texrow,
3477                                              foot_count);
3478                 }
3479         }
3480         lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
3481
3482         return par;
3483 }
3484
3485
3486 LyXParagraph * LyXParagraph::TeXEnvironment(ostream & os, TexRow & texrow,
3487                                             ostream & foot,
3488                                             TexRow & foot_texrow,
3489                                             int & foot_count)
3490 {
3491         bool eindent_open = false;
3492         bool foot_this_level = false;
3493         // flags when footnotetext should be appended to file.
3494         static bool minipage_open = false;
3495         static int minipage_open_depth = 0;
3496         char par_sep = current_view->buffer()->params.paragraph_separation;
3497     
3498         lyxerr[Debug::LATEX] << "TeXEnvironment...     " << this << endl;
3499         if (IsDummy())
3500                 lyxerr << "ERROR (LyXParagraph::TeXEnvironment)" << endl;
3501
3502         LyXLayout const & style =
3503                 textclasslist.Style(current_view->buffer()->params.textclass,
3504                                     layout);
3505        
3506         if (pextra_type == PEXTRA_INDENT) {
3507                 if (!pextra_width.empty()) {
3508                         os << "\\begin{LyXParagraphIndent}{"
3509                            << pextra_width << "}\n";
3510                 } else {
3511                         //float ib = atof(pextra_widthp.c_str())/100;
3512                         // string can't handle floats at present (971109)
3513                         // so I'll do a conversion by hand knowing that
3514                         // the limits are 0.0 to 1.0. ARRae.
3515                         os << "\\begin{LyXParagraphIndent}{";
3516                         switch (pextra_widthp.length()) {
3517                         case 3:
3518                                 os << "1.00";
3519                                 break;
3520                         case 2:
3521                                 os << "0."
3522                                    << pextra_widthp;
3523                                 break;
3524                         case 1:
3525                                 os << "0.0"
3526                                    << pextra_widthp;
3527                         }
3528                         os << "\\columnwidth}\n";
3529                 }
3530                 texrow.newline();
3531                 eindent_open = true;
3532         }
3533         if ((pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3534                 if (pextra_hfill && Previous() &&
3535                     (Previous()->pextra_type == PEXTRA_MINIPAGE)) {
3536                         os << "\\hfill{}\n";
3537                         texrow.newline();
3538                 }
3539                 if (par_sep == BufferParams::PARSEP_INDENT) {
3540                         os << "{\\setlength\\parindent{0pt}\n";
3541                         texrow.newline();
3542                 }
3543                 os << "\\begin{minipage}";
3544                 switch(pextra_alignment) {
3545                 case MINIPAGE_ALIGN_TOP:
3546                         os << "[t]";
3547                         break;
3548                 case MINIPAGE_ALIGN_MIDDLE:
3549                         os << "[m]";
3550                         break;
3551                 case MINIPAGE_ALIGN_BOTTOM:
3552                         os << "[b]";
3553                         break;
3554                 }
3555                 if (!pextra_width.empty()) {
3556                         os << '{' << pextra_width << "}\n";
3557                 } else {
3558                         //float ib = atof(par->pextra_width.c_str())/100;
3559                         // string can't handle floats at present
3560                         // so I'll do a conversion by hand knowing that
3561                         // the limits are 0.0 to 1.0. ARRae.
3562                         os << '{';
3563                         switch (pextra_widthp.length()) {
3564                         case 3:
3565                                 os << "1.00";
3566                                 break;
3567                         case 2:
3568                                 os << "0."
3569                                    << pextra_widthp;
3570                                 break;
3571                         case 1:
3572                                 os << "0.0"
3573                                    << pextra_widthp;
3574                         }
3575                         os << "\\columnwidth}\n";
3576                 }
3577                 texrow.newline();
3578                 if (par_sep == BufferParams::PARSEP_INDENT) {
3579                         os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3580                         texrow.newline();
3581                 }
3582                 minipage_open = true;
3583                 minipage_open_depth = depth;
3584         }
3585
3586 #ifdef WITH_WARNINGS
3587 #warning Define FANCY_FOOTNOTE_CODE to re-enable Allan footnote code
3588         //I disabled it because it breaks when lists span on several
3589         //pages (JMarc)
3590 #endif
3591         if (style.isEnvironment()){
3592                 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
3593 #ifdef FANCY_FOOTNOTE_CODE
3594                         if (foot_count < 0) {
3595                                 // flag that footnote[mark][text] should be
3596                                 // used for any footnotes from now on
3597                                 foot_count = 0;
3598                                 foot_this_level = true;
3599                         }
3600 #endif
3601                         os << "\\begin{" << style.latexname() << "}{"
3602                            << labelwidthstring << "}\n";
3603                 } else if (style.labeltype == LABEL_BIBLIO) {
3604                         // ale970405
3605                         os << "\\begin{" << style.latexname() << "}{"
3606                            << bibitemWidthest(current_view->painter())
3607                            << "}\n";
3608                 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
3609 #ifdef FANCY_FOOTNOTE_CODE
3610                         if (foot_count < 0) {
3611                                 // flag that footnote[mark][text] should be
3612                                 // used for any footnotes from now on
3613                                 foot_count = 0;
3614                                 foot_this_level = true;
3615                         }
3616 #endif
3617                         os << "\\begin{" << style.latexname() << '}'
3618                            << style.latexparam() << '\n';
3619                 } else 
3620                         os << "\\begin{" << style.latexname() << '}'
3621                            << style.latexparam() << '\n';
3622                 texrow.newline();
3623         }
3624         LyXParagraph * par = this;
3625         do {
3626                 par = par->TeXOnePar(os, texrow,
3627                                      foot, foot_texrow, foot_count);
3628
3629                 if (minipage_open && par && !style.isEnvironment() &&
3630                     (par->pextra_type == PEXTRA_MINIPAGE) &&
3631                     par->pextra_start_minipage) {
3632                         os << "\\end{minipage}\n";
3633                         texrow.newline();
3634                         if (par_sep == BufferParams::PARSEP_INDENT) {
3635                                 os << "}\n";
3636                                 texrow.newline();
3637                         }
3638                         minipage_open = false;
3639                 }
3640                 if (par && par->depth > depth) {
3641                         if (textclasslist.Style(current_view->buffer()->params.textclass,
3642                                                 par->layout).isParagraph()
3643                             && !par->table
3644                             // Thinko!
3645                             // How to handle this? (Lgb)
3646                             //&& !suffixIs(os, "\n\n")
3647                                 ) {
3648                                 // There should be at least one '\n' already
3649                                 // but we need there to be two for Standard 
3650                                 // paragraphs that are depth-increment'ed to be
3651                                 // output correctly.  However, tables can
3652                                 // also be paragraphs so don't adjust them.
3653                                 // ARRae
3654                                 // Thinkee:
3655                                 // Will it ever harm to have one '\n' too
3656                                 // many? i.e. that we sometimes will have
3657                                 // three in a row. (Lgb)
3658                                 os << '\n';
3659                                 texrow.newline();
3660                         }
3661                         par = par->TeXDeeper(os, texrow,
3662                                              foot, foot_texrow, foot_count);
3663                 }
3664                 if (par && par->layout == layout && par->depth == depth &&
3665                     (par->pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3666                         if (par->pextra_hfill && par->Previous() &&
3667                             (par->Previous()->pextra_type == PEXTRA_MINIPAGE)){
3668                                 os << "\\hfill{}\n";
3669                                 texrow.newline();
3670                         }
3671                         if (par_sep == BufferParams::PARSEP_INDENT) {
3672                                 os << "{\\setlength\\parindent{0pt}\n";
3673                                 texrow.newline();
3674                         }
3675                         os << "\\begin{minipage}";
3676                         switch(par->pextra_alignment) {
3677                         case MINIPAGE_ALIGN_TOP:
3678                                 os << "[t]";
3679                                 break;
3680                         case MINIPAGE_ALIGN_MIDDLE:
3681                                 os << "[m]";
3682                                 break;
3683                         case MINIPAGE_ALIGN_BOTTOM:
3684                                 os << "[b]";
3685                                 break;
3686                         }
3687                         if (!par->pextra_width.empty()) {
3688                                 os << '{' << par->pextra_width << "}\n";
3689                         } else {
3690                                 //float ib = atof(par->pextra_widthp.c_str())/100;
3691                                 // string can't handle floats at present
3692                                 // so I'll do a conversion by hand knowing that
3693                                 // the limits are 0.0 to 1.0. ARRae.
3694                                 os << '{';
3695                                 switch (par->pextra_widthp.length()) {
3696                                 case 3:
3697                                         os << "1.00";
3698                                         break;
3699                                 case 2:
3700                                         os << "0." << par->pextra_widthp;
3701                                         break;
3702                                 case 1:
3703                                         os << "0.0" << par->pextra_widthp;
3704                                 }
3705                                 os << "\\columnwidth}\n";
3706                         }
3707                         texrow.newline();
3708                         if (par_sep == BufferParams::PARSEP_INDENT) {
3709                                 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3710                                 texrow.newline();
3711                         }
3712                         minipage_open = true;
3713                         minipage_open_depth = par->depth;
3714                 }
3715         } while (par
3716                  && par->layout == layout
3717                  && par->depth == depth
3718                  && par->pextra_type == pextra_type
3719                  && par->footnoteflag == footnoteflag);
3720  
3721         if (style.isEnvironment()) {
3722                 os << "\\end{" << style.latexname() << '}';
3723                 // maybe this should go after the minipage closes?
3724                 if (foot_this_level) {
3725                         if (foot_count >= 1) {
3726                                 if (foot_count > 1) {
3727                                         os << "\\addtocounter{footnote}{-"
3728                                            << foot_count - 1
3729                                            << '}';
3730                                 }
3731                                 os << foot;
3732                                 texrow += foot_texrow;
3733                                 foot.clear();
3734                                 foot_texrow.reset();
3735                                 foot_count = 0;
3736                         }
3737                 }
3738         }
3739         if (minipage_open && (minipage_open_depth == depth) &&
3740             (!par || par->pextra_start_minipage ||
3741              par->pextra_type != PEXTRA_MINIPAGE)) {
3742                 os << "\\end{minipage}\n";
3743                 texrow.newline();
3744                 if (par_sep == BufferParams::PARSEP_INDENT) {
3745                         os << "}\n";
3746                         texrow.newline();
3747                 }
3748                 if (par && par->pextra_type != PEXTRA_MINIPAGE) {
3749                         os << "\\medskip\n\n";
3750                         texrow.newline();
3751                         texrow.newline();
3752                 }
3753                 minipage_open = false;
3754         }
3755         if (eindent_open) {
3756                 os << "\\end{LyXParagraphIndent}\n";
3757                 texrow.newline();
3758         }
3759         if (!(par && (par->pextra_type == PEXTRA_MINIPAGE) 
3760               && par->pextra_hfill)) {
3761                 os << '\n';
3762                 texrow.newline();
3763         }
3764         lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
3765         return par;  // ale970302
3766 }
3767
3768
3769 LyXParagraph * LyXParagraph::TeXFootnote(ostream & os, TexRow & texrow,
3770                                          ostream & foot, TexRow & foot_texrow,
3771                                          int & foot_count,
3772                                          bool parent_is_rtl)
3773 {
3774         lyxerr[Debug::LATEX] << "TeXFootnote...  " << this << endl;
3775         if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
3776                 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3777                         "No footnote!" << endl;
3778
3779         LyXParagraph * par = this;
3780         LyXLayout const & style =
3781                 textclasslist.Style(current_view->buffer()->params.textclass, 
3782                                     previous->GetLayout());
3783         
3784         if (style.needprotect && footnotekind != LyXParagraph::FOOTNOTE){
3785                 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3786                         "Float other than footnote in command"
3787                         " with moving argument is illegal" << endl;
3788         }
3789
3790         if (footnotekind != LyXParagraph::FOOTNOTE
3791             && footnotekind != LyXParagraph::MARGIN
3792             && os.tellp()
3793             // Thinko
3794             // How to solve this?
3795             //&& !suffixIs(file, '\n')
3796                 ) {
3797                 // we need to ensure that real floats like tables and figures
3798                 // have their \begin{} on a new line otherwise we can get
3799                 // incorrect results when using the endfloat.sty package
3800                 // especially if two floats follow one another.  ARRae 981022
3801                 // NOTE: if the file is length 0 it must have just been
3802                 //       written out so we assume it ended with a '\n'
3803                 // Thinkee:
3804                 // As far as I can see there is never any harm in writing
3805                 // a '\n' too much. Please tell me if I am wrong. (Lgb)
3806                 os << '\n';
3807                 texrow.newline();
3808         }
3809
3810         bool need_closing = false;
3811         bool is_rtl = isRightToLeftPar();
3812         if (is_rtl != parent_is_rtl) {
3813                 if (is_rtl)
3814                         os << "\\R{";
3815                 else
3816                         os << "\\L{";
3817                 need_closing = true;
3818         }
3819         
3820         BufferParams * params = &current_view->buffer()->params;
3821         bool footer_in_body = true;
3822         switch (footnotekind) {
3823         case LyXParagraph::FOOTNOTE:
3824                 if (style.intitle) {
3825                         os << "\\thanks{\n";
3826                         footer_in_body = false;
3827                 } else {
3828                         if (foot_count == -1) {
3829                                 // we're at depth 0 so we can use:
3830                                 os << "\\footnote{%\n";
3831                                 footer_in_body = false;
3832                         } else {
3833                                 os << "\\footnotemark{}%\n";
3834                                 if (foot_count) {
3835                                         // we only need this when there are
3836                                         // multiple footnotes
3837                                         os << "\\stepcounter{footnote}";
3838                                 }
3839                                 os << "\\footnotetext{%\n";
3840                                 foot_texrow.start(this, 0);
3841                                 foot_texrow.newline();
3842                                 ++foot_count;
3843                         }
3844                 }
3845                 break;
3846         case LyXParagraph::MARGIN:
3847                 os << "\\marginpar{\n";
3848                 break;
3849         case LyXParagraph::FIG:
3850                 if (pextra_type == PEXTRA_FLOATFLT
3851                     && (!pextra_width.empty()
3852                         || !pextra_widthp.empty())) {
3853                         if (!pextra_width.empty())
3854                                 os << "\\begin{floatingfigure}{"
3855                                    << pextra_width << "}\n";
3856                         else
3857                                 os << "\\begin{floatingfigure}{"
3858                                    << atoi(pextra_widthp.c_str())/100.0
3859                                    << "\\textwidth}\n";
3860                 } else {
3861                         os << "\\begin{figure}";
3862                         if (!params->float_placement.empty()) { 
3863                                 os << '[' << params->float_placement << "]\n";
3864                         } else {
3865                                 os << '\n';
3866                         }
3867                 }
3868                 break;
3869         case LyXParagraph::TAB:
3870                 os << "\\begin{table}";
3871                 if (!params->float_placement.empty()) { 
3872                         os << '[' << params->float_placement << "]\n";
3873                 } else {
3874                         os << '\n';
3875                 }
3876                 break;
3877         case LyXParagraph::WIDE_FIG:
3878                 os << "\\begin{figure*}";
3879                 if (!params->float_placement.empty()) { 
3880                         os << '[' << params->float_placement << "]\n";
3881                 } else {
3882                         os << '\n';
3883                 }
3884                 break;
3885         case LyXParagraph::WIDE_TAB:
3886                 os << "\\begin{table*}";
3887                 if (!params->float_placement.empty()) { 
3888                         os << '[' << params->float_placement << "]\n";
3889                 } else {
3890                         os << '\n';
3891                 }
3892                 break;
3893         case LyXParagraph::ALGORITHM:
3894                 os << "\\begin{algorithm}\n";
3895                 break;
3896         }
3897         texrow.newline();
3898    
3899         if (footnotekind != LyXParagraph::FOOTNOTE
3900             || !footer_in_body) {
3901                 // Process text for all floats except footnotes in body
3902                 do {
3903                         LyXLayout const & style =
3904                                 textclasslist
3905                                 .Style(current_view->buffer()->params
3906                                        .textclass,
3907                                        par->layout);
3908                         if (par->IsDummy())
3909                                 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3910                                        << endl;
3911                         if (style.isEnvironment()
3912                             || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3913                                 // Allows the use of minipages within float
3914                                 // environments. Shouldn't be circular because
3915                                 // we don't support footnotes inside
3916                                 // floats (yet). ARRae
3917                                 par = par->TeXEnvironment(os, texrow,
3918                                                           foot, foot_texrow,
3919                                                           foot_count);
3920                         } else {
3921                                 par = par->TeXOnePar(os, texrow,
3922                                                      foot, foot_texrow,
3923                                                      foot_count);
3924                         }
3925                         
3926                         if (par && !par->IsDummy() && par->depth > depth) {
3927                                 par = par->TeXDeeper(os, texrow,
3928                                                      foot, foot_texrow,
3929                                                      foot_count);
3930                         }
3931                 } while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3932         } else {
3933                 // process footnotes > depth 0 or in environments separately
3934                 // NOTE: Currently don't support footnotes within footnotes
3935                 //       even though that is possible using the \footnotemark
3936 #ifdef HAVE_SSTREAM
3937                 std::ostringstream dummy;
3938 #else
3939                 ostrstream dummy;
3940 #endif
3941                 TexRow dummy_texrow;
3942                 int dummy_count = 0;
3943                 do {
3944                         LyXLayout const & style =
3945                                 textclasslist
3946                                 .Style(current_view->buffer()->params
3947                                        .textclass,
3948                                        par->layout);
3949                         if (par->IsDummy())
3950                                 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3951                                        << endl;
3952                         if (style.isEnvironment()
3953                             || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3954                                 // Allows the use of minipages within float
3955                                 // environments. Shouldn't be circular because
3956                                 // we don't support footnotes inside
3957                                 // floats (yet). ARRae
3958                                 par = par->TeXEnvironment(foot, foot_texrow,
3959                                                           dummy, dummy_texrow,
3960                                                           dummy_count);
3961                         } else {
3962                                 par = par->TeXOnePar(foot, foot_texrow,
3963                                                      dummy, dummy_texrow,
3964                                                      dummy_count);
3965                         }
3966
3967                         if (par && !par->IsDummy() && par->depth > depth) {
3968                                 par = par->TeXDeeper(foot, foot_texrow,
3969                                                      dummy, dummy_texrow,
3970                                                      dummy_count);
3971                         }
3972                 } while (par
3973                          && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3974                 if (dummy_count) {
3975                         lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3976                                 "Footnote in a Footnote -- not supported"
3977                                << endl;
3978                 }
3979 #ifndef HAVE_OSTREAM
3980                 delete [] dummy.str();
3981 #endif
3982         }
3983
3984         switch (footnotekind) {
3985         case LyXParagraph::FOOTNOTE:
3986                 if (footer_in_body) {
3987                         // This helps tell which of the multiple
3988                         // footnotetexts an error was in.
3989                         foot << "}%\n";
3990                         foot_texrow.newline();
3991                 } else {
3992                         os << '}';
3993                 }
3994                 break;
3995         case LyXParagraph::MARGIN:
3996                 os << '}';
3997                 break;
3998         case LyXParagraph::FIG:
3999                 if (pextra_type == PEXTRA_FLOATFLT
4000                     && (!pextra_width.empty()
4001                         || !pextra_widthp.empty()))
4002                         os << "\\end{floatingfigure}";
4003                 else
4004                         os << "\\end{figure}";
4005                 break;
4006         case LyXParagraph::TAB:
4007                 os << "\\end{table}";
4008                 break;
4009         case LyXParagraph::WIDE_FIG:
4010                 os << "\\end{figure*}";
4011                 break;
4012         case LyXParagraph::WIDE_TAB:
4013                 os << "\\end{table*}";
4014                 break;
4015         case LyXParagraph::ALGORITHM:
4016                 os << "\\end{algorithm}";
4017                 break;
4018         }
4019
4020         if (need_closing)
4021                 os << "}";
4022
4023         if (footnotekind != LyXParagraph::FOOTNOTE
4024             && footnotekind != LyXParagraph::MARGIN) {
4025                 // we need to ensure that real floats like tables and figures
4026                 // have their \end{} on a line of their own otherwise we can
4027                 // get incorrect results when using the endfloat.sty package.
4028                 os << "\n";
4029                 texrow.newline();
4030         }
4031
4032         lyxerr[Debug::LATEX] << "TeXFootnote...done " << par->next << endl;
4033         return par;
4034 }
4035
4036
4037 bool LyXParagraph::IsDummy() const
4038 {
4039         return (footnoteflag == LyXParagraph::NO_FOOTNOTE && previous
4040                 && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE);
4041 }
4042
4043
4044 void LyXParagraph::SetPExtraType(int type, char const * width,
4045                                  char const * widthp)
4046 {
4047         pextra_type = type;
4048         pextra_width = width;
4049         pextra_widthp = widthp;
4050
4051         if (textclasslist.Style(current_view->buffer()->params.textclass, 
4052                                 layout).isEnvironment()) {
4053                 LyXParagraph
4054                         * par = this,
4055                         * ppar = par;
4056
4057                 while (par && (par->layout == layout)
4058                        && (par->depth == depth)) {
4059                         ppar = par;
4060                         par = par->Previous();
4061                         if (par)
4062                                 par = par->FirstPhysicalPar();
4063                         while (par && par->depth > depth) {
4064                                 par = par->Previous();
4065                                 if (par)
4066                                         par = par->FirstPhysicalPar();
4067                         }
4068                 }
4069                 par = ppar;
4070                 while (par && (par->layout == layout)
4071                        && (par->depth == depth)) {
4072                         par->pextra_type = type;
4073                         par->pextra_width = width;
4074                         par->pextra_widthp = widthp;
4075                         par = par->NextAfterFootnote();
4076                         if (par && (par->depth > depth))
4077                                 par->SetPExtraType(type, width, widthp);
4078                         while (par && ((par->depth > depth) || par->IsDummy()))
4079                                 par = par->NextAfterFootnote();
4080                 }
4081         }
4082 }
4083
4084
4085 void LyXParagraph::UnsetPExtraType()
4086 {
4087         if (pextra_type == PEXTRA_NONE)
4088                 return;
4089     
4090         pextra_type = PEXTRA_NONE;
4091         pextra_width.erase();
4092         pextra_widthp.erase();
4093
4094         if (textclasslist.Style(current_view->buffer()->params.textclass, 
4095                                 layout).isEnvironment()) {
4096                 LyXParagraph
4097                         * par = this,
4098                         * ppar = par;
4099
4100                 while (par && (par->layout == layout)
4101                        && (par->depth == depth)) {
4102                         ppar = par;
4103                         par = par->Previous();
4104                         if (par)
4105                                 par = par->FirstPhysicalPar();
4106                         while (par && par->depth > depth) {
4107                                 par = par->Previous();
4108                                 if (par)
4109                                         par = par->FirstPhysicalPar();
4110                         }
4111                 }
4112                 par = ppar;
4113                 while (par && (par->layout == layout)
4114                        && (par->depth == depth)) {
4115                         par->pextra_type = PEXTRA_NONE;
4116                         par->pextra_width.erase();
4117                         par->pextra_widthp.erase();
4118                         par = par->NextAfterFootnote();
4119                         if (par && (par->depth > depth))
4120                                 par->UnsetPExtraType();
4121                         while (par && ((par->depth > depth) || par->IsDummy()))
4122                                 par = par->NextAfterFootnote();
4123                 }
4124         }
4125 }
4126
4127
4128 bool LyXParagraph::IsHfill(size_type pos) const
4129 {
4130         return IsHfillChar(GetChar(pos));
4131 }
4132
4133
4134 bool LyXParagraph::IsInset(size_type pos) const
4135 {
4136         return IsInsetChar(GetChar(pos));
4137 }
4138
4139
4140 bool LyXParagraph::IsFloat(size_type pos) const
4141 {
4142         return IsFloatChar(GetChar(pos));
4143 }
4144
4145
4146 bool LyXParagraph::IsNewline(size_type pos) const
4147 {
4148         return pos >= 0 && IsNewlineChar(GetChar(pos));
4149 }
4150
4151
4152 bool LyXParagraph::IsSeparator(size_type pos) const
4153 {
4154         return IsSeparatorChar(GetChar(pos));
4155 }
4156
4157
4158 bool LyXParagraph::IsLineSeparator(size_type pos) const
4159 {
4160         return IsLineSeparatorChar(GetChar(pos));
4161 }
4162
4163
4164 bool LyXParagraph::IsKomma(size_type pos) const
4165 {
4166         return IsKommaChar(GetChar(pos));
4167 }
4168
4169
4170 /// Used by the spellchecker
4171 bool LyXParagraph::IsLetter(LyXParagraph::size_type pos) const
4172 {
4173         unsigned char c = GetChar(pos);
4174         if (IsLetterChar(c))
4175                 return true;
4176         // '\0' is not a letter, allthough every string contains "" (below)
4177         if( c == '\0')
4178                 return false;
4179         // We want to pass the ' and escape chars to ispell
4180         string extra = lyxrc.isp_esc_chars + '\'';
4181         char ch[2];
4182         ch[0] = c;
4183         ch[1] = 0;
4184         return contains(extra, ch);
4185 }
4186  
4187  
4188 bool LyXParagraph::IsWord(size_type pos ) const
4189 {
4190         return IsWordChar(GetChar(pos)) ;
4191 }
4192
4193
4194 Language const * LyXParagraph::getParLanguage() const 
4195 {
4196         if (size() > 0)
4197                 if (!table)
4198                         return FirstPhysicalPar()->GetFirstFontSettings()
4199                                 .language();
4200                 else {
4201                         for (size_type pos = 0; pos < size(); ++pos)
4202                                 if (IsNewline(pos))
4203                                         return GetFontSettings(pos).language();
4204                         return GetFirstFontSettings().language();
4205                 }
4206         else if (previous)
4207                 return previous->getParLanguage();
4208         else
4209                 return current_view->buffer()->params.language_info;
4210 }
4211
4212
4213 bool LyXParagraph::isRightToLeftPar() const
4214 {
4215         return lyxrc.rtl_support && !table && getParLanguage()->RightToLeft;
4216 }
4217
4218
4219 void LyXParagraph::ChangeLanguage(Language const * from, Language const * to)
4220 {
4221         for(size_type i = 0; i < size(); ++i) {
4222                 LyXFont font = GetFontSettings(i);
4223                 if (font.language() == from) {
4224                         font.setLanguage(to);
4225                         SetFont(i, font);
4226                 }
4227         }
4228 }
4229
4230
4231 bool LyXParagraph::isMultiLingual()
4232 {
4233         Language const * doc_language =
4234                 current_view->buffer()->params.language_info;
4235         for(size_type i = 0; i < size(); ++i) {
4236                 LyXFont font = GetFontSettings(i);
4237                 if (font.language() != doc_language)
4238                         return true;
4239         }
4240         return false;
4241 }