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