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