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