]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
a0635fdd44dcd9f342c30b005375ae533a5be4e9
[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
1897         // ok, now copy the paragraph
1898         for (size_type i = 0; i <= pos_end; ++i) {
1899                 the_next->CutIntoMinibuffer(i);
1900                 InsertFromMinibuffer(pos_insert + i);
1901         }
1902    
1903         // delete the next paragraph
1904         delete the_next;
1905 }
1906
1907
1908 void LyXParagraph::OpenFootnote(LyXParagraph::size_type pos)
1909 {
1910         LyXParagraph * par = ParFromPos(pos);
1911         par = par->next;
1912         while (par && par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1913                 par->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
1914                 par = par->next;
1915         }
1916 }
1917
1918
1919 void LyXParagraph::CloseFootnote(LyXParagraph::size_type pos)
1920 {
1921         LyXParagraph * par = ParFromPos(pos);
1922         par = par->next;
1923         while (par && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
1924                 par->footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
1925                 par = par->next;
1926         }
1927 }
1928
1929
1930 LyXTextClass::LayoutList::size_type LyXParagraph::GetLayout() const
1931 {
1932         return FirstPhysicalPar()->layout;
1933 }
1934
1935
1936 char LyXParagraph::GetDepth() const
1937 {
1938         return FirstPhysicalPar()->depth;
1939 }
1940
1941
1942 char LyXParagraph::GetAlign() const
1943 {
1944         return FirstPhysicalPar()->align;
1945 }
1946
1947
1948 string LyXParagraph::GetLabestring() const
1949 {
1950         return FirstPhysicalPar()->labelstring;
1951 }
1952
1953
1954 int LyXParagraph::GetFirstCounter(int i) const
1955 {
1956         return FirstPhysicalPar()->counter_[i];
1957 }
1958
1959
1960 // the next two functions are for the manual labels
1961 string LyXParagraph::GetLabelWidthString() const
1962 {
1963         if (!FirstPhysicalPar()->labelwidthstring.empty())
1964                 return FirstPhysicalPar()->labelwidthstring;
1965         else
1966                 return _("Senseless with this layout!");
1967 }
1968
1969
1970 void LyXParagraph::SetLabelWidthString(string const & s)
1971 {
1972         LyXParagraph * par = FirstPhysicalPar();
1973
1974         par->labelwidthstring = s;
1975 }
1976
1977
1978 void LyXParagraph::SetOnlyLayout(LyXTextClass::LayoutList::size_type new_layout)
1979 {
1980         LyXParagraph * par = FirstPhysicalPar();
1981         LyXParagraph * ppar = 0;
1982         LyXParagraph * npar = 0;
1983
1984         par->layout = new_layout;
1985         /* table stuff -- begin*/ 
1986         if (table) 
1987                 par->layout = 0;
1988         /* table stuff -- end*/ 
1989         if (par->pextra_type == PEXTRA_NONE) {
1990                 if (par->Previous()) {
1991                         ppar = par->Previous()->FirstPhysicalPar();
1992                         while(ppar
1993                               && ppar->Previous()
1994                               && (ppar->depth > par->depth))
1995                                 ppar = ppar->Previous()->FirstPhysicalPar();
1996                 }
1997                 if (par->Next()) {
1998                         npar = par->Next()->NextAfterFootnote();
1999                         while(npar
2000                               && npar->Next()
2001                               && (npar->depth > par->depth))
2002                                 npar = npar->Next()->NextAfterFootnote();
2003                 }
2004                 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
2005                         string
2006                                 p1 = ppar->pextra_width,
2007                                 p2 = ppar->pextra_widthp;
2008                         ppar->SetPExtraType(ppar->pextra_type,
2009                                             p1.c_str(), p2.c_str());
2010                 }
2011                 if ((par->pextra_type == PEXTRA_NONE) &&
2012                     npar && (npar->pextra_type != PEXTRA_NONE)) {
2013                         string
2014                                 p1 = npar->pextra_width,
2015                                 p2 = npar->pextra_widthp;
2016                         npar->SetPExtraType(npar->pextra_type,
2017                                             p1.c_str(), p2.c_str());
2018                 }
2019         }
2020 }
2021
2022
2023 void LyXParagraph::SetLayout(LyXTextClass::LayoutList::size_type new_layout)
2024 {
2025         LyXParagraph
2026                 * par = FirstPhysicalPar(),
2027                 * ppar = 0,
2028                 * npar = 0;
2029
2030         par->layout = new_layout;
2031         par->labelwidthstring.clear();
2032         par->align = LYX_ALIGN_LAYOUT;
2033         par->added_space_top = VSpace(VSpace::NONE);
2034         par->added_space_bottom = VSpace(VSpace::NONE);
2035         /* table stuff -- begin*/ 
2036         if (table) 
2037                 par->layout = 0;
2038         /* table stuff -- end*/
2039         if (par->pextra_type == PEXTRA_NONE) {
2040                 if (par->Previous()) {
2041                         ppar = par->Previous()->FirstPhysicalPar();
2042                         while(ppar
2043                               && ppar->Previous()
2044                               && (ppar->depth > par->depth))
2045                                 ppar = ppar->Previous()->FirstPhysicalPar();
2046                 }
2047                 if (par->Next()) {
2048                         npar = par->Next()->NextAfterFootnote();
2049                         while(npar
2050                               && npar->Next()
2051                               && (npar->depth > par->depth))
2052                                 npar = npar->Next()->NextAfterFootnote();
2053                 }
2054                 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
2055                         string
2056                                 p1 = ppar->pextra_width,
2057                                 p2 = ppar->pextra_widthp;
2058                         ppar->SetPExtraType(ppar->pextra_type,
2059                                             p1.c_str(), p2.c_str());
2060                 }
2061                 if ((par->pextra_type == PEXTRA_NONE) &&
2062                     npar && (npar->pextra_type != PEXTRA_NONE)) {
2063                         string
2064                                 p1 = npar->pextra_width,
2065                                 p2 = npar->pextra_widthp;
2066                         npar->SetPExtraType(npar->pextra_type,
2067                                             p1.c_str(), p2.c_str());
2068                 }
2069         }
2070 }
2071
2072
2073 // if the layout of a paragraph contains a manual label, the beginning of the 
2074 // main body is the beginning of the second word. This is what the par-
2075 // function returns. If the layout does not contain a label, the main
2076 // body always starts with position 0. This differentiation is necessary,
2077 // because there cannot be a newline or a blank <= the beginning of the 
2078 // main body in TeX.
2079
2080 int LyXParagraph::BeginningOfMainBody() const
2081 {
2082         if (FirstPhysicalPar() != this)
2083                 return -1;
2084    
2085         // Unroll the first two cycles of the loop
2086         // and remember the previous character to
2087         // remove unnecessary GetChar() calls
2088         size_type i = 0;
2089         if (i < size()
2090             && GetChar(i) != LyXParagraph::META_NEWLINE) {
2091                 ++i;
2092                 char previous_char, temp;
2093                 if (i < size()
2094                     && (previous_char = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2095                         // Yes, this  ^ is supposed to be "= " not "=="
2096                         ++i;
2097                         while (i < size()
2098                                && previous_char != ' '
2099                                && (temp = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2100                                 ++i;
2101                                 previous_char = temp;
2102                         }
2103                 }
2104         }
2105
2106         if (i == 0 && i == size() &&
2107             !(footnoteflag == LyXParagraph::NO_FOOTNOTE
2108               && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE))
2109                 ++i;                           /* the cursor should not jump  
2110                                                 * to the main body if there
2111                                                 * is nothing in! */
2112         return i;
2113 }
2114
2115
2116 LyXParagraph * LyXParagraph::DepthHook(int deth)
2117 {
2118         LyXParagraph * newpar = this;
2119         if (deth < 0)
2120                 return 0;
2121    
2122         do {
2123                 newpar = newpar->FirstPhysicalPar()->Previous();
2124         } while (newpar && newpar->GetDepth() > deth
2125                  && newpar->footnoteflag == footnoteflag);
2126    
2127         if (!newpar) {
2128                 if (Previous() || GetDepth())
2129                         lyxerr << "ERROR (LyXParagraph::DepthHook): "
2130                                 "no hook." << endl;
2131                 newpar = this;
2132         }
2133         return newpar->FirstPhysicalPar();
2134 }
2135
2136
2137 LyXParagraph const * LyXParagraph::DepthHook(int deth) const
2138 {
2139         LyXParagraph const * newpar = this;
2140         if (deth < 0)
2141                 return 0;
2142    
2143         do {
2144                 newpar = newpar->FirstPhysicalPar()->Previous();
2145         } while (newpar && newpar->GetDepth() > deth
2146                  && newpar->footnoteflag == footnoteflag);
2147    
2148         if (!newpar) {
2149                 if (Previous() || GetDepth())
2150                         lyxerr << "ERROR (LyXParagraph::DepthHook): "
2151                                 "no hook." << endl;
2152                 newpar = this;
2153         }
2154         return newpar->FirstPhysicalPar();
2155 }
2156
2157
2158 int LyXParagraph::AutoDeleteInsets()
2159 {
2160 #ifdef NEW_INSETTABLE
2161         vector<size_type> tmpvec;
2162         int i = 0;
2163         for (InsetList::iterator it = insetlist.begin();
2164              it != insetlist.end(); ++it) {
2165                 if ((*it).inset && (*it).inset->AutoDelete()) {
2166                         tmpvec.push_back((*it).pos);
2167                         ++i;
2168                 }
2169         }
2170         for (vector<size_type>::const_iterator cit = tmpvec.begin();
2171              cit != tmpvec.end(); ++cit) {
2172                 Erase((*cit));
2173         }
2174         return i;
2175 #else
2176         InsetTable * tmpi = insettable;
2177         InsetTable * tmpi2 = tmpi;
2178         int i = 0;
2179         while (tmpi) {
2180                 tmpi2 = tmpi;
2181                 tmpi = tmpi->next;
2182                 if (tmpi2->inset) {
2183                         if (tmpi2->inset->AutoDelete()) {
2184                                 ++i;
2185                                 Erase(tmpi2->pos);
2186                         }
2187                 } else {
2188                         lyxerr << "ERROR (LyXParagraph::AutoDeleteInsets): "
2189                                 "cannot auto-delete insets" << endl;
2190                 }
2191         }
2192         return i;
2193 #endif
2194 }
2195
2196
2197 Inset * LyXParagraph::ReturnNextInsetPointer(LyXParagraph::size_type & pos)
2198 {
2199 #ifdef NEW_INSETTABLE
2200         InsetList::iterator it2 = insetlist.end();
2201         for (InsetList::iterator it = insetlist.begin();
2202              it != insetlist.end(); ++it) {
2203                 if ((*it).pos >= pos) {
2204                         if (it2 != insetlist.end() || (*it).pos < (*it2).pos)
2205                                 it2 = it;
2206                 }
2207         }
2208         if (it2 != insetlist.end()) {
2209                 pos = (*it2).pos;
2210                 return (*it2).inset;
2211         }
2212         return 0;
2213 #else
2214         InsetTable * tmpi = insettable;
2215         InsetTable * tmpi2 = 0;
2216         while (tmpi){
2217                 if (tmpi->pos >= pos) {
2218                         if (!tmpi2 || tmpi->pos < tmpi2->pos)
2219                                 tmpi2 = tmpi;
2220                 }
2221                 tmpi= tmpi->next;
2222         }
2223         if (tmpi2){
2224                 pos = tmpi2->pos;
2225                 return tmpi2->inset;
2226         }
2227         else
2228                 return 0;
2229 #endif
2230 }
2231
2232
2233 // returns -1 if inset not found
2234 int LyXParagraph::GetPositionOfInset(Inset * inset) const
2235 {
2236         // Find the entry.
2237 #ifdef NEW_INSETTABLE
2238         for (InsetList::const_iterator cit = insetlist.begin();
2239              cit != insetlist.end(); ++cit) {
2240                 if ((*cit).inset == inset) {
2241                         return (*cit).pos;
2242                 }
2243         }
2244 #else
2245         InsetTable * tmpi = insettable;
2246         while (tmpi && tmpi->inset != inset) {
2247                 tmpi = tmpi->next;
2248         }
2249         if (tmpi && tmpi->inset)
2250                 return tmpi->pos;
2251 #endif
2252         // Think about footnotes.
2253         if (footnoteflag == LyXParagraph::NO_FOOTNOTE 
2254             && next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
2255                 int further = 
2256                         NextAfterFootnote()->GetPositionOfInset(inset);
2257                 if (further != -1)
2258                         return text.size() + 1 + further;
2259         }
2260         return -1;
2261 }
2262
2263
2264 void LyXParagraph::readSimpleWholeFile(istream & is)
2265 {
2266         is.seekg(0);
2267         char c = 0;
2268         while(!is.eof()) {
2269                 is.get(c);
2270                 InsertChar(text.size(), c);
2271         };
2272 }
2273
2274
2275 LyXParagraph * LyXParagraph::TeXOnePar(string & file, TexRow & texrow,
2276                                        string & foot, TexRow & foot_texrow,
2277                                        int & foot_count)
2278 {
2279         lyxerr[Debug::LATEX] << "TeXOnePar...     " << this << endl;
2280         LyXParagraph * par = next;
2281         LyXLayout const & style =
2282                 textclasslist.Style(current_view->buffer()->params.textclass,
2283                                     layout);
2284
2285         bool further_blank_line = false;
2286         if (IsDummy())
2287                 lyxerr << "ERROR (LyXParagraph::TeXOnePar) is dummy." << endl;
2288
2289         if (start_of_appendix) {
2290                 file += "\\appendix\n";
2291                 texrow.newline();
2292         }
2293
2294         if (tex_code_break_column && style.isCommand()){
2295                 file += '\n';
2296                 texrow.newline();
2297         }
2298
2299         if (pagebreak_top) {
2300                 file += "\\newpage";
2301                 further_blank_line = true;
2302         }
2303         if (added_space_top.kind() != VSpace::NONE) {
2304                 file += added_space_top.asLatexCommand();
2305                 further_blank_line = true;
2306         }
2307       
2308         if (line_top) {
2309                 file += "\\lyxline{\\" + getFont(0).latexSize() + '}';
2310                 file += "\\vspace{-1\\parskip}";
2311                 further_blank_line = true;
2312         }
2313
2314         if (further_blank_line){
2315                 file += '\n';
2316                 texrow.newline();
2317         }
2318
2319         switch (style.latextype) {
2320         case LATEX_COMMAND:
2321                 file += '\\';
2322                 file += style.latexname();
2323                 file += style.latexparam();
2324                 break;
2325         case LATEX_ITEM_ENVIRONMENT:
2326                 if (bibkey) 
2327                         bibkey->Latex(file, false);
2328                 else
2329                         file += "\\item ";
2330                 break;
2331         case LATEX_LIST_ENVIRONMENT:
2332                 file += "\\item ";
2333                 break;
2334         default:
2335                 break;
2336         }
2337
2338         bool need_par = SimpleTeXOnePar(file, texrow);
2339  
2340         // Spit out footnotes
2341         while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2342                && par->footnoteflag != footnoteflag) {
2343                 par = par->TeXFootnote(file, texrow,
2344                                        foot, foot_texrow, foot_count);
2345                 par->SimpleTeXOnePar(file, texrow);
2346                 par = par->next;
2347         }
2348
2349         // Make sure that \\par is done with the font of the last
2350         // character if this has another size as the default.
2351         // This is necessary because LaTeX (and LyX on the screen)
2352         // calculates the space between the baselines according
2353         // to this font. (Matthias)
2354         LyXFont font = getFont(Last()-1);
2355         if (need_par) {
2356                 if (style.resfont.size() != font.size()) {
2357                         file += '\\';
2358                         file += font.latexSize();
2359                         file += ' ';
2360                 }
2361                 file += "\\par}";
2362         } else if (textclasslist.Style(current_view->buffer()->params.textclass,
2363                                        GetLayout()).isCommand()){
2364                 if (style.resfont.size() != font.size()) {
2365                         file += '\\';
2366                         file += font.latexSize();
2367                         file += ' ';
2368                 }
2369                 file += '}';
2370         } else if (style.resfont.size() != font.size()){
2371                 file += "{\\" + font.latexSize() + " \\par}";
2372         }
2373         
2374         switch (style.latextype) {
2375         case LATEX_ITEM_ENVIRONMENT:
2376         case LATEX_LIST_ENVIRONMENT:
2377                 if (par && (depth < par->depth)) {
2378                         file += '\n';
2379                         texrow.newline();
2380                 }
2381                 break;
2382         case LATEX_ENVIRONMENT:
2383                 // if its the last paragraph of the current environment
2384                 // skip it otherwise fall through
2385                 if (par
2386                     && (par->layout != layout
2387                         || par->depth != depth
2388                         || par->pextra_type != pextra_type))
2389                         break;
2390         default:
2391                 if (!(footnoteflag != LyXParagraph::NO_FOOTNOTE
2392                       && footnotekind != LyXParagraph::FOOTNOTE
2393                       && footnotekind != LyXParagraph::MARGIN
2394                       && (table
2395                           || (par
2396                               && par->table)))) {
2397                         // don't insert this if we would be adding it
2398                         // before or after a table in a float.  This 
2399                         // little trick is needed in order to allow
2400                         // use of tables in \subfigures or \subtables.
2401                         file += '\n';
2402                         texrow.newline();
2403                 }
2404         }
2405         
2406         further_blank_line = false;
2407         if (line_bottom) {
2408                 file += "\\lyxline{\\" + getFont(Last()-1).latexSize() + '}';
2409                 further_blank_line = true;
2410         }
2411
2412         if (added_space_bottom.kind() != VSpace::NONE) {
2413                 file += added_space_bottom.asLatexCommand();
2414                 further_blank_line = true;
2415         }
2416       
2417         if (pagebreak_bottom) {
2418                 file += "\\newpage";
2419                 further_blank_line = true;
2420         }
2421
2422         if (further_blank_line){
2423                 file += '\n';
2424                 texrow.newline();
2425         }
2426
2427         if (!(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2428               par->footnoteflag == LyXParagraph::NO_FOOTNOTE)) {
2429                 file += '\n';
2430                 texrow.newline();
2431         }
2432
2433         lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2434         return par;
2435 }
2436
2437
2438 // This one spits out the text of the paragraph
2439 bool LyXParagraph::SimpleTeXOnePar(string & file, TexRow & texrow)
2440 {
2441         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
2442
2443         if (table)
2444                 return SimpleTeXOneTablePar(file, texrow);
2445
2446         char c;
2447         size_type main_body;
2448         
2449         bool return_value = false;
2450
2451         LyXLayout const & style = textclasslist.Style(current_view->buffer()->params.textclass, GetLayout());
2452         LyXFont basefont;
2453
2454         // Maybe we have to create a optional argument.
2455         if (style.labeltype != LABEL_MANUAL)
2456                 main_body = 0;
2457         else
2458                 main_body = BeginningOfMainBody();
2459
2460         if (main_body > 0) {
2461                 file += '[';
2462                 basefont = getFont(-2); // Get label font
2463         } else {
2464                 basefont = getFont(-1); // Get layout font
2465         }
2466
2467         int column = 0;
2468
2469         if (main_body >= 0
2470             && !text.size()
2471             && !IsDummy()) {
2472                 if (style.isCommand()) {
2473                         file += '{';
2474                         ++column;
2475                 } else if (align != LYX_ALIGN_LAYOUT) {
2476                         file += '{';
2477                         ++column;
2478                         return_value = true;
2479                 }
2480         }
2481  
2482         // Which font is currently active?
2483         LyXFont running_font = basefont;
2484         // Do we have an open font change?
2485         bool open_font = false;
2486
2487         texrow.start(this, 0);
2488
2489         for (size_type i = 0; i < size(); ++i) {
2490                 ++column;
2491                 // First char in paragraph or after label?
2492                 if (i == main_body && !IsDummy()) {
2493                         if (main_body > 0) {
2494                                 if (open_font) {
2495                                         column += running_font.latexWriteEndChanges(file, basefont);
2496                                         open_font = false;
2497                                 }
2498                                 basefont = getFont(-1); // Now use the layout font
2499                                 running_font = basefont;
2500                                 file += ']';
2501                                 ++column;
2502                         }
2503                         if (style.isCommand()) {
2504                                 file += '{';
2505                                 ++column;
2506                         } else if (align != LYX_ALIGN_LAYOUT) {
2507                                 file += "{\\par";
2508                                 column += 4;
2509                                 return_value = true;
2510                         }
2511
2512                         if (noindent) {
2513                                 file += "\\noindent ";
2514                                 column += 10;
2515                         }
2516                         switch (align) {
2517                         case LYX_ALIGN_NONE:
2518                         case LYX_ALIGN_BLOCK:
2519                         case LYX_ALIGN_LAYOUT:
2520                         case LYX_ALIGN_SPECIAL: break;
2521                         case LYX_ALIGN_LEFT:
2522                                 file += "\\raggedright ";
2523                                 column+= 13;
2524                                 break;
2525                         case LYX_ALIGN_RIGHT:
2526                                 file += "\\raggedleft ";
2527                                 column+= 12;
2528                                 break;
2529                         case LYX_ALIGN_CENTER:
2530                                 file += "\\centering ";
2531                                 column+= 11;
2532                                 break;
2533                         }        
2534                 }
2535
2536                 c = GetChar(i);
2537
2538                 // Fully instantiated font
2539                 LyXFont font = getFont(i);
2540
2541                 // Spaces at end of font change are simulated to be
2542                 // outside font change, i.e. we write "\textXX{text} "
2543                 // rather than "\textXX{text }". (Asger)
2544                 if (open_font && c == ' ' && i <= size() - 2 
2545                     && !getFont(i+1).equalExceptLatex(running_font) 
2546                     && !getFont(i+1).equalExceptLatex(font)) {
2547                         font = getFont(i + 1);
2548                 }
2549                 // We end font definition before blanks
2550                 if (!font.equalExceptLatex(running_font) && open_font) {
2551                         column += running_font.latexWriteEndChanges(file,
2552                                                                     basefont);
2553                         running_font = basefont;
2554                         open_font = false;
2555                 }
2556
2557                 // Blanks are printed before start of fontswitch
2558                 if (c == ' '){
2559                         // Do not print the separation of the optional argument
2560                         if (i != main_body - 1) {
2561                                 SimpleTeXBlanks(file, texrow, i,
2562                                                 column, font, style);
2563                         }
2564                 }
2565
2566                 // Do we need to change font?
2567                 if (!font.equalExceptLatex(running_font)
2568                     && i != main_body-1) {
2569                         column += font.latexWriteStartChanges(file, basefont);
2570                         running_font = font;
2571                         open_font = true;
2572                 }
2573
2574                 if (c == LyXParagraph::META_NEWLINE) {
2575                         // newlines are handled differently here than
2576                         // the default in SimpleTeXSpecialChars().
2577                         if (!style.newline_allowed
2578                             || font.latex() == LyXFont::ON) {
2579                                 file += '\n';
2580                         } else {
2581                                 if (open_font) {
2582                                         column += running_font.latexWriteEndChanges(file, basefont);
2583                                         open_font = false;
2584                                 }
2585                                 basefont = getFont(-1);
2586                                 running_font = basefont;
2587                                 if (font.family() == 
2588                                     LyXFont::TYPEWRITER_FAMILY) {
2589                                         file += "~";
2590                                 }
2591                                 file += "\\\\\n";
2592                         }
2593                         texrow.newline();
2594                         texrow.start(this, i + 1);
2595                         column = 0;
2596                 } else {
2597                         SimpleTeXSpecialChars(file, texrow,
2598                                               font, running_font, basefont,
2599                                               open_font, style, i, column, c);
2600                 }
2601         }
2602
2603         // If we have an open font definition, we have to close it
2604         if (open_font) {
2605                 running_font.latexWriteEndChanges(file, basefont);
2606         }
2607
2608         // Needed if there is an optional argument but no contents.
2609         if (main_body > 0 && main_body == size()) {
2610                 file += "]~";
2611                 return_value = false;
2612         }
2613
2614         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2615         return return_value;
2616 }
2617
2618
2619 // This one spits out the text of a table paragraph
2620 bool LyXParagraph::SimpleTeXOneTablePar(string & file, TexRow & texrow)
2621 {
2622         lyxerr[Debug::LATEX] << "SimpleTeXOneTablePar...     " << this << endl;
2623    
2624         bool return_value = false;
2625
2626         LyXLayout const & style = 
2627                 textclasslist.Style(current_view->buffer()->params.textclass,
2628                                     GetLayout());
2629  
2630         int column = 0;
2631         if (!IsDummy()) { // it is dummy if it is in a float!!!
2632                 if (style.isCommand()) {
2633                         file += '{';
2634                         ++column;
2635                 } else if (align != LYX_ALIGN_LAYOUT) {
2636                         file += '{';
2637                         ++column;
2638                         return_value = true;
2639                 }
2640                 if (noindent) {
2641                         file += "\\noindent ";
2642                         column += 10;
2643                 }
2644                 switch (align) {
2645                 case LYX_ALIGN_NONE:
2646                 case LYX_ALIGN_BLOCK:
2647                 case LYX_ALIGN_LAYOUT:
2648                 case LYX_ALIGN_SPECIAL: break;
2649                 case LYX_ALIGN_LEFT:
2650                         file += "\\raggedright ";
2651                         column+= 13;
2652                         break;
2653                 case LYX_ALIGN_RIGHT:
2654                         file += "\\raggedleft ";
2655                         column+= 12;
2656                         break;
2657                 case LYX_ALIGN_CENTER:
2658                         file += "\\centering ";
2659                         column+= 11;
2660                         break;
2661                 }
2662         }
2663
2664         LyXFont basefont = getFont(-1); // Get layout font
2665         // Which font is currently active?
2666         LyXFont running_font = basefont;
2667         // Do we have an open font change?
2668         bool open_font = false;
2669         int current_cell_number = -1;
2670         int tmp = table->TexEndOfCell(file, current_cell_number);
2671         for (; tmp > 0 ; --tmp)
2672                 texrow.newline();
2673         
2674         texrow.start(this, 0);
2675
2676         for (size_type i = 0; i < size(); ++i) {
2677                 char c = GetChar(i);
2678                 if (table->IsContRow(current_cell_number + 1)) {
2679                         if (c == LyXParagraph::META_NEWLINE)
2680                                 ++current_cell_number;
2681                         continue;
2682                 }
2683                 ++column;
2684                 
2685                 // Fully instantiated font
2686                 LyXFont font = getFont(i);
2687
2688                 // Spaces at end of font change are simulated to be
2689                 // outside font change.
2690                 // i.e. we write "\textXX{text} " rather than
2691                 // "\textXX{text }". (Asger)
2692                 if (open_font && c == ' ' && i <= size() - 2
2693                     && getFont(i+1) != running_font && getFont(i+1) != font) {
2694                         font = getFont(i+1);
2695                 }
2696
2697                 // We end font definition before blanks
2698                 if (font != running_font && open_font) {
2699                         column += running_font.latexWriteEndChanges(file,
2700                                                                     basefont);
2701                         running_font = basefont;
2702                         open_font = false;
2703                 }
2704                 // Blanks are printed before start of fontswitch
2705                 if (c == ' '){
2706                         SimpleTeXBlanks(file, texrow, i, column, font, style);
2707                 }
2708                 // Do we need to change font?
2709                 if (font != running_font) {
2710                         column += font.latexWriteStartChanges(file, basefont);
2711                         running_font = font;
2712                         open_font = true;
2713                 }
2714                 // Do we need to turn on LaTeX mode?
2715                 if (font.latex() != running_font.latex()) {
2716                         if (font.latex() == LyXFont::ON
2717                             && style.needprotect) {
2718                                 file += "\\protect ";
2719                                 column += 9;
2720                         }
2721                 }
2722                 if (c == LyXParagraph::META_NEWLINE) {
2723                         // special case for inside a table
2724                         // different from default case in
2725                         // SimpleTeXSpecialChars()
2726                         if (open_font) {
2727                                 column += running_font
2728                                         .latexWriteEndChanges(file, basefont);
2729                                 open_font = false;
2730                         }
2731                         basefont = getFont(-1);
2732                         running_font = basefont;
2733                         ++current_cell_number;
2734                         if (table->CellHasContRow(current_cell_number) >= 0) {
2735                                 TeXContTableRows(file, i + 1,
2736                                                  current_cell_number,
2737                                                  column, texrow);
2738                         }
2739                         // if this cell follow only ContRows till end don't
2740                         // put the EndOfCell because it is put after the
2741                         // for(...)
2742                         if (table->ShouldBeVeryLastCell(current_cell_number)) {
2743                                 --current_cell_number;
2744                                 break;
2745                         }
2746                         int tmp = table->TexEndOfCell(file,
2747                                                       current_cell_number);
2748                         if (tmp > 0) {
2749                                 column = 0;
2750                         } else if (tmp < 0) {
2751                                 tmp = -tmp;
2752                         }
2753                         for (; tmp--;) {
2754                                 texrow.newline();
2755                         }
2756                         texrow.start(this, i + 1);
2757                 } else {
2758                         SimpleTeXSpecialChars(file, texrow,
2759                                               font, running_font, basefont,
2760                                               open_font, style, i, column, c);
2761                 }
2762         }
2763
2764         // If we have an open font definition, we have to close it
2765         if (open_font) {
2766                 running_font.latexWriteEndChanges(file, basefont);
2767         }
2768         ++current_cell_number;
2769         tmp = table->TexEndOfCell(file, current_cell_number);
2770         for (; tmp > 0; --tmp)
2771                 texrow.newline();
2772         lyxerr[Debug::LATEX] << "SimpleTeXOneTablePar...done " << this << endl;
2773         return return_value;
2774 }
2775
2776
2777 // This one spits out the text off ContRows in tables
2778 bool LyXParagraph::TeXContTableRows(string & file,
2779                                     LyXParagraph::size_type i,
2780                                     int current_cell_number,
2781                                     int & column, TexRow & texrow)
2782 {
2783         lyxerr[Debug::LATEX] << "TeXContTableRows...     " << this << endl;
2784         if (!table)
2785                 return false;
2786     
2787         char c;
2788    
2789         bool return_value = false;
2790         LyXLayout const & style =
2791                 textclasslist.Style(current_view->buffer()->params.textclass,
2792                                     GetLayout());
2793         LyXFont basefont = getFont(-1); // Get layout font
2794         // Which font is currently active?
2795         LyXFont running_font = basefont;
2796         // Do we have an open font change?
2797         bool open_font = false;
2798
2799         size_type lastpos = i;
2800         int cell = table->CellHasContRow(current_cell_number);
2801         ++current_cell_number;
2802         while(cell >= 0) {
2803                 // first find the right position
2804                 i = lastpos;
2805                 for (; (i < size()) && (current_cell_number<cell); ++i) {
2806                         c = GetChar(i);
2807                         if (c == LyXParagraph::META_NEWLINE)
2808                                 ++current_cell_number;
2809                 }
2810                 lastpos = i;
2811                 c = GetChar(i);
2812                 if (table->Linebreaks(table->FirstVirtualCell(cell))) {
2813                         file += " \\\\\n";
2814                         texrow.newline();
2815                         column = 0;
2816                 } else if ((c != ' ') && (c != LyXParagraph::META_NEWLINE)) {
2817                         file += ' ';
2818                 }
2819
2820                 for (; i < size()
2821                              && (c = GetChar(i)) != LyXParagraph::META_NEWLINE;
2822                      ++i) {
2823                         ++column;
2824
2825                         // Fully instantiated font
2826                         LyXFont font = getFont(i);
2827
2828                         // Spaces at end of font change are simulated to
2829                         // be outside font change. i.e. we write
2830                         // "\textXX{text} " rather than "\textXX{text }".
2831                         // (Asger)
2832                         if (open_font && c == ' ' && i <= size() - 2 
2833                             && getFont(i + 1) != running_font
2834                             && getFont(i + 1) != font) {
2835                                 font = getFont(i + 1);
2836                         }
2837
2838                         // We end font definition before blanks
2839                         if (font != running_font && open_font) {
2840                                 column += running_font.latexWriteEndChanges(file, basefont);
2841                                 running_font = basefont;
2842                                 open_font = false;
2843                         }
2844                         // Blanks are printed before start of fontswitch
2845                         if (c == ' '){
2846                                 SimpleTeXBlanks(file, texrow, i,
2847                                                 column, font, style);
2848                         }
2849                         // Do we need to change font?
2850                         if (font != running_font) {
2851                                 column +=
2852                                         font.latexWriteStartChanges(file,
2853                                                                     basefont);
2854                                 running_font = font;
2855                                 open_font = true;
2856                         }
2857                         // Do we need to turn on LaTeX mode?
2858                         if (font.latex() != running_font.latex()) {
2859                                 if (font.latex() == LyXFont::ON
2860                                     && style.needprotect)
2861                                         {
2862                                                 file += "\\protect ";
2863                                                 column += 9;
2864                                         }
2865                         }
2866                         SimpleTeXSpecialChars(file, texrow, font,
2867                                               running_font, basefont,
2868                                               open_font, style, i, column, c);
2869                 }
2870                 // If we have an open font definition, we have to close it
2871                 if (open_font) {
2872                         running_font.latexWriteEndChanges(file, basefont);
2873                         open_font = false;
2874                 }
2875                 basefont = getFont(-1);
2876                 running_font = basefont;
2877                 cell = table->CellHasContRow(current_cell_number);
2878         }
2879         lyxerr[Debug::LATEX] << "TeXContTableRows...done " << this << endl;
2880         return return_value;
2881 }
2882
2883
2884 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2885 {
2886         bool retval = false;
2887         switch (c) {
2888         case LyXParagraph::META_HFILL:
2889                 sgml_string.clear();
2890                 break;
2891         case LyXParagraph::META_PROTECTED_SEPARATOR: 
2892                 sgml_string = ' ';
2893                 break;
2894         case LyXParagraph::META_NEWLINE:
2895                 sgml_string = '\n';
2896                 break;
2897         case '&': 
2898                 sgml_string = "&amp;";
2899                 break;
2900         case '<': 
2901                 sgml_string = "&lt;"; 
2902                 break;
2903         case '>':
2904                 sgml_string = "&gt;"; 
2905                 break;
2906         case '$': 
2907                 sgml_string = "&dollar;"; 
2908                 break;
2909         case '#': 
2910                 sgml_string = "&num;";
2911                 break;
2912         case '%': 
2913                 sgml_string = "&percnt;";
2914                 break;
2915         case '[': 
2916                 sgml_string = "&lsqb;";
2917                 break;
2918         case ']': 
2919                 sgml_string = "&rsqb;";
2920                 break;
2921         case '{': 
2922                 sgml_string = "&lcub;";
2923                 break;
2924         case '}': 
2925                 sgml_string = "&rcub;";
2926                 break;
2927         case '~': 
2928                 sgml_string = "&tilde;";
2929                 break;
2930         case '"': 
2931                 sgml_string = "&quot;";
2932                 break;
2933         case '\\': 
2934                 sgml_string = "&bsol;";
2935                 break;
2936         case ' ':
2937                 retval = true;
2938                 sgml_string = ' ';
2939                 break;
2940         case '\0': // Ignore :-)
2941                 sgml_string.clear();
2942                 break;
2943         default:
2944                 sgml_string = c;
2945                 break;
2946         }
2947         return retval;
2948 }
2949
2950
2951 void LyXParagraph::SimpleDocBookOneTablePar(string & file, string & extra,
2952                                             int & desc_on, int depth) 
2953 {
2954         if (!table) return;
2955         lyxerr[Debug::LATEX] << "SimpleDocbookOneTablePar... " << this << endl;
2956         int column;
2957         LyXFont font1, font2;
2958         char c;
2959         Inset * inset;
2960         size_type main_body;
2961         string emph = "emphasis";
2962         bool emph_flag = false;
2963         
2964         LyXLayout const & style =
2965                 textclasslist.Style(current_view->buffer()->params.textclass,
2966                                     GetLayout());
2967         
2968         if (style.labeltype != LABEL_MANUAL)
2969                 main_body = 0;
2970         else
2971                 main_body = BeginningOfMainBody();
2972         
2973         // Gets paragraph main font.
2974         if (main_body > 0)
2975                 font1 = style.labelfont;
2976         else
2977                 font1 = style.font;
2978         
2979         int char_line_count = depth;
2980         addNewlineAndDepth(file, depth);
2981         if (footnoteflag == LyXParagraph::NO_FOOTNOTE) {
2982                 file += "<INFORMALTABLE>";
2983                 addNewlineAndDepth(file, ++depth);
2984         }
2985         int current_cell_number = -1;
2986         int tmp = table->DocBookEndOfCell(file, current_cell_number, depth);
2987         
2988         // Parsing main loop.
2989         for (size_type i = 0; i < size(); ++i) {
2990                 c = GetChar(i);
2991                 if (table->IsContRow(current_cell_number+1)) {
2992                         if (c == LyXParagraph::META_NEWLINE)
2993                                 ++current_cell_number;
2994                         continue;
2995                 }
2996                 ++column;
2997                 
2998                 // Fully instantiated font
2999                 font2 = getFont(i);
3000                 
3001                 // Handle <emphasis> tag.
3002                 if (font1.emph() != font2.emph() && i) {
3003                         if (font2.emph() == LyXFont::ON) {
3004                                 file += "<emphasis>";
3005                                 emph_flag= true;
3006                         } else if (emph_flag) {
3007                                 file += "</emphasis>";
3008                                 emph_flag= false;
3009                         }
3010                 }
3011                 if (c == LyXParagraph::META_NEWLINE) {
3012                         // We have only to control for emphasis open here!
3013                         if (emph_flag) {
3014                                 file += "</emphasis>";
3015                                 emph_flag= false;
3016                         }
3017                         font1 = font2 = getFont(-1);
3018                         ++current_cell_number;
3019                         if (table->CellHasContRow(current_cell_number) >= 0) {
3020                                 DocBookContTableRows(file, extra, desc_on, i+1,
3021                                                      current_cell_number,
3022                                                      column);
3023                         }
3024                         // if this cell follow only ContRows till end don't
3025                         // put the EndOfCell because it is put after the
3026                         // for(...)
3027                         if (table->ShouldBeVeryLastCell(current_cell_number)) {
3028                                 --current_cell_number;
3029                                 break;
3030                         }
3031                         tmp = table->DocBookEndOfCell(file,
3032                                                       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 }