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