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