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