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