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