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