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