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