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