]> git.lyx.org Git - features.git/blob - src/paragraph.C
f7c4994512bc5ffa032f05e4f66047c632d3d114
[features.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                 // we don't need it for the last paragraph!!!
2130                 if (next && !(footnoteflag != LyXParagraph::NO_FOOTNOTE
2131                       && footnotekind != LyXParagraph::FOOTNOTE
2132                       && footnotekind != LyXParagraph::MARGIN
2133                       && (table
2134                           || (par
2135                               && par->table)))) {
2136                         // don't insert this if we would be adding it
2137                         // before or after a table in a float.  This 
2138                         // little trick is needed in order to allow
2139                         // use of tables in \subfigures or \subtables.
2140                         os << '\n';
2141                         texrow.newline();
2142                 }
2143         }
2144         
2145         further_blank_line = false;
2146         if (line_bottom) {
2147                 os << "\\lyxline{\\" << getFont(Last() - 1).latexSize() << '}';
2148                 further_blank_line = true;
2149         }
2150
2151         if (added_space_bottom.kind() != VSpace::NONE) {
2152                 os << added_space_bottom.asLatexCommand(current_view->buffer()->params);
2153                 further_blank_line = true;
2154         }
2155       
2156         if (pagebreak_bottom) {
2157                 os << "\\newpage";
2158                 further_blank_line = true;
2159         }
2160
2161         if (further_blank_line){
2162                 os << '\n';
2163                 texrow.newline();
2164         }
2165
2166         if (!spacing.isDefault()
2167             && (!par || !par->HasSameLayout(this))) {
2168                 os << spacing.writeEnvirEnd() << "\n";
2169                 texrow.newline();
2170         }
2171         
2172         // we don't need it for the last paragraph!!!
2173         if (next && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2174               par->footnoteflag == LyXParagraph::NO_FOOTNOTE)) {
2175                 os << '\n';
2176                 texrow.newline();
2177         }
2178
2179         lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2180         return par;
2181 }
2182
2183
2184 // This one spits out the text of the paragraph
2185 bool LyXParagraph::SimpleTeXOnePar(ostream & os, TexRow & texrow)
2186 {
2187         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
2188
2189         if (table)
2190                 return SimpleTeXOneTablePar(os, texrow);
2191
2192         bool return_value = false;
2193
2194         LyXLayout const & style =
2195                 textclasslist.Style(current_view->buffer()->params.textclass,
2196                                     GetLayout());
2197         LyXFont basefont, last_font;
2198
2199         // Maybe we have to create a optional argument.
2200         size_type main_body;
2201         if (style.labeltype != LABEL_MANUAL)
2202                 main_body = 0;
2203         else
2204                 main_body = BeginningOfMainBody();
2205
2206         if (main_body > 0) {
2207                 os << '[';
2208                 basefont = getFont(-2); // Get label font
2209         } else {
2210                 basefont = getFont(-1); // Get layout font
2211         }
2212
2213         int column = 0;
2214
2215         if (main_body >= 0
2216             && !text.size()
2217             && !IsDummy()) {
2218                 if (style.isCommand()) {
2219                         os << '{';
2220                         ++column;
2221                 } else if (align != LYX_ALIGN_LAYOUT) {
2222                         os << '{';
2223                         ++column;
2224                         return_value = true;
2225                 }
2226         }
2227  
2228         // Which font is currently active?
2229         LyXFont running_font(basefont);
2230         // Do we have an open font change?
2231         bool open_font = false;
2232
2233         texrow.start(this, 0);
2234
2235         for (size_type i = 0; i < size(); ++i) {
2236                 ++column;
2237                 // First char in paragraph or after label?
2238                 if (i == main_body && !IsDummy()) {
2239                         if (main_body > 0) {
2240                                 if (open_font) {
2241                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
2242                                         open_font = false;
2243                                 }
2244                                 basefont = getFont(-1); // Now use the layout font
2245                                 running_font = basefont;
2246                                 os << ']';
2247                                 ++column;
2248                         }
2249                         if (style.isCommand()) {
2250                                 os << '{';
2251                                 ++column;
2252                         } else if (align != LYX_ALIGN_LAYOUT) {
2253                                 os << "{\\par";
2254                                 column += 4;
2255                                 return_value = true;
2256                         }
2257
2258                         if (noindent) {
2259                                 os << "\\noindent ";
2260                                 column += 10;
2261                         }
2262                         switch (align) {
2263                         case LYX_ALIGN_NONE:
2264                         case LYX_ALIGN_BLOCK:
2265                         case LYX_ALIGN_LAYOUT:
2266                         case LYX_ALIGN_SPECIAL:
2267                                 break;
2268                         case LYX_ALIGN_LEFT:
2269                                 if (getParLanguage()->lang != "hebrew") {
2270                                         os << "\\raggedright ";
2271                                         column+= 13;
2272                                 } else {
2273                                         os << "\\raggedleft ";
2274                                         column+= 12;
2275                                 }
2276                                 break;
2277                         case LYX_ALIGN_RIGHT:
2278                                 if (getParLanguage()->lang != "hebrew") {
2279                                         os << "\\raggedleft ";
2280                                         column+= 12;
2281                                 } else {
2282                                         os << "\\raggedright ";
2283                                         column+= 13;
2284                                 }
2285                                 break;
2286                         case LYX_ALIGN_CENTER:
2287                                 os << "\\centering ";
2288                                 column+= 11;
2289                                 break;
2290                         }        
2291                 }
2292
2293                 int c = GetChar(i);
2294
2295                 // Fully instantiated font
2296                 LyXFont font = getFont(i);
2297                 LyXParagraph * p = 0;
2298                 if (i == 0 && previous && 
2299                     previous->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2300                     (p = PreviousBeforeFootnote()) != 0)
2301                         last_font = p->getFont(p->size()-1);
2302                 else
2303                         last_font = running_font;
2304
2305                 // Spaces at end of font change are simulated to be
2306                 // outside font change, i.e. we write "\textXX{text} "
2307                 // rather than "\textXX{text }". (Asger)
2308                 if (open_font && c == ' ' && i <= size() - 2 
2309                     && !getFont(i+1).equalExceptLatex(running_font) 
2310                     && !getFont(i+1).equalExceptLatex(font)) {
2311                         font = getFont(i + 1);
2312                 }
2313                 // We end font definition before blanks
2314                 if (!font.equalExceptLatex(running_font) && open_font) {
2315                         column += running_font.latexWriteEndChanges(os,
2316                                                                     basefont,
2317                                                                     (i == main_body-1) ? basefont : font);
2318                         running_font = basefont;
2319                         open_font = false;
2320                 }
2321
2322                 // Blanks are printed before start of fontswitch
2323                 if (c == ' '){
2324                         // Do not print the separation of the optional argument
2325                         if (i != main_body - 1) {
2326                                 SimpleTeXBlanks(os, texrow, i,
2327                                                 column, font, style);
2328                         }
2329                 }
2330
2331                 // Do we need to change font?
2332                 if (!font.equalExceptLatex(running_font)
2333                     && i != main_body-1) {
2334                         column += font.latexWriteStartChanges(os, basefont,
2335                                                               last_font);
2336                         running_font = font;
2337                         open_font = true;
2338                 }
2339
2340                 if (c == LyXParagraph::META_NEWLINE) {
2341                         // newlines are handled differently here than
2342                         // the default in SimpleTeXSpecialChars().
2343                         if (!style.newline_allowed
2344                             || font.latex() == LyXFont::ON) {
2345                                 os << '\n';
2346                         } else {
2347                                 if (open_font) {
2348                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
2349                                         open_font = false;
2350                                 }
2351                                 basefont = getFont(-1);
2352                                 running_font = basefont;
2353                                 if (font.family() == 
2354                                     LyXFont::TYPEWRITER_FAMILY) {
2355                                         os << "~";
2356                                 }
2357                                 os << "\\\\\n";
2358                         }
2359                         texrow.newline();
2360                         texrow.start(this, i + 1);
2361                         column = 0;
2362                 } else {
2363                         SimpleTeXSpecialChars(os, texrow,
2364                                               font, running_font, basefont,
2365                                               open_font, style, i, column, c);
2366                 }
2367         }
2368
2369         // If we have an open font definition, we have to close it
2370         if (open_font) {
2371                 LyXParagraph * p = 0;
2372                 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2373                     && (p =  NextAfterFootnote()) != 0)
2374                         running_font.latexWriteEndChanges(os, basefont,
2375                                                           p->getFont(0));
2376                 else
2377                         running_font.latexWriteEndChanges(os, basefont, basefont);
2378         }
2379
2380         // Needed if there is an optional argument but no contents.
2381         if (main_body > 0 && main_body == size()) {
2382                 os << "]~";
2383                 return_value = false;
2384         }
2385
2386         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2387         return return_value;
2388 }
2389
2390
2391 // This one spits out the text of a table paragraph
2392 bool LyXParagraph::SimpleTeXOneTablePar(ostream & os, TexRow & texrow)
2393 {
2394         lyxerr[Debug::LATEX] << "SimpleTeXOneTablePar...     " << this << endl;
2395    
2396         bool return_value = false;
2397
2398         LyXLayout const & style = 
2399                 textclasslist.Style(current_view->buffer()->params.textclass,
2400                                     GetLayout());
2401  
2402         int column = 0;
2403         if (!IsDummy()) { // it is dummy if it is in a float!!!
2404                 if (style.isCommand()) {
2405                         os << '{';
2406                         ++column;
2407                 } else if (align != LYX_ALIGN_LAYOUT) {
2408                         os << '{';
2409                         ++column;
2410                         return_value = true;
2411                 }
2412                 if (noindent) {
2413                         os << "\\noindent ";
2414                         column += 10;
2415                 }
2416                 switch (align) {
2417                 case LYX_ALIGN_NONE:
2418                 case LYX_ALIGN_BLOCK:
2419                 case LYX_ALIGN_LAYOUT:
2420                 case LYX_ALIGN_SPECIAL: break;
2421                 case LYX_ALIGN_LEFT:
2422                         os << "\\raggedright ";
2423                         column+= 13;
2424                         break;
2425                 case LYX_ALIGN_RIGHT:
2426                         os << "\\raggedleft ";
2427                         column+= 12;
2428                         break;
2429                 case LYX_ALIGN_CENTER:
2430                         os << "\\centering ";
2431                         column+= 11;
2432                         break;
2433                 }
2434         }
2435
2436         LyXFont basefont = getFont(-1); // Get layout font
2437         // Which font is currently active?
2438         LyXFont running_font = basefont;
2439         LyXFont last_font;
2440         // Do we have an open font change?
2441         bool open_font = false;
2442         int current_cell_number = -1;
2443         int tmp = table->TexEndOfCell(os, current_cell_number);
2444         for (; tmp > 0 ; --tmp)
2445                 texrow.newline();
2446         
2447         texrow.start(this, 0);
2448
2449         bool is_rtl = getParLanguage()->RightToLeft;
2450         bool first_in_cell = true;
2451                 
2452         for (size_type i = 0; i < size(); ++i) {
2453                 char c = GetChar(i);
2454                 if (table->IsContRow(current_cell_number + 1)) {
2455                         if (c == LyXParagraph::META_NEWLINE)
2456                                 ++current_cell_number;
2457                         continue;
2458                 }
2459                 ++column;
2460
2461                 if (first_in_cell && is_rtl) {
2462                         os << "\\R{";
2463                         column += 3;
2464                         first_in_cell = false;
2465                 }
2466
2467                 // Fully instantiated font
2468                 LyXFont font = getFont(i);
2469                 last_font = running_font;
2470
2471                 // Spaces at end of font change are simulated to be
2472                 // outside font change.
2473                 // i.e. we write "\textXX{text} " rather than
2474                 // "\textXX{text }". (Asger)
2475                 if (open_font && c == ' ' && i <= size() - 2
2476                     && getFont(i + 1) != running_font
2477                     && getFont(i + 1) != font) {
2478                         font = getFont(i + 1);
2479                 }
2480
2481                 // We end font definition before blanks
2482                 if (font != running_font && open_font) {
2483                         column += running_font.latexWriteEndChanges(os,
2484                                                                     basefont,
2485                                                                     font);
2486                         running_font = basefont;
2487                         open_font = false;
2488                 }
2489                 // Blanks are printed before start of fontswitch
2490                 if (c == ' '){
2491                         SimpleTeXBlanks(os, texrow, i, column, font, style);
2492                 }
2493                 // Do we need to change font?
2494                 if (font != running_font) {
2495                         column += font.latexWriteStartChanges(os, basefont,
2496                                                               last_font);
2497                         running_font = font;
2498                         open_font = true;
2499                 }
2500                 // Do we need to turn on LaTeX mode?
2501                 if (font.latex() != running_font.latex()) {
2502                         if (font.latex() == LyXFont::ON
2503                             && style.needprotect) {
2504                                 os << "\\protect ";
2505                                 column += 9;
2506                         }
2507                 }
2508                 if (c == LyXParagraph::META_NEWLINE) {
2509                         // special case for inside a table
2510                         // different from default case in
2511                         // SimpleTeXSpecialChars()
2512                         if (open_font) {
2513                                 column += running_font
2514                                         .latexWriteEndChanges(os, basefont,
2515                                                               basefont);
2516                                 open_font = false;
2517                         }
2518                         basefont = getFont(-1);
2519                         running_font = basefont;
2520                         ++current_cell_number;
2521                         if (table->CellHasContRow(current_cell_number) >= 0) {
2522                                 TeXContTableRows(os, i + 1,
2523                                                  current_cell_number,
2524                                                  column, texrow);
2525                         }
2526                         if (is_rtl && !first_in_cell) {
2527                                 os << "}";
2528                                 first_in_cell = true;
2529                         }
2530
2531                         // if this cell follow only ContRows till end don't
2532                         // put the EndOfCell because it is put after the
2533                         // for(...)
2534                         if (table->ShouldBeVeryLastCell(current_cell_number)) {
2535                                 --current_cell_number;
2536                                 break;
2537                         }
2538                         int tmp = table->TexEndOfCell(os,
2539                                                       current_cell_number);
2540                         if (tmp > 0) {
2541                                 column = 0;
2542                         } else if (tmp < 0) {
2543                                 tmp = -tmp;
2544                         }
2545                         for (; tmp--;) {
2546                                 texrow.newline();
2547                         }
2548                         texrow.start(this, i + 1);
2549                 } else {
2550                         SimpleTeXSpecialChars(os, texrow,
2551                                               font, running_font, basefont,
2552                                               open_font, style, i, column, c);
2553                 }
2554         }
2555
2556         // If we have an open font definition, we have to close it
2557         if (open_font) {
2558                 running_font.latexWriteEndChanges(os, basefont, basefont);
2559         }
2560         ++current_cell_number;
2561         if (is_rtl && !first_in_cell)
2562                 os << "}";
2563         tmp = table->TexEndOfCell(os, current_cell_number);
2564         for (; tmp > 0; --tmp)
2565                 texrow.newline();
2566         lyxerr[Debug::LATEX] << "SimpleTeXOneTablePar...done " << this << endl;
2567         return return_value;
2568 }
2569
2570
2571 // This one spits out the text off ContRows in tables
2572 bool LyXParagraph::TeXContTableRows(ostream & os,
2573                                     LyXParagraph::size_type i,
2574                                     int current_cell_number,
2575                                     int & column, TexRow & texrow)
2576 {
2577         lyxerr[Debug::LATEX] << "TeXContTableRows...     " << this << endl;
2578         if (!table)
2579                 return false;
2580     
2581         char c;
2582    
2583         bool return_value = false;
2584         LyXLayout const & style =
2585                 textclasslist.Style(current_view->buffer()->params.textclass,
2586                                     GetLayout());
2587         LyXFont basefont = getFont(-1); // Get layout font
2588         LyXFont last_font;
2589         // Which font is currently active?
2590         LyXFont running_font = basefont;
2591         // Do we have an open font change?
2592         bool open_font = false;
2593
2594         size_type lastpos = i;
2595         int cell = table->CellHasContRow(current_cell_number);
2596         ++current_cell_number;
2597         while(cell >= 0) {
2598                 // first find the right position
2599                 i = lastpos;
2600                 for (; (i < size()) && (current_cell_number<cell); ++i) {
2601                         c = GetChar(i);
2602                         if (c == LyXParagraph::META_NEWLINE)
2603                                 ++current_cell_number;
2604                 }
2605                 lastpos = i;
2606                 c = GetChar(i);
2607                 if (table->Linebreaks(table->FirstVirtualCell(cell))) {
2608                         os << " \\\\\n";
2609                         texrow.newline();
2610                         column = 0;
2611                 } else if ((c != ' ') && (c != LyXParagraph::META_NEWLINE)) {
2612                         os << ' ';
2613                 }
2614
2615                 for (; i < size()
2616                              && (c = GetChar(i)) != LyXParagraph::META_NEWLINE;
2617                      ++i) {
2618                         ++column;
2619
2620                         // Fully instantiated font
2621                         LyXFont font = getFont(i);
2622                         last_font = running_font;
2623
2624                         // Spaces at end of font change are simulated to
2625                         // be outside font change. i.e. we write
2626                         // "\textXX{text} " rather than "\textXX{text }".
2627                         // (Asger)
2628                         if (open_font && c == ' ' && i <= size() - 2 
2629                             && getFont(i + 1) != running_font
2630                             && getFont(i + 1) != font) {
2631                                 font = getFont(i + 1);
2632                         }
2633
2634                         // We end font definition before blanks
2635                         if (font != running_font && open_font) {
2636                                 column += running_font.latexWriteEndChanges(os, basefont, font);
2637                                 running_font = basefont;
2638                                 open_font = false;
2639                         }
2640                         // Blanks are printed before start of fontswitch
2641                         if (c == ' '){
2642                                 SimpleTeXBlanks(os, texrow, i,
2643                                                 column, font, style);
2644                         }
2645                         // Do we need to change font?
2646                         if (font != running_font) {
2647                                 column +=
2648                                         font.latexWriteStartChanges(os,
2649                                                                     basefont,
2650                                                                     last_font);
2651                                 running_font = font;
2652                                 open_font = true;
2653                         }
2654                         // Do we need to turn on LaTeX mode?
2655                         if (font.latex() != running_font.latex()) {
2656                                 if (font.latex() == LyXFont::ON
2657                                     && style.needprotect) {
2658                                         os << "\\protect ";
2659                                         column += 9;
2660                                 }
2661                         }
2662                         SimpleTeXSpecialChars(os, texrow, font,
2663                                               running_font, basefont,
2664                                               open_font, style, i, column, c);
2665                 }
2666                 // If we have an open font definition, we have to close it
2667                 if (open_font) {
2668                         running_font.latexWriteEndChanges(os, basefont,
2669                                                           basefont);
2670                         open_font = false;
2671                 }
2672                 basefont = getFont(-1);
2673                 running_font = basefont;
2674                 cell = table->CellHasContRow(current_cell_number);
2675         }
2676         lyxerr[Debug::LATEX] << "TeXContTableRows...done " << this << endl;
2677         return return_value;
2678 }
2679
2680
2681 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2682 {
2683         bool retval = false;
2684         switch (c) {
2685         case LyXParagraph::META_HFILL:
2686                 sgml_string.clear();
2687                 break;
2688         case LyXParagraph::META_NEWLINE:
2689                 sgml_string = '\n';
2690                 break;
2691         case '&': 
2692                 sgml_string = "&amp;";
2693                 break;
2694         case '<': 
2695                 sgml_string = "&lt;"; 
2696                 break;
2697         case '>':
2698                 sgml_string = "&gt;"; 
2699                 break;
2700         case '$': 
2701                 sgml_string = "&dollar;"; 
2702                 break;
2703         case '#': 
2704                 sgml_string = "&num;";
2705                 break;
2706         case '%': 
2707                 sgml_string = "&percnt;";
2708                 break;
2709         case '[': 
2710                 sgml_string = "&lsqb;";
2711                 break;
2712         case ']': 
2713                 sgml_string = "&rsqb;";
2714                 break;
2715         case '{': 
2716                 sgml_string = "&lcub;";
2717                 break;
2718         case '}': 
2719                 sgml_string = "&rcub;";
2720                 break;
2721         case '~': 
2722                 sgml_string = "&tilde;";
2723                 break;
2724         case '"': 
2725                 sgml_string = "&quot;";
2726                 break;
2727         case '\\': 
2728                 sgml_string = "&bsol;";
2729                 break;
2730         case ' ':
2731                 retval = true;
2732                 sgml_string = ' ';
2733                 break;
2734         case '\0': // Ignore :-)
2735                 sgml_string.clear();
2736                 break;
2737         default:
2738                 sgml_string = c;
2739                 break;
2740         }
2741         return retval;
2742 }
2743
2744
2745 void LyXParagraph::SimpleDocBookOneTablePar(ostream & os, string & extra,
2746                                             int & desc_on, int depth) 
2747 {
2748         if (!table) return;
2749         lyxerr[Debug::LATEX] << "SimpleDocbookOneTablePar... " << this << endl;
2750         int column = 0;
2751         LyXFont font1, font2;
2752         char c;
2753         Inset * inset;
2754         size_type main_body;
2755         bool emph_flag = false;
2756         
2757         LyXLayout const & style =
2758                 textclasslist.Style(current_view->buffer()->params.textclass,
2759                                     GetLayout());
2760         
2761         if (style.labeltype != LABEL_MANUAL)
2762                 main_body = 0;
2763         else
2764                 main_body = BeginningOfMainBody();
2765         
2766         // Gets paragraph main font.
2767         if (main_body > 0)
2768                 font1 = style.labelfont;
2769         else
2770                 font1 = style.font;
2771         
2772         int char_line_count = depth;
2773         os << newlineAndDepth(depth);
2774         if (footnoteflag == LyXParagraph::NO_FOOTNOTE) {
2775                 os << "<INFORMALTABLE>"
2776                    << newlineAndDepth(++depth);
2777         }
2778         int current_cell_number = -1;
2779         int tmp = table->DocBookEndOfCell(os, current_cell_number, depth);
2780         
2781         // Parsing main loop.
2782         for (size_type i = 0; i < size(); ++i) {
2783                 c = GetChar(i);
2784                 if (table->IsContRow(current_cell_number+1)) {
2785                         if (c == LyXParagraph::META_NEWLINE)
2786                                 ++current_cell_number;
2787                         continue;
2788                 }
2789                 ++column;
2790                 
2791                 // Fully instantiated font
2792                 font2 = getFont(i);
2793                 
2794                 // Handle <emphasis> tag.
2795                 if (font1.emph() != font2.emph() && i) {
2796                         if (font2.emph() == LyXFont::ON) {
2797                                 os << "<emphasis>";
2798                                 emph_flag= true;
2799                         } else if (emph_flag) {
2800                                 os << "</emphasis>";
2801                                 emph_flag= false;
2802                         }
2803                 }
2804                 if (c == LyXParagraph::META_NEWLINE) {
2805                         // We have only to control for emphasis open here!
2806                         if (emph_flag) {
2807                                 os << "</emphasis>";
2808                                 emph_flag= false;
2809                         }
2810                         font1 = font2 = getFont(-1);
2811                         ++current_cell_number;
2812                         if (table->CellHasContRow(current_cell_number) >= 0) {
2813                                 DocBookContTableRows(os, extra, desc_on, i + 1,
2814                                                      current_cell_number,
2815                                                      column);
2816                         }
2817                         // if this cell follow only ContRows till end don't
2818                         // put the EndOfCell because it is put after the
2819                         // for(...)
2820                         if (table->ShouldBeVeryLastCell(current_cell_number)) {
2821                                 --current_cell_number;
2822                                 break;
2823                         }
2824                         tmp = table->DocBookEndOfCell(os,
2825                                                       current_cell_number,
2826                                                       depth);
2827                         
2828                         if (tmp > 0)
2829                                 column = 0;
2830                 } else if (c == LyXParagraph::META_INSET) {
2831                         inset = GetInset(i);
2832 #ifdef HAVE_SSTREAM
2833                         std::ostringstream ost;
2834                         inset->DocBook(ost);
2835                         string tmp_out = ost.str().c_str();
2836 #else
2837                         ostrstream ost;
2838                         inset->DocBook(ost);
2839                         ost << '\0';
2840                         char * ctmp = ost.str();
2841                         string tmp_out(ctmp);
2842                         delete [] ctmp;
2843 #endif
2844                         //
2845                         // This code needs some explanation:
2846                         // Two insets are treated specially
2847                         //   label if it is the first element in a
2848                         //   command paragraph
2849                         //         desc_on == 3
2850                         //   graphics inside tables or figure floats
2851                         //   can't go on
2852                         //   title (the equivalente in latex for this
2853                         //   case is caption
2854                         //   and title should come first
2855                         //         desc_on == 4
2856                         //
2857                         if(desc_on != 3 || i != 0) {
2858                                 if(tmp_out[0] == '@') {
2859                                         if(desc_on == 4)
2860                                                 extra += frontStrip(tmp_out,
2861                                                                     '@');
2862                                         else
2863                                                 os << frontStrip(tmp_out,
2864                                                                  '@');
2865                                 } else
2866                                         os << tmp_out;
2867                         }
2868                 } else if (font2.latex() == LyXFont::ON) {
2869                         // "TeX"-Mode on == > SGML-Mode on.
2870                         if (c != '\0')
2871                                 os << c;
2872                         ++char_line_count;
2873                 } else {
2874                         string sgml_string;
2875                         if (linuxDocConvertChar(c, sgml_string) 
2876                             && !style.free_spacing) {
2877                                 // in freespacing mode, spaces are
2878                                 // non-breaking characters
2879                                 // char is ' '
2880                                 if (desc_on == 1) {
2881                                         ++char_line_count;
2882                                         os << '\n'
2883                                            << "</term><listitem><para>";
2884                                         desc_on = 2;
2885                                 } else  {
2886                                         os << c;
2887                                 }
2888                         } else {
2889                                 os << sgml_string;
2890                         }
2891                 }
2892                 font1 = font2;
2893         }
2894         
2895         // Needed if there is an optional argument but no contents.
2896         if (main_body > 0 && main_body == size()) {
2897                 font1 = style.font;
2898         }
2899
2900         if (emph_flag) {
2901                 os << "</emphasis>";
2902         }
2903         
2904         ++current_cell_number;
2905         tmp = table->DocBookEndOfCell(os, current_cell_number, depth);
2906         // Resets description flag correctly.
2907         switch(desc_on){
2908         case 1:
2909                 // <term> not closed...
2910                 os << "</term>";
2911                 break;
2912         }
2913         if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
2914                 os << "</INFORMALTABLE>";
2915         os << '\n';
2916         lyxerr[Debug::LATEX] << "SimpleDocbookOneTablePar...done "
2917                              << this << endl;
2918 }
2919
2920
2921 void LyXParagraph::DocBookContTableRows(ostream & os, string & extra,
2922                                         int & desc_on,
2923                                         LyXParagraph::size_type i,
2924                                         int current_cell_number, int &column) 
2925
2926 {
2927         if (!table) return;
2928         
2929         lyxerr[Debug::LATEX] << "DocBookContTableRows... " << this << endl;
2930
2931         LyXFont font2;
2932         char c;
2933         Inset * inset;
2934         //string emph = "emphasis";
2935         bool emph_flag = false;
2936         int char_line_count = 0;
2937         
2938         LyXLayout const & style =
2939                 textclasslist.Style(current_view->buffer()->params.textclass,
2940                                     GetLayout());
2941         
2942         size_type main_body;
2943         if (style.labeltype != LABEL_MANUAL)
2944                 main_body = 0;
2945         else
2946                 main_body = BeginningOfMainBody();
2947         
2948         // Gets paragraph main font.
2949         LyXFont font1;
2950         if (main_body > 0)
2951                 font1 = style.labelfont;
2952         else
2953                 font1 = style.font;
2954         
2955         size_type lastpos = i;
2956         int cell = table->CellHasContRow(current_cell_number);
2957         ++current_cell_number;
2958         while(cell >= 0) {
2959                 // first find the right position
2960                 i = lastpos;
2961                 for (; i < size() && current_cell_number < cell; ++i) {
2962                         c = GetChar(i);
2963                         if (c == LyXParagraph::META_NEWLINE)
2964                                 ++current_cell_number;
2965                 }
2966                 lastpos = i;
2967                 c = GetChar(i);
2968                 // I don't know how to handle this so I comment it
2969                 // for the moment (Jug)
2970 //             if (table->Linebreaks(table->FirstVirtualCell(cell))) {
2971 //                     file += " \\\\\n";
2972 //                     column = 0;
2973 //             } else
2974                 if ((c != ' ') && (c != LyXParagraph::META_NEWLINE)) {
2975                         os << ' ';
2976                 }
2977
2978                 for (; i < size()
2979                              && (c = GetChar(i)) != LyXParagraph::META_NEWLINE;
2980                      ++i) {
2981                         ++column;
2982                         
2983                         // Fully instantiated font
2984                         font2 = getFont(i);
2985                         
2986                         // Handle <emphasis> tag.
2987                         if (font1.emph() != font2.emph() && i) {
2988                                 if (font2.emph() == LyXFont::ON) {
2989                                         os << "<emphasis>";
2990                                         emph_flag= true;
2991                                 } else if (emph_flag) {
2992                                         os << "</emphasis>";
2993                                         emph_flag= false;
2994                                 }
2995                         }
2996                         if (c == LyXParagraph::META_INSET) {
2997                                 inset = GetInset(i);
2998 #ifdef HAVE_SSTREAM
2999                                 std::ostringstream ost;
3000                                 inset->DocBook(ost);
3001                                 string tmp_out = ost.str().c_str();
3002 #else
3003                                 ostrstream ost;
3004                                 inset->DocBook(ost);
3005                                 ost << '\0';
3006                                 char * ctmp = ost.str();
3007                                 string tmp_out(ctmp);
3008                                 delete [] ctmp;
3009 #endif
3010                                 //
3011                                 // This code needs some explanation:
3012                                 // Two insets are treated specially
3013                                 //   label if it is the first element in a
3014                                 //   command paragraph
3015                                 //       desc_on == 3
3016                                 //   graphics inside tables or figure floats
3017                                 //   can't go on title (the equivalente in
3018                                 //   latex for this case is caption and title
3019                                 //   should come first
3020                                 //       desc_on == 4
3021                                 //
3022                                 if(desc_on != 3 || i != 0) {
3023                                         if(tmp_out[0] == '@') {
3024                                                 if(desc_on == 4)
3025                                                         extra += frontStrip(tmp_out, '@');
3026                                                 else
3027                                                         os << frontStrip(tmp_out, '@');
3028                                         } else
3029                                                 os << tmp_out;
3030                                 }
3031                         } else if (font2.latex() == LyXFont::ON) {
3032                                 // "TeX"-Mode on == > SGML-Mode on.
3033                                 if (c!= '\0')
3034                                         os << c;
3035                                 ++char_line_count;
3036                         } else {
3037                                 string sgml_string;
3038                                 if (linuxDocConvertChar(c, sgml_string) 
3039                                     && !style.free_spacing) {
3040                                         // in freespacing mode, spaces are
3041                                         // non-breaking characters
3042                                         // char is ' '
3043                                         if (desc_on == 1) {
3044                                                 ++char_line_count;
3045                                                 os << '\n'
3046                                                    << "</term><listitem><para>";
3047                                                 desc_on = 2;
3048                                         } else  {
3049                                                 os << c;
3050                                         }
3051                                 } else {
3052                                         os << sgml_string;
3053                                 }
3054                         }
3055                 }
3056                 // we have only to control for emphasis open here!
3057                 if (emph_flag) {
3058                         os << "</emphasis>";
3059                         emph_flag= false;
3060                 }
3061                 font1 = font2 = getFont(-1);
3062                 cell = table->CellHasContRow(current_cell_number);
3063         }
3064         lyxerr[Debug::LATEX] << "DocBookContTableRows...done " << this << endl;
3065 }
3066
3067
3068 void LyXParagraph::SimpleTeXBlanks(ostream & os, TexRow & texrow,
3069                                    LyXParagraph::size_type const i,
3070                                    int & column, LyXFont const & font,
3071                                    LyXLayout const & style)
3072 {
3073         if (column > tex_code_break_column
3074             && i 
3075             && GetChar(i - 1) != ' '
3076             && (i < size() - 1)
3077             // In LaTeX mode, we don't want to
3078             // break lines since some commands
3079             // do not like this
3080             && ! (font.latex() == LyXFont::ON)
3081             // same in FreeSpacing mode
3082             && !style.free_spacing
3083             // In typewriter mode, we want to avoid 
3084             // ! . ? : at the end of a line
3085             && !(font.family() == LyXFont::TYPEWRITER_FAMILY
3086                  && (GetChar(i-1) == '.'
3087                      || GetChar(i-1) == '?' 
3088                      || GetChar(i-1) == ':'
3089                      || GetChar(i-1) == '!'))) {
3090                 if (tex_code_break_column == 0) {
3091                         // in batchmode we need LaTeX to still
3092                         // see it as a space not as an extra '\n'
3093                         os << " %\n";
3094                 } else {
3095                         os << '\n';
3096                 }
3097                 texrow.newline();
3098                 texrow.start(this, i + 1);
3099                 column = 0;
3100         } else if (font.latex() == LyXFont::OFF) {
3101                 if (style.free_spacing) {
3102                         os << '~';
3103                 } else {
3104                         os << ' ';
3105                 }
3106         }
3107 }
3108
3109
3110 void LyXParagraph::SimpleTeXSpecialChars(ostream & os, TexRow & texrow,
3111                                          LyXFont & font,
3112                                          LyXFont & running_font,
3113                                          LyXFont & basefont,
3114                                          bool & open_font,
3115                                          LyXLayout const & style,
3116                                          LyXParagraph::size_type & i,
3117                                          int & column, char const c)
3118 {
3119         // Two major modes:  LaTeX or plain
3120         // Handle here those cases common to both modes
3121         // and then split to handle the two modes separately.
3122         switch (c) {
3123         case LyXParagraph::META_INSET: {
3124                 Inset * inset = GetInset(i);
3125                 if (inset) {
3126                         bool close = false;
3127                         int len = os.tellp();
3128                         if ((inset->LyxCode() == Inset::GRAPHICS_CODE
3129                              || inset->LyxCode() == Inset::MATH_CODE
3130                              || inset->LyxCode() == Inset::URL_CODE)
3131                             && running_font.isRightToLeft()) {
3132                                 os << "\\L{";
3133                                 close = true;
3134                         }
3135
3136                         int tmp = inset->Latex(os, style.isCommand(),
3137                                                style.free_spacing);
3138
3139                         if (close)
3140                                 os << "}";
3141
3142                         if (tmp) {
3143                                 column = 0;
3144                         } else {
3145                                 column += os.tellp() - len;
3146                         }
3147                         for (; tmp--;) {
3148                                 texrow.newline();
3149                         }
3150                 }
3151         }
3152         break;
3153
3154         case LyXParagraph::META_NEWLINE:
3155                 if (open_font) {
3156                         column += running_font.latexWriteEndChanges(os,
3157                                                                     basefont,
3158                                                                     basefont);
3159                         open_font = false;
3160                 }
3161                 basefont = getFont(-1);
3162                 running_font = basefont;
3163                 break;
3164
3165         case LyXParagraph::META_HFILL: 
3166                 os << "\\hfill{}";
3167                 column += 7;
3168                 break;
3169
3170         default:
3171                 // And now for the special cases within each mode
3172                 // Are we in LaTeX mode?
3173                 if (font.latex() == LyXFont::ON) {
3174                         // at present we only have one option
3175                         // but I'll leave it as a switch statement
3176                         // so its simpler to extend. (ARRae)
3177                         switch (c) {
3178                         default:
3179                                 // make sure that we will not print
3180                                 // error generating chars to the tex
3181                                 // file. This test would not be needed
3182                                 // if it were done in the buffer
3183                                 // itself.
3184                                 if (c != '\0') {
3185                                         os << c;
3186                                 }
3187                                 break;
3188                         }
3189                 } else {
3190                         // Plain mode (i.e. not LaTeX)
3191                         switch (c) {
3192                         case '\\': 
3193                                 os << "\\textbackslash{}";
3194                                 column += 15;
3195                                 break;
3196                 
3197                         case '°': case '±': case '²': case '³':  
3198                         case '×': case '÷': case '¹': case 'ª':
3199                         case 'º': case '¬': case 'µ':
3200                                 if (current_view->buffer()->params.inputenc == "latin1") {
3201                                         os << "\\ensuremath{"
3202                                            << c
3203                                            << '}';
3204                                         column += 13;
3205                                 } else {
3206                                         os << c;
3207                                 }
3208                                 break;
3209
3210                         case '|': case '<': case '>':
3211                                 // In T1 encoding, these characters exist
3212                                 if (lyxrc.fontenc == "T1") {
3213                                         os << c;
3214                                         //... but we should avoid ligatures
3215                                         if ((c == '>' || c == '<')
3216                                             && i <= size() - 2
3217                                             && GetChar(i + 1) == c){
3218                                                 os << "\\textcompwordmark{}";
3219                                                 column += 19;
3220                                         }
3221                                         break;
3222                                 }
3223                                 // Typewriter font also has them
3224                                 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
3225                                         os << c;
3226                                         break;
3227                                 } 
3228                                 // Otherwise, we use what LaTeX
3229                                 // provides us.
3230                                 switch(c) {
3231                                 case '<':
3232                                         os << "\\textless{}";
3233                                         column += 10;
3234                                         break;
3235                                 case '>':
3236                                         os << "\\textgreater{}";
3237                                         column += 13;
3238                                         break;
3239                                 case '|':
3240                                         os << "\\textbar{}";
3241                                         column += 9;
3242                                         break;
3243                                 }
3244                                 break;
3245
3246                         case '-': // "--" in Typewriter mode -> "-{}-"
3247                                 if (i <= size() - 2
3248                                     && GetChar(i + 1) == '-'
3249                                     && font.family() == LyXFont::TYPEWRITER_FAMILY) {
3250                                         os << "-{}";
3251                                         column += 2;
3252                                 } else {
3253                                         os << '-';
3254                                 }
3255                                 break;
3256
3257                         case '\"': 
3258                                 os << "\\char`\\\"{}";
3259                                 column += 9;
3260                                 break;
3261
3262                         case '£':
3263                                 if (current_view->buffer()->params.inputenc == "default") {
3264                                         os << "\\pounds{}";
3265                                         column += 8;
3266                                 } else {
3267                                         os << c;
3268                                 }
3269                                 break;
3270
3271                         case '$': case '&':
3272                         case '%': case '#': case '{':
3273                         case '}': case '_':
3274                                 os << '\\' << c;
3275                                 column += 1;
3276                                 break;
3277
3278                         case '~':
3279                                 os << "\\textasciitilde{}";
3280                                 column += 16;
3281                                 break;
3282
3283                         case '^':
3284                                 os << "\\textasciicircum{}";
3285                                 column += 17;
3286                                 break;
3287
3288                         case '*': case '[': case ']':
3289                                 // avoid being mistaken for optional arguments
3290                                 os << '{' << c << '}';
3291                                 column += 2;
3292                                 break;
3293
3294                         case ' ':
3295                                 // Blanks are printed before font switching.
3296                                 // Sure? I am not! (try nice-latex)
3297                                 // I am sure it's correct. LyX might be smarter
3298                                 // in the future, but for now, nothing wrong is
3299                                 // written. (Asger)
3300                                 break;
3301
3302                         default:
3303                                 /* idea for labels --- begin*/
3304                                 // Check for "LyX"
3305                                 if (c ==  'L'
3306                                     && i <= size() - 3
3307                                     && font.family() != LyXFont::TYPEWRITER_FAMILY
3308                                     && GetChar(i + 1) == 'y'
3309                                     && GetChar(i + 2) == 'X') {
3310                                         os << "\\LyX{}";
3311                                         i += 2;
3312                                         column += 5;
3313                                 }
3314                                 // Check for "TeX"
3315                                 else if (c == 'T'
3316                                          && i <= size() - 3
3317                                          && font.family() != LyXFont::TYPEWRITER_FAMILY
3318                                          && GetChar(i + 1) == 'e'
3319                                          && GetChar(i + 2) == 'X') {
3320                                         os << "\\TeX{}";
3321                                         i += 2;
3322                                         column += 5;
3323                                 }
3324                                 // Check for "LaTeX2e"
3325                                 else if (c == 'L'
3326                                          && i <= size() - 7
3327                                          && font.family() != LyXFont::TYPEWRITER_FAMILY
3328                                          && GetChar(i + 1) == 'a'
3329                                          && GetChar(i + 2) == 'T'
3330                                          && GetChar(i + 3) == 'e'
3331                                          && GetChar(i + 4) == 'X'
3332                                          && GetChar(i + 5) == '2'
3333                                          && GetChar(i + 6) == 'e') {
3334                                         os << "\\LaTeXe{}";
3335                                         i += 6;
3336                                         column += 8;
3337                                 }
3338                                 // Check for "LaTeX"
3339                                 else if (c == 'L'
3340                                          && i <= size() - 5
3341                                          && font.family() != LyXFont::TYPEWRITER_FAMILY
3342                                          && GetChar(i + 1) == 'a'
3343                                          && GetChar(i + 2) == 'T'
3344                                          && GetChar(i + 3) == 'e'
3345                                          && GetChar(i + 4) == 'X') {
3346                                         os << "\\LaTeX{}";
3347                                         i += 4;
3348                                         column += 7;
3349                                         /* idea for labels --- end*/ 
3350                                 } else if (c != '\0') {
3351                                         os << c;
3352                                 }
3353                                 break;
3354                         }
3355                 }
3356         }
3357 }
3358
3359
3360 #if 0
3361 bool LyXParagraph::RoffContTableRows(ostream & os,
3362                                      LyXParagraph::size_type i,
3363                                      int actcell)
3364 {
3365         if (!table)
3366                 return false;
3367
3368         LyXFont font1(LyXFont::ALL_INHERIT);
3369         LyXFont font2;
3370         Inset * inset;
3371         char c;
3372
3373         string fname2 = TmpFileName(string(), "RAT2");
3374         int lastpos = i;
3375         int cell = table->CellHasContRow(actcell);
3376         ++actcell;
3377         while(cell >= 0) {
3378                 // first find the right position
3379                 i = lastpos;
3380                 for (; i < size() && actcell < cell; ++i) {
3381                         c = GetChar(i);
3382                         if (c == LyXParagraph::META_NEWLINE)
3383                                 ++actcell;
3384                 }
3385                 lastpos = i;
3386                 c = GetChar(i);
3387                 if ((c != ' ') && (c != LyXParagraph::META_NEWLINE))
3388                         os << " ";
3389                 for (; i < size()
3390                              && (c = GetChar(i)) != LyXParagraph::META_NEWLINE;
3391                      ++i) {
3392                         font2 = GetFontSettings(i);
3393                         if (font1.latex() != font2.latex()) {
3394                                 if (font2.latex() != LyXFont::OFF)
3395                                         continue;
3396                         }
3397                         c = GetChar(i);
3398                         switch (c) {
3399                         case LyXParagraph::META_INSET:
3400                                 if ((inset = GetInset(i))) {
3401 #ifdef HAVE_SSTREAM
3402                                         stringstream ss(ios::in | ios::out);
3403                                         inset->Ascii(ss);
3404                                         ss.seekp(0);
3405                                         ss.get(c);
3406                                         while (!ss) {
3407                                                 if (c == '\\')
3408                                                         os << "\\\\";
3409                                                 else
3410                                                         os << c;
3411                                                 ss.get(c);
3412                                         }
3413 #else
3414                                         strstream ss;
3415                                         inset->Ascii(ss);
3416                                         ss.seekp(0);
3417                                         ss.get(c);
3418                                         while (!ss) {
3419                                                 if (c == '\\')
3420                                                         os << "\\\\";
3421                                                 else
3422                                                         os << c;
3423                                                 ss.get(c);
3424                                         }
3425                                         delete [] ss.str();
3426 #endif
3427                                 }
3428                                 break;
3429                         case LyXParagraph::META_NEWLINE:
3430                                 break;
3431                         case LyXParagraph::META_HFILL: 
3432                                 break;
3433                         case '\\': 
3434                                 os << "\\\\";
3435                                 break;
3436                         default:
3437                                 if (c != '\0')
3438                                         os << c;
3439                                 else
3440                                         lyxerr.debug() << "RoffAsciiTable: "
3441                                                 "NULL char in structure."
3442                                                        << endl;
3443                                 break;
3444                         }
3445                 }
3446                 cell = table->CellHasContRow(actcell);
3447         }
3448         return true;
3449 }
3450 #endif
3451
3452
3453 LyXParagraph * LyXParagraph::TeXDeeper(ostream & os, TexRow & texrow,
3454                                        ostream & foot,
3455                                        TexRow & foot_texrow,
3456                                        int & foot_count)
3457 {
3458         lyxerr[Debug::LATEX] << "TeXDeeper...     " << this << endl;
3459         LyXParagraph * par = this;
3460
3461         while (par &&
3462                (par->depth == depth) &&
3463                (par->footnoteflag == footnoteflag)) {
3464                 if (par->IsDummy())
3465                         lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
3466                 if (textclasslist.Style(current_view->buffer()->params.textclass, 
3467                                         par->layout).isEnvironment()
3468                     || par->pextra_type != PEXTRA_NONE) {
3469                         par = par->TeXEnvironment(os, texrow,
3470                                                   foot, foot_texrow,
3471                                                   foot_count);
3472                 } else {
3473                         par = par->TeXOnePar(os, texrow,
3474                                              foot, foot_texrow,
3475                                              foot_count);
3476                 }
3477         }
3478         lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
3479
3480         return par;
3481 }
3482
3483
3484 LyXParagraph * LyXParagraph::TeXEnvironment(ostream & os, TexRow & texrow,
3485                                             ostream & foot,
3486                                             TexRow & foot_texrow,
3487                                             int & foot_count)
3488 {
3489         bool eindent_open = false;
3490         bool foot_this_level = false;
3491         // flags when footnotetext should be appended to file.
3492         static bool minipage_open = false;
3493         static int minipage_open_depth = 0;
3494         char par_sep = current_view->buffer()->params.paragraph_separation;
3495     
3496         lyxerr[Debug::LATEX] << "TeXEnvironment...     " << this << endl;
3497         if (IsDummy())
3498                 lyxerr << "ERROR (LyXParagraph::TeXEnvironment)" << endl;
3499
3500         LyXLayout const & style =
3501                 textclasslist.Style(current_view->buffer()->params.textclass,
3502                                     layout);
3503        
3504         if (pextra_type == PEXTRA_INDENT) {
3505                 if (!pextra_width.empty()) {
3506                         os << "\\begin{LyXParagraphIndent}{"
3507                            << pextra_width << "}\n";
3508                 } else {
3509                         //float ib = atof(pextra_widthp.c_str())/100;
3510                         // string can't handle floats at present (971109)
3511                         // so I'll do a conversion by hand knowing that
3512                         // the limits are 0.0 to 1.0. ARRae.
3513                         os << "\\begin{LyXParagraphIndent}{";
3514                         switch (pextra_widthp.length()) {
3515                         case 3:
3516                                 os << "1.00";
3517                                 break;
3518                         case 2:
3519                                 os << "0."
3520                                    << pextra_widthp;
3521                                 break;
3522                         case 1:
3523                                 os << "0.0"
3524                                    << pextra_widthp;
3525                         }
3526                         os << "\\columnwidth}\n";
3527                 }
3528                 texrow.newline();
3529                 eindent_open = true;
3530         }
3531         if ((pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3532                 if (pextra_hfill && Previous() &&
3533                     (Previous()->pextra_type == PEXTRA_MINIPAGE)) {
3534                         os << "\\hfill{}\n";
3535                         texrow.newline();
3536                 }
3537                 if (par_sep == BufferParams::PARSEP_INDENT) {
3538                         os << "{\\setlength\\parindent{0pt}\n";
3539                         texrow.newline();
3540                 }
3541                 os << "\\begin{minipage}";
3542                 switch(pextra_alignment) {
3543                 case MINIPAGE_ALIGN_TOP:
3544                         os << "[t]";
3545                         break;
3546                 case MINIPAGE_ALIGN_MIDDLE:
3547                         os << "[m]";
3548                         break;
3549                 case MINIPAGE_ALIGN_BOTTOM:
3550                         os << "[b]";
3551                         break;
3552                 }
3553                 if (!pextra_width.empty()) {
3554                         os << '{' << pextra_width << "}\n";
3555                 } else {
3556                         //float ib = atof(par->pextra_width.c_str())/100;
3557                         // string can't handle floats at present
3558                         // so I'll do a conversion by hand knowing that
3559                         // the limits are 0.0 to 1.0. ARRae.
3560                         os << '{';
3561                         switch (pextra_widthp.length()) {
3562                         case 3:
3563                                 os << "1.00";
3564                                 break;
3565                         case 2:
3566                                 os << "0."
3567                                    << pextra_widthp;
3568                                 break;
3569                         case 1:
3570                                 os << "0.0"
3571                                    << pextra_widthp;
3572                         }
3573                         os << "\\columnwidth}\n";
3574                 }
3575                 texrow.newline();
3576                 if (par_sep == BufferParams::PARSEP_INDENT) {
3577                         os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3578                         texrow.newline();
3579                 }
3580                 minipage_open = true;
3581                 minipage_open_depth = depth;
3582         }
3583
3584 #ifdef WITH_WARNINGS
3585 #warning Define FANCY_FOOTNOTE_CODE to re-enable Allan footnote code
3586         //I disabled it because it breaks when lists span on several
3587         //pages (JMarc)
3588 #endif
3589         if (style.isEnvironment()){
3590                 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
3591 #ifdef FANCY_FOOTNOTE_CODE
3592                         if (foot_count < 0) {
3593                                 // flag that footnote[mark][text] should be
3594                                 // used for any footnotes from now on
3595                                 foot_count = 0;
3596                                 foot_this_level = true;
3597                         }
3598 #endif
3599                         os << "\\begin{" << style.latexname() << "}{"
3600                            << labelwidthstring << "}\n";
3601                 } else if (style.labeltype == LABEL_BIBLIO) {
3602                         // ale970405
3603                         os << "\\begin{" << style.latexname() << "}{"
3604                            << bibitemWidthest(current_view->painter())
3605                            << "}\n";
3606                 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
3607 #ifdef FANCY_FOOTNOTE_CODE
3608                         if (foot_count < 0) {
3609                                 // flag that footnote[mark][text] should be
3610                                 // used for any footnotes from now on
3611                                 foot_count = 0;
3612                                 foot_this_level = true;
3613                         }
3614 #endif
3615                         os << "\\begin{" << style.latexname() << '}'
3616                            << style.latexparam() << '\n';
3617                 } else 
3618                         os << "\\begin{" << style.latexname() << '}'
3619                            << style.latexparam() << '\n';
3620                 texrow.newline();
3621         }
3622         LyXParagraph * par = this;
3623         do {
3624                 par = par->TeXOnePar(os, texrow,
3625                                      foot, foot_texrow, foot_count);
3626
3627                 if (minipage_open && par && !style.isEnvironment() &&
3628                     (par->pextra_type == PEXTRA_MINIPAGE) &&
3629                     par->pextra_start_minipage) {
3630                         os << "\\end{minipage}\n";
3631                         texrow.newline();
3632                         if (par_sep == BufferParams::PARSEP_INDENT) {
3633                                 os << "}\n";
3634                                 texrow.newline();
3635                         }
3636                         minipage_open = false;
3637                 }
3638                 if (par && par->depth > depth) {
3639                         if (textclasslist.Style(current_view->buffer()->params.textclass,
3640                                                 par->layout).isParagraph()
3641                             && !par->table
3642                             // Thinko!
3643                             // How to handle this? (Lgb)
3644                             //&& !suffixIs(os, "\n\n")
3645                                 ) {
3646                                 // There should be at least one '\n' already
3647                                 // but we need there to be two for Standard 
3648                                 // paragraphs that are depth-increment'ed to be
3649                                 // output correctly.  However, tables can
3650                                 // also be paragraphs so don't adjust them.
3651                                 // ARRae
3652                                 // Thinkee:
3653                                 // Will it ever harm to have one '\n' too
3654                                 // many? i.e. that we sometimes will have
3655                                 // three in a row. (Lgb)
3656                                 os << '\n';
3657                                 texrow.newline();
3658                         }
3659                         par = par->TeXDeeper(os, texrow,
3660                                              foot, foot_texrow, foot_count);
3661                 }
3662                 if (par && par->layout == layout && par->depth == depth &&
3663                     (par->pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3664                         if (par->pextra_hfill && par->Previous() &&
3665                             (par->Previous()->pextra_type == PEXTRA_MINIPAGE)){
3666                                 os << "\\hfill{}\n";
3667                                 texrow.newline();
3668                         }
3669                         if (par_sep == BufferParams::PARSEP_INDENT) {
3670                                 os << "{\\setlength\\parindent{0pt}\n";
3671                                 texrow.newline();
3672                         }
3673                         os << "\\begin{minipage}";
3674                         switch(par->pextra_alignment) {
3675                         case MINIPAGE_ALIGN_TOP:
3676                                 os << "[t]";
3677                                 break;
3678                         case MINIPAGE_ALIGN_MIDDLE:
3679                                 os << "[m]";
3680                                 break;
3681                         case MINIPAGE_ALIGN_BOTTOM:
3682                                 os << "[b]";
3683                                 break;
3684                         }
3685                         if (!par->pextra_width.empty()) {
3686                                 os << '{' << par->pextra_width << "}\n";
3687                         } else {
3688                                 //float ib = atof(par->pextra_widthp.c_str())/100;
3689                                 // string can't handle floats at present
3690                                 // so I'll do a conversion by hand knowing that
3691                                 // the limits are 0.0 to 1.0. ARRae.
3692                                 os << '{';
3693                                 switch (par->pextra_widthp.length()) {
3694                                 case 3:
3695                                         os << "1.00";
3696                                         break;
3697                                 case 2:
3698                                         os << "0." << par->pextra_widthp;
3699                                         break;
3700                                 case 1:
3701                                         os << "0.0" << par->pextra_widthp;
3702                                 }
3703                                 os << "\\columnwidth}\n";
3704                         }
3705                         texrow.newline();
3706                         if (par_sep == BufferParams::PARSEP_INDENT) {
3707                                 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3708                                 texrow.newline();
3709                         }
3710                         minipage_open = true;
3711                         minipage_open_depth = par->depth;
3712                 }
3713         } while (par
3714                  && par->layout == layout
3715                  && par->depth == depth
3716                  && par->pextra_type == pextra_type
3717                  && par->footnoteflag == footnoteflag);
3718  
3719         if (style.isEnvironment()) {
3720                 os << "\\end{" << style.latexname() << '}';
3721                 // maybe this should go after the minipage closes?
3722                 if (foot_this_level) {
3723                         if (foot_count >= 1) {
3724                                 if (foot_count > 1) {
3725                                         os << "\\addtocounter{footnote}{-"
3726                                            << foot_count - 1
3727                                            << '}';
3728                                 }
3729                                 os << foot;
3730                                 texrow += foot_texrow;
3731                                 foot.clear();
3732                                 foot_texrow.reset();
3733                                 foot_count = 0;
3734                         }
3735                 }
3736         }
3737         if (minipage_open && (minipage_open_depth == depth) &&
3738             (!par || par->pextra_start_minipage ||
3739              par->pextra_type != PEXTRA_MINIPAGE)) {
3740                 os << "\\end{minipage}\n";
3741                 texrow.newline();
3742                 if (par_sep == BufferParams::PARSEP_INDENT) {
3743                         os << "}\n";
3744                         texrow.newline();
3745                 }
3746                 if (par && par->pextra_type != PEXTRA_MINIPAGE) {
3747                         os << "\\medskip\n\n";
3748                         texrow.newline();
3749                         texrow.newline();
3750                 }
3751                 minipage_open = false;
3752         }
3753         if (eindent_open) {
3754                 os << "\\end{LyXParagraphIndent}\n";
3755                 texrow.newline();
3756         }
3757         if (!(par && (par->pextra_type == PEXTRA_MINIPAGE) 
3758               && par->pextra_hfill)) {
3759                 os << '\n';
3760                 texrow.newline();
3761         }
3762         lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
3763         return par;  // ale970302
3764 }
3765
3766
3767 LyXParagraph * LyXParagraph::TeXFootnote(ostream & os, TexRow & texrow,
3768                                          ostream & foot, TexRow & foot_texrow,
3769                                          int & foot_count,
3770                                          bool parent_is_rtl)
3771 {
3772         lyxerr[Debug::LATEX] << "TeXFootnote...  " << this << endl;
3773         if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
3774                 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3775                         "No footnote!" << endl;
3776
3777         LyXParagraph * par = this;
3778         LyXLayout const & style =
3779                 textclasslist.Style(current_view->buffer()->params.textclass, 
3780                                     previous->GetLayout());
3781         
3782         if (style.needprotect && footnotekind != LyXParagraph::FOOTNOTE){
3783                 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3784                         "Float other than footnote in command"
3785                         " with moving argument is illegal" << endl;
3786         }
3787
3788         if (footnotekind != LyXParagraph::FOOTNOTE
3789             && footnotekind != LyXParagraph::MARGIN
3790             && os.tellp()
3791             // Thinko
3792             // How to solve this?
3793             //&& !suffixIs(file, '\n')
3794                 ) {
3795                 // we need to ensure that real floats like tables and figures
3796                 // have their \begin{} on a new line otherwise we can get
3797                 // incorrect results when using the endfloat.sty package
3798                 // especially if two floats follow one another.  ARRae 981022
3799                 // NOTE: if the file is length 0 it must have just been
3800                 //       written out so we assume it ended with a '\n'
3801                 // Thinkee:
3802                 // As far as I can see there is never any harm in writing
3803                 // a '\n' too much. Please tell me if I am wrong. (Lgb)
3804                 os << '\n';
3805                 texrow.newline();
3806         }
3807
3808         bool need_closing = false;
3809         bool is_rtl = isRightToLeftPar();
3810         if (is_rtl != parent_is_rtl) {
3811                 if (is_rtl)
3812                         os << "\\R{";
3813                 else
3814                         os << "\\L{";
3815                 need_closing = true;
3816         }
3817         
3818         BufferParams * params = &current_view->buffer()->params;
3819         bool footer_in_body = true;
3820         switch (footnotekind) {
3821         case LyXParagraph::FOOTNOTE:
3822                 if (style.intitle) {
3823                         os << "\\thanks{\n";
3824                         footer_in_body = false;
3825                 } else {
3826                         if (foot_count == -1) {
3827                                 // we're at depth 0 so we can use:
3828                                 os << "\\footnote{%\n";
3829                                 footer_in_body = false;
3830                         } else {
3831                                 os << "\\footnotemark{}%\n";
3832                                 if (foot_count) {
3833                                         // we only need this when there are
3834                                         // multiple footnotes
3835                                         os << "\\stepcounter{footnote}";
3836                                 }
3837                                 os << "\\footnotetext{%\n";
3838                                 foot_texrow.start(this, 0);
3839                                 foot_texrow.newline();
3840                                 ++foot_count;
3841                         }
3842                 }
3843                 break;
3844         case LyXParagraph::MARGIN:
3845                 os << "\\marginpar{\n";
3846                 break;
3847         case LyXParagraph::FIG:
3848                 if (pextra_type == PEXTRA_FLOATFLT
3849                     && (!pextra_width.empty()
3850                         || !pextra_widthp.empty())) {
3851                         if (!pextra_width.empty())
3852                                 os << "\\begin{floatingfigure}{"
3853                                    << pextra_width << "}\n";
3854                         else
3855                                 os << "\\begin{floatingfigure}{"
3856                                    << atoi(pextra_widthp.c_str())/100.0
3857                                    << "\\textwidth}\n";
3858                 } else {
3859                         os << "\\begin{figure}";
3860                         if (!params->float_placement.empty()) { 
3861                                 os << '[' << params->float_placement << "]\n";
3862                         } else {
3863                                 os << '\n';
3864                         }
3865                 }
3866                 break;
3867         case LyXParagraph::TAB:
3868                 os << "\\begin{table}";
3869                 if (!params->float_placement.empty()) { 
3870                         os << '[' << params->float_placement << "]\n";
3871                 } else {
3872                         os << '\n';
3873                 }
3874                 break;
3875         case LyXParagraph::WIDE_FIG:
3876                 os << "\\begin{figure*}";
3877                 if (!params->float_placement.empty()) { 
3878                         os << '[' << params->float_placement << "]\n";
3879                 } else {
3880                         os << '\n';
3881                 }
3882                 break;
3883         case LyXParagraph::WIDE_TAB:
3884                 os << "\\begin{table*}";
3885                 if (!params->float_placement.empty()) { 
3886                         os << '[' << params->float_placement << "]\n";
3887                 } else {
3888                         os << '\n';
3889                 }
3890                 break;
3891         case LyXParagraph::ALGORITHM:
3892                 os << "\\begin{algorithm}\n";
3893                 break;
3894         }
3895         texrow.newline();
3896    
3897         if (footnotekind != LyXParagraph::FOOTNOTE
3898             || !footer_in_body) {
3899                 // Process text for all floats except footnotes in body
3900                 do {
3901                         LyXLayout const & style =
3902                                 textclasslist
3903                                 .Style(current_view->buffer()->params
3904                                        .textclass,
3905                                        par->layout);
3906                         if (par->IsDummy())
3907                                 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3908                                        << endl;
3909                         if (style.isEnvironment()
3910                             || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3911                                 // Allows the use of minipages within float
3912                                 // environments. Shouldn't be circular because
3913                                 // we don't support footnotes inside
3914                                 // floats (yet). ARRae
3915                                 par = par->TeXEnvironment(os, texrow,
3916                                                           foot, foot_texrow,
3917                                                           foot_count);
3918                         } else {
3919                                 par = par->TeXOnePar(os, texrow,
3920                                                      foot, foot_texrow,
3921                                                      foot_count);
3922                         }
3923                         
3924                         if (par && !par->IsDummy() && par->depth > depth) {
3925                                 par = par->TeXDeeper(os, texrow,
3926                                                      foot, foot_texrow,
3927                                                      foot_count);
3928                         }
3929                 } while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3930         } else {
3931                 // process footnotes > depth 0 or in environments separately
3932                 // NOTE: Currently don't support footnotes within footnotes
3933                 //       even though that is possible using the \footnotemark
3934 #ifdef HAVE_SSTREAM
3935                 std::ostringstream dummy;
3936 #else
3937                 ostrstream dummy;
3938 #endif
3939                 TexRow dummy_texrow;
3940                 int dummy_count = 0;
3941                 do {
3942                         LyXLayout const & style =
3943                                 textclasslist
3944                                 .Style(current_view->buffer()->params
3945                                        .textclass,
3946                                        par->layout);
3947                         if (par->IsDummy())
3948                                 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3949                                        << endl;
3950                         if (style.isEnvironment()
3951                             || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3952                                 // Allows the use of minipages within float
3953                                 // environments. Shouldn't be circular because
3954                                 // we don't support footnotes inside
3955                                 // floats (yet). ARRae
3956                                 par = par->TeXEnvironment(foot, foot_texrow,
3957                                                           dummy, dummy_texrow,
3958                                                           dummy_count);
3959                         } else {
3960                                 par = par->TeXOnePar(foot, foot_texrow,
3961                                                      dummy, dummy_texrow,
3962                                                      dummy_count);
3963                         }
3964
3965                         if (par && !par->IsDummy() && par->depth > depth) {
3966                                 par = par->TeXDeeper(foot, foot_texrow,
3967                                                      dummy, dummy_texrow,
3968                                                      dummy_count);
3969                         }
3970                 } while (par
3971                          && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3972                 if (dummy_count) {
3973                         lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3974                                 "Footnote in a Footnote -- not supported"
3975                                << endl;
3976                 }
3977 #ifndef HAVE_OSTREAM
3978                 delete [] dummy.str();
3979 #endif
3980         }
3981
3982         switch (footnotekind) {
3983         case LyXParagraph::FOOTNOTE:
3984                 if (footer_in_body) {
3985                         // This helps tell which of the multiple
3986                         // footnotetexts an error was in.
3987                         foot << "}%\n";
3988                         foot_texrow.newline();
3989                 } else {
3990                         os << '}';
3991                 }
3992                 break;
3993         case LyXParagraph::MARGIN:
3994                 os << '}';
3995                 break;
3996         case LyXParagraph::FIG:
3997                 if (pextra_type == PEXTRA_FLOATFLT
3998                     && (!pextra_width.empty()
3999                         || !pextra_widthp.empty()))
4000                         os << "\\end{floatingfigure}";
4001                 else
4002                         os << "\\end{figure}";
4003                 break;
4004         case LyXParagraph::TAB:
4005                 os << "\\end{table}";
4006                 break;
4007         case LyXParagraph::WIDE_FIG:
4008                 os << "\\end{figure*}";
4009                 break;
4010         case LyXParagraph::WIDE_TAB:
4011                 os << "\\end{table*}";
4012                 break;
4013         case LyXParagraph::ALGORITHM:
4014                 os << "\\end{algorithm}";
4015                 break;
4016         }
4017
4018         if (need_closing)
4019                 os << "}";
4020
4021         if (footnotekind != LyXParagraph::FOOTNOTE
4022             && footnotekind != LyXParagraph::MARGIN) {
4023                 // we need to ensure that real floats like tables and figures
4024                 // have their \end{} on a line of their own otherwise we can
4025                 // get incorrect results when using the endfloat.sty package.
4026                 os << "\n";
4027                 texrow.newline();
4028         }
4029
4030         lyxerr[Debug::LATEX] << "TeXFootnote...done " << par->next << endl;
4031         return par;
4032 }
4033
4034
4035 bool LyXParagraph::IsDummy() const
4036 {
4037         return (footnoteflag == LyXParagraph::NO_FOOTNOTE && previous
4038                 && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE);
4039 }
4040
4041
4042 void LyXParagraph::SetPExtraType(int type, char const * width,
4043                                  char const * widthp)
4044 {
4045         pextra_type = type;
4046         pextra_width = width;
4047         pextra_widthp = widthp;
4048
4049         if (textclasslist.Style(current_view->buffer()->params.textclass, 
4050                                 layout).isEnvironment()) {
4051                 LyXParagraph
4052                         * par = this,
4053                         * ppar = par;
4054
4055                 while (par && (par->layout == layout)
4056                        && (par->depth == depth)) {
4057                         ppar = par;
4058                         par = par->Previous();
4059                         if (par)
4060                                 par = par->FirstPhysicalPar();
4061                         while (par && par->depth > depth) {
4062                                 par = par->Previous();
4063                                 if (par)
4064                                         par = par->FirstPhysicalPar();
4065                         }
4066                 }
4067                 par = ppar;
4068                 while (par && (par->layout == layout)
4069                        && (par->depth == depth)) {
4070                         par->pextra_type = type;
4071                         par->pextra_width = width;
4072                         par->pextra_widthp = widthp;
4073                         par = par->NextAfterFootnote();
4074                         if (par && (par->depth > depth))
4075                                 par->SetPExtraType(type, width, widthp);
4076                         while (par && ((par->depth > depth) || par->IsDummy()))
4077                                 par = par->NextAfterFootnote();
4078                 }
4079         }
4080 }
4081
4082
4083 void LyXParagraph::UnsetPExtraType()
4084 {
4085         if (pextra_type == PEXTRA_NONE)
4086                 return;
4087     
4088         pextra_type = PEXTRA_NONE;
4089         pextra_width.clear();
4090         pextra_widthp.clear();
4091
4092         if (textclasslist.Style(current_view->buffer()->params.textclass, 
4093                                 layout).isEnvironment()) {
4094                 LyXParagraph
4095                         * par = this,
4096                         * ppar = par;
4097
4098                 while (par && (par->layout == layout)
4099                        && (par->depth == depth)) {
4100                         ppar = par;
4101                         par = par->Previous();
4102                         if (par)
4103                                 par = par->FirstPhysicalPar();
4104                         while (par && par->depth > depth) {
4105                                 par = par->Previous();
4106                                 if (par)
4107                                         par = par->FirstPhysicalPar();
4108                         }
4109                 }
4110                 par = ppar;
4111                 while (par && (par->layout == layout)
4112                        && (par->depth == depth)) {
4113                         par->pextra_type = PEXTRA_NONE;
4114                         par->pextra_width.clear();
4115                         par->pextra_widthp.clear();
4116                         par = par->NextAfterFootnote();
4117                         if (par && (par->depth > depth))
4118                                 par->UnsetPExtraType();
4119                         while (par && ((par->depth > depth) || par->IsDummy()))
4120                                 par = par->NextAfterFootnote();
4121                 }
4122         }
4123 }
4124
4125
4126 bool LyXParagraph::IsHfill(size_type pos) const
4127 {
4128         return IsHfillChar(GetChar(pos));
4129 }
4130
4131
4132 bool LyXParagraph::IsInset(size_type pos) const
4133 {
4134         return IsInsetChar(GetChar(pos));
4135 }
4136
4137
4138 bool LyXParagraph::IsFloat(size_type pos) const
4139 {
4140         return IsFloatChar(GetChar(pos));
4141 }
4142
4143
4144 bool LyXParagraph::IsNewline(size_type pos) const
4145 {
4146         return pos >= 0 && IsNewlineChar(GetChar(pos));
4147 }
4148
4149
4150 bool LyXParagraph::IsSeparator(size_type pos) const
4151 {
4152         return IsSeparatorChar(GetChar(pos));
4153 }
4154
4155
4156 bool LyXParagraph::IsLineSeparator(size_type pos) const
4157 {
4158         return IsLineSeparatorChar(GetChar(pos));
4159 }
4160
4161
4162 bool LyXParagraph::IsKomma(size_type pos) const
4163 {
4164         return IsKommaChar(GetChar(pos));
4165 }
4166
4167
4168 /// Used by the spellchecker
4169 bool LyXParagraph::IsLetter(LyXParagraph::size_type pos) const
4170 {
4171         unsigned char c = GetChar(pos);
4172         if (IsLetterChar(c))
4173                 return true;
4174         // '\0' is not a letter, allthough every string contains "" (below)
4175         if( c == '\0')
4176                 return false;
4177         // We want to pass the ' and escape chars to ispell
4178         string extra = lyxrc.isp_esc_chars + '\'';
4179         char ch[2];
4180         ch[0] = c;
4181         ch[1] = 0;
4182         return contains(extra, ch);
4183 }
4184  
4185  
4186 bool LyXParagraph::IsWord(size_type pos ) const
4187 {
4188         return IsWordChar(GetChar(pos)) ;
4189 }
4190
4191
4192 Language const * LyXParagraph::getParLanguage() const 
4193 {
4194         if (size() > 0)
4195                 if (!table)
4196                         return FirstPhysicalPar()->GetFirstFontSettings()
4197                                 .language();
4198                 else {
4199                         for (size_type pos = 0; pos < size(); ++pos)
4200                                 if (IsNewline(pos))
4201                                         return GetFontSettings(pos).language();
4202                         return GetFirstFontSettings().language();
4203                 }
4204         else if (previous)
4205                 return previous->getParLanguage();
4206         else
4207                 return current_view->buffer()->params.language_info;
4208 }
4209
4210
4211 bool LyXParagraph::isRightToLeftPar() const
4212 {
4213         return lyxrc.rtl_support && !table && getParLanguage()->RightToLeft;
4214 }
4215
4216
4217 void LyXParagraph::ChangeLanguage(Language const * from, Language const * to)
4218 {
4219         for(size_type i = 0; i < size(); ++i) {
4220                 LyXFont font = GetFontSettings(i);
4221                 if (font.language() == from) {
4222                         font.setLanguage(to);
4223                         SetFont(i, font);
4224                 }
4225         }
4226 }
4227
4228
4229 bool LyXParagraph::isMultiLingual()
4230 {
4231         Language const * doc_language =
4232                 current_view->buffer()->params.language_info;
4233         for(size_type i = 0; i < size(); ++i) {
4234                 LyXFont font = GetFontSettings(i);
4235                 if (font.language() != doc_language)
4236                         return true;
4237         }
4238         return false;
4239 }