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