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