]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
remove noload/don't typeset
[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 using std::reverse;
53
54 using lyx::pos_type;
55
56 // this is a bad idea, but how can Paragraph find its buffer to get
57 // parameters? (JMarc)
58
59 extern string bibitemWidest(Buffer const *);
60
61 // this is a minibuffer
62
63 namespace {
64
65 char minibuffer_char;
66 LyXFont minibuffer_font;
67 Inset * minibuffer_inset;
68
69 } // namespace anon
70
71
72 extern BufferView * current_view;
73
74
75 Paragraph::Paragraph()
76         : pimpl_(new Paragraph::Pimpl(this))
77 {
78 #ifndef NO_NEXT
79         next_ = 0;
80         previous_ = 0;
81 #endif
82         enumdepth = 0;
83         itemdepth = 0;
84         bibkey = 0; // ale970302
85         params().clear();
86 }
87
88
89 #ifndef NO_NEXT
90 // This constructor inserts the new paragraph in a list.
91 Paragraph::Paragraph(Paragraph * par)
92         : pimpl_(new Paragraph::Pimpl(this))
93 {
94         enumdepth = 0;
95         itemdepth = 0;
96
97         // double linked list begin
98         next_ = par->next_;
99         if (next_)
100                 next_->previous_ = this;
101         previous_ = par;
102         previous_->next_ = this;
103         // end
104
105         bibkey = 0; // ale970302
106         params().clear();
107 }
108 #endif
109
110
111 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
112         : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
113 {
114         enumdepth = 0;
115         itemdepth = 0;
116 #ifndef NO_NEXT
117         next_     = 0;
118         previous_ = 0;
119 #endif
120         // this is because of the dummy layout of the paragraphs that
121         // follow footnotes
122         layout_ = lp.layout();
123
124         // ale970302
125         if (lp.bibkey) {
126                 bibkey = static_cast<InsetBibKey *>
127                         (lp.bibkey->clone(*current_view->buffer()));
128         } else {
129                 bibkey = 0;
130         }
131
132         // copy everything behind the break-position to the new paragraph
133         insetlist = lp.insetlist;
134         InsetList::iterator it = insetlist.begin();
135         InsetList::iterator end = insetlist.end();
136         for (; it != end; ++it) {
137                 it.setInset(it.getInset()->clone(*current_view->buffer(),
138                                                  same_ids));
139                 // tell the new inset who is the boss now
140                 it.getInset()->parOwner(this);
141         }
142 }
143
144
145 // the destructor removes the new paragraph from the list
146 Paragraph::~Paragraph()
147 {
148 #ifndef NO_NEXT
149         if (previous_)
150                 previous_->next_ = next_;
151         if (next_)
152                 next_->previous_ = previous_;
153 #endif
154
155         // ale970302
156         delete bibkey;
157
158         delete pimpl_;
159         //
160         //lyxerr << "Paragraph::paragraph_id = "
161         //       << Paragraph::paragraph_id << endl;
162 }
163
164
165 void Paragraph::write(Buffer const * buf, ostream & os,
166                           BufferParams const & bparams,
167                           depth_type & dth) const
168 {
169         // The beginning or end of a deeper (i.e. nested) area?
170         if (dth != params().depth()) {
171                 if (params().depth() > dth) {
172                         while (params().depth() > dth) {
173                                 os << "\n\\begin_deeper ";
174                                 ++dth;
175                         }
176                 } else {
177                         while (params().depth() < dth) {
178                                 os << "\n\\end_deeper ";
179                                 --dth;
180                         }
181                 }
182         }
183
184         // First write the layout
185         os << "\n\\layout " << layout()->name() << "\n";
186
187         // Maybe some vertical spaces.
188         if (params().spaceTop().kind() != VSpace::NONE)
189                 os << "\\added_space_top "
190                    << params().spaceTop().asLyXCommand() << " ";
191         if (params().spaceBottom().kind() != VSpace::NONE)
192                 os << "\\added_space_bottom "
193                    << params().spaceBottom().asLyXCommand() << " ";
194
195         // Maybe the paragraph has special spacing
196         params().spacing().writeFile(os, true);
197
198         // The labelwidth string used in lists.
199         if (!params().labelWidthString().empty())
200                 os << "\\labelwidthstring "
201                    << params().labelWidthString() << '\n';
202
203         // Lines above or below?
204         if (params().lineTop())
205                 os << "\\line_top ";
206         if (params().lineBottom())
207                 os << "\\line_bottom ";
208
209         // Pagebreaks above or below?
210         if (params().pagebreakTop())
211                 os << "\\pagebreak_top ";
212         if (params().pagebreakBottom())
213                 os << "\\pagebreak_bottom ";
214
215         // Start of appendix?
216         if (params().startOfAppendix())
217                 os << "\\start_of_appendix ";
218
219         // Noindent?
220         if (params().noindent())
221                 os << "\\noindent ";
222
223         // Do we have a manual left indent?
224         if (!params().leftIndent().zero())
225                 os << "\\leftindent " << params().leftIndent().asString() << " ";
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() || previous()->layout() != layout() ||
1028                          previous()->params().depth() != params().depth())))
1029         {
1030                 if (!lyxrc.language_command_end.empty() &&
1031                     previous_language->babel() != doc_language->babel())
1032                 {
1033                         os << subst(lyxrc.language_command_end, "$$lang",
1034                                     previous_language->babel())
1035                            << endl;
1036                         texrow.newline();
1037                 }
1038
1039                 if (lyxrc.language_command_end.empty() ||
1040                     language->babel() != doc_language->babel())
1041                 {
1042                         os << subst(lyxrc.language_command_begin, "$$lang",
1043                                     language->babel())
1044                            << endl;
1045                         texrow.newline();
1046                 }
1047         }
1048
1049         if (bparams.inputenc == "auto" &&
1050             language->encoding() != previous_language->encoding()) {
1051                 os << "\\inputencoding{"
1052                    << language->encoding()->LatexName()
1053                    << "}" << endl;
1054                 texrow.newline();
1055         }
1056
1057         switch (style->latextype) {
1058         case LATEX_COMMAND:
1059                 os << '\\'
1060                    << style->latexname();
1061
1062                 // Separate handling of optional argument inset.
1063                 if (style->optionalargs == 1) {
1064                         InsetOptArg * it = optArgInset(*this);
1065                         if (it != 0)
1066                                 it->latexOptional(buf, os, false, false);
1067                 }
1068                 else
1069                         os << style->latexparam();
1070                 break;
1071         case LATEX_ITEM_ENVIRONMENT:
1072                 if (bibkey) {
1073                         bibkey->latex(buf, os, false, false);
1074                 } else
1075                         os << "\\item ";
1076                 break;
1077         case LATEX_LIST_ENVIRONMENT:
1078                 os << "\\item ";
1079                 break;
1080         default:
1081                 break;
1082         }
1083
1084         bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1085
1086         // Make sure that \\par is done with the font of the last
1087         // character if this has another size as the default.
1088         // This is necessary because LaTeX (and LyX on the screen)
1089         // calculates the space between the baselines according
1090         // to this font. (Matthias)
1091         //
1092         // Is this really needed ? (Dekel)
1093         // We do not need to use to change the font for the last paragraph
1094         // or for a command.
1095         LyXFont const font =
1096                 (empty()
1097                  ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1098
1099         bool is_command = style->isCommand();
1100
1101         if (style->resfont.size() != font.size() && next_ && !is_command) {
1102                 if (!need_par)
1103                         os << "{";
1104                 os << "\\" << font.latexSize() << " \\par}";
1105         } else if (need_par) {
1106                 os << "\\par}";
1107         } else if (is_command)
1108                 os << "}";
1109
1110         switch (style->latextype) {
1111         case LATEX_ITEM_ENVIRONMENT:
1112         case LATEX_LIST_ENVIRONMENT:
1113                 if (next_ && (params().depth() < next_->params().depth())) {
1114                         os << '\n';
1115                         texrow.newline();
1116                 }
1117                 break;
1118         case LATEX_ENVIRONMENT:
1119                 // if its the last paragraph of the current environment
1120                 // skip it otherwise fall through
1121                 if (next_
1122                     && (next_->layout() != layout()
1123                         || next_->params().depth() != params().depth()))
1124                         break;
1125                 // fall through possible
1126         default:
1127                 // we don't need it for the last paragraph!!!
1128                 if (next_) {
1129                         os << '\n';
1130                         texrow.newline();
1131                 }
1132         }
1133
1134         if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1135                 further_blank_line = false;
1136                 if (params().lineBottom()) {
1137                         os << "\\lyxline{\\" << font.latexSize() << '}';
1138                         further_blank_line = true;
1139                 }
1140
1141                 if (params().spaceBottom().kind() != VSpace::NONE) {
1142                         os << params().spaceBottom().asLatexCommand(bparams);
1143                         further_blank_line = true;
1144                 }
1145
1146                 if (params().pagebreakBottom()) {
1147                         os << "\\newpage";
1148                         further_blank_line = true;
1149                 }
1150
1151                 if (further_blank_line) {
1152                         os << '\n';
1153                         texrow.newline();
1154                 }
1155
1156                 if (!params().spacing().isDefault()
1157                         && (!next_ || !next_->hasSameLayout(this))) {
1158                         os << params().spacing().writeEnvirEnd() << "\n";
1159                         texrow.newline();
1160                 }
1161         }
1162
1163         // we don't need it for the last paragraph!!!
1164         if (next_) {
1165                 os << '\n';
1166                 texrow.newline();
1167         } else {
1168                 // Since \selectlanguage write the language to the aux file,
1169                 // we need to reset the language at the end of footnote or
1170                 // float.
1171
1172                 if (language->babel() != doc_language->babel()) {
1173                         if (lyxrc.language_command_end.empty())
1174                                 os << subst(lyxrc.language_command_begin,
1175                                             "$$lang",
1176                                             doc_language->babel())
1177                                    << endl;
1178                         else
1179                                 os << subst(lyxrc.language_command_end,
1180                                             "$$lang",
1181                                             language->babel())
1182                                    << endl;
1183                         texrow.newline();
1184                 }
1185         }
1186
1187         lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1188         return next_;
1189 }
1190
1191
1192 // This could go to ParagraphParameters if we want to
1193 int Paragraph::startTeXParParams(BufferParams const & bparams,
1194                                  ostream & os, bool moving_arg) const
1195 {
1196         int column = 0;
1197
1198         if (params().noindent()) {
1199                 os << "\\noindent ";
1200                 column += 10;
1201         }
1202
1203         switch (params().align()) {
1204         case LYX_ALIGN_NONE:
1205         case LYX_ALIGN_BLOCK:
1206         case LYX_ALIGN_LAYOUT:
1207         case LYX_ALIGN_SPECIAL:
1208                 break;
1209         case LYX_ALIGN_LEFT:
1210         case LYX_ALIGN_RIGHT:
1211         case LYX_ALIGN_CENTER:
1212                 if (moving_arg) {
1213                         os << "\\protect";
1214                         column = 8;
1215                 }
1216                 break;
1217         }
1218
1219         switch (params().align()) {
1220         case LYX_ALIGN_NONE:
1221         case LYX_ALIGN_BLOCK:
1222         case LYX_ALIGN_LAYOUT:
1223         case LYX_ALIGN_SPECIAL:
1224                 break;
1225         case LYX_ALIGN_LEFT:
1226                 if (getParLanguage(bparams)->babel() != "hebrew") {
1227                         os << "\\begin{flushleft}";
1228                         column += 17;
1229                 } else {
1230                         os << "\\begin{flushright}";
1231                         column += 18;
1232                 }
1233                 break;
1234         case LYX_ALIGN_RIGHT:
1235                 if (getParLanguage(bparams)->babel() != "hebrew") {
1236                         os << "\\begin{flushright}";
1237                         column += 18;
1238                 } else {
1239                         os << "\\begin{flushleft}";
1240                         column += 17;
1241                 }
1242                 break;
1243         case LYX_ALIGN_CENTER:
1244                 os << "\\begin{center}";
1245                 column += 14;
1246                 break;
1247         }
1248
1249         return column;
1250 }
1251
1252
1253 // This could go to ParagraphParameters if we want to
1254 int Paragraph::endTeXParParams(BufferParams const & bparams,
1255                                ostream & os, bool moving_arg) const
1256 {
1257         int column = 0;
1258
1259         switch (params().align()) {
1260         case LYX_ALIGN_NONE:
1261         case LYX_ALIGN_BLOCK:
1262         case LYX_ALIGN_LAYOUT:
1263         case LYX_ALIGN_SPECIAL:
1264                 break;
1265         case LYX_ALIGN_LEFT:
1266         case LYX_ALIGN_RIGHT:
1267         case LYX_ALIGN_CENTER:
1268                 if (moving_arg) {
1269                         os << "\\protect";
1270                         column = 8;
1271                 }
1272                 break;
1273         }
1274
1275         switch (params().align()) {
1276         case LYX_ALIGN_NONE:
1277         case LYX_ALIGN_BLOCK:
1278         case LYX_ALIGN_LAYOUT:
1279         case LYX_ALIGN_SPECIAL:
1280                 break;
1281         case LYX_ALIGN_LEFT:
1282                 if (getParLanguage(bparams)->babel() != "hebrew") {
1283                         os << "\\end{flushleft}";
1284                         column = 15;
1285                 } else {
1286                         os << "\\end{flushright}";
1287                         column = 16;
1288                 }
1289                 break;
1290         case LYX_ALIGN_RIGHT:
1291                 if (getParLanguage(bparams)->babel() != "hebrew") {
1292                         os << "\\end{flushright}";
1293                         column+= 16;
1294                 } else {
1295                         os << "\\end{flushleft}";
1296                         column = 15;
1297                 }
1298                 break;
1299         case LYX_ALIGN_CENTER:
1300                 os << "\\end{center}";
1301                 column = 12;
1302                 break;
1303         }
1304         return column;
1305 }
1306
1307
1308 // This one spits out the text of the paragraph
1309 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1310                                 BufferParams const & bparams,
1311                                 ostream & os, TexRow & texrow,
1312                                 bool moving_arg)
1313 {
1314         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
1315
1316         bool return_value = false;
1317
1318         LyXLayout_ptr style;
1319
1320         // well we have to check if we are in an inset with unlimited
1321         // lenght (all in one row) if that is true then we don't allow
1322         // any special options in the paragraph and also we don't allow
1323         // any environment other then "Standard" to be valid!
1324         bool asdefault =
1325                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1326
1327         if (asdefault) {
1328                 style = bparams.getLyXTextClass().defaultLayout();
1329         } else {
1330                 style = layout();
1331         }
1332
1333         LyXFont basefont;
1334
1335         // Maybe we have to create a optional argument.
1336         pos_type main_body;
1337         if (style->labeltype != LABEL_MANUAL)
1338                 main_body = 0;
1339         else
1340                 main_body = beginningOfMainBody();
1341
1342         int column = 0;
1343
1344         if (main_body > 0) {
1345                 os << '[';
1346                 ++column;
1347                 basefont = getLabelFont(bparams);
1348         } else {
1349                 basefont = getLayoutFont(bparams);
1350         }
1351
1352         moving_arg |= style->needprotect;
1353
1354         // Which font is currently active?
1355         LyXFont running_font(basefont);
1356         // Do we have an open font change?
1357         bool open_font = false;
1358
1359         texrow.start(this, 0);
1360
1361         // if the paragraph is empty, the loop will not be entered at all
1362         if (empty()) {
1363                 if (style->isCommand()) {
1364                         os << '{';
1365                         ++column;
1366                 }
1367                 if (!asdefault)
1368                         column += startTeXParParams(bparams, os, moving_arg);
1369
1370         }
1371
1372         for (pos_type i = 0; i < size(); ++i) {
1373                 ++column;
1374                 // First char in paragraph or after label?
1375                 if (i == main_body) {
1376                         if (main_body > 0) {
1377                                 if (open_font) {
1378                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
1379                                         open_font = false;
1380                                 }
1381                                 basefont = getLayoutFont(bparams);
1382                                 running_font = basefont;
1383                                 os << ']';
1384                                 ++column;
1385                         }
1386                         if (style->isCommand()) {
1387                                 os << '{';
1388                                 ++column;
1389                         }
1390
1391                         if (!asdefault)
1392                                 column += startTeXParParams(bparams, os,
1393                                                             moving_arg);
1394                 }
1395
1396                 value_type c = getChar(i);
1397
1398                 // Fully instantiated font
1399                 LyXFont font = getFont(bparams, i);
1400
1401                 LyXFont const last_font = running_font;
1402
1403                 // Spaces at end of font change are simulated to be
1404                 // outside font change, i.e. we write "\textXX{text} "
1405                 // rather than "\textXX{text }". (Asger)
1406                 if (open_font && c == ' ' && i <= size() - 2) {
1407                         LyXFont const & next_font = getFont(bparams, i + 1);
1408                         if (next_font != running_font
1409                             && next_font != font) {
1410                                 font = next_font;
1411                         }
1412                 }
1413
1414                 // We end font definition before blanks
1415                 if (open_font &&
1416                     (font != running_font ||
1417                      font.language() != running_font.language()))
1418                 {
1419                         column += running_font.latexWriteEndChanges(os,
1420                                                                     basefont,
1421                                                                     (i == main_body-1) ? basefont : font);
1422                         running_font = basefont;
1423                         open_font = false;
1424                 }
1425
1426                 // Blanks are printed before start of fontswitch
1427                 if (c == ' ') {
1428                         // Do not print the separation of the optional argument
1429                         if (i != main_body - 1) {
1430                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1431                                                        column, font, *style);
1432                         }
1433                 }
1434
1435                 // Do we need to change font?
1436                 if ((font != running_font ||
1437                      font.language() != running_font.language()) &&
1438                         i != main_body - 1)
1439                 {
1440                         column += font.latexWriteStartChanges(os, basefont,
1441                                                               last_font);
1442                         running_font = font;
1443                         open_font = true;
1444                 }
1445
1446                 if (c == Paragraph::META_NEWLINE) {
1447                         // newlines are handled differently here than
1448                         // the default in SimpleTeXSpecialChars().
1449                         if (!style->newline_allowed) {
1450                                 os << '\n';
1451                         } else {
1452                                 if (open_font) {
1453                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
1454                                         open_font = false;
1455                                 }
1456                                 basefont = getLayoutFont(bparams);
1457                                 running_font = basefont;
1458                                 if (font.family() ==
1459                                     LyXFont::TYPEWRITER_FAMILY) {
1460                                         os << "~";
1461                                 }
1462                                 if (moving_arg)
1463                                         os << "\\protect ";
1464
1465                                 os << "\\\\\n";
1466                         }
1467                         texrow.newline();
1468                         texrow.start(this, i + 1);
1469                         column = 0;
1470                 } else {
1471                         pimpl_->simpleTeXSpecialChars(buf, bparams,
1472                                                       os, texrow, moving_arg,
1473                                                       font, running_font,
1474                                                       basefont, open_font,
1475                                                       *style, i, column, c);
1476                 }
1477         }
1478
1479         // If we have an open font definition, we have to close it
1480         if (open_font) {
1481 #ifdef FIXED_LANGUAGE_END_DETECTION
1482                 if (next_) {
1483                         running_font
1484                                 .latexWriteEndChanges(os, basefont,
1485                                                       next_->getFont(bparams,
1486                                                       0));
1487                 } else {
1488                         running_font.latexWriteEndChanges(os, basefont,
1489                                                           basefont);
1490                 }
1491 #else
1492 #ifdef WITH_WARNINGS
1493 //#warning For now we ALWAYS have to close the foreign font settings if they are
1494 //#warning there as we start another \selectlanguage with the next paragraph if
1495 //#warning we are in need of this. This should be fixed sometime (Jug)
1496 #endif
1497                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1498 #endif
1499         }
1500
1501         // Needed if there is an optional argument but no contents.
1502         if (main_body > 0 && main_body == size()) {
1503                 os << "]~";
1504                 return_value = false;
1505         }
1506
1507         if (!asdefault) {
1508                 column += endTeXParParams(bparams, os, moving_arg);
1509         }
1510
1511         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1512         return return_value;
1513 }
1514
1515
1516 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1517                                             BufferParams const & bparams,
1518                                             ostream & os, TexRow & texrow)
1519 {
1520         lyxerr[Debug::LATEX] << "TeXEnvironment...     " << this << endl;
1521
1522         LyXLayout_ptr const & style = layout();
1523
1524         Language const * language = getParLanguage(bparams);
1525         Language const * doc_language = bparams.language;
1526         Language const * previous_language = previous_
1527                 ? previous_->getParLanguage(bparams) : doc_language;
1528         if (language->babel() != previous_language->babel()) {
1529
1530                 if (!lyxrc.language_command_end.empty() &&
1531                     previous_language->babel() != doc_language->babel()) {
1532                         os << subst(lyxrc.language_command_end, "$$lang",
1533                                     previous_language->babel())
1534                            << endl;
1535                         texrow.newline();
1536                 }
1537
1538                 if (lyxrc.language_command_end.empty() ||
1539                     language->babel() != doc_language->babel()) {
1540                         os << subst(lyxrc.language_command_begin, "$$lang",
1541                                     language->babel())
1542                            << endl;
1543                         texrow.newline();
1544                 }
1545         }
1546
1547         bool leftindent_open = false;
1548         if (!params().leftIndent().zero()) {
1549                 os << "\\begin{LyXParagraphLeftIndent}{" <<
1550                         params().leftIndent().asLatexString() << "}\n";
1551                 texrow.newline();
1552                 leftindent_open = true;
1553         }
1554
1555         if (style->isEnvironment()) {
1556                 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
1557                         os << "\\begin{" << style->latexname() << "}{"
1558                            << params().labelWidthString() << "}\n";
1559                 } else if (style->labeltype == LABEL_BIBLIO) {
1560                         // ale970405
1561                         os << "\\begin{" << style->latexname() << "}{"
1562                            <<  bibitemWidest(buf)
1563                            << "}\n";
1564                 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1565                         os << "\\begin{" << style->latexname() << '}'
1566                            << style->latexparam() << '\n';
1567                 } else
1568                         os << "\\begin{" << style->latexname() << '}'
1569                            << style->latexparam() << '\n';
1570                 texrow.newline();
1571         }
1572         Paragraph * par = this;
1573         do {
1574                 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1575
1576                 if (par && par->params().depth() > params().depth()) {
1577                             if (par->layout()->isParagraph()) {
1578
1579                             // Thinko!
1580                             // How to handle this? (Lgb)
1581                             //&& !suffixIs(os, "\n\n")
1582                                     //) {
1583                                 // There should be at least one '\n' already
1584                                 // but we need there to be two for Standard
1585                                 // paragraphs that are depth-increment'ed to be
1586                                 // output correctly.  However, tables can
1587                                 // also be paragraphs so don't adjust them.
1588                                 // ARRae
1589                                 // Thinkee:
1590                                 // Will it ever harm to have one '\n' too
1591                                 // many? i.e. that we sometimes will have
1592                                 // three in a row. (Lgb)
1593                                 os << '\n';
1594                                 texrow.newline();
1595                         }
1596                         par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1597                 }
1598         } while (par
1599                  && par->layout() == layout()
1600                  && par->params().depth() == params().depth()
1601                  && par->params().leftIndent() == params().leftIndent());
1602
1603         if (style->isEnvironment()) {
1604                 os << "\\end{" << style->latexname() << "}\n";
1605                 texrow.newline();
1606         }
1607
1608         if (leftindent_open) {
1609                 os << "\\end{LyXParagraphLeftIndent}\n";
1610                 texrow.newline();
1611         }
1612
1613         lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1614         return par;  // ale970302
1615 }
1616
1617
1618 bool Paragraph::isHfill(pos_type pos) const
1619 {
1620         return IsHfillChar(getChar(pos));
1621 }
1622
1623
1624 bool Paragraph::isInset(pos_type pos) const
1625 {
1626         return IsInsetChar(getChar(pos));
1627 }
1628
1629
1630 bool Paragraph::isNewline(pos_type pos) const
1631 {
1632         return pos >= 0 && IsNewlineChar(getChar(pos));
1633 }
1634
1635
1636 bool Paragraph::isSeparator(pos_type pos) const
1637 {
1638         return IsSeparatorChar(getChar(pos));
1639 }
1640
1641
1642 bool Paragraph::isLineSeparator(pos_type pos) const
1643 {
1644         value_type const c = getChar(pos);
1645         return IsLineSeparatorChar(c)
1646                 || (IsInsetChar(c) && getInset(pos) &&
1647                 getInset(pos)->isLineSeparator());
1648 }
1649
1650
1651 bool Paragraph::isKomma(pos_type pos) const
1652 {
1653         return IsKommaChar(getChar(pos));
1654 }
1655
1656
1657 /// Used by the spellchecker
1658 bool Paragraph::isLetter(pos_type pos) const
1659 {
1660         value_type const c = getChar(pos);
1661         if (IsLetterChar(c))
1662                 return true;
1663         if (isInset(pos))
1664                 return getInset(pos)->isLetter();
1665         // We want to pass the ' and escape chars to ispell
1666         string const extra = lyxrc.isp_esc_chars + '\'';
1667         return contains(extra, c);
1668 }
1669
1670
1671 bool Paragraph::isWord(pos_type pos) const
1672 {
1673         return IsWordChar(getChar(pos)) ;
1674 }
1675
1676
1677 Language const *
1678 Paragraph::getParLanguage(BufferParams const & bparams) const
1679 {
1680         if (!empty()) {
1681                 return getFirstFontSettings().language();
1682         } else if (previous_)
1683                 return previous_->getParLanguage(bparams);
1684         else
1685                 return bparams.language;
1686 }
1687
1688
1689 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1690 {
1691         return lyxrc.rtl_support
1692                 && getParLanguage(bparams)->RightToLeft()
1693                 && !(inInset() && inInset()->owner() &&
1694                      inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1695 }
1696
1697
1698 void Paragraph::changeLanguage(BufferParams const & bparams,
1699                                   Language const * from, Language const * to)
1700 {
1701         for (pos_type i = 0; i < size(); ++i) {
1702                 LyXFont font = getFontSettings(bparams, i);
1703                 if (font.language() == from) {
1704                         font.setLanguage(to);
1705                         setFont(i, font);
1706                 }
1707         }
1708 }
1709
1710
1711 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1712 {
1713         Language const * doc_language = bparams.language;
1714         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1715         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1716
1717         for (; cit != end; ++cit)
1718                 if (cit->font().language() != ignore_language &&
1719                     cit->font().language() != latex_language &&
1720                         cit->font().language() != doc_language)
1721                         return true;
1722         return false;
1723 }
1724
1725
1726 // Convert the paragraph to a string.
1727 // Used for building the table of contents
1728 string const Paragraph::asString(Buffer const * buffer, bool label)
1729 {
1730         BufferParams const & bparams = buffer->params;
1731         string s;
1732         if (label && !params().labelString().empty())
1733                 s += params().labelString() + ' ';
1734         string::size_type const len = s.size();
1735
1736         for (pos_type i = 0; i < size(); ++i) {
1737                 value_type c = getChar(i);
1738                 if (IsPrintable(c))
1739                         s += c;
1740                 else if (c == META_INSET &&
1741                          getInset(i)->lyxCode() == Inset::MATH_CODE) {
1742                         ostringstream ost;
1743                         getInset(i)->ascii(buffer, ost);
1744                         s += subst(ost.str().c_str(),'\n',' ');
1745                 }
1746         }
1747
1748         if (isRightToLeftPar(bparams))
1749                 reverse(s.begin() + len,s.end());
1750
1751         return s;
1752 }
1753
1754
1755 string const Paragraph::asString(Buffer const * buffer,
1756                                  pos_type beg, pos_type end, bool label)
1757 {
1758         ostringstream ost;
1759
1760         if (beg == 0 && label && !params().labelString().empty())
1761                 ost << params().labelString() << ' ';
1762
1763         for (pos_type i = beg; i < end; ++i) {
1764                 value_type const c = getUChar(buffer->params, i);
1765                 if (IsPrintable(c))
1766                         ost << c;
1767                 else if (c == META_NEWLINE)
1768                         ost << '\n';
1769                 else if (c == META_HFILL)
1770                         ost << '\t';
1771                 else if (c == META_INSET) {
1772                         getInset(i)->ascii(buffer, ost);
1773                 }
1774         }
1775
1776         return ost.str().c_str();
1777 }
1778
1779
1780 void Paragraph::setInsetOwner(Inset * i)
1781 {
1782         pimpl_->inset_owner = i;
1783         InsetList::iterator it = insetlist.begin();
1784         InsetList::iterator end = insetlist.end();
1785         for (; it != end; ++it) {
1786                 if (it.getInset())
1787                         it.getInset()->setOwner(i);
1788         }
1789 }
1790
1791
1792 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1793 {
1794         // then the insets
1795         insetlist.deleteInsetsLyXText(bv);
1796 }
1797
1798
1799 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1800 {
1801         // then the insets
1802         insetlist.resizeInsetsLyXText(bv);
1803 }
1804
1805
1806 void Paragraph::setContentsFromPar(Paragraph * par)
1807 {
1808         pimpl_->setContentsFromPar(par);
1809 }
1810
1811
1812 lyx::pos_type Paragraph::size() const
1813 {
1814         return pimpl_->size();
1815 }
1816
1817
1818 bool Paragraph::empty() const
1819 {
1820         return pimpl_->empty();
1821 }
1822
1823
1824 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1825 {
1826         return pimpl_->getChar(pos);
1827 }
1828
1829
1830 int Paragraph::id() const
1831 {
1832         return pimpl_->id_;
1833 }
1834
1835
1836 LyXLayout_ptr const & Paragraph::layout() const
1837 {
1838         return layout_;
1839 }
1840
1841
1842 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1843 {
1844         layout_ = new_layout;
1845 }
1846
1847
1848 bool Paragraph::isFirstInSequence() const
1849 {
1850         Paragraph const * dhook = depthHook(getDepth());
1851         return (dhook == this
1852                 || dhook->layout() != layout()
1853                 || dhook->getDepth() != getDepth());
1854 }
1855
1856
1857 Inset * Paragraph::inInset() const
1858 {
1859         return pimpl_->inset_owner;
1860 }
1861
1862
1863 void Paragraph::clearContents()
1864 {
1865         pimpl_->clear();
1866 }
1867
1868 void Paragraph::setChar(pos_type pos, value_type c)
1869 {
1870         pimpl_->setChar(pos, c);
1871 }
1872
1873
1874 ParagraphParameters & Paragraph::params()
1875 {
1876         return pimpl_->params;
1877 }
1878
1879
1880 ParagraphParameters const & Paragraph::params() const
1881 {
1882         return pimpl_->params;
1883 }
1884
1885
1886 Paragraph * Paragraph::getParFromID(int id) const
1887 {
1888         return pimpl_->getParFromID(id);
1889 }
1890
1891
1892 bool Paragraph::isFreeSpacing() const
1893 {
1894         // for now we just need this, later should we need this in some
1895         // other way we can always add a function to Inset::() too.
1896         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1897                 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);
1898         return false;
1899 }