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