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