]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
76311c25589cc39b6d4d60a381c2fbcea9f640f6
[lyx.git] / src / paragraph.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team. 
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include <algorithm>
18 #include <fstream>
19 #include <csignal>
20
21 #include "paragraph.h"
22 #include "paragraph_pimpl.h"
23 #include "support/textutils.h"
24 #include "lyxrc.h"
25 #include "layout.h"
26 #include "language.h"
27 #include "tex-strings.h"
28 #include "buffer.h"
29 #include "bufferparams.h"
30 #include "support/FileInfo.h"
31 #include "support/LAssert.h"
32 #include "debug.h"
33 #include "LaTeXFeatures.h"
34 #include "insets/insetinclude.h"
35 #include "insets/insetbib.h"
36 #include "insets/insettext.h"
37 #include "support/filetools.h"
38 #include "support/lstrings.h"
39 #include "lyx_gui_misc.h"
40 #include "texrow.h"
41 #include "support/lyxmanip.h"
42 #include "BufferView.h"
43 #include "encoding.h"
44 #include "ParameterStruct.h"
45 #include "gettext.h"
46
47 using std::ostream;
48 using std::endl;
49 using std::fstream;
50 using std::ios;
51 using std::lower_bound;
52 using std::upper_bound;
53 using std::reverse;
54
55 using lyx::pos_type;
56 using lyx::layout_type;
57
58 int tex_code_break_column = 72;  // needs non-zero initialization. set later.
59 // this is a bad idea, but how can Paragraph find its buffer to get
60 // parameters? (JMarc)
61
62 extern string bibitemWidest(Buffer const *);
63
64 // this is a minibuffer
65
66 namespace {
67
68 char minibuffer_char;
69 LyXFont minibuffer_font;
70 Inset * minibuffer_inset;
71
72 } // namespace anon
73
74
75 extern BufferView * current_view;
76
77
78 Paragraph::Paragraph()
79                 : pimpl_(new Paragraph::Pimpl(this))
80 {
81         for (int i = 0; i < 10; ++i) setCounter(i , 0);
82         next_ = 0;
83         previous_ = 0;
84         enumdepth = 0;
85         itemdepth = 0;
86         bibkey = 0; // ale970302
87         clear();
88 }
89
90
91 // This konstruktor inserts the new paragraph in a list.
92 Paragraph::Paragraph(Paragraph * par)
93                 : pimpl_(new Paragraph::Pimpl(this))
94 {
95         for (int i = 0; i < 10; ++i)
96                 setCounter(i, 0);
97         enumdepth = 0;
98         itemdepth = 0;
99
100         // double linked list begin
101         next_ = par->next_;
102         if (next_)
103                 next_->previous_ = this;
104         previous_ = par;
105         previous_->next_ = this;
106         // end
107
108         bibkey = 0; // ale970302        
109
110         clear();
111 }
112
113
114 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
115         : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
116 {
117         for (int i = 0; i < 10; ++i)
118                 setCounter(i, 0);
119         enumdepth = 0;
120         itemdepth = 0;
121         next_     = 0;
122         previous_ = 0;
123
124         // this is because of the dummy layout of the paragraphs that
125         // follow footnotes
126         layout = lp.layout;
127
128         // ale970302
129         if (lp.bibkey) {
130                 bibkey = static_cast<InsetBibKey *>
131                         (lp.bibkey->clone(*current_view->buffer()));
132         } else {
133                 bibkey = 0;
134         }
135         
136         // copy everything behind the break-position to the new paragraph
137         insetlist = lp.insetlist;
138         for (InsetList::iterator it = insetlist.begin();
139              it != insetlist.end(); ++it)
140         {
141                 it->inset = it->inset->clone(*current_view->buffer(), same_ids);
142         }
143 }
144
145
146 // the destructor removes the new paragraph from the list
147 Paragraph::~Paragraph()
148 {
149         if (previous_)
150                 previous_->next_ = next_;
151         if (next_)
152                 next_->previous_ = previous_;
153
154         for (InsetList::iterator it = insetlist.begin();
155              it != insetlist.end(); ++it) {
156                 delete it->inset;
157         }
158
159         // ale970302
160         delete bibkey;
161
162         delete pimpl_;
163         //
164         //lyxerr << "Paragraph::paragraph_id = "
165         //       << Paragraph::paragraph_id << endl;
166 }
167
168
169 void Paragraph::writeFile(Buffer const * buf, ostream & os,
170                           BufferParams const & bparams,
171                           depth_type dth) const
172 {
173         // The beginning or end of a deeper (i.e. nested) area?
174         if (dth != params().depth()) {
175                 if (params().depth() > dth) {
176                         while (params().depth() > dth) {
177                                 os << "\n\\begin_deeper ";
178                                 ++dth;
179                         }
180                 } else {
181                         while (params().depth() < dth) {
182                                 os << "\n\\end_deeper ";
183                                 --dth;
184                         }
185                 }
186         }
187         
188         // First write the layout
189         os << "\n\\layout "
190            << textclasslist.NameOfLayout(bparams.textclass, layout)
191            << "\n";
192         
193         // Maybe some vertical spaces.
194         if (params().spaceTop().kind() != VSpace::NONE)
195                 os << "\\added_space_top "
196                    << params().spaceTop().asLyXCommand() << " ";
197         if (params().spaceBottom().kind() != VSpace::NONE)
198                 os << "\\added_space_bottom "
199                    << params().spaceBottom().asLyXCommand() << " ";
200         
201         // Maybe the paragraph has special spacing
202         params().spacing().writeFile(os, true);
203         
204         // The labelwidth string used in lists.
205         if (!params().labelWidthString().empty())
206                 os << "\\labelwidthstring "
207                    << params().labelWidthString() << '\n';
208         
209         // Lines above or below?
210         if (params().lineTop())
211                 os << "\\line_top ";
212         if (params().lineBottom())
213                 os << "\\line_bottom ";
214         
215         // Pagebreaks above or below?
216         if (params().pagebreakTop())
217                 os << "\\pagebreak_top ";
218         if (params().pagebreakBottom())
219                 os << "\\pagebreak_bottom ";
220         
221         // Start of appendix?
222         if (params().startOfAppendix())
223                 os << "\\start_of_appendix ";
224         
225         // Noindent?
226         if (params().noindent())
227                 os << "\\noindent ";
228         
229         // Alignment?
230         if (params().align() != LYX_ALIGN_LAYOUT) {
231                 int h = 0;
232                 switch (params().align()) {
233                 case LYX_ALIGN_LEFT: h = 1; break;
234                 case LYX_ALIGN_RIGHT: h = 2; break;
235                 case LYX_ALIGN_CENTER: h = 3; break;
236                 default: h = 0; break;
237                 }
238                 os << "\\align " << string_align[h] << " ";
239         }
240         
241         // bibitem  ale970302
242         if (bibkey)
243                 bibkey->write(buf, os);
244         
245         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
246         
247         int column = 0;
248         for (pos_type i = 0; i < size(); ++i) {
249                 if (!i) {
250                         os << "\n";
251                         column = 0;
252                 }
253                 
254                 // Write font changes
255                 LyXFont font2 = getFontSettings(bparams, i);
256                 if (font2 != font1) {
257 #ifndef INHERIT_LANGUAGE
258                         font2.lyxWriteChanges(font1, os);
259 #else
260                         font2.lyxWriteChanges(font1, bparams.language, os);
261 #endif
262                         column = 0;
263                         font1 = font2;
264                 }
265                 
266                 value_type const c = getChar(i);
267                 switch (c) {
268                 case META_INSET:
269                 {
270                         Inset const * inset = getInset(i);
271                         if (inset)
272                                 if (inset->directWrite()) {
273                                         // international char, let it write
274                                         // code directly so it's shorter in
275                                         // the file
276                                         inset->write(buf, os);
277                                 } else {
278                                         os << "\n\\begin_inset ";
279                                         inset->write(buf, os);
280                                         os << "\n\\end_inset \n\n";
281                                         column = 0;
282                                 }
283                 }
284                 break;
285                 case META_NEWLINE: 
286                         os << "\n\\newline \n";
287                         column = 0;
288                         break;
289                 case META_HFILL: 
290                         os << "\n\\hfill \n";
291                         column = 0;
292                         break;
293                 case '\\':
294                         os << "\n\\backslash \n";
295                         column = 0;
296                         break;
297                 case '.':
298                         if (i + 1 < size() && getChar(i + 1) == ' ') {
299                                 os << ".\n";
300                                 column = 0;
301                         } else
302                                 os << ".";
303                         break;
304                 default:
305                         if ((column > 70 && c == ' ')
306                             || column > 79) {
307                                 os << "\n";
308                                 column = 0;
309                         }
310                         // this check is to amend a bug. LyX sometimes
311                         // inserts '\0' this could cause problems.
312                         if (c != '\0')
313                                 os << c;
314                         else
315                                 lyxerr << "ERROR (Paragraph::writeFile):"
316                                         " NULL char in structure." << endl;
317                         ++column;
318                         break;
319                 }
320         }
321         
322         // now write the next paragraph
323         if (next_)
324                 next_->writeFile(buf, os, bparams, dth);
325 }
326
327
328 void Paragraph::validate(LaTeXFeatures & features) const
329 {
330         BufferParams const & bparams = features.bufferParams();
331
332         // check the params.
333         if (params().lineTop() || params().lineBottom())
334                 features.require("lyxline");
335         if (!params().spacing().isDefault())
336                 features.require("setspace");
337         
338         // then the layouts
339         features.useLayout(getLayout());
340
341         // then the fonts
342         Language const * doc_language = bparams.language;
343         
344         for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
345              cit != pimpl_->fontlist.end(); ++cit) {
346                 if (cit->font().noun() == LyXFont::ON) {
347                         lyxerr[Debug::LATEX] << "font.noun: "
348                                              << cit->font().noun()
349                                              << endl;
350                         features.require("noun");
351                         lyxerr[Debug::LATEX] << "Noun enabled. Font: "
352                                              << cit->font().stateText(0)
353                                              << endl;
354                 }
355                 switch (cit->font().color()) {
356                 case LColor::none:
357                 case LColor::inherit:
358                 case LColor::ignore:
359                         // probably we should put here all interface colors used for
360                         // font displaying! For now I just add this ones I know of (Jug)
361                 case LColor::latex:
362                 case LColor::note:
363                         break;
364                 default:
365                         features.require("color");
366                         lyxerr[Debug::LATEX] << "Color enabled. Font: "
367                                              << cit->font().stateText(0)
368                                              << endl;
369                 }
370
371                 Language const * language = cit->font().language();
372                 if (language->babel() != doc_language->babel() &&
373                     language != ignore_language &&
374 #ifdef INHERIT_LANGUAGE
375                     language != inherit_language &&
376 #endif
377                     language != latex_language)
378                 {
379                         features.useLanguage(language);
380                         lyxerr[Debug::LATEX] << "Found language "
381                                              << language->babel() << endl;
382                 }
383         }
384
385         // then the insets
386         LyXLayout const & layout =
387              textclasslist.Style(bparams.textclass, getLayout());
388
389         for (InsetList::const_iterator cit = insetlist.begin();
390              cit != insetlist.end(); ++cit) {
391                 if (cit->inset) {
392                         cit->inset->validate(features);
393                         if (layout.needprotect &&
394                             cit->inset->lyxCode() == Inset::FOOT_CODE)
395                                 features.require("NeedLyXFootnoteCode");
396                 }
397         }
398 }
399
400
401 // First few functions needed for cut and paste and paragraph breaking.
402 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
403 {
404         BufferParams bparams = buffer.params;
405
406         minibuffer_char = getChar(pos);
407         minibuffer_font = getFontSettings(bparams, pos);
408         minibuffer_inset = 0;
409         if (minibuffer_char == Paragraph::META_INSET) {
410                 if (getInset(pos)) {
411                         minibuffer_inset = getInset(pos)->clone(buffer);
412                 } else {
413                         minibuffer_inset = 0;
414                         minibuffer_char = ' ';
415                         // This reflects what GetInset() does (ARRae)
416                 }
417         }
418 }
419
420
421 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
422 {
423         minibuffer_char = getChar(pos);
424         minibuffer_font = getFontSettings(bparams, pos);
425         minibuffer_inset = 0;
426         if (minibuffer_char == Paragraph::META_INSET) {
427                 if (getInset(pos)) {
428                         minibuffer_inset = getInset(pos);
429                         // This is a little hack since I want exactly
430                         // the inset, not just a clone. Otherwise
431                         // the inset would be deleted when calling Erase(pos)
432                         // find the entry
433                         InsetTable search_elem(pos, 0);
434                         InsetList::iterator it =
435                                 lower_bound(insetlist.begin(),
436                                             insetlist.end(),
437                                             search_elem, Pimpl::matchIT());
438                         if (it != insetlist.end() && it->pos == pos)
439                                 it->inset = 0;
440                 } else {
441                         minibuffer_inset = 0;
442                         minibuffer_char = ' ';
443                         // This reflects what GetInset() does (ARRae)
444                 }
445
446         }
447
448         // Erase(pos); now the caller is responsible for that.
449 }
450
451
452 bool Paragraph::insertFromMinibuffer(pos_type pos)
453 {
454         if (minibuffer_char == Paragraph::META_INSET) {
455                 if (!insetAllowed(minibuffer_inset->lyxCode())) {
456                         return false;
457                 }
458                 insertInset(pos, minibuffer_inset, minibuffer_font);
459         } else {
460                 LyXFont f = minibuffer_font;
461                 if (!checkInsertChar(f)) {
462                         return false;
463                 }
464                 insertChar(pos, minibuffer_char, f);
465         }
466         return true;
467 }
468
469 // end of minibuffer
470
471
472
473 void Paragraph::clear()
474 {
475         params().clear();
476         
477         layout = 0;
478         bibkey = 0;
479 }
480
481
482 void Paragraph::erase(pos_type pos)
483 {
484         pimpl_->erase(pos);
485 }
486
487
488 bool Paragraph::checkInsertChar(LyXFont & font)
489 {
490         if (pimpl_->inset_owner)
491                 return pimpl_->inset_owner->checkInsertChar(font);
492         return true;
493 }
494
495
496 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
497 {
498         LyXFont const f(LyXFont::ALL_INHERIT);
499         insertChar(pos, c, f);
500 }
501
502
503 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
504                            LyXFont const & font)
505 {
506         pimpl_->insertChar(pos, c, font);
507 }
508
509
510 void Paragraph::insertInset(pos_type pos, Inset * inset)
511 {
512         LyXFont const f(LyXFont::ALL_INHERIT);
513         insertInset(pos, inset, f);
514 }
515
516
517 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
518 {
519         pimpl_->insertInset(pos, inset, font);
520 }
521
522
523 bool Paragraph::insetAllowed(Inset::Code code)
524 {
525         //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
526         
527         if (pimpl_->inset_owner)
528                 return pimpl_->inset_owner->insetAllowed(code);
529         return true;
530 }
531
532
533 Inset * Paragraph::getInset(pos_type pos)
534 {
535         lyx::Assert(pos < size());
536
537         // Find the inset.
538         InsetTable search_inset(pos, 0);
539         InsetList::iterator it = lower_bound(insetlist.begin(),
540                                              insetlist.end(),
541                                              search_inset, Pimpl::matchIT());
542         if (it != insetlist.end() && it->pos == pos)
543                 return it->inset;
544
545         lyxerr << "ERROR (Paragraph::getInset): "
546                 "Inset does not exist: " << pos << endl;
547         //::raise(SIGSTOP);
548         
549         // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
550         // Did this commenting out introduce a bug? So far I have not
551         // see any, please enlighten me. (Lgb)
552         // My guess is that since the inset does not exist, we might
553         // as well replace it with a space to prevent craches. (Asger)
554         return 0;
555 }
556
557
558 Inset const * Paragraph::getInset(pos_type pos) const
559 {
560         lyx::Assert(pos < size());
561
562         // Find the inset.
563         InsetTable search_inset(pos, 0);
564         InsetList::const_iterator cit = lower_bound(insetlist.begin(),
565                                                     insetlist.end(),
566                                                     search_inset, Pimpl::matchIT());
567         if (cit != insetlist.end() && cit->pos == pos)
568                 return cit->inset;
569
570         lyxerr << "ERROR (Paragraph::GetInset): "
571                 "Inset does not exist: " << pos << endl;
572         //::raise(SIGSTOP);
573         //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
574         // Did this commenting out introduce a bug? So far I have not
575         // see any, please enlighten me. (Lgb)
576         // My guess is that since the inset does not exist, we might
577         // as well replace it with a space to prevent craches. (Asger)
578         return 0;
579 }
580
581
582 // Gets uninstantiated font setting at position.
583 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
584                                          pos_type pos) const
585 {
586         lyx::Assert(pos <= size());
587         
588         Pimpl::FontTable search_font(pos, LyXFont());
589         Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
590                                                    pimpl_->fontlist.end(),
591                                                    search_font, Pimpl::matchFT());
592         LyXFont retfont;
593         if (cit != pimpl_->fontlist.end()) {
594                 retfont = cit->font();
595         } else if (pos == size() && size()) {
596                 retfont = getFontSettings(bparams, pos - 1);
597         } else
598                 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
599 #ifdef INHERIT_LANGUAGE
600         if (retfont.language() == inherit_language)
601                 retfont.setLanguage(bparams.language);
602 #endif
603
604         return retfont;
605 }
606
607
608 // Gets uninstantiated font setting at position 0
609 LyXFont const Paragraph::getFirstFontSettings() const
610 {
611         if (size() > 0 && !pimpl_->fontlist.empty())
612                 return pimpl_->fontlist[0].font();
613         
614         return LyXFont(LyXFont::ALL_INHERIT);
615 }
616
617
618 // Gets the fully instantiated font at a given position in a paragraph
619 // This is basically the same function as LyXText::GetFont() in text2.C.
620 // The difference is that this one is used for generating the LaTeX file,
621 // and thus cosmetic "improvements" are disallowed: This has to deliver
622 // the true picture of the buffer. (Asger)
623 // If position is -1, we get the layout font of the paragraph.
624 // If position is -2, we get the font of the manual label of the paragraph.
625 LyXFont const Paragraph::getFont(BufferParams const & bparams,
626                                  pos_type pos) const
627 {
628         lyx::Assert(pos >= 0);
629         
630         LyXLayout const & layout =
631                 textclasslist.Style(bparams.textclass, 
632                                     getLayout());
633         pos_type main_body = 0;
634         if (layout.labeltype == LABEL_MANUAL)
635                 main_body = beginningOfMainBody();
636
637         LyXFont layoutfont;
638         if (pos < main_body)
639                 layoutfont = layout.labelfont;
640         else
641                 layoutfont = layout.font;
642         
643         LyXFont tmpfont = getFontSettings(bparams, pos);
644 #ifndef INHERIT_LANGUAGE
645         tmpfont.realize(layoutfont);
646 #else
647         tmpfont.realize(layoutfont, bparams.language);
648 #endif
649
650         return pimpl_->realizeFont(tmpfont, bparams);
651 }
652
653
654 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
655 {
656         LyXLayout const & layout =
657                 textclasslist.Style(bparams.textclass, getLayout());
658         
659         LyXFont tmpfont = layout.labelfont;
660         tmpfont.setLanguage(getParLanguage(bparams));
661
662         return pimpl_->realizeFont(tmpfont, bparams);
663 }
664
665
666 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
667 {
668         LyXLayout const & layout =
669                 textclasslist.Style(bparams.textclass, 
670                                     getLayout());
671
672         LyXFont tmpfont = layout.font;
673         tmpfont.setLanguage(getParLanguage(bparams));
674
675         return pimpl_->realizeFont(tmpfont, bparams);
676 }
677
678
679 /// Returns the height of the highest font in range
680 LyXFont::FONT_SIZE
681 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
682                               LyXFont::FONT_SIZE const def_size) const
683 {
684         if (pimpl_->fontlist.empty())
685                 return def_size;
686
687         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
688         Pimpl::FontTable end_search(endpos, LyXFont());
689         Pimpl::FontList::const_iterator end_it =
690                 lower_bound(pimpl_->fontlist.begin(),
691                             pimpl_->fontlist.end(),
692                             end_search, Pimpl::matchFT());
693         if (end_it != pimpl_->fontlist.end())
694                 ++end_it;
695
696         Pimpl::FontTable start_search(startpos, LyXFont());
697         Pimpl::FontList::const_iterator cit =
698                 lower_bound(pimpl_->fontlist.begin(),
699                             pimpl_->fontlist.end(),
700                             start_search, Pimpl::matchFT());
701         
702         for (; cit != end_it; ++cit) {
703                 LyXFont::FONT_SIZE size = cit->font().size();
704                 if (size == LyXFont::INHERIT_SIZE)
705                         size = def_size;
706                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
707                         maxsize = size;
708         }
709         return maxsize;
710 }
711
712
713 Paragraph::value_type
714 Paragraph::getUChar(BufferParams const & bparams,
715                        pos_type pos) const
716 {
717         value_type c = getChar(pos);
718         if (!lyxrc.rtl_support)
719                 return c;
720
721         value_type uc = c;
722         switch (c) {
723         case '(':
724                 uc = ')';
725                 break;
726         case ')':
727                 uc = '(';
728                 break;
729         case '[':
730                 uc = ']';
731                 break;
732         case ']':
733                 uc = '[';
734                 break;
735         case '{':
736                 uc = '}';
737                 break;
738         case '}':
739                 uc = '{';
740                 break;
741         case '<':
742                 uc = '>';
743                 break;
744         case '>':
745                 uc = '<';
746                 break;
747         }
748         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
749                 return uc;
750         else
751                 return c;
752 }
753
754
755 void Paragraph::setFont(pos_type pos,
756                            LyXFont const & font)
757 {
758         lyx::Assert(pos <= size());
759
760         // First, reduce font against layout/label font
761         // Update: The SetCharFont() routine in text2.C already
762         // reduces font, so we don't need to do that here. (Asger)
763         // No need to simplify this because it will disappear
764         // in a new kernel. (Asger)
765         // Next search font table
766
767         Pimpl::FontTable search_font(pos, LyXFont());
768         Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
769                                             pimpl_->fontlist.end(),
770                                             search_font, Pimpl::matchFT());
771         unsigned int i = it - pimpl_->fontlist.begin();
772         bool notfound = it == pimpl_->fontlist.end();
773
774         if (!notfound && pimpl_->fontlist[i].font() == font)
775                 return;
776
777         bool begin = pos == 0 || notfound ||
778                 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
779         // Is position pos is a beginning of a font block?
780         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
781         // Is position pos is the end of a font block?
782         if (begin && end) { // A single char block
783                 if (i + 1 < pimpl_->fontlist.size() &&
784                     pimpl_->fontlist[i + 1].font() == font) {
785                         // Merge the singleton block with the next block
786                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
787                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
788                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
789                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
790                         // Merge the singleton block with the previous block
791                         pimpl_->fontlist[i - 1].pos(pos);
792                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
793                 } else
794                         pimpl_->fontlist[i].font(font);
795         } else if (begin) {
796                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
797                         pimpl_->fontlist[i - 1].pos(pos);
798                 else
799                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
800                                         Pimpl::FontTable(pos, font));
801         } else if (end) {
802                 pimpl_->fontlist[i].pos(pos - 1);
803                 if (!(i + 1 < pimpl_->fontlist.size() &&
804                       pimpl_->fontlist[i + 1].font() == font))
805                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
806                                         Pimpl::FontTable(pos, font));
807         } else { // The general case. The block is splitted into 3 blocks
808                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i, 
809                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
810                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
811                                 Pimpl::FontTable(pos, font));
812         }
813 }
814
815
816
817 void Paragraph::next(Paragraph * p)
818 {
819         next_ = p;
820 }
821
822
823 // This function is able to hide closed footnotes.
824 Paragraph * Paragraph::next()
825 {
826         return next_;
827 }
828
829
830 Paragraph const * Paragraph::next() const
831 {
832         return next_;
833 }
834
835
836 void Paragraph::previous(Paragraph * p)
837 {
838         previous_ = p;
839 }
840
841
842 // This function is able to hide closed footnotes.
843 Paragraph * Paragraph::previous()
844 {
845         return previous_;
846 }
847
848
849 // This function is able to hide closed footnotes.
850 Paragraph const * Paragraph::previous() const
851 {
852         return previous_;
853 }
854
855
856 void Paragraph::breakParagraph(BufferParams const & bparams,
857                                   pos_type pos,
858                                   int flag)
859 {
860         // create a new paragraph
861         Paragraph * tmp = new Paragraph(this);
862         // remember to set the inset_owner
863         tmp->setInsetOwner(inInset());
864         
865         // this is an idea for a more userfriendly layout handling, I will
866         // see what the users say
867         
868         // layout stays the same with latex-environments
869         if (flag) {
870                 tmp->setOnlyLayout(layout);
871                 tmp->setLabelWidthString(params().labelWidthString());
872         }
873         
874         if (size() > pos || !size() || flag == 2) {
875                 tmp->setOnlyLayout(layout);
876                 tmp->params().align(params().align());
877                 tmp->setLabelWidthString(params().labelWidthString());
878                 
879                 tmp->params().lineBottom(params().lineBottom());
880                 params().lineBottom(false);
881                 tmp->params().pagebreakBottom(params().pagebreakBottom());
882                 params().pagebreakBottom(false);
883                 tmp->params().spaceBottom(params().spaceBottom());
884                 params().spaceBottom(VSpace(VSpace::NONE));
885                 
886                 tmp->params().depth(params().depth());
887                 tmp->params().noindent(params().noindent());
888                 
889                 // copy everything behind the break-position
890                 // to the new paragraph
891                 pos_type pos_end = pimpl_->size() - 1;
892                 pos_type i = pos;
893                 pos_type j = pos;
894                 for (; i <= pos_end; ++i) {
895                         cutIntoMinibuffer(bparams, i);
896                         if (tmp->insertFromMinibuffer(j - pos))
897                                 ++j;
898                 }
899                 for (i = pos_end; i >= pos; --i) {
900                         erase(i);
901                 }
902         }
903         
904         // just an idea of me
905         if (!pos) {
906                 tmp->params().lineTop(params().lineTop());
907                 tmp->params().pagebreakTop(params().pagebreakTop());
908                 tmp->params().spaceTop(params().spaceTop());
909                 tmp->bibkey = bibkey;
910                 clear();
911                 // layout stays the same with latex-environments
912                 if (flag) {
913                         setOnlyLayout(tmp->layout);
914                         setLabelWidthString(tmp->params().labelWidthString());
915                         params().depth(tmp->params().depth());
916                 }
917         }
918 }
919         
920
921 void Paragraph::makeSameLayout(Paragraph const * par)
922 {
923         layout = par->layout;
924         // move to pimpl?
925         params() = par->params();
926 }
927
928
929 int Paragraph::stripLeadingSpaces(LyXTextClassList::size_type tclass) 
930 {
931         if (textclasslist.Style(tclass, getLayout()).free_spacing ||
932                 isFreeSpacing())
933         {
934                 return 0;
935         }
936         
937         int i = 0;
938         while (size()
939                && (isNewline(0) || isLineSeparator(0))){
940                 erase(0);
941                 ++i;
942         }
943
944         return i;
945 }
946
947
948 bool Paragraph::hasSameLayout(Paragraph const * par) const
949 {
950         return 
951                 par->layout == layout &&
952                 params().sameLayout(par->params());
953 }
954
955
956 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
957                                            pos_type pos)
958 {
959         // create a new paragraph
960         Paragraph * tmp = new Paragraph(this);
961         tmp->makeSameLayout(this);
962
963         // When can pos > Last()?
964         // I guess pos == Last() is possible.
965         if (size() > pos) {
966                 // copy everything behind the break-position to the new
967                 // paragraph
968                 pos_type pos_end = pimpl_->size() - 1;
969
970                 //pos_type i = pos;
971                 //pos_type j = pos;
972                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
973                         cutIntoMinibuffer(bparams, i);
974                         if (tmp->insertFromMinibuffer(j - pos))
975                                 ++j;
976                 }
977                 
978                 for (pos_type k = pos_end; k >= pos; --k) {
979                         erase(k);
980                 }
981         }
982 }
983
984
985 // Be carefull, this does not make any check at all.
986 // This method has wrong name, it combined this par with the next par.
987 // In that sense it is the reverse of break paragraph. (Lgb)
988 void Paragraph::pasteParagraph(BufferParams const & bparams)
989 {
990         // copy the next paragraph to this one
991         Paragraph * the_next = next();
992
993         // first the DTP-stuff
994         params().lineBottom(the_next->params().lineBottom());
995         params().spaceBottom(the_next->params().spaceBottom());
996         params().pagebreakBottom(the_next->params().pagebreakBottom());
997
998         pos_type pos_end = the_next->pimpl_->size() - 1;
999         pos_type pos_insert = size();
1000
1001         // ok, now copy the paragraph
1002         for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
1003                 the_next->cutIntoMinibuffer(bparams, i);
1004                 if (insertFromMinibuffer(pos_insert + j))
1005                         ++j;
1006         }
1007
1008         // delete the next paragraph
1009         Paragraph * ppar = the_next->previous_;
1010         Paragraph * npar = the_next->next_;
1011         delete the_next;
1012         ppar->next(npar);
1013 }
1014
1015
1016 int Paragraph::getEndLabel(BufferParams const & bparams) const
1017 {
1018         Paragraph const * par = this;
1019         depth_type par_depth = getDepth();
1020         while (par) {
1021                 layout_type layout = par->getLayout();
1022                 int const endlabeltype =
1023                         textclasslist.Style(bparams.textclass,
1024                                             layout).endlabeltype;
1025                 if (endlabeltype != END_LABEL_NO_LABEL) {
1026                         if (!next_)
1027                                 return endlabeltype;
1028
1029                         depth_type const next_depth = next_->getDepth();
1030                         if (par_depth > next_depth ||
1031                             (par_depth == next_depth
1032                              && layout != next_->getLayout()))
1033                                 return endlabeltype;
1034                         break;
1035                 }
1036                 if (par_depth == 0)
1037                         break;
1038                 par = par->outerHook();
1039                 if (par)
1040                         par_depth = par->getDepth();
1041         }
1042         return END_LABEL_NO_LABEL;
1043 }
1044
1045
1046 Paragraph::depth_type Paragraph::getDepth() const
1047 {
1048         return params().depth();
1049 }
1050
1051
1052 char Paragraph::getAlign() const
1053 {
1054         return params().align();
1055 }
1056
1057
1058 string const & Paragraph::getLabelstring() const
1059 {
1060         return params().labelString();
1061 }
1062
1063
1064 int Paragraph::getFirstCounter(int i) const
1065 {
1066         return pimpl_->counter_[i];
1067 }
1068
1069
1070 // the next two functions are for the manual labels
1071 string const Paragraph::getLabelWidthString() const
1072 {
1073         if (!params().labelWidthString().empty())
1074                 return params().labelWidthString();
1075         else
1076                 return _("Senseless with this layout!");
1077 }
1078
1079
1080 void Paragraph::setLabelWidthString(string const & s)
1081 {
1082         params().labelWidthString(s);
1083 }
1084
1085
1086 void Paragraph::setOnlyLayout(layout_type new_layout)
1087 {
1088         layout = new_layout;
1089 }
1090
1091
1092 void Paragraph::setLayout(layout_type new_layout)
1093 {
1094         layout = new_layout;
1095         params().labelWidthString(string());
1096         params().align(LYX_ALIGN_LAYOUT);
1097         params().spaceTop(VSpace(VSpace::NONE));
1098         params().spaceBottom(VSpace(VSpace::NONE));
1099         params().spacing(Spacing(Spacing::Default));
1100 }
1101
1102
1103 // if the layout of a paragraph contains a manual label, the beginning of the 
1104 // main body is the beginning of the second word. This is what the par-
1105 // function returns. If the layout does not contain a label, the main
1106 // body always starts with position 0. This differentiation is necessary,
1107 // because there cannot be a newline or a blank <= the beginning of the 
1108 // main body in TeX.
1109
1110 int Paragraph::beginningOfMainBody() const
1111 {
1112         // Unroll the first two cycles of the loop
1113         // and remember the previous character to
1114         // remove unnecessary GetChar() calls
1115         pos_type i = 0;
1116         if (i < size()
1117             && getChar(i) != Paragraph::META_NEWLINE) {
1118                 ++i;
1119                 char previous_char = 0;
1120                 char temp = 0; 
1121                 if (i < size()
1122                     && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1123                         // Yes, this  ^ is supposed to be "= " not "=="
1124                         ++i;
1125                         while (i < size()
1126                                && previous_char != ' '
1127                                && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1128                                 ++i;
1129                                 previous_char = temp;
1130                         }
1131                 }
1132         }
1133
1134         return i;
1135 }
1136
1137
1138 Paragraph * Paragraph::depthHook(depth_type depth)
1139 {
1140         Paragraph * newpar = this;
1141
1142         do {
1143                 newpar = newpar->previous();
1144         } while (newpar && newpar->getDepth() > depth);
1145
1146         if (!newpar) {
1147                 if (previous() || getDepth())
1148                         lyxerr << "ERROR (Paragraph::DepthHook): "
1149                                 "no hook." << endl;
1150                 newpar = this;
1151         }
1152
1153         return newpar;
1154 }
1155
1156
1157 Paragraph const * Paragraph::depthHook(depth_type depth) const
1158 {
1159         Paragraph const * newpar = this;
1160
1161         do {
1162                 newpar = newpar->previous();
1163         } while (newpar && newpar->getDepth() > depth);
1164
1165         if (!newpar) {
1166                 if (previous() || getDepth())
1167                         lyxerr << "ERROR (Paragraph::DepthHook): "
1168                                 "no hook." << endl;
1169                 newpar = this;
1170         }
1171
1172         return newpar;
1173 }
1174
1175 Paragraph * Paragraph::outerHook()
1176 {
1177         if(!getDepth())
1178                 return 0;
1179         return depthHook(depth_type(getDepth() - 1));
1180 }
1181
1182 Paragraph const * Paragraph::outerHook() const
1183 {
1184         if(!getDepth())
1185                 return 0;
1186         return depthHook(depth_type(getDepth() - 1));
1187 }
1188
1189 int Paragraph::autoDeleteInsets()
1190 {
1191         int count = 0;
1192         InsetList::size_type index = 0;
1193         while (index < insetlist.size()) {
1194                 if (insetlist[index].inset && insetlist[index].inset->autoDelete()) {
1195                         erase(insetlist[index].pos); 
1196                         // Erase() calls to insetlist.erase(&insetlist[index])
1197                         // so index shouldn't be increased.
1198                         ++count;
1199                 } else
1200                         ++index;
1201         }
1202         return count;
1203 }
1204
1205
1206 Paragraph::inset_iterator
1207 Paragraph::InsetIterator(pos_type pos)
1208 {
1209         InsetTable search_inset(pos, 0);
1210         InsetList::iterator it = lower_bound(insetlist.begin(),
1211                                              insetlist.end(),
1212                                              search_inset, Pimpl::matchIT());
1213         return inset_iterator(it);
1214 }
1215
1216
1217 // returns -1 if inset not found
1218 int Paragraph::getPositionOfInset(Inset * inset) const
1219 {
1220         // Find the entry.
1221         for (InsetList::const_iterator cit = insetlist.begin();
1222              cit != insetlist.end(); ++cit) {
1223                 if (cit->inset == inset) {
1224                         return cit->pos;
1225                 }
1226         }
1227         if (inset == bibkey)
1228                 return 0;
1229
1230         return -1;
1231 }
1232
1233
1234 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1235                                        BufferParams const & bparams,
1236                                        ostream & os, TexRow & texrow,
1237                                        bool moving_arg)
1238 {
1239         lyxerr[Debug::LATEX] << "TeXOnePar...     " << this << endl;
1240         LyXLayout const & style =
1241                 textclasslist.Style(bparams.textclass,
1242                                     layout);
1243
1244         bool further_blank_line = false;
1245
1246         if (params().startOfAppendix()) {
1247                 os << "\\appendix\n";
1248                 texrow.newline();
1249         }
1250
1251         if (!params().spacing().isDefault()
1252             && (!previous() || !previous()->hasSameLayout(this))) {
1253                 os << params().spacing().writeEnvirBegin() << "\n";
1254                 texrow.newline();
1255         }
1256         
1257         if (tex_code_break_column && style.isCommand()){
1258                 os << '\n';
1259                 texrow.newline();
1260         }
1261
1262         if (params().pagebreakTop()) {
1263                 os << "\\newpage";
1264                 further_blank_line = true;
1265         }
1266         if (params().spaceTop().kind() != VSpace::NONE) {
1267                 os << params().spaceTop().asLatexCommand(bparams);
1268                 further_blank_line = true;
1269         }
1270
1271         if (params().lineTop()) {
1272                 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1273                    << "\\vspace{-1\\parskip}";
1274                 further_blank_line = true;
1275         }
1276
1277         if (further_blank_line){
1278                 os << '\n';
1279                 texrow.newline();
1280         }
1281
1282         Language const * language = getParLanguage(bparams);
1283         Language const * doc_language = bparams.language;
1284         Language const * previous_language = previous()
1285                 ? previous()->getParLanguage(bparams) : doc_language;
1286
1287         if (language->babel() != previous_language->babel()
1288             // check if we already put language command in TeXEnvironment()
1289             && !(textclasslist.Style(bparams.textclass, layout).isEnvironment()
1290                  && (!previous() || previous()->layout != layout ||
1291                      previous()->params().depth() != params().depth()))) {
1292
1293                 if (!lyxrc.language_command_end.empty() &&
1294                     previous_language->babel() != doc_language->babel()) {
1295                         os << subst(lyxrc.language_command_end, "$$lang",
1296                                     previous_language->babel())
1297                            << endl;
1298                         texrow.newline();
1299                 }
1300
1301                 if (lyxrc.language_command_end.empty() ||
1302                     language->babel() != doc_language->babel()) {
1303                         os << subst(lyxrc.language_command_begin, "$$lang",
1304                                     language->babel())
1305                            << endl;
1306                         texrow.newline();
1307                 }
1308         }
1309
1310         if (bparams.inputenc == "auto" &&
1311             language->encoding() != previous_language->encoding()) {
1312                 os << "\\inputencoding{"
1313                    << language->encoding()->LatexName()
1314                    << "}" << endl;
1315                 texrow.newline();
1316         }
1317
1318         switch (style.latextype) {
1319         case LATEX_COMMAND:
1320                 os << '\\'
1321                    << style.latexname()
1322                    << style.latexparam();
1323                 break;
1324         case LATEX_ITEM_ENVIRONMENT:
1325                 if (bibkey) {
1326                         bibkey->latex(buf, os, false, false);
1327                 } else
1328                         os << "\\item ";
1329                 break;
1330         case LATEX_LIST_ENVIRONMENT:
1331                 os << "\\item ";
1332                 break;
1333         default:
1334                 break;
1335         }
1336
1337         bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1338  
1339         // Make sure that \\par is done with the font of the last
1340         // character if this has another size as the default.
1341         // This is necessary because LaTeX (and LyX on the screen)
1342         // calculates the space between the baselines according
1343         // to this font. (Matthias)
1344         //
1345         // Is this really needed ? (Dekel)
1346         // We do not need to use to change the font for the last paragraph
1347         // or for a command.
1348         LyXFont const font =
1349                 (size() == 0
1350                  ? getLayoutFont(bparams)
1351                  : getFont(bparams, size() - 1));
1352
1353         bool is_command = textclasslist.Style(bparams.textclass,
1354                                               getLayout()).isCommand();
1355         if (style.resfont.size() != font.size() && next_ && !is_command) {
1356                 if (!need_par)
1357                         os << "{";
1358                 os << "\\" << font.latexSize() << " \\par}";
1359         } else if (need_par) {
1360                 os << "\\par}";
1361         } else if (is_command)
1362                 os << "}";
1363
1364         switch (style.latextype) {
1365         case LATEX_ITEM_ENVIRONMENT:
1366         case LATEX_LIST_ENVIRONMENT:
1367                 if (next_ && (params().depth() < next_->params().depth())) {
1368                         os << '\n';
1369                         texrow.newline();
1370                 }
1371                 break;
1372         case LATEX_ENVIRONMENT:
1373                 // if its the last paragraph of the current environment
1374                 // skip it otherwise fall through
1375                 if (next_
1376                     && (next_->layout != layout
1377                         || next_->params().depth() != params().depth()))
1378                         break;
1379                 // fall through possible
1380         default:
1381                 // we don't need it for the last paragraph!!!
1382                 if (next_) {
1383                         os << '\n';
1384                         texrow.newline();
1385                 }
1386         }
1387         
1388         further_blank_line = false;
1389         if (params().lineBottom()) {
1390                 os << "\\lyxline{\\" << font.latexSize() << '}';
1391                 further_blank_line = true;
1392         }
1393
1394         if (params().spaceBottom().kind() != VSpace::NONE) {
1395                 os << params().spaceBottom().asLatexCommand(bparams);
1396                 further_blank_line = true;
1397         }
1398
1399         if (params().pagebreakBottom()) {
1400                 os << "\\newpage";
1401                 further_blank_line = true;
1402         }
1403
1404         if (further_blank_line){
1405                 os << '\n';
1406                 texrow.newline();
1407         }
1408
1409         if (!params().spacing().isDefault()
1410             && (!next_ || !next_->hasSameLayout(this))) {
1411                 os << params().spacing().writeEnvirEnd() << "\n";
1412                 texrow.newline();
1413         }
1414         
1415         // we don't need it for the last paragraph!!!
1416         if (next_) {
1417                 os << '\n';
1418                 texrow.newline();
1419         } else {
1420                 // Since \selectlanguage write the language to the aux file,
1421                 // we need to reset the language at the end of footnote or
1422                 // float.
1423
1424                 if (language->babel() != doc_language->babel()) {
1425                         if (lyxrc.language_command_end.empty())
1426                                 os << subst(lyxrc.language_command_begin,
1427                                             "$$lang",
1428                                             doc_language->babel())
1429                                    << endl;
1430                         else
1431                                 os << subst(lyxrc.language_command_end,
1432                                             "$$lang",
1433                                             language->babel())
1434                                    << endl;
1435                         texrow.newline();
1436                 }
1437         }
1438
1439         lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1440         return next_;
1441 }
1442
1443
1444 // This one spits out the text of the paragraph
1445 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1446                                    BufferParams const & bparams,
1447                                    ostream & os, TexRow & texrow,
1448                                    bool moving_arg)
1449 {
1450         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
1451
1452         bool return_value = false;
1453
1454         LyXLayout const & style =
1455                 textclasslist.Style(bparams.textclass,
1456                                     getLayout());
1457         LyXFont basefont;
1458
1459         // Maybe we have to create a optional argument.
1460         pos_type main_body;
1461         if (style.labeltype != LABEL_MANUAL)
1462                 main_body = 0;
1463         else
1464                 main_body = beginningOfMainBody();
1465
1466         int column = 0;
1467
1468         if (main_body > 0) {
1469                 os << '[';
1470                 ++column;
1471                 basefont = getLabelFont(bparams);
1472         } else {
1473                 basefont = getLayoutFont(bparams);
1474         }
1475
1476         if (main_body >= 0
1477             && !pimpl_->size()) {
1478                 if (style.isCommand()) {
1479                         os << '{';
1480                         ++column;
1481                 }
1482         }
1483
1484         moving_arg |= style.needprotect;
1485  
1486         // Which font is currently active?
1487         LyXFont running_font(basefont);
1488         // Do we have an open font change?
1489         bool open_font = false;
1490
1491         texrow.start(this, 0);
1492
1493         for (pos_type i = 0; i < size(); ++i) {
1494                 ++column;
1495                 // First char in paragraph or after label?
1496                 if (i == main_body) {
1497                         if (main_body > 0) {
1498                                 if (open_font) {
1499                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
1500                                         open_font = false;
1501                                 }
1502                                 basefont = getLayoutFont(bparams);
1503                                 running_font = basefont;
1504                                 os << ']';
1505                                 ++column;
1506                         }
1507                         if (style.isCommand()) {
1508                                 os << '{';
1509                                 ++column;
1510                         }
1511
1512                         if (params().noindent()) {
1513                                 os << "\\noindent ";
1514                                 column += 10;
1515                         }
1516                         switch (params().align()) {
1517                         case LYX_ALIGN_NONE:
1518                         case LYX_ALIGN_BLOCK:
1519                         case LYX_ALIGN_LAYOUT:
1520                         case LYX_ALIGN_SPECIAL:
1521                                 break;
1522                         case LYX_ALIGN_LEFT:
1523                                 if (getParLanguage(bparams)->babel() != "hebrew") {
1524                                         os << "\\begin{flushleft}";
1525                                         column += 17;
1526                                 } else {
1527                                         os << "\\begin{flushright}";
1528                                         column += 18;
1529                                 }
1530                                 break;
1531                         case LYX_ALIGN_RIGHT:
1532                                 if (getParLanguage(bparams)->babel() != "hebrew") {
1533                                         os << "\\begin{flushright}";
1534                                         column += 18;
1535                                 } else {
1536                                         os << "\\begin{flushleft}";
1537                                         column += 17;
1538                                 }
1539                                 break;
1540                         case LYX_ALIGN_CENTER:
1541                                 os << "\\begin{center}";
1542                                 column += 14;
1543                                 break;
1544                         }        
1545                 }
1546
1547                 value_type c = getChar(i);
1548
1549                 // Fully instantiated font
1550                 LyXFont font = getFont(bparams, i);
1551
1552                 LyXFont const last_font = running_font;
1553
1554                 // Spaces at end of font change are simulated to be
1555                 // outside font change, i.e. we write "\textXX{text} "
1556                 // rather than "\textXX{text }". (Asger)
1557                 if (open_font && c == ' ' && i <= size() - 2) {
1558                         LyXFont const next_font = getFont(bparams, i + 1);
1559                         if (next_font != running_font
1560                             && next_font != font) {
1561                                 font = next_font;
1562                         }
1563                 }
1564                 
1565                 // We end font definition before blanks
1566                 if (font != running_font && open_font) {
1567                         column += running_font.latexWriteEndChanges(os,
1568                                                                     basefont,
1569                                                                     (i == main_body-1) ? basefont : font);
1570                         running_font = basefont;
1571                         open_font = false;
1572                 }
1573
1574                 // Blanks are printed before start of fontswitch
1575                 if (c == ' ') {
1576                         // Do not print the separation of the optional argument
1577                         if (i != main_body - 1) {
1578                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1579                                                        column, font, style);
1580                         }
1581                 }
1582
1583                 // Do we need to change font?
1584                 if (font != running_font && i != main_body - 1) {
1585                         column += font.latexWriteStartChanges(os, basefont,
1586                                                               last_font);
1587                         running_font = font;
1588                         open_font = true;
1589                 }
1590
1591                 if (c == Paragraph::META_NEWLINE) {
1592                         // newlines are handled differently here than
1593                         // the default in SimpleTeXSpecialChars().
1594                         if (!style.newline_allowed) {
1595                                 os << '\n';
1596                         } else {
1597                                 if (open_font) {
1598                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
1599                                         open_font = false;
1600                                 }
1601                                 basefont = getLayoutFont(bparams);
1602                                 running_font = basefont;
1603                                 if (font.family() == 
1604                                     LyXFont::TYPEWRITER_FAMILY) {
1605                                         os << "~";
1606                                 }
1607                                 if (moving_arg)
1608                                         os << "\\protect ";
1609                                 os << "\\\\\n";
1610                         }
1611                         texrow.newline();
1612                         texrow.start(this, i + 1);
1613                         column = 0;
1614                 } else {
1615                         pimpl_->simpleTeXSpecialChars(buf, bparams,
1616                                                       os, texrow, moving_arg,
1617                                                       font, running_font, 
1618                                                       basefont, open_font, 
1619                                                       style, i, column, c);
1620                 }
1621         }
1622
1623         // If we have an open font definition, we have to close it
1624         if (open_font) {
1625 #ifdef FIXED_LANGUAGE_END_DETECTION
1626                 if (next_) {
1627                         running_font
1628                                 .latexWriteEndChanges(os, basefont,
1629                                                       next_->getFont(bparams,
1630                                                       0));
1631                 } else {
1632                         running_font.latexWriteEndChanges(os, basefont,
1633                                                           basefont);
1634                 }
1635 #else
1636 #ifdef WITH_WARNINGS
1637 //#warning For now we ALWAYS have to close the foreign font settings if they are
1638 //#warning there as we start another \selectlanguage with the next paragraph if
1639 //#warning we are in need of this. This should be fixed sometime (Jug)
1640 #endif
1641                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1642 #endif
1643         }
1644
1645         // Needed if there is an optional argument but no contents.
1646         if (main_body > 0 && main_body == size()) {
1647                 os << "]~";
1648                 return_value = false;
1649         }
1650
1651         switch (params().align()) {
1652         case LYX_ALIGN_NONE:
1653         case LYX_ALIGN_BLOCK:
1654         case LYX_ALIGN_LAYOUT:
1655         case LYX_ALIGN_SPECIAL:
1656                 break;
1657         case LYX_ALIGN_LEFT:
1658                 if (getParLanguage(bparams)->babel() != "hebrew") {
1659                         os << "\\end{flushleft}";
1660                         column+= 15;
1661                 } else {
1662                         os << "\\end{flushright}";
1663                         column+= 16;
1664                 }
1665                 break;
1666         case LYX_ALIGN_RIGHT:
1667                 if (getParLanguage(bparams)->babel() != "hebrew") {
1668                         os << "\\end{flushright}";
1669                         column+= 16;
1670                 } else {
1671                         os << "\\end{flushleft}";
1672                         column+= 15;
1673                 }
1674                 break;
1675         case LYX_ALIGN_CENTER:
1676                 os << "\\end{center}";
1677                 column+= 12;
1678                 break;
1679         }        
1680
1681         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1682         return return_value;
1683 }
1684
1685
1686 bool Paragraph::sgmlConvertChar(char c, string & sgml_string)
1687 {
1688         bool retval = false;
1689         switch (c) {
1690         case Paragraph::META_HFILL:
1691                 sgml_string.erase();
1692                 break;
1693         case Paragraph::META_NEWLINE:
1694                 sgml_string = '\n';
1695                 break;
1696         case '&': 
1697                 sgml_string = "&amp;";
1698                 break;
1699         case '<': 
1700                 sgml_string = "&lt;"; 
1701                 break;
1702         case '>':
1703                 sgml_string = "&gt;"; 
1704                 break;
1705         case '$': 
1706                 sgml_string = "&dollar;"; 
1707                 break;
1708         case '#': 
1709                 sgml_string = "&num;";
1710                 break;
1711         case '%': 
1712                 sgml_string = "&percnt;";
1713                 break;
1714         case '[': 
1715                 sgml_string = "&lsqb;";
1716                 break;
1717         case ']': 
1718                 sgml_string = "&rsqb;";
1719                 break;
1720         case '{': 
1721                 sgml_string = "&lcub;";
1722                 break;
1723         case '}': 
1724                 sgml_string = "&rcub;";
1725                 break;
1726         case '~': 
1727                 sgml_string = "&tilde;";
1728                 break;
1729         case '"': 
1730                 sgml_string = "&quot;";
1731                 break;
1732         case '\\': 
1733                 sgml_string = "&bsol;";
1734                 break;
1735         case ' ':
1736                 retval = true;
1737                 sgml_string = ' ';
1738                 break;
1739         case '\0': // Ignore :-)
1740                 sgml_string.erase();
1741                 break;
1742         default:
1743                 sgml_string = c;
1744                 break;
1745         }
1746         return retval;
1747 }
1748
1749
1750 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1751                                             BufferParams const & bparams,
1752                                             ostream & os, TexRow & texrow)
1753 {
1754         lyxerr[Debug::LATEX] << "TeXEnvironment...     " << this << endl;
1755
1756         LyXLayout const & style =
1757                 textclasslist.Style(bparams.textclass,
1758                                     layout);
1759
1760         Language const * language = getParLanguage(bparams);
1761         Language const * doc_language = bparams.language;
1762         Language const * previous_language = previous_
1763                 ? previous_->getParLanguage(bparams) : doc_language;
1764         if (language->babel() != previous_language->babel()) {
1765
1766                 if (!lyxrc.language_command_end.empty() &&
1767                     previous_language->babel() != doc_language->babel()) {
1768                         os << subst(lyxrc.language_command_end, "$$lang",
1769                                     previous_language->babel())
1770                            << endl;
1771                         texrow.newline();
1772                 }
1773
1774                 if (lyxrc.language_command_end.empty() ||
1775                     language->babel() != doc_language->babel()) {
1776                         os << subst(lyxrc.language_command_begin, "$$lang",
1777                                     language->babel())
1778                            << endl;
1779                         texrow.newline();
1780                 }
1781         }
1782
1783         if (style.isEnvironment()) {
1784                 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1785                         os << "\\begin{" << style.latexname() << "}{"
1786                            << params().labelWidthString() << "}\n";
1787                 } else if (style.labeltype == LABEL_BIBLIO) {
1788                         // ale970405
1789                         os << "\\begin{" << style.latexname() << "}{"
1790                            <<  bibitemWidest(buf)
1791                            << "}\n";
1792                 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1793                         os << "\\begin{" << style.latexname() << '}'
1794                            << style.latexparam() << '\n';
1795                 } else 
1796                         os << "\\begin{" << style.latexname() << '}'
1797                            << style.latexparam() << '\n';
1798                 texrow.newline();
1799         }
1800         Paragraph * par = this;
1801         do {
1802                 par = par->TeXOnePar(buf, bparams,
1803                                      os, texrow, false);
1804
1805                 if (par && par->params().depth() > params().depth()) {
1806                         if (textclasslist.Style(bparams.textclass,
1807                                                 par->layout).isParagraph()
1808                             // Thinko!
1809                             // How to handle this? (Lgb)
1810                             //&& !suffixIs(os, "\n\n")
1811                                 ) {
1812                                 // There should be at least one '\n' already
1813                                 // but we need there to be two for Standard 
1814                                 // paragraphs that are depth-increment'ed to be
1815                                 // output correctly.  However, tables can
1816                                 // also be paragraphs so don't adjust them.
1817                                 // ARRae
1818                                 // Thinkee:
1819                                 // Will it ever harm to have one '\n' too
1820                                 // many? i.e. that we sometimes will have
1821                                 // three in a row. (Lgb)
1822                                 os << '\n';
1823                                 texrow.newline();
1824                         }
1825                         par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1826                 }
1827         } while (par
1828                  && par->layout == layout
1829                  && par->params().depth() == params().depth());
1830  
1831         if (style.isEnvironment()) {
1832                 os << "\\end{" << style.latexname() << "}\n";
1833         }
1834
1835         lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1836         return par;  // ale970302
1837 }
1838
1839
1840 bool Paragraph::isHfill(pos_type pos) const
1841 {
1842         return IsHfillChar(getChar(pos));
1843 }
1844
1845
1846 bool Paragraph::isInset(pos_type pos) const
1847 {
1848         return IsInsetChar(getChar(pos));
1849 }
1850
1851
1852 bool Paragraph::isNewline(pos_type pos) const
1853 {
1854         return pos >= 0 && IsNewlineChar(getChar(pos));
1855 }
1856
1857
1858 bool Paragraph::isSeparator(pos_type pos) const
1859 {
1860         return IsSeparatorChar(getChar(pos));
1861 }
1862
1863
1864 bool Paragraph::isLineSeparator(pos_type pos) const
1865 {
1866         return IsLineSeparatorChar(getChar(pos));
1867 }
1868
1869
1870 bool Paragraph::isKomma(pos_type pos) const
1871 {
1872         return IsKommaChar(getChar(pos));
1873 }
1874
1875
1876 /// Used by the spellchecker
1877 bool Paragraph::isLetter(pos_type pos) const
1878 {
1879         value_type const c = getChar(pos);
1880         if (IsLetterChar(c))
1881                 return true;
1882         if (isInset(pos)) 
1883                 return getInset(pos)->isLetter();
1884         // We want to pass the ' and escape chars to ispell
1885         string const extra = lyxrc.isp_esc_chars + '\'';
1886         return contains(extra, c);
1887 }
1888  
1889  
1890 bool Paragraph::isWord(pos_type pos ) const
1891 {
1892         return IsWordChar(getChar(pos)) ;
1893 }
1894
1895
1896 Language const *
1897 Paragraph::getParLanguage(BufferParams const & bparams) const 
1898 {
1899         if (size() > 0) {
1900 #ifndef INHERIT_LANGUAGE
1901                 return getFirstFontSettings().language();
1902 #else
1903                 Language const * lang = getFirstFontSettings().language();
1904 #warning We should make this somewhat better, any ideas? (Jug)
1905                 if (lang == inherit_language || lang == ignore_language)
1906                         lang = bparams.language;
1907                 return lang;
1908 #endif
1909         } else if (previous_)
1910                 return previous_->getParLanguage(bparams);
1911         else
1912                 return bparams.language;
1913 }
1914
1915
1916 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1917 {
1918         return lyxrc.rtl_support
1919                 && getParLanguage(bparams)->RightToLeft();
1920 }
1921
1922
1923 void Paragraph::changeLanguage(BufferParams const & bparams,
1924                                   Language const * from, Language const * to)
1925 {
1926         for (pos_type i = 0; i < size(); ++i) {
1927                 LyXFont font = getFontSettings(bparams, i);
1928                 if (font.language() == from) {
1929                         font.setLanguage(to);
1930                         setFont(i, font);
1931                 }
1932         }
1933 }
1934
1935
1936 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1937 {
1938         Language const * doc_language = bparams.language;
1939         for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1940              cit != pimpl_->fontlist.end(); ++cit)
1941                 if (cit->font().language() != ignore_language &&
1942                     cit->font().language() != latex_language &&
1943 #ifdef INHERIT_LANGUAGE
1944                         cit->font().language() != inherit_language &&
1945 #endif
1946                         cit->font().language() != doc_language)
1947                         return true;
1948         return false;
1949 }
1950
1951
1952 // Convert the paragraph to a string.
1953 // Used for building the table of contents
1954 string const Paragraph::asString(Buffer const * buffer, bool label)
1955 {
1956         BufferParams const & bparams = buffer->params;
1957         string s;
1958         if (label && !params().labelString().empty())
1959                 s += params().labelString() + ' ';
1960         string::size_type const len = s.size();
1961
1962         for (pos_type i = 0; i < size(); ++i) {
1963                 value_type c = getChar(i);
1964                 if (IsPrintable(c))
1965                         s += c;
1966                 else if (c == META_INSET &&
1967                          getInset(i)->lyxCode() == Inset::MATH_CODE) {
1968                         ostringstream ost;
1969                         getInset(i)->ascii(buffer, ost);
1970                         s += subst(ost.str().c_str(),'\n',' ');
1971                 }
1972         }
1973
1974         if (isRightToLeftPar(bparams))
1975                 reverse(s.begin() + len,s.end());
1976
1977         return s;
1978 }
1979
1980
1981 string const Paragraph::asString(Buffer const * buffer, 
1982                                  pos_type beg, pos_type end, bool label)
1983 {
1984         ostringstream ost;
1985
1986         if (beg == 0 && label && !params().labelString().empty())
1987                 ost << params().labelString() << ' ';
1988
1989         for (pos_type i = beg; i < end; ++i) {
1990                 value_type const c = getUChar(buffer->params, i);
1991                 if (IsPrintable(c))
1992                         ost << c;
1993                 else if (c == META_NEWLINE)
1994                         ost << '\n';
1995                 else if (c == META_HFILL)
1996                         ost << '\t'; 
1997                 else if (c == META_INSET) {
1998                         getInset(i)->ascii(buffer, ost);
1999                 }
2000         }
2001
2002         return ost.str().c_str();
2003 }
2004
2005
2006 void Paragraph::setInsetOwner(Inset * i)
2007 {
2008         pimpl_->inset_owner = i;
2009         for (InsetList::const_iterator cit = insetlist.begin();
2010              cit != insetlist.end(); ++cit) {
2011                 if (cit->inset)
2012                         cit->inset->setOwner(i);
2013         }
2014 }
2015
2016
2017 void Paragraph::deleteInsetsLyXText(BufferView * bv)
2018 {
2019         // then the insets
2020         for (InsetList::const_iterator cit = insetlist.begin();
2021              cit != insetlist.end(); ++cit) {
2022                 if (cit->inset) {
2023                         if (cit->inset->isTextInset()) {
2024                                 static_cast<UpdatableInset *>
2025                                         (cit->inset)->deleteLyXText(bv, true);
2026                         }
2027                 }
2028         }
2029 }
2030
2031
2032 void Paragraph::resizeInsetsLyXText(BufferView * bv)
2033 {
2034         // then the insets
2035         for (InsetList::const_iterator cit = insetlist.begin();
2036              cit != insetlist.end(); ++cit)
2037         {
2038                 if (cit->inset) {
2039                         if (cit->inset->isTextInset()) {
2040                                 static_cast<UpdatableInset *>
2041                                         (cit->inset)->resizeLyXText(bv, true);
2042                         }
2043                 }
2044         }
2045 }
2046
2047
2048 void Paragraph::setContentsFromPar(Paragraph * par)
2049 {
2050         pimpl_->setContentsFromPar(par);
2051 }
2052
2053
2054 lyx::pos_type Paragraph::size() const
2055 {
2056         return pimpl_->size();
2057 }
2058
2059
2060 Paragraph::value_type Paragraph::getChar(pos_type pos) const
2061 {
2062         return pimpl_->getChar(pos);
2063 }
2064
2065
2066 int Paragraph::id() const
2067 {
2068         return pimpl_->id_;
2069 }
2070
2071
2072 void  Paragraph::id(int id_arg)
2073 {
2074         pimpl_->id_ = id_arg;
2075 }
2076
2077
2078 layout_type Paragraph::getLayout() const
2079 {
2080         return layout;
2081 }
2082
2083
2084 bool Paragraph::isFirstInSequence() const
2085 {
2086         Paragraph const * dhook = depthHook(getDepth());
2087         return (dhook == this
2088                 || dhook->getLayout() != getLayout()
2089                 || dhook->getDepth() != getDepth());
2090 }
2091
2092
2093 Inset * Paragraph::inInset() const
2094 {
2095         return pimpl_->inset_owner;
2096 }
2097
2098
2099 void Paragraph::clearContents()
2100 {
2101         pimpl_->clear();
2102 }
2103
2104
2105 void Paragraph::setCounter(int i, int v)
2106 {
2107         pimpl_->counter_[i] = v;
2108 }
2109
2110
2111 int Paragraph::getCounter(int i) const
2112 {
2113         return pimpl_->counter_[i];
2114 }
2115
2116
2117 void Paragraph::incCounter(int i)
2118 {
2119         pimpl_->counter_[i]++;
2120 }
2121
2122
2123 void Paragraph::setChar(pos_type pos, value_type c)
2124 {
2125         pimpl_->setChar(pos, c);
2126 }
2127
2128
2129 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2130  : it(iter) 
2131 {}
2132
2133
2134 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2135 {
2136         return inset_iterator(insetlist.begin());
2137 }
2138
2139
2140 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2141 {
2142         return inset_iterator(insetlist.end());
2143 }
2144
2145
2146 ParagraphParameters & Paragraph::params()
2147 {
2148         return pimpl_->params;
2149 }
2150
2151
2152 ParagraphParameters const & Paragraph::params() const
2153 {
2154         return pimpl_->params;
2155 }
2156
2157
2158 Paragraph * Paragraph::getParFromID(int id) const
2159 {
2160         return pimpl_->getParFromID(id);
2161 }
2162
2163
2164 bool Paragraph::isFreeSpacing() const
2165 {
2166         // for now we just need this, later should we need this in some
2167         // other way we can always add a function to Inset::() too.
2168         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
2169                 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);
2170         return false;
2171 }