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