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