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