]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
9bdfded1761bc100b101bfd533e92436c1b91093
[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 // the next two functions are for the manual labels
988 string const Paragraph::getLabelWidthString() const
989 {
990         if (!params().labelWidthString().empty())
991                 return params().labelWidthString();
992         else
993                 return _("Senseless with this layout!");
994 }
995
996
997 void Paragraph::setLabelWidthString(string const & s)
998 {
999         params().labelWidthString(s);
1000 }
1001
1002
1003 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
1004 {
1005         layout(new_layout);
1006         params().labelWidthString(string());
1007         params().align(LYX_ALIGN_LAYOUT);
1008         params().spaceTop(VSpace(VSpace::NONE));
1009         params().spaceBottom(VSpace(VSpace::NONE));
1010         params().spacing(Spacing(Spacing::Default));
1011 }
1012
1013
1014 // if the layout of a paragraph contains a manual label, the beginning of the
1015 // main body is the beginning of the second word. This is what the par-
1016 // function returns. If the layout does not contain a label, the main
1017 // body always starts with position 0. This differentiation is necessary,
1018 // because there cannot be a newline or a blank <= the beginning of the
1019 // main body in TeX.
1020
1021 int Paragraph::beginningOfMainBody() const
1022 {
1023         // Unroll the first two cycles of the loop
1024         // and remember the previous character to
1025         // remove unnecessary GetChar() calls
1026         pos_type i = 0;
1027         if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
1028                 ++i;
1029                 char previous_char = 0;
1030                 char temp = 0;
1031                 if (i < size()
1032                     && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1033                         // Yes, this  ^ is supposed to be "= " not "=="
1034                         ++i;
1035                         while (i < size()
1036                                && previous_char != ' '
1037                                && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1038                                 ++i;
1039                                 previous_char = temp;
1040                         }
1041                 }
1042         }
1043
1044         return i;
1045 }
1046
1047
1048 Paragraph * Paragraph::depthHook(depth_type depth)
1049 {
1050         Paragraph * newpar = this;
1051
1052         do {
1053                 newpar = newpar->previous();
1054         } while (newpar && newpar->getDepth() > depth);
1055
1056         if (!newpar) {
1057                 if (previous() || getDepth())
1058                         lyxerr << "ERROR (Paragraph::DepthHook): "
1059                                 "no hook." << endl;
1060                 newpar = this;
1061         }
1062
1063         return newpar;
1064 }
1065
1066
1067 Paragraph const * Paragraph::depthHook(depth_type depth) const
1068 {
1069         Paragraph const * newpar = this;
1070
1071         do {
1072                 newpar = newpar->previous();
1073         } while (newpar && newpar->getDepth() > depth);
1074
1075         if (!newpar) {
1076                 if (previous() || getDepth())
1077                         lyxerr << "ERROR (Paragraph::DepthHook): "
1078                                 "no hook." << endl;
1079                 newpar = this;
1080         }
1081
1082         return newpar;
1083 }
1084
1085 Paragraph * Paragraph::outerHook()
1086 {
1087         if (!getDepth())
1088                 return 0;
1089         return depthHook(depth_type(getDepth() - 1));
1090 }
1091
1092 Paragraph const * Paragraph::outerHook() const
1093 {
1094         if (!getDepth())
1095                 return 0;
1096         return depthHook(depth_type(getDepth() - 1));
1097 }
1098
1099
1100 // returns -1 if inset not found
1101 int Paragraph::getPositionOfInset(Inset const * inset) const
1102 {
1103         // Find the entry.
1104         InsetList::iterator it = insetlist.begin();
1105         InsetList::iterator end = insetlist.end();
1106         for (; it != end; ++it) {
1107                 if (it.getInset() == inset) {
1108                         return it.getPos();
1109                 }
1110         }
1111         if (inset == bibkey)
1112                 return 0;
1113
1114         return -1;
1115 }
1116
1117
1118 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1119                                  BufferParams const & bparams,
1120                                  ostream & os, TexRow & texrow,
1121                                  bool moving_arg)
1122 {
1123         lyxerr[Debug::LATEX] << "TeXOnePar...     " << this << endl;
1124         Inset const * in = inInset();
1125         bool further_blank_line = false;
1126         LyXLayout_ptr style;
1127
1128         // well we have to check if we are in an inset with unlimited
1129         // lenght (all in one row) if that is true then we don't allow
1130         // any special options in the paragraph and also we don't allow
1131         // any environment other then "Standard" to be valid!
1132         if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1133                 style = layout();
1134
1135                 if (params().startOfAppendix()) {
1136                         os << "\\appendix\n";
1137                         texrow.newline();
1138                 }
1139
1140                 if (!params().spacing().isDefault()
1141                         && (!previous() || !previous()->hasSameLayout(this))) {
1142                         os << params().spacing().writeEnvirBegin() << "\n";
1143                         texrow.newline();
1144                 }
1145
1146                 if (style->isCommand()) {
1147                         os << '\n';
1148                         texrow.newline();
1149                 }
1150
1151                 if (params().pagebreakTop()) {
1152                         os << "\\newpage";
1153                         further_blank_line = true;
1154                 }
1155                 if (params().spaceTop().kind() != VSpace::NONE) {
1156                         os << params().spaceTop().asLatexCommand(bparams);
1157                         further_blank_line = true;
1158                 }
1159
1160                 if (params().lineTop()) {
1161                         os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1162                            << "\\vspace{-1\\parskip}";
1163                         further_blank_line = true;
1164                 }
1165
1166                 if (further_blank_line) {
1167                         os << '\n';
1168                         texrow.newline();
1169                 }
1170         } else {
1171                 style = bparams.getLyXTextClass().defaultLayout();
1172         }
1173
1174         Language const * language = getParLanguage(bparams);
1175         Language const * doc_language = bparams.language;
1176         Language const * previous_language = previous()
1177                 ? previous()->getParLanguage(bparams) : doc_language;
1178
1179         if (language->babel() != previous_language->babel()
1180             // check if we already put language command in TeXEnvironment()
1181             && !(style->isEnvironment()
1182                  && (!previous() || previous()->layout() != layout() ||
1183                          previous()->params().depth() != params().depth())))
1184         {
1185                 if (!lyxrc.language_command_end.empty() &&
1186                     previous_language->babel() != doc_language->babel())
1187                 {
1188                         os << subst(lyxrc.language_command_end, "$$lang",
1189                                     previous_language->babel())
1190                            << endl;
1191                         texrow.newline();
1192                 }
1193
1194                 if (lyxrc.language_command_end.empty() ||
1195                     language->babel() != doc_language->babel())
1196                 {
1197                         os << subst(lyxrc.language_command_begin, "$$lang",
1198                                     language->babel())
1199                            << endl;
1200                         texrow.newline();
1201                 }
1202         }
1203
1204         if (bparams.inputenc == "auto" &&
1205             language->encoding() != previous_language->encoding()) {
1206                 os << "\\inputencoding{"
1207                    << language->encoding()->LatexName()
1208                    << "}" << endl;
1209                 texrow.newline();
1210         }
1211
1212         switch (style->latextype) {
1213         case LATEX_COMMAND:
1214                 os << '\\'
1215                    << style->latexname()
1216                    << style->latexparam();
1217                 break;
1218         case LATEX_ITEM_ENVIRONMENT:
1219                 if (bibkey) {
1220                         bibkey->latex(buf, os, false, false);
1221                 } else
1222                         os << "\\item ";
1223                 break;
1224         case LATEX_LIST_ENVIRONMENT:
1225                 os << "\\item ";
1226                 break;
1227         default:
1228                 break;
1229         }
1230
1231         bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1232
1233         // Make sure that \\par is done with the font of the last
1234         // character if this has another size as the default.
1235         // This is necessary because LaTeX (and LyX on the screen)
1236         // calculates the space between the baselines according
1237         // to this font. (Matthias)
1238         //
1239         // Is this really needed ? (Dekel)
1240         // We do not need to use to change the font for the last paragraph
1241         // or for a command.
1242         LyXFont const font =
1243                 (empty()
1244                  ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1245
1246         bool is_command = style->isCommand();
1247
1248         if (style->resfont.size() != font.size() && next_ && !is_command) {
1249                 if (!need_par)
1250                         os << "{";
1251                 os << "\\" << font.latexSize() << " \\par}";
1252         } else if (need_par) {
1253                 os << "\\par}";
1254         } else if (is_command)
1255                 os << "}";
1256
1257         switch (style->latextype) {
1258         case LATEX_ITEM_ENVIRONMENT:
1259         case LATEX_LIST_ENVIRONMENT:
1260                 if (next_ && (params().depth() < next_->params().depth())) {
1261                         os << '\n';
1262                         texrow.newline();
1263                 }
1264                 break;
1265         case LATEX_ENVIRONMENT:
1266                 // if its the last paragraph of the current environment
1267                 // skip it otherwise fall through
1268                 if (next_
1269                     && (next_->layout() != layout()
1270                         || next_->params().depth() != params().depth()))
1271                         break;
1272                 // fall through possible
1273         default:
1274                 // we don't need it for the last paragraph!!!
1275                 if (next_) {
1276                         os << '\n';
1277                         texrow.newline();
1278                 }
1279         }
1280
1281         if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1282                 further_blank_line = false;
1283                 if (params().lineBottom()) {
1284                         os << "\\lyxline{\\" << font.latexSize() << '}';
1285                         further_blank_line = true;
1286                 }
1287
1288                 if (params().spaceBottom().kind() != VSpace::NONE) {
1289                         os << params().spaceBottom().asLatexCommand(bparams);
1290                         further_blank_line = true;
1291                 }
1292
1293                 if (params().pagebreakBottom()) {
1294                         os << "\\newpage";
1295                         further_blank_line = true;
1296                 }
1297
1298                 if (further_blank_line) {
1299                         os << '\n';
1300                         texrow.newline();
1301                 }
1302
1303                 if (!params().spacing().isDefault()
1304                         && (!next_ || !next_->hasSameLayout(this))) {
1305                         os << params().spacing().writeEnvirEnd() << "\n";
1306                         texrow.newline();
1307                 }
1308         }
1309
1310         // we don't need it for the last paragraph!!!
1311         if (next_) {
1312                 os << '\n';
1313                 texrow.newline();
1314         } else {
1315                 // Since \selectlanguage write the language to the aux file,
1316                 // we need to reset the language at the end of footnote or
1317                 // float.
1318
1319                 if (language->babel() != doc_language->babel()) {
1320                         if (lyxrc.language_command_end.empty())
1321                                 os << subst(lyxrc.language_command_begin,
1322                                             "$$lang",
1323                                             doc_language->babel())
1324                                    << endl;
1325                         else
1326                                 os << subst(lyxrc.language_command_end,
1327                                             "$$lang",
1328                                             language->babel())
1329                                    << endl;
1330                         texrow.newline();
1331                 }
1332         }
1333
1334         lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1335         return next_;
1336 }
1337
1338 // This could go to ParagraphParameters if we want to
1339 int Paragraph::startTeXParParams(BufferParams const & bparams,
1340                                  ostream & os, bool moving_arg) const
1341 {
1342         int column = 0;
1343
1344         if (params().noindent()) {
1345                 os << "\\noindent ";
1346                 column += 10;
1347         }
1348
1349         switch (params().align()) {
1350         case LYX_ALIGN_NONE:
1351         case LYX_ALIGN_BLOCK:
1352         case LYX_ALIGN_LAYOUT:
1353         case LYX_ALIGN_SPECIAL:
1354                 break;
1355         case LYX_ALIGN_LEFT:
1356         case LYX_ALIGN_RIGHT:
1357         case LYX_ALIGN_CENTER:
1358                 if (moving_arg) {
1359                         os << "\\protect";
1360                         column = 8;
1361                 }
1362                 break;
1363         }
1364
1365         switch (params().align()) {
1366         case LYX_ALIGN_NONE:
1367         case LYX_ALIGN_BLOCK:
1368         case LYX_ALIGN_LAYOUT:
1369         case LYX_ALIGN_SPECIAL:
1370                 break;
1371         case LYX_ALIGN_LEFT:
1372                 if (getParLanguage(bparams)->babel() != "hebrew") {
1373                         os << "\\begin{flushleft}";
1374                         column += 17;
1375                 } else {
1376                         os << "\\begin{flushright}";
1377                         column += 18;
1378                 }
1379                 break;
1380         case LYX_ALIGN_RIGHT:
1381                 if (getParLanguage(bparams)->babel() != "hebrew") {
1382                         os << "\\begin{flushright}";
1383                         column += 18;
1384                 } else {
1385                         os << "\\begin{flushleft}";
1386                         column += 17;
1387                 }
1388                 break;
1389         case LYX_ALIGN_CENTER:
1390                 os << "\\begin{center}";
1391                 column += 14;
1392                 break;
1393         }
1394
1395         return column;
1396 }
1397
1398 // This could go to ParagraphParameters if we want to
1399 int Paragraph::endTeXParParams(BufferParams const & bparams,
1400                                ostream & os, bool moving_arg) const
1401 {
1402         int column = 0;
1403
1404         switch (params().align()) {
1405         case LYX_ALIGN_NONE:
1406         case LYX_ALIGN_BLOCK:
1407         case LYX_ALIGN_LAYOUT:
1408         case LYX_ALIGN_SPECIAL:
1409                 break;
1410         case LYX_ALIGN_LEFT:
1411         case LYX_ALIGN_RIGHT:
1412         case LYX_ALIGN_CENTER:
1413                 if (moving_arg) {
1414                         os << "\\protect";
1415                         column = 8;
1416                 }
1417                 break;
1418         }
1419
1420         switch (params().align()) {
1421         case LYX_ALIGN_NONE:
1422         case LYX_ALIGN_BLOCK:
1423         case LYX_ALIGN_LAYOUT:
1424         case LYX_ALIGN_SPECIAL:
1425                 break;
1426         case LYX_ALIGN_LEFT:
1427                 if (getParLanguage(bparams)->babel() != "hebrew") {
1428                         os << "\\end{flushleft}";
1429                         column = 15;
1430                 } else {
1431                         os << "\\end{flushright}";
1432                         column = 16;
1433                 }
1434                 break;
1435         case LYX_ALIGN_RIGHT:
1436                 if (getParLanguage(bparams)->babel() != "hebrew") {
1437                         os << "\\end{flushright}";
1438                         column+= 16;
1439                 } else {
1440                         os << "\\end{flushleft}";
1441                         column = 15;
1442                 }
1443                 break;
1444         case LYX_ALIGN_CENTER:
1445                 os << "\\end{center}";
1446                 column = 12;
1447                 break;
1448         }
1449         return column;
1450 }
1451
1452
1453 // This one spits out the text of the paragraph
1454 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1455                                 BufferParams const & bparams,
1456                                 ostream & os, TexRow & texrow,
1457                                 bool moving_arg)
1458 {
1459         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
1460
1461         bool return_value = false;
1462
1463         LyXLayout_ptr style;
1464
1465         // well we have to check if we are in an inset with unlimited
1466         // lenght (all in one row) if that is true then we don't allow
1467         // any special options in the paragraph and also we don't allow
1468         // any environment other then "Standard" to be valid!
1469         bool asdefault =
1470                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1471
1472         if (asdefault) {
1473                 style = bparams.getLyXTextClass().defaultLayout();
1474         } else {
1475                 style = layout();
1476         }
1477
1478         LyXFont basefont;
1479
1480         // Maybe we have to create a optional argument.
1481         pos_type main_body;
1482         if (style->labeltype != LABEL_MANUAL)
1483                 main_body = 0;
1484         else
1485                 main_body = beginningOfMainBody();
1486
1487         int column = 0;
1488
1489         if (main_body > 0) {
1490                 os << '[';
1491                 ++column;
1492                 basefont = getLabelFont(bparams);
1493         } else {
1494                 basefont = getLayoutFont(bparams);
1495         }
1496
1497         moving_arg |= style->needprotect;
1498
1499         // Which font is currently active?
1500         LyXFont running_font(basefont);
1501         // Do we have an open font change?
1502         bool open_font = false;
1503
1504         texrow.start(this, 0);
1505
1506         // if the paragraph is empty, the loop will not be entered at all
1507         if (empty()) {
1508                 if (style->isCommand()) {
1509                         os << '{';
1510                         ++column;
1511                 }
1512                 if (!asdefault)
1513                         column += startTeXParParams(bparams, os, moving_arg);
1514
1515         }
1516
1517         for (pos_type i = 0; i < size(); ++i) {
1518                 ++column;
1519                 // First char in paragraph or after label?
1520                 if (i == main_body) {
1521                         if (main_body > 0) {
1522                                 if (open_font) {
1523                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
1524                                         open_font = false;
1525                                 }
1526                                 basefont = getLayoutFont(bparams);
1527                                 running_font = basefont;
1528                                 os << ']';
1529                                 ++column;
1530                         }
1531                         if (style->isCommand()) {
1532                                 os << '{';
1533                                 ++column;
1534                         }
1535
1536                         if (!asdefault)
1537                                 column += startTeXParParams(bparams, os,
1538                                                             moving_arg);
1539                 }
1540
1541                 value_type c = getChar(i);
1542
1543                 // Fully instantiated font
1544                 LyXFont font = getFont(bparams, i);
1545
1546                 LyXFont const last_font = running_font;
1547
1548                 // Spaces at end of font change are simulated to be
1549                 // outside font change, i.e. we write "\textXX{text} "
1550                 // rather than "\textXX{text }". (Asger)
1551                 if (open_font && c == ' ' && i <= size() - 2) {
1552                         LyXFont const & next_font = getFont(bparams, i + 1);
1553                         if (next_font != running_font
1554                             && next_font != font) {
1555                                 font = next_font;
1556                         }
1557                 }
1558
1559                 // We end font definition before blanks
1560                 if (open_font &&
1561                     (font != running_font ||
1562                      font.language() != running_font.language()))
1563                 {
1564                         column += running_font.latexWriteEndChanges(os,
1565                                                                     basefont,
1566                                                                     (i == main_body-1) ? basefont : font);
1567                         running_font = basefont;
1568                         open_font = false;
1569                 }
1570
1571                 // Blanks are printed before start of fontswitch
1572                 if (c == ' ') {
1573                         // Do not print the separation of the optional argument
1574                         if (i != main_body - 1) {
1575                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1576                                                        column, font, *style);
1577                         }
1578                 }
1579
1580                 // Do we need to change font?
1581                 if ((font != running_font ||
1582                      font.language() != running_font.language()) &&
1583                         i != main_body - 1)
1584                 {
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
1610                                 os << "\\\\\n";
1611                         }
1612                         texrow.newline();
1613                         texrow.start(this, i + 1);
1614                         column = 0;
1615                 } else {
1616                         pimpl_->simpleTeXSpecialChars(buf, bparams,
1617                                                       os, texrow, moving_arg,
1618                                                       font, running_font,
1619                                                       basefont, open_font,
1620                                                       *style, i, column, c);
1621                 }
1622         }
1623
1624         // If we have an open font definition, we have to close it
1625         if (open_font) {
1626 #ifdef FIXED_LANGUAGE_END_DETECTION
1627                 if (next_) {
1628                         running_font
1629                                 .latexWriteEndChanges(os, basefont,
1630                                                       next_->getFont(bparams,
1631                                                       0));
1632                 } else {
1633                         running_font.latexWriteEndChanges(os, basefont,
1634                                                           basefont);
1635                 }
1636 #else
1637 #ifdef WITH_WARNINGS
1638 //#warning For now we ALWAYS have to close the foreign font settings if they are
1639 //#warning there as we start another \selectlanguage with the next paragraph if
1640 //#warning we are in need of this. This should be fixed sometime (Jug)
1641 #endif
1642                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1643 #endif
1644         }
1645
1646         // Needed if there is an optional argument but no contents.
1647         if (main_body > 0 && main_body == size()) {
1648                 os << "]~";
1649                 return_value = false;
1650         }
1651
1652         if (!asdefault) {
1653                 column += endTeXParParams(bparams, os, moving_arg);
1654         }
1655
1656         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1657         return return_value;
1658 }
1659
1660
1661 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1662                                             BufferParams const & bparams,
1663                                             ostream & os, TexRow & texrow)
1664 {
1665         lyxerr[Debug::LATEX] << "TeXEnvironment...     " << this << endl;
1666
1667         LyXLayout_ptr const & style = layout();
1668
1669         Language const * language = getParLanguage(bparams);
1670         Language const * doc_language = bparams.language;
1671         Language const * previous_language = previous_
1672                 ? previous_->getParLanguage(bparams) : doc_language;
1673         if (language->babel() != previous_language->babel()) {
1674
1675                 if (!lyxrc.language_command_end.empty() &&
1676                     previous_language->babel() != doc_language->babel()) {
1677                         os << subst(lyxrc.language_command_end, "$$lang",
1678                                     previous_language->babel())
1679                            << endl;
1680                         texrow.newline();
1681                 }
1682
1683                 if (lyxrc.language_command_end.empty() ||
1684                     language->babel() != doc_language->babel()) {
1685                         os << subst(lyxrc.language_command_begin, "$$lang",
1686                                     language->babel())
1687                            << endl;
1688                         texrow.newline();
1689                 }
1690         }
1691
1692         bool leftindent_open = false;
1693         if (!params().leftIndent().zero()) {
1694                 os << "\\begin{LyXParagraphLeftIndent}{" <<
1695                         params().leftIndent().asLatexString() << "}\n";
1696                 texrow.newline();
1697                 leftindent_open = true;
1698         }
1699
1700         if (style->isEnvironment()) {
1701                 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1702                         os << "\\begin{" << style->latexname() << "}{"
1703                            << params().labelWidthString() << "}\n";
1704                 } else if (style->labeltype == LABEL_BIBLIO) {
1705                         // ale970405
1706                         os << "\\begin{" << style->latexname() << "}{"
1707                            <<  bibitemWidest(buf)
1708                            << "}\n";
1709                 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1710                         os << "\\begin{" << style->latexname() << '}'
1711                            << style->latexparam() << '\n';
1712                 } else
1713                         os << "\\begin{" << style->latexname() << '}'
1714                            << style->latexparam() << '\n';
1715                 texrow.newline();
1716         }
1717         Paragraph * par = this;
1718         do {
1719                 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1720
1721                 if (par && par->params().depth() > params().depth()) {
1722                             if (par->layout()->isParagraph()) {
1723
1724                             // Thinko!
1725                             // How to handle this? (Lgb)
1726                             //&& !suffixIs(os, "\n\n")
1727                                     //) {
1728                                 // There should be at least one '\n' already
1729                                 // but we need there to be two for Standard
1730                                 // paragraphs that are depth-increment'ed to be
1731                                 // output correctly.  However, tables can
1732                                 // also be paragraphs so don't adjust them.
1733                                 // ARRae
1734                                 // Thinkee:
1735                                 // Will it ever harm to have one '\n' too
1736                                 // many? i.e. that we sometimes will have
1737                                 // three in a row. (Lgb)
1738                                 os << '\n';
1739                                 texrow.newline();
1740                         }
1741                         par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1742                 }
1743         } while (par
1744                  && par->layout() == layout()
1745                  && par->params().depth() == params().depth()
1746                  && par->params().leftIndent() == params().leftIndent());
1747
1748         if (style->isEnvironment()) {
1749                 os << "\\end{" << style->latexname() << "}\n";
1750                 texrow.newline();
1751         }
1752
1753         if (leftindent_open) {
1754                 os << "\\end{LyXParagraphLeftIndent}\n";
1755                 texrow.newline();
1756         }
1757
1758         lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1759         return par;  // ale970302
1760 }
1761
1762
1763 bool Paragraph::isHfill(pos_type pos) const
1764 {
1765         return IsHfillChar(getChar(pos));
1766 }
1767
1768
1769 bool Paragraph::isInset(pos_type pos) const
1770 {
1771         return IsInsetChar(getChar(pos));
1772 }
1773
1774
1775 bool Paragraph::isNewline(pos_type pos) const
1776 {
1777         return pos >= 0 && IsNewlineChar(getChar(pos));
1778 }
1779
1780
1781 bool Paragraph::isSeparator(pos_type pos) const
1782 {
1783         return IsSeparatorChar(getChar(pos));
1784 }
1785
1786
1787 bool Paragraph::isLineSeparator(pos_type pos) const
1788 {
1789         value_type const c = getChar(pos);
1790         return IsLineSeparatorChar(c)
1791                 || (IsInsetChar(c) && getInset(pos) &&
1792                 getInset(pos)->isLineSeparator());
1793 }
1794
1795
1796 bool Paragraph::isKomma(pos_type pos) const
1797 {
1798         return IsKommaChar(getChar(pos));
1799 }
1800
1801
1802 /// Used by the spellchecker
1803 bool Paragraph::isLetter(pos_type pos) const
1804 {
1805         value_type const c = getChar(pos);
1806         if (IsLetterChar(c))
1807                 return true;
1808         if (isInset(pos))
1809                 return getInset(pos)->isLetter();
1810         // We want to pass the ' and escape chars to ispell
1811         string const extra = lyxrc.isp_esc_chars + '\'';
1812         return contains(extra, c);
1813 }
1814
1815
1816 bool Paragraph::isWord(pos_type pos) const
1817 {
1818         return IsWordChar(getChar(pos)) ;
1819 }
1820
1821
1822 Language const *
1823 Paragraph::getParLanguage(BufferParams const & bparams) const
1824 {
1825         if (!empty()) {
1826 #ifndef INHERIT_LANGUAGE
1827                 return getFirstFontSettings().language();
1828 #else
1829                 Language const * lang = getFirstFontSettings().language();
1830 #ifdef WITH_WARNINGS
1831 #warning We should make this somewhat better, any ideas? (Jug)
1832 #endif
1833                 if (lang == inherit_language || lang == ignore_language)
1834                         lang = bparams.language;
1835                 return lang;
1836 #endif
1837         } else if (previous_)
1838                 return previous_->getParLanguage(bparams);
1839         else
1840                 return bparams.language;
1841 }
1842
1843
1844 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1845 {
1846         return lyxrc.rtl_support
1847                 && getParLanguage(bparams)->RightToLeft()
1848                 && !(inInset() && inInset()->owner() &&
1849                      inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1850 }
1851
1852
1853 void Paragraph::changeLanguage(BufferParams const & bparams,
1854                                   Language const * from, Language const * to)
1855 {
1856         for (pos_type i = 0; i < size(); ++i) {
1857                 LyXFont font = getFontSettings(bparams, i);
1858                 if (font.language() == from) {
1859                         font.setLanguage(to);
1860                         setFont(i, font);
1861                 }
1862         }
1863 }
1864
1865
1866 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1867 {
1868         Language const * doc_language = bparams.language;
1869         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1870         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1871
1872         for (; cit != end; ++cit)
1873                 if (cit->font().language() != ignore_language &&
1874                     cit->font().language() != latex_language &&
1875 #ifdef INHERIT_LANGUAGE
1876                         cit->font().language() != inherit_language &&
1877 #endif
1878                         cit->font().language() != doc_language)
1879                         return true;
1880         return false;
1881 }
1882
1883
1884 // Convert the paragraph to a string.
1885 // Used for building the table of contents
1886 string const Paragraph::asString(Buffer const * buffer, bool label)
1887 {
1888         BufferParams const & bparams = buffer->params;
1889         string s;
1890         if (label && !params().labelString().empty())
1891                 s += params().labelString() + ' ';
1892         string::size_type const len = s.size();
1893
1894         for (pos_type i = 0; i < size(); ++i) {
1895                 value_type c = getChar(i);
1896                 if (IsPrintable(c))
1897                         s += c;
1898                 else if (c == META_INSET &&
1899                          getInset(i)->lyxCode() == Inset::MATH_CODE) {
1900                         ostringstream ost;
1901                         getInset(i)->ascii(buffer, ost);
1902                         s += subst(ost.str().c_str(),'\n',' ');
1903                 }
1904         }
1905
1906         if (isRightToLeftPar(bparams))
1907                 reverse(s.begin() + len,s.end());
1908
1909         return s;
1910 }
1911
1912
1913 string const Paragraph::asString(Buffer const * buffer,
1914                                  pos_type beg, pos_type end, bool label)
1915 {
1916         ostringstream ost;
1917
1918         if (beg == 0 && label && !params().labelString().empty())
1919                 ost << params().labelString() << ' ';
1920
1921         for (pos_type i = beg; i < end; ++i) {
1922                 value_type const c = getUChar(buffer->params, i);
1923                 if (IsPrintable(c))
1924                         ost << c;
1925                 else if (c == META_NEWLINE)
1926                         ost << '\n';
1927                 else if (c == META_HFILL)
1928                         ost << '\t';
1929                 else if (c == META_INSET) {
1930                         getInset(i)->ascii(buffer, ost);
1931                 }
1932         }
1933
1934         return ost.str().c_str();
1935 }
1936
1937
1938 void Paragraph::setInsetOwner(Inset * i)
1939 {
1940         pimpl_->inset_owner = i;
1941         InsetList::iterator it = insetlist.begin();
1942         InsetList::iterator end = insetlist.end();
1943         for (; it != end; ++it) {
1944                 if (it.getInset())
1945                         it.getInset()->setOwner(i);
1946         }
1947 }
1948
1949
1950 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1951 {
1952         // then the insets
1953         insetlist.deleteInsetsLyXText(bv);
1954 }
1955
1956
1957 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1958 {
1959         // then the insets
1960         insetlist.resizeInsetsLyXText(bv);
1961 }
1962
1963
1964 void Paragraph::setContentsFromPar(Paragraph * par)
1965 {
1966         pimpl_->setContentsFromPar(par);
1967 }
1968
1969
1970 lyx::pos_type Paragraph::size() const
1971 {
1972         return pimpl_->size();
1973 }
1974
1975
1976 bool Paragraph::empty() const
1977 {
1978         return pimpl_->empty();
1979 }
1980
1981
1982 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1983 {
1984         return pimpl_->getChar(pos);
1985 }
1986
1987
1988 int Paragraph::id() const
1989 {
1990         return pimpl_->id_;
1991 }
1992
1993
1994 LyXLayout_ptr const & Paragraph::layout() const
1995 {
1996         return layout_;
1997 }
1998
1999
2000 void Paragraph::layout(LyXLayout_ptr const & new_layout)
2001 {
2002         layout_ = new_layout;
2003 }
2004
2005
2006 bool Paragraph::isFirstInSequence() const
2007 {
2008         Paragraph const * dhook = depthHook(getDepth());
2009         return (dhook == this
2010                 || dhook->layout() != layout()
2011                 || dhook->getDepth() != getDepth());
2012 }
2013
2014
2015 Inset * Paragraph::inInset() const
2016 {
2017         return pimpl_->inset_owner;
2018 }
2019
2020
2021 void Paragraph::clearContents()
2022 {
2023         pimpl_->clear();
2024 }
2025
2026 void Paragraph::setChar(pos_type pos, value_type c)
2027 {
2028         pimpl_->setChar(pos, c);
2029 }
2030
2031
2032 ParagraphParameters & Paragraph::params()
2033 {
2034         return pimpl_->params;
2035 }
2036
2037
2038 ParagraphParameters const & Paragraph::params() const
2039 {
2040         return pimpl_->params;
2041 }
2042
2043
2044 Paragraph * Paragraph::getParFromID(int id) const
2045 {
2046         return pimpl_->getParFromID(id);
2047 }
2048
2049
2050 bool Paragraph::isFreeSpacing() const
2051 {
2052         // for now we just need this, later should we need this in some
2053         // other way we can always add a function to Inset::() too.
2054         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
2055                 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);
2056         return false;
2057 }
2058
2059