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